diff --git a/account_banking_ach_discount/models/account_payment.py b/account_banking_ach_discount/models/account_payment.py index a03a2066..a35e9158 100644 --- a/account_banking_ach_discount/models/account_payment.py +++ b/account_banking_ach_discount/models/account_payment.py @@ -50,10 +50,8 @@ def action_validate_invoice_payment(self): class AccountPaymentLine(models.Model): _inherit = "account.payment.line" - discount_amount = fields.Monetary(currency_field="currency_id") - total_amount = fields.Monetary( - compute="_compute_total_amount", currency_field="currency_id" - ) + discount_amount = fields.Monetary(currency_field="currency_id",readonly=True) + total_amount = fields.Monetary(currency_field="currency_id",readonly=True) payment_difference_handling = fields.Selection( [("open", "Keep open"), ("reconcile", "Mark invoice as fully paid")], default="reconcile", @@ -73,10 +71,11 @@ class AccountPaymentLine(models.Model): "account.move", related="move_line_id.move_id", store=True ) - @api.depends("amount_currency", "discount_amount") - def _compute_total_amount(self): + @api.onchange("amount_currency") + def _onchange_amount_currency(self): for line in self: - line.total_amount = line.amount_currency + line.payment_difference + if not line.total_amount and not line.payment_difference: + line.total_amount = line.amount_currency @api.model def same_fields_payment_line_and_bank_payment_line(self): diff --git a/account_banking_ach_discount/models/account_payment_order.py b/account_banking_ach_discount/models/account_payment_order.py index d90b051c..3795555b 100644 --- a/account_banking_ach_discount/models/account_payment_order.py +++ b/account_banking_ach_discount/models/account_payment_order.py @@ -19,8 +19,10 @@ def _prepare_move(self, bank_lines=None): temp_vals = vals[2].copy() amount = line.amount_currency + total_amount = line.total_amount + amount_difference = round((total_amount - amount),2) discount = line.discount_amount - payment_difference = line.payment_difference + line.payment_difference = payment_difference = amount_difference writeoff = ( payment_difference and payment_difference - discount or 0.0 ) @@ -29,16 +31,15 @@ def _prepare_move(self, bank_lines=None): "in_invoice", "out_refund", ) - temp_vals["move_id"] = line.move_id.id if use_debit: - temp_vals["debit"] = amount + discount + temp_vals["debit"] = total_amount - payment_difference else: - temp_vals["credit"] = amount + discount + temp_vals["credit"] = total_amount - payment_difference line_ids.append((0, 0, temp_vals)) - if discount > 0: + if discount > 0 and discount == payment_difference: discount_information = line.move_id.invoice_payment_term_id._check_payment_term_discount( line.move_id, line.date ) @@ -55,17 +56,18 @@ def _prepare_move(self, bank_lines=None): if discount_vals: line_ids.append((0, 0, discount_vals)) - if invoice_close and round(writeoff, 2): + if invoice_close : if use_debit: - temp_vals["debit"] = amount + discount + round(writeoff, 2) + temp_vals["debit"] = amount + payment_difference else: - temp_vals["credit"] = amount + discount + round(writeoff, 2) - writeoff_vals = line.move_id._prepare_writeoff_move_line( - line, temp_vals.copy() - ) - writeoff_vals["bank_payment_line_id"] = False - if writeoff_vals: - line_ids.append((0, 0, writeoff_vals)) + temp_vals["credit"] = amount + payment_difference + if round(writeoff, 2): + writeoff_vals = line.move_id._prepare_writeoff_move_line( + line, temp_vals.copy() + ) + writeoff_vals["bank_payment_line_id"] = False + if writeoff_vals: + line_ids.append((0, 0, writeoff_vals)) # payment order line else: line_ids.append(vals) diff --git a/account_banking_ach_discount/wizard/account_payment_register.py b/account_banking_ach_discount/wizard/account_payment_register.py index 45e8cf17..054464c4 100644 --- a/account_banking_ach_discount/wizard/account_payment_register.py +++ b/account_banking_ach_discount/wizard/account_payment_register.py @@ -56,6 +56,7 @@ def make_payments(self): "communication_type": "normal", "amount_currency": line.amount, "payment_difference": line.payment_difference, + "total_amount": line.amount + line.payment_difference, } ) return action