From 602712960a68460bbe13512db08814fd993db26e Mon Sep 17 00:00:00 2001 From: isgos-odoo Date: Wed, 18 Feb 2026 10:53:21 +0530 Subject: [PATCH] [ADD] po_global_discount: add module to apply global discounts on purchase orders Key Features: - Global Discount Wizard: Allows users to input a discount as either a fixed amount or a percentage. - Dual Calculation Logic: - Percentage: Directly applies the value to the 'discount' field on lines. - Fixed Amount: Calculates the equivalent percentage based on the total order value and distributes it across lines. - Validation: Displays a warning about potential rounding errors when using fixed amount mode. Technical Details: - Model: Created `purchase.global.discount` (TransientModel). - Views: Extended `purchase.order` form and created wizard form view. --- po_global_discount/__init__.py | 2 ++ po_global_discount/__manifest__.py | 17 +++++++++ po_global_discount/models/__init__.py | 1 + po_global_discount/models/purchase_order.py | 15 ++++++++ .../security/ir.model.access.csv | 2 ++ .../views/purchase_order_view.xml | 16 +++++++++ po_global_discount/wizard/__init__.py | 1 + .../wizard/purchase_global_discount.py | 36 +++++++++++++++++++ .../wizard/purchase_global_discount_view.xml | 36 +++++++++++++++++++ 9 files changed, 126 insertions(+) create mode 100644 po_global_discount/__init__.py create mode 100644 po_global_discount/__manifest__.py create mode 100644 po_global_discount/models/__init__.py create mode 100644 po_global_discount/models/purchase_order.py create mode 100644 po_global_discount/security/ir.model.access.csv create mode 100644 po_global_discount/views/purchase_order_view.xml create mode 100644 po_global_discount/wizard/__init__.py create mode 100644 po_global_discount/wizard/purchase_global_discount.py create mode 100644 po_global_discount/wizard/purchase_global_discount_view.xml diff --git a/po_global_discount/__init__.py b/po_global_discount/__init__.py new file mode 100644 index 00000000000..9b4296142f4 --- /dev/null +++ b/po_global_discount/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/po_global_discount/__manifest__.py b/po_global_discount/__manifest__.py new file mode 100644 index 00000000000..29268fbcdd2 --- /dev/null +++ b/po_global_discount/__manifest__.py @@ -0,0 +1,17 @@ +{ + 'name': 'Purchase global discount', + 'version': '0.1.0', + 'description': 'Add a global discount to the all purchase order lines', + 'depends': [ + 'purchase' + ], + 'data': [ + 'security/ir.model.access.csv', + 'views/purchase_order_view.xml', + 'wizard/purchase_global_discount_view.xml', + ], + 'installable': True, + 'application': True, + 'author': 'Ishwar', + 'license': 'LGPL-3' +} diff --git a/po_global_discount/models/__init__.py b/po_global_discount/models/__init__.py new file mode 100644 index 00000000000..9f03530643d --- /dev/null +++ b/po_global_discount/models/__init__.py @@ -0,0 +1 @@ +from . import purchase_order diff --git a/po_global_discount/models/purchase_order.py b/po_global_discount/models/purchase_order.py new file mode 100644 index 00000000000..f320c714d99 --- /dev/null +++ b/po_global_discount/models/purchase_order.py @@ -0,0 +1,15 @@ +from odoo import models + + +class PurchaseOrder(models.Model): + _inherit = 'purchase.order' + + def action_purchase_global_discount(self): + return { + 'name': 'Global Discount', + 'view_mode': 'form', + 'res_model': 'purchase.global.discount', + 'type': 'ir.actions.act_window', + 'target': 'new', + 'context': {'default_order_id': self.id}, + } diff --git a/po_global_discount/security/ir.model.access.csv b/po_global_discount/security/ir.model.access.csv new file mode 100644 index 00000000000..2cc761f1b50 --- /dev/null +++ b/po_global_discount/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_purchase_global_discount,purchase.global.discount,model_purchase_global_discount,base.group_user,1,1,1,1 diff --git a/po_global_discount/views/purchase_order_view.xml b/po_global_discount/views/purchase_order_view.xml new file mode 100644 index 00000000000..ba6b8a2de65 --- /dev/null +++ b/po_global_discount/views/purchase_order_view.xml @@ -0,0 +1,16 @@ + + + purchase.order.view.form + purchase.order + + + +
+ +
+
+
+
+
diff --git a/po_global_discount/wizard/__init__.py b/po_global_discount/wizard/__init__.py new file mode 100644 index 00000000000..3739f320df0 --- /dev/null +++ b/po_global_discount/wizard/__init__.py @@ -0,0 +1 @@ +from . import purchase_global_discount diff --git a/po_global_discount/wizard/purchase_global_discount.py b/po_global_discount/wizard/purchase_global_discount.py new file mode 100644 index 00000000000..3c899909936 --- /dev/null +++ b/po_global_discount/wizard/purchase_global_discount.py @@ -0,0 +1,36 @@ +from odoo import api, fields, models +from odoo.exceptions import UserError + + +class PurchaseGlobalDiscount(models.TransientModel): + _name = 'purchase.global.discount' + + order_id = fields.Many2one('purchase.order', required=True) + discount_value = fields.Float() + discount_type = fields.Selection( + [ + ("percentage", "%"), + ("amount", "$"), + ], + default="percentage" + ) + discount_percentage = fields.Float(compute="_compute_discount_percentage", string="Preview Percentage") + + @api.depends('discount_value', 'discount_type', 'order_id') + def _compute_discount_percentage(self): + for wizard in self: + wizard.discount_percentage = 0.0 + if wizard.discount_type == 'percentage': + wizard.discount_percentage = wizard.discount_value + elif wizard.discount_type == 'amount' and wizard.order_id: + total_amount = sum(line.price_unit * line.product_qty for line in wizard.order_id.order_line) + if total_amount > 0: + wizard.discount_percentage = (wizard.discount_value / total_amount) * 100 + + def action_apply_discount(self): + self.ensure_one() + if self.discount_percentage > 100: + raise UserError("Discount Must be less then 100% ") + for line in self.order_id.order_line: + line.discount = self.discount_percentage + return {'type': 'ir.actions.act_window_close'} diff --git a/po_global_discount/wizard/purchase_global_discount_view.xml b/po_global_discount/wizard/purchase_global_discount_view.xml new file mode 100644 index 00000000000..793518fffd7 --- /dev/null +++ b/po_global_discount/wizard/purchase_global_discount_view.xml @@ -0,0 +1,36 @@ + + + purchase.global.discount.form + purchase.global.discount + +
+ + + + + + + + +
+
+
+