Fix + Enhancement in Program Enrollment Tool (#12291)

* Fix + Enhancement in Program Enrollment Tool

* updated the docs
diff --git a/erpnext/docs/user/manual/en/education/admission/program-enrollment-tool.md b/erpnext/docs/user/manual/en/education/admission/program-enrollment-tool.md
index 53d9d9c..97a26c7 100644
--- a/erpnext/docs/user/manual/en/education/admission/program-enrollment-tool.md
+++ b/erpnext/docs/user/manual/en/education/admission/program-enrollment-tool.md
@@ -1,16 +1,21 @@
 # Program Enrollment Tool
 
-The Program Enrollment tool allows the bulk enrollment of the **Student Applicants** in a Program. 
+The Program Enrollment tool allows the bulk enrollment of the new and old students in a Program. If you are enrolling a new student, you  can fetch the students from the **Student Applicant** or if you are promoting the older students you can fetch them from the **Program Enrollment** itself
 
+> Note: Academic Term is optional in the Program Enrollment Tool
 
-You can create the the Program Enrollment for :
+You can create the Program Enrollment for :
 
-1. **Student Applicants** >> List of Student Applicants will be fetched for the selected **Program** and **Academic year**.
+1. **Student Applicants**: List of Student Applicants will be fetched for the selected **Program**, **Academic year** and **Academic Term** (if provided). 
 
 <img class="screenshot" alt="Student Applicant Enrollment" src="/docs/assets/img/education/admission/program-enrollment-tool.gif">
 
-2. **Program Enrollment** >> You can bulk update the **Program** for the students from one academic year to another in the same **Program** or a new **Program**.
+2. **Program Enrollment**: List of students already enrolled in selected **Program** for the given **Academic Year**, **Academic Term** (if provided) and **Student Batch** will be fetched and can be used to enroll students from one academic year/term to another in the same **Program** or a new **Program**.
 
 <img class="screenshot" alt="Student Applicant Enrollment" src="/docs/assets/img/education/admission/program-enrollment-tool01.gif">
 
+**New Student Batch**: This can be selected for the entire students fetched in the table. Priority will be given to the Batch selected in the table (for individual students).
+
+For promoting the students, the new academic year, academic term and program can also be selected for the enrollment of the fetched students list.
+
 {next}
\ No newline at end of file
diff --git a/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.js b/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.js
index bf6f3e0..4f8ce6d 100644
--- a/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.js
+++ b/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.js
@@ -1,11 +1,19 @@
 // Copyright (c) 2016, Frappe and contributors
 // For license information, please see license.txt
-cur_frm.add_fetch("student", "title", "student_name");
-cur_frm.add_fetch("student_applicant", "title", "student_name");
 
 frappe.ui.form.on("Program Enrollment Tool", {
+	setup: function(frm) {
+		frm.add_fetch("student", "title", "student_name");
+		frm.add_fetch("student_applicant", "title", "student_name");
+	},
+
 	"refresh": function(frm) {
 		frm.disable_save();
+		frm.fields_dict.enroll_students.$input.addClass(' btn btn-primary');
+		frappe.realtime.on("program_enrollment_tool", function(data) {
+			frappe.hide_msgprint(true);
+			frappe.show_progress(__("Enrolling students"), data.progress[0], data.progress[1]);
+		});
 	},
 
 	"get_students": function(frm) {
@@ -18,7 +26,7 @@
 					frm.set_value("students", r.message);
 				}
 			}
-		})
+		});
 	},
 
 	"enroll_students": function(frm) {
@@ -27,7 +35,8 @@
 			doc:frm.doc,
 			callback: function(r) {
 				frm.set_value("students", []);
+				frappe.hide_msgprint(true);
 			}
-		})
+		});
 	}
 });
diff --git a/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.json b/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.json
index 71071cd..d611a6f 100644
--- a/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.json
+++ b/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.json
@@ -29,7 +29,7 @@
    "label": "Get Students From", 
    "length": 0, 
    "no_copy": 0, 
-   "options": "\nStudent Applicants\nProgram Enrollments", 
+   "options": "\nStudent Applicant\nProgram Enrollment", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 0, 
@@ -79,6 +79,67 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "depends_on": "eval:doc.get_students_from==\"Program Enrollment\"", 
+   "fieldname": "student_batch", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Student Batch", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Student Batch Name", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "column_break_3", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "academic_year", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -110,8 +171,8 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fieldname": "get_students", 
-   "fieldtype": "Button", 
+   "fieldname": "academic_term", 
+   "fieldtype": "Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
    "ignore_xss_filter": 0, 
@@ -119,9 +180,10 @@
    "in_global_search": 0, 
    "in_list_view": 0, 
    "in_standard_filter": 0, 
-   "label": "Get Students", 
+   "label": "Academic Term", 
    "length": 0, 
    "no_copy": 0, 
+   "options": "Academic Term", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 0, 
@@ -169,6 +231,36 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "get_students", 
+   "fieldtype": "Button", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Get Students", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "students", 
    "fieldtype": "Table", 
    "hidden": 0, 
@@ -209,6 +301,7 @@
    "in_global_search": 0, 
    "in_list_view": 0, 
    "in_standard_filter": 0, 
+   "label": "Enrollment Details", 
    "length": 0, 
    "no_copy": 0, 
    "permlevel": 0, 
@@ -229,7 +322,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "depends_on": "eval:doc.get_students_from==\"Program Enrollments\"", 
+   "depends_on": "eval:doc.get_students_from==\"Program Enrollment\"", 
    "fieldname": "new_program", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -261,8 +354,7 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "depends_on": "eval:doc.get_students_from==\"Program Enrollments\"", 
-   "fieldname": "new_academic_year", 
+   "fieldname": "new_student_batch", 
    "fieldtype": "Link", 
    "hidden": 0, 
    "ignore_user_permissions": 0, 
@@ -271,10 +363,10 @@
    "in_global_search": 0, 
    "in_list_view": 0, 
    "in_standard_filter": 0, 
-   "label": "New Academic Year", 
+   "label": "New Student Batch", 
    "length": 0, 
    "no_copy": 0, 
-   "options": "Academic Year", 
+   "options": "Student Batch Name", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 0, 
@@ -316,6 +408,99 @@
    "search_index": 0, 
    "set_only_once": 0, 
    "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "column_break_12", 
+   "fieldtype": "Column Break", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "eval:doc.get_students_from==\"Program Enrollment\"", 
+   "fieldname": "new_academic_year", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "New Academic Year", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Academic Year", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "eval:doc.get_students_from==\"Program Enrollment\"", 
+   "fieldname": "new_academic_term", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "New Academic Term", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Academic Term", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
   }
  ], 
  "has_web_view": 0, 
@@ -328,7 +513,7 @@
  "issingle": 1, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-12-27 09:35:45.002469", 
+ "modified": "2018-01-02 11:59:40.230689", 
  "modified_by": "Administrator", 
  "module": "Education", 
  "name": "Program Enrollment Tool", 
diff --git a/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.py b/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.py
index d989d9f..824a295 100644
--- a/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.py
+++ b/erpnext/education/doctype/program_enrollment_tool/program_enrollment_tool.py
@@ -17,21 +17,26 @@
 		elif not self.academic_year:
 			frappe.throw(_("Mandatory field - Academic Year"))
 		else:
-			if self.get_students_from == "Student Applicants":
-				students = frappe.db.sql("select name as student_applicant, title as student_name from \
-					`tabStudent Applicant` where program = %s and academic_year = %s",(self.program, self.academic_year), as_dict=1)
-			else:
-				students = frappe.db.sql("select student, student_name, student_batch_name from \
-					`tabProgram Enrollment` where program = %s and academic_year = %s",(self.program, self.academic_year), as_dict=1)
+			condition = 'and academic_term=%(academic_term)s' if self.academic_term else " "
+			if self.get_students_from == "Student Applicant":
+				students = frappe.db.sql('''select name as student_applicant, title as student_name from `tabStudent Applicant`
+					where application_status="Approved" and program=%(program)s and academic_year=%(academic_year)s {0}'''
+					.format(condition), self.as_dict(), as_dict=1)
+			elif self.get_students_from == "Program Enrollment":
+				condition2 = 'and student_batch_name=%(student_batch)s' if self.student_batch else " "
+				students = frappe.db.sql('''select student, student_name, student_batch_name from `tabProgram Enrollment`
+					where program=%(program)s and academic_year=%(academic_year)s {0} {1}'''
+					.format(condition, condition2), self.as_dict(), as_dict=1)
+
 				student_list = [d.student for d in students]
