Skip to content

Commit 06b0bde

Browse files
committed
[18.0][MIG] sale_global_discount
1 parent 5640fe6 commit 06b0bde

File tree

6 files changed

+115
-64
lines changed

6 files changed

+115
-64
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: 59 additions & 47 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
@@ -109,7 +111,9 @@ def _compute_amounts(self):
109111
line.price_subtotal, discounts.copy()
110112
)
111113
amount_discounted_untaxed += discounted_subtotal
112-
discounted_tax = line.tax_id.compute_all(
114+
discounted_tax = line.tax_id.with_context(
115+
force_price_include=False
116+
).compute_all(
113117
discounted_subtotal,
114118
line.order_id.currency_id,
115119
1.0,
@@ -143,11 +147,12 @@ def _compute_tax_totals(self):
143147
for order in self:
144148
amount_discount_by_group = {}
145149
cumulative_discount_rate = 1.0
150+
currency = order.currency_id
146151
# Calculate cumulative discount rate
147152
for gbl_disc in order.global_discount_ids:
148153
discount_rate = gbl_disc.discount / 100
149154
cumulative_discount_rate *= 1 - discount_rate
150-
amount_untaxed = 0.0
155+
base_amount = 0.0
151156
# Calculate the total discount amount and discount by tax group
152157
for line in order.order_line:
153158
if line.display_type or not line.product_id:
@@ -159,69 +164,76 @@ def _compute_tax_totals(self):
159164
)
160165
else:
161166
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
167+
base_amount += discounted_price_subtotal
168+
# Calculate tax amounts for each tax group based on the discounted
169+
# subtotal
165170
for tax in line.tax_id:
166171
tax_group_id = tax.tax_group_id.id
167172
if tax_group_id not in amount_discount_by_group:
168173
amount_discount_by_group[tax_group_id] = 0.0
169-
# Calculate correct base amount for tax computation
170-
base_amount = discounted_price_subtotal
171174
# Compute taxes on the correct base amount
172-
discounted_tax_vals = tax.compute_all(
173-
base_amount,
174-
order.currency_id,
175+
discounted_tax_vals = tax.with_context(
176+
force_price_include=False
177+
).compute_all(
178+
discounted_price_subtotal,
179+
currency,
175180
1.0,
176181
product=line.product_id,
177182
partner=order.partner_shipping_id,
178183
)
179-
total_discounted_tax = sum(
184+
amount_discount_by_group[tax_group_id] += sum(
180185
t.get("amount", 0.0)
181186
for t in discounted_tax_vals.get("taxes", [])
182187
)
183-
amount_discount_by_group[tax_group_id] += total_discounted_tax
184188
# 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
189+
total_amount = base_amount + sum(amount_discount_by_group.values())
190+
base_amount_currency = formatLang(
191+
self.env, base_amount, currency_obj=currency
190192
)
191-
order.tax_totals["formatted_amount_total"] = formatLang(
192-
self.env, amount_total, currency_obj=order.currency_id
193+
order.tax_totals.update(
194+
{
195+
"base_amount": base_amount,
196+
"total_amount": total_amount,
197+
"base_amount_currency": base_amount,
198+
"total_amount_currency": total_amount,
199+
}
193200
)
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
201+
# Update subtotals and groups by subtotal
202+
for group in order.tax_totals["subtotals"]:
203+
group.update(
204+
{
205+
"base_amount": base_amount,
206+
"base_amount_currency": base_amount,
207+
"amount": base_amount,
208+
"formatted_amount": base_amount_currency,
209+
}
210210
)
211+
for tax_group in group["tax_groups"]:
212+
discounted_tax_amount = amount_discount_by_group.get(
213+
tax_group["id"], 0.0
214+
)
215+
tax_group.update(
216+
{
217+
"base_amount": base_amount,
218+
"base_amount_currency": base_amount,
219+
"tax_amount": discounted_tax_amount,
220+
"tax_amount_currency": discounted_tax_amount,
221+
}
222+
)
211223
return res
212224

213225
@api.depends("partner_id", "company_id")
214226
def _compute_global_discount_ids(self):
215227
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
228+
commercial_global_disc = (
229+
order.partner_id.commercial_partner_id.customer_global_discount_ids
230+
)
231+
_discounts = (
232+
commercial_global_disc
233+
if commercial_global_disc
234+
else order.partner_id.customer_global_discount_ids
235+
)
219236
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
225237
for discount in _discounts:
226238
if discount.company_id == order.company_id:
227239
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>

0 commit comments

Comments
 (0)