From 61f2a85e3e959c9bc1a2b47653c9aa5d9478c95a Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Wed, 21 Oct 2026 15:56:46 +0700 Subject: [PATCH] feat(qma): streamline support timesheet workflow by selecting location first to filter ready units --- .../Qma/SupportTimesheetController.php | 31 +- .../views/qma/support_timesheet.blade.php | 472 +++++++++++++----- tests/Feature/QmaSupportTimesheetTest.php | 45 ++ 3 files changed, 412 insertions(+), 136 deletions(-) diff --git a/app/Http/Controllers/Qma/SupportTimesheetController.php b/app/Http/Controllers/Qma/SupportTimesheetController.php index 979a98f..12a3658 100644 --- a/app/Http/Controllers/Qma/SupportTimesheetController.php +++ b/app/Http/Controllers/Qma/SupportTimesheetController.php @@ -138,6 +138,15 @@ class SupportTimesheetController extends Controller $activityTypes = self::PRESET_ACTIVITIES; $locationSuggestions = UnitManagementController::getAllLocationSuggestions(); + $unitLocations = $unitSupports->groupBy(function ($u) { + return trim($u->location) ?: 'Site Kerinci'; + })->map(fn($group) => $group->count())->sortKeys(); + + $otherLocations = collect($locationSuggestions) + ->diff($unitLocations->keys()) + ->values() + ->all(); + return view('qma.support_timesheet', compact( 'timesheets', 'startDate', @@ -151,7 +160,9 @@ class SupportTimesheetController extends Controller 'operators', 'shifts', 'activityTypes', - 'locationSuggestions' + 'locationSuggestions', + 'unitLocations', + 'otherLocations' )); } @@ -173,7 +184,15 @@ class SupportTimesheetController extends Controller } } - // 2. Defaultkan shift ke Shift Siang jika tidak diisi + // 2. Tangani normalisasi lokasi kerja jika custom atau ALL + if ($request->input('location') === '__CUSTOM__' && $request->filled('custom_location')) { + $request->merge(['location' => trim($request->input('custom_location'))]); + } elseif (!$request->filled('location') || $request->input('location') === 'ALL') { + $veh = $request->filled('vehicle_id') ? Vehicle::find($request->input('vehicle_id')) : null; + $request->merge(['location' => $veh?->location ?: 'Site Kerinci']); + } + + // 3. Defaultkan shift ke Shift Siang jika tidak diisi if (!$request->filled('shift')) { $request->merge(['shift' => 'Shift Siang']); } @@ -306,6 +325,14 @@ class SupportTimesheetController extends Controller } } + // 2. Tangani normalisasi lokasi kerja jika custom atau ALL + if ($request->input('location') === '__CUSTOM__' && $request->filled('custom_location')) { + $request->merge(['location' => trim($request->input('custom_location'))]); + } elseif (!$request->filled('location') || $request->input('location') === 'ALL') { + $veh = $request->filled('vehicle_id') ? Vehicle::find($request->input('vehicle_id')) : null; + $request->merge(['location' => $veh?->location ?: $timesheet->location ?: 'Site Kerinci']); + } + if (!$request->filled('shift')) { $request->merge(['shift' => 'Shift Siang']); } diff --git a/resources/views/qma/support_timesheet.blade.php b/resources/views/qma/support_timesheet.blade.php index 0528f69..5dc2a1f 100644 --- a/resources/views/qma/support_timesheet.blade.php +++ b/resources/views/qma/support_timesheet.blade.php @@ -465,32 +465,70 @@
@csrf - +
+
+ +
+ + +
+ + +

Pilih lokasi agar sistem memfilter unit yang ready di area tersebut.

