Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
1544 Visualizzazioni

Dear All,

​I would like to know how can I develop as the same as Odoo Original which is calculated of Sale Order Line Total Amount for my custom one2many fields of total amount.



 

Avatar
Abbandona
Risposta migliore

Hello Gladiator, 

Here’s how Odoo calculates the total amount on sales orders, and if I’ve understood your requirement, here’s how you can do the same for your custom One2many field.

How Odoo Does It:

In Odoo, the total amount in the sales order is calculated using a computed field in sale.order.line, which multiplies the unit price and quantity to get the subtotal for each line. Then, it sums the subtotals to get the total for the entire sales order.

Here’s a simplified version of the code Odoo uses:

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    price_subtotal = fields.Monetary(compute='_compute_amount', string='Subtotal')

    @api.depends('product_uom_qty', 'price_unit')
    def _compute_amount(self):
        for line in self:
            line.price_subtotal = line.price_unit * line.product_uom_qty
How You Can Do It:

If you want to do something similar for your custom One2many field, you can create a computed field to calculate the total amount for your custom model. Here’s an example of how you can implement it:

  1. For the custom order lines, you calculate the subtotal for each line (quantity * unit price).
  2. For the main record, you sum all the subtotals to get the total amount.
Example Code for Your Custom Model:
from odoo import models, fields, api

class MyCustomOrder(models.Model):
    _name = 'my.custom.order'

    order_line_ids = fields.One2many('my.custom.order.line', 'order_id', string="Order Lines")
    total_amount = fields.Monetary(string="Total", compute="_compute_total_amount")

    @api.depends('order_line_ids.price_subtotal')
    def _compute_total_amount(self):
        for order in self:
            order.total_amount = sum(line.price_subtotal for line in order.order_line_ids)


class MyCustomOrderLine(models.Model):
    _name = 'my.custom.order.line'

    order_id = fields.Many2one('my.custom.order', string="Order Reference", ondelete='cascade')
    price_unit = fields.Float(string='Unit Price', required=True)
    quantity = fields.Float(string='Quantity', required=True, default=1.0)
    price_subtotal = fields.Float(string='Subtotal', compute='_compute_price_subtotal')

    @api.depends('price_unit', 'quantity')
    def _compute_price_subtotal(self):
        for line in self:
            line.price_subtotal = line.price_unit * line.quantity
In Short:
  • Subtotal for each line = quantity * unit price.
  • Total amount for the entire record = sum of all line subtotals.

Let me know if this helps!

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
giu 24
1700
1
lug 25
591
1
gen 25
1850
0
nov 24
1271
0
ott 24
1542