169 lines
8.3 KiB
Python
169 lines
8.3 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_ids 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_ids:
|
|
payment.expense_ids.invalidate_recordset(['state'])
|
|
if payment.realization_id and payment.realization_id.expense_id:
|
|
payment.realization_id.expense_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_ids:
|
|
payment.expense_ids.invalidate_recordset(['state'])
|
|
if payment.realization_id and payment.realization_id.expense_id:
|
|
payment.realization_id.expense_id.invalidate_recordset(['state'])
|
|
return res
|
|
|
|
def _seek_for_lines(self):
|
|
"""
|
|
Override _seek_for_lines to explicitly force the destination_account_id
|
|
to be recognized as the counterpart_line.
|
|
|
|
Native Odoo sometimes misclassifies the advance/expense account (e.g. 118101)
|
|
as a write-off line if it's not strictly a receivable/payable account type.
|
|
Simultaneously, if a tax deduction account (e.g. 217103 PPh 23) is a payable account type,
|
|
Odoo erroneously classifies the deduction as the counterpart.
|
|
This causes duplicate lines when regenerating the journal entries.
|
|
"""
|
|
self.ensure_one()
|
|
|
|
# Capture the original output
|
|
liquidity_lines, counterpart_lines, writeoff_lines = super()._seek_for_lines()
|
|
|
|
# If there's only one line in write-off, Odoo native might have forcefully moved it
|
|
# to counterpart. But we need to make sure the correct counterpart is identified based
|
|
# on the configured destination account.
|
|
|
|
if self.destination_account_id:
|
|
# We want to re-evaluate counterpart vs writeoff based purely on destination_account_id
|
|
|
|
# Reset counterpart and writeoff
|
|
all_non_liquidity = counterpart_lines + writeoff_lines
|
|
new_counterpart = self.env['account.move.line']
|
|
new_writeoff = self.env['account.move.line']
|
|
|
|
for line in all_non_liquidity:
|
|
# The primary destination account is the counterpart
|
|
if line.account_id == self.destination_account_id:
|
|
new_counterpart += line
|
|
else:
|
|
new_writeoff += line
|
|
|
|
# In edge cases where Odoo couldn't find ANY counterpart, let's preserve Odoo's fallback
|
|
# if our logic resulted in empty counterpart but native didn't.
|
|
# However, typically the destination_account_id *must* be the counterpart.
|
|
if new_counterpart:
|
|
return liquidity_lines, new_counterpart, new_writeoff
|
|
|
|
return liquidity_lines, counterpart_lines, writeoff_lines
|