Skip to content

Commit

Permalink
Merge pull request #3920 from ONE-F-M/188754874
Browse files Browse the repository at this point in the history
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
ks093 authored Jan 13, 2025
2 parents a8639e7 + 0830752 commit 78ced52
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions one_fm/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ one_fm.patches.v15_0.update_shift_request_day_off
one_fm.patches.v15_0.update_job_offer_00057_erf
one_fm.patches.v15_0.update_purchase_order_according_to_new_workflow
one_fm.patches.v15_0.update_erf_for_job_offer
one_fm.patches.v15_0.update_employees_operation_role

49 changes: 49 additions & 0 deletions one_fm/patches/v15_0/update_employees_operation_role.py
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

0 comments on commit 78ced52

Please sign in to comment.