Skip to content
Open
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
1 change: 1 addition & 0 deletions sale_order_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
10 changes: 10 additions & 0 deletions sale_order_discount/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
'name': "Sale Order Discount",
'version': "1.0",
'category': "Sales",
'description': "Updates global discount when order lines are changed",
'depends': ['sale_management'],
'installable': True,
'author': "jakan",
'license': "LGPL-3",
}
1 change: 1 addition & 0 deletions sale_order_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_order
47 changes: 47 additions & 0 deletions sale_order_discount/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import re
from odoo import api, models
from odoo.exceptions import UserError


class SaleOrder(models.Model):
_inherit = 'sale.order'

@api.onchange('order_line')
def _onchange_recalculate_global_discount(self):
discount_lines = self.env['sale.order.line']
for line in self.order_line:
if line._is_global_discount():
discount_lines += line

if not discount_lines:
return

product_lines = self.env['sale.order.line']
for line in self.order_line:
if not line._is_global_discount():
product_lines += line

if not product_lines:
self.order_line -= discount_lines
return

subtotal = 0
for line in product_lines:
subtotal += line.price_subtotal

for discount_line in discount_lines:
match = re.search(r"(\d+(?:\.\d+)?)%", discount_line.name)
if match:
percent = float(match.group(1))
discount_line.price_unit = -(subtotal * percent / 100)

@api.constrains('order_line')
def _check_single_global_discount(self):
for order in self:
discounts = order.order_line.filtered(
lambda l: l._is_global_discount()
)
if len(discounts) > 1:
raise UserError(
"Only one global discount is allowed per order."
)