refactor: replace context-based bypass with cache manipulation in account payment synchronization to handle hr_expense restrictions

This commit is contained in:
Suherdy Yacob 2026-04-06 12:07:19 +07:00
parent ca4daf1276
commit 32e784d7bb

View File

@ -1,4 +1,4 @@
from odoo import fields, models, _
from odoo import fields, models, api, _
from odoo.exceptions import UserError
class AccountPayment(models.Model):
@ -8,53 +8,65 @@ class AccountPayment(models.Model):
def action_post(self):
"""
Force synchronization before posting to ensure deductions are included.
We use the 'skip_expense_lock' context to bypass hr_expense's strict validation.
Overriding to bypass hr_expense restriction and ensure deductions are synced.
"""
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)
if locked_payouts:
# Store links to restore later
links = {p.id: p.expense_sheet_id.id for p in locked_payouts}
# 1. Hide the links in the cache so hr_expense's _synchronize_to_moves doesn't see them
for p in locked_payouts:
p.env.cache.set(p, p._fields['expense_sheet_id'], False)
try:
# 2. Force a sync with the 'amount' field to ensure deductions are redrawn
for p in locked_payouts:
if p.state == 'draft':
p._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
# 3. Call the standard post logic while the link is hidden
res = super().action_post()
return res
finally:
# 4. Restore the links
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]))
else:
return super().action_post()
def _synchronize_to_moves(self, changed_fields):
# Trigger deduction sync if needed
# If deductions changed, we MUST include 'amount' to trigger Odoo's refresh
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'}
if self._context.get('skip_expense_lock'):
# Safely call super() - the hr_expense override also checks context (via our own override below)
# 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]))
# 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)
# Standard Odoo write: if we are editing deductions, ensure bypass is used if needed
# (Though _synchronize_to_moves override above should handle it)
return super().write(vals)
def write(self, vals):
"""
Override write to manually trigger synchronization when deductions change.
We include 'amount' in the changed_fields to trick Odoo's core sync engine
into refreshing the move lines, which will then include the deductions.
"""
res = super().write(vals)
if 'deduction_line_ids' in vals or 'amount_substract' in vals:
# Force trigger sync by pretending amount changed if deductions changed
self._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
return res
def action_cancel(self):
res = super().action_cancel()
for payment in self:
if payment.expense_sheet_id:
payment.expense_sheet_id.invalidate_recordset(['state'])
# Also check if it's linked via realization
if payment.realization_id and payment.realization_id.expense_sheet_id:
payment.realization_id.expense_sheet_id.invalidate_recordset(['state'])
return res