TIFNJK_E41222887/app/Http/Controllers/Admin/AttendanceController.php

62 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Attendance;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Carbon\Carbon;
class AttendanceController extends Controller
{
public function index(Request $request)
{
$query = Attendance::with('employee')->orderBy('date', 'desc');
if ($request->search) {
$query->whereHas('employee', function ($q) use ($request) {
$q->where('name', 'like', '%' . $request->search . '%')
->orWhere('nip', 'like', '%' . $request->search . '%');
});
}
if ($request->date) {
$query->where('date', $request->date);
}
if ($request->status) {
$query->where('status', $request->status);
}
$attendances = $query->get()->map(function ($attendance) {
$totalHours = null;
if ($attendance->check_in && $attendance->check_out) {
$checkIn = Carbon::parse($attendance->check_in);
$checkOut = Carbon::parse($attendance->check_out);
$diff = $checkIn->diff($checkOut);
$totalHours = sprintf('%02d:%02d', $diff->h, $diff->i);
}
return [
'id' => $attendance->id,
'employee_name' => $attendance->employee->name ?? '-',
'employee_nik' => $attendance->employee->nip ?? '-',
'date' => $attendance->date,
'check_in' => $attendance->check_in,
'check_out' => $attendance->check_out,
'status' => $attendance->status,
'notes' => $attendance->notes,
'total_hours' => $totalHours,
'latitude_in' => $attendance->latitude_in,
'longitude_in' => $attendance->longitude_in,
'latitude_out' => $attendance->latitude_out,
'longitude_out'=> $attendance->longitude_out,
];
});
return Inertia::render('admin/attendances/index', [
'attendances' => $attendances,
'filters' => $request->only(['search', 'date', 'status']),
]);
}
}