[IMP] product: extract pricelist item price computation into its model

The pricelist item model (product.pricelist.item) has a compute_price field,
and the necessary fields to be able to compute the price of a product.
However, these fields are never used into the model itself;
the parent model (product.pricelist) actually makes the computation,
embedded into _compute_price_rule.
Because of this, it's not possible to inherit pricelist items.
It's also a better design, on object principles.
Last, it lighten a method that is still 130+ lines long.

opw 2368733

X-original-commit: 9cd0bf740706a64e49599111304a6fb5ec6d7287
This commit is contained in:
nans
2020-11-02 11:54:10 +00:00
parent 4f3af25a15
commit 922dbe7de0
+31 -24
View File
@@ -220,31 +220,8 @@ class Pricelist(models.Model):
# price_compute returns the price in the context UoM, i.e. qty_uom_id
price = product.price_compute(rule.base)[product.id]
convert_to_price_uom = (lambda price: product.uom_id._compute_price(price, price_uom))
if price is not False:
if rule.compute_price == 'fixed':
price = convert_to_price_uom(rule.fixed_price)
elif rule.compute_price == 'percentage':
price = (price - (price * (rule.percent_price / 100))) or 0.0
else:
# complete formula
price_limit = price
price = (price - (price * (rule.price_discount / 100))) or 0.0
if rule.price_round:
price = tools.float_round(price, precision_rounding=rule.price_round)
if rule.price_surcharge:
price_surcharge = convert_to_price_uom(rule.price_surcharge)
price += price_surcharge
if rule.price_min_margin:
price_min_margin = convert_to_price_uom(rule.price_min_margin)
price = max(price, price_limit + price_min_margin)
if rule.price_max_margin:
price_max_margin = convert_to_price_uom(rule.price_max_margin)
price = min(price, price_limit + price_max_margin)
price = rule._compute_price(price, price_uom, product, quantity=qty, partner=partner)
suitable_rule = rule
break
# Final price conversion into pricelist currency
@@ -601,3 +578,33 @@ class PricelistItem(models.Model):
self.flush()
self.invalidate_cache()
return res
def _compute_price(self, price, price_uom, product, quantity=1.0, partner=False):
"""Compute the unit price of a product in the context of a pricelist application.
The unused parameters are there to make the full context available for overrides.
"""
self.ensure_one()
convert_to_price_uom = (lambda price: product.uom_id._compute_price(price, price_uom))
if self.compute_price == 'fixed':
price = convert_to_price_uom(self.fixed_price)
elif self.compute_price == 'percentage':
price = (price - (price * (self.percent_price / 100))) or 0.0
else:
# complete formula
price_limit = price
price = (price - (price * (self.price_discount / 100))) or 0.0
if self.price_round:
price = tools.float_round(price, precision_rounding=self.price_round)
if self.price_surcharge:
price_surcharge = convert_to_price_uom(self.price_surcharge)
price += price_surcharge
if self.price_min_margin:
price_min_margin = convert_to_price_uom(self.price_min_margin)
price = max(price, price_limit + price_min_margin)
if self.price_max_margin:
price_max_margin = convert_to_price_uom(self.price_max_margin)
price = min(price, price_limit + price_max_margin)
return price