hr_expense_account_split/models/account_payment.py

128 lines
6.2 KiB
Python

from odoo import fields, models, api, _
from odoo.exceptions import UserError
import logging
_logger = logging.getLogger(__name__)
class AccountPayment(models.Model):
_inherit = 'account.payment'
realization_id = fields.Many2one('hr.expense.realization', string='Originating Realization', readonly=True)
def _get_hr_expense_base_class(self):
""" Returns the hr_expense class in the MRO to jump over it. """
mro = type(self).mro()
return next((c for c in mro if c.__module__ == 'odoo.addons.hr_expense.models.account_payment'), None)
def action_post(self):
"""
Confirmation bypass. Calls standard post with skip flag and FORCES a sync
to ensure deductions are applied to the Journal Entry.
"""
# 1. Force a synchronization of the moves right before posting.
# We pass 'skip_expense_lock' to all layers (Move, Line) via context.
self_bypass = self.with_context(skip_expense_lock=True)
for payment in self_bypass:
if payment.state == 'draft':
payment._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
# 2. Standard confirmation logic with bypass flag for internal writes
hr_expense_class = self._get_hr_expense_base_class()
if hr_expense_class:
return super(hr_expense_class, self.with_context(skip_expense_lock=True)).action_post()
return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post()
def _synchronize_to_moves(self, changed_fields):
# 1. Standard synchronization first
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'}
# Triple Jumper: If we have the bypass flag, we jump over the hr_expense method.
# This works in tandem with our Model-level jumpers in account_move and account_move_line.
hr_expense_class = self._get_hr_expense_base_class()
if self._context.get('skip_expense_lock') and hr_expense_class:
super(hr_expense_class, self)._synchronize_to_moves(changed_fields)
else:
super()._synchronize_to_moves(changed_fields)
# The base _synchronize_to_moves naturally calls _prepare_move_line_default_vals.
# vendor_payment_diff_amount ALREADY overrides _prepare_move_line_default_vals to handle Deductions.
# So we don't need to manually recreate deduction lines here—just jumping the lock above is enough!
# Odoo 17 handles balance checks automatically; no need to call _check_balanced() manually
def _synchronize_from_moves(self, changed_fields):
# 1. Standard sync with jumper support
hr_expense_class = self._get_hr_expense_base_class()
if self._context.get('skip_expense_lock') and hr_expense_class:
super(hr_expense_class, self)._synchronize_from_moves(changed_fields)
else:
super()._synchronize_from_moves(changed_fields)
# 2. Custom Deduction Restoration
for payment in self.with_context(skip_expense_lock=True, skip_account_move_synchronization=True):
if payment.amount_substract and payment.amount_substract > 0 and payment.move_id:
liquidity_lines, counterpart_lines, writeoff_lines = payment._seek_for_lines()
non_liquidity_lines = counterpart_lines + writeoff_lines
# Gross amount is sum of counterparts that balance the liquidity
if payment.payment_type == 'outbound':
gross_lines = non_liquidity_lines.filtered(lambda l: l.debit > 0)
else:
gross_lines = non_liquidity_lines.filtered(lambda l: l.credit > 0)
if gross_lines:
correct_gross_amount = sum(abs(l.amount_currency) for l in gross_lines)
if abs(payment.amount - correct_gross_amount) > 0.001:
# Use SQL to avoid triggering more syncs, then invalidate cache
payment.env.cr.execute(
"UPDATE account_payment SET amount = %s WHERE id = %s",
(correct_gross_amount, payment.id)
)
payment.invalidate_recordset(['amount'])
payment._compute_final_payment_amount()
def write(self, vals):
# Propagate bypass flag during writes to avoid locked checks
hr_expense_class = self._get_hr_expense_base_class()
if hr_expense_class and self._context.get('skip_expense_lock'):
return super(hr_expense_class, self).write(vals)
if self.expense_sheet_id and self.state == 'draft':
return super(AccountPayment, self.with_context(skip_expense_lock=True)).write(vals)
return super().write(vals)
def action_cancel(self):
# Propagate bypass flag during cancel
hr_expense_class = self._get_hr_expense_base_class()
if hr_expense_class:
res = super(hr_expense_class, self.with_context(skip_expense_lock=True)).action_cancel()
else:
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):
# Propagate bypass flag during reset to draft
hr_expense_class = self._get_hr_expense_base_class()
if hr_expense_class:
res = super(hr_expense_class, self.with_context(skip_expense_lock=True)).action_draft()
else:
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