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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 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
import frappe
from frappe import _
def execute(filters=None):
2022-03-28 13:22:46 +00:00
if not filters:
filters = {}
if not filters["company"]:
2022-03-28 13:22:46 +00:00
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)
2022-03-28 13:22:46 +00:00
chart = get_chart_data(parameters, employees, filters)
return columns, employees, None, chart
2022-03-28 13:22:46 +00:00
def get_columns():
return [
2022-03-28 13:22:46 +00:00
_("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",
]
2022-03-28 13:22:46 +00:00
def get_conditions(filters):
2022-03-28 13:22:46 +00:00
conditions = " and " + filters.get("parameter").lower().replace(" ", "_") + " IS NOT NULL "
2020-05-13 05:17:36 +00:00
2022-03-28 13:22:46 +00:00
if filters.get("company"):
conditions += " and company = '%s'" % filters["company"].replace("'", "\\'")
return conditions
2022-03-28 13:22:46 +00:00
def get_employees(filters):
conditions = get_conditions(filters)
2022-03-28 13:22:46 +00:00
return frappe.db.sql(
"""select name, employee_name, date_of_birth,
branch, department, designation,
2022-03-28 13:22:46 +00:00
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")
2022-03-28 13:22:46 +00:00
return frappe.db.sql("""select name from `tab""" + parameter + """` """, as_list=1)
2020-05-13 05:17:36 +00:00
2022-03-28 13:22:46 +00:00
def get_chart_data(parameters, employees, filters):
2020-05-13 05:17:36 +00:00
if not parameters:
parameters = []
datasets = []
2022-03-28 13:22:46 +00:00
parameter_field_name = filters.get("parameter").lower().replace(" ", "_")
2020-05-13 05:17:36 +00:00
label = []
for parameter in parameters:
if parameter:
2022-03-28 13:22:46 +00:00
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,
)
2020-05-13 05:17:36 +00:00
if total_employee[0][0]:
label.append(parameter)
datasets.append(total_employee[0][0])
2020-05-13 05:17:36 +00:00
2022-03-28 13:22:46 +00:00
values = [value for value in datasets if value != 0]
2020-05-13 05:17:36 +00:00
2022-03-28 13:22:46 +00:00
total_employee = frappe.db.count("Employee", {"status": "Active"})
2020-05-13 05:17:36 +00:00
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)
2022-03-28 13:22:46 +00:00
chart = {"data": {"labels": label, "datasets": [{"name": "Employees", "values": values}]}}
2020-05-13 05:17:36 +00:00
chart["type"] = "donut"
return chart