payment_residual_display/models/__init__.py
2025-11-19 15:57:33 +07:00

62 lines
2.3 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
)
# One2many to show journal items with residual amounts
payment_move_line_ids = fields.One2many(
'account.move.line',
'payment_id',
string='Payment Journal Items',
domain=lambda self: [
('account_id.reconcile', '=', True),
('account_id.account_type', 'in', ['asset_receivable', 'liability_payable'])
],
readonly=True
)
@api.depends('payment_move_line_ids.amount_residual',
'payment_move_line_ids.account_id')
def _compute_payment_residual(self):
"""Compute the residual amount from payment journal items"""
for payment in self:
if payment.state in ['draft', 'cancel'] or not payment.payment_move_line_ids:
payment.payment_residual = 0.0
else:
# Get all reconcilable journal items for this payment
reconcilable_lines = payment.payment_move_line_ids.filtered(
lambda l: l.account_id.reconcile and
l.account_id.account_type in ['asset_receivable', 'liability_payable']
)
# Sum up the residual amounts
total_residual = sum(reconcilable_lines.mapped('amount_residual'))
# For display purposes, show absolute value with proper sign
payment.payment_residual = total_residual
def action_view_journal_items(self):
"""Action to view the payment's journal items"""
self.ensure_one()
return {
'name': _('Payment Journal Items'),
'view_mode': 'tree,form',
'res_model': 'account.move.line',
'domain': [('payment_id', '=', self.id)],
'context': {
'default_payment_id': self.id,
'search_default_payment_id': self.id,
},
'type': 'ir.actions.act_window',
}
from . import account_payment