From 3437c22f77b03fcb076f13d2b0f31065d404fc3c Mon Sep 17 00:00:00 2001 From: pkhu-odoo Date: Mon, 16 Feb 2026 18:44:25 +0530 Subject: [PATCH] [ADD] sale_management_discount: recalculate global discount Task Description - 1. Add multiple products to a Sale Order. 2. Apply a single global discount. 3. When order lines are updated or removed, the discount should automatically recalculate based on the current order lines. 4. If no order lines remain, the global discount should be removed/reset. 5. Handle single global discount only. - Created new module `global_discount_update` with required base files - Inherited `sale.order.line` model to handle order line modifications - Implemented `ondelete` logic to update/remove discount when lines are deleted - Overridden `write` method to recalculate discount when order lines are updated - Ensured discount is cleared when no order lines exist Task - 5928684 --- sale_management_discount/__init__.py | 1 + sale_management_discount/__manifest__.py | 8 ++++ sale_management_discount/models/__init__.py | 1 + .../models/sale_order_line.py | 40 +++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 sale_management_discount/__init__.py create mode 100644 sale_management_discount/__manifest__.py create mode 100644 sale_management_discount/models/__init__.py create mode 100644 sale_management_discount/models/sale_order_line.py diff --git a/sale_management_discount/__init__.py b/sale_management_discount/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/sale_management_discount/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/sale_management_discount/__manifest__.py b/sale_management_discount/__manifest__.py new file mode 100644 index 00000000000..de6091c9055 --- /dev/null +++ b/sale_management_discount/__manifest__.py @@ -0,0 +1,8 @@ +{ + 'name': "sale_management_discount", + 'author': "pkhu", + 'license': "LGPL-3", + 'depends': ['sale_management'], + 'data': [], + 'auto_install': True, +} diff --git a/sale_management_discount/models/__init__.py b/sale_management_discount/models/__init__.py new file mode 100644 index 00000000000..8eb9d1d4046 --- /dev/null +++ b/sale_management_discount/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order_line diff --git a/sale_management_discount/models/sale_order_line.py b/sale_management_discount/models/sale_order_line.py new file mode 100644 index 00000000000..0f5946503e5 --- /dev/null +++ b/sale_management_discount/models/sale_order_line.py @@ -0,0 +1,40 @@ +from odoo import api, models + + +class SaleOrderLine(models.Model): + _inherit = ['sale.order.line'] + + @api.ondelete(at_uninstall=True) + def _onDelete_sale_order_line(self): + self._update_global_discount('delete') + + def _get_global_discount_id(self): + return int(self.extra_tax_data.get( + "computation_key").split(",")[1]) + + def _update_global_discount(self, operation): + for record in self: + discount_order_line = record.order_id.order_line.filtered( + lambda o: o._is_global_discount()) + if discount_order_line and record.id not in discount_order_line.ids: + for val in discount_order_line: + DOL_id = val._get_global_discount_id() + domain = [('order_id', 'in', record.order_id), + ('id', 'not in', discount_order_line.ids)] + if operation == 'delete': + domain.append(('id', 'not in', record.ids)) + total_price = dict(record.env['sale.order.line']._read_group( + domain=domain, aggregates=['price_subtotal:sum'], groupby=['order_id'])).get(record.order_id, 0.0) + if total_price == 0: + val.unlink() + continue + discount_per = record.env['sale.order.discount'].search( + domain=[('sale_order_id', 'in', record.order_id), ('id', '=', DOL_id)]).discount_percentage + new_price = -(total_price * discount_per) + val.update({ + 'price_unit': new_price}) + + def write(self, vals): + res = super().write(vals) + self._update_global_discount('update') + return res