+				if student_list:
+					inactive_students = frappe.db.sql('''
+						select name as student, title as student_name from `tabStudent` where name in (%s) and enabled = 0''' %
+						', '.join(['%s']*len(student_list)), tuple(student_list), as_dict=1)
 
-				inactive_students = frappe.db.sql('''
-					select name as student, title as student_name from `tabStudent` where name in (%s) and enabled = 0''' %
-					', '.join(['%s']*len(student_list)), tuple(student_list), as_dict=1)
-
-				for student in students:
-					if student.student in [d.student for d in inactive_students]:
-						students.remove(student)
+					for student in students:
+						if student.student in [d.student for d in inactive_students]:
+							students.remove(student)
 
 		if students:
 			return students
@@ -39,18 +44,22 @@
 			frappe.throw(_("No students Found"))
 			
 	def enroll_students(self):
-		for stud in self.students:
+		total = len(self.students)
+		for i, stud in enumerate(self.students):
+			frappe.publish_realtime("program_enrollment_tool", dict(progress=[i+1, total]), user=frappe.session.user)
 			if stud.student:
 				prog_enrollment = frappe.new_doc("Program Enrollment")
 				prog_enrollment.student = stud.student
 				prog_enrollment.student_name = stud.student_name
-				prog_enrollment.student_batch_name = stud.student_batch_name
 				prog_enrollment.program = self.new_program
 				prog_enrollment.academic_year = self.new_academic_year
+				prog_enrollment.academic_term = self.new_academic_term
+				prog_enrollment.student_batch_name = stud.student_batch_name if stud.student_batch_name else self.new_student_batch
 				prog_enrollment.save()
 			elif stud.student_applicant:
 				prog_enrollment = enroll_student(stud.student_applicant)
 				prog_enrollment.academic_year = self.academic_year
+				prog_enrollment.academic_term = self.academic_term
+				prog_enrollment.student_batch_name = stud.student_batch_name if stud.student_batch_name else self.new_student_batch
 				prog_enrollment.save()
-		frappe.msgprint("Students have been enrolled.")
-			
\ No newline at end of file
+		frappe.msgprint("{0} Students have been enrolled.".format(total))
diff --git a/erpnext/education/doctype/program_enrollment_tool_student/program_enrollment_tool_student.json b/erpnext/education/doctype/program_enrollment_tool_student/program_enrollment_tool_student.json
index 0dbe1b8..9be292b 100644
--- a/erpnext/education/doctype/program_enrollment_tool_student/program_enrollment_tool_student.json
+++ b/erpnext/education/doctype/program_enrollment_tool_student/program_enrollment_tool_student.json
@@ -133,6 +133,37 @@
    "search_index": 0, 
    "set_only_once": 0, 
    "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "student_batch_name", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 1, 
+   "in_standard_filter": 0, 
+   "label": "Student Batch Name", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Student Batch Name", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
   }
  ], 
  "has_web_view": 0, 
@@ -145,7 +176,7 @@
  "issingle": 0, 
  "istable": 1, 
  "max_attachments": 0, 
- "modified": "2017-11-10 19:09:59.530615", 
+ "modified": "2018-01-02 12:03:53.890741", 
  "modified_by": "Administrator", 
  "module": "Education", 
  "name": "Program Enrollment Tool Student", 
diff --git a/erpnext/education/doctype/student_applicant/student_applicant.json b/erpnext/education/doctype/student_applicant/student_applicant.json
index 9c84234..1140984 100644
--- a/erpnext/education/doctype/student_applicant/student_applicant.json
+++ b/erpnext/education/doctype/student_applicant/student_applicant.json
@@ -326,6 +326,37 @@
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fieldname": "academic_term", 
+   "fieldtype": "Link", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Academic Term", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Academic Term", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 0, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
    "fieldname": "student_admission", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -1058,7 +1089,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2017-11-10 19:08:55.049625", 
+ "modified": "2018-01-02 11:47:14.944338", 
  "modified_by": "Administrator", 
  "module": "Education", 
  "name": "Student Applicant",