diff --git a/purchase_discount/__init__.py b/purchase_discount/__init__.py new file mode 100644 index 00000000000..9b4296142f4 --- /dev/null +++ b/purchase_discount/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/purchase_discount/__manifest__.py b/purchase_discount/__manifest__.py new file mode 100644 index 00000000000..8526d99daa7 --- /dev/null +++ b/purchase_discount/__manifest__.py @@ -0,0 +1,18 @@ +{ + 'name': 'purchase_dicount', + 'description': "Add function for disscount", + 'author': "meet kavathiya", + 'website': "https://www.odoo.com/", + 'category': "Real-estate", + 'version': "0.1", + 'application': True, + 'installable': True, + 'depends': ['purchase'], + 'data': [ + 'security/ir.model.access.csv', + 'views/purchase_order_view.xml', + 'wizard/purchase_order_discount_view.xml' + ], + 'assets': {}, + 'license': 'LGPL-3', +} diff --git a/purchase_discount/models/__init__.py b/purchase_discount/models/__init__.py new file mode 100644 index 00000000000..9f03530643d --- /dev/null +++ b/purchase_discount/models/__init__.py @@ -0,0 +1 @@ +from . import purchase_order diff --git a/purchase_discount/models/purchase_order.py b/purchase_discount/models/purchase_order.py new file mode 100644 index 00000000000..dc2d774dc1a --- /dev/null +++ b/purchase_discount/models/purchase_order.py @@ -0,0 +1,15 @@ +from odoo import models + + +class InheritedPurchaseOrder(models.Model): + _inherit = 'purchase.order' + + def action_open_discount_wizard(self): + self.ensure_one() + return { + 'name': "Discount", + 'type': 'ir.actions.act_window', + 'res_model': 'purchase.order.discount', + 'view_mode': 'form', + 'target': 'new', + } diff --git a/purchase_discount/security/ir.model.access.csv b/purchase_discount/security/ir.model.access.csv new file mode 100644 index 00000000000..ef7a38f3312 --- /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,base.group_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..0d345f71c02 --- /dev/null +++ b/purchase_discount/views/purchase_order_view.xml @@ -0,0 +1,15 @@ + + + + purchase.view.form.inherit.purchase.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..00155840fbb --- /dev/null +++ b/purchase_discount/wizard/purchase_order_discount.py @@ -0,0 +1,61 @@ +from odoo import api, fields, models +from odoo.exceptions import ValidationError + + +class PurchaseOrderDiscount(models.TransientModel): + _name = 'purchase.order.discount' + _description = "Discount Wizard" + + purchase_order_id = fields.Many2one( + 'purchase.order', + default=lambda self: self.env.context.get('active_id'), + required=True, + ) + discount_percentage = fields.Float(string="Percentage") + discount_type = fields.Selection( + selection=[ + ('percentage', "%"), + ('amount', "₹"), + ], + default='percentage', + ) + percentage = fields.Float(compute='_compute_initial_discount') + + @api.constrains('discount_type', 'discount_percentage') + def _check_discount_amount(self): + for wizard in self: + if ( + (wizard.discount_type in ('percentage') + and wizard.discount_percentage > 100.0) + or (wizard.discount_type in ('amount') + and wizard.discount_percentage > wizard.purchase_order_id.amount_untaxed) + ): + raise ValidationError("Invalid discount amount") + + @api.depends('discount_type', 'discount_percentage') + def _compute_initial_discount(self): + if self.discount_type == 'amount': + if self.purchase_order_id.amount_untaxed == 0: + raise ValidationError("No more discount possible") + self.percentage = ( + self.discount_percentage * 100 / self.purchase_order_id.amount_untaxed + ) + else: + self.percentage = self.discount_percentage + + def action_apply_discount(self): + self.ensure_one() + if self.discount_type == 'amount': + self.purchase_order_id.order_line.write( + { + 'discount': ( + self.discount_percentage + * 100 + / self.purchase_order_id.amount_untaxed + ) + } + ) + else: + self.purchase_order_id.order_line.write( + {'discount': self.discount_percentage} + ) diff --git a/purchase_discount/wizard/purchase_order_discount_view.xml b/purchase_discount/wizard/purchase_order_discount_view.xml new file mode 100644 index 00000000000..1d62ebbd24d --- /dev/null +++ b/purchase_discount/wizard/purchase_order_discount_view.xml @@ -0,0 +1,23 @@ + + + + + purchase.order.discount.form + purchase.order.discount + +
+ + + + + + + +
+
+
+ +