Skip to content

Commit

Permalink
Merge branch '12.0-port-sale_discount_display_amount-from-14.0-to-12.…
Browse files Browse the repository at this point in the history
…0-pr-1651-2952' of git+ssh://github.com/sergiocorato/sale-workflow into 12.0
  • Loading branch information
Pretecno committed Jun 11, 2024
2 parents 02f9e79 + b28d032 commit d90167c
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 32 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': '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',
Expand Down
40 changes: 22 additions & 18 deletions sale_discount_display_amount/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 = """
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
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", "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")
40 changes: 30 additions & 10 deletions sale_discount_display_amount/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
8 changes: 8 additions & 0 deletions sale_discount_display_amount/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand 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
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 3 additions & 2 deletions sale_discount_display_amount/views/sale_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<field name="priority">99</field>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='amount_tax']" position="after">
<field name="price_total_no_discount"/>
<xpath expr="//field[@name='amount_untaxed']" position="before">
<field name="price_subtotal_no_discount" />
<field name="price_total_no_discount" />
<field name="discount_total"/>
</xpath>
</data>
Expand Down

0 comments on commit d90167c

Please sign in to comment.