# -*- coding: utf-8 -*-777777yyy
# Copyright (c) 2015, Frappe Technologies and contributors
# For license information, please see license.txt

from __future__ import print_function, unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import getdate

class StudentApplicant(Document):
	def autoname(self):
		from frappe.model.naming import set_name_by_naming_series
		if self.student_admission:
			if self.program:
				student_admission = get_student_admission_data(self.student_admission, self.program)
				if student_admission:
					naming_series = student_admission.get("applicant_naming_series")
			else:
				frappe.throw(_("Select the program first"))

			if naming_series:
				self.naming_series = naming_series

		set_name_by_naming_series(self)

	def validate(self):
		self.title = " ".join(filter(None, [self.first_name, self.middle_name, self.last_name]))
		if self.student_admission and self.program and self.date_of_birth:
			self.validation_from_student_admission()

	def on_update_after_submit(self):
		student = frappe.get_list("Student",  filters= {"student_applicant": self.name})
		if student:
			frappe.throw(_("Cannot change status as student {0} is linked with student application {1}").format(student[0].name, self.name))

	def on_submit(self):
		if self.paid and not self.student_admission:
			frappe.throw(_("Please select Student Admission which is mandatory for the paid student applicant"))

	def validation_from_student_admission(self):
		student_admission = get_student_admission_data(self.student_admission, self.program)
		if student_admission:
			if ((
					student_admission.minimum_age
					and getdate(student_admission.minimum_age) > getdate(self.date_of_birth)
				) or (
					student_admission.maximum_age
					and getdate(student_admission.maximum_age) < getdate(self.date_of_birth)
				)):
					frappe.throw(_("Not eligible for the admission in this program as per DOB"))

	def on_payment_authorized(self, *args, **kwargs):
		self.db_set('paid', 1)


def get_student_admission_data(student_admission, program):
	student_admission = frappe.db.sql("""select sa.admission_start_date, sa.admission_end_date,
		sap.program, sap.minimum_age, sap.maximum_age, sap.applicant_naming_series
		from `tabStudent Admission` sa, `tabStudent Admission Program` sap
		where sa.name = sap.parent and sa.name = %s and sap.program = %s""", (student_admission, program), as_dict=1)
	if student_admission:
		return student_admission[0]
	else:
		return None

@@ -468,16 +468,84 @@
 rogram:%0A
+%09%09%09%09# set the naming series from the student admission if provided.%0A
 %09%09%09%09stud
@@ -721,16 +721,52 @@
 eries%22)%0A
+%09%09%09%09else:%0A%09%09%09%09%09naming_series = None%0A
 %09%09%09else:
@@ -1708,16 +1708,122 @@
 program)
+%0A%0A%09%09# different validation for minimum and maximum age so that either min/max can also work independently.
 %0A%09%09if st
@@ -1841,24 +1841,13 @@
 sion
-:%0A%09%09%09if ((%0A%09%09%09%09%09
+ and 
 stud
@@ -1867,34 +1867,34 @@
 .minimum_age
-%0A%09%09%09%09%09and 
+ and %5C%0A%09%09%09
 getdate(stud
@@ -1924,9 +1924,9 @@
 ge) 
-%3E
+%3C
  get
@@ -1953,25 +1953,119 @@
 rth)
+:
 %0A%09%09%09%09
-) or (%0A%09%09%09%09%09
+frappe.throw(_(%22Not eligible for the admission in this program as per DOB%22))%0A%0A%09%09if student_admission and 
 stud
@@ -2093,18 +2093,18 @@
 _age
-%0A%09%09%09%09%09and 
+ and %5C%0A%09%09%09
 getd
@@ -2130,33 +2130,33 @@
 on.maximum_age) 
-%3C
+%3E
  getdate(self.da
@@ -2171,18 +2171,10 @@
 rth)
-%0A%09%09%09%09))
 :%0A
-%09
 %09%09%09%09
@@ -2247,24 +2247,25 @@
 per DOB%22))%0A%0A
+%0A
 %09def on_paym

set the naming series and independent dob validation