diff --git a/models/account_payment.py b/models/account_payment.py index 5455641..ff941f0 100644 --- a/models/account_payment.py +++ b/models/account_payment.py @@ -10,7 +10,30 @@ class AccountPayment(models.Model): """ Confirmation bypass. Calls standard post with skip flag. """ - return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post() + # 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 @@ -18,47 +41,27 @@ class AccountPayment(models.Model): 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']) + # 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 _synchronize_from_moves(self, changed_fields): - if self._context.get('skip_expense_lock'): - # 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): 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) + # 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):