refactor: remove redundant manual deduction line synchronization in account payment move synchronization

This commit is contained in:
Suherdy Yacob 2026-04-06 15:00:45 +07:00
parent 04e6bc1496
commit 3b6e99df13

View File

@ -47,50 +47,9 @@ class AccountPayment(models.Model):
else: else:
super()._synchronize_to_moves(changed_fields) super()._synchronize_to_moves(changed_fields)
# 2. Custom Deduction Synchronization # The base _synchronize_to_moves naturally calls _prepare_move_line_default_vals.
# After base sync, we "fix" the lines if deductions are present # vendor_payment_diff_amount ALREADY overrides _prepare_move_line_default_vals to handle Deductions.
for payment in self.with_context(skip_expense_lock=True, skip_account_move_synchronization=True): # So we don't need to manually recreate deduction lines here—just jumping the lock above is enough!
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)
# Odoo 17 handles balance checks automatically; no need to call _check_balanced() manually # Odoo 17 handles balance checks automatically; no need to call _check_balanced() manually