diff --git a/sale_order_discount/__init__.py b/sale_order_discount/__init__.py new file mode 100644 index 00000000000..9b4296142f4 --- /dev/null +++ b/sale_order_discount/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/sale_order_discount/__manifest__.py b/sale_order_discount/__manifest__.py new file mode 100644 index 00000000000..bf51253ac64 --- /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'], + 'auto_install': True, + 'author': "times", + '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..a49a17f1a28 --- /dev/null +++ b/sale_order_discount/models/sale_order.py @@ -0,0 +1,62 @@ +import re +from odoo import _, api, fields, models +from odoo.tools import float_repr + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + + global_discount_percentage = fields.Float() + + @api.onchange('order_line') + def _onchange_update_global_discount(self): + discount_lines = self.env['sale.order.line'] + product_lines = self.env['sale.order.line'] + + for line in self.order_line: + if line._is_global_discount(): + discount_lines += line + if not line._is_global_discount(): + product_lines += line + + if not discount_lines: + return + + if not product_lines: + self.order_line -= discount_lines + return + + subtotal = sum(product_lines.mapped('price_subtotal')) + + for discount_line in discount_lines: + + discount_dp = self.env['decimal.precision'].precision_get('Discount') + origin_record = self.env['sale.order'].browse(self.id.origin) + + match = re.search(r"(\d+(?:\.\d+)?)", discount_line.name) + discount_from_name = float(match.group(1)) if match else False + + if not discount_from_name: + self.env.user._bus_send("simple_notification", { + 'type': 'danger', + 'title': _("Error"), + 'message': _("Discounts should be in Float(0.00%)") + }) + percent = origin_record.global_discount_percentage + + discount_line.name = _( + "Discount %(percent)s%%", + percent=float_repr(percent, discount_dp), + ) + return + + if discount_from_name != self.global_discount_percentage: + origin_record.global_discount_percentage = discount_from_name + + percent = origin_record.global_discount_percentage + + discount_line.name = _( + "Discount %(percent)s%%", + percent=float_repr(percent, discount_dp), + ) + discount_line.price_unit = -(subtotal * (percent / 100)) diff --git a/sale_order_discount/wizard/__init__.py b/sale_order_discount/wizard/__init__.py new file mode 100644 index 00000000000..d156570a8be --- /dev/null +++ b/sale_order_discount/wizard/__init__.py @@ -0,0 +1 @@ +from . import sale_order_discount diff --git a/sale_order_discount/wizard/sale_order_discount.py b/sale_order_discount/wizard/sale_order_discount.py new file mode 100644 index 00000000000..5c5582039ea --- /dev/null +++ b/sale_order_discount/wizard/sale_order_discount.py @@ -0,0 +1,10 @@ +from odoo import models + + +class SaleOrderDiscount(models.TransientModel): + _inherit = 'sale.order.discount' + + def _prepare_global_discount_so_lines(self, base_lines): + res = super()._prepare_global_discount_so_lines(base_lines) + self.sale_order_id.global_discount_percentage = self.discount_percentage * 100 + return res