[IMP] sale: delegate update_prices logic to the computes

Remove duplicated price & discount computation and trigger
the necessary computes.
This commit is contained in:
Victor Feyens
2021-12-23 15:30:23 +01:00
parent b4cc2df510
commit bb1cf7d7ce
2 changed files with 8 additions and 21 deletions
+7 -19
View File
@@ -524,25 +524,13 @@ class SaleOrder(models.Model):
def update_prices(self):
self.ensure_one()
lines_to_update = []
for line in self.order_line.filtered(lambda line: not line.display_type):
# TODO VFE remove price field usage
product = line.product_id.with_context(
partner=self.partner_id,
quantity=line.product_uom_qty,
date=self.date_order,
pricelist=self.pricelist_id.id,
uom=line.product_uom.id
)
price_unit = self.env['account.tax']._fix_tax_included_price_company(
line._get_display_price(product), line.product_id.taxes_id, line.tax_id, line.company_id)
if self.pricelist_id.discount_policy == 'without_discount' and price_unit:
price_discount_unrounded = self.pricelist_id.get_product_price(product, line.product_uom_qty, self.partner_id, self.date_order, line.product_uom.id)
discount = max(0, (price_unit - price_discount_unrounded) * 100 / price_unit)
else:
discount = 0
lines_to_update.append((1, line.id, {'price_unit': price_unit, 'discount': discount}))
self.update({'order_line': lines_to_update})
lines_to_recompute = self.order_line.filtered(lambda line: not line.display_type)
lines_to_recompute._compute_price_unit()
# Special case: we want to overwrite the existing discount on update_prices call
# i.e. to make sure the discount is correctly reset
# if pricelist discount_policy is different than when the price was first computed.
lines_to_recompute.discount = 0.0
lines_to_recompute._compute_discount()
self.show_update_pricelist = False
self.message_post(body=_("Product prices have been recomputed according to pricelist <b>%s<b> ", self.pricelist_id.display_name))
+1 -2
View File
@@ -682,8 +682,8 @@ class TestSaleOrder(TestSaleCommon):
'compute_price': 'percentage'
})
]
self.env.user.groups_id += self.env.ref('product.group_discount_per_so_line')
pricelist.discount_policy = "without_discount"
self.env['product.product'].invalidate_cache(['price'])
sale_order.update_prices()
self.assertTrue(all(line.discount == 5 for line in sale_order.order_line))
@@ -691,7 +691,6 @@ class TestSaleOrder(TestSaleCommon):
self.assertEqual(sale_order.amount_total, 0.95*so_amount)
pricelist.discount_policy = "with_discount"
self.env['product.product'].invalidate_cache(['price'])
sale_order.update_prices()
self.assertTrue(all(line.discount == 0 for line in sale_order.order_line))