hr_expense_account_split/models/account_payment.py

81 lines
3.9 KiB
Python

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.
"""
return super(AccountPayment, self.with_context(skip_expense_lock=True)).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'}
# 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'])
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)
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