Skip to content

Commit

Permalink
[FIX] account_payment_term: Add cash_rounding support in _compute_ter…
Browse files Browse the repository at this point in the history
…ms()

Required since odoo/odoo@84b03f7
  • Loading branch information
victoralmau committed Aug 30, 2024
1 parent 85d2ed9 commit 7544e4e
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions account_payment_term_extension/models/account_payment_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def _compute_terms(
sign,
untaxed_amount,
untaxed_amount_currency,
cash_rounding=None,
):
"""Complete overwrite of compute method for adding extra options."""
# FIXME: Find an inheritable way of doing this
Expand All @@ -240,6 +241,8 @@ def _compute_terms(
total_amount_currency = remaining_amount_currency = (
tax_amount_currency + untaxed_amount_currency
)
foreign_rounding_amount = 0
company_rounding_amount = 0
result = []
precision_digits = currency.decimal_places
company_precision_digits = company_currency.decimal_places
Expand Down Expand Up @@ -350,15 +353,44 @@ def _compute_terms(
tax_amount_currency_left -= line_tax_amount_currency
untaxed_amount_left -= line_untaxed_amount
untaxed_amount_currency_left -= line_untaxed_amount_currency

if cash_rounding and line.value in ["fixed", "percent"]:
cash_rounding_difference_currency = cash_rounding.compute_difference(
currency, term_vals["foreign_amount"]
)
if not currency.is_zero(cash_rounding_difference_currency):
rate = (
abs(term_vals["foreign_amount"] / term_vals["company_amount"])
if term_vals["company_amount"]
else 1.0
)

foreign_rounding_amount += cash_rounding_difference_currency
term_vals["foreign_amount"] += cash_rounding_difference_currency

company_amount = company_currency.round(
term_vals["foreign_amount"] / rate
)
cash_rounding_difference = (
company_amount - term_vals["company_amount"]
)
if not currency.is_zero(cash_rounding_difference):
company_rounding_amount += cash_rounding_difference
term_vals["company_amount"] = company_amount

remaining_amount = tax_amount_left + untaxed_amount_left
remaining_amount_currency = (
tax_amount_currency_left + untaxed_amount_currency_left
)

if line.value == "balance":
term_vals["company_amount"] = tax_amount_left + untaxed_amount_left
term_vals["company_amount"] = (
tax_amount_left + untaxed_amount_left - company_rounding_amount
)
term_vals["foreign_amount"] = (
tax_amount_currency_left + untaxed_amount_currency_left
tax_amount_currency_left
+ untaxed_amount_currency_left
- foreign_rounding_amount
)
line_tax_amount = tax_amount_left
line_tax_amount_currency = tax_amount_currency_left
Expand Down Expand Up @@ -390,5 +422,25 @@ def _compute_terms(
days=line.discount_days
)

if cash_rounding and line.discount_percentage:
cash_rounding_difference_currency = cash_rounding.compute_difference(
currency, term_vals["discount_amount_currency"]
)
if not currency.is_zero(cash_rounding_difference_currency):
rate = (
abs(
term_vals["discount_amount_currency"]
/ term_vals["discount_balance"]
)
if term_vals["discount_balance"]
else 1.0
)
term_vals[
"discount_amount_currency"
] += cash_rounding_difference_currency
term_vals["discount_balance"] = company_currency.round(
term_vals["discount_amount_currency"] / rate
)

result.append(term_vals)
return result

0 comments on commit 7544e4e

Please sign in to comment.