refactor: replace cache manipulation with database unlinking to bypass hr_expense restrictions during payment posting and synchronization

This commit is contained in:
Suherdy Yacob 2026-04-06 12:11:23 +07:00
parent 32e784d7bb
commit 481b03e73f

View File

@ -9,59 +9,45 @@ class AccountPayment(models.Model):
def action_post(self): def action_post(self):
""" """
Overriding to bypass hr_expense restriction and ensure deductions are synced. Overriding to bypass hr_expense restriction and ensure deductions are synced.
We temporarily clear the link in the DB to avoid the 'Invalid Operation' error.
""" """
# Identify payments that would normally be locked by hr_expense for payment in self:
locked_payouts = self.filtered(lambda p: p.expense_sheet_id) if payment.expense_sheet_id and payment.state == 'draft':
if locked_payouts: sheet_id = payment.expense_sheet_id.id
# Store links to restore later # 1. Clear the link in DB and Cache (Skip sync to avoid recursion)
links = {p.id: p.expense_sheet_id.id for p in locked_payouts} payment.with_context(skip_account_move_synchronization=True).write({'expense_sheet_id': False})
# 1. Hide the links in the cache so hr_expense's _synchronize_to_moves doesn't see them try:
for p in locked_payouts: # 2. Force Sync Deductions while 'unlinked'
p.env.cache.set(p, p._fields['expense_sheet_id'], False) payment._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
try: # 3. Call standard post
# 2. Force a sync with the 'amount' field to ensure deductions are redrawn res = super(AccountPayment, payment).action_post()
for p in locked_payouts: finally:
if p.state == 'draft': # 4. Restore the link
p._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'}) payment.with_context(skip_account_move_synchronization=True).write({'expense_sheet_id': sheet_id})
else:
# 3. Call the standard post logic while the link is hidden super(AccountPayment, payment).action_post()
res = super().action_post() return True
return res
finally:
# 4. Restore the links
for p in locked_payouts:
if p.id in links:
p.env.cache.set(p, p._fields['expense_sheet_id'], self.env['hr.expense.sheet'].browse(links[p.id]))
else:
return super().action_post()
def _synchronize_to_moves(self, changed_fields): def _synchronize_to_moves(self, changed_fields):
# If deductions changed, we MUST include 'amount' to trigger Odoo's refresh # Trigger deduction sync if needed
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:
changed_fields = set(changed_fields) | {'amount'} if 'amount' not in changed_fields:
changed_fields = set(changed_fields) | {'amount'}
# Case for manual edits (write) when payment is draft # Bypass for manual edits
draft_locked = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id) for payment in self:
if draft_locked: if payment.expense_sheet_id and payment.state == 'draft':
links = {p.id: p.expense_sheet_id.id for p in draft_locked} sheet_id = payment.expense_sheet_id.id
for p in draft_locked: payment.with_context(skip_account_move_synchronization=True).write({'expense_sheet_id': False})
p.env.cache.set(p, p._fields['expense_sheet_id'], False) try:
try: return super(AccountPayment, payment)._synchronize_to_moves(changed_fields)
return super()._synchronize_to_moves(changed_fields) finally:
finally: payment.with_context(skip_account_move_synchronization=True).write({'expense_sheet_id': sheet_id})
for p in draft_locked:
if p.id in links:
p.env.cache.set(p, p._fields['expense_sheet_id'], self.env['hr.expense.sheet'].browse(links[p.id]))
return super()._synchronize_to_moves(changed_fields) return super()._synchronize_to_moves(changed_fields)
def write(self, vals):
# Standard Odoo write: if we are editing deductions, ensure bypass is used if needed
# (Though _synchronize_to_moves override above should handle it)
return super().write(vals)
def action_cancel(self): def action_cancel(self):
res = super().action_cancel() res = super().action_cancel()
for payment in self: for payment in self: