59 lines
2.3 KiB
Python
59 lines
2.3 KiB
Python
from odoo import models, fields
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
class LoanRepaymentPreviewWizard(models.TransientModel):
|
|
_name = 'loan.repayment.preview.wizard'
|
|
_description = 'Loan Repayment Preview Wizard'
|
|
|
|
loan_request_id = fields.Many2one('loan.request', string='Yêu cầu vay', required=True)
|
|
preview_line_ids = fields.One2many('loan.repayment.preview.line', 'wizard_id', string='Chi tiết lịch trả', readonly=True)
|
|
|
|
def action_generate_preview_schedule(self):
|
|
self.ensure_one()
|
|
request = self.loan_request_id
|
|
self.preview_line_ids.unlink() # Clear old preview lines
|
|
|
|
amount = request.amount
|
|
term = request.term
|
|
rate = request.interest_rate
|
|
repayment_type = request.repayment_type
|
|
start_date = request.request_datetime.date() or fields.Date.today()
|
|
|
|
for i in range(term):
|
|
due_date = start_date + relativedelta(months=i + 1)
|
|
|
|
if repayment_type == 'fixed_principal':
|
|
principal = amount / term
|
|
interest = (amount - principal * i) * rate / 100 / 12
|
|
total = principal + interest
|
|
|
|
elif repayment_type == 'annuity':
|
|
monthly_rate = rate / 100 / 12
|
|
if monthly_rate == 0:
|
|
annuity = amount / term
|
|
else:
|
|
annuity = (amount * monthly_rate) / (1 - (1 + monthly_rate) ** -term)
|
|
interest = (amount - i * (amount / term)) * monthly_rate
|
|
principal = annuity - interest
|
|
total = annuity
|
|
|
|
elif repayment_type == 'interest_only':
|
|
principal = amount if i == term - 1 else 0
|
|
interest = amount * rate / 100 / 12
|
|
total = principal + interest
|
|
|
|
elif repayment_type == 'lump_sum':
|
|
principal = amount if i == term - 1 else 0
|
|
interest = amount * rate / 100 / 12
|
|
total = principal + interest
|
|
|
|
self.env['loan.repayment.preview.line'].create({
|
|
'wizard_id': self.id,
|
|
'installment': i + 1,
|
|
'due_date': due_date,
|
|
'principal': round(principal, 2),
|
|
'interest': round(interest, 2),
|
|
'total': round(total, 2),
|
|
})
|
|
|