Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] account_invoice_triple_discount: Discount rounding #1606

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion account_invoice_triple_discount/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Account Invoice Triple Discount
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:89bbe50c0f673cb6d1bce75de0d3031cc5e37537320b310b3bb29bac0ee7724a
!! source digest: sha256:9ad7382e62c36c90b601a9fc2a61570cc2eaa85957cf82e46d405a4718dda37d
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -81,6 +81,9 @@ Contributors
* David Vidal <[email protected]>
* Pedro M. Baeza <[email protected]>
* Nikul Chaudhary <[email protected]>
* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>

Maintainers
~~~~~~~~~~~
Expand Down
26 changes: 25 additions & 1 deletion account_invoice_triple_discount/models/account_move.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Copyright 2017 Tecnativa - David Vidal
# Copyright 2017 Tecnativa - Pedro M. Baeza
# Copyright 2023 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models
from odoo import api, models


class AccountMove(models.Model):
Expand All @@ -17,3 +18,26 @@ def _has_discount(self):
for line in self.invoice_line_ids
]
)

@api.model
def _field_will_change(self, record, vals, field_name):
result = super()._field_will_change(record, vals, field_name)
is_discount_field = (
record._name == self.line_ids._name and field_name == "discount"
)
if (
not result
and self.env.context.get("restoring_triple_discount")
and is_discount_field
):
# Discount value in the cache has many digits (e.g. 100.00000000000009),
# but we have just restored the original digits (e.g. 2) in the field
# and want to write the correct value (e.g. 100.00).
# The method in super compares:
# - the cache value rounded with the field's digits: 100.00
# - the value we want to write: 100.00
# and concludes that the value won't change,
# thus leaving the cache value (100.00000000000009)
# instead of updating the cache with the value we actually want (100.00)
result = True
return result
9 changes: 7 additions & 2 deletions account_invoice_triple_discount/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2020 ACSONE SA/NV
# Copyright 2023 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import functools
Expand Down Expand Up @@ -81,7 +82,9 @@ def _compute_totals(self):
for line in self:
if line.id not in old_values_by_line_id:
continue
line.update(old_values_by_line_id[line.id])
line.with_context(
restoring_triple_discount=True,
).update(old_values_by_line_id[line.id])
return res

@api.depends(
Expand Down Expand Up @@ -114,7 +117,9 @@ def _compute_all_tax(self):
for line in self:
if line.id not in old_values_by_line_id:
continue
line.update(old_values_by_line_id[line.id])
line.with_context(
restoring_triple_discount=True,
).update(old_values_by_line_id[line.id])
return res

def _convert_to_tax_base_line_dict(self):
Expand Down
3 changes: 3 additions & 0 deletions account_invoice_triple_discount/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* David Vidal <[email protected]>
* Pedro M. Baeza <[email protected]>
* Nikul Chaudhary <[email protected]>
* `Aion Tech <https://aiontech.company/>`_:

* Simone Rubino <[email protected]>
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Account Invoice Triple Discount</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:89bbe50c0f673cb6d1bce75de0d3031cc5e37537320b310b3bb29bac0ee7724a
!! source digest: sha256:9ad7382e62c36c90b601a9fc2a61570cc2eaa85957cf82e46d405a4718dda37d
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/account-invoicing/tree/16.0/account_invoice_triple_discount"><img alt="OCA/account-invoicing" src="https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/account-invoicing-16-0/account-invoicing-16-0-account_invoice_triple_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/account-invoicing&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows to have three successive discounts on each invoice line.</p>
Expand Down Expand Up @@ -430,6 +430,10 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<li>David Vidal &lt;<a class="reference external" href="mailto:david.vidal&#64;tecnativa.com">david.vidal&#64;tecnativa.com</a>&gt;</li>
<li>Pedro M. Baeza &lt;<a class="reference external" href="mailto:pedro.baeza&#64;tecnativa.com">pedro.baeza&#64;tecnativa.com</a>&gt;</li>
<li>Nikul Chaudhary &lt;<a class="reference external" href="mailto:nikulchaudhary2112&#64;gmail.com">nikulchaudhary2112&#64;gmail.com</a>&gt;</li>
<li><a class="reference external" href="https://aiontech.company/">Aion Tech</a>:<ul>
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;aion-tech.it">simone.rubino&#64;aion-tech.it</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2017 Tecnativa - David Vidal
# Copyright 2023 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests import TransactionCase
Expand Down Expand Up @@ -186,3 +187,18 @@ def test_05_has_discount(self):
line_form.discount3 = 50.0
invoice_form.save()
self.assertTrue(invoice._has_discount())

def test_06_round_discount(self):
"""Discount value is rounded correctly"""
invoice = self.create_simple_invoice(0)
invoice_line = invoice.invoice_line_ids[0]
invoice_line.discount = 100
self.assertEqual(invoice_line.discount, 100)

def test_07_round_tax_discount(self):
"""Discount value is rounded correctly when taxes change"""
invoice = self.create_simple_invoice(0)
invoice_line = invoice.invoice_line_ids[0]
invoice_line.discount = 100
invoice_line.tax_ids = False
self.assertEqual(invoice_line.discount, 100)
Loading