Skip to content

Commit 5fecb97

Browse files
committed
[17.0][MIG] sale_global_discount: Migration to 17.0
1 parent 210389e commit 5fecb97

8 files changed

Lines changed: 117 additions & 27 deletions

File tree

sale_global_discount/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ Contributors
9393

9494
- Omar Castiñeira <omar@comunitea.com>
9595

96+
- `Studio73 <https://www.studio73.es>`__
97+
98+
- Miguel Gandia
99+
96100
Maintainers
97101
-----------
98102

sale_global_discount/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
44
{
55
"name": "Sale Global Discount",
6-
"version": "16.0.1.0.0",
6+
"version": "17.0.1.0.0",
77
"category": "Sales Management",
88
"author": "Tecnativa," "Odoo Community Association (OCA)",
99
"website": "https://github.com/OCA/sale-workflow",

sale_global_discount/hooks.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
from odoo.tools.sql import column_exists
22

33

4-
def _pre_init_global_discount_fields(cr):
5-
if not column_exists(cr, "sale_order", "amount_global_discount"):
6-
cr.execute(
4+
def _pre_init_global_discount_fields(env):
5+
if not column_exists(env.cr, "sale_order", "amount_global_discount"):
6+
env.cr.execute(
77
"""
88
ALTER TABLE "sale_order"
99
ADD COLUMN "amount_global_discount" double precision DEFAULT 0
1010
"""
1111
)
12-
cr.execute(
12+
env.cr.execute(
1313
"""
1414
ALTER TABLE "sale_order" ALTER COLUMN "amount_global_discount" DROP DEFAULT
1515
"""
1616
)
17-
if not column_exists(cr, "sale_order", "amount_untaxed_before_global_discounts"):
18-
cr.execute(
17+
if not column_exists(
18+
env.cr, "sale_order", "amount_untaxed_before_global_discounts"
19+
):
20+
env.cr.execute(
1921
"""
2022
ALTER TABLE "sale_order"
2123
ADD COLUMN "amount_untaxed_before_global_discounts" double precision
2224
"""
2325
)
24-
cr.execute(
26+
env.cr.execute(
2527
"""
2628
update sale_order set amount_untaxed_before_global_discounts = amount_untaxed
2729
"""
2830
)
29-
if not column_exists(cr, "sale_order", "amount_total_before_global_discounts"):
30-
cr.execute(
31+
if not column_exists(env.cr, "sale_order", "amount_total_before_global_discounts"):
32+
env.cr.execute(
3133
"""
3234
ALTER TABLE "sale_order"
3335
ADD COLUMN "amount_total_before_global_discounts" double precision
3436
"""
3537
)
36-
cr.execute(
38+
env.cr.execute(
3739
"""
3840
update sale_order set amount_total_before_global_discounts = amount_total
3941
"""

sale_global_discount/models/sale_order.py

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
44

55
from odoo import _, api, exceptions, fields, models
6+
from odoo.tools.misc import formatLang
67

78

89
class SaleOrder(models.Model):
@@ -14,6 +15,9 @@ class SaleOrder(models.Model):
1415
domain="[('discount_scope', '=', 'sale'), "
1516
"('account_id', '!=', False), '|', "
1617
"('company_id', '=', company_id), ('company_id', '=', False)]",
18+
compute="_compute_global_discount_ids",
19+
store=True,
20+
readonly=False,
1721
)
1822
# HACK: Looks like UI doesn't behave well with Many2many fields and
1923
# negative groups when the same field is shown. In this case, we want to
@@ -68,7 +72,7 @@ def _check_global_discounts_sanity(self):
6872
return True
6973
taxes_keys = {}
7074
for line in self.order_line.filtered(
71-
lambda l: not l.display_type and l.product_id
75+
lambda _line: not _line.display_type and _line.product_id
7276
):
7377
if not line.tax_id:
7478
raise exceptions.UserError(
@@ -135,20 +139,92 @@ def _compute_amounts(self):
135139
return res
136140

137141
def _compute_tax_totals(self):
138-
return super(
139-
SaleOrder, self.with_context(from_tax_calculation=True)
140-
)._compute_tax_totals()
142+
res = super()._compute_tax_totals()
143+
for order in self:
144+
amount_discount_by_group = {}
145+
cumulative_discount_rate = 1.0
146+
# Calculate the total discount amount and discount by tax group
147+
for line in order.order_line:
148+
if (
149+
line.display_type
150+
or not line.product_id
151+
or line.product_id.bypass_global_discount
152+
):
153+
continue
141154

142-
@api.onchange("partner_id")
143-
def onchange_partner_id_set_gbl_disc(self):
144-
self.global_discount_ids = (
145-
self.partner_id.customer_global_discount_ids.filtered(
146-
lambda d: d.company_id == self.company_id
155+
cumulative_discount_rate = 1.0
156+
for gbl_disc in order.global_discount_ids:
157+
discount_rate = gbl_disc.discount / 100
158+
cumulative_discount_rate *= 1 - discount_rate
159+
discounted_price_subtotal = (
160+
line.price_subtotal * cumulative_discount_rate
161+
)
162+
# Calculate tax amounts for each tax group based on the discounted subtotal
163+
for tax in line.tax_id:
164+
tax_group_id = tax.tax_group_id.id
165+
if tax_group_id not in amount_discount_by_group:
166+
amount_discount_by_group[tax_group_id] = 0.0
167+
# Compute taxes on the discounted subtotal
168+
discounted_tax_vals = tax.compute_all(
169+
discounted_price_subtotal,
170+
order.currency_id,
171+
1.0,
172+
product=line.product_id,
173+
partner=order.partner_shipping_id,
174+
)
175+
total_discounted_tax = sum(
176+
t.get("amount", 0.0)
177+
for t in discounted_tax_vals.get("taxes", [])
178+
)
179+
amount_discount_by_group[tax_group_id] += total_discounted_tax
180+
# Update tax totals
181+
amount_untaxed = (
182+
order.amount_untaxed_before_global_discounts * cumulative_discount_rate
183+
)
184+
amount_total = amount_untaxed + sum(amount_discount_by_group.values())
185+
order.tax_totals["amount_untaxed"] = amount_untaxed
186+
order.tax_totals["amount_total"] = amount_total
187+
order.tax_totals["formatted_amount_untaxed"] = formatLang(
188+
self.env, amount_untaxed, currency_obj=order.currency_id
147189
)
148-
or self.partner_id.commercial_partner_id.customer_global_discount_ids.filtered(
149-
lambda d: d.company_id == self.company_id
190+
order.tax_totals["formatted_amount_total"] = formatLang(
191+
self.env, amount_total, currency_obj=order.currency_id
150192
)
151-
)
193+
# Update groups by subtotal
194+
for group in order.tax_totals["groups_by_subtotal"].values():
195+
for tax_group in group:
196+
tax_group_id = tax_group["tax_group_id"]
197+
discount_for_group = amount_discount_by_group.get(tax_group_id, 0.0)
198+
tax_group["tax_group_amount"] = discount_for_group
199+
tax_group["formatted_tax_group_amount"] = formatLang(
200+
self.env,
201+
tax_group["tax_group_amount"],
202+
currency_obj=order.currency_id,
203+
)
204+
# Update subtotals
205+
for subtotal in order.tax_totals["subtotals"]:
206+
subtotal["amount"] = amount_untaxed
207+
subtotal["formatted_amount"] = formatLang(
208+
self.env, amount_untaxed, currency_obj=order.currency_id
209+
)
210+
return res
211+
212+
@api.depends("partner_id", "company_id")
213+
def _compute_global_discount_ids(self):
214+
for order in self:
215+
commercial = order.partner_id.commercial_partner_id
216+
commercial_global_disc = commercial.customer_global_discount_ids
217+
partner_global_disc = order.partner_id.customer_global_discount_ids
218+
discounts = self.env["global.discount"]
219+
_discounts = self.env["global.discount"]
220+
if partner_global_disc:
221+
_discounts = partner_global_disc
222+
else:
223+
_discounts = commercial_global_disc
224+
for discount in _discounts:
225+
if discount.company_id == order.company_id:
226+
discounts |= discount
227+
order.global_discount_ids = discounts
152228

153229
def _prepare_invoice(self):
154230
invoice_vals = super()._prepare_invoice()

sale_global_discount/readme/CONTRIBUTORS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
- David Vidal
33
- Pedro M. Baeza
44
- Omar Castiñeira \<<omar@comunitea.com>\>
5+
6+
- [Studio73](https://www.studio73.es)
7+
- Miguel Gandia
8+

sale_global_discount/static/description/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
441441
</ul>
442442
</li>
443443
<li>Omar Castiñeira &lt;<a class="reference external" href="mailto:omar&#64;comunitea.com">omar&#64;comunitea.com</a>&gt;</li>
444+
<li><a class="reference external" href="https://www.studio73.es">Studio73</a><ul>
445+
<li>Miguel Gandia</li>
446+
</ul>
447+
</li>
444448
</ul>
445449
</div>
446450
<div class="section" id="maintainers">

sale_global_discount/tests/test_sale_global_discount.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def setUpClass(cls, chart_template_ref=None):
5252
"account_id": cls.account.id,
5353
}
5454
)
55-
cls.pricelist = cls.env.ref("product.list0")
55+
cls.pricelist = cls.env["product.pricelist"].create(
56+
{"name": "Public Pricelist", "sequence": 1}
57+
)
5658
cls.partner_1 = cls.env["res.partner"].create(
5759
{"name": "Mr. Odoo", "property_product_pricelist": cls.pricelist.id}
5860
)
@@ -157,7 +159,6 @@ def test_02_global_sale_discounts_from_partner(self):
157159
"""Change the partner and his global discounts go to the invoice"""
158160
# (30% then 50%)
159161
self.sale.partner_id = self.partner_2
160-
self.sale.onchange_partner_id_set_gbl_disc()
161162
self.assertAlmostEqual(self.sale.amount_global_discount, 162.49)
162163
self.assertAlmostEqual(self.sale.amount_untaxed, 87.5)
163164
self.assertAlmostEqual(self.sale.amount_untaxed_before_global_discounts, 249.99)
@@ -171,7 +172,6 @@ def test_02_global_sale_discounts_from_partner(self):
171172
def test_03_global_sale_discounts_to_invoice(self):
172173
"""All the discounts go to the invoice"""
173174
self.sale.partner_id = self.partner_2
174-
self.sale.onchange_partner_id_set_gbl_disc()
175175
self.sale.order_line.mapped("product_id").write({"invoice_policy": "order"})
176176
self.sale.action_confirm()
177177
move = self.sale._create_invoices()

sale_global_discount/views/report_sale_order.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<strong>Global Discounts</strong>
2323
<br />
2424
<t
25-
t-esc="'→'.join(['{:.2f}%'.format(x.discount) for x in doc.global_discount_ids])"
25+
t-out="'→'.join(['{:.2f}%'.format(x.discount) for x in doc.global_discount_ids])"
2626
/>
2727
</td>
2828
<td class="text-end">

0 commit comments

Comments
 (0)