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() # The module for Odoo's hr_expense override is usually 'odoo.addons.hr_expense' return next((c for c in mro if c.__module__.startswith('odoo.addons.hr_expense')), 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) # 2. Custom Deduction Synchronization # After base sync, we "fix" the lines if deductions are present 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() # Adjust liquidity line to final_payment_amount for line in liquidity_lines: final_amount = payment.final_payment_amount line_vals = { 'amount_currency': -final_amount if payment.payment_type == 'outbound' else final_amount, 'debit': payment.currency_id._convert(final_amount, payment.company_id.currency_id, payment.company_id, payment.date) if payment.payment_type == 'inbound' else 0.0, 'credit': payment.currency_id._convert(final_amount, payment.company_id.currency_id, payment.company_id, payment.date) if payment.payment_type == 'outbound' else 0.0, } line.write(line_vals) # Deduction lines management existing_deduction_accounts = payment.deduction_line_ids.mapped('substract_account_id') other_lines = payment.move_id.line_ids - liquidity_lines - counterpart_lines - writeoff_lines to_delete = other_lines.filtered(lambda l: l.account_id.id in existing_deduction_accounts.ids) if to_delete: to_delete.unlink() # Re-add deduction lines for deduction in payment.deduction_line_ids: deduction_amount = deduction.amount_substract deduction_balance = payment.currency_id._convert( deduction_amount, payment.company_id.currency_id, payment.company_id, payment.date, ) line_vals = { 'name': deduction.name or _('Payment Deduction: %s') % deduction.substract_account_id.name, 'move_id': payment.move_id.id, 'date_maturity': payment.date, 'amount_currency': -deduction_amount if payment.payment_type == 'outbound' else deduction_amount, 'currency_id': payment.currency_id.id, 'debit': deduction_balance if payment.payment_type == 'inbound' else 0.0, 'credit': deduction_balance if payment.payment_type == 'outbound' else 0.0, 'account_id': deduction.substract_account_id.id, } payment.env['account.move.line'].with_context(skip_expense_lock=True, check_move_validity=False).create(line_vals) # Force re-balance check payment.move_id._check_balanced() 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