Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Scheduling Multiple shifts and Auto Attendance #29955

Merged
merged 34 commits into from
Apr 6, 2022

Conversation

ruchamahabal
Copy link
Member

@ruchamahabal ruchamahabal commented Feb 23, 2022

Problem

  • Earlier, only 1 Shift Type could be assigned to an employee for one day.
  • However, in some organizations, employees could be working in multiple shifts (Morning Shift, Evening Shift) on the same day.

What Changes?

Schedule multiple shifts

  • Users can make multiple non-overlapping Shift Assignments for the same employee on the same day

Shift Request

  • Users can make multiple non-overlapping Shift Requests for the same employee on the same day

Fetching shift in Employee Check In

  • When employee check-ins are logged via biometric or manually, the exact shift based on the timestamp is fetched. Before, shifts used to be fetched based on date. Now they are timestamp specific.
  • Handles shifts spanning over 2 different days (eg: 23:00 - 2:00), midnight shifts.
  • Shift fetching used to fail on timing boundaries, this is fixed.
  • Handles 2 consecutive shifts if they are overlapping within grace periods.

Attendance

  • Earlier, only 1 attendance record could be marked for 1 day.
  • Now multiple attendance records can be marked for each shift. Eg: employee can be absent for one shift, present for another on the same day
  • Auto Attendance now, takes care of marking shift specific attendance and not for the whole day
  • Earlier, employees with no attendance records on a specific date used to be marked absent via Auto Attendance. Now, this also checks for shift-wise attendance records along with the date. So, only if attendance is not marked for a particular shift/for the whole day, the employee will be marked absent.

Attendance Reports

  • Added shift-wise attendance against employees
  • Added legend for status abbr with full form and color.
  • Formatter for data to quickly identify cells with lesser cognitive load.
  • Refactored Monthly Attendance Sheet code. Can still be better but will leave it for the next iteration.

Before:
image

After:
image

Impact on Payroll

If there are multiple attendance records marked for a single day with separate shifts, each record will be considered as 1 Payment Day in payroll. So if an employee works 2 shifts in a single day this contributes to 2 payment days in Payroll. No code level changes are done for this

Documentation: Added docs for Monthly Attendance Sheet only since others are mostly backend changes: https://docs.erpnext.com/docs/v14/user/manual/en/human-resources/human-resources-reports/edit?wiki_page_patch=ea798a28e2
Also closes #29210

ToDo

  • Allow scheduling multiple shifts
  • Shift fetching in employee check-ins
  • Multiple attendance records for the same day
  • Auto Attendance for multiple shifts
  • Handle impact on attendance reports
  • Refactor Shift Request Overlapping validation
  • Test cases
  • Documentation

