Department Tree (#13567)

* Department Tree

* print hide fields

* create 'All Departments' as parent on setup

* patch to set lft rgt

* fix translatable string
diff --git a/erpnext/hr/doctype/department/department.json b/erpnext/hr/doctype/department/department.json
index 2f4f20d..5a62548 100644
--- a/erpnext/hr/doctype/department/department.json
+++ b/erpnext/hr/doctype/department/department.json
@@ -1,5 +1,6 @@
 {
  "allow_copy": 0, 
+ "allow_guest_to_view": 0, 
  "allow_import": 1, 
  "allow_rename": 1, 
  "autoname": "field:department_name", 
@@ -12,6 +13,7 @@
  "editable_grid": 0, 
  "fields": [
   {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -39,9 +41,104 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "parent_department", 
+   "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": "Parent Department", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Department", 
+   "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, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 1, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "is_group", 
+   "fieldtype": "Check", 
+   "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": "Is Group", 
+   "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, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "section_break_5", 
+   "fieldtype": "Section 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, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
    "allow_on_submit": 0, 
    "bold": 0, 
    "collapsible": 0, 
@@ -69,21 +166,115 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "lft", 
+   "fieldtype": "Int", 
+   "hidden": 1, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "lft", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "rgt", 
+   "fieldtype": "Int", 
+   "hidden": 1, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "rgt", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "fieldname": "old_parent", 
+   "fieldtype": "Data", 
+   "hidden": 1, 
+   "ignore_user_permissions": 1, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Old Parent", 
+   "length": 0, 
+   "no_copy": 0, 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 1, 
+   "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, 
+   "translatable": 0, 
    "unique": 0
   }
  ], 
+ "has_web_view": 0, 
  "hide_heading": 0, 
  "hide_toolbar": 0, 
  "icon": "fa fa-sitemap", 
  "idx": 1, 
  "image_view": 0, 
  "in_create": 0, 
- "in_dialog": 0, 
  "is_submittable": 0, 
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-02-17 16:55:19.875594", 
+ "modified": "2018-04-09 12:54:13.608107", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Department", 
@@ -91,7 +282,6 @@
  "permissions": [
   {
    "amend": 0, 
-   "apply_user_permissions": 0, 
    "cancel": 0, 
    "create": 1, 
    "delete": 1, 
@@ -111,7 +301,6 @@
   }, 
   {
    "amend": 0, 
-   "apply_user_permissions": 0, 
    "cancel": 0, 
    "create": 1, 
    "delete": 1, 
diff --git a/erpnext/hr/doctype/department/department.py b/erpnext/hr/doctype/department/department.py
index 2e4c0f2..68f3405 100644
--- a/erpnext/hr/doctype/department/department.py
+++ b/erpnext/hr/doctype/department/department.py
@@ -3,8 +3,22 @@
 
 from __future__ import unicode_literals
 import frappe
-
+from frappe.utils.nestedset import NestedSet
+from erpnext.utilities.transaction_base import delete_events
 from frappe.model.document import Document
 
-class Department(Document):
-	pass
\ No newline at end of file
+class Department(NestedSet):
+	nsm_parent_field = 'parent_department'
+
+	def update_nsm_model(self):
+		frappe.utils.nestedset.update_nsm(self)
+
+	def on_update(self):
+		self.update_nsm_model()
+
+	def on_trash(self):
+		super(Department, self).on_trash()
+ 		delete_events(self.doctype, self.name)
+
+def on_doctype_update():
+	frappe.db.add_index("Department", ["lft", "rgt"])
\ No newline at end of file
diff --git a/erpnext/hr/doctype/department/department_tree.js b/erpnext/hr/doctype/department/department_tree.js
new file mode 100644
index 0000000..5652ad6
--- /dev/null
+++ b/erpnext/hr/doctype/department/department_tree.js
@@ -0,0 +1,3 @@
+frappe.treeview_settings["Department"] = {
+	ignore_fields:["parent_department"]
+};
\ No newline at end of file
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index ce208ad..edd9757 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -517,3 +517,4 @@
 erpnext.patches.v10_0.fix_reserved_qty_for_sub_contract
 erpnext.patches.v11_0.add_index_on_nestedset_doctypes
 erpnext.patches.v11_0.remove_modules_setup_page
+erpnext.patches.v11_0.update_department_lft_rgt
diff --git a/erpnext/patches/v11_0/update_department_lft_rgt.py b/erpnext/patches/v11_0/update_department_lft_rgt.py
new file mode 100644
index 0000000..9bb1772
--- /dev/null
+++ b/erpnext/patches/v11_0/update_department_lft_rgt.py
@@ -0,0 +1,19 @@
+import frappe
+from frappe import _
+from frappe.utils.nestedset import rebuild_tree
+
+def execute():
+	""" assign lft and rgt appropriately """
+	frappe.reload_doc("hr", "doctype", "department")
+
+	if not frappe.db.exists("Department", _('All Departments')):
+		frappe.get_doc({
+			'doctype': 'Department',
+			'department_name': _('All Departments'),
+			'is_group': 1
+		}).insert(ignore_permissions=True)
+
+	frappe.db.sql("""update `tabDepartment` set parent_department = '{0}'
+		where is_group = 0""".format(_('All Departments')))
+
+	rebuild_tree("Department", "parent_department")
\ No newline at end of file
diff --git a/erpnext/setup/setup_wizard/operations/install_fixtures.py b/erpnext/setup/setup_wizard/operations/install_fixtures.py
index 045dca0..397b093 100644
--- a/erpnext/setup/setup_wizard/operations/install_fixtures.py
+++ b/erpnext/setup/setup_wizard/operations/install_fixtures.py
@@ -105,19 +105,20 @@
 		{'doctype': 'Employment Type', 'employee_type_name': _('Apprentice')},
 
 		# Department
-		{'doctype': 'Department', 'department_name': _('Accounts')},
-		{'doctype': 'Department', 'department_name': _('Marketing')},
-		{'doctype': 'Department', 'department_name': _('Sales')},
-		{'doctype': 'Department', 'department_name': _('Purchase')},
-		{'doctype': 'Department', 'department_name': _('Operations')},
-		{'doctype': 'Department', 'department_name': _('Production')},
-		{'doctype': 'Department', 'department_name': _('Dispatch')},
-		{'doctype': 'Department', 'department_name': _('Customer Service')},
-		{'doctype': 'Department', 'department_name': _('Human Resources')},
-		{'doctype': 'Department', 'department_name': _('Management')},
-		{'doctype': 'Department', 'department_name': _('Quality Management')},
-		{'doctype': 'Department', 'department_name': _('Research & Development')},
-		{'doctype': 'Department', 'department_name': _('Legal')},
+		{'doctype': 'Department', 'department_name': _('All Departments'), 'is_group': 1, 'parent_department': ''},
+		{'doctype': 'Department', 'department_name': _('Accounts'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Marketing'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Sales'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Purchase'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Operations'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Production'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Dispatch'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Customer Service'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Human Resources'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Management'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Quality Management'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Research & Development'), 'parent_department': _('All Departments')},
+		{'doctype': 'Department', 'department_name': _('Legal'), 'parent_department': _('All Departments')},
 
 		# Designation
 		{'doctype': 'Designation', 'designation_name': _('CEO')},