Skip to content

Commit 00f6ac9

Browse files
committed
[18.0][MIG] sale_global_discount
1 parent 5640fe6 commit 00f6ac9

File tree

6 files changed

+79
-61
lines changed

6 files changed

+79
-61
lines changed

sale_global_discount/README.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Sale Global Discount
1717
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1818
:alt: License: AGPL-3
1919
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github
20-
:target: https://github.com/OCA/sale-workflow/tree/17.0/sale_global_discount
20+
:target: https://github.com/OCA/sale-workflow/tree/18.0/sale_global_discount
2121
:alt: OCA/sale-workflow
2222
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23-
:target: https://translation.odoo-community.org/projects/sale-workflow-17-0/sale-workflow-17-0-sale_global_discount
23+
:target: https://translation.odoo-community.org/projects/sale-workflow-18-0/sale-workflow-18-0-sale_global_discount
2424
:alt: Translate me on Weblate
2525
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26-
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&target_branch=17.0
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&target_branch=18.0
2727
:alt: Try me on Runboat
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -70,7 +70,7 @@ Bug Tracker
7070
Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-workflow/issues>`_.
7171
In case of trouble, please check there if your issue has already been reported.
7272
If you spotted it first, help us to smash it by providing a detailed and welcomed
73-
`feedback <https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_global_discount%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
73+
`feedback <https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_global_discount%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
7474

7575
Do not contact contributors directly about support or help with technical issues.
7676

@@ -95,6 +95,7 @@ Contributors
9595
- `Studio73 <https://www.studio73.es>`__
9696

9797
- Miguel Gandia
98+
- Eugenio Micó
9899

99100
Maintainers
100101
-----------
@@ -109,6 +110,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
109110
mission is to support the collaborative development of Odoo features and
110111
promote its widespread use.
111112

112-
This module is part of the `OCA/sale-workflow <https://github.com/OCA/sale-workflow/tree/17.0/sale_global_discount>`_ project on GitHub.
113+
This module is part of the `OCA/sale-workflow <https://github.com/OCA/sale-workflow/tree/18.0/sale_global_discount>`_ project on GitHub.
113114

114115
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

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": "17.0.1.0.0",
6+
"version": "18.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/models/sale_order.py

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright 2020 Tecnativa - Pedro M. Baeza
33
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
44

5-
from odoo import _, api, exceptions, fields, models
5+
from odoo import api, exceptions, fields, models
66
from odoo.tools.misc import formatLang
77

88

