From d91be06b816353c7f030e2b69e07bac68914a19d Mon Sep 17 00:00:00 2001 From: "Louis (loti)" Date: Wed, 29 Nov 2023 16:23:55 +0100 Subject: [PATCH] [IMP] sale: allow batch removal of discounts This change allows to remove discounts from all sale order lines at once, by enabling 0% discounts in the discount wizard. task-3584450 closes odoo/odoo#144244 Signed-off-by: Victor Feyens (vfe) --- addons/sale/tests/test_sale_order_discount.py | 24 +++++++++++++++++++ addons/sale/wizard/sale_order_discount.py | 6 ++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/addons/sale/tests/test_sale_order_discount.py b/addons/sale/tests/test_sale_order_discount.py index 5dbcabe85e6..8158dace889 100644 --- a/addons/sale/tests/test_sale_order_discount.py +++ b/addons/sale/tests/test_sale_order_discount.py @@ -1,5 +1,6 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo.exceptions import ValidationError from odoo.fields import Command from odoo.tests import tagged @@ -83,3 +84,26 @@ class TestSaleOrderDiscount(SaleCommon): all(line.discount == 50 for line in self.sale_order.order_line) ) self.assertAlmostEqual(self.sale_order.amount_untaxed, so_amount*0.5) + + self.wizard.write({'discount_percentage': -0.5}) + self.wizard.action_apply_discount() + + self.assertTrue( + all(line.discount == -50 for line in self.sale_order.order_line) + ) + self.assertAlmostEqual(self.sale_order.amount_untaxed, so_amount*1.5) + + def test_sol_discount_removal(self): + so_amount = self.sale_order.amount_untaxed + self.wizard.write({'discount_percentage': 0.5, 'discount_type': 'sol_discount'}) + self.wizard.action_apply_discount() + + self.wizard.write({'discount_percentage': 0}) + self.wizard.action_apply_discount() + + self.assertFalse(self.sale_order.order_line.filtered('discount')) + self.assertAlmostEqual(self.sale_order.amount_untaxed, so_amount) + + def test_percent_discount_above_100(self): + with self.assertRaises(ValidationError): + self.wizard.write({'discount_percentage': 1.1, 'discount_type': 'sol_discount'}) diff --git a/addons/sale/wizard/sale_order_discount.py b/addons/sale/wizard/sale_order_discount.py index 5d607ba0b94..e8356554cae 100644 --- a/addons/sale/wizard/sale_order_discount.py +++ b/addons/sale/wizard/sale_order_discount.py @@ -30,9 +30,9 @@ class SaleOrderDiscount(models.TransientModel): @api.constrains('discount_type', 'discount_percentage') def _check_discount_amount(self): for wizard in self: - if wizard.discount_type in ('sol_discount', 'so_discount') and ( - wizard.discount_percentage <= 0.0 - or wizard.discount_percentage > 1.0 + if ( + wizard.discount_type in ('sol_discount', 'so_discount') + and wizard.discount_percentage > 1.0 ): raise ValidationError(_("Invalid discount amount"))