@github-actions github-actions bot added the needs-tests This PR needs automated unit-tests. label Feb 23, 2022
base_dates_query = """select adddate(%(start_date)s, t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@ruchamahabal ruchamahabal changed the title feat: Ability to schedule multiple shifts and auto attendance for the same employee on the same day feat: Multiple shifts and Auto Attendance for the same employee on the same day Feb 23, 2022
@ruchamahabal ruchamahabal changed the title feat: Multiple shifts and Auto Attendance for the same employee on the same day feat: Scheduling Multiple shifts and Auto Attendance Feb 23, 2022
@naquiuddin
Copy link

naquiuddin commented Mar 5, 2022

Most companies use payment days as a measure in salary components to calculate payroll amounts. Current payment days calculation is very straightforward. If an attendance record is found, include that day in payment days. Now that there are multiple attendance records created for a single day.

  • This either needs conversion to working hours (will break backward compatibility? Users might already have payment_days in the component formulae)
  • or Normalization (to evaluate the payment day between 0 and 1 based on all the shifts) - very complex - can go wrong.
  • or something else?

Rather than converting calculation based on payment_days or normalisation, can we provide a Select Dropdown in salary component called Depends On and show these options - No dependency, Shift Payment Days, Total_Shift_Payment_Days, Total Working Hours, Total Working Minutes, Shift_Working_Hours, Shift_Working_Minutes ?

Using this setting the calculation method/function can be selected in Salary slip code as well.

Total Payment Days

  • In earning component this would be Total salary for all working days regardless of how many shifts a user works in a day
  • In deduction component this would be Total Salary that has to be deducted for all LWPs.
  • This component would be useful if employee has fixed salary amount per month, regardless of how many shifts they work

Shift Payment Days

  • In earning component is similar to Payment Days, when employee works for only one shift. for eg: 9 to 5. However when more than one shift is there the salary would be sum of multiple shifts.
  • In Deduction component this will be salary deducted for LWPs, in multiple shifts, Sum of Salary of LWPs per shift.
  • This will be useful when different shifts have different salary amount.

Shift Working Hours or Minutes

  • In earning component it would be salary of working hours or minutes per shift.
  • In deduction component it would be salary of missed hours or minutes per shift
  • This will be useful when different shifts have different Hourly Rate or Minute Rate. Hourly/Minute Rate is not fixed on employee but rather depends on Shift.

Total Shift Working Hours or minutes

  • In earning component it would be Hourly Rate * (sum of all shift working hours or minutes)
  • In deduction component it would be Hourly Rate * (sum of all shift working hours or minutes)
  • This will be useful when all shifts have same Hourly rate or Hourly Rate is fixed on Employee.

P.S. Working Minutes is something we use in our organisation. Similar is used in middle east areas. Because based on my knowledge in Islamic Shariah employee salary has to be calculated based on minutes and not hours.

@naquiuddin
Copy link

naquiuddin commented Mar 5, 2022

Additionally we have to edit Leave Application Form. The From Date and To Date fields has to change to the type DateTime. so that we can calculate which shift hours user was on leave.
For example if user takes leave from 9 to 10 but his shift lies between 9 to 12 then in that case 9 to 10 has to be accounted as leave or break time during the shift.

- convert raw query to frappe.qb

- check for overlapping timings if dates overlap

- translation friendly error messages with link to overlapping doc
- break down into smaller functions

- make it work with multiple shifts

- this will mark employee as absent per shift, meaning employee can be present for one shift and absent for another on the same day
- split into smaller functions

- add type hints

- get rid of unnecessary db calls and loops

- add docstrings for functions
- legend with colors and full form for status abbreviations
@ruchamahabal ruchamahabal force-pushed the multiple-shifts branch 2 times, most recently from a5d08d9 to 9956ac0 Compare March 29, 2022 11:12
@codecov
Copy link

codecov bot commented Mar 30, 2022

Codecov Report

Merging #29955 (0e15283) into develop (74e9bc6) will increase coverage by 0.24%.
The diff coverage is 94.97%.

@@             Coverage Diff             @@
##           develop   #29955      +/-   ##
===========================================
+ Coverage    60.91%   61.15%   +0.24%     
===========================================
  Files         1083     1083              
  Lines        69141    69306     +165     
===========================================
+ Hits         42119    42386     +267     
+ Misses       27022    26920     -102     
Impacted Files Coverage Δ
...xt/hr/doctype/shift_assignment/shift_assignment.py 76.21% <89.92%> (+26.83%) ⬆️
erpnext/hr/doctype/shift_type/shift_type.py 88.60% <93.54%> (+67.08%) ⬆️
...nthly_attendance_sheet/monthly_attendance_sheet.py 95.60% <96.26%> (+33.21%) ⬆️
erpnext/hr/doctype/attendance/attendance.py 71.52% <100.00%> (+9.13%) ⬆️
...xt/hr/doctype/employee_checkin/employee_checkin.py 93.63% <100.00%> (+8.58%) ⬆️
erpnext/hr/doctype/shift_request/shift_request.py 93.24% <100.00%> (+7.52%) ⬆️
...tch_item_expiry_status/batch_item_expiry_status.py 67.92% <0.00%> (-24.53%) ⬇️
...wise_balance_history/batch_wise_balance_history.py 67.79% <0.00%> (-23.73%) ⬇️
erpnext/stock/doctype/manufacturer/manufacturer.py 80.00% <0.00%> (-20.00%) ⬇️
erpnext/accounts/doctype/bank/bank.py 71.42% <0.00%> (-14.29%) ⬇️
... and 39 more

- check half day attendance threshold before absent threshold to avoid half day getting marked as absent

- round working hours to 2 digits for better accuracy

- start and end dates for absent attendance marking
- commonify time overlap function between request and assignment

- add tests for shift request overlap
- skip auto attendance in case of overlapping shift attendance record

- this case won't occur in case of shift assignment, since it will not allow overlapping shifts to be assigned

- can happen if manual attendance records are created
@ruchamahabal ruchamahabal marked this pull request as ready for review April 4, 2022 06:18
@ruchamahabal ruchamahabal removed the needs-tests This PR needs automated unit-tests. label Apr 4, 2022
@ruchamahabal
Copy link
Member Author

Unrelated test failures

@ruchamahabal ruchamahabal requested a review from nabinhait April 4, 2022 06:21
@ruchamahabal
Copy link
Member Author

ruchamahabal commented Apr 6, 2022

Reverted #30587 and re-merging PR #30588 without squashing since this has a high blast radius and needs a good commit history. 🙈

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error on creating a non - overlapping shift assignment for same day for same employee
4 participants