2021-06-20 07:12:06 +00:00
|
|
|
import frappe
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-06-20 07:12:06 +00:00
|
|
|
@frappe.whitelist()
|
2021-07-08 11:35:40 +00:00
|
|
|
def get_children(parent=None, company=None, exclude_node=None):
|
2021-06-20 07:12:06 +00:00
|
|
|
filters = [['status', '!=', 'Left']]
|
|
|
|
if company and company != 'All Companies':
|
|
|
|
filters.append(['company', '=', company])
|
|
|
|
|
2021-07-07 06:35:50 +00:00
|
|
|
if parent and company and parent != company:
|
2021-06-20 07:12:06 +00:00
|
|
|
filters.append(['reports_to', '=', parent])
|
|
|
|
else:
|
|
|
|
filters.append(['reports_to', '=', ''])
|
|
|
|
|
2021-07-08 11:35:40 +00:00
|
|
|
if exclude_node:
|
|
|
|
filters.append(['name', '!=', exclude_node])
|
|
|
|
|
2021-07-08 04:23:31 +00:00
|
|
|
employees = frappe.get_list('Employee',
|
|
|
|
fields=['employee_name as name', 'name as id', 'reports_to', 'image', 'designation as title'],
|
|
|
|
filters=filters,
|
|
|
|
order_by='name'
|
|
|
|
)
|
2021-06-20 07:12:06 +00:00
|
|
|
|
|
|
|
for employee in employees:
|
2021-07-08 04:23:31 +00:00
|
|
|
is_expandable = frappe.db.count('Employee', filters={'reports_to': employee.get('id')})
|
2021-06-29 15:56:47 +00:00
|
|
|
employee.connections = get_connections(employee.id)
|
2021-06-20 07:12:06 +00:00
|
|
|
employee.expandable = 1 if is_expandable else 0
|
|
|
|
|
|
|
|
return employees
|
|
|
|
|
|
|
|
|
|
|
|
def get_connections(employee):
|
|
|
|
num_connections = 0
|
|
|
|
|
2021-08-16 04:49:48 +00:00
|
|
|
nodes_to_expand = frappe.get_list('Employee', filters=[
|
2021-06-20 07:12:06 +00:00
|
|
|
['reports_to', '=', employee]
|
|
|
|
])
|
2021-08-16 04:49:48 +00:00
|
|
|
num_connections += len(nodes_to_expand)
|
|
|
|
|
|
|
|
while nodes_to_expand:
|
|
|
|
parent = nodes_to_expand.pop(0)
|
|
|
|
descendants = frappe.get_list('Employee', filters=[
|
|
|
|
['reports_to', '=', parent.name]
|
|
|
|
])
|
|
|
|
num_connections += len(descendants)
|
|
|
|
nodes_to_expand.extend(descendants)
|
2021-06-20 07:12:06 +00:00
|
|
|
|
|
|
|
return num_connections
|