From 3aa205f19d6c93efc3876010ebf0c439a803bdf2 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 29 May 2018 13:08:40 +0530 Subject: [PATCH] Fixes related to department tree --- erpnext/hr/doctype/department/department.py | 13 +++++++------ .../create_department_records_for_each_company.py | 3 ++- erpnext/patches/v11_0/update_department_lft_rgt.py | 6 ++++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/erpnext/hr/doctype/department/department.py b/erpnext/hr/doctype/department/department.py index 99a9b8518c..063ed052a3 100644 --- a/erpnext/hr/doctype/department/department.py +++ b/erpnext/hr/doctype/department/department.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import frappe from frappe import _ -from frappe.utils.nestedset import NestedSet +from frappe.utils.nestedset import NestedSet, get_root_of from erpnext.utilities.transaction_base import delete_events from frappe.model.document import Document @@ -12,16 +12,17 @@ class Department(NestedSet): nsm_parent_field = 'parent_department' def autoname(self): - if not self.department_name==_("All Departments"): + if self.department_name != get_root_of("Department"): abbr = frappe.db.get_value('Company', self.company, 'abbr') self.name = '{0} - {1}'.format(self.department_name, abbr) else: self.name = self.department_name def validate(self): - if not self.parent_department and self.department_name != _("All Departments") \ - and frappe.db.exists("Department", _("All Departments")): - self.parent_department = _("All Departments") + if not self.parent_department: + root = get_root_of("Department") + if root: + self.parent_department = root def update_nsm_model(self): frappe.utils.nestedset.update_nsm(self) @@ -40,7 +41,7 @@ def on_doctype_update(): def get_children(doctype, parent=None, company=None, is_root=False): condition = '' if company == parent: - condition = 'name="All Departments"' + condition = "name='%s'".format(get_root_of("Department")) elif company: condition = "parent_department='{0}' and company='{1}'".format(parent, company) else: diff --git a/erpnext/patches/v11_0/create_department_records_for_each_company.py b/erpnext/patches/v11_0/create_department_records_for_each_company.py index 4210c018cc..a5b76e96c5 100644 --- a/erpnext/patches/v11_0/create_department_records_for_each_company.py +++ b/erpnext/patches/v11_0/create_department_records_for_each_company.py @@ -1,4 +1,5 @@ import frappe +from frappe import _ from frappe.utils.nestedset import rebuild_tree def execute(): @@ -15,7 +16,7 @@ def execute(): for department in departments: # skip root node - if department.name == "All Departments": + if department.name == _("All Departments"): continue # for each company, create a copy of the doc diff --git a/erpnext/patches/v11_0/update_department_lft_rgt.py b/erpnext/patches/v11_0/update_department_lft_rgt.py index 9bb1772115..533055a5d7 100644 --- a/erpnext/patches/v11_0/update_department_lft_rgt.py +++ b/erpnext/patches/v11_0/update_department_lft_rgt.py @@ -7,11 +7,13 @@ def execute(): frappe.reload_doc("hr", "doctype", "department") if not frappe.db.exists("Department", _('All Departments')): - frappe.get_doc({ + dept = frappe.get_doc({ 'doctype': 'Department', 'department_name': _('All Departments'), 'is_group': 1 - }).insert(ignore_permissions=True) + }) + dept.flags.ignore_validate=True + dept.insert(ignore_permissions=True) frappe.db.sql("""update `tabDepartment` set parent_department = '{0}' where is_group = 0""".format(_('All Departments')))