diff --git a/addons/sale/models/sale_order.py b/addons/sale/models/sale_order.py index 5e4be166c0e..6681f31bf35 100644 --- a/addons/sale/models/sale_order.py +++ b/addons/sale/models/sale_order.py @@ -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 %s ", self.pricelist_id.display_name)) diff --git a/addons/sale/tests/test_sale_order.py b/addons/sale/tests/test_sale_order.py index 04f09818e01..b3ab8a628b4 100644 --- a/addons/sale/tests/test_sale_order.py +++ b/addons/sale/tests/test_sale_order.py @@ -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))