diff --git a/sale_discount_display_amount/__manifest__.py b/sale_discount_display_amount/__manifest__.py
index 93828e525fd6..cc5ca76aa0e7 100644
--- a/sale_discount_display_amount/__manifest__.py
+++ b/sale_discount_display_amount/__manifest__.py
@@ -6,7 +6,7 @@
'summary': """
This addon intends to display the amount of the discount computed on
sale_order_line and sale_order level""",
- 'version': '12.0.1.1.0',
+ 'version': '12.0.1.2.0',
'license': 'AGPL-3',
'author': 'ACSONE SA/NV,Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/sale-workflow',
diff --git a/sale_discount_display_amount/hooks.py b/sale_discount_display_amount/hooks.py
index 35582e2c8512..22cc3d3f98a7 100644
--- a/sale_discount_display_amount/hooks.py
+++ b/sale_discount_display_amount/hooks.py
@@ -3,25 +3,25 @@
import logging
from odoo.api import Environment
from odoo import SUPERUSER_ID
+from odoo.tools.sql import column_exists, create_column
_logger = logging.getLogger(__name__)
+COLUMNS = (
+ ("sale_order", "price_total_no_discount"),
+ ("sale_order", "discount_total"),
+ ("sale_order", "price_subtotal_no_discount"),
+ ("sale_order_line", "price_subtotal_no_discount"),
+ ("sale_order_line", "price_total_no_discount"),
+ ("sale_order_line", "discount_total"),
+)
+
def pre_init_hook(cr):
- _logger.info("Create discount columns in database")
- cr.execute("""
- ALTER TABLE sale_order ADD COLUMN price_total_no_discount numeric;
- """)
- cr.execute("""
- ALTER TABLE sale_order ADD COLUMN discount_total numeric;
- """)
- cr.execute("""
- ALTER TABLE sale_order_line ADD COLUMN price_total_no_discount
- numeric;
- """)
- cr.execute("""
- ALTER TABLE sale_order_line ADD COLUMN discount_total numeric;
- """)
+ for table, column in COLUMNS:
+ if not column_exists(cr, table, column):
+ _logger.info("Create discount column %s in database", column)
+ create_column(cr, table, column, "numeric")
def post_init_hook(cr, registry):
@@ -30,15 +30,19 @@ def post_init_hook(cr, registry):
query = """
update sale_order_line
- set price_total_no_discount = price_total
+ set
+ price_subtotal_no_discount = price_subtotal,
+ price_total_no_discount = price_total
where discount = 0.0
"""
cr.execute(query)
query = """
- update sale_order
- set price_total_no_discount = amount_total
- """
+ update sale_order
+ set
+ price_subtotal_no_discount = amount_untaxed,
+ price_total_no_discount = amount_total
+ """
cr.execute(query)
query = """
diff --git a/sale_discount_display_amount/migrations/12.0.1.2.0/post-compute-subtotal-columns.py b/sale_discount_display_amount/migrations/12.0.1.2.0/post-compute-subtotal-columns.py
new file mode 100644
index 000000000000..64674ae45753
--- /dev/null
+++ b/sale_discount_display_amount/migrations/12.0.1.2.0/post-compute-subtotal-columns.py
@@ -0,0 +1,36 @@
+import logging
+
+from odoo import SUPERUSER_ID
+from odoo.api import Environment
+
+_logger = logging.getLogger(__name__)
+
+
+def migrate(cr, version):
+ _logger.info("Compute discount columns")
+ env = Environment(cr, SUPERUSER_ID, {})
+
+ query = """
+ update sale_order_line
+ set
+ price_subtotal_no_discount = price_subtotal
+ where discount = 0.0
+ """
+ cr.execute(query)
+
+ query = """
+ update sale_order
+ set
+ price_subtotal_no_discount = amount_untaxed
+ """
+ cr.execute(query)
+
+ query = """
+ select distinct order_id from sale_order_line where discount > 0.0;
+ """
+
+ cr.execute(query)
+ order_ids = cr.fetchall()
+
+ orders = env["sale.order"].search([("id", "in", order_ids)])
+ orders.mapped("order_line")._compute_discount()
diff --git a/sale_discount_display_amount/migrations/12.0.1.2.0/pre-add-subtotal-columns.py b/sale_discount_display_amount/migrations/12.0.1.2.0/pre-add-subtotal-columns.py
new file mode 100644
index 000000000000..053ce595e712
--- /dev/null
+++ b/sale_discount_display_amount/migrations/12.0.1.2.0/pre-add-subtotal-columns.py
@@ -0,0 +1,17 @@
+import logging
+
+from odoo.tools.sql import column_exists, create_column
+
+_logger = logging.getLogger(__name__)
+
+COLUMNS = (
+ ("sale_order", "price_subtotal_no_discount"),
+ ("sale_order_line", "price_subtotal_no_discount"),
+)
+
+
+def migrate(cr, version):
+ for table, column in COLUMNS:
+ if not column_exists(cr, table, column):
+ _logger.info("Create discount column %s in database", column)
+ create_column(cr, table, column, "numeric")
diff --git a/sale_discount_display_amount/models/sale_order.py b/sale_discount_display_amount/models/sale_order.py
index 7876dff1fd2e..f43178004681 100644
--- a/sale_discount_display_amount/models/sale_order.py
+++ b/sale_discount_display_amount/models/sale_order.py
@@ -9,24 +9,44 @@ class SaleOrder(models.Model):
_inherit = 'sale.order'
discount_total = fields.Monetary(
- compute='_compute_discount',
+ compute='_compute_discount_total',
string='Discount Subtotal',
currency_field='currency_id',
store=True)
+ price_subtotal_no_discount = fields.Monetary(
+ compute='_compute_discount_total',
+ string='Subtotal Without Discount',
+ currency_field='currency_id',
+ store=True,
+ )
price_total_no_discount = fields.Monetary(
- compute='_compute_discount',
+ compute='_compute_discount_total',
string='Subtotal Without Discount',
currency_field='currency_id',
store=True)
- @api.depends('order_line.discount_total',
- 'order_line.price_total_no_discount')
- def _compute_discount(self):
+ @api.model
+ def _get_compute_discount_total_depends(self):
+ return [
+ 'order_line.discount_total',
+ 'order_line.price_subtotal_no_discount',
+ 'order_line.price_total_no_discount',
+ ]
+
+ @api.depends(lambda self: self._get_compute_discount_total_depends())
+ def _compute_discount_total(self):
for order in self:
discount_total = sum(order.order_line.mapped('discount_total'))
+ price_subtotal_no_discount = sum(
+ order.order_line.mapped('price_subtotal_no_discount')
+ )
price_total_no_discount = sum(
- order.order_line.mapped('price_total_no_discount'))
- order.update({
- 'discount_total': discount_total,
- 'price_total_no_discount': price_total_no_discount
- })
+ order.order_line.mapped('price_total_no_discount')
+ )
+ order.update(
+ {
+ 'discount_total': discount_total,
+ 'price_subtotal_no_discount': price_subtotal_no_discount,
+ 'price_total_no_discount': price_total_no_discount,
+ }
+ )
diff --git a/sale_discount_display_amount/models/sale_order_line.py b/sale_discount_display_amount/models/sale_order_line.py
index 83378a713444..daa7f14c25cf 100644
--- a/sale_discount_display_amount/models/sale_order_line.py
+++ b/sale_discount_display_amount/models/sale_order_line.py
@@ -12,6 +12,11 @@ class SaleOrderLine(models.Model):
compute='_compute_amount',
string='Discount Subtotal',
store=True)
+ price_subtotal_no_discount = fields.Monetary(
+ compute='_compute_amount',
+ string='Subtotal Without Discount',
+ store=True
+ )
price_total_no_discount = fields.Monetary(
compute='_compute_amount',
string='Subtotal Without Discount',
@@ -21,6 +26,7 @@ def _compute_discount(self):
for line in self:
if not line.discount:
line.price_total_no_discount = line.price_total
+ line.price_subtotal_no_discount = line.price_subtotal
continue
price = line.price_unit
taxes = line.tax_id.compute_all(
@@ -30,11 +36,13 @@ def _compute_discount(self):
product=line.product_id,
partner=line.order_id.partner_shipping_id)
+ price_subtotal_no_discount = taxes['total_excluded']
price_total_no_discount = taxes['total_included']
discount_total = price_total_no_discount - line.price_total
line.update({
'discount_total': discount_total,
+ 'price_subtotal_no_discount': price_subtotal_no_discount,
'price_total_no_discount': price_total_no_discount
})
diff --git a/sale_discount_display_amount/tests/test_discount_display_amount.py b/sale_discount_display_amount/tests/test_discount_display_amount.py
index 0be4fe3de1dc..1f6a308c9cef 100644
--- a/sale_discount_display_amount/tests/test_discount_display_amount.py
+++ b/sale_discount_display_amount/tests/test_discount_display_amount.py
@@ -24,7 +24,9 @@ def test_sale_discount_value(self):
first_line = so.order_line[0]
first_line.discount = 10
+ self.assertAlmostEqual(first_line.price_subtotal_no_discount, 30.75)
self.assertAlmostEqual(first_line.price_total_no_discount, 35.36)
self.assertAlmostEqual(first_line.discount_total, 3.53)
- self.assertAlmostEqual(so.discount_total, 3.53)
+ self.assertAlmostEqual(so.price_subtotal_no_discount, 30.75)
self.assertAlmostEqual(so.price_total_no_discount, 35.36)
+ self.assertAlmostEqual(so.discount_total, 3.53)
diff --git a/sale_discount_display_amount/views/sale_view.xml b/sale_discount_display_amount/views/sale_view.xml
index 86faefa8f374..06d7dbf80c9c 100644
--- a/sale_discount_display_amount/views/sale_view.xml
+++ b/sale_discount_display_amount/views/sale_view.xml
@@ -7,8 +7,9 @@
99
-
-
+
+
+