Skip to content

Commit d90167c

Browse files
author
Pretecno
committed
Merge branch '12.0-port-sale_discount_display_amount-from-14.0-to-12.0-pr-1651-2952' of git+ssh://github.com/sergiocorato/sale-workflow into 12.0
2 parents 02f9e79 + b28d032 commit d90167c

File tree

8 files changed

+120
-32
lines changed

8 files changed

+120
-32
lines changed

sale_discount_display_amount/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'summary': """
77
This addon intends to display the amount of the discount computed on
88
sale_order_line and sale_order level""",
9-
'version': '12.0.1.1.0',
9+
'version': '12.0.1.2.0',
1010
'license': 'AGPL-3',
1111
'author': 'ACSONE SA/NV,Odoo Community Association (OCA)',
1212
'website': 'https://github.com/OCA/sale-workflow',

sale_discount_display_amount/hooks.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
import logging
44
from odoo.api import Environment
55
from odoo import SUPERUSER_ID
6+
from odoo.tools.sql import column_exists, create_column
67

78
_logger = logging.getLogger(__name__)
89

10+
COLUMNS = (
11+
("sale_order", "price_total_no_discount"),
12+
("sale_order", "discount_total"),
13+
("sale_order", "price_subtotal_no_discount"),
14+
("sale_order_line", "price_subtotal_no_discount"),
15+
("sale_order_line", "price_total_no_discount"),
16+
("sale_order_line", "discount_total"),
17+
)
18+
919

1020
def pre_init_hook(cr):
11-
_logger.info("Create discount columns in database")
12-
cr.execute("""
13-
ALTER TABLE sale_order ADD COLUMN price_total_no_discount numeric;
14-
""")
15-
cr.execute("""
16-
ALTER TABLE sale_order ADD COLUMN discount_total numeric;
17-
""")
18-
cr.execute("""
19-
ALTER TABLE sale_order_line ADD COLUMN price_total_no_discount
20-
numeric;
21-
""")
22-
cr.execute("""
23-
ALTER TABLE sale_order_line ADD COLUMN discount_total numeric;
24-
""")
21+
for table, column in COLUMNS:
22+
if not column_exists(cr, table, column):
23+
_logger.info("Create discount column %s in database", column)
24+
create_column(cr, table, column, "numeric")
2525

2626

2727
def post_init_hook(cr, registry):
@@ -30,15 +30,19 @@ def post_init_hook(cr, registry):
3030

3131
query = """
3232
update sale_order_line
33-
set price_total_no_discount = price_total
33+
set
34+
price_subtotal_no_discount = price_subtotal,
35+
price_total_no_discount = price_total
3436
where discount = 0.0
3537
"""
3638
cr.execute(query)
3739

3840
query = """
39-
update sale_order
40-
set price_total_no_discount = amount_total
41-
"""
41+
update sale_order
42+
set
43+
price_subtotal_no_discount = amount_untaxed,
44+
price_total_no_discount = amount_total
45+
"""
4246
cr.execute(query)
4347

4448
query = """
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import logging
2+
3+
from odoo import SUPERUSER_ID
4+
from odoo.api import Environment
5+
6+
_logger = logging.getLogger(__name__)
7+
8+
9+
def migrate(cr, version):
10+
_logger.info("Compute discount columns")
11+
env = Environment(cr, SUPERUSER_ID, {})
12+
13+
query = """
14+
update sale_order_line
15+
set
16+
price_subtotal_no_discount = price_subtotal
17+
where discount = 0.0
18+
"""
19+
cr.execute(query)
20+
21+
query = """
22+
update sale_order
23+
set
24+
price_subtotal_no_discount = amount_untaxed
25+
"""
26+
cr.execute(query)
27+
28+
query = """
29+
select distinct order_id from sale_order_line where discount > 0.0;
30+
"""
31+
32+
cr.execute(query)
33+
order_ids = cr.fetchall()
34+
35+
orders = env["sale.order"].search([("id", "in", order_ids)])
36+
orders.mapped("order_line")._compute_discount()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import logging
2+
3+
from odoo.tools.sql import column_exists, create_column
4+
5+
_logger = logging.getLogger(__name__)
6+
7+
COLUMNS = (
8+
("sale_order", "price_subtotal_no_discount"),
9+
("sale_order_line", "price_subtotal_no_discount"),
10+
)
11+
12+
13+
def migrate(cr, version):
14+
for table, column in COLUMNS:
15+
if not column_exists(cr, table, column):
16+
_logger.info("Create discount column %s in database", column)
17+
create_column(cr, table, column, "numeric")

