refactor: simplify surgical jumper logic in account_payment using super() and update write bypass condition

This commit is contained in:
Suherdy Yacob 2026-04-06 13:12:12 +07:00
parent 8586daa2f0
commit 98e06420bc

View File

@ -13,7 +13,7 @@ class AccountPayment(models.Model):
""" """
Confirmation bypass. Calls standard post with skip flag. Confirmation bypass. Calls standard post with skip flag.
""" """
# We use a context flag that our surgical jumper will check # Propagate the flag to all internal calls
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):
@ -22,34 +22,29 @@ 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'}
# SURGICAL JUMPER: If we have the bypass flag, we skip the hr_expense method specifically. # SURGICAL JUMPER: If we have the bypass flag, we jump over the hr_expense method.
if self._context.get('skip_expense_lock'): if self._context.get('skip_expense_lock'):
# Find the hr_expense class in the MRO
mro = type(self).mro() mro = type(self).mro()
try: # Find the class that belongs to the hr_expense module
# Find the index of the middle-man we want to skip hr_expense_class = next((c for c in mro if c.__module__.startswith('odoo.addons.hr_expense')), None)
hr_index = next(i for i, c in enumerate(mro) if c.__module__.startswith('odoo.addons.hr_expense')) if hr_expense_class:
# Jump to the next class AFTER hr_expense # Standard Python jumping syntax: calls the method of the parent of hr_expense_class
return mro[hr_index + 1]._synchronize_to_moves(self, changed_fields) return super(hr_expense_class, self)._synchronize_to_moves(changed_fields)
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): def _synchronize_from_moves(self, changed_fields):
if self._context.get('skip_expense_lock'): if self._context.get('skip_expense_lock'):
mro = type(self).mro() mro = type(self).mro()
try: hr_expense_class = next((c for c in mro if c.__module__.startswith('odoo.addons.hr_expense')), None)
hr_index = next(i for i, c in enumerate(mro) if c.__module__.startswith('odoo.addons.hr_expense')) if hr_expense_class:
return mro[hr_index + 1]._synchronize_from_moves(self, changed_fields) return super(hr_expense_class, self)._synchronize_from_moves(changed_fields)
except (StopIteration, IndexError):
pass
return super()._synchronize_from_moves(changed_fields) 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 or 'amount' in vals) and self.expense_sheet_id: # Propagate bypass flag during writes to avoid checks on draft payments
# For draft payments linked to expenses, use the jumper to allow edits if self.expense_sheet_id and self.state == 'draft':
return super(AccountPayment, self.with_context(skip_expense_lock=True)).write(vals) return super(AccountPayment, self.with_context(skip_expense_lock=True)).write(vals)
return super().write(vals) return super().write(vals)