Fixes related to department tree

This commit is contained in:
Nabin Hait 2018-05-29 13:08:40 +05:30
parent d3a8c9308b
commit 3aa205f19d
3 changed files with 13 additions and 9 deletions

View File

@ -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:

View File

@ -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

View File

@ -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')))