-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3920 from ONE-F-M/188754874
Feat : I want to set the last basic duty as Employee's default Operations role so that it will not be manually set in the Employee record
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import frappe | ||
|
||
@frappe.whitelist() | ||
def execute(): | ||
# Fetch all employees and their schedules in a single query | ||
query = """ | ||
SELECT e.name AS employee, e.shift AS current_shift, es.shift AS schedule_shift, es.operations_role | ||
FROM `tabEmployee` AS e | ||
LEFT JOIN ( | ||
SELECT es.employee, es.shift, es.operations_role | ||
FROM `tabEmployee Schedule` AS es | ||
WHERE es.roster_type = 'Basic' | ||
AND es.employee_availability = 'Working' | ||
AND es.date = ( | ||
SELECT MAX(date) | ||
FROM `tabEmployee Schedule` AS sub | ||
WHERE sub.employee = es.employee | ||
) | ||
) AS es | ||
ON e.name = es.employee | ||
WHERE e.shift_working = 1 AND e.status = 'Active'; | ||
""" | ||
results = frappe.db.sql(query, as_dict=True) | ||
|
||
# Prepare bulk update data | ||
updates = [] | ||
for schedule in results: | ||
if schedule["schedule_shift"] and schedule["current_shift"] == schedule["schedule_shift"]: | ||
updates.append((schedule["operations_role"], schedule["employee"])) | ||
|
||
# Execute bulk update if there are matching records | ||
if updates: | ||
update_query = """ | ||
UPDATE `tabEmployee` | ||
SET custom_operations_role_allocation = CASE name | ||
""" | ||
update_cases = [] | ||
employee_ids = [] | ||
for operations_role, employee_name in updates: | ||
update_cases.append(f"WHEN '{employee_name}' THEN '{operations_role}'") | ||
employee_ids.append(f"'{employee_name}'") | ||
|
||
# Combine the query | ||
update_query += " ".join(update_cases) + " END WHERE name IN (" + ", ".join(employee_ids) + ")" | ||
|
||
# Execute the query | ||
frappe.db.sql(update_query) | ||
frappe.db.commit() | ||
return results |