diff --git a/account_payment_term_extension/models/account_payment_term.py b/account_payment_term_extension/models/account_payment_term.py index a21615f7b96f..a58a7a06b252 100644 --- a/account_payment_term_extension/models/account_payment_term.py +++ b/account_payment_term_extension/models/account_payment_term.py @@ -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 @@ -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 @@ -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 @@ -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