diff --git a/purchase_order_discount/__init__.py b/purchase_order_discount/__init__.py new file mode 100644 index 00000000000..9b4296142f4 --- /dev/null +++ b/purchase_order_discount/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/purchase_order_discount/__manifest__.py b/purchase_order_discount/__manifest__.py new file mode 100644 index 00000000000..2f5a1f731e9 --- /dev/null +++ b/purchase_order_discount/__manifest__.py @@ -0,0 +1,15 @@ +{ + "name": "Purchase Order Discount", + "version": "1.0", + "category": "Purchase", + "description": "Purchase order global discount wizard.", + "author": "juson-odoo", + "depends": ["base", "purchase"], + "installable": True, + "license": "LGPL-3", + "data": [ + "security/ir.model.access.csv", + "views/purchase_order_views.xml", + "wizard/purchase_order_discount.xml", + ], +} diff --git a/purchase_order_discount/models/__init__.py b/purchase_order_discount/models/__init__.py new file mode 100644 index 00000000000..9f03530643d --- /dev/null +++ b/purchase_order_discount/models/__init__.py @@ -0,0 +1 @@ +from . import purchase_order diff --git a/purchase_order_discount/models/purchase_order.py b/purchase_order_discount/models/purchase_order.py new file mode 100644 index 00000000000..5413517228a --- /dev/null +++ b/purchase_order_discount/models/purchase_order.py @@ -0,0 +1,18 @@ +from odoo import models + + +class PurchaseOrder(models.Model): + _inherit = "purchase.order" + + def action_open_discount_wizard(self): + self.ensure_one() + return { + "type": "ir.actions.act_window", + "name": "Apply Discount", + "res_model": "purchase.order.discount", + "view_mode": "form", + "target": "new", + "context": { + "default_purchase_order_id": self.id, + }, + } diff --git a/purchase_order_discount/security/ir.model.access.csv b/purchase_order_discount/security/ir.model.access.csv new file mode 100644 index 00000000000..b02746d58d8 --- /dev/null +++ b/purchase_order_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_order_discount,access_purchase_order_discount,model_purchase_order_discount,base.group_user,1,1,1,1 diff --git a/purchase_order_discount/views/purchase_order_views.xml b/purchase_order_discount/views/purchase_order_views.xml new file mode 100644 index 00000000000..8f8af76d621 --- /dev/null +++ b/purchase_order_discount/views/purchase_order_views.xml @@ -0,0 +1,23 @@ + + + + purchase.order.form.discount.inherit + purchase.order + + + + + + + + + + + + + + diff --git a/purchase_order_discount/wizard/__init__.py b/purchase_order_discount/wizard/__init__.py new file mode 100644 index 00000000000..510f03e2878 --- /dev/null +++ b/purchase_order_discount/wizard/__init__.py @@ -0,0 +1 @@ +from . import purchase_order_discount diff --git a/purchase_order_discount/wizard/purchase_order_discount.py b/purchase_order_discount/wizard/purchase_order_discount.py new file mode 100644 index 00000000000..436f0674b10 --- /dev/null +++ b/purchase_order_discount/wizard/purchase_order_discount.py @@ -0,0 +1,55 @@ +from odoo import api, fields, models + + +class PurchaseOrderDiscount(models.TransientModel): + _name = "purchase.order.discount" + _description = "Purchase Discount Wizard" + + purchase_order_id = fields.Many2one( + "purchase.order", + default=lambda self: self.env.context.get("default_purchase_order_id"), + required=True, + ) + company_id = fields.Many2one(related="purchase_order_id.company_id") + currency_id = fields.Many2one(related="purchase_order_id.currency_id") + discount_amount = fields.Monetary(string="Amount") + discount_percentage = fields.Float(string="Percentage") + discount_type = fields.Selection( + string="Discount Type", + selection=[ + ("percentage", "Percentage"), + ("amount", "Fixed Amount"), + ], + default="percentage", + required=True, + ) + discount_amount_percentage = fields.Float( + string="Amount %", compute="_compute_discount_amount_percentage", store=True + ) + + @api.depends("discount_amount") + def _compute_discount_amount_percentage(self): + for record in self: + order = record.purchase_order_id + total = sum(line.price_unit * line.product_qty for line in order.order_line) + if total: + record.discount_amount_percentage = record.discount_amount / total + else: + record.discount_amount_percentage = 0.0 + + def action_apply_discount(self): + self.ensure_one() + order = self.purchase_order_id + + if self.discount_type == "percentage": + for line in order.order_line: + line.discount = self.discount_percentage * 100 + elif self.discount_type == "amount": + self._apply_fixed_amount_discount() + + def _apply_fixed_amount_discount(self): + order = self.purchase_order_id + for line in order.order_line: + line.discount = 0.0 + if self.discount_amount_percentage: + line.discount = self.discount_amount_percentage * 100 diff --git a/purchase_order_discount/wizard/purchase_order_discount.xml b/purchase_order_discount/wizard/purchase_order_discount.xml new file mode 100644 index 00000000000..d67afaad246 --- /dev/null +++ b/purchase_order_discount/wizard/purchase_order_discount.xml @@ -0,0 +1,45 @@ + + + + purchase.order.discount.form + purchase.order.discount + + + + + + + + + + + + + + + Discount will be applied to all lines. + + + Rounding error may occur when applying a discount amount. + + + + + + + +