diff --git a/purchase_discount/__init__.py b/purchase_discount/__init__.py new file mode 100644 index 00000000000..40272379f72 --- /dev/null +++ b/purchase_discount/__init__.py @@ -0,0 +1 @@ +from . import wizard diff --git a/purchase_discount/__manifest__.py b/purchase_discount/__manifest__.py new file mode 100644 index 00000000000..32523119b93 --- /dev/null +++ b/purchase_discount/__manifest__.py @@ -0,0 +1,16 @@ +{ + "name": "purchase discount", + "version": "1.0.0", + "depends": ["base", "purchase"], + "author": "Mehul Kotak", + "category": "Task-1", + "description": "This perfrom global discount in purchase", + "license": "LGPL-3", + "auto_install": True, + "installable": True, + "data": [ + "security/ir.model.access.csv", + "wizard/purchase_order_discount.xml", + "views/purchase_order_view.xml", + ], +} diff --git a/purchase_discount/security/ir.model.access.csv b/purchase_discount/security/ir.model.access.csv new file mode 100644 index 00000000000..d27226be896 --- /dev/null +++ b/purchase_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,purchase.group_purchase_user,1,1,1,1 diff --git a/purchase_discount/views/purchase_order_view.xml b/purchase_discount/views/purchase_order_view.xml new file mode 100644 index 00000000000..394760b6713 --- /dev/null +++ b/purchase_discount/views/purchase_order_view.xml @@ -0,0 +1,16 @@ + + + + purchase.order.view.form.discount + purchase.order + + + +
+
+
+
+
+
+ diff --git a/purchase_discount/wizard/__init__.py b/purchase_discount/wizard/__init__.py new file mode 100644 index 00000000000..510f03e2878 --- /dev/null +++ b/purchase_discount/wizard/__init__.py @@ -0,0 +1 @@ +from . import purchase_order_discount diff --git a/purchase_discount/wizard/purchase_order_discount.py b/purchase_discount/wizard/purchase_order_discount.py new file mode 100644 index 00000000000..b1004088273 --- /dev/null +++ b/purchase_discount/wizard/purchase_order_discount.py @@ -0,0 +1,59 @@ +from odoo import models, fields, api +from odoo.exceptions import UserError, ValidationError + + +class PurchaseOrderDiscount(models.TransientModel): + _name = "purchase.order.discount" + _description = "Apply Global Discount on Purchase Order" + + discount = fields.Float(string="Discount", required=True) + discount_type = fields.Selection( + [("amount", "$"), ("percent", "%")], default="percent", string="Discount Type" + ) + discount_percent = fields.Float( + compute="_calculate_percentage", string="Calculated Percentage" + ) + + @api.constrains("discount", "discount_type") + def _check_discount_limit(self): + if self.discount < 0 or self.discount > 100: + raise ValidationError("Discount value is invalid") + + def _get_base_untaxed_amount(self, order): + total = 0.0 + for line in order.order_line: + total += line.price_unit * line.product_qty + return total + + @api.depends("discount", "discount_type") + def _calculate_percentage(self): + order = self.env["purchase.order"].browse(self.env.context.get("active_id")) + base_amount = self._get_base_untaxed_amount(order) + if self.discount_type == "percent": + self.discount_percent = self.discount + elif base_amount > 0: + self.discount_percent = (self.discount * 100) / base_amount + else: + self.discount_percent = 0 + + def action_apply_discount(self): + self.ensure_one() + order = self.env["purchase.order"].browse(self.env.context.get("active_id")) + + if not order.order_line: + raise UserError("There are no lines on this order to discount.") + + base_amount = self._get_base_untaxed_amount(order) + + if self.discount_type == "percent": + target_discount = self.discount + else: + if base_amount <= 0: + raise UserError("Cannot apply an amount discount to a $0 order.") + target_discount = (self.discount * 100) / base_amount + + # Final check to ensure calculated amount doesn't exceed 100% + if target_discount > 100: + raise UserError("The discount amount exceeds the total value of the order.") + + order.order_line.write({"discount": target_discount}) diff --git a/purchase_discount/wizard/purchase_order_discount.xml b/purchase_discount/wizard/purchase_order_discount.xml new file mode 100644 index 00000000000..4f5df9839ee --- /dev/null +++ b/purchase_discount/wizard/purchase_order_discount.xml @@ -0,0 +1,32 @@ + + + + + Discount + purchase.order.discount + form + new + + + + purchase.order.discount.form + purchase.order.discount + +
+ + + + ( + +% + ) + + + + +
+ +