128 lines
5.5 KiB
Python
128 lines
5.5 KiB
Python
from odoo import _, api, fields, models
|
|
|
|
|
|
class DnLoanLine(models.Model):
|
|
_name = 'dn.loan.line'
|
|
_description = 'Loan Installment'
|
|
_order = 'date, id'
|
|
_check_company_auto = True
|
|
|
|
sequence = fields.Integer(string='#', compute='_compute_sequence')
|
|
loan_id = fields.Many2one(
|
|
'dn.loan', string='Loan', required=True, ondelete='cascade', index=True,
|
|
)
|
|
company_id = fields.Many2one(related='loan_id.company_id', store=True, index=True)
|
|
currency_id = fields.Many2one(related='loan_id.currency_id')
|
|
loan_state = fields.Selection(related='loan_id.state', store=True)
|
|
loan_name = fields.Char(related='loan_id.name', store=True)
|
|
|
|
date = fields.Date(string='Date', required=True)
|
|
principal = fields.Monetary(string='Principal')
|
|
interest = fields.Monetary(string='Interest')
|
|
payment = fields.Monetary(
|
|
string='Payment', compute='_compute_payment', store=True,
|
|
# Stored so the pivot view can sum it.
|
|
)
|
|
outstanding_balance = fields.Monetary(
|
|
string='Outstanding Balance', compute='_compute_outstanding_balance',
|
|
help="Principal still owed once this installment has been paid.",
|
|
)
|
|
long_term_theoretical_balance = fields.Monetary(
|
|
string='Long-Term', compute='_compute_theoretical_balances', store=True,
|
|
help="Principal still owed more than twelve months after this date. "
|
|
"Stored so a pivot can sum it.",
|
|
)
|
|
short_term_theoretical_balance = fields.Monetary(
|
|
string='Short-Term', compute='_compute_theoretical_balances', store=True,
|
|
help="Principal due within twelve months after this date.",
|
|
)
|
|
|
|
move_ids = fields.One2many(
|
|
'account.move', 'dn_loan_line_id', string='Generated Entries',
|
|
readonly=True, copy=False,
|
|
help="Entries generated from this installment.",
|
|
)
|
|
is_payment_move_posted = fields.Boolean(
|
|
compute='_compute_is_payment_move_posted',
|
|
help="Whether the payment entry for this installment is in the ledger.",
|
|
)
|
|
|
|
# -- computes --------------------------------------------------------
|
|
|
|
@api.depends('principal', 'interest')
|
|
def _compute_payment(self):
|
|
for line in self:
|
|
line.payment = line.principal + line.interest
|
|
|
|
@api.depends('loan_id.amount_borrowed', 'loan_id.line_ids.principal',
|
|
'loan_id.line_ids.date', 'date', 'principal')
|
|
def _compute_outstanding_balance(self):
|
|
"""Theoretical balance after paying this installment.
|
|
|
|
This is the schedule's own view of the balance, used in the schedule
|
|
table. The loan's `outstanding_balance` is the accounting one and can
|
|
legitimately differ while entries are still in draft.
|
|
"""
|
|
for line in self:
|
|
if not line.date:
|
|
line.outstanding_balance = line.loan_id.amount_borrowed
|
|
continue
|
|
repaid = sum(
|
|
other.principal for other in line.loan_id.line_ids
|
|
if other.date and other.date <= line.date
|
|
)
|
|
line.outstanding_balance = line.loan_id.amount_borrowed - repaid
|
|
|
|
@api.depends('date', 'loan_id.line_ids.date', 'loan_id.line_ids.principal')
|
|
def _compute_theoretical_balances(self):
|
|
"""Split the remaining principal into the part due within a year.
|
|
|
|
This is what the reclassification entry books, shown next to each row
|
|
so the amounts can be checked before anything is posted.
|
|
"""
|
|
for line in self:
|
|
if not line.date:
|
|
line.long_term_theoretical_balance = 0.0
|
|
line.short_term_theoretical_balance = 0.0
|
|
continue
|
|
later = line.loan_id.line_ids.filtered(
|
|
lambda other: other.date and other.date > line.date
|
|
).sorted(lambda other: (other.date, other.id))
|
|
line.short_term_theoretical_balance = sum(
|
|
later[:12].mapped('principal'))
|
|
line.long_term_theoretical_balance = sum(
|
|
later[12:].mapped('principal'))
|
|
|
|
@api.depends('date', 'loan_id.line_ids.date')
|
|
def _compute_sequence(self):
|
|
for line in self.sorted(lambda l: (l.date or fields.Date.today(), l.id)):
|
|
if not line.date:
|
|
line.sequence = 0
|
|
continue
|
|
line.sequence = len(line.loan_id.line_ids.filtered(
|
|
lambda other: other.date and other.date <= line.date
|
|
))
|
|
|
|
@api.depends('move_ids.state', 'move_ids.is_loan_payment_move',
|
|
'move_ids.reversal_move_ids')
|
|
def _compute_is_payment_move_posted(self):
|
|
for line in self:
|
|
payments = line.move_ids.filtered(lambda m: m.is_loan_payment_move)
|
|
# When the audit trail is enabled a loan can be cancelled and
|
|
# confirmed again, which leaves several payment entries. The one
|
|
# that still stands is the one without a reversal.
|
|
if len(payments) > 1 and any(m.reversal_move_ids for m in payments):
|
|
payments = payments.filtered(lambda m: not m.reversal_move_ids)
|
|
line.is_payment_move_posted = any(m.state == 'posted' for m in payments)
|
|
|
|
# -- constrains ------------------------------------------------------
|
|
|
|
@api.constrains('principal', 'interest')
|
|
def _check_positive_amounts(self):
|
|
for line in self:
|
|
if line.principal < 0 or line.interest < 0:
|
|
from odoo.exceptions import ValidationError
|
|
raise ValidationError(_(
|
|
"An installment cannot have a negative principal or a "
|
|
"negative interest."))
|