refactor: replace direct base class invocation with SQL-based bypass for expense sheet synchronization in account payment

This commit is contained in:
Suherdy Yacob 2026-04-06 12:18:26 +07:00
parent 4261b6c53c
commit 14c7489ae0

View File

@ -1,8 +1,5 @@
from odoo import fields, models, api, _
from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)
class AccountPayment(models.Model):
_inherit = 'account.payment'
@ -16,31 +13,46 @@ class AccountPayment(models.Model):
return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post()
def _synchronize_to_moves(self, changed_fields):
# Always allow sync if bypass flag is set
# Force the refresh by ensuring 'amount' is in fields if deductions are involved
if 'deduction_line_ids' in changed_fields or 'amount_substract' in changed_fields:
if 'amount' not in changed_fields:
changed_fields = set(changed_fields) | {'amount'}
# BYPASS LOGIC: Temporarily disconnect to skip hr_expense UserError while keeping other logic
if self._context.get('skip_expense_lock'):
# Force the refresh by ensuring 'amount' is in fields
if 'deduction_line_ids' in changed_fields or 'amount_substract' in changed_fields:
if 'amount' not in changed_fields:
changed_fields = set(changed_fields) | {'amount'}
# Store original values
links = {p.id: p.expense_sheet_id.id for p in self if p.expense_sheet_id}
# SURGICAL BYPASS:
# We call the base 'account.payment' method directly, skipping the 'hr_expense' override
# that raises the UserError.
from odoo.addons.account.models.account_payment import AccountPayment as AccountPaymentBase
return AccountPaymentBase._synchronize_to_moves(self, changed_fields)
if links:
# Use direct SQL to bypass cache and re-fetching issues
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = NULL WHERE id IN %s", [tuple(links.keys())])
self.invalidate_recordset(['expense_sheet_id'])
# Trigger deduction sync for manual edits (write) if not locked
if 'deduction_line_ids' in changed_fields or 'amount_substract' in changed_fields:
if not self.expense_sheet_id:
if 'amount' not in changed_fields:
changed_fields = set(changed_fields) | {'amount'}
try:
# This now correctly calls all modules in the MRO, including vendor_payment_diff_amount
return super()._synchronize_to_moves(changed_fields)
finally:
# Restore the links
for p_id, sheet_id in links.items():
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = %s WHERE id = %s", [sheet_id, p_id])
self.invalidate_recordset(['expense_sheet_id'])
return super()._synchronize_to_moves(changed_fields)
def _synchronize_from_moves(self, changed_fields):
if self._context.get('skip_expense_lock'):
from odoo.addons.account.models.account_payment import AccountPayment as AccountPaymentBase
return AccountPaymentBase._synchronize_from_moves(self, changed_fields)
# Same bypass for sync back
links = {p.id: p.expense_sheet_id.id for p in self if p.expense_sheet_id}
if links:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = NULL WHERE id IN %s", [tuple(links.keys())])
self.invalidate_recordset(['expense_sheet_id'])
try:
return super()._synchronize_from_moves(changed_fields)
finally:
for p_id, sheet_id in links.items():
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = %s WHERE id = %s", [sheet_id, p_id])
self.invalidate_recordset(['expense_sheet_id'])
return super()._synchronize_from_moves(changed_fields)
def write(self, vals):