Healthcare Service Unit - Tree View Updated
diff --git a/erpnext/healthcare/utils.py b/erpnext/healthcare/utils.py
index b6b7330..3372e1d 100644
--- a/erpnext/healthcare/utils.py
+++ b/erpnext/healthcare/utils.py
@@ -350,3 +350,52 @@
 						item_to_invoice.append({'drug_code': drug_line.drug_code, 'quantity': qty,
 						'description': drug_line.dosage+" for "+drug_line.period})
 				return item_to_invoice
+
+@frappe.whitelist()
+def get_children(doctype, parent, company, is_root=False):
+	parent_fieldname = 'parent_' + doctype.lower().replace(' ', '_')
+	fields = [
+		'name as value',
+		'is_group as expandable',
+		'lft',
+		'rgt'
+	]
+	# fields = [ 'name', 'is_group', 'lft', 'rgt' ]
+	filters = [['ifnull(`{0}`,"")'.format(parent_fieldname), '=', '' if is_root else parent]]
+
+	if is_root:
+		fields += ['service_unit_type'] if doctype == 'Healthcare Service Unit' else []
+		filters.append(['company', '=', company])
+
+	else:
+		fields += ['service_unit_type', 'allow_appointments', 'inpatient_occupancy', 'occupancy_status'] if doctype == 'Healthcare Service Unit' else []
+		fields += [parent_fieldname + ' as parent']
+
+	hc_service_units = frappe.get_list(doctype, fields=fields, filters=filters)
+
+	if doctype == 'Healthcare Service Unit':
+		for each in hc_service_units:
+			occupancy_msg = ""
+			if each['expandable'] == 1:
+				occupied = False
+				vacant = False
+				child_list = frappe.db.sql("""
+					select name, occupancy_status from `tabHealthcare Service Unit`
+					where inpatient_occupancy = 1 and
+					lft > %s and rgt < %s""",
+					(each['lft'], each['rgt']))
+				for child in child_list:
+					print child[0], child[1]
+					if not occupied:
+						occupied = 0
+					if child[1] == "Occupied":
+						occupied += 1
+					if not vacant:
+						vacant = 0
+					if child[1] == "Vacant":
+						vacant += 1
+				if vacant and occupied:
+					occupancy_total = vacant+occupied
+					occupancy_msg = str(occupied) + " Occupied out of " + str(occupancy_total)
+			each["occupied_out_of_vacant"] = occupancy_msg
+	return hc_service_units