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.
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
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.
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.
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
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:
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
Let me know if this helps!
Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!
AanmeldenGerelateerde posts | Antwoorden | Weergaven | Activiteit | |
---|---|---|---|---|
One2many field displaying KeyError
Opgelost
|
|
1
jun. 24
|
1652 | |
|
1
jul. 25
|
545 | ||
|
1
jan. 25
|
1819 | ||
|
0
nov. 24
|
1256 | ||
|
0
okt. 24
|
1511 |