hr_expense_account_split/models/account_payment.py

84 lines
3.9 KiB
Python

from odoo import fields, models, _
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):
"""
Force synchronization before posting to ensure deductions are included,
even if the payment was previously 'Reset to Draft' without edits.
"""
for payment in self:
if payment.state == 'draft' and payment.expense_sheet_id:
# Force sync by pretending amount changed to refresh the lines
payment._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
return super().action_post()
def _synchronize_to_moves(self, changed_fields):
"""
Bypass standard Odoo restriction for payments linked to expense reports
if the payment is still in Draft state. This allows applying deductions
or refining clearing accounts.
"""
# Include deduction fields in the fields that trigger synchronization
if 'deduction_line_ids' in changed_fields or 'amount_substract' in changed_fields:
# If deductions change, we MUST sync to moves to ensure the deduction lines are created
pass
# If we have draft payments linked to a sheet, we want to skip the hr_expense UserError
draft_payouts = self.filtered(lambda p: p.state == 'draft' and p.expense_sheet_id)
if draft_payouts:
# Store the original links
links = {p.id: p.expense_sheet_id.id for p in draft_payouts}
# Temporarily clear the field in the instance cache to fool the hr_expense method
for p in draft_payouts:
p.env.cache.set(p, p._fields['expense_sheet_id'], False)
try:
# This will call the chain, including hr_expense (which will now see no sheet)
# and finally reach account module to perform the move sync.
res = super()._synchronize_to_moves(changed_fields)
finally:
# Restore the links in the cache
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 res
return super()._synchronize_to_moves(changed_fields)
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
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