diff --git a/easy_my_coop_loan_account/models/interest_line.py b/easy_my_coop_loan_account/models/interest_line.py
index 6b5f90e..413ad2a 100644
--- a/easy_my_coop_loan_account/models/interest_line.py
+++ b/easy_my_coop_loan_account/models/interest_line.py
@@ -1,6 +1,7 @@
# Copyright 2020 Coop IT Easy SCRL fs
# Houssine BAKKALI
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
+from dateutil.relativedelta import relativedelta
from odoo import api, fields, models
@@ -19,6 +20,14 @@ class LoanInterestLine(models.Model):
comodel_name="account.move",
string="Loan reimbursement account move"
)
+ interest_closing_fy = fields.Many2one(
+ comodel_name="account.move",
+ string="Interest closing fiscal year account move"
+ )
+ interest_opening_fy = fields.Many2one(
+ comodel_name="account.move",
+ string="Interest opening fiscal year account move"
+ )
@api.multi
def get_move_line(self, move_id, partner=None):
@@ -71,7 +80,7 @@ class LoanInterestLine(models.Model):
vals_list = [debit_vals, loaner_vals]
if line.taxes_amount > 0:
- tax_vals = self.get_move_line(move)
+ tax_vals = line.get_move_line(move)
tax_vals["credit"] = line.taxes_amount
tax_vals["account_id"] = company.tax_account.id
vals_list.append(tax_vals)
@@ -82,28 +91,54 @@ class LoanInterestLine(models.Model):
"state": "scheduled"})
@api.multi
- def generate_interest_move_lines_fy(self, date):
-
+ def generate_interest_move_lines_fy(self, date, next_fy):
+ aml_obj = self.env["account.move.line"]
for line in self:
- if not self.loan_due_fy_move:
+ if not self.interest_closing_fy:
company = line.company_id
- move = line.create_move(date)
- deb_vals = line.get_move_line(move, line.partner_id)
- cred_vals = line.get_move_line(move, line.partner_id)
+ # compute the prorata interest for the fiscal year
+ prorata_date = line.due_date - relativedelta(years=-1)
+ diff_days = (prorata_date - date).days
+ days = line.loan_issue_id.get_number_of_days(date.year)
- deb_vals["debit"] = line.due_loan_amount
+ previous_interest = line.accrued_interest - line.interest
+ prorata_interest = line.interest * (diff_days / days)
+ interest_fy = previous_interest + prorata_interest
+
+ # create interest closing account move lines
+ close_fy_move = line.create_move(date)
+ deb_vals = line.get_move_line(close_fy_move, line.partner_id)
+ cred_vals = line.get_move_line(close_fy_move, line.partner_id)
+
+ deb_vals["debit"] = interest_fy
deb_vals["date"] = date
- deb_vals["account_id"] = company.debt_long_term_account.id
+ deb_vals["account_id"] = company.interest_account.id
- cred_vals["credit"] = line.due_loan_amount
+ cred_vals["credit"] = interest_fy
cred_vals["date"] = date
- cred_vals["account_id"] = company.debt_long_term_fy_account.id
+ cred_vals["account_id"] = company.expense_account.id
- self.env["account.move.line"].create([deb_vals, cred_vals])
+ aml_obj.create([deb_vals, cred_vals])
- line.write({"loan_due_fy_move": move.id,
- "state": "due_fy"})
+ line.write({"interest_closing_fy": close_fy_move.id})
+
+ # create interest opening account move lines
+ opening_date = next_fy.date_from
+ open_fy_move = line.create_move(opening_date)
+ deb_vals = line.get_move_line(open_fy_move, line.partner_id)
+ cred_vals = line.get_move_line(open_fy_move, line.partner_id)
+
+ deb_vals["debit"] = interest_fy
+ deb_vals["date"] = opening_date
+ deb_vals["account_id"] = company.expense_account.id
+
+ cred_vals["credit"] = interest_fy
+ cred_vals["date"] = opening_date
+ cred_vals["account_id"] = company.interest_account.id
+
+ aml_obj.create([deb_vals, cred_vals])
+ line.write({"interest_opening_fy": open_fy_move.id})
@api.multi
def generate_loan_due_fy(self, date):
@@ -137,7 +172,7 @@ class LoanInterestLine(models.Model):
company = line.company_id
move = line.create_move(fields.Date.today())
- debit_vals = line.get_move_line(move, line)
+ debit_vals = line.get_move_line(move, line.partner_id)
credit_vals = line.get_move_line(move, line.partner_id)
debit_vals["debit"] = line.due_loan_amount
diff --git a/easy_my_coop_loan_account/wizard/end_of_year_operation.py b/easy_my_coop_loan_account/wizard/end_of_year_operation.py
index 6ce356c..e2b8f3c 100644
--- a/easy_my_coop_loan_account/wizard/end_of_year_operation.py
+++ b/easy_my_coop_loan_account/wizard/end_of_year_operation.py
@@ -9,12 +9,6 @@ from odoo.exceptions import UserError
class LoanEndOfYearOperation(models.TransientModel):
_name = "loan.end.of.year.operation"
- operation_type = fields.Selection(
- [("eoy_debt", "End of year debt operation"),
- ("eoy_interest", "End of year interest operation")],
- required=True,
- string="Operation type"
- )
ongoing_fy_id = fields.Many2one(
comodel_name="account.fiscal.year",
string="Ongoing fiscal year",
@@ -33,31 +27,27 @@ class LoanEndOfYearOperation(models.TransientModel):
last_fy_day = self.ongoing_fy_id.date_to
next_fy = afy_obj.get_next_fiscal_year(last_fy_day)
- if self.operation_type == "eoy_debt":
- if next_fy:
- interest_lines = interest_line_obj.search([
- ("due_date", ">=", next_fy.date_from),
- ("due_date", "<=", next_fy.date_to),
- ("due_loan_amount", ">", 0),
- ("loan_issue_id", "in", loan_issues.ids)
- ])
-
- if interest_lines:
- interest_lines.generate_loan_due_fy(last_fy_day)
- else:
- raise UserError("There is no account move lines to"
- " generate for the selected loan issue")
- elif self.operation_type == "eoy_interest":
- if next_fy:
- interest_lines = interest_line_obj.search([
- ("due_date", ">=", next_fy.date_from),
- ("due_date", "<=", next_fy.date_to),
- ("interest", ">", 0),
- ("loan_issue_id", "in", loan_issues.ids)
- ])
-
- if interest_lines:
- interest_lines.generate_loan_due_fy(last_fy_day)
- else:
- raise UserError("There is no account move lines to"
- " generate for the selected loan issue")
+ if next_fy:
+ interest_lines_loan = interest_line_obj.search([
+ ("due_date", ">=", next_fy.date_from),
+ ("due_date", "<=", next_fy.date_to),
+ ("due_loan_amount", ">", 0),
+ ("loan_issue_id", "in", loan_issues.ids),
+ ("loan_reimbursment_move", "=", False)
+ ])
+
+ interest_lines_loan.generate_loan_due_fy(last_fy_day)
+ interest_lines_inter = interest_line_obj.search([
+ ("due_date", ">=", next_fy.date_from),
+ ("due_date", "<=", next_fy.date_to),
+ ("interest", ">", 0),
+ ("loan_issue_id", "in", loan_issues.ids),
+ ("interest_closing_fy", "=", False),
+ ("interest_opening_fy", "=", False)
+ ])
+
+ interest_lines_inter.generate_interest_move_lines_fy(last_fy_day,
+ next_fy)
+ if not interest_lines_loan and not interest_lines_inter:
+ raise UserError("There is no end of year account move lines to"
+ " generate for the selected loan issue")
diff --git a/easy_my_coop_loan_account/wizard/end_of_year_operation.xml b/easy_my_coop_loan_account/wizard/end_of_year_operation.xml
index b2ae81a..d64dda1 100644
--- a/easy_my_coop_loan_account/wizard/end_of_year_operation.xml
+++ b/easy_my_coop_loan_account/wizard/end_of_year_operation.xml
@@ -9,7 +9,6 @@
End of year loan operation.
-