@@ -71,18 +71,20 @@ def _check_global_discounts_sanity(self):
7171
return True
7272
taxes_keys = {}
7373
for line in self.order_line.filtered(
74-
lambda _line: not _line.display_type and _line.product_id
74+
lambda _line: not _line.display_type
75+
and _line.product_id
76+
and not _line.product_id.bypass_global_discount
7577
):
7678
if not line.tax_id:
7779
raise exceptions.UserError(
78-
_("With global discounts, taxes in lines are required.")
80+
self.env._("With global discounts, taxes in lines are required.")
7981
)
8082
for key in taxes_keys:
8183
if key == line.tax_id:
8284
break
8385
elif key & line.tax_id:
8486
raise exceptions.UserError(
85-
_("Incompatible taxes found for global discounts.")
87+
self.env._("Incompatible taxes found for global discounts.")
8688
)
8789
else:
8890
taxes_keys[line.tax_id] = True
@@ -143,11 +145,12 @@ def _compute_tax_totals(self):
143145
for order in self:
144146
amount_discount_by_group = {}
145147
cumulative_discount_rate = 1.0
148+
currency = order.currency_id
146149
# Calculate cumulative discount rate
147150
for gbl_disc in order.global_discount_ids:
148151
discount_rate = gbl_disc.discount / 100
149152
cumulative_discount_rate *= 1 - discount_rate
150-
amount_untaxed = 0.0
153+
base_amount = 0.0
151154
# Calculate the total discount amount and discount by tax group
152155
for line in order.order_line:
153156
if line.display_type or not line.product_id:
@@ -159,69 +162,74 @@ def _compute_tax_totals(self):
159162
)
160163
else:
161164
discounted_price_subtotal = line.price_subtotal
162-
amount_untaxed += discounted_price_subtotal
163-
# Calculate tax amounts for each tax group based on the
164-
# discounted subtotal
165+
base_amount += discounted_price_subtotal
166+
# Calculate tax amounts for each tax group based on the discounted
167+
# subtotal
165168
for tax in line.tax_id:
166169
tax_group_id = tax.tax_group_id.id
167170
if tax_group_id not in amount_discount_by_group:
168171
amount_discount_by_group[tax_group_id] = 0.0
169-
# Calculate correct base amount for tax computation
170-
base_amount = discounted_price_subtotal
171172
# Compute taxes on the correct base amount
172173
discounted_tax_vals = tax.compute_all(
173-
base_amount,
174-
order.currency_id,
174+
discounted_price_subtotal,
175+
currency,
175176
1.0,
176177
product=line.product_id,
177178
partner=order.partner_shipping_id,
178179
)
179-
total_discounted_tax = sum(
180+
amount_discount_by_group[tax_group_id] += sum(
180181
t.get("amount", 0.0)
181182
for t in discounted_tax_vals.get("taxes", [])
182183
)
183-
amount_discount_by_group[tax_group_id] += total_discounted_tax
184184
# Calculate the final amount total
185-
amount_total = amount_untaxed + sum(amount_discount_by_group.values())
186-
order.tax_totals["amount_untaxed"] = amount_untaxed
187-
order.tax_totals["amount_total"] = amount_total
188-
order.tax_totals["formatted_amount_untaxed"] = formatLang(
189-
self.env, amount_untaxed, currency_obj=order.currency_id
185+
total_amount = base_amount + sum(amount_discount_by_group.values())
186+
base_amount_currency = formatLang(
187+
self.env, base_amount, currency_obj=currency
190188
)
191-
order.tax_totals["formatted_amount_total"] = formatLang(
192-
self.env, amount_total, currency_obj=order.currency_id
189+
order.tax_totals.update(
190+
{
191+
"base_amount": base_amount,
192+
"total_amount": total_amount,
193+
"base_amount_currency": base_amount,
194+
"total_amount_currency": total_amount,
195+
}
193196
)
194-
# Update groups by subtotal
195-
for group in order.tax_totals["groups_by_subtotal"].values():
196-
for tax_group in group:
197-
tax_group_id = tax_group["tax_group_id"]
198-
discount_for_group = amount_discount_by_group.get(tax_group_id, 0.0)
199-
tax_group["tax_group_amount"] = discount_for_group
200-
tax_group["formatted_tax_group_amount"] = formatLang(
201-
self.env,
202-
tax_group["tax_group_amount"],
203-
currency_obj=order.currency_id,
204-
)
205-
# Update subtotals
206-
for subtotal in order.tax_totals["subtotals"]:
207-
subtotal["amount"] = amount_untaxed
208-
subtotal["formatted_amount"] = formatLang(
209-
self.env, amount_untaxed, currency_obj=order.currency_id
197+
# Update subtotals and groups by subtotal
198+
for group in order.tax_totals["subtotals"]:
199+
group.update(
200+
{
201+
"base_amount": base_amount,
202+
"base_amount_currency": base_amount,
203+
"amount": base_amount,
204+
"formatted_amount": base_amount_currency,
205+
}
210206
)
207+
for tax_group in group["tax_groups"]:
208+
discounted_tax_amount = amount_discount_by_group.get(
209+
tax_group["id"], 0.0
210+
)
211+
tax_group.update(
212+
{
213+
"base_amount": base_amount,
214+
"base_amount_currency": base_amount,
215+
"tax_amount": discounted_tax_amount,
216+
"tax_amount_currency": discounted_tax_amount,
217+
}
218+
)
211219
return res
212220

213221
@api.depends("partner_id", "company_id")
214222
def _compute_global_discount_ids(self):
215223
for order in self:
216-
commercial = order.partner_id.commercial_partner_id
217-
commercial_global_disc = commercial.customer_global_discount_ids
218-
partner_global_disc = order.partner_id.customer_global_discount_ids
224+
commercial_global_disc = (
225+
order.partner_id.commercial_partner_id.customer_global_discount_ids
226+
)
227+
_discounts = (
228+
commercial_global_disc
229+
if commercial_global_disc
230+
else order.partner_id.customer_global_discount_ids
231+
)
219232
discounts = self.env["global.discount"]
220-
_discounts = self.env["global.discount"]
221-
if partner_global_disc:
222-
_discounts = partner_global_disc
223-
else:
224-
_discounts = commercial_global_disc
225233
for discount in _discounts:
226234
if discount.company_id == order.company_id:
227235
discounts |= discount

sale_global_discount/readme/CONTRIBUTORS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
- [Studio73](https://www.studio73.es)
77
- Miguel Gandia
8-
8+
- Eugenio Micó

sale_global_discount/static/description/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ <h1 class="title">Sale Global Discount</h1>
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370370
!! source digest: sha256:0639903acb4ff0d9ec7a5f120af7b75f65034ffefc2c87b37008a214f2f466ab
371371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/sale-workflow/tree/17.0/sale_global_discount"><img alt="OCA/sale-workflow" src="https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/sale-workflow-17-0/sale-workflow-17-0-sale_global_discount"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/sale-workflow/tree/18.0/sale_global_discount"><img alt="OCA/sale-workflow" src="https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/sale-workflow-18-0/sale-workflow-18-0-sale_global_discount"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373373
<p>Apply global financial discounts to sales that will be transmited to
374374
invoices and accounting.</p>
375375
<p><strong>Table of contents</strong></p>
@@ -420,7 +420,7 @@ <h1><a class="toc-backref" href="#toc-entry-4">Bug Tracker</a></h1>
420420
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/sale-workflow/issues">GitHub Issues</a>.
421421
In case of trouble, please check there if your issue has already been reported.
422422
If you spotted it first, help us to smash it by providing a detailed and welcomed
423-
<a class="reference external" href="https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_global_discount%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
423+
<a class="reference external" href="https://github.com/OCA/sale-workflow/issues/new?body=module:%20sale_global_discount%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
424424
<p>Do not contact contributors directly about support or help with technical issues.</p>
425425
</div>
426426
<div class="section" id="credits">
@@ -442,6 +442,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
442442
<li>Omar Castiñeira &lt;<a class="reference external" href="mailto:omar&#64;comunitea.com">omar&#64;comunitea.com</a>&gt;</li>
443443
<li><a class="reference external" href="https://www.studio73.es">Studio73</a><ul>
444444
<li>Miguel Gandia</li>
445+
<li>Eugenio Micó</li>
445446
</ul>
446447
</li>
447448
</ul>
@@ -455,7 +456,7 @@ <h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
455456
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
456457
mission is to support the collaborative development of Odoo features and
457458
promote its widespread use.</p>
458-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/sale-workflow/tree/17.0/sale_global_discount">OCA/sale-workflow</a> project on GitHub.</p>
459+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/sale-workflow/tree/18.0/sale_global_discount">OCA/sale-workflow</a> project on GitHub.</p>
459460
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
460461
</div>
461462
</div>

sale_global_discount/tests/test_sale_global_discount.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
class TestSaleGlobalDiscount(AccountTestInvoicingCommon):
1212
@classmethod
1313
def setUpClass(cls, chart_template_ref=None):
14-
super().setUpClass(chart_template_ref=chart_template_ref)
14+
super().setUpClass()
15+
cls.chart_template = chart_template_ref
1516
cls.env.ref("base_global_discount.group_global_discount").write(
1617
{"users": [(4, cls.env.user.id)]}
1718
)
@@ -105,11 +106,14 @@ def setUpClass(cls, chart_template_ref=None):
105106
order_line.price_unit = 33.33
106107
cls.sale = sale_form.save()
107108

108-
def get_taxes_widget_total_tax(self, order):
109-
return sum(
110-
tax_vals["tax_group_amount"]
111-
for tax_vals in order.tax_totals["groups_by_subtotal"]["Untaxed Amount"]
112-
)
109+
@staticmethod
110+
def get_taxes_widget_total_tax(order):
111+
tax_amount_total = 0.0
112+
for subtotal in order.tax_totals["subtotals"]:
113+
tax_amount_total = sum(
114+
tax_group["tax_amount"] for tax_group in subtotal["tax_groups"]
115+
)
116+
return tax_amount_total
113117

114118
def test_01_global_sale_succesive_discounts(self):
115119
"""Add global discounts to the sale order"""
@@ -121,6 +125,7 @@ def test_01_global_sale_succesive_discounts(self):
121125
self.assertAlmostEqual(self.sale.amount_untaxed, 249.99)
122126
# Apply a single 20% global discount
123127
self.sale.global_discount_ids = self.global_discount_1
128+
self.sale._compute_tax_totals()
124129
# Discount is computed over the base and global taxes are computed
125130
# according to it line by line with the core method
126131
self.assertAlmostEqual(self.sale.amount_global_discount, 50)
@@ -134,6 +139,7 @@ def test_01_global_sale_succesive_discounts(self):
134139
)
135140
# Apply an additional 30% global discount
136141
self.sale.global_discount_ids += self.global_discount_2
142+
self.sale._compute_tax_totals()
137143
self.assertAlmostEqual(self.sale.amount_global_discount, 110)
138144
self.assertAlmostEqual(self.sale.amount_untaxed, 139.99)
139145
self.assertAlmostEqual(self.sale.amount_untaxed_before_global_discounts, 249.99)
@@ -159,6 +165,7 @@ def test_02_global_sale_discounts_from_partner(self):
159165
"""Change the partner and his global discounts go to the invoice"""
160166
# (30% then 50%)
161167
self.sale.partner_id = self.partner_2
168+
self.sale._compute_tax_totals()
162169
self.assertAlmostEqual(self.sale.amount_global_discount, 162.49)
163170
self.assertAlmostEqual(self.sale.amount_untaxed, 87.5)
164171
self.assertAlmostEqual(self.sale.amount_untaxed_before_global_discounts, 249.99)
@@ -172,6 +179,7 @@ def test_02_global_sale_discounts_from_partner(self):
172179
def test_03_global_sale_discounts_to_invoice(self):
173180
"""All the discounts go to the invoice"""
174181
self.sale.partner_id = self.partner_2
182+
self.sale._compute_tax_totals()
175183
self.sale.order_line.mapped("product_id").write({"invoice_policy": "order"})
176184
self.sale.action_confirm()
177185
move = self.sale._create_invoices()

0 commit comments

Comments
 (0)