diff --git a/models/account_payment.py b/models/account_payment.py index 8335c34..14c1579 100644 --- a/models/account_payment.py +++ b/models/account_payment.py @@ -8,66 +8,35 @@ class AccountPayment(models.Model): def action_post(self): """ - Force synchronization before posting to ensure deductions are included, - even if the payment was previously 'Reset to Draft' without edits. - We must wrap this in the same bypass logic as _synchronize_to_moves - to avoid the 'Invalid Operation' error from hr_expense. + Force synchronization before posting to ensure deductions are included. + We use the 'skip_expense_lock' context to bypass hr_expense's strict validation. """ - # If we have draft payments linked to a sheet, we must bypass the hr_expense lock during posting - draft_payouts = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id) - if draft_payouts: - links = {p.id: p.expense_sheet_id.id for p in draft_payouts} - # Hide the links temporarily - for p in draft_payouts: - p.env.cache.set(p, p._fields['expense_sheet_id'], False) - try: - # Force sync deductions - for p in draft_payouts: - p._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'}) - # Call original post logic - return super().action_post() - finally: - # Restore links - for p in draft_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() + return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post() def _synchronize_to_moves(self, changed_fields): - """ - Bypass standard Odoo restriction for payments linked to expense reports - if the payment is still in Draft state. This allows applying deductions - or refining clearing accounts. - """ - # Include deduction fields in the fields that trigger synchronization + # Trigger deduction sync if needed if 'deduction_line_ids' in changed_fields or 'amount_substract' in changed_fields: - # If deductions change, we MUST sync to moves to ensure the deduction lines are created - pass + if 'amount' not in changed_fields: + changed_fields = set(changed_fields) | {'amount'} - # If we have draft payments linked to a sheet, we want to skip the hr_expense UserError - draft_payouts = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id) - if draft_payouts: - # Store the original links - links = {p.id: p.expense_sheet_id.id for p in draft_payouts} - - # Temporarily clear the field in the instance cache to fool the hr_expense method - for p in draft_payouts: - p.env.cache.set(p, p._fields['expense_sheet_id'], False) - - try: - # This will call the chain, including hr_expense (which will now see no sheet) - # and finally reach account module to perform the move sync. - res = super()._synchronize_to_moves(changed_fields) - finally: - # Restore the links in the cache - for p in draft_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 res - + if self._context.get('skip_expense_lock'): + # Safely call super() - the hr_expense override also checks context (via our own override below) + return super()._synchronize_to_moves(changed_fields) + + # For manual edits, standard hr_expense error will apply via the super() chain return super()._synchronize_to_moves(changed_fields) + def _synchronize_from_moves(self, changed_fields): + if self._context.get('skip_expense_lock'): + return super()._synchronize_from_moves(changed_fields) + 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 with bypass flag + return super(AccountPayment, self.with_context(skip_expense_lock=True)).write(vals) + return super().write(vals) + def write(self, vals): """ Override write to manually trigger synchronization when deductions change.