-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[IMP] purchase: add global discount support on RFQ lines #1160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 19.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import models | ||
| from . import wizard |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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", | ||
| "application": True, | ||
| "installable": True, | ||
| "data": [ | ||
| "security/ir.model.access.csv", | ||
| "views/purchase_order_view.xml", | ||
| "wizard/purchase_order_discount.xml", | ||
| ], | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import purchase_order |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from odoo import models | ||
|
|
||
|
|
||
| class PurchaseOrder(models.Model): | ||
| _inherit = "purchase.order" | ||
|
|
||
| def action_purchase_global_discount(self): | ||
| self.ensure_one() | ||
| return { | ||
| "name": "Discount", | ||
| "type": "ir.actions.act_window", | ||
| "res_model": "purchase.order.discount", | ||
| "view_mode": "form", | ||
| "target": "new", | ||
| } | ||
|
Comment on lines
+7
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make button |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really useful for non purchase user? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <odoo> | ||
| <record id="view_purchase_order_form" model="ir.ui.view"> | ||
| <field name="name">purchase.order.view.form.discount</field> | ||
| <field name="model">purchase.order</field> | ||
| <field name="inherit_id" ref="purchase.purchase_order_form"/> | ||
| <field name="arch" type="xml"> | ||
| <xpath expr="//field[@name='order_line']" position="after"> | ||
| <div class="d-flex justify-content-end mt-2 mb-2"> | ||
| <button string="Discount" name="action_purchase_global_discount" type="object" class="btn btn-secondary"/> | ||
| </div> | ||
| </xpath> | ||
| </field> | ||
| </record> | ||
| </odoo> | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import purchase_order_discount |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from odoo import models, fields, api | ||
| from odoo.exceptions import UserError | ||
|
|
||
|
|
||
| class PurchaseOrderDiscount(models.TransientModel): | ||
| _name = "purchase.order.discount" | ||
| _description = "apply global discount on purchase order" | ||
|
|
||
| discount = fields.Float() | ||
| discount_type = fields.Selection( | ||
| [("amount", "$"), ("percent", "%")], default="percent" | ||
| ) | ||
| discount_percent = fields.Float(compute="_calculate_percentage") | ||
|
|
||
| @api.depends("discount", "discount_type") | ||
| def _calculate_percentage(self): | ||
| order = self.env["purchase.order"].browse(self.env.context.get("active_id")) | ||
| if self.discount_type == "percent": | ||
| self.discount_percent = self.discount | ||
| elif order.amount_untaxed != 0: | ||
| self.discount_percent = (self.discount * 100) / order.amount_untaxed | ||
| else: | ||
| self.discount_percent = 0 | ||
|
|
||
| def action_apply_discount(self): | ||
| order = self.env["purchase.order"].browse(self.env.context.get("active_id")) | ||
| if self.discount_type == "percent": | ||
| order.order_line.write({"discount": self.discount}) | ||
| elif order.amount_untaxed != 0: | ||
| order.order_line.write({"discount": 0}) | ||
| self.discount = (self.discount * 100) / order.amount_untaxed | ||
| order.order_line.write({"discount": self.discount}) | ||
| else: | ||
| raise UserError( | ||
| "Cannot apply fixed discount because Total Amount is already zero." | ||
| ) | ||
|
Comment on lines
+15
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems so wrong and giving me very weird behaviors, Could you try to understand it more and make it work properly? You can take inspiration from Discount on SO and Loyalty module or even try to use it's logic but it should not give any weird behavior 😉 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <odoo> | ||
|
|
||
| <record id="sale_order_line_wizard_form" model="ir.ui.view"> | ||
| <field name="name">purchase.order.discount.form</field> | ||
| <field name="model">purchase.order.discount</field> | ||
| <field name="arch" type="xml"> | ||
| <form> | ||
| <field name="discount"/> | ||
| <field name="discount_type" nolabel="True"/> | ||
| <span class="text-muted"> | ||
| ( | ||
| <field name="discount_percent" nolabel="1"/> | ||
| % | ||
| ) | ||
| </span> | ||
| <footer> | ||
| <button name="action_apply_discount" string="Apply" type="object" class="btn btn-primary"/> | ||
| <button special="cancel" string="Discard" class="btn btn-secondary" data-hotkey="x"/> | ||
| </footer> | ||
| </form> | ||
|
Comment on lines
+8
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make it more user friendly currently is seems very ugly 🙁 |
||
| </field> | ||
| </record> | ||
|
|
||
| </odoo> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not be application and auto_install should be true