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..7fdbd011621 --- /dev/null +++ b/purchase_discount/wizard/purchase_order_discount.py @@ -0,0 +1,66 @@ +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", + required=True, + ) + discount_percent = fields.Float( + compute="_calculate_percentage", string="Calculated Percentage" + ) + order_id = fields.Many2one( + "purchase.order", + string="Purchase Order", + required=True, + ) + + @api.constrains("discount", "discount_type") + def _check_discount_limit(self): + if ( + self.discount < 0 or self.discount > 100 + ) and self.discount_type == "percent": + 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): + base_amount = self._get_base_untaxed_amount(self.order_id) + 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() + if not self.order_id.order_line: + raise UserError("There are no lines on this order to discount.") + + base_amount = self._get_base_untaxed_amount(self.order_id) + + 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.") + + self.order_id.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..942f5f584fa --- /dev/null +++ b/purchase_discount/wizard/purchase_order_discount.xml @@ -0,0 +1,44 @@ + + + + + Discount + purchase.order.discount + form + new + {'default_order_id': active_id} + + + + purchase.order.discount.form + purchase.order.discount + +
+ + +
+
+
+ +
+
+
+
+ +