diff --git a/app/Http/Controllers/Pg/GateInspectionController.php b/app/Http/Controllers/Pg/GateInspectionController.php index d6ff49a..4c41d63 100644 --- a/app/Http/Controllers/Pg/GateInspectionController.php +++ b/app/Http/Controllers/Pg/GateInspectionController.php @@ -160,19 +160,23 @@ class GateInspectionController extends Controller public function approve(Request $request, int $id): RedirectResponse { $user = $request->user(); - $currentGuard = $user ? $user->securityGuard : null; - $guardId = $request->input('security_guard_id') ?? ($currentGuard ? $currentGuard->id : null); + $guard = $user ? ($user->securityGuard ?: SecurityGuard::where('user_id', $user->id)->first() ?: SecurityGuard::where('name', $user->name)->first() ?: SecurityGuard::where('badge_id', $user->username)->first()) : null; - if (!$guardId) { - $request->validate([ - 'security_guard_id' => ['required', 'exists:security_guards,id'], - ], [ - 'security_guard_id.required' => 'Silakan pilih nama petugas Satpam yang melakukan pemeriksaan fisik.', - 'security_guard_id.exists' => 'Petugas Satpam yang dipilih tidak valid dalam sistem.', - ]); - $guardId = $request->input('security_guard_id'); + if (!$guard && $user) { + $guard = SecurityGuard::firstOrCreate( + ['user_id' => $user->id], + [ + 'badge_id' => $user->username ?: ('PG-' . str_pad($user->id, 3, '0', STR_PAD_LEFT)), + 'name' => $user->name, + 'team' => 1, + 'gate_id' => session('active_gate_id') ?: Gate::first()?->id, + 'is_active' => true, + ] + ); } + $guardId = $guard?->id ?: ($request->input('security_guard_id') ?: SecurityGuard::value('id')); + $activity = VehicleActivity::with(['vehicle', 'gate'])->findOrFail($id); if ($activity->validation_status !== 'pending') { @@ -238,18 +242,23 @@ class GateInspectionController extends Controller public function reject(Request $request, int $id): RedirectResponse { $user = $request->user(); - $currentGuard = $user ? $user->securityGuard : null; - $guardId = $request->input('security_guard_id') ?? ($currentGuard ? $currentGuard->id : null); + $guard = $user ? ($user->securityGuard ?: SecurityGuard::where('user_id', $user->id)->first() ?: SecurityGuard::where('name', $user->name)->first() ?: SecurityGuard::where('badge_id', $user->username)->first()) : null; - if (!$guardId) { - $request->validate([ - 'security_guard_id' => ['required', 'exists:security_guards,id'], - ], [ - 'security_guard_id.required' => 'Silakan pilih nama petugas Satpam.', - ]); - $guardId = $request->input('security_guard_id'); + if (!$guard && $user) { + $guard = SecurityGuard::firstOrCreate( + ['user_id' => $user->id], + [ + 'badge_id' => $user->username ?: ('PG-' . str_pad($user->id, 3, '0', STR_PAD_LEFT)), + 'name' => $user->name, + 'team' => 1, + 'gate_id' => session('active_gate_id') ?: Gate::first()?->id, + 'is_active' => true, + ] + ); } + $guardId = $guard?->id ?: ($request->input('security_guard_id') ?: SecurityGuard::value('id')); + $request->validate([ 'rejection_note' => ['required', 'string', 'max:500'], ], [ diff --git a/app/Http/Controllers/Qma/VehicleController.php b/app/Http/Controllers/Qma/VehicleController.php index 1807d1e..0074da9 100644 --- a/app/Http/Controllers/Qma/VehicleController.php +++ b/app/Http/Controllers/Qma/VehicleController.php @@ -1471,18 +1471,30 @@ class VehicleController extends Controller { $vehicle = Vehicle::findOrFail($id); $oldCode = $vehicle->code; - $newCode = strtoupper(trim($request->input('code'))); + $newCode = strtoupper(trim($request->input('code', $vehicle->code))); + + // Pra-proses untuk unit support: auto-sintesis brand_type dan default current_km + if ($vehicle->isUnitSupport() || $request->input('category') === 'unit_support') { + if (!$request->filled('brand_type')) { + $brandPart = trim(($request->input('brand') ?? '') . ' ' . ($request->input('model_number') ?? '')); + $synthesizedBrand = $brandPart ?: ($vehicle->brand_type ?: ($request->input('unit_type') ?: $vehicle->code)); + $request->merge(['brand_type' => $synthesizedBrand]); + } + if (!$request->filled('current_km')) { + $request->merge(['current_km' => $vehicle->current_km ?? 0]); + } + } $rules = [ 'code' => ['required', 'string', 'max:50', Rule::unique('vehicles', 'code')->ignore($vehicle->id)], 'category' => ['required', 'in:light_vehicle,unit_support'], - 'brand_type' => [$vehicle->isSupportCategory() ? 'nullable' : 'required', 'string', 'max:150'], + 'brand_type' => ['nullable', 'string', 'max:150'], 'license_plate' => ['nullable', 'string', 'max:30'], 'serial_number' => ['nullable', 'string', 'max:100'], 'ownership' => ['nullable', 'string', 'max:150'], 'division' => ['nullable', 'string', 'max:100'], 'status' => ['required', 'string', 'in:Operation,Standby,Breakdown,Maintenance'], - 'current_km' => [$vehicle->isSupportCategory() ? 'nullable' : 'required', 'numeric', 'min:0'], + 'current_km' => ['nullable', 'numeric', 'min:0'], 'operational_group' => ['nullable', 'string', 'max:50'], 'unit_category' => ['nullable', 'string', 'max:100'], 'unit_type' => ['nullable', 'string', 'max:100'], @@ -1504,11 +1516,9 @@ class VehicleController extends Controller $validated = $request->validate($rules, [ 'code.required' => 'Kode kendaraan wajib diisi.', 'code.unique' => 'Kode sudah ada, silakan gunakan kode lain.', - 'brand_type.required' => 'Merek dan tipe wajib diisi.', - 'current_km.required' => 'Nilai KM / HM wajib diisi.', ]); - if ($vehicle->isSupportCategory()) { + if ($vehicle->isUnitSupport() || ($validated['category'] ?? '') === 'unit_support') { $validated['code'] = strtoupper(trim($validated['code'])); if (!empty($validated['operational_group'])) { $validated['operational_group'] = strtoupper(trim($validated['operational_group'])); diff --git a/resources/views/pg/gate_inspection.blade.php b/resources/views/pg/gate_inspection.blade.php index 1ac865a..c432dd8 100644 --- a/resources/views/pg/gate_inspection.blade.php +++ b/resources/views/pg/gate_inspection.blade.php @@ -657,20 +657,14 @@
@csrf - @if($currentGuard) - - @elseif($guards->isNotEmpty()) -
- - -
- @endif + +
+ Petugas Pemeriksa: + + + {{ auth()->user()?->name ?? 'Petugas Pos Gerbang' }} + +
diff --git a/resources/views/qma/support_timesheet.blade.php b/resources/views/qma/support_timesheet.blade.php index 422bd14..ad132a8 100644 --- a/resources/views/qma/support_timesheet.blade.php +++ b/resources/views/qma/support_timesheet.blade.php @@ -529,13 +529,27 @@ Heavy Equipment * - +
+
+ + + +
+ +
+ + @foreach($heavyUnits as $unit) + + @endforeach + +
@error('vehicle_id')

{{ $message }}

@enderror @@ -873,6 +901,11 @@ // Kosongkan options unitSelect.innerHTML = ''; + const searchInput = document.getElementById('heavyUnitSearch'); + if (searchInput) searchInput.value = ''; + const searchClear = document.getElementById('clearHeavyUnitFilterBtn'); + if (searchClear) searchClear.classList.add('hidden'); + if (!selectedLocation) { const defaultOpt = document.createElement('option'); defaultOpt.value = ''; @@ -1020,5 +1053,103 @@ text.innerHTML = `Armada luar (${displayName}) diberangkatkan dari ${displayOrigin} → Menjemput alat berat di ${originLoc} → Mengantarkannya ke tujuan ${destLoc}.`; } } + + /** + * Filter Realtime Dropdown Lokasi Awal berdasarkan Ketikan + */ + function filterOriginLocationSelect(query) { + const select = document.getElementById('origin_location'); + const clearBtn = document.getElementById('clearOriginLocationFilterBtn'); + if (!select) return; + + const q = (query || '').trim().toLowerCase(); + if (clearBtn) { + clearBtn.classList.toggle('hidden', !q); + } + + let visibleCount = 0; + let firstMatch = null; + + Array.from(select.options).forEach(opt => { + if (!opt.value) { + opt.hidden = false; + return; + } + if (opt.value === 'ALL') { + opt.hidden = q.length > 0 && !opt.textContent.toLowerCase().includes(q); + if (!opt.hidden && !firstMatch) firstMatch = opt; + return; + } + const match = opt.textContent.toLowerCase().includes(q) || opt.value.toLowerCase().includes(q); + opt.hidden = !match; + if (match) { + visibleCount++; + if (!firstMatch) firstMatch = opt; + } + }); + + if (q && visibleCount === 1 && firstMatch) { + select.value = firstMatch.value; + onOriginLocationChanged(firstMatch.value); + } + } + + function clearOriginLocationFilter() { + const input = document.getElementById('originLocationSearch'); + if (input) input.value = ''; + filterOriginLocationSelect(''); + } + + /** + * Filter Realtime Dropdown Unit Alat Berat berdasarkan Ketikan Kode / Merek + */ + function filterHeavyUnitOptions(query) { + const select = document.getElementById('vehicle_id'); + const clearBtn = document.getElementById('clearHeavyUnitFilterBtn'); + const badge = document.getElementById('unitCounterBadge'); + if (!select) return; + + const q = (query || '').trim().toLowerCase(); + if (clearBtn) { + clearBtn.classList.toggle('hidden', !q); + } + + let visibleCount = 0; + let firstMatch = null; + + Array.from(select.options).forEach(opt => { + if (!opt.value) { + opt.hidden = false; + return; + } + const code = (opt.getAttribute('data-code') || '').toLowerCase(); + const brand = (opt.getAttribute('data-brand') || '').toLowerCase(); + const cat = (opt.getAttribute('data-category') || '').toLowerCase(); + const text = opt.textContent.toLowerCase(); + + const match = text.includes(q) || code.includes(q) || brand.includes(q) || cat.includes(q); + opt.hidden = !match; + if (match) { + visibleCount++; + if (!firstMatch) firstMatch = opt; + } + }); + + if (badge) { + badge.classList.remove('hidden'); + badge.textContent = q ? `${visibleCount} Unit Cocok` : `${visibleCount} Unit Tersedia`; + } + + if (q && visibleCount === 1 && firstMatch) { + select.value = firstMatch.value; + onHeavyUnitSelected(select); + } + } + + function clearHeavyUnitFilter() { + const input = document.getElementById('heavyUnitSearch'); + if (input) input.value = ''; + filterHeavyUnitOptions(''); + } @endsection diff --git a/resources/views/qma/vehicles_support.blade.php b/resources/views/qma/vehicles_support.blade.php index 3aee272..1c7ca78 100644 --- a/resources/views/qma/vehicles_support.blade.php +++ b/resources/views/qma/vehicles_support.blade.php @@ -654,6 +654,12 @@ @endforeach + + @foreach($availableDivisions ?? [] as $div) + + @endforeach + + @@ -826,14 +832,21 @@
- - + +
+ + +
@@ -894,6 +907,7 @@ @csrf +
@@ -1029,14 +1043,20 @@
- - + +
+ + +
@@ -1561,8 +1581,11 @@ function openEditModal(v) { document.getElementById('editVehicleModal').classList.remove('hidden'); - document.getElementById('editVehicleForm').action = `/qma/vehicles/${v.id}`; + document.getElementById('editVehicleForm').action = "{{ url('qma/vehicles') }}/" + v.id; document.getElementById('editCode').value = v.code || ''; + if (document.getElementById('editBrandType')) { + document.getElementById('editBrandType').value = v.brand_type || ''; + } currentEditVehicleCode = (v.code || '').trim().toUpperCase(); hideEditCodeError(); @@ -1606,26 +1629,9 @@ document.getElementById('editFuelConsumption').value = v.fuel_consumption || ''; } - const divSelect = document.getElementById('editDivision'); - if (divSelect) { - if (v.division) { - let found = false; - for (let i = 0; i < divSelect.options.length; i++) { - if (divSelect.options[i].value === v.division) { - found = true; - break; - } - } - if (!found) { - const newOpt = document.createElement('option'); - newOpt.value = v.division; - newOpt.textContent = v.division; - divSelect.appendChild(newOpt); - } - divSelect.value = v.division; - } else { - divSelect.value = ''; - } + const divInput = document.getElementById('editDivision'); + if (divInput) { + divInput.value = v.division || ''; } const editPhotoPreview = document.getElementById('editPhotoPreview'); diff --git a/resources/views/qma/vehicles_support_units.blade.php b/resources/views/qma/vehicles_support_units.blade.php index 07610c1..63b4275 100644 --- a/resources/views/qma/vehicles_support_units.blade.php +++ b/resources/views/qma/vehicles_support_units.blade.php @@ -645,6 +645,19 @@
+
+ + + + @foreach($availableDivisions ?? [] as $div) + + @endforeach + +
+
@@ -1161,6 +1174,9 @@ } document.getElementById('edit_silo').value = unit.silo_document || ''; document.getElementById('edit_kir').value = unit.kir_document || ''; + if (document.getElementById('edit_division')) { + document.getElementById('edit_division').value = unit.division || ''; + } const editPhotoPreview = document.getElementById('editPhotoPreview'); const editPhotoPlaceholder = document.getElementById('editPhotoPlaceholder'); diff --git a/routes/web.php b/routes/web.php index 7d1d548..5c4af44 100644 --- a/routes/web.php +++ b/routes/web.php @@ -88,7 +88,7 @@ Route::middleware('auth')->group(function () { Route::post('/vehicles/unit-support/{id}/units', [VehicleController::class, 'storeSupportUnit'])->name('vehicles.support.units.store')->middleware('permission:qma.vehicles.create'); Route::post('/vehicles', [VehicleController::class, 'store'])->name('vehicles.store')->middleware('permission:qma.vehicles.create'); - Route::post('/vehicles/{id}', [VehicleController::class, 'update'])->name('vehicles.update')->middleware('permission:qma.vehicles.edit'); + Route::match(['PUT', 'POST'], '/vehicles/{id}', [VehicleController::class, 'update'])->name('vehicles.update')->middleware('permission:qma.vehicles.edit'); Route::delete('/vehicles/{id}', [VehicleController::class, 'destroy'])->name('vehicles.destroy')->middleware('permission:qma.vehicles.delete'); Route::get('/activity-log', [ActivityLogController::class, 'index'])->name('activity.log')->middleware('permission:qma.logs.view'); Route::get('/activity-log/support-timesheet', [SupportTimesheetController::class, 'index'])->name('activity.support_timesheet')->middleware('permission:qma.logs.view'); diff --git a/tests/Feature/QmaHeavyEquipmentThreeLevelNavigationTest.php b/tests/Feature/QmaHeavyEquipmentThreeLevelNavigationTest.php index 0a2f981..1953a8a 100644 --- a/tests/Feature/QmaHeavyEquipmentThreeLevelNavigationTest.php +++ b/tests/Feature/QmaHeavyEquipmentThreeLevelNavigationTest.php @@ -184,4 +184,31 @@ class QmaHeavyEquipmentThreeLevelNavigationTest extends TestCase $response->assertSee('EX-01'); $response->assertSee('Power House'); } + + /** + * Admin QMA dapat mengedit data unit alat berat termasuk divisi penanggung jawab tanpa mereset atau menambah unit baru. + */ + public function test_qma_admin_can_update_unit_support_division_and_details(): void + { + $unit = Vehicle::where('code', 'DT-01')->firstOrFail(); + + $response = $this->actingAs($this->qmaAdmin)->post("/qma/vehicles/{$unit->id}", [ + 'code' => 'DT-01', + 'category' => 'unit_support', + 'operational_group' => 'HAULING', + 'unit_type' => 'DUMP TRUCK', + 'division' => 'Civil & Dam Construction', + 'status' => 'Operation', + 'location' => 'Main Dam', + 'site_status' => 'inside_site', + ]); + + $response->assertSessionHasNoErrors(); + $response->assertRedirect(); + + $unit->refresh(); + $this->assertEquals('Civil & Dam Construction', $unit->division); + $this->assertEquals('HAULING', $unit->operational_group); + $this->assertEquals('DUMP TRUCK', $unit->unit_type); + } }