Skip to content

Commit b1e8260

Browse files
committed
[ADD] purchase_discount: add global discount functionality.
add discount button on purchase order page. two ways to give discount in percentage and dollar. check discount is valid or not.
1 parent b68a192 commit b1e8260

File tree

9 files changed

+145
-0
lines changed

9 files changed

+145
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,10 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
.eslintignore
132+
.eslintrc.json
133+
jsconfig.json
134+
package.json
135+
package-lock.json
136+
node_modules/

purchase_discount/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models

purchase_discount/__manifest__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
'author': 'Odoo S.A.',
3+
'name': 'Purchase Discount',
4+
'depends': ['purchase'],
5+
'license': 'LGPL-3',
6+
'data': [
7+
'security/ir.model.access.csv',
8+
'views/purchase_order_discount_views.xml',
9+
'views/purchase_order_views.xml'
10+
],
11+
'application': True,
12+
'installable': True
13+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import purchase_order_discount
2+
from . import purchase_order
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from odoo import models, fields
2+
3+
4+
class PurchaseOrder(models.Model):
5+
_inherit = "purchase.order"
6+
7+
discount_id = fields.Many2one("purchase.order.discount")
8+
9+
def action_discount(self):
10+
if not self.discount_id:
11+
new_data = self.env["purchase.order.discount"].create(
12+
{
13+
"discount_type": "value",
14+
"discount_in_value": 0,
15+
"discount_in_percentage": 0,
16+
}
17+
)
18+
self.discount_id = new_data.id
19+
return {
20+
"type": "ir.actions.act_window",
21+
"name": "Discount",
22+
"res_model": "purchase.order.discount",
23+
"view_mode": "form",
24+
"view_id": self.env.ref(
25+
"purchase_discount.purchase_order_discount_view_form"
26+
).id,
27+
"res_id": self.discount_id.id,
28+
"target": "new",
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from odoo import models, fields, api
2+
from odoo.exceptions import ValidationError
3+
4+
5+
class PurchaseOrderDiscount(models.Model):
6+
_name = "purchase.order.discount"
7+
_description = "Purchase Order Discount"
8+
9+
order_id = fields.One2many("purchase.order", "discount_id")
10+
discount_type = fields.Selection(
11+
selection=[
12+
("value", "$"),
13+
("percentage", "%"),
14+
],
15+
default="value",
16+
)
17+
discount_in_value = fields.Float(string="Discount")
18+
discount_in_percentage = fields.Float(compute="_compute_discount_percentage", store=True, readonly=True)
19+
order_line_id = fields.Many2one("purchase.order.line")
20+
21+
@api.depends("discount_type", "discount_in_value", "order_id.amount_total")
22+
def _compute_discount_percentage(self):
23+
for record in self:
24+
if record.discount_type == "value" and record.order_id.amount_total > 0:
25+
record.discount_in_percentage = (record.discount_in_value * 100) / (record.order_id.amount_total)
26+
elif record.discount_type == "percentage":
27+
record.discount_in_percentage = record.discount_in_value
28+
else:
29+
record.discount_in_percentage = 0.0
30+
31+
@api.constrains("discount_in_value", "discount_type", "order_id.amount_total")
32+
def _check_discount_price(self):
33+
for record in self:
34+
if record.discount_type == "percentage":
35+
if record.discount_in_value < 0 or record.discount_in_value > 100:
36+
raise ValidationError("discount value is not valid please check again")
37+
if record.discount_type == "value":
38+
if record.discount_in_value < 0 or record.discount_in_value > record.order_id.amount_total:
39+
raise ValidationError("discount value is not valid please check again")
40+
41+
def apply_discount(self):
42+
self.order_id.order_line.discount = self.discount_in_percentage
43+
44+
def apply_cancle(self):
45+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
2+
"access_purchase_order_discount","access_purchase_order_discount","model_purchase_order_discount","base.group_user",1,1,1,1
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<odoo>
3+
<record id="purchase_order_discount_view_form" model="ir.ui.view">
4+
<field name="name">purchase.order.discount.form</field>
5+
<field name="model">purchase.order.discount</field>
6+
<field name="arch" type="xml">
7+
<form>
8+
<sheet>
9+
<group>
10+
<field name="discount_type"/>
11+
<field name="discount_in_value"/>
12+
<field name="discount_in_percentage"/>
13+
</group>
14+
</sheet>
15+
<footer>
16+
<button name="apply_discount" type="object" string="Apply" class="oe_highlight"/>
17+
<button name="apply_cancle" type="object" string="Cancle"/>
18+
</footer>
19+
</form>
20+
</field>
21+
</record>
22+
23+
<record id="action_open_discount" model="ir.actions.act_window">
24+
<field name="name">Apply Discount</field>
25+
<field name="res_model">purchase.order.discount</field>
26+
<field name="view_mode">form</field>
27+
</record>
28+
</odoo>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<odoo>
3+
<record id="purchase_order_form_inherit" model="ir.ui.view">
4+
<field name="name">purchase.order.view.form.inherit</field>
5+
<field name="model">purchase.order</field>
6+
<field name="inherit_id" ref="purchase.purchase_order_form"/>
7+
<field name="arch" type="xml">
8+
<xpath expr="//*[hasclass('oe_subtotal_footer')]" position="replace">
9+
<div class="d-flex flex-column align-items-end">
10+
<button name="action_discount" type="object" class="oe_highlight" string="Discount"/>
11+
<group class="oe_subtotal_footer">
12+
<field name="tax_totals" widget="account-tax-totals-field" nolabel="1" readonly="1"/>
13+
</group>
14+
</div>
15+
</xpath>
16+
</field>
17+
</record>
18+
</odoo>

0 commit comments

Comments
 (0)