diff --git a/sale_order_discount/__init__.py b/sale_order_discount/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/sale_order_discount/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_order_discount/__manifest__.py b/sale_order_discount/__manifest__.py new file mode 100644 index 00000000000..83c62ae4e39 --- /dev/null +++ b/sale_order_discount/__manifest__.py @@ -0,0 +1,10 @@ +{ + 'name': "Sale Order Discount", + 'version': "1.0", + 'category': "Sales", + 'description': "Updates global discount when order lines are changed", + 'depends': ['sale_management'], + 'installable': True, + 'author': "jakan", + 'license': "LGPL-3", +} diff --git a/sale_order_discount/models/__init__.py b/sale_order_discount/models/__init__.py new file mode 100644 index 00000000000..6aacb753131 --- /dev/null +++ b/sale_order_discount/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order diff --git a/sale_order_discount/models/sale_order.py b/sale_order_discount/models/sale_order.py new file mode 100644 index 00000000000..9d8457e158f --- /dev/null +++ b/sale_order_discount/models/sale_order.py @@ -0,0 +1,47 @@ +import re +from odoo import api, models +from odoo.exceptions import UserError + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + + @api.onchange('order_line') + def _onchange_recalculate_global_discount(self): + discount_lines = self.env['sale.order.line'] + for line in self.order_line: + if line._is_global_discount(): + discount_lines += line + + if not discount_lines: + return + + product_lines = self.env['sale.order.line'] + for line in self.order_line: + if not line._is_global_discount(): + product_lines += line + + if not product_lines: + self.order_line -= discount_lines + return + + subtotal = 0 + for line in product_lines: + subtotal += line.price_subtotal + + for discount_line in discount_lines: + match = re.search(r"(\d+(?:\.\d+)?)%", discount_line.name) + if match: + percent = float(match.group(1)) + discount_line.price_unit = -(subtotal * percent / 100) + + @api.constrains('order_line') + def _check_single_global_discount(self): + for order in self: + discounts = order.order_line.filtered( + lambda l: l._is_global_discount() + ) + if len(discounts) > 1: + raise UserError( + "Only one global discount is allowed per order." + )