2018-05-26 03:56:03 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-05-26 03:56:03 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-05-26 03:56:03 +00:00
|
|
|
def execute(filters=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
if not filters:
|
|
|
|
filters = {}
|
2019-11-06 09:11:16 +00:00
|
|
|
|
|
|
|
if not filters["company"]:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(_("{0} is mandatory").format(_("Company")))
|
2019-11-06 09:11:16 +00:00
|
|
|
|
2018-05-26 03:56:03 +00:00
|
|
|
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)
|
2018-05-26 03:56:03 +00:00
|
|
|
return columns, employees, None, chart
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-05-26 03:56:03 +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",
|
2018-05-26 03:56:03 +00:00
|
|
|
]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-05-26 03:56:03 +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("'", "\\'")
|
2018-05-26 03:56:03 +00:00
|
|
|
return conditions
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-05-26 03:56:03 +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,
|
2018-05-26 03:56:03 +00:00
|
|
|
branch, department, designation,
|
2022-03-28 13:22:46 +00:00
|
|
|
gender, company from `tabEmployee` where status = 'Active' %s"""
|
|
|
|
% conditions,
|
|
|
|
as_list=1,
|
|
|
|
)
|
|
|
|
|
2018-05-26 03:56:03 +00:00
|
|
|
|
2020-05-13 05:17:36 +00:00
|
|
|
def get_parameters(filters):
|
2020-05-27 06:56:54 +00:00
|
|
|
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 = []
|
2018-05-26 03:56:03 +00:00
|
|
|
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)
|
2018-05-26 03:56:03 +00:00
|
|
|
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"
|
2018-05-26 03:56:03 +00:00
|
|
|
return chart
|