From b9fabffe4eda43ba7fb0fef341ecb0a74b292116 Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Tue, 12 Nov 2019 11:25:46 +0000 Subject: [PATCH] [IMP] sale_margin: make the cost a computed field. And clean the code mess. closes odoo/odoo#40953 Signed-off-by: Damien Bouvy (dbo) --- addons/sale/models/sale.py | 4 - addons/sale_management/models/sale_order.py | 2 - addons/sale_margin/models/sale_order.py | 96 +++++++------------- addons/sale_margin/tests/test_sale_margin.py | 8 +- 4 files changed, 38 insertions(+), 72 deletions(-) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index 6c13d57e855..c49c864e772 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -1091,10 +1091,6 @@ class SaleOrderLine(models.Model): taxes = line.product_id.taxes_id.filtered(lambda t: t.company_id == line.env.company) line.tax_id = fpos.map_tax(taxes, line.product_id, line.order_id.partner_shipping_id) - @api.model - def _get_purchase_price(self, pricelist, product, product_uom, date): - return {} - @api.model def _prepare_add_missing_fields(self, values): """ Deduce missing required fields from the onchange """ diff --git a/addons/sale_management/models/sale_order.py b/addons/sale_management/models/sale_order.py index cf594cc6d0a..3be4dfb202b 100644 --- a/addons/sale_management/models/sale_order.py +++ b/addons/sale_management/models/sale_order.py @@ -96,8 +96,6 @@ class SaleOrder(models.Model): 'product_uom': line.product_uom_id.id, 'customer_lead': self._get_customer_lead(line.product_id.product_tmpl_id), }) - if self.pricelist_id: - data.update(self.env['sale.order.line']._get_purchase_price(self.pricelist_id, line.product_id, line.product_uom_id, fields.Date.context_today(self))) order_lines.append((0, 0, data)) self.order_line = order_lines diff --git a/addons/sale_margin/models/sale_order.py b/addons/sale_margin/models/sale_order.py index 7a813e1b210..965ced7a778 100644 --- a/addons/sale_margin/models/sale_order.py +++ b/addons/sale_margin/models/sale_order.py @@ -7,75 +7,45 @@ from odoo import api, fields, models class SaleOrderLine(models.Model): _inherit = "sale.order.line" - margin = fields.Float(compute='_product_margin', digits='Product Price', store=True) - purchase_price = fields.Float(string='Cost', digits='Product Price') + margin = fields.Float(compute='_compute_margin', digits='Product Price', store=True) + purchase_price = fields.Float(string='Cost', compute="_compute_purchase_price", digits='Product Price', store=True, readonly=False) - def _compute_margin(self, order_id, product_id, product_uom_id): - frm_cur = self.env.company.currency_id - to_cur = order_id.pricelist_id.currency_id - purchase_price = product_id.standard_price - if product_uom_id != product_id.uom_id: - purchase_price = product_id.uom_id._compute_price(purchase_price, product_uom_id) - price = frm_cur._convert( - purchase_price, to_cur, order_id.company_id or self.env.company, - order_id.date_order or fields.Date.today(), round=False) - return price - - @api.model - def _get_purchase_price(self, pricelist, product, product_uom, date): - frm_cur = self.env.company.currency_id - to_cur = pricelist.currency_id - purchase_price = product.standard_price - if product_uom != product.uom_id: - purchase_price = product.uom_id._compute_price(purchase_price, product_uom) - price = frm_cur._convert( - purchase_price, to_cur, - self.order_id.company_id or self.env.company, - date or fields.Date.today(), round=False) - return {'purchase_price': price} - - @api.onchange('product_id', 'product_uom') - def product_id_change_margin(self): - if not self.order_id.pricelist_id or not self.product_id or not self.product_uom: - return - self.purchase_price = self._compute_margin(self.order_id, self.product_id, self.product_uom) - - @api.onchange('product_id') - def product_id_change(self): - # VFE FIXME : bugfix for matrix, the purchase_price will be changed to a computed field in master. - res = super(SaleOrderLine, self).product_id_change() - self.product_id_change_margin() - return res - - @api.model - def create(self, vals): - vals.update(self._prepare_add_missing_fields(vals)) - - # Calculation of the margin for programmatic creation of a SO line. It is therefore not - # necessary to call product_id_change_margin manually - if 'purchase_price' not in vals and ('display_type' not in vals or not vals['display_type']): - order_id = self.env['sale.order'].browse(vals['order_id']) - product_id = self.env['product.product'].browse(vals['product_id']) - product_uom_id = self.env['uom.uom'].browse(vals['product_uom']) - - vals['purchase_price'] = self._compute_margin(order_id, product_id, product_uom_id) - - return super(SaleOrderLine, self).create(vals) - - @api.depends('product_id', 'purchase_price', 'product_uom_qty', 'price_unit', 'price_subtotal') - def _product_margin(self): + @api.depends('product_id', 'company_id', 'currency_id', 'product_uom') + def _compute_purchase_price(self): for line in self: - currency = line.order_id.pricelist_id.currency_id - price = line.purchase_price - margin = line.price_subtotal - (price * line.product_uom_qty) - line.margin = currency.round(margin) if currency else margin + line = line.with_company(line.company_id) + product = line.product_id + product_cost = product.standard_price + fro_cur = product.cost_currency_id + to_cur = line.currency_id or line.order_id.currency_id + if line.product_uom != product.uom_id: + product_cost = product.uom_id._compute_price( + product_cost, + line.product_uom, + ) + line.purchase_price = fro_cur._convert( + from_amount=product_cost, + to_currency=to_cur, + company=line.company_id or self.env.company, + date=line.order_id.date_order or fields.Date.today(), + round=False, + ) if to_cur else product_cost + # The pricelist may not have been set, therefore no conversion + # is needed because we don't know the target currency.. + + @api.depends('price_subtotal', 'product_uom_qty', 'purchase_price') + def _compute_margin(self): + for line in self: + line.margin = line.price_subtotal - (line.purchase_price * line.product_uom_qty) + class SaleOrder(models.Model): _inherit = "sale.order" - margin = fields.Monetary(compute='_product_margin', help="It gives profitability by calculating the difference between the Unit Price and the cost.", currency_field='currency_id', store=True) + margin = fields.Monetary(compute='_compute_margin', store=True) @api.depends('order_line.margin') - def _product_margin(self): + def _compute_margin(self): for order in self: - order.margin = sum(order.order_line.filtered(lambda r: r.state != 'cancel').mapped('margin')) + order.margin = 0 if order.state == 'cancel' else \ + sum(order.order_line.mapped('margin')) diff --git a/addons/sale_margin/tests/test_sale_margin.py b/addons/sale_margin/tests/test_sale_margin.py index 52a5e36baa2..849c6eb11da 100644 --- a/addons/sale_margin/tests/test_sale_margin.py +++ b/addons/sale_margin/tests/test_sale_margin.py @@ -12,23 +12,26 @@ class TestSaleMargin(common.TransactionCase): self.SaleOrder = self.env['sale.order'] self.product_uom_id = self.ref('uom.product_uom_unit') - self.product_id = self.env['product.product'].create({'name': 'Individual Workplace'}).id + self.product = self.env['product.product'].create({'name': 'Individual Workplace'}) + self.product_id = self.product.id self.partner_id = self.env['res.partner'].create({'name': 'A test partner'}).id self.partner_invoice_address_id = self.env['res.partner'].create({ 'name': 'A test partner address', 'parent_id': self.partner_id, }).id self.pricelist_id = self.ref('product.list0') + self.pricelist = self.env.ref('product.list0') def test_sale_margin(self): """ Test the sale_margin module in Odoo. """ + self.pricelist.currency_id = self.env.company.currency_id + self.product.standard_price = 700.0 sale_order_so11 = self.SaleOrder.create({ 'date_order': datetime.today(), 'name': 'Test_SO011', 'order_line': [ (0, 0, { 'name': '[CARD] Individual Workplace', - 'purchase_price': 700.0, 'price_unit': 1000.0, 'product_uom': self.product_uom_id, 'product_uom_qty': 10.0, @@ -37,7 +40,6 @@ class TestSaleMargin(common.TransactionCase): (0, 0, { 'name': 'Line without product_uom', 'price_unit': 1000.0, - 'purchase_price': 700.0, 'product_uom_qty': 10.0, 'state': 'draft', 'product_id': self.product_id})],