Skip to content

Commit f9fc3f1

Browse files
committed
[IMP] sale: auto-recalculate global discount on order line changes
Enhanced the Sales Order logic to automatically recalculate and update the global discount line whenever sale order lines are added, updated, or deleted. This ensures that the discount remains in sync with the current order total. If all product lines are removed, the discount line is also removed. This improves data consistency and reduces the need for manual discount updates. task-4962386
1 parent fbf9ee9 commit f9fc3f1

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

discount_update/__init__.py

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

discount_update/__manifest__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
'name': "Discount Update",
3+
'version': "1.0",
4+
'depends': ["sale_management"],
5+
'insallable': True,
6+
'license': 'LGPL-3'
7+
}

discount_update/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import sale_order
2+
from . import sale_order_line

discount_update/models/sale_order.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from odoo import fields, models
2+
3+
4+
class SaleOrder(models.Model):
5+
_inherit = "sale.order"
6+
7+
discount_percentage = fields.Float(string="Global Discount (%)")
8+
wizard_ids = fields.One2many(
9+
"sale.order.discount", "sale_order_id", string="Discount Wizards"
10+
)
11+
12+
def _update_discount(self):
13+
14+
discount_product = self.env["product.product"].search([("name", "=", "Discount")], limit=1)
15+
if not discount_product:
16+
return
17+
18+
total_amount = sum(line.price_total for line in self.order_line if not line.is_discount_line())
19+
if not total_amount:
20+
discount_lines = self.order_line.filtered(lambda l: l.is_discount_line())
21+
discount_lines.unlink()
22+
return
23+
24+
if self.wizard_ids:
25+
discount_percentage = self.wizard_ids[-1].discount_percentage
26+
else:
27+
return
28+
29+
discount_amount = total_amount * (discount_percentage)
30+
discount_line = self.order_line.filtered(lambda l: l.is_discount_line())
31+
32+
if discount_line:
33+
discount_line.write(
34+
{
35+
"price_unit": discount_amount * (-1),
36+
"name": f"Discount{discount_percentage * 100:.2f}%",
37+
}
38+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from odoo import api, models
2+
3+
4+
class SaleOrderLine(models.Model):
5+
_inherit = "sale.order.line"
6+
7+
def is_discount_line(self):
8+
discount_product = self.order_id.env["product.product"].search([("name", "=", "Discount")], limit=1)
9+
return self.product_id == discount_product
10+
11+
def unlink(self):
12+
order_ids = self.mapped("order_id")
13+
res = super().unlink()
14+
for order in order_ids:
15+
order._update_discount()
16+
return res
17+
18+
@api.model_create_multi
19+
def create(self, vals):
20+
lines = super().create(vals)
21+
for line in lines:
22+
if line.order_id and not line.is_discount_line():
23+
line.order_id._update_discount()
24+
return lines
25+
26+
def write(self, vals):
27+
res = super().write(vals)
28+
for line in self:
29+
if not line.is_discount_line():
30+
line.order_id._update_discount()
31+
return res

0 commit comments

Comments
 (0)