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

46 lines
1.5 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()
{
$attendances = Attendance::with('employee')->orderBy('date', 'desc')->get();
$attendances = $attendances->map(function ($attendance) {
$totalHours = null;
// Menghitung selisih jam kerja dalam format H:i
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,
];
});
return Inertia::render('admin/attendances/index', [
'attendances' => $attendances
]);
}
}