91 lines
3.2 KiB
PHP
91 lines
3.2 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']);
|
|
|
|
if ($isAdmin) {
|
|
return Inertia::render('admin/hr/attendance/index', [
|
|
'attendances' => $this->service->getByMonth($year, $month),
|
|
'leaves' => $this->service->getLeavesByMonth($year, $month),
|
|
'employees' => $this->service->getAllEmployees(),
|
|
'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;
|
|
$isOnLeave = $this->service->isOnLeave($user);
|
|
|
|
return Inertia::render('admin/hr/attendance/index', [
|
|
'attendances' => $this->service->getByMonth($year, $month, $employeeId),
|
|
'leaves' => $this->service->getLeavesByMonth($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,
|
|
],
|
|
'isOnLeave' => $isOnLeave,
|
|
'canCheckIn' => $user->employee !== null,
|
|
'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);
|
|
}
|
|
}
|