diff --git a/models/account_payment.py b/models/account_payment.py index 385f9d7..5455641 100644 --- a/models/account_payment.py +++ b/models/account_payment.py @@ -1,8 +1,5 @@ from odoo import fields, models, api, _ from odoo.exceptions import UserError -import logging - -_logger = logging.getLogger(__name__) class AccountPayment(models.Model): _inherit = 'account.payment' @@ -16,31 +13,46 @@ class AccountPayment(models.Model): return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post() def _synchronize_to_moves(self, changed_fields): - # Always allow sync if bypass flag is set - 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 + # Force the refresh by ensuring 'amount' is in fields if deductions are involved 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: - changed_fields = set(changed_fields) | {'amount'} + if 'amount' not in changed_fields: + 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) def _synchronize_from_moves(self, changed_fields): if self._context.get('skip_expense_lock'): - from odoo.addons.account.models.account_payment import AccountPayment as AccountPaymentBase - return AccountPaymentBase._synchronize_from_moves(self, changed_fields) + # Same bypass for sync back + 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) def write(self, vals):