diff --git a/product_sale_price_from_pricelist/models/__init__.py b/product_sale_price_from_pricelist/models/__init__.py index 8d222ed..f4e7395 100644 --- a/product_sale_price_from_pricelist/models/__init__.py +++ b/product_sale_price_from_pricelist/models/__init__.py @@ -1,5 +1,6 @@ from . import product_pricelist from . import product_pricelist_item +from . import product_product from . import product_template from . import res_config from . import stock_move diff --git a/product_sale_price_from_pricelist/models/product_pricelist.py b/product_sale_price_from_pricelist/models/product_pricelist.py index b309ee0..72400e4 100644 --- a/product_sale_price_from_pricelist/models/product_pricelist.py +++ b/product_sale_price_from_pricelist/models/product_pricelist.py @@ -15,7 +15,12 @@ class ProductPricelist(models.Model): def _compute_price_rule(self, products, qty, uom=None, date=False, **kwargs): ProductPricelistItem = self.env["product.pricelist.item"] ProductProduct = self.env["product.product"] - res = super()._compute_price_rule(products, date=date, qty=1) + res = super()._compute_price_rule( + products, + qty=1, + uom=uom, + date=date, + ) new_res = res.copy() item_id = [] for product_id, values in res.items(): diff --git a/product_sale_price_from_pricelist/models/product_pricelist_item.py b/product_sale_price_from_pricelist/models/product_pricelist_item.py index 136ce81..2b6581d 100644 --- a/product_sale_price_from_pricelist/models/product_pricelist_item.py +++ b/product_sale_price_from_pricelist/models/product_pricelist_item.py @@ -9,6 +9,12 @@ class ProductPricelistItem(models.Model): _inherit = "product.pricelist.item" base = fields.Selection( - selection_add=[("last_purchase_price_received", "Last purchase price")], - ondelete={"last_purchase_price_received": "set default"}, + selection_add=[("last_purchase_price", "Last purchase price")], + ondelete={"last_purchase_price": "set default"}, ) + + def _compute_price(self, product, quantity, uom, date, currency=None): + result = super()._compute_price(product, quantity, uom, date, currency) + if self.compute_price == "formula" and self.base == "last_purchase_price": + result = product.sudo().last_purchase_price_received + return result diff --git a/product_sale_price_from_pricelist/models/product_product.py b/product_sale_price_from_pricelist/models/product_product.py new file mode 100644 index 0000000..f867604 --- /dev/null +++ b/product_sale_price_from_pricelist/models/product_product.py @@ -0,0 +1,22 @@ +from odoo import models + + +class ProductProduct(models.Model): + _inherit = "product.product" + + def price_compute( + self, price_type, uom=None, currency=None, company=None, date=False + ): + """Return dummy not falsy prices when computation is done from supplier + info for avoiding error on super method. We will later fill these with + correct values. + """ + if price_type == "last_purchase_price": + return dict.fromkeys(self.ids, 1.0) + return super().price_compute( + price_type, + uom=uom, + currency=currency, + company=company, + date=date, + ) diff --git a/product_sale_price_from_pricelist/models/product_template.py b/product_sale_price_from_pricelist/models/product_template.py index b2a1b01..5ccf188 100644 --- a/product_sale_price_from_pricelist/models/product_template.py +++ b/product_sale_price_from_pricelist/models/product_template.py @@ -18,7 +18,6 @@ class ProductTemplate(models.Model): ) list_price_theoritical = fields.Float( "Theoritical price", - readonly=True, company_dependent=True, ) last_purchase_price_received = fields.Float( @@ -66,8 +65,14 @@ class ProductTemplate(models.Model): partial_price = template.product_variant_id._get_price( qty=1, pricelist=pricelist ) - template.list_price_theoritical = partial_price["value"] - template.last_purchase_price_updated = True + template.write( + { + "list_price_theoritical": partial_price[ + template.product_variant_id.id + ]["value"], + "last_purchase_price_updated": True, + } + ) else: raise UserError( _( @@ -79,3 +84,16 @@ class ProductTemplate(models.Model): for template in self: template.list_price = template.list_price_theoritical template.last_purchase_price_updated = False + + def price_compute( + self, price_type, uom=None, currency=None, company=False, date=False + ): + """Return dummy not falsy prices when computation is done from supplier + info for avoiding error on super method. We will later fill these with + correct values. + """ + if price_type == "last_purchase_price": + return dict.fromkeys(self.ids, 1.0) + return super().price_compute( + price_type, uom=uom, currency=currency, company=company, date=date + )