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


import influxframework
from influxframework import _
from influxframework.model.document import Document


class LoanType(Document):
	def validate(self):
		self.validate_accounts()

	def validate_accounts(self):
		for fieldname in [
			"payment_account",
			"loan_account",
			"interest_income_account",
			"penalty_income_account",
		]:
			company = influxframework.get_value("Account", self.get(fieldname), "company")

			if company and company != self.company:
				influxframework.throw(
					_("Account {0} does not belong to company {1}").format(
						influxframework.bold(self.get(fieldname)), influxframework.bold(self.company)
					)
				)

		if self.get("loan_account") == self.get("payment_account"):
			influxframework.throw(_("Loan Account and Payment Account cannot be same"))
