What determines lateness?
Jun 20, 2021
6
Level 9
Collection where date between
Guys. I have attendance system and I want to generate a report that show employees and if they are late in particular date.
- I get employees and eager load their attendances in that date.
$date = '2021-05-01';
$closure = fn ($query) => $query->whereDate('date', $date);
Employee::where('branch_id', 82)->whereHas('attendances', $closure)
->with(['attendances' => $closure])
->get()
->map(function ($employee) use ($date) {
$calc = new Calculator(
$employee->attendances ,
Carbon::create($date)
);
$employee['state'] = $calc->getResults();
return $employee;
});
- In calculator class I receive a set of attendances for that employee
// this just return a range to compare valid attendance
// each timesheet has start_time, end_time like from 09:00 to 17:00
// so the will return 2021-05-01 09:00:00 & 2021-05-01 17:00:00
[$firstRange, $secondRange] = $first->timesheet->ranges($this->date);
//after I get ranges I want to get all attendance in this range and ignore others because they're not valid
// it's hard to do it in query level because each employee has diff timesheet and each timesheet has diff start/end time
$attendances->filter(function ($item) {
// how to do the filter here by $firstRange, $secondRange
})->values();
Please or to participate in this conversation.