fix: bypass hr_expense lock during payment posting by temporarily clearing expense sheet cache

This commit is contained in:
Suherdy Yacob 2026-04-06 12:04:09 +07:00
parent a91e13991e
commit 70ac4baf8b

View File

@ -10,11 +10,28 @@ class AccountPayment(models.Model):
""" """
Force synchronization before posting to ensure deductions are included, Force synchronization before posting to ensure deductions are included,
even if the payment was previously 'Reset to Draft' without edits. 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.
""" """
for payment in self: # If we have draft payments linked to a sheet, we must bypass the hr_expense lock during posting
if payment.state == 'draft' and payment.expense_sheet_id: draft_payouts = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id)
# Force sync by pretending amount changed to refresh the lines if draft_payouts:
payment._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'}) 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().action_post()
def _synchronize_to_moves(self, changed_fields): def _synchronize_to_moves(self, changed_fields):