sale_discount_display_amount/models/sale_order.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,44 @@ class SaleOrder(models.Model):
99
_inherit = 'sale.order'
1010

1111
discount_total = fields.Monetary(
12-
compute='_compute_discount',
12+
compute='_compute_discount_total',
1313
string='Discount Subtotal',
1414
currency_field='currency_id',
1515
store=True)
16+
price_subtotal_no_discount = fields.Monetary(
17+
compute='_compute_discount_total',
18+
string='Subtotal Without Discount',
19+
currency_field='currency_id',
20+
store=True,
21+
)
1622
price_total_no_discount = fields.Monetary(
17-
compute='_compute_discount',
23+
compute='_compute_discount_total',
1824
string='Subtotal Without Discount',
1925
currency_field='currency_id',
2026
store=True)
2127

22-
@api.depends('order_line.discount_total',
23-
'order_line.price_total_no_discount')
24-
def _compute_discount(self):
28+
@api.model
29+
def _get_compute_discount_total_depends(self):
30+
return [
31+
'order_line.discount_total',
32+
'order_line.price_subtotal_no_discount',
33+
'order_line.price_total_no_discount',
34+
]
35+
36+
@api.depends(lambda self: self._get_compute_discount_total_depends())
37+
def _compute_discount_total(self):
2538
for order in self:
2639
discount_total = sum(order.order_line.mapped('discount_total'))
40+
price_subtotal_no_discount = sum(
41+
order.order_line.mapped('price_subtotal_no_discount')
42+
)
2743
price_total_no_discount = sum(
28-
order.order_line.mapped('price_total_no_discount'))
29-
order.update({
30-
'discount_total': discount_total,
31-
'price_total_no_discount': price_total_no_discount
32-
})
44+
order.order_line.mapped('price_total_no_discount')
45+
)
46+
order.update(
47+
{
48+
'discount_total': discount_total,
49+
'price_subtotal_no_discount': price_subtotal_no_discount,
50+
'price_total_no_discount': price_total_no_discount,
51+
}
52+
)

sale_discount_display_amount/models/sale_order_line.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class SaleOrderLine(models.Model):
1212
compute='_compute_amount',
1313
string='Discount Subtotal',
1414
store=True)
15+
price_subtotal_no_discount = fields.Monetary(
16+
compute='_compute_amount',
17+
string='Subtotal Without Discount',
18+
store=True
19+
)
1520
price_total_no_discount = fields.Monetary(
1621
compute='_compute_amount',
1722
string='Subtotal Without Discount',
@@ -21,6 +26,7 @@ def _compute_discount(self):
2126
for line in self:
2227
if not line.discount:
2328
line.price_total_no_discount = line.price_total
29+
line.price_subtotal_no_discount = line.price_subtotal
2430
continue
2531
price = line.price_unit
2632
taxes = line.tax_id.compute_all(
@@ -30,11 +36,13 @@ def _compute_discount(self):
3036
product=line.product_id,
3137
partner=line.order_id.partner_shipping_id)
3238

39+
price_subtotal_no_discount = taxes['total_excluded']
3340
price_total_no_discount = taxes['total_included']
3441
discount_total = price_total_no_discount - line.price_total
3542

3643
line.update({
3744
'discount_total': discount_total,
45+
'price_subtotal_no_discount': price_subtotal_no_discount,
3846
'price_total_no_discount': price_total_no_discount
3947
})
4048

sale_discount_display_amount/tests/test_discount_display_amount.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ def test_sale_discount_value(self):
2424
first_line = so.order_line[0]
2525
first_line.discount = 10
2626

27+
self.assertAlmostEqual(first_line.price_subtotal_no_discount, 30.75)
2728
self.assertAlmostEqual(first_line.price_total_no_discount, 35.36)
2829
self.assertAlmostEqual(first_line.discount_total, 3.53)
29-
self.assertAlmostEqual(so.discount_total, 3.53)
30+
self.assertAlmostEqual(so.price_subtotal_no_discount, 30.75)
3031
self.assertAlmostEqual(so.price_total_no_discount, 35.36)
32+
self.assertAlmostEqual(so.discount_total, 3.53)

sale_discount_display_amount/views/sale_view.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
<field name="priority">99</field>
88
<field name="arch" type="xml">
99
<data>
10-
<xpath expr="//field[@name='amount_tax']" position="after">
11-
<field name="price_total_no_discount"/>
10+
<xpath expr="//field[@name='amount_untaxed']" position="before">
11+
<field name="price_subtotal_no_discount" />
12+
<field name="price_total_no_discount" />
1213
<field name="discount_total"/>
1314
</xpath>
1415
</data>

0 commit comments

Comments
 (0)