Browse Source

[IMP] move code to loan line to allows computation on individual loan

[IMP] making the function looping on the recordset
[IMP] add compute button on the issue line
pull/134/head
houssine 4 years ago
parent
commit
2bf4233e61
  1. 98
      easy_my_coop_loan/models/loan.py
  2. 102
      easy_my_coop_loan/models/loan_issue_line.py
  3. 3
      easy_my_coop_loan/views/loan_line_view.xml

98
easy_my_coop_loan/models/loan.py

@ -4,10 +4,6 @@
import logging import logging
from dateutil.relativedelta import relativedelta
from datetime import date
import calendar
from odoo import _, api, fields, models from odoo import _, api, fields, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
@ -187,87 +183,6 @@ class LoanIssue(models.Model):
self.ensure_one() self.ensure_one()
self.write({"state": "closed"}) self.write({"state": "closed"})
def get_number_of_days(self):
if calendar.isleap(date.today().year):
return 366
else:
return 365
def get_interest_vals(self, line, vals):
list_vals = []
accrued_amount = line.amount
accrued_interest = 0
accrued_net_interest = 0
accrued_taxes = 0
taxes_amount = 0
diff_days = 0
loan_term = int(self.loan_term / 12)
# if payment_date is first day of the month we take the current month
if line.payment_date.day == 1:
start_date = line.payment_date
else:
start_date = line.payment_date + relativedelta(months=+1, day=1)
# we calculate the number of day between payment date
# and the end of the month of the payment
diff_days = ((start_date - relativedelta(days=1)) -
line.payment_date).days
rate = self.rate / 100
# take leap year into account
days = self.get_number_of_days()
interim_amount = line.amount * rate * (diff_days / days)
due_date = start_date + relativedelta(years=+loan_term)
for year in range(1, loan_term + 1):
interest = accrued_amount * rate
due_amount = 0
if self.capital_payment == "end":
if year == loan_term:
due_amount = line.amount
else:
due_amount = line.amount * (loan_term / 100)
accrued_amount -= due_amount
if self.interest_payment == "end":
accrued_interest += interest
accrued_amount += interest
net_interest = 0
if year == loan_term:
taxes_amount = accrued_interest * (self.taxes_rate / 100)
net_interest = accrued_interest - taxes_amount
due_amount += net_interest
else:
due_date = start_date + relativedelta(years=+year)
taxes_amount = interest * (self.taxes_rate / 100)
net_interest = interest - taxes_amount
due_amount += net_interest
accrued_interest = interest
if year == 1:
interest += interim_amount
accrued_interest = interest
accrued_net_interest += net_interest
accrued_taxes += taxes_amount
vals["due_date"] = due_date
vals["due_amount"] = due_amount
vals["interest"] = interest
vals["net_interest"] = net_interest
vals["taxes_amount"] = taxes_amount
vals["accrued_amount"] = accrued_amount
vals["accrued_interest"] = accrued_interest
vals["accrued_net_interest"] = accrued_net_interest
vals["accrued_taxes"] = accrued_taxes
vals["name"] = year
list_vals.append(vals.copy())
self.env["loan.interest.line"].create(list_vals)
@api.multi @api.multi
def compute_loan_interest(self): def compute_loan_interest(self):
self.ensure_one() self.ensure_one()
@ -281,13 +196,6 @@ class LoanIssue(models.Model):
) )
) )
for line in self.loan_issue_lines:
# TODO remove this line
line.interest_lines.unlink()
# Please Do not Forget
if line.state == "paid":
vals = {
"issue_line": line.id,
"taxes_rate": self.taxes_rate,
}
self.get_interest_vals(line, vals)
lines = self.loan_issue_lines.filtered(lambda record:
record.state == "paid")
lines.action_compute_interest()

102
easy_my_coop_loan/models/loan_issue_line.py

