162 lines
8.6 KiB
Python
162 lines
8.6 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 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 'amount' and 'deduction_line_ids' to trigger vendor_payment_diff_amount.
|
|
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
|
|
return super(AccountPayment, self.with_context(skip_expense_lock=True)).action_post()
|
|
|
|
def _synchronize_to_moves(self, changed_fields):
|
|
# 1. Standard synchronization first
|
|
# We ensure 'amount' is in changed_fields if deductions are involved to trigger base sync
|
|
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'}
|
|
|
|
# SURGICAL JUMPER: If we have the bypass flag, we jump over the hr_expense method.
|
|
if self._context.get('skip_expense_lock'):
|
|
mro = type(self).mro()
|
|
hr_expense_class = next((c for c in mro if c.__module__.startswith('odoo.addons.hr_expense')), None)
|
|
if hr_expense_class:
|
|
super(hr_expense_class, self)._synchronize_to_moves(changed_fields)
|
|
else:
|
|
super()._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_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
|
|
# Remove existing deduction lines that are no longer present or need refresh
|
|
existing_deduction_accounts = payment.deduction_line_ids.mapped('substract_account_id')
|
|
|
|
# We identify deduction lines as those not being liquidity or counterpart
|
|
# and having an account from the deduction list
|
|
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,
|
|
)
|
|
|
|
# For outbound (Send Money): Deductions are CREDITS
|
|
# For inbound (Receive Money): Deductions are DEBITS (rare but possible)
|
|
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(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
|
|
if self._context.get('skip_expense_lock'):
|
|
mro = type(self).mro()
|
|
hr_expense_class = next((c for c in mro if c.__module__.startswith('odoo.addons.hr_expense')), None)
|
|
if hr_expense_class:
|
|
super(hr_expense_class, self)._synchronize_from_moves(changed_fields)
|
|
else:
|
|
super()._synchronize_from_moves(changed_fields)
|
|
else:
|
|
super()._synchronize_from_moves(changed_fields)
|
|
|
|
# 2. Custom Deduction Restoration
|
|
# If we have deductions, we must restore the gross amount from the move counterpart lines,
|
|
# because the bank line (which base Odoo syncs from) only contains final_payment_amount.
|
|
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 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'])
|
|
# Ensure computed fields are re-evaluated
|
|
payment._compute_final_payment_amount()
|
|
|
|
|
|
def write(self, vals):
|
|
# Propagate bypass flag during writes to avoid locked checks
|
|
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):
|
|
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
|