From 3b6e99df130437908bb32a4deae1ba7a86c546d7 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Mon, 6 Apr 2026 15:00:45 +0700 Subject: [PATCH] refactor: remove redundant manual deduction line synchronization in account payment move synchronization --- models/account_payment.py | 47 +++------------------------------------ 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/models/account_payment.py b/models/account_payment.py index 2655102..1c0b448 100644 --- a/models/account_payment.py +++ b/models/account_payment.py @@ -47,50 +47,9 @@ class AccountPayment(models.Model): else: super()._synchronize_to_moves(changed_fields) - # 2. Custom Deduction Synchronization - # After base sync, we "fix" the lines if deductions are present - for payment in self.with_context(skip_expense_lock=True, skip_account_move_synchronization=True): - if payment.amount_substract and payment.amount_substract > 0 and payment.move_id: - liquidity_lines, counterpart_lines, writeoff_lines = payment._seek_for_lines() - - # Adjust liquidity line to final_payment_amount - for line in liquidity_lines: - final_amount = payment.final_payment_amount - line_vals = { - 'amount_currency': -final_amount if payment.payment_type == 'outbound' else final_amount, - 'debit': payment.currency_id._convert(final_amount, payment.company_id.currency_id, payment.company_id, payment.date) if payment.payment_type == 'inbound' else 0.0, - 'credit': payment.currency_id._convert(final_amount, payment.company_id.currency_id, payment.company_id, payment.date) if payment.payment_type == 'outbound' else 0.0, - } - line.write(line_vals) - - # Deduction lines management - existing_deduction_accounts = payment.deduction_line_ids.mapped('substract_account_id') - other_lines = payment.move_id.line_ids - liquidity_lines - counterpart_lines - writeoff_lines - to_delete = other_lines.filtered(lambda l: l.account_id.id in existing_deduction_accounts.ids) - if to_delete: - to_delete.unlink() - - # Re-add deduction lines - for deduction in payment.deduction_line_ids: - deduction_amount = deduction.amount_substract - deduction_balance = payment.currency_id._convert( - deduction_amount, - payment.company_id.currency_id, - payment.company_id, - payment.date, - ) - - line_vals = { - 'name': deduction.name or _('Payment Deduction: %s') % deduction.substract_account_id.name, - 'move_id': payment.move_id.id, - 'date_maturity': payment.date, - 'amount_currency': -deduction_amount if payment.payment_type == 'outbound' else deduction_amount, - 'currency_id': payment.currency_id.id, - 'debit': deduction_balance if payment.payment_type == 'inbound' else 0.0, - 'credit': deduction_balance if payment.payment_type == 'outbound' else 0.0, - 'account_id': deduction.substract_account_id.id, - } - payment.env['account.move.line'].with_context(skip_expense_lock=True, check_move_validity=False).create(line_vals) + # The base _synchronize_to_moves naturally calls _prepare_move_line_default_vals. + # vendor_payment_diff_amount ALREADY overrides _prepare_move_line_default_vals to handle Deductions. + # So we don't need to manually recreate deduction lines here—just jumping the lock above is enough! # Odoo 17 handles balance checks automatically; no need to call _check_balanced() manually