Skip to content

Commit

Permalink
Merge branch '14.0-sale_discount_display_amount-add_discount_subtotal…
Browse files Browse the repository at this point in the history
…_with_migr' of git+ssh://github.com/efatto/sale-workflow into 14.0
  • Loading branch information
Pretecno committed Jun 15, 2024
2 parents 1671f6d + 361ba1e commit d65b010
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sale_discount_display_amount/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions sale_discount_display_amount/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)


Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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")
8 changes: 8 additions & 0 deletions sale_discount_display_amount/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
)
Expand All @@ -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,
}
Expand Down
6 changes: 5 additions & 1 deletion sale_discount_display_amount/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d65b010

Please sign in to comment.