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


import xhiveframework
from xhiveframework import _
from xhiveframework.model.document import Document
from xhiveframework.utils import get_datetime


class LoanSecurityPrice(Document):
	def validate(self):
		self.validate_dates()

	def validate_dates(self):

		if self.valid_from > self.valid_upto:
			xhiveframework.throw(_("Valid From Time must be lesser than Valid Upto Time."))

		existing_loan_security = xhiveframework.db.sql(
			""" SELECT name from `tabLoan Security Price`
			WHERE loan_security = %s AND name != %s AND (valid_from BETWEEN %s and %s OR valid_upto BETWEEN %s and %s) """,
			(
				self.loan_security,
				self.name,
				self.valid_from,
				self.valid_upto,
				self.valid_from,
				self.valid_upto,
			),
		)

		if existing_loan_security:
			xhiveframework.throw(_("Loan Security Price overlapping with {0}").format(existing_loan_security[0][0]))


@xhiveframework.whitelist()
def get_loan_security_price(loan_security, valid_time=None):
	if not valid_time:
		valid_time = get_datetime()

	loan_security_price = xhiveframework.db.get_value(
		"Loan Security Price",
		{
			"loan_security": loan_security,
			"valid_from": ("<=", valid_time),
			"valid_upto": (">=", valid_time),
		},
		"loan_security_price",
	)

	if not loan_security_price:
		xhiveframework.throw(_("No valid Loan Security Price found for {0}").format(xhiveframework.bold(loan_security)))
	else:
		return loan_security_price
