diff --git a/sale_discount_display_amount/__manifest__.py b/sale_discount_display_amount/__manifest__.py index bc7432ff7892..e8faf03e02c9 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": "14.0.2.1.0", + "version": "14.0.2.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 870abceae399..11cf8feed6b3 100644 --- a/sale_discount_display_amount/hooks.py +++ b/sale_discount_display_amount/hooks.py @@ -12,9 +12,11 @@ ("sale_order", "price_subtotal_no_discount"), ("sale_order", "price_total_no_discount"), ("sale_order", "discount_total"), + ("sale_order", "discount_subtotal"), ("sale_order_line", "price_subtotal_no_discount"), ("sale_order_line", "price_total_no_discount"), ("sale_order_line", "discount_total"), + ("sale_order_line", "discount_subtotal"), ) diff --git a/sale_discount_display_amount/migrations/14.0.2.2.0/post-compute-subtotal-columns.py b/sale_discount_display_amount/migrations/14.0.2.2.0/post-compute-subtotal-columns.py new file mode 100644 index 000000000000..06747e47031e --- /dev/null +++ b/sale_discount_display_amount/migrations/14.0.2.2.0/post-compute-subtotal-columns.py @@ -0,0 +1,21 @@ +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 = """ + 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")._update_discount_display_fields() diff --git a/sale_discount_display_amount/migrations/14.0.2.2.0/pre-add-subtotal-columns.py b/sale_discount_display_amount/migrations/14.0.2.2.0/pre-add-subtotal-columns.py new file mode 100644 index 000000000000..8be68f33d72b --- /dev/null +++ b/sale_discount_display_amount/migrations/14.0.2.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", "discount_subtotal"), + ("sale_order_line", "discount_subtotal"), +) + + +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 e1d61da029f2..7a0b2e98bf10 100644 --- a/sale_discount_display_amount/models/sale_order.py +++ b/sale_discount_display_amount/models/sale_order.py @@ -8,6 +8,12 @@ class SaleOrder(models.Model): _inherit = "sale.order" discount_total = fields.Monetary( + compute="_compute_discount_total", + string="Discount total", + currency_field="currency_id", + store=True, + ) + discount_subtotal = fields.Monetary( compute="_compute_discount_total", string="Discount Subtotal", currency_field="currency_id", @@ -38,6 +44,7 @@ def _get_compute_discount_total_depends(self): def _compute_discount_total(self): for order in self: discount_total = sum(order.order_line.mapped("discount_total")) + discount_subtotal = sum(order.order_line.mapped("discount_subtotal")) price_subtotal_no_discount = sum( order.order_line.mapped("price_subtotal_no_discount") ) @@ -47,6 +54,7 @@ def _compute_discount_total(self): order.update( { "discount_total": discount_total, + "discount_subtotal": discount_subtotal, "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 ccb01b813472..ca39707bf042 100644 --- a/sale_discount_display_amount/models/sale_order_line.py +++ b/sale_discount_display_amount/models/sale_order_line.py @@ -8,6 +8,9 @@ class SaleOrderLine(models.Model): _inherit = "sale.order.line" discount_total = fields.Monetary( + compute="_compute_amount", string="Discount total", store=True + ) + discount_subtotal = fields.Monetary( compute="_compute_amount", string="Discount Subtotal", store=True ) price_subtotal_no_discount = fields.Monetary( @@ -38,10 +41,11 @@ def _update_discount_display_fields(self): price_subtotal_no_discount = taxes["total_excluded"] price_total_no_discount = taxes["total_included"] discount_total = price_total_no_discount - line.price_total - + discount_subtotal = price_subtotal_no_discount - line.price_subtotal line.update( { "discount_total": discount_total, + "discount_subtotal": discount_subtotal, "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 5f650885f22b..b6de5e9c4646 100644 --- a/sale_discount_display_amount/tests/test_discount_display_amount.py +++ b/sale_discount_display_amount/tests/test_discount_display_amount.py @@ -26,3 +26,4 @@ def test_sale_discount_value(self): 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) + self.assertAlmostEqual(so.discount_subtotal, 3.07)