payment_residual_display/models/account_payment.py

71 lines
2.8 KiB
Python

from odoo import api, fields, models, _
class AccountPayment(models.Model):
_inherit = 'account.payment'
# Computed residual amount field
payment_residual = fields.Monetary(
string='Payment Residual',
compute='_compute_payment_residual',
currency_field='currency_id',
help="Residual amount of this payment (amount not yet reconciled)",
readonly=True
)
payment_residual_currency = fields.Monetary(
string='Payment Residual Currency',
compute='_compute_payment_residual',
currency_field='currency_id',
help="Residual amount in payment currency",
readonly=True
)
@api.depends('move_id.line_ids.amount_residual',
'move_id.line_ids.amount_residual_currency',
'move_id.line_ids.reconciled',
'is_matched')
def _compute_payment_residual(self):
"""Compute the residual amount from payment journal items
Shows 0 residual when payment is matched with bank statement.
Otherwise shows the residual from all reconcilable lines except liquidity accounts.
"""
for pay in self:
# If payment is matched with bank statement, no residual to show
if pay.is_matched:
pay.payment_residual = 0.0
pay.payment_residual_currency = 0.0
continue
if not pay.move_id:
pay.payment_residual = 0.0
pay.payment_residual_currency = 0.0
continue
# Get all reconcilable lines except liquidity accounts
# This includes receivable, payable, and other reconcilable accounts
# but excludes bank/cash accounts that get matched with statements
reconcilable_lines = pay.move_id.line_ids.filtered(
lambda line: line.account_id.reconcile and
line.account_id.account_type not in ('asset_cash', 'liability_credit_card')
)
# Calculate residual based on currency
residual = 0.0
residual_currency = 0.0
for line in reconcilable_lines:
# Always add to company currency residual
residual += line.amount_residual
# For foreign currency, use amount_residual_currency
if line.currency_id and line.currency_id != pay.company_id.currency_id:
residual_currency += line.amount_residual_currency
else:
residual_currency += line.amount_residual
# Store absolute values
pay.payment_residual = abs(residual)
pay.payment_residual_currency = abs(residual_currency)