create and update department records patch

This commit is contained in:
Zlash65 2018-05-13 14:34:20 +05:30
parent 03ad0f2f4a
commit d25a264e5b
2 changed files with 52 additions and 0 deletions

View File

@ -527,3 +527,4 @@ erpnext.patches.v11_0.rename_field_max_days_allowed
erpnext.patches.v11_0.create_salary_structure_assignments
erpnext.patches.v11_0.rename_health_insurance
erpnext.patches.v11_0.rebuild_tree_for_company
erpnext.patches.v11_0.create_department_records_for_each_company

View File

@ -0,0 +1,51 @@
import frappe
from frappe.utils.nestedset import rebuild_tree
def execute():
frappe.reload_doc("hr", "doctype", "department")
companies = frappe.db.get_all("Company", fields=["name", "abbr"])
departments = frappe.db.get_all("Department")
comp_dict = {}
# create a blank list for each company
for company in companies:
comp_dict[company.name] = {}
for department in departments:
# skip root node
if department.name == "All Departments":
continue
# for each company, create a copy of the doc
department_doc = frappe.get_doc("Department", department)
for company in companies:
copy_doc = frappe.copy_doc(department_doc)
copy_doc.update({"company": company.name})
copy_doc.insert()
# append list of new department for each company
comp_dict[company.name][department.name] = copy_doc.name
rebuild_tree('Department', 'parent_department')
doctypes = ["Asset", "Employee", "Leave Period", "Payroll Entry", "Staffing Plan", "Job Opening"]
for d in doctypes:
update_records(d, comp_dict)
def update_records(doctype, comp_dict):
when_then = []
for company in comp_dict:
records = comp_dict[company]
for department in records:
when_then.append('''
WHEN company = "%s" and department = "%s"
THEN "%s"
'''%(company, department, records[department]))
frappe.db.sql("""
update
`tab%s`
set
department = CASE %s END
"""%(doctype, " ".join(when_then)), debug=1)