Skip to content

Commit

Permalink
Merge pull request #3896 from ONE-F-M/188702800
Browse files Browse the repository at this point in the history
feat: Default Shift Checker
  • Loading branch information
ibtesam-onefm authored Dec 24, 2024
2 parents 390e5cb + 78a2082 commit 622883d
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 1 deletion.
3 changes: 2 additions & 1 deletion one_fm/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@
"10 4 * * *": [ #“At 04:10.”
'one_fm.utils.check_grp_operator_submission_four',
'one_fm.operations.doctype.post_scheduler_checker.post_scheduler_checker.schedule_roster_checker',
'one_fm.operations.doctype.roster_day_off_checker.roster_day_off_checker.generate_checker'
'one_fm.operations.doctype.roster_day_off_checker.roster_day_off_checker.generate_checker',
'one_fm.operations.doctype.default_shift_checker.default_shift_checker.create_default_shift_checker'
],
"30 4 * * *": [
'one_fm.utils.check_grp_operator_submission_four_half'
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2024, omar jaber and contributors
// For license information, please see license.txt

// frappe.ui.form.on("Default Shift Checker", {
// refresh(frm) {

// },
// });
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"actions": [],
"autoname": "format: {start_date} | {end_date} | Check Default Shift | {operations_shift}",
"creation": "2024-12-20 10:44:56.021618",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": [
"start_date",
"operations_shift",
"column_break_fxzs",
"end_date",
"actions_section",
"status",
"shift_supervisor",
"shift_supervisor_name",
"column_break_mejq",
"action_type",
"site_supervisor",
"site_supervisor_name",
"site_supervisor_user",
"section_break_yzvl",
"employees_assigned_outside_default_shift"
],
"fields": [
{
"fieldname": "start_date",
"fieldtype": "Date",
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Start Date",
"reqd": 1
},
{
"fieldname": "column_break_fxzs",
"fieldtype": "Column Break"
},
{
"fieldname": "end_date",
"fieldtype": "Date",
"in_list_view": 1,
"label": "End Date",
"reqd": 1
},
{
"fieldname": "actions_section",
"fieldtype": "Section Break",
"label": "Actions"
},
{
"default": "Pending",
"fieldname": "status",
"fieldtype": "Select",
"in_standard_filter": 1,
"label": "Status",
"options": "Pending\nCompleted\nOverdue\nCancelled"
},
{
"fieldname": "shift_supervisor",
"fieldtype": "Link",
"label": "Shift Supervisor",
"options": "Employee"
},
{
"fetch_from": "shift_supervisor.employee_name",
"fieldname": "shift_supervisor_name",
"fieldtype": "Data",
"label": "Shift Supervisor Name"
},
{
"fieldname": "column_break_mejq",
"fieldtype": "Column Break"
},
{
"default": "No Changes Made",
"fieldname": "action_type",
"fieldtype": "Select",
"in_standard_filter": 1,
"label": "Action Type",
"options": "No Changes Made\nShift Allocation Update\nMarked Employees as Relievers\nMixed Actions"
},
{
"fieldname": "site_supervisor",
"fieldtype": "Link",
"label": "Site Supervisor",
"options": "Employee"
},
{
"fetch_from": "site_supervisor.employee_name",
"fetch_if_empty": 1,
"fieldname": "site_supervisor_name",
"fieldtype": "Data",
"label": "Site Supervisor Name"
},
{
"fieldname": "section_break_yzvl",
"fieldtype": "Section Break"
},
{
"fieldname": "employees_assigned_outside_default_shift",
"fieldtype": "Table",
"label": "Employees Assigned Outside Default Shift",
"options": "Default Shift Checker Employee"
},
{
"fieldname": "operations_shift",
"fieldtype": "Link",
"in_standard_filter": 1,
"label": "Operations Shift",
"options": "Operations Shift"
},
{
"fetch_from": "site_supervisor.user_id",
"fetch_if_empty": 1,
"fieldname": "site_supervisor_user",
"fieldtype": "Link",
"hidden": 1,
"label": "Site Supervisor User",
"options": "User"
}
],
"in_create": 1,
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-12-24 09:49:23.206286",
"modified_by": "Administrator",
"module": "Operations",
"name": "Default Shift Checker",
"naming_rule": "Expression",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
},
{
"create": 1,
"read": 1,
"report": 1,
"role": "Site Supervisor",
"share": 1,
"write": 1
},
{
"create": 1,
"read": 1,
"report": 1,
"role": "Shift Supervisor",
"share": 1,
"write": 1
}
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [
{
"color": "Orange",
"title": "Pending"
},
{
"color": "Pink",
"title": "Overdue"
},
{
"color": "Green",
"title": "Completed"
},
{
"color": "Red",
"title": "Cancelled"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright (c) 2024, omar jaber and contributors
# For license information, please see license.txt

from collections import defaultdict

import frappe
from frappe.model.document import Document
from frappe import _
from frappe.query_builder import DocType
from frappe.query_builder.functions import Count
from frappe.utils import getdate, get_last_day
from one_fm.operations.doctype.operations_shift.operations_shift import get_shift_supervisor

class DefaultShiftChecker(Document):
pass


def create_default_shift_checker():
start_date = getdate()
last_day_of_month = get_last_day(start_date)

threshold = frappe.db.get_single_value("ONEFM General Setting", "default_shift_checker_threshold")

Employee = DocType("Employee")
EmployeeSchedule = DocType("Employee Schedule")

query = (
frappe.qb.from_(Employee)
.join(EmployeeSchedule)
.on(Employee.name == EmployeeSchedule.employee)
.select(
Employee.name,
Employee.employee_name,
EmployeeSchedule.shift.as_("scheduled_shift"),
Employee.shift.as_("default_shift"),
EmployeeSchedule.site,
Count(EmployeeSchedule.shift).as_("operations_shift_count"),
)
.where(
(Employee.shift_working == 1)
& (Employee.shift.isnotnull())
& (Employee.shift != EmployeeSchedule.shift)
& (EmployeeSchedule.roster_type == "Basic")
& (EmployeeSchedule.date[start_date:last_day_of_month])
)
.groupby(Employee.name, EmployeeSchedule.shift)
.having(Count(EmployeeSchedule.shift) > threshold)
)

result = query.run(as_dict=1)

grouped_data = defaultdict(list)
for item in result:
grouped_data[(item["scheduled_shift"], item["site"])].append(item)

for key, employees in grouped_data.items():
shift, site = key
try:
default_shift_checker = frappe.new_doc("Default Shift Checker")
default_shift_checker.start_date = start_date
default_shift_checker.end_date = last_day_of_month
default_shift_checker.operations_shift = shift

site_supervisor = frappe.db.get_value("Operations Site", site, "account_supervisor")
shift_supervisor = get_shift_supervisor(shift)
default_shift_checker.site_supervisor = site_supervisor
default_shift_checker.shift_supervisor = shift_supervisor

for employee in employees:
default_shift_checker.append("employees_assigned_outside_default_shift", {
"employee": employee.name,
"employee_name": employee.employee_name,
"count": employee.operations_shift_count
})
default_shift_checker.save()
except Exception as e:
frappe.log_error("Default Shift Checker", frappe.get_traceback())
continue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024, omar jaber and Contributors
# See license.txt

# import frappe
from frappe.tests.utils import FrappeTestCase


class TestDefaultShiftChecker(FrappeTestCase):
pass
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"actions": [],
"allow_rename": 1,
"creation": "2024-12-20 10:58:49.275676",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"employee",
"column_break_sulr",
"employee_name",
"column_break_pbsg",
"count"
],
"fields": [
{
"fieldname": "employee",
"fieldtype": "Link",
"in_list_view": 1,
"in_preview": 1,
"label": "Employee",
"options": "Employee"
},
{
"fieldname": "column_break_sulr",
"fieldtype": "Column Break"
},
{
"fetch_from": "employee.employee_name",
"fieldname": "employee_name",
"fieldtype": "Data",
"in_list_view": 1,
"in_preview": 1,
"label": "Employee Name"
},
{
"fieldname": "column_break_pbsg",
"fieldtype": "Column Break"
},
{
"fieldname": "count",
"fieldtype": "Int",
"in_list_view": 1,
"in_preview": 1,
"label": "Count"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-12-20 11:00:07.869155",
"modified_by": "Administrator",
"module": "Operations",
"name": "Default Shift Checker Employee",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC",
"states": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024, omar jaber and contributors
# For license information, please see license.txt

# import frappe
from frappe.model.document import Document


class DefaultShiftCheckerEmployee(Document):
pass

0 comments on commit 622883d

Please sign in to comment.