[Tests] Employee Onboarding & Separation (#14606)

* Set query on department

* Add test for Employee Onboarding

* Add fetch for Employee Separation fields

* Add test for Employee Separation

* Fix Staffing Plan test case
diff --git a/erpnext/hr/doctype/employee_onboarding/employee_onboarding.py b/erpnext/hr/doctype/employee_onboarding/employee_onboarding.py
index 9262a06..e22fbc9 100644
--- a/erpnext/hr/doctype/employee_onboarding/employee_onboarding.py
+++ b/erpnext/hr/doctype/employee_onboarding/employee_onboarding.py
@@ -8,6 +8,7 @@
 from erpnext.hr.utils import EmployeeBoardingController
 from frappe.model.mapper import get_mapped_doc
 
+class IncompleteTaskError(frappe.ValidationError): pass
 
 class EmployeeOnboarding(EmployeeBoardingController):
 	def validate(self):
@@ -23,7 +24,7 @@
 				else:
 					task_status = frappe.db.get_value("Task", activity.task, "status")
 					if task_status not in ["Closed", "Cancelled"]:
-						frappe.throw(_("All the mandatory Task for employee creation hasn't been done yet."))
+						frappe.throw(_("All the mandatory Task for employee creation hasn't been done yet."), IncompleteTaskError)
 
 	def on_submit(self):
 		super(EmployeeOnboarding, self).on_submit()
diff --git a/erpnext/hr/doctype/employee_onboarding/test_employee_onboarding.py b/erpnext/hr/doctype/employee_onboarding/test_employee_onboarding.py
index b37ae88..8bf6957 100644
--- a/erpnext/hr/doctype/employee_onboarding/test_employee_onboarding.py
+++ b/erpnext/hr/doctype/employee_onboarding/test_employee_onboarding.py
@@ -5,6 +5,61 @@
 
 import frappe
 import unittest
+from frappe.utils import nowdate
+from erpnext.hr.doctype.employee_onboarding.employee_onboarding import make_employee
+from erpnext.hr.doctype.employee_onboarding.employee_onboarding import IncompleteTaskError
 
 class TestEmployeeOnboarding(unittest.TestCase):
-	pass
+	def test_employee_onboarding_incomplete_task(self):
+		if frappe.db.exists('Employee Onboarding', {'employee_name': 'Test Applicant'}):
+			return frappe.get_doc('Employee Onboarding', {'employee_name': 'Test Applicant'})
+		_set_up()
+		applicant = get_job_applicant()
+		onboarding = frappe.new_doc('Employee Onboarding')
+		onboarding.job_applicant = applicant.name
+		onboarding.employee_name = 'Test Applicant'
+		onboarding.company = '_Test Company'
+		onboarding.designation = 'Researcher'
+		onboarding.append('activities', {
+			'activity_name': 'Assign ID Card',
+			'role': 'HR User',
+			'required_for_employee_creation': 1
+		})
+		onboarding.append('activities', {
+			'activity_name': 'Assign a laptop',
+			'role': 'HR User'
+		})
+		onboarding.status = 'Pending'
+		onboarding.insert()
+		onboarding.submit()
+		self.assertEqual(onboarding.project, 'Employee Onboarding : Test Researcher - test@researcher.com')
+		self.assertRaises(IncompleteTaskError, make_employee, onboarding.name)
+		return onboarding
+
+	def test_employee_onboarding_completed_task(self):
+		doc = self.test_employee_onboarding_incomplete_task()
+		project = frappe.get_doc('Project', doc.project)
+		project.tasks[0].status = 'Closed'
+		project.save()
+		doc.reload()
+		employee = make_employee(doc.name)
+		employee.date_of_joining = nowdate()
+		employee.date_of_birth = '1990-05-08'
+		employee.gender = 'Female'
+		employee.insert()
+		self.assertEqual(employee.employee_name, 'Test Applicant')
+
+def get_job_applicant():
+	if frappe.db.exists('Job Applicant', 'Test Researcher - test@researcher.com'):
+		return frappe.get_doc('Job Applicant', 'Test Researcher - test@researcher.com')
+	applicant = frappe.new_doc('Job Applicant')
+	applicant.applicant_name = 'Test Researcher'
+	applicant.email_id = 'test@researcher.com'
+	applicant.status = 'Open'
+	applicant.cover_letter = 'I am a great Researcher.'
+	applicant.insert()
+	return applicant
+
+def _set_up():
+	for doctype in ["Employee Onboarding"]:
+		frappe.db.sql("delete from `tab{doctype}`".format(doctype=doctype))
\ No newline at end of file
diff --git a/erpnext/hr/doctype/employee_onboarding_template/employee_onboarding_template.js b/erpnext/hr/doctype/employee_onboarding_template/employee_onboarding_template.js
index 2a531f3..5e1b6e9 100644
--- a/erpnext/hr/doctype/employee_onboarding_template/employee_onboarding_template.js
+++ b/erpnext/hr/doctype/employee_onboarding_template/employee_onboarding_template.js
@@ -2,7 +2,13 @@
 // For license information, please see license.txt
 
 frappe.ui.form.on('Employee Onboarding Template', {
-	refresh: function(frm) {
-
+	setup: function(frm) {
+		frm.set_query("department", function() {
+			return {
+				filters: {
+					company: frm.doc.company
+				}
+			};
+		});
 	}
 });
diff --git a/erpnext/hr/doctype/employee_separation/employee_separation.json b/erpnext/hr/doctype/employee_separation/employee_separation.json
index 7941576..2b281dc 100644
--- a/erpnext/hr/doctype/employee_separation/employee_separation.json
+++ b/erpnext/hr/doctype/employee_separation/employee_separation.json
@@ -15,6 +15,7 @@
  "fields": [
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -42,16 +43,17 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
-   "fetch_from": "employee.employee_name",
+   "fetch_from": "employee.employee_name", 
    "fieldname": "employee_name", 
    "fieldtype": "Data", 
    "hidden": 0, 
@@ -75,15 +77,17 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_from": "employee.resignation_letter_date", 
    "fieldname": "resignation_letter_date", 
    "fieldtype": "Date", 
    "hidden": 0, 
@@ -106,11 +110,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
@@ -138,11 +143,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -170,11 +176,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -200,11 +207,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -232,15 +240,17 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_from": "employee.company", 
    "fieldname": "company", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -264,15 +274,17 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_from": "employee.department", 
    "fieldname": "department", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -296,15 +308,17 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_from": "employee.designation", 
    "fieldname": "designation", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -328,15 +342,17 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
    "columns": 0, 
+   "fetch_from": "employee.grade", 
    "fieldname": "employee_grade", 
    "fieldtype": "Link", 
    "hidden": 0, 
@@ -360,11 +376,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -391,11 +408,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 1, 
    "bold": 0, 
    "collapsible": 0, 
@@ -423,11 +441,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -453,11 +472,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -484,11 +504,12 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }, 
   {
    "allow_bulk_edit": 0, 
+   "allow_in_quick_entry": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -515,7 +536,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
-   "translatable": 0,
+   "translatable": 0, 
    "unique": 0
   }
  ], 
@@ -529,7 +550,7 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2018-05-16 22:42:46.734328",
+ "modified": "2018-06-20 14:21:54.707831", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Employee Separation", 
diff --git a/erpnext/hr/doctype/employee_separation/test_employee_separation.py b/erpnext/hr/doctype/employee_separation/test_employee_separation.py
index 0773fb6..6ae90df 100644
--- a/erpnext/hr/doctype/employee_separation/test_employee_separation.py
+++ b/erpnext/hr/doctype/employee_separation/test_employee_separation.py
@@ -6,5 +6,24 @@
 import frappe
 import unittest
 
+test_dependencies = ["Employee Onboarding"]
+
 class TestEmployeeSeparation(unittest.TestCase):
-	pass
+	def test_employee_separation(self):
+		employee = get_employee()
+		separation = frappe.new_doc('Employee Separation')
+		separation.employee = employee.name
+		separation.company = '_Test Company'
+		separation.append('activities', {
+			'activity_name': 'Deactivate Employee',
+			'role': 'HR User'
+		})
+		separation.status = 'Pending'
+		separation.insert()
+		separation.submit()
+		self.assertEqual(separation.docstatus, 1)
+		separation.cancel()
+		self.assertEqual(separation.project, "")
+
+def get_employee():
+	return frappe.get_doc('Employee', {'employee_name': 'Test Applicant'})
\ No newline at end of file
diff --git a/erpnext/hr/doctype/staffing_plan/test_staffing_plan.py b/erpnext/hr/doctype/staffing_plan/test_staffing_plan.py
index d95284c..66d9cdd 100644
--- a/erpnext/hr/doctype/staffing_plan/test_staffing_plan.py
+++ b/erpnext/hr/doctype/staffing_plan/test_staffing_plan.py
@@ -15,8 +15,6 @@
 	def test_staffing_plan(self):
 		_set_up()
 		frappe.db.set_value("Company", "_Test Company", "is_group", 1)
-		make_company()
-		set_employees()
 		if frappe.db.exists("Staffing Plan", "Test"):
 			return
 		staffing_plan = frappe.new_doc("Staffing Plan")
@@ -25,15 +23,16 @@
 		staffing_plan.from_date = nowdate()
 		staffing_plan.to_date = add_days(nowdate(), 10)
 		staffing_plan.append("staffing_details", {
-			"designation": "Researcher",
+			"designation": "Designer",
 			"number_of_positions": 6,
 			"estimated_cost_per_position": 50000
 		})
 		staffing_plan.insert()
 		staffing_plan.submit()
-		self.assertEqual(staffing_plan.total_estimated_budget, 250000.00)
+		self.assertEqual(staffing_plan.total_estimated_budget, 300000.00)
 
 	def test_staffing_plan_subsidiary_company(self):
+		self.test_staffing_plan()
 		if frappe.db.exists("Staffing Plan", "Test 1"):
 			return
 		staffing_plan = frappe.new_doc("Staffing Plan")
@@ -42,7 +41,7 @@
 		staffing_plan.from_date = nowdate()
 		staffing_plan.to_date = add_days(nowdate(), 10)
 		staffing_plan.append("staffing_details", {
-			"designation": "Researcher",
+			"designation": "Designer",
 			"number_of_positions": 3,
 			"estimated_cost_per_position": 45000
 		})
@@ -58,13 +57,13 @@
 		staffing_plan.from_date = nowdate()
 		staffing_plan.to_date = add_days(nowdate(), 10)
 		staffing_plan.append("staffing_details", {
-			"designation": "Researcher",
+			"designation": "Designer",
 			"number_of_positions": 7,
 			"estimated_cost_per_position": 50000
 		})
 		staffing_plan.insert()
 		staffing_plan.submit()
-		self.assertEqual(staffing_plan.total_estimated_budget, 250000.00)
+		self.assertEqual(staffing_plan.total_estimated_budget, 350000.00)
 		if frappe.db.exists("Staffing Plan", "Test 1"):
 			return
 		staffing_plan = frappe.new_doc("Staffing Plan")
@@ -73,7 +72,7 @@
 		staffing_plan.from_date = nowdate()
 		staffing_plan.to_date = add_days(nowdate(), 10)
 		staffing_plan.append("staffing_details", {
-			"designation": "Researcher",
+			"designation": "Designer",
 			"number_of_positions": 7,
 			"estimated_cost_per_position": 60000
 		})
@@ -83,6 +82,7 @@
 def _set_up():
 	for doctype in ["Staffing Plan", "Staffing Plan Detail"]:
 		frappe.db.sql("delete from `tab{doctype}`".format(doctype=doctype))
+	make_company()
 
 def make_company():
 	if frappe.db.exists("Company", "_Test Company 3"):
@@ -94,9 +94,3 @@
 	company.default_currency = "INR"
 	company.country = "India"
 	company.insert()
-
-def set_employees():
-	frappe.db.set_value("Employee", "_T-Employee-00001", "designation", "Researcher")
-	frappe.db.set_value("Employee", "_T-Employee-00001", "company", "_Test Company")
-	frappe.db.set_value("Employee", "_T-Employee-00002", "designation", "Researcher")
-	frappe.db.set_value("Employee", "_T-Employee-00002", "company", "_Test Company 3")