refactor: replace direct base class invocation with SQL-based bypass for expense sheet synchronization in account payment
This commit is contained in:
parent
4261b6c53c
commit
14c7489ae0
@ -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 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'}
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
# Trigger deduction sync for manual edits (write) if not locked
|
|
||||||
if 'deduction_line_ids' in changed_fields or 'amount_substract' in changed_fields:
|
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:
|
||||||
if 'amount' not in changed_fields:
|
changed_fields = set(changed_fields) | {'amount'}
|
||||||
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'):
|
||||||
|
# Store original values
|
||||||
|
links = {p.id: p.expense_sheet_id.id for p in self if p.expense_sheet_id}
|
||||||
|
|
||||||
|
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'])
|
||||||
|
|
||||||
|
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)
|
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):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user