# Copyright (c) 2019, InfluxFramework LLC and contributors
# For license information, please see license.txt


import influxframework
from influxframework.model.document import Document
from influxframework.utils import nowdate

from influxerp.loan_management.doctype.loan_interest_accrual.loan_interest_accrual import (
	make_accrual_interest_entry_for_demand_loans,
	make_accrual_interest_entry_for_term_loans,
)


class ProcessLoanInterestAccrual(Document):
	def on_submit(self):
		open_loans = []

		if self.loan:
			loan_doc = influxframework.get_doc("Loan", self.loan)
			if loan_doc:
				open_loans.append(loan_doc)

		if (not self.loan or not loan_doc.is_term_loan) and self.process_type != "Term Loans":
			make_accrual_interest_entry_for_demand_loans(
				self.posting_date,
				self.name,
				open_loans=open_loans,
				loan_type=self.loan_type,
				accrual_type=self.accrual_type,
			)

		if (not self.loan or loan_doc.is_term_loan) and self.process_type != "Demand Loans":
			make_accrual_interest_entry_for_term_loans(
				self.posting_date,
				self.name,
				term_loan=self.loan,
				loan_type=self.loan_type,
				accrual_type=self.accrual_type,
			)


def process_loan_interest_accrual_for_demand_loans(
	posting_date=None, loan_type=None, loan=None, accrual_type="Regular"
):
	loan_process = influxframework.new_doc("Process Loan Interest Accrual")
	loan_process.posting_date = posting_date or nowdate()
	loan_process.loan_type = loan_type
	loan_process.process_type = "Demand Loans"
	loan_process.loan = loan
	loan_process.accrual_type = accrual_type

	loan_process.submit()

	return loan_process.name


def process_loan_interest_accrual_for_term_loans(posting_date=None, loan_type=None, loan=None):

	if not term_loan_accrual_pending(posting_date or nowdate(), loan=loan):
		return

	loan_process = influxframework.new_doc("Process Loan Interest Accrual")
	loan_process.posting_date = posting_date or nowdate()
	loan_process.loan_type = loan_type
	loan_process.process_type = "Term Loans"
	loan_process.loan = loan

	loan_process.submit()

	return loan_process.name


def term_loan_accrual_pending(date, loan=None):
	filters = {"payment_date": ("<=", date), "is_accrued": 0}

	if loan:
		filters.update({"parent": loan})

	pending_accrual = influxframework.db.get_value("Repayment Schedule", filters)

	return pending_accrual
