from odoo import fields, models, api, _ from odoo.exceptions import UserError class AccountPayment(models.Model): _inherit = 'account.payment' realization_id = fields.Many2one('hr.expense.realization', string='Originating Realization', readonly=True) def action_post(self): """ 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() def _synchronize_to_moves(self, changed_fields): # 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 '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) 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])) return super()._synchronize_to_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 return super().write(vals) def action_cancel(self): res = super().action_cancel() for payment in self: if payment.expense_sheet_id: payment.expense_sheet_id.invalidate_recordset(['state']) if payment.realization_id and payment.realization_id.expense_sheet_id: payment.realization_id.expense_sheet_id.invalidate_recordset(['state']) return res def action_draft(self): res = super().action_draft() for payment in self: if payment.expense_sheet_id: payment.expense_sheet_id.invalidate_recordset(['state']) if payment.realization_id and payment.realization_id.expense_sheet_id: payment.realization_id.expense_sheet_id.invalidate_recordset(['state']) return res