Currently, I'm building an app (API) for employee management with Laravel! I will try and explain the process step by step so you can understand what I want much easier!
When you create/register an Employee you have to assign how many hours will work during the week.
Example:
Id: 100
Name: Joe Doe
Working hours: 40 hours per week (Monday - Tuesday 9 hours per day, and Friday 4 hours)
After the employee is created he can start writing the hours he has worked daily
The database table is called "hours"
Example:
user_id | date | start | end |
---|---|---|---|
100 | 17.10.22 | 09:00 | 18:00 |
100 | 18.10.22 | 09:00 | 18:00 |
100 | 20.10.22 | 09:00 | 18:00 |
100 | 21.10.22 | 09:00 | 14:00 |
100 | 24.10.22 | 09:00 | 20:00 |
I have this function function raportEmployee($employee_id, $date_start, $date_end)
and I call this with these parameters
$employee_id = 100;
$date_start = 17.10.22
$date_end = 24.10.22
This has to return each day of the week even those where the employee has not put any data and show if he is working less or more each day
The result I want (this will return JSON):
Employee | date | start | end | total hours |
---|---|---|---|---|
Joe Doe | 17.10.22 | 09:00 | 18:00 | 0 |
Joe Doe | 18.10.22 | 09:00 | 18:00 | 0 |
Joe Doe | 19.10.22 | null | null | -9 |
Joe Doe | 20.10.22 | 09:00 | 18:00 | -9 |
Joe Doe | 21.10.22 | 09:00 | 14:00 | -9 |
Joe Doe | 22.10.22 | null | null | -9 |
Joe Doe | 23.10.22 | null | null | -9 |
Joe Doe | 24.10.22 | 09:00 | 21:00 | -6 |
So 19.10 the employee is -9 hours because has not written any hours on this date. Then Saturday (22.10) and Sunday(23.10) even though he has not written any hours, the total hours are still -9 because based on "Working hours" is "obligated" to work only during weekdays. Then on Monday (24.10) has worked from 09:00 - 21:00 (12hr; 3hr more than planned) the total hours now is -6.
How can I achieve this I have some ideas but are not very well in terms of speed and optimization because imagine where we want a range from 01-01-2022 to 31-12-2022, each day will be displayed and has to constantly calculate "total hours" (if is in positive, negative or as planned)
Sorry for this long post but I thought this was the best way to explain the problem. If you have any questions please let me know.
Thank you!