refactor: replace cache-based locking bypass with a surgical MRO jumper to skip hr_expense methods

This commit is contained in:
Suherdy Yacob 2026-04-06 12:31:38 +07:00
parent 066709f86b
commit 8586daa2f0

View File

@ -1,5 +1,8 @@
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'
@ -10,30 +13,8 @@ class AccountPayment(models.Model):
""" """
Confirmation bypass. Calls standard post with skip flag. Confirmation bypass. Calls standard post with skip flag.
""" """
# Identify payments that would normally be locked by hr_expense # We use a context flag that our surgical jumper will check
locked_payouts = self.filtered(lambda p: p.expense_sheet_id and p.state == 'draft') return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post()
if locked_payouts:
# Store link values to restore
links = {p.id: p.expense_sheet_id.id for p in locked_payouts}
# Hide the links in the cache temporarily to avoid 'hr_expense' validation error
for p in locked_payouts:
p.env.cache.set(p, p._fields['expense_sheet_id'], False)
try:
# Force synchronization before posting while links are hidden
for p in locked_payouts:
p._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
# Standard confirmation logic
return super().action_post()
finally:
# Restore the links in the cache
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]))
return super().action_post()
def _synchronize_to_moves(self, changed_fields): def _synchronize_to_moves(self, changed_fields):
# Force the refresh by ensuring 'amount' is in fields if deductions are involved # Force the refresh by ensuring 'amount' is in fields if deductions are involved
@ -41,27 +22,35 @@ class AccountPayment(models.Model):
if 'amount' not in changed_fields: if 'amount' not in changed_fields:
changed_fields = set(changed_fields) | {'amount'} changed_fields = set(changed_fields) | {'amount'}
# Case for manual edits (write) when payment is draft # SURGICAL JUMPER: If we have the bypass flag, we skip the hr_expense method specifically.
draft_locked = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id) if self._context.get('skip_expense_lock'):
if draft_locked: # Find the hr_expense class in the MRO
links = {p.id: p.expense_sheet_id.id for p in draft_locked} mro = type(self).mro()
for p in draft_locked:
p.env.cache.set(p, p._fields['expense_sheet_id'], False)
try: try:
return super()._synchronize_to_moves(changed_fields) # Find the index of the middle-man we want to skip
finally: hr_index = next(i for i, c in enumerate(mro) if c.__module__.startswith('odoo.addons.hr_expense'))
for p in draft_locked: # Jump to the next class AFTER hr_expense
if p.id in links: return mro[hr_index + 1]._synchronize_to_moves(self, changed_fields)
p.env.cache.set(p, p._fields['expense_sheet_id'], self.env['hr.expense.sheet'].browse(links[p.id])) except (StopIteration, IndexError):
pass
return super()._synchronize_to_moves(changed_fields) return super()._synchronize_to_moves(changed_fields)
def _synchronize_from_moves(self, changed_fields):
if self._context.get('skip_expense_lock'):
mro = type(self).mro()
try:
hr_index = next(i for i, c in enumerate(mro) if c.__module__.startswith('odoo.addons.hr_expense'))
return mro[hr_index + 1]._synchronize_from_moves(self, changed_fields)
except (StopIteration, IndexError):
pass
return super()._synchronize_from_moves(changed_fields)
def write(self, vals): def write(self, vals):
if 'deduction_line_ids' in vals or 'amount_substract' in vals: if ('deduction_line_ids' in vals or 'amount_substract' in vals or 'amount' in vals) and self.expense_sheet_id:
# Force trigger sync # For draft payments linked to expenses, use the jumper to allow edits
res = super().write(vals) return super(AccountPayment, self.with_context(skip_expense_lock=True)).write(vals)
self._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
return res
return super().write(vals) return super().write(vals)
def action_cancel(self): def action_cancel(self):