Skip to content

Commit

Permalink
Merge pull request #977 from niraj2477/gh-975
Browse files Browse the repository at this point in the history
refactor(Report): Employee working on a holiday
  • Loading branch information
krantheman authored Oct 14, 2024
2 parents 76a0d1a + 6892449 commit 7f6d346
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,19 @@ frappe.query_reports["Employees working on a holiday"] = {
fieldtype: "Link",
options: "Holiday List",
},
{
fieldname: "department",
label: __("Department"),
fieldtype: "Link",
options: "Department",
},
{
fieldname: "company",
label: __("Company"),
fieldtype: "Link",
options: "Company",
reqd: 1,
default: frappe.defaults.get_user_default("Company"),
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,86 @@
import frappe
from frappe import _

from erpnext.setup.doctype.employee.employee import get_holiday_list_for_employee


def execute(filters=None):
if not filters:
filters = {}

columns = get_columns()
data = get_employees(filters)
data = get_data(filters)
return columns, data


def get_columns():
return [
_("Employee") + ":Link/Employee:120",
_("Name") + ":Data:200",
_("Date") + ":Date:100",
_("Status") + ":Data:70",
_("Holiday") + ":Data:200",
{
"label": _("Employee"),
"fieldtype": "Link",
"fieldname": "employee",
"options": "Employee",
"width": 300,
},
{
"label": _("Employee Name"),
"fieldtype": "Data",
"width": 0,
"hidden": 1,
},
{
"label": _("Date"),
"fieldtype": "Date",
"width": 120,
},
{
"label": _("Status"),
"fieldtype": "Data",
"width": 100,
},
{
"label": _("Holiday"),
"fieldtype": "Data",
"width": 200,
},
]


def get_employees(filters):
holiday_filter = [
["holiday_date", ">=", filters.from_date],
["holiday_date", "<=", filters.to_date],
]
if filters.holiday_list:
holiday_filter.append(["parent", "=", filters.holiday_list])

holidays = frappe.get_all("Holiday", fields=["holiday_date", "description"], filters=holiday_filter)
def get_data(filters):
Attendance = frappe.qb.DocType("Attendance")
Holiday = frappe.qb.DocType("Holiday")

holiday_names = {}
holidays_list = []
data = []

for holiday in holidays:
holidays_list.append(holiday.holiday_date)
holiday_names[holiday.holiday_date] = holiday.description
employee_filters = {"company": filters.company}
if filters.department:
employee_filters["department"] = filters.department

if holidays_list:
cond = " attendance_date in %(holidays_list)s and status not in ('On Leave','Absent') "
for employee in frappe.get_list("Employee", filters=employee_filters, pluck="name"):
holiday_list = get_holiday_list_for_employee(employee, raise_exception=False)
if not holiday_list or (filters.holiday_list and filters.holiday_list != holiday_list):
continue

if filters.holiday_list:
cond += (
""" and (employee in (select employee from tabEmployee where holiday_list = %(holidays)s))"""
working_days = (
frappe.qb.from_(Attendance)
.inner_join(Holiday)
.on(Attendance.attendance_date == Holiday.holiday_date)
.select(
Attendance.employee,
Attendance.employee_name,
Attendance.attendance_date,
Attendance.status,
Holiday.description,
)

employee_list = frappe.db.sql(
"""select
employee, employee_name, attendance_date, status
from tabAttendance
where %s"""
% cond.format(", ".join(["%s"] * len(holidays_list))),
{"holidays_list": holidays_list, "holidays": filters.holiday_list},
as_list=True,
.where(
(Attendance.employee == employee)
& (Attendance.attendance_date[filters.from_date : filters.to_date])
& (Attendance.status.notin(["Absent", "On Leave"]))
& (Attendance.docstatus == 1)
& (Holiday.parent == holiday_list)
)
.run(as_list=True)
)
data.extend(working_days)

for employee_data in employee_list:
employee_data.append(holiday_names[employee_data[2]])

return employee_list
else:
return []
return data

0 comments on commit 7f6d346

Please sign in to comment.