From 58f18018109c166455c3c4e02541add0e8bb5881 Mon Sep 17 00:00:00 2001 From: Ray1111-aa Date: Sun, 25 Oct 2026 23:21:22 +0700 Subject: [PATCH] feat: support multiple vehicle category scopes selection in hse master simper types --- .../Controllers/Hse/SimperTypeController.php | 59 ++- app/Models/Employee.php | 2 +- app/Models/SimperType.php | 89 ++++ ...er_simper_types_category_scope_to_text.php | 34 ++ resources/views/hse/employees_edit.blade.php | 14 +- resources/views/hse/employees_index.blade.php | 8 +- .../views/hse/simper_types_index.blade.php | 424 +++++++++++++----- tests/Feature/HseSimperTypeTest.php | 92 ++++ 8 files changed, 594 insertions(+), 128 deletions(-) create mode 100644 database/migrations/2026_10_25_232000_alter_simper_types_category_scope_to_text.php diff --git a/app/Http/Controllers/Hse/SimperTypeController.php b/app/Http/Controllers/Hse/SimperTypeController.php index 3d17f24..4aaf740 100644 --- a/app/Http/Controllers/Hse/SimperTypeController.php +++ b/app/Http/Controllers/Hse/SimperTypeController.php @@ -45,22 +45,31 @@ class SimperTypeController extends Controller */ public function store(Request $request): RedirectResponse { + $scopes = $this->resolveScopesInput($request); + $request->merge(['category_scopes' => $scopes]); + $validScopes = array_keys(SimperType::getAvailableScopes()); $validated = $request->validate([ 'code' => ['required', 'string', 'max:50', 'unique:simper_types,code'], 'name' => ['required', 'string', 'max:100'], - 'category_scope' => ['required', 'string', Rule::in($validScopes)], + 'category_scopes' => ['required', 'array', 'min:1'], + 'category_scopes.*' => ['required', 'string', Rule::in($validScopes)], 'description' => ['nullable', 'string', 'max:500'], 'is_active' => ['nullable', 'boolean'], + ], [ + 'category_scopes.required' => 'Pilih minimal satu cakupan kategori kendaraan.', + 'category_scopes.min' => 'Pilih minimal satu cakupan kategori kendaraan.', ]); $code = strtoupper(trim($validated['code'])); + $finalScopes = $validated['category_scopes']; + $storedScope = count($finalScopes) === 1 ? $finalScopes[0] : json_encode($finalScopes); SimperType::create([ 'code' => $code, 'name' => trim($validated['name']), - 'category_scope' => $validated['category_scope'], + 'category_scope' => $storedScope, 'description' => $validated['description'] ?? null, 'is_active' => $request->boolean('is_active', true), ]); @@ -74,20 +83,31 @@ class SimperTypeController extends Controller public function update(Request $request, int $id): RedirectResponse { $simperType = SimperType::findOrFail($id); + $scopes = $this->resolveScopesInput($request); + $request->merge(['category_scopes' => $scopes]); + $validScopes = array_keys(SimperType::getAvailableScopes()); $validated = $request->validate([ 'code' => ['required', 'string', 'max:50', Rule::unique('simper_types', 'code')->ignore($simperType->id)], 'name' => ['required', 'string', 'max:100'], - 'category_scope' => ['required', 'string', Rule::in($validScopes)], + 'category_scopes' => ['required', 'array', 'min:1'], + 'category_scopes.*' => ['required', 'string', Rule::in($validScopes)], 'description' => ['nullable', 'string', 'max:500'], 'is_active' => ['nullable', 'boolean'], + ], [ + 'category_scopes.required' => 'Pilih minimal satu cakupan kategori kendaraan.', + 'category_scopes.min' => 'Pilih minimal satu cakupan kategori kendaraan.', ]); + $code = strtoupper(trim($validated['code'])); + $finalScopes = $validated['category_scopes']; + $storedScope = count($finalScopes) === 1 ? $finalScopes[0] : json_encode($finalScopes); + $simperType->update([ - 'code' => strtoupper(trim($validated['code'])), + 'code' => $code, 'name' => trim($validated['name']), - 'category_scope' => $validated['category_scope'], + 'category_scope' => $storedScope, 'description' => $validated['description'] ?? null, 'is_active' => $request->boolean('is_active', true), ]); @@ -95,6 +115,35 @@ class SimperTypeController extends Controller return back()->with('success', "Jenis SIMPER '{$simperType->name}' berhasil diperbarui."); } + /** + * Helper normalisasi input cakupan kategori kendaraan. + * Mendukung multiple array (category_scopes[]) maupun legacy single string (category_scope). + * + * @return array + */ + private function resolveScopesInput(Request $request): array + { + $raw = $request->input('category_scopes') ?? $request->input('category_scope'); + + if (is_string($raw)) { + $trimmed = trim($raw); + if (str_starts_with($trimmed, '[') && str_ends_with($trimmed, ']')) { + $decoded = json_decode($trimmed, true); + if (is_array($decoded)) { + $raw = $decoded; + } else { + $raw = [$trimmed]; + } + } else { + $raw = [$trimmed]; + } + } elseif (!is_array($raw)) { + $raw = []; + } + + return array_values(array_filter(array_unique($raw))); + } + /** * Hapus jenis SIMPER. */ diff --git a/app/Models/Employee.php b/app/Models/Employee.php index 3c18a6c..048cf00 100644 --- a/app/Models/Employee.php +++ b/app/Models/Employee.php @@ -116,7 +116,7 @@ class Employee extends Model { $simpers = $this->relationLoaded('simpers') ? $this->simpers : $this->simpers()->with('simperType')->get(); foreach ($simpers as $s) { - if ($s->simperType && $s->simperType->category_scope === $scope && $s->isValid()) { + if ($s->simperType && $s->simperType->coversScope($scope) && $s->isValid()) { return true; } } diff --git a/app/Models/SimperType.php b/app/Models/SimperType.php index 0046e53..a407e92 100644 --- a/app/Models/SimperType.php +++ b/app/Models/SimperType.php @@ -94,6 +94,95 @@ class SimperType extends Model 'is_active' => 'boolean', ]; + protected $appends = [ + 'scopes', + 'category_scope_display', + ]; + + /** + * Dapatkan seluruh cakupan kategori dalam bentuk array terurai. + * + * @return array + */ + public function getScopesAttribute(): array + { + $raw = $this->category_scope; + + if (empty($raw)) { + return [self::SCOPE_ALL]; + } + + if (is_array($raw)) { + return array_values(array_unique($raw)); + } + + if (is_string($raw)) { + $trimmed = trim($raw); + if (str_starts_with($trimmed, '[') && str_ends_with($trimmed, ']')) { + $decoded = json_decode($trimmed, true); + if (is_array($decoded)) { + return array_values(array_unique($decoded)); + } + } + if (str_contains($trimmed, ',')) { + return array_values(array_filter(array_map('trim', explode(',', $trimmed)))); + } + return [$trimmed]; + } + + return [(string) $raw]; + } + + /** + * Cek apakah jenis SIMPER ini mencakup scope target tertentu. + */ + public function coversScope(string $targetScope): bool + { + $scopes = $this->scopes; + + // Universal scope mencakup semua kategori + if (in_array(self::SCOPE_ALL, $scopes, true) || in_array('all', $scopes, true)) { + return true; + } + + if (in_array($targetScope, $scopes, true)) { + return true; + } + + // Bandingkan case-insensitive + foreach ($scopes as $s) { + if (strcasecmp($s, $targetScope) === 0) { + return true; + } + } + + return false; + } + + /** + * Teks ringkas representasi cakupan kendaraan untuk badge UI. + */ + public function getCategoryScopeDisplayAttribute(): string + { + $scopes = $this->scopes; + + if (empty($scopes) || in_array(self::SCOPE_ALL, $scopes, true)) { + return 'Semua Kategori (Universal)'; + } + + if (count($scopes) === 1 && $scopes[0] === self::SCOPE_LIGHT_VEHICLE) { + return 'Light Vehicle (LV)'; + } + + $formatted = array_map(function ($s) { + if ($s === self::SCOPE_LIGHT_VEHICLE) return 'LV'; + if ($s === self::SCOPE_ALL) return 'Universal'; + return str_replace('_', ' ', $s); + }, $scopes); + + return implode(', ', $formatted); + } + public function employeeSimpers(): HasMany { return $this->hasMany(EmployeeSimper::class); diff --git a/database/migrations/2026_10_25_232000_alter_simper_types_category_scope_to_text.php b/database/migrations/2026_10_25_232000_alter_simper_types_category_scope_to_text.php new file mode 100644 index 0000000..8577276 --- /dev/null +++ b/database/migrations/2026_10_25_232000_alter_simper_types_category_scope_to_text.php @@ -0,0 +1,34 @@ +dropIndex('simper_types_category_scope_index'); + } catch (\Throwable $e) { + // Ignore jika indeks tidak ada + } + $table->text('category_scope')->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('simper_types', function (Blueprint $table) { + $table->string('category_scope', 50)->change(); + $table->index('category_scope'); + }); + } +}; diff --git a/resources/views/hse/employees_edit.blade.php b/resources/views/hse/employees_edit.blade.php index 958cb03..e68d631 100644 --- a/resources/views/hse/employees_edit.blade.php +++ b/resources/views/hse/employees_edit.blade.php @@ -180,17 +180,17 @@ - @if($st->category_scope === 'light_vehicle') - - LV - - @elseif($st->category_scope === 'all') + @if($st->coversScope('all')) SEMUA ARMADA + @elseif($st->scopes === ['light_vehicle']) + + LV + @else - - {{ strtoupper($st->category_scope) }} + + {{ strtoupper($st->category_scope_display) }} @endif diff --git a/resources/views/hse/employees_index.blade.php b/resources/views/hse/employees_index.blade.php index ed3942e..82b95c7 100644 --- a/resources/views/hse/employees_index.blade.php +++ b/resources/views/hse/employees_index.blade.php @@ -473,8 +473,8 @@ class="w-4 h-4 rounded text-emerald-700 border-slate-300 focus:ring-emerald-500 flex-shrink-0"> {{ $st->name }} ({{ $st->code }}) - - {{ str_replace('_', ' ', $st->category_scope) }} + + {{ $st->category_scope_display }}