brotherton-erpnext/erpnext/hr/report/employee_analytics/employee_analytics.py

85 lines
2.5 KiB
Python
Raw Normal View History

# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe
from frappe import _
def execute(filters=None):
if not filters: filters = {}
if not filters["company"]:
frappe.throw(_('{0} is mandatory').format(_('Company')))
columns = get_columns()
employees = get_employees(filters)
2020-05-13 05:17:36 +00:00
parameters_result = get_parameters(filters)
parameters = []
if parameters_result:
for department in parameters_result:
parameters.append(department)
chart = get_chart_data(parameters,employees, filters)
return columns, employees, None, chart
def get_columns():
return [
_("Employee") + ":Link/Employee:120", _("Name") + ":Data:200", _("Date of Birth")+ ":Date:100",
_("Branch") + ":Link/Branch:120", _("Department") + ":Link/Department:120",
_("Designation") + ":Link/Designation:120", _("Gender") + "::100", _("Company") + ":Link/Company:120"
]
def get_conditions(filters):
2020-05-13 05:17:36 +00:00
conditions = " and "+filters.get("parameter").lower().replace(" ","_")+" IS NOT NULL "
if filters.get("company"): conditions += " and company = '%s'" % \
filters["company"].replace("'", "\\'")
return conditions
def get_employees(filters):
conditions = get_conditions(filters)
return frappe.db.sql("""select name, employee_name, date_of_birth,
branch, department, designation,
gender, company from `tabEmployee` where status = 'Active' %s""" % conditions, as_list=1)
2020-05-13 05:17:36 +00:00
def get_parameters(filters):
if filters.get("parameter") == "Grade":
parameter = "Employee Grade"
else:
parameter = filters.get("parameter")
return frappe.db.sql("""select name from `tab"""+ parameter +"""` """, as_list=1)
2020-05-13 05:17:36 +00:00
def get_chart_data(parameters,employees, filters):
if not parameters:
parameters = []
datasets = []
2020-05-13 05:17:36 +00:00
parameter_field_name = filters.get("parameter").lower().replace(" ","_")
label = []
for parameter in parameters:
if parameter:
total_employee = frappe.db.sql("""select count(*) from
`tabEmployee` where """+
parameter_field_name + """ = %s and company = %s""" ,( parameter[0], filters.get("company")), as_list=1)
if total_employee[0][0]:
label.append(parameter)
datasets.append(total_employee[0][0])
2020-05-13 05:17:36 +00:00
values = [ value for value in datasets if value !=0]
total_employee = frappe.db.count('Employee', {'status':'Active'})
others = total_employee - sum(values)
2020-05-13 16:17:52 +00:00
label.append(["Not Set"])
2020-05-13 05:17:36 +00:00
values.append(others)
chart = {
"data": {
2020-05-13 05:17:36 +00:00
'labels': label,
'datasets': [{'name': 'Employees','values': values}]
}
}
2020-05-13 05:17:36 +00:00
chart["type"] = "donut"
return chart