@ -2,7 +2,9 @@
# Houssine BAKKALI <houssine@coopiteasy.be> # Houssine BAKKALI <houssine@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from datetime import datetime
import calendar
from datetime import date
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models from odoo import api, fields, models
@ -37,7 +39,7 @@ class LoanIssueLine(models.Model):
) )
date = fields.Date( date = fields.Date(
string="Subscription date", string="Subscription date",
default=lambda self: datetime.strftime(datetime.now(), "%Y-%m-%d"),
default=lambda self: date.strftime(date.today(), "%Y-%m-%d"),
required=True, required=True,
) )
payment_date = fields.Date( payment_date = fields.Date(
@ -133,3 +135,99 @@ class LoanIssueLine(models.Model):
if not line.payment_date: if not line.payment_date:
vals["payement_date"] = fields.Date.today() vals["payement_date"] = fields.Date.today()
line.write(vals) line.write(vals)
def get_number_of_days(self, year):
if calendar.isleap(year):
return 366
else:
return 365
@api.multi
def action_compute_interest(self):
for line in self:
loan_issue = line.loan_issue_id
vals = {
"issue_line": line.id,
"taxes_rate": loan_issue.taxes_rate,
}
list_vals = []
accrued_amount = self.amount
accrued_interest = 0
accrued_net_interest = 0
accrued_taxes = 0
taxes_amount = 0
diff_days = 0
# TODO remove this line
line.interest_lines.unlink()
# Please Do not Forget
loan_term = int(loan_issue.loan_term / 12)
# if payment_date is first day of the month
# we take the current month
if line.payment_date.day == 1:
start_date = line.payment_date
else:
start_date = line.payment_date + relativedelta(months=+1,
day=1)
# we calculate the number of day between payment date
# and the end of the month of the payment
diff_days = ((start_date - relativedelta(days=1)) -
line.payment_date).days
rate = loan_issue.rate / 100
# take leap year into account
days = self.get_number_of_days(line.payment_date.year)
interim_amount = line.amount * rate * (diff_days / days)
due_date = start_date + relativedelta(years=+loan_term)
for year in range(1, loan_term + 1):
interest = accrued_amount * rate
due_amount = 0
if loan_issue.capital_payment == "end":
if year == loan_term:
due_amount = line.amount
else:
due_amount = line.amount * (loan_term / 100)
accrued_amount -= due_amount
if loan_issue.interest_payment == "end":
accrued_interest += interest
accrued_amount += interest
net_interest = 0
if year == loan_term:
taxes_amount = (accrued_interest *
(loan_issue.taxes_rate / 100)
)
net_interest = accrued_interest - taxes_amount
due_amount += net_interest
else:
due_date = start_date + relativedelta(years=+year)
taxes_amount = interest * (loan_issue.taxes_rate / 100)
net_interest = interest - taxes_amount
due_amount += net_interest
accrued_interest = interest
if year == 1:
interest += interim_amount
accrued_interest = interest
accrued_net_interest += net_interest
accrued_taxes += taxes_amount
vals["due_date"] = due_date
vals["due_amount"] = due_amount
vals["interest"] = interest
vals["net_interest"] = net_interest
vals["taxes_amount"] = taxes_amount
vals["accrued_amount"] = accrued_amount
vals["accrued_interest"] = accrued_interest
vals["accrued_net_interest"] = accrued_net_interest
vals["accrued_taxes"] = accrued_taxes
vals["name"] = year
list_vals.append(vals.copy())
self.env["loan.interest.line"].create(list_vals)

3
easy_my_coop_loan/views/loan_line_view.xml

@ -41,6 +41,9 @@
type="object" states="cancelled"/> type="object" states="cancelled"/>
<button name="action_paid" string="Paid" <button name="action_paid" string="Paid"
type="object" states="waiting"/> type="object" states="waiting"/>
<button name="action_compute_interest" string="Compute interest"
type="object" states="paid"/>
<field name="state" widget="statusbar"/> <field name="state" widget="statusbar"/>
</header> </header>
<sheet> <sheet>

Loading…
Cancel
Save