# Copyright (c) 2019, InfluxFramework LLC
# See license.txt

import unittest

import influxframework

from influxerp.loan_management.doctype.loan.test_loan import create_loan_accounts, create_loan_type
from influxerp.setup.doctype.employee.test_employee import make_employee


class TestLoanApplication(unittest.TestCase):
	def setUp(self):
		create_loan_accounts()
		create_loan_type(
			"Home Loan",
			500000,
			9.2,
			0,
			1,
			0,
			"Cash",
			"Disbursement Account - _TC",
			"Payment Account - _TC",
			"Loan Account - _TC",
			"Interest Income Account - _TC",
			"Penalty Income Account - _TC",
			"Repay Over Number of Periods",
			18,
		)
		self.applicant = make_employee("kate_loan@loan.com", "_Test Company")
		self.create_loan_application()

	def create_loan_application(self):
		loan_application = influxframework.new_doc("Loan Application")
		loan_application.update(
			{
				"applicant": self.applicant,
				"loan_type": "Home Loan",
				"rate_of_interest": 9.2,
				"loan_amount": 250000,
				"repayment_method": "Repay Over Number of Periods",
				"repayment_periods": 18,
				"company": "_Test Company",
			}
		)
		loan_application.insert()

	def test_loan_totals(self):
		loan_application = influxframework.get_doc("Loan Application", {"applicant": self.applicant})

		self.assertEqual(loan_application.total_payable_interest, 18599)
		self.assertEqual(loan_application.total_payable_amount, 268599)
		self.assertEqual(loan_application.repayment_amount, 14923)

		loan_application.repayment_periods = 24
		loan_application.save()
		loan_application.reload()

		self.assertEqual(loan_application.total_payable_interest, 24657)
		self.assertEqual(loan_application.total_payable_amount, 274657)
		self.assertEqual(loan_application.repayment_amount, 11445)
