Skip to content

Commit

Permalink
refactor: use get_holiday_list_for_employee for holidays
Browse files Browse the repository at this point in the history
  • Loading branch information
krantheman committed Jun 28, 2024
1 parent b7013b0 commit b73956e
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions hrms/api/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from frappe import _
from frappe.utils import add_days, date_diff

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

from hrms.hr.doctype.shift_assignment.shift_assignment import ShiftAssignment
from hrms.hr.doctype.shift_assignment_tool.shift_assignment_tool import create_shift_assignment

Expand All @@ -21,7 +23,6 @@ def get_events(

events = {}
for event in [holidays, leaves, shifts]:
event = group_by_employee(event)
for key, value in event.items():
if key in events:
events[key].extend(value)
Expand Down Expand Up @@ -159,30 +160,17 @@ def insert_shift(


def get_holidays(month_start: str, month_end: str, employee_filters: dict[str, str]) -> dict[str, list[dict]]:
Employee = frappe.qb.DocType("Employee")
HolidayList = frappe.qb.DocType("Holiday List")
Holiday = frappe.qb.DocType("Holiday")
holidays = {}

query = (
frappe.qb.select(
Employee.employee,
Holiday.name.as_("holiday"),
Holiday.holiday_date,
Holiday.description,
Holiday.weekly_off,
)
.from_(Employee)
.join(HolidayList)
.on(Employee.holiday_list == HolidayList.name)
.join(Holiday)
.on(Holiday.parent == HolidayList.name)
.where(Holiday.holiday_date[month_start:month_end])
)

for filter in employee_filters:
query = query.where(Employee[filter] == employee_filters[filter])
for employee in frappe.get_list("Employee", filters=employee_filters, pluck="name"):
if holiday_list := get_holiday_list_for_employee(employee, raise_exception=False):
holidays[employee] = frappe.get_all(
"Holiday",
filters={"parent": holiday_list, "holiday_date": ["between", [month_start, month_end]]},
fields=["name as holiday", "holiday_date", "description", "weekly_off"],
)

return query.run(as_dict=True)
return holidays


def get_leaves(month_start: str, month_end: str, employee_filters: dict[str, str]) -> dict[str, list[dict]]:
Expand Down Expand Up @@ -211,7 +199,7 @@ def get_leaves(month_start: str, month_end: str, employee_filters: dict[str, str
for filter in employee_filters:
query = query.where(Employee[filter] == employee_filters[filter])

return query.run(as_dict=True)
return group_by_employee(query.run(as_dict=True))


def get_shifts(
Expand Down Expand Up @@ -251,7 +239,7 @@ def get_shifts(
for filter in shift_filters:
query = query.where(ShiftAssignment[filter] == shift_filters[filter])

return query.run(as_dict=True)
return group_by_employee(query.run(as_dict=True))


def group_by_employee(events: list[dict]) -> dict[str, list[dict]]:
Expand Down

0 comments on commit b73956e

Please sign in to comment.