81 lines
2.6 KiB
PHP
81 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Hr;
|
|
|
|
use App\Enums\Permission;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Hr\AttendanceCheckInRequest;
|
|
use App\Http\Requests\Admin\Hr\AttendanceCheckOutRequest;
|
|
use App\Models\Attendance;
|
|
use App\Services\Hr\AttendanceService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class AttendanceController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly AttendanceService $attendanceService,
|
|
) {}
|
|
|
|
public function index(Request $request): Response
|
|
{
|
|
$user = auth()->user();
|
|
$employee = $user?->employee;
|
|
$canManageAll = $user?->can(Permission::ATTENDANCES_MANAGE->value) ?? false;
|
|
|
|
$scopedEmployeeId = $canManageAll ? null : $employee?->id;
|
|
$hasScopedAccess = $canManageAll || $employee !== null;
|
|
|
|
$start = $request->date('start') ?? now()->startOfMonth();
|
|
$end = $request->date('end') ?? now()->endOfMonth();
|
|
|
|
return Inertia::render('admin/hr/attendances/Index', [
|
|
'attendances' => $this->attendanceService->listForCalendar(
|
|
$start,
|
|
$end,
|
|
$scopedEmployeeId,
|
|
$hasScopedAccess,
|
|
),
|
|
'todayAttendance' => $employee
|
|
? $this->attendanceService->todayAttendanceForEmployee($employee)
|
|
: null,
|
|
'canCheckIn' => ($user?->can(Permission::ATTENDANCES_CREATE->value) ?? false)
|
|
&& $employee !== null,
|
|
'canManageAll' => $canManageAll,
|
|
'calendarRange' => [
|
|
'start' => $start->toDateString(),
|
|
'end' => $end->toDateString(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function checkIn(AttendanceCheckInRequest $request): RedirectResponse
|
|
{
|
|
$this->attendanceService->checkIn($request->validated());
|
|
|
|
Inertia::flash('success', 'Presensi masuk berhasil dicatat.');
|
|
|
|
return redirect()->route('admin.hr.attendances.index');
|
|
}
|
|
|
|
public function checkOut(AttendanceCheckOutRequest $request): RedirectResponse
|
|
{
|
|
$this->attendanceService->checkOut($request->validated());
|
|
|
|
Inertia::flash('success', 'Presensi pulang berhasil dicatat.');
|
|
|
|
return redirect()->route('admin.hr.attendances.index');
|
|
}
|
|
|
|
public function destroy(Attendance $attendance): RedirectResponse
|
|
{
|
|
$this->attendanceService->delete($attendance);
|
|
|
|
Inertia::flash('success', 'Data presensi berhasil dihapus.');
|
|
|
|
return redirect()->route('admin.hr.attendances.index');
|
|
}
|
|
}
|