feat: support multiple vehicle category scopes selection in hse master simper types

This commit is contained in:
Ray1111-aa
2026-10-25 23:21:22 +07:00
parent 01152a4260
commit 58f1801810
8 changed files with 594 additions and 128 deletions
@@ -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<string>
*/
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.
*/
+1 -1
View File
@@ -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;
}
}
+89
View File
@@ -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<string>
*/
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);