Skip to content

Commit

Permalink
[FIX] account_cash_discount_write_off: allow batch payments
Browse files Browse the repository at this point in the history
  • Loading branch information
AnizR committed Nov 27, 2023
1 parent 11c0df0 commit 3000030
Showing 1 changed file with 64 additions and 55 deletions.
119 changes: 64 additions & 55 deletions account_cash_discount_write_off/models/account_payment_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,77 @@ def _prepare_account_payment_vals(self):
"""Prepare the dictionary to create an account payment record from a set of
payment lines.
"""

payment_vals = super()._prepare_account_payment_vals()
currency = self.currency_id
conversion_rate = self.env["res.currency"]._get_conversion_rate(
currency,
self.company_id.currency_id,
self.company_id,
self.date,
)

epd_aml_values_list = []
aml = self.move_line_id
date_for_discount = self.date
if self.pay_with_discount:
# force aml to be eligible for early payment_discount
date_for_discount = aml.discount_date
if (
aml._is_eligible_for_early_payment_discount(currency, date_for_discount)
or self.pay_with_discount
):
epd_aml_values_list.append(
{
"aml": aml,
"amount_currency": -aml.amount_residual_currency,
"balance": aml.company_currency_id.round(
-aml.amount_residual_currency * conversion_rate
),
}
)
for rec in self:
currency = rec.currency_id
conversion_rate = rec.env["res.currency"]._get_conversion_rate(
currency,
rec.company_id.currency_id,
rec.company_id,
rec.date,
)

aml = rec.move_line_id
date_for_discount = rec.date
if rec.pay_with_discount:
# force aml to be eligible for early payment_discount
date_for_discount = aml.discount_date
if (
aml._is_eligible_for_early_payment_discount(
currency, date_for_discount
)
or rec.pay_with_discount
):
epd_aml_values_list.append(
{
"aml": aml,
"amount_currency": -aml.amount_residual_currency,
"balance": aml.company_currency_id.round(
-aml.amount_residual_currency * conversion_rate
),
}
)

# compute open amount
invoice = rec.move_line_id.move_id
refunds_amount_total = 0
sign = (
-1 if invoice.payment_mode_id.payment_type == "outbound" else 1
)
for invoice_line in invoice.line_ids:
# compute discount amount
if invoice_line.currency_id:
amount_of_discount = invoice_line.amount_residual_currency
else:
amount_of_discount = invoice_line.amount_residual

Check warning on line 59 in account_cash_discount_write_off/models/account_payment_line.py

View check run for this annotation

Codecov / codecov/patch

account_cash_discount_write_off/models/account_payment_line.py#L59

Added line #L59 was not covered by tests

# compute open amount
invoice = self.move_line_id.move_id
refunds_amount_total = 0
sign = -1 if invoice.payment_mode_id.payment_type == "outbound" else 1
for invoice_line in invoice.line_ids:
# compute discount amount
if invoice_line.currency_id:
amount_of_discount = invoice_line.amount_residual_currency
else:
amount_of_discount = invoice_line.amount_residual
if invoice_line.move_id.is_invoice():
amount_of_discount *= -1

if invoice_line.move_id.is_invoice():
amount_of_discount *= -1
# apply discount
amount_of_discount *= 1 - (
invoice_line.discount_percentage / 100
)
refunds_amount_total += amount_of_discount

# apply discount
amount_of_discount *= 1 - (invoice_line.discount_percentage / 100)
refunds_amount_total += amount_of_discount
open_amount_currency = (
invoice.amount_residual - refunds_amount_total
) * sign

open_amount_currency = (
invoice.amount_residual - refunds_amount_total
) * sign
open_balance = rec.company_id.currency_id.round(
open_amount_currency * conversion_rate
)
if epd_aml_values_list:
early_payment_values = rec.env[
"account.move"
]._get_invoice_counterpart_amls_for_early_payment_discount(
epd_aml_values_list, open_balance
)
payment_vals["write_off_line_vals"] = []

open_balance = self.company_id.currency_id.round(
open_amount_currency * conversion_rate
)
early_payment_values = self.env[
"account.move"
]._get_invoice_counterpart_amls_for_early_payment_discount(
epd_aml_values_list, open_balance
)
payment_vals["write_off_line_vals"] = []
for aml_values_list in early_payment_values.values():
payment_vals["write_off_line_vals"] += aml_values_list
for aml_values_list in early_payment_values.values():
payment_vals["write_off_line_vals"] += aml_values_list

return payment_vals

0 comments on commit 3000030

Please sign in to comment.