+
+
- - +
+
+ +
+ +
+ + +
- +
- +
- -
-
- - -
- -
- - -
-
-
@@ -1060,6 +1113,28 @@ function openAddModal() { const modal = document.getElementById('addTimesheetModal'); if (modal) { + // Reset form pilihan lokasi & unit + const addLocSelect = document.getElementById('addLocationSelect'); + if (addLocSelect) addLocSelect.selectedIndex = 0; + const customWrap = document.getElementById('addCustomLocationWrapper'); + if (customWrap) customWrap.classList.add('hidden'); + const customInput = document.getElementById('addCustomLocationInput'); + if (customInput) customInput.value = ''; + const finalLoc = document.getElementById('addFinalLocation'); + if (finalLoc) finalLoc.value = ''; + + const vehSelect = document.getElementById('addVehicleSelect'); + if (vehSelect) { + vehSelect.innerHTML = ''; + } + const counter = document.getElementById('addUnitCounterBadge'); + if (counter) counter.classList.add('hidden'); + const preview = document.getElementById('addVehiclePreview'); + if (preview) preview.classList.add('hidden'); + + const startMeter = document.getElementById('addStartMeter'); + if (startMeter) startMeter.value = ''; + modal.classList.remove('hidden'); modal.style.display = 'block'; } @@ -1122,15 +1197,40 @@ const unitCode = ts.vehicle ? ts.vehicle.code : (ts.vehicle_code || ''); document.getElementById('editTimesheetSubtitle').textContent = `No: ${ts.timesheet_no} (${unitCode || '-'})`; - - const editVehicleInput = document.getElementById('editVehicleInput'); - const editVehicleId = document.getElementById('editVehicleId'); - if (editVehicleInput) { - editVehicleInput.value = unitCode; - editVehicleId.value = ts.vehicle_id; - onVehicleTyped(editVehicleInput, 'edit'); + + // 1. Sinkronisasi Lokasi Kerja + const locSelect = document.getElementById('editLocationSelect'); + const finalLoc = document.getElementById('editFinalLocation'); + const customWrap = document.getElementById('editCustomLocationWrapper'); + const targetLoc = ts.location || ''; + + finalLoc.value = targetLoc; + + let matched = false; + if (locSelect) { + for (let i = 0; i < locSelect.options.length; i++) { + if (locSelect.options[i].value.toLowerCase() === targetLoc.toLowerCase()) { + locSelect.selectedIndex = i; + matched = true; + break; + } + } + + if (!matched && targetLoc) { + // Tambahkan option lokasi baru jika belum terdaftar + const newOpt = document.createElement('option'); + newOpt.value = targetLoc; + newOpt.textContent = `${targetLoc} (Lokasi Riwayat)`; + locSelect.appendChild(newOpt); + locSelect.value = targetLoc; + } } + if (customWrap) customWrap.classList.add('hidden'); + + // Filter unit pada lokasi ini dengan memilih ts.vehicle_id + onTimesheetLocationChanged(locSelect ? locSelect.value : targetLoc, 'edit', ts.vehicle_id); + document.getElementById('editOperatorSelect').value = ts.operator_id; // Format tanggal YYYY-MM-DD @@ -1143,7 +1243,6 @@ document.getElementById('editStandbyHours').value = ts.standby_hours || 0; document.getElementById('editBreakdownHours').value = ts.breakdown_hours || 0; - document.getElementById('editLocationInput').value = ts.location; document.getElementById('editActivityType').value = ts.activity_type; document.getElementById('editStatus').value = ts.status || 'Operation'; document.getElementById('editTaskDescription').value = ts.task_description || ts.notes || ''; @@ -1176,54 +1275,159 @@ modal.style.display = 'none'; } - // 5. Handler saat Unit Diketik (Auto-detect kode/nama unit & auto isi meter awal) - function onVehicleTyped(inputEl, prefix) { - const val = (inputEl.value || '').trim().toLowerCase(); + // 5. Handler saat Lokasi Kerja Dipilih (Filter Unit Support yang Ready di Lokasi tersebut) + function onTimesheetLocationChanged(selectedLoc, prefix, preselectVehicleId = null) { + const customWrapper = document.getElementById(`${prefix}CustomLocationWrapper`); + const customInput = document.getElementById(`${prefix}CustomLocationInput`); + const finalLocation = document.getElementById(`${prefix}FinalLocation`); + const vehicleSelect = document.getElementById(`${prefix}VehicleSelect`); + const counterBadge = document.getElementById(`${prefix}UnitCounterBadge`); + const previewEl = document.getElementById(`${prefix}VehiclePreview`); + + if (!vehicleSelect) return; + + // Tangani pilihan custom atau ALL + if (selectedLoc === '__CUSTOM__') { + if (customWrapper) customWrapper.classList.remove('hidden'); + if (customInput) { + customInput.focus(); + if (finalLocation) finalLocation.value = customInput.value.trim(); + } + } else { + if (customWrapper) customWrapper.classList.add('hidden'); + if (finalLocation) { + finalLocation.value = (selectedLoc === 'ALL' || !selectedLoc) ? '' : selectedLoc; + } + } + + const allUnits = window.unitSupportsData || []; + let filteredUnits = []; + + if (!selectedLoc) { + vehicleSelect.innerHTML = ''; + if (counterBadge) counterBadge.classList.add('hidden'); + if (previewEl) previewEl.classList.add('hidden'); + return; + } else if (selectedLoc === 'ALL' || selectedLoc === '__CUSTOM__') { + filteredUnits = allUnits; + } else { + const norm = selectedLoc.trim().toLowerCase(); + filteredUnits = allUnits.filter(u => { + const uLoc = (u.location || 'Site Kerinci').trim().toLowerCase(); + return uLoc === norm; + }); + + // Jika dalam mode edit dan preselectVehicleId tidak ditemukan di lokasi ini (misal unit berpindah setelah ts dicatat), sertakan unit tersebut + if (preselectVehicleId) { + const foundInFiltered = filteredUnits.some(u => String(u.id) === String(preselectVehicleId)); + if (!foundInFiltered) { + const extra = allUnits.find(u => String(u.id) === String(preselectVehicleId)); + if (extra) filteredUnits.unshift(extra); + } + } + } + + // Update badge counter + if (counterBadge) { + counterBadge.classList.remove('hidden'); + if (selectedLoc === 'ALL' || selectedLoc === '__CUSTOM__') { + counterBadge.textContent = `${filteredUnits.length} Total Unit`; + } else { + counterBadge.textContent = `${filteredUnits.length} Unit Ready`; + } + } + + // Render option unit support + vehicleSelect.innerHTML = ''; + const defaultPrompt = document.createElement('option'); + defaultPrompt.value = ''; + defaultPrompt.disabled = true; + defaultPrompt.textContent = filteredUnits.length > 0 + ? `-- Pilih Unit Support (${filteredUnits.length} Unit Ready) --` + : `-- Tidak ada unit di lokasi ini (Pilih 'Semua Unit') --`; + vehicleSelect.appendChild(defaultPrompt); + + let hasSelected = false; + filteredUnits.forEach(u => { + const opt = document.createElement('option'); + opt.value = u.id; + opt.setAttribute('data-km', u.current_km ?? 0); + opt.setAttribute('data-code', u.code || ''); + opt.setAttribute('data-brand', u.brand_type || ''); + opt.setAttribute('data-location', u.location || 'Site Kerinci'); + + const lastKm = parseFloat(u.current_km) || 0; + const code = (u.code || '').toUpperCase(); + const meterType = code.startsWith('DT') ? 'KM' : 'HM'; + const kmFormatted = lastKm.toLocaleString('id-ID', {minimumFractionDigits: 1, maximumFractionDigits: 2}); + + opt.textContent = `${u.code} - ${u.brand_type || ''} (Meter: ${kmFormatted} ${meterType})`; + + if (preselectVehicleId && String(u.id) === String(preselectVehicleId)) { + opt.selected = true; + hasSelected = true; + } + vehicleSelect.appendChild(opt); + }); + + if (hasSelected) { + onVehicleSelectChanged(vehicleSelect, prefix); + } else { + defaultPrompt.selected = true; + if (previewEl) previewEl.classList.add('hidden'); + } + } + + // 6. Handler saat Custom Lokasi Diketik + function onCustomLocationTyped(val, prefix) { + const finalLocation = document.getElementById(`${prefix}FinalLocation`); + if (finalLocation) { + finalLocation.value = val.trim(); + } + } + + // 7. Handler saat Unit Support Dipilih + function onVehicleSelectChanged(selectEl, prefix) { + const opt = selectEl.options[selectEl.selectedIndex]; + if (!opt || !opt.value) return; + + const code = opt.getAttribute('data-code') || ''; + const brand = opt.getAttribute('data-brand') || ''; + const lastKm = parseFloat(opt.getAttribute('data-km')) || 0; + const unitLocation = opt.getAttribute('data-location') || 'Site Kerinci'; + + // Tampilkan info preview const previewEl = document.getElementById(`${prefix}VehiclePreview`); const previewText = document.getElementById(`${prefix}VehiclePreviewText`); const lastKmText = document.getElementById(`${prefix}VehicleLastKmText`); - const hiddenId = document.getElementById(`${prefix}VehicleId`); - const meterTypeSelect = document.getElementById(`${prefix}MeterType`); - const startMeterInput = document.getElementById(`${prefix}StartMeter`); - - if (!val) { - if (previewEl) previewEl.classList.add('hidden'); - if (hiddenId) hiddenId.value = ''; - return; + if (previewEl) { + previewEl.classList.remove('hidden'); + previewText.textContent = `${code} - ${brand}`; + lastKmText.textContent = `Meter Terakhir: ${lastKm.toLocaleString('id-ID', {minimumFractionDigits: 1, maximumFractionDigits: 2})}`; } - const found = (window.unitSupportsData || []).find(u => - (u.code && u.code.toLowerCase() === val) || - (u.brand_type && u.brand_type.toLowerCase() === val) || - (u.code && u.code.toLowerCase().startsWith(val)) - ); + // Auto-isi start_meter jika kosong atau 0 + const startMeterInput = document.getElementById(`${prefix}StartMeter`); + if (startMeterInput && (!startMeterInput.value || parseFloat(startMeterInput.value) === 0)) { + startMeterInput.value = lastKm.toFixed(2); + } - if (found) { - if (hiddenId) hiddenId.value = found.id; - if (previewEl) { - previewEl.classList.remove('hidden'); - previewText.textContent = `${found.code} - ${found.brand_type || ''}`; - const lastKm = parseFloat(found.current_km) || 0; - lastKmText.textContent = `Meter Terakhir: ${lastKm.toLocaleString('id-ID', {minimumFractionDigits: 1, maximumFractionDigits: 2})}`; + // Auto-deteksi tipe meteran: DT -> KM, selainnya -> HM + const meterTypeSelect = document.getElementById(`${prefix}MeterType`); + if (meterTypeSelect) { + if (code.toUpperCase().startsWith('DT')) { + meterTypeSelect.value = 'KM'; + } else { + meterTypeSelect.value = 'HM'; } + updateMeterUnitLabels(prefix); + } - // Auto isi meter awal jika belum diisi atau bernilai 0 - if (startMeterInput && (!startMeterInput.value || parseFloat(startMeterInput.value) === 0)) { - startMeterInput.value = (parseFloat(found.current_km) || 0).toFixed(2); - } - - // Auto pilih tipe meter: jika diawali DT -> KM, selainnya -> HM - if (meterTypeSelect) { - if ((found.code || '').toUpperCase().startsWith('DT')) { - meterTypeSelect.value = 'KM'; - } else { - meterTypeSelect.value = 'HM'; - } - updateMeterUnitLabels(prefix); - } - } else { - if (previewEl) previewEl.classList.add('hidden'); - if (hiddenId) hiddenId.value = ''; + // Jika lokasi kerja dipilih 'ALL', sinkronkan finalLocation ke lokasi unit + const locSelect = document.getElementById(`${prefix}LocationSelect`); + const finalLoc = document.getElementById(`${prefix}FinalLocation`); + if (locSelect && locSelect.value === 'ALL' && finalLoc && !finalLoc.value) { + finalLoc.value = unitLocation; } } diff --git a/tests/Feature/QmaSupportTimesheetTest.php b/tests/Feature/QmaSupportTimesheetTest.php index 8c8bb40..3fc4b3f 100644 --- a/tests/Feature/QmaSupportTimesheetTest.php +++ b/tests/Feature/QmaSupportTimesheetTest.php @@ -413,4 +413,49 @@ class QmaSupportTimesheetTest extends TestCase 'application/pdf' )); } + + /** + * Halaman index memuat unitLocations dan merefleksikan posisi unit. + */ + public function test_index_passes_unit_locations_for_location_first_workflow(): void + { + $response = $this->actingAs($this->qmaAdmin) + ->get(route('qma.support_timesheet.index')); + + $response->assertStatus(200); + $response->assertViewHas('unitLocations'); + $response->assertSee('Tentukan Lokasi Dulu'); + $response->assertSee('Adit 1 (1 Unit Ready)'); + } + + /** + * Penyimpanan dengan lokasi custom atau normalisasi fallback. + */ + public function test_store_handles_custom_location_input(): void + { + $data = [ + 'vehicle_id' => $this->supportUnit->id, + 'operator_id' => $this->operator->id, + 'date' => Carbon::today()->toDateString(), + 'shift' => 'Shift Siang', + 'meter_type' => 'HM', + 'start_meter' => 1300.00, + 'location' => '__CUSTOM__', + 'custom_location' => 'Quarry Batu KM 12', + 'activity_type' => 'Galian Quarry', + 'status' => 'Operation', + ]; + + $response = $this->actingAs($this->qmaAdmin) + ->post(route('qma.support_timesheet.store'), $data); + + $response->assertRedirect(); + $this->assertDatabaseHas('unit_support_timesheets', [ + 'vehicle_id' => $this->supportUnit->id, + 'location' => 'Quarry Batu KM 12', + ]); + + $this->supportUnit->refresh(); + $this->assertEquals('Quarry Batu KM 12', $this->supportUnit->location); + } }