hr_expense_account_split/models/account_payment.py

186 lines
10 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()
# 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 using SQL De-coupling.
Temporarily hides the linked expense from Odoo to bypass all rigid locking logic.
"""
# Save links
sheet = self.expense_sheet_id
move_lines = self.move_id.line_ids.filtered(lambda l: l.expense_id)
move_lines_map = {line.id: line.expense_id.id for line in move_lines}
# SQL Detach
if sheet:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.invalidate_recordset(['expense_sheet_id'])
if move_lines_map:
self.env.cr.execute("UPDATE account_move_line SET expense_id = NULL WHERE id IN %s", (tuple(move_lines_map.keys()),))
self.env['account.move.line'].browse(move_lines_map.keys()).invalidate_recordset(['expense_id'])
try:
# Force apply deductions with custom labels and amounts
self.with_context(skip_account_move_synchronization=True)._synchronize_to_moves({'amount', 'deduction_line_ids', 'amount_substract'})
# Standard posting
res = super(AccountPayment, self).action_post()
finally:
# SQL Restore
if sheet:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.invalidate_recordset(['expense_sheet_id'])
if move_lines_map:
for line_id, exp_id in move_lines_map.items():
self.env.cr.execute("UPDATE account_move_line SET expense_id = %s WHERE id = %s", (exp_id, line_id))
self.env['account.move.line'].browse(move_lines_map.keys()).invalidate_recordset(['expense_id'])
return res
def _synchronize_to_moves(self, changed_fields):
# 1. Base Synchronization (forced via SQL bypass if already called from action_post)
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'}
super()._synchronize_to_moves(changed_fields)
# 2. Apply complex deductions
bypass_ctx = {'skip_account_move_synchronization': True}
for payment in self.with_context(**bypass_ctx):
if payment.amount_substract and payment.amount_substract > 0 and payment.move_id:
liquidity_lines, counterpart_lines, writeoff_lines = payment._seek_for_lines()
# Update Bank Line to Net
for line in liquidity_lines:
final_amount = payment.final_payment_amount
line.write({
'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,
})
# Refresh Deduction Lines
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()
for deduction in payment.deduction_line_ids:
ded_amt = deduction.amount_substract
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': -ded_amt if payment.payment_type == 'outbound' else ded_amt,
'currency_id': payment.currency_id.id,
'debit': payment.currency_id._convert(ded_amt, payment.company_id.currency_id, payment.company_id, payment.date) if payment.payment_type == 'inbound' else 0.0,
'credit': payment.currency_id._convert(ded_amt, payment.company_id.currency_id, payment.company_id, payment.date) if payment.payment_type == 'outbound' else 0.0,
'account_id': deduction.substract_account_id.id,
}
payment.env['account.move.line'].with_context(check_move_validity=False).create(line_vals)
payment.move_id._check_balanced()
def _synchronize_from_moves(self, changed_fields):
# Base restoration logic
super()._synchronize_from_moves(changed_fields)
# Restore Gross from lines if Odoo accidentally set it to Net
for payment in self.with_context(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_lines = non_liquidity_lines.filtered(lambda l: l.debit > 0 if payment.payment_type == 'outbound' else l.credit > 0)
if gross_lines:
correct_gross = sum(abs(l.amount_currency) for l in gross_lines)
if abs(payment.amount - correct_gross) > 0.001:
payment.env.cr.execute("UPDATE account_payment SET amount = %s WHERE id = %s", (correct_gross, payment.id))
payment.invalidate_recordset(['amount'])
payment._compute_final_payment_amount()
def write(self, vals):
# SQL Bypass for writes to avoid locked checks
sheet = self.expense_sheet_id
if sheet and self._context.get('skip_expense_lock'):
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.invalidate_recordset(['expense_sheet_id'])
try:
return super(AccountPayment, self).write(vals)
finally:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.invalidate_recordset(['expense_sheet_id'])
# Standard auto-bypass for internal draft writes
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):
# SQL Bypass for cancel
sheet = self.expense_sheet_id
if sheet:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.invalidate_recordset(['expense_sheet_id'])
try:
res = super(AccountPayment, self).action_cancel()
finally:
if sheet:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.invalidate_recordset(['expense_sheet_id'])
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):
# SQL Bypass for reset to draft
sheet = self.expense_sheet_id
if sheet:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = NULL WHERE id = %s", (self.id,))
self.invalidate_recordset(['expense_sheet_id'])
try:
res = super(AccountPayment, self).action_draft()
finally:
if sheet:
self.env.cr.execute("UPDATE account_payment SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.env.cr.execute("UPDATE account_move SET expense_sheet_id = %s WHERE id = %s", (sheet.id, self.id))
self.invalidate_recordset(['expense_sheet_id'])
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