StocksNew/Modules/Attendance/app/Http/Controllers/ReportController.php

68 lines
2.4 KiB
PHP
Raw Normal View History

2024-08-27 12:03:06 +00:00
<?php
namespace Modules\Attendance\Http\Controllers;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Modules\Attendance\Models\Attendance;
use Modules\Attendance\Repositories\AttendanceInterface;
use Modules\Attendance\Repositories\AttendanceRepository;
use Modules\Employee\Repositories\EmployeeInterface;
use Modules\Leave\Models\Leave;
class ReportController extends Controller
{
private $attendanceRepository;
private $employeeRepository;
public function __construct(
AttendanceInterface $attendanceRepository,
EmployeeInterface $employeeRepository) {
$this->attendanceRepository = $attendanceRepository;
$this->employeeRepository = $employeeRepository;
}
public function monthly()
{
$data['title'] = 'Employee Attendance Lists';
$firstDayOfMonth = Carbon::createFromDate(date('Y'), date('m'), 1)->startOfMonth();
$lastDayOfMonth = Carbon::createFromDate(date('Y'), date('m'), 1)->endOfMonth();
for ($day = $firstDayOfMonth; $day <= $lastDayOfMonth; $day->addDay()) {
$data['dayLists'][] = $day->format('Y-m-d');
}
$data['employees'] = $this->employeeRepository->findAll()
->transform(function ($employee) use ($data) {
$attendances = Attendance::
where('employee_id', $employee->id)
->whereIn('date', $data['dayLists'])
->select('clock_in_time', 'clock_out_time', 'date')
->get()
// ->groupBy('date')
->mapWithKeys(function ($atd) {
return [$atd['date'] => 'P'];
});
$leave = Leave::
where('employee_id', $employee->id)
->whereIn('start_date', $data['dayLists'])
->whereStatus(2)
->get()
->mapWithKeys(function ($atd) {
return [$atd['start_date'] => 'L'];
});
$final = $attendances->push($leave);
$attendanceArray = [];
foreach ($data['dayLists'] as $date) {
$attendanceArray[$date] = array_key_exists($date, $final->toArray()) ? $final[$date] : '';
}
$employee->dates = $attendanceArray;
return $employee;
});
return view('attendance::report.monthly', $data);
}
}