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
2 changes: 2 additions & 0 deletions sale_order_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import wizard
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'],
'auto_install': True,
'author': "times",
'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
62 changes: 62 additions & 0 deletions sale_order_discount/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import re
from odoo import _, api, fields, models
from odoo.tools import float_repr


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

global_discount_percentage = fields.Float()

@api.onchange('order_line')
def _onchange_update_global_discount(self):
discount_lines = self.env['sale.order.line']
product_lines = self.env['sale.order.line']

for line in self.order_line:
if line._is_global_discount():
discount_lines += line
if not line._is_global_discount():
product_lines += line

if not discount_lines:
return

if not product_lines:
self.order_line -= discount_lines
return

subtotal = sum(product_lines.mapped('price_subtotal'))

for discount_line in discount_lines:

discount_dp = self.env['decimal.precision'].precision_get('Discount')
origin_record = self.env['sale.order'].browse(self.id.origin)

match = re.search(r"(\d+(?:\.\d+)?)", discount_line.name)
discount_from_name = float(match.group(1)) if match else False

if not discount_from_name:
self.env.user._bus_send("simple_notification", {
'type': 'danger',
'title': _("Error"),
'message': _("Discounts should be in Float(0.00%)")
})
percent = origin_record.global_discount_percentage

discount_line.name = _(
"Discount %(percent)s%%",
percent=float_repr(percent, discount_dp),
)
return

if discount_from_name != self.global_discount_percentage:
origin_record.global_discount_percentage = discount_from_name

percent = origin_record.global_discount_percentage

discount_line.name = _(
"Discount %(percent)s%%",
percent=float_repr(percent, discount_dp),
)
discount_line.price_unit = -(subtotal * (percent / 100))
1 change: 1 addition & 0 deletions sale_order_discount/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import sale_order_discount
10 changes: 10 additions & 0 deletions sale_order_discount/wizard/sale_order_discount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from odoo import models


class SaleOrderDiscount(models.TransientModel):
_inherit = 'sale.order.discount'

def _prepare_global_discount_so_lines(self, base_lines):
res = super()._prepare_global_discount_so_lines(base_lines)
self.sale_order_id.global_discount_percentage = self.discount_percentage * 100
return res