Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions purchase_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
16 changes: 16 additions & 0 deletions purchase_discount/__manifest__.py
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,
Comment on lines +9 to +10

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

"data": [
"security/ir.model.access.csv",
"views/purchase_order_view.xml",
"wizard/purchase_order_discount.xml",
],
}
1 change: 1 addition & 0 deletions purchase_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_order
15 changes: 15 additions & 0 deletions purchase_discount/models/purchase_order.py
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make button type="action instead object

2 changes: 2 additions & 0 deletions purchase_discount/security/ir.model.access.csv
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really useful for non purchase user?

16 changes: 16 additions & 0 deletions purchase_discount/views/purchase_order_view.xml
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>

1 change: 1 addition & 0 deletions purchase_discount/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_order_discount
36 changes: 36 additions & 0 deletions purchase_discount/wizard/purchase_order_discount.py
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

Choose a reason for hiding this comment

The 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 😉

25 changes: 25 additions & 0 deletions purchase_discount/wizard/purchase_order_discount.xml
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

Choose a reason for hiding this comment

The 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>