dstpabuaran.com/app/Http/Controllers/Admin/HR/AttendanceController.php

85 lines
2.9 KiB
PHP

<?php
namespace App\Http\Controllers\Admin\HR;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\HR\AttendanceRequest;
use App\Models\Attendance;
use App\Services\Admin\HR\AttendanceService;
use App\Settings\HRSettings;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
class AttendanceController extends Controller
{
public function __construct(
private AttendanceService $service
) {}
public function index(Request $request): Response
{
$year = $request->integer('year', now()->year);
$month = $request->integer('month', now()->month);
$hrSettings = app(HRSettings::class);
$user = auth()->user();
$isAdmin = $user->hasAnyRole(['developer', 'owner', 'direktur']);
if ($isAdmin) {
return Inertia::render('admin/hr/attendance/index', [
'attendances' => $this->service->getByMonth($year, $month),
'todayAttendance' => null,
'currentYear' => $year,
'currentMonth' => $month,
'monthStats' => $this->service->getMonthStats($year, $month),
'hrSettings' => [
'scheduled_check_in_time' => $hrSettings->scheduled_check_in_time,
'scheduled_check_out_time' => $hrSettings->scheduled_check_out_time,
],
'isAdmin' => true,
]);
}
$employeeId = $user->employee?->id;
return Inertia::render('admin/hr/attendance/index', [
'attendances' => $this->service->getByMonth($year, $month, $employeeId),
'todayAttendance' => $this->service->getToday(),
'currentYear' => $year,
'currentMonth' => $month,
'monthStats' => $this->service->getMonthStats($year, $month, $employeeId),
'hrSettings' => [
'scheduled_check_in_time' => $hrSettings->scheduled_check_in_time,
'scheduled_check_out_time' => $hrSettings->scheduled_check_out_time,
],
'isAdmin' => false,
]);
}
public function store(AttendanceRequest $request): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->checkIn($request->validated()),
'Berhasil check-in.',
'admin.hr.attendances.index'
);
}
public function update(AttendanceRequest $request, Attendance $attendance): RedirectResponse
{
return $this->handleAction(
fn () => $this->service->checkOut($attendance, $request->validated()),
'Berhasil check-out.',
'admin.hr.attendances.index'
);
}
public function byDate(Request $request): ?array
{
$date = $request->query('date', now()->toDateString());
return $this->service->getByDate($date);
}
}