fix: replace SQL-based expense lock bypass with cache manipulation in payment synchronization logic
This commit is contained in:
parent
14c7489ae0
commit
066709f86b
@ -10,7 +10,30 @@ class AccountPayment(models.Model):
|
|||||||
"""
|
"""
|
||||||
Confirmation bypass. Calls standard post with skip flag.
|
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):
|
def _synchronize_to_moves(self, changed_fields):
|
||||||
# Force the refresh by ensuring 'amount' is in fields if deductions are involved
|
# 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:
|
if 'amount' not in changed_fields:
|
||||||
changed_fields = set(changed_fields) | {'amount'}
|
changed_fields = set(changed_fields) | {'amount'}
|
||||||
|
|
||||||
# BYPASS LOGIC: Temporarily disconnect to skip hr_expense UserError while keeping other logic
|
# Case for manual edits (write) when payment is draft
|
||||||
if self._context.get('skip_expense_lock'):
|
draft_locked = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id)
|
||||||
# Store original values
|
if draft_locked:
|
||||||
links = {p.id: p.expense_sheet_id.id for p in self if p.expense_sheet_id}
|
links = {p.id: p.expense_sheet_id.id for p in draft_locked}
|
||||||
|
for p in draft_locked:
|
||||||
if links:
|
p.env.cache.set(p, p._fields['expense_sheet_id'], False)
|
||||||
# Use direct SQL to bypass cache and re-fetching issues
|
try:
|
||||||
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = NULL WHERE id IN %s", [tuple(links.keys())])
|
return super()._synchronize_to_moves(changed_fields)
|
||||||
self.invalidate_recordset(['expense_sheet_id'])
|
finally:
|
||||||
|
for p in draft_locked:
|
||||||
try:
|
if p.id in links:
|
||||||
# This now correctly calls all modules in the MRO, including vendor_payment_diff_amount
|
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)
|
|
||||||
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'])
|
|
||||||
|
|
||||||
return super()._synchronize_to_moves(changed_fields)
|
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):
|
def write(self, vals):
|
||||||
if 'deduction_line_ids' in vals or 'amount_substract' in vals:
|
if 'deduction_line_ids' in vals or 'amount_substract' in vals:
|
||||||
# Force trigger sync with bypass flag
|
# Force trigger sync
|
||||||
return super(AccountPayment, self.with_context(skip_expense_lock=True)).write(vals)
|
res = super().write(vals)
|
||||||
|
self._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
|
||||||
|
return res
|
||||||
return super().write(vals)
|
return super().write(vals)
|
||||||
|
|
||||||
def action_cancel(self):
|
def action_cancel(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user