From 8586daa2f0265825b11f310e27092a01a6719799 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Mon, 6 Apr 2026 12:31:38 +0700 Subject: [PATCH] refactor: replace cache-based locking bypass with a surgical MRO jumper to skip hr_expense methods --- models/account_payment.py | 69 ++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/models/account_payment.py b/models/account_payment.py index ff941f0..420fa9a 100644 --- a/models/account_payment.py +++ b/models/account_payment.py @@ -1,5 +1,8 @@ from odoo import fields, models, api, _ from odoo.exceptions import UserError +import logging + +_logger = logging.getLogger(__name__) class AccountPayment(models.Model): _inherit = 'account.payment' @@ -10,30 +13,8 @@ class AccountPayment(models.Model): """ Confirmation bypass. Calls standard post with skip flag. """ - # Identify payments that would normally be locked by hr_expense - locked_payouts = self.filtered(lambda p: p.expense_sheet_id and p.state == 'draft') - 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() + # We use a context flag that our surgical jumper will check + return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post() def _synchronize_to_moves(self, changed_fields): # 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: changed_fields = set(changed_fields) | {'amount'} - # Case for manual edits (write) when payment is draft - draft_locked = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id) - if draft_locked: - links = {p.id: p.expense_sheet_id.id for p in draft_locked} - for p in draft_locked: - p.env.cache.set(p, p._fields['expense_sheet_id'], False) + # SURGICAL JUMPER: If we have the bypass flag, we skip the hr_expense method specifically. + if self._context.get('skip_expense_lock'): + # Find the hr_expense class in the MRO + mro = type(self).mro() try: - return super()._synchronize_to_moves(changed_fields) - finally: - 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])) + # Find the index of the middle-man we want to skip + hr_index = next(i for i, c in enumerate(mro) if c.__module__.startswith('odoo.addons.hr_expense')) + # Jump to the next class AFTER hr_expense + return mro[hr_index + 1]._synchronize_to_moves(self, changed_fields) + except (StopIteration, IndexError): + pass 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): - if 'deduction_line_ids' in vals or 'amount_substract' in vals: - # Force trigger sync - res = super().write(vals) - self._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'}) - return res + if ('deduction_line_ids' in vals or 'amount_substract' in vals or 'amount' in vals) and self.expense_sheet_id: + # For draft payments linked to expenses, use the jumper to allow edits + return super(AccountPayment, self.with_context(skip_expense_lock=True)).write(vals) return super().write(vals) def action_cancel(self):