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