diff --git a/app/Http/Controllers/Qma/SupportTimesheetController.php b/app/Http/Controllers/Qma/SupportTimesheetController.php index 9b74ee3..5c16753 100644 --- a/app/Http/Controllers/Qma/SupportTimesheetController.php +++ b/app/Http/Controllers/Qma/SupportTimesheetController.php @@ -147,6 +147,7 @@ class SupportTimesheetController extends Controller $q->whereNotNull('parent_id') ->orWhereDoesntHave('units'); }) + ->with(['parent.requiredSimperType', 'requiredSimperType']) ->orderBy('code') ->get(); @@ -154,6 +155,18 @@ class SupportTimesheetController extends Controller ->orderBy('name') ->get(['id', 'nik', 'name', 'position', 'is_operator', 'employee_type', 'simper_expired_at']); + // Pemetaan kualifikasi SIMPER: Unit ID => Daftar Operator ID yang memiliki izin SIMPER sah + $unitOperatorsMap = []; + foreach ($unitSupports as $u) { + $eligibleIds = []; + foreach ($operators as $op) { + if ($op->hasValidSimperForVehicle($u)) { + $eligibleIds[] = $op->id; + } + } + $unitOperatorsMap[$u->id] = $eligibleIds; + } + $shifts = self::SHIFTS; $activityTypes = self::PRESET_ACTIVITIES; $locationSuggestions = UnitManagementController::getAllLocationSuggestions(); @@ -178,6 +191,7 @@ class SupportTimesheetController extends Controller 'breakdownCount', 'unitSupports', 'operators', + 'unitOperatorsMap', 'shifts', 'activityTypes', 'locationSuggestions', @@ -254,8 +268,15 @@ class SupportTimesheetController extends Controller 'activity_type.required' => 'Kategori pekerjaan wajib diisi.', ]); - $vehicle = Vehicle::findOrFail($validated['vehicle_id']); - $operator = Employee::findOrFail($validated['operator_id']); + $vehicle = Vehicle::with(['parent.requiredSimperType', 'requiredSimperType'])->findOrFail($validated['vehicle_id']); + $operator = Employee::with(['simpers.simperType'])->findOrFail($validated['operator_id']); + + if (!$operator->hasValidSimperForVehicle($vehicle)) { + $unitLabel = trim("{$vehicle->code} " . ($vehicle->brand_type ?? '')); + return back() + ->withInput() + ->with('error', "Operator {$operator->name} ({$operator->nik}) tidak memiliki izin SIMPER yang aktif/sah untuk mengoperasikan unit {$unitLabel}."); + } $startMeter = (float) $validated['start_meter']; $endMeter = isset($validated['end_meter']) && $validated['end_meter'] !== '' && $validated['end_meter'] !== null @@ -401,6 +422,18 @@ class SupportTimesheetController extends Controller $operatingHours = $endMeter !== null ? max(0, $endMeter - $startMeter) : null; $shift = $validated['shift'] ?: 'Shift Siang'; + $vehicle = Vehicle::with(['parent.requiredSimperType', 'requiredSimperType'])->findOrFail($validated['vehicle_id']); + $operator = Employee::with(['simpers.simperType'])->findOrFail($validated['operator_id']); + + if ((int) $timesheet->operator_id !== (int) $operator->id || (int) $timesheet->vehicle_id !== (int) $vehicle->id) { + if (!$operator->hasValidSimperForVehicle($vehicle)) { + $unitLabel = trim("{$vehicle->code} " . ($vehicle->brand_type ?? '')); + return back() + ->withInput() + ->with('error', "Operator {$operator->name} ({$operator->nik}) tidak memiliki izin SIMPER yang aktif/sah untuk mengoperasikan unit {$unitLabel}."); + } + } + $timesheet->update([ 'vehicle_id' => $validated['vehicle_id'], 'operator_id' => $validated['operator_id'], diff --git a/app/Models/Employee.php b/app/Models/Employee.php index 6b0ec60..d68eaa9 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -140,11 +140,12 @@ class Employee extends Model return $this->hasValidSimper(); } - // 1. Jika kendaraan mensyaratkan spesifik required_simper_type_id - if ($vehicle->required_simper_type_id) { + // 1. Jika kendaraan atau parent category kendaraan mensyaratkan spesifik required_simper_type_id + $requiredTypeId = $vehicle->required_simper_type_id ?? $vehicle->parent?->required_simper_type_id; + if ($requiredTypeId) { $simpers = $this->relationLoaded('simpers') ? $this->simpers : $this->simpers()->get(); foreach ($simpers as $s) { - if ($s->simper_type_id === $vehicle->required_simper_type_id && $s->isValid()) { + if ($s->simper_type_id === $requiredTypeId && $s->isValid()) { return true; } } @@ -159,23 +160,45 @@ class Employee extends Model } if ($vehicle->isUnitSupport()) { + $catName = $vehicle->unit_category ?: $vehicle->parent?->unit_category; + $brandName = $vehicle->brand_type ?: $vehicle->parent?->brand_type; + // Cek apakah personil memiliki izin SIMPER dengan scope kategori alat berat ini (misal: 'Dump Truck', 'Excavator', dll.) - if ($vehicle->unit_category && $this->hasValidSimperForScope($vehicle->unit_category)) { + if ($catName && $this->hasValidSimperForScope($catName)) { return true; } - if ($vehicle->brand_type && $this->hasValidSimperForScope($vehicle->brand_type)) { + if ($brandName && $this->hasValidSimperForScope($brandName)) { return true; } - if ($vehicle->is_timesheet_mobile) { + // Cek jika brand type mengandung kategori spesifik yang dimiliki SIMPER personil + if ($brandName) { + $simpers = $this->relationLoaded('simpers') ? $this->simpers : $this->simpers()->with('simperType')->get(); + foreach ($simpers as $s) { + if ($s->isValid() && $s->simperType) { + foreach ($s->simperType->scopes as $scope) { + if (!in_array($scope, [SimperType::SCOPE_ALL, SimperType::SCOPE_LIGHT_VEHICLE, SimperType::SCOPE_UNIT_SUPPORT_MOBILE, SimperType::SCOPE_UNIT_SUPPORT_HEAVY], true)) { + if (stripos($brandName, $scope) !== false) { + return true; + } + } + } + } + } + } + + $isMobile = $vehicle->is_timesheet_mobile + || ($vehicle->parent && $vehicle->parent->is_timesheet_mobile) + || str_starts_with(strtoupper($vehicle->code), 'DT') + || (stripos($catName ?? '', 'Dump Truck') !== false); + + if ($isMobile) { return $this->hasValidSimperForType('SIMPER-DT') - || $this->hasValidSimperForType('SIMPER-HEAVY') || $this->hasValidSimperForScope('unit_support_mobile') || $this->hasValidSimperForScope(SimperType::SCOPE_ALL); } return $this->hasValidSimperForType('SIMPER-HEAVY') - || $this->hasValidSimperForType('SIMPER-DT') || $this->hasValidSimperForScope('unit_support_heavy') || $this->hasValidSimperForScope(SimperType::SCOPE_ALL); } diff --git a/resources/views/qma/support_timesheet.blade.php b/resources/views/qma/support_timesheet.blade.php index b045039..ba261c1 100644 --- a/resources/views/qma/support_timesheet.blade.php +++ b/resources/views/qma/support_timesheet.blade.php @@ -571,7 +571,7 @@ id="addOperatorSelect" required class="w-full text-xs px-3.5 py-2.5 rounded-xl border border-slate-200 bg-white text-slate-800 focus:outline-none focus:border-[#1b5e20] focus:ring-1 focus:ring-[#1b5e20] transition"> - + @foreach($operators as $op) @php $opType = $op->employee_type ?: 'Karyawan'; @@ -587,7 +587,7 @@ @endforeach

- Total personil: {{ count($operators) }} + Total personil: - (Pilih unit terlebih dahulu) Filter: Semua

@@ -903,7 +903,7 @@ id="editOperatorSelect" required class="w-full text-xs px-3.5 py-2.5 rounded-xl border border-slate-200 bg-white text-slate-800 focus:outline-none focus:border-[#1b5e20] focus:ring-1 focus:ring-[#1b5e20] transition"> - + @foreach($operators as $op) @php $opType = $op->employee_type ?: 'Karyawan'; @@ -919,7 +919,7 @@ @endforeach

- Total personil: {{ count($operators) }} + Total personil: - (Pilih unit terlebih dahulu) Filter: Semua

@@ -1175,13 +1175,17 @@