feat(timesheet): dynamically filter operators by equipment SIMPER in QMA heavy equipment timesheet

This commit is contained in:
Ray1111-aa
2026-10-28 14:18:47 +07:00
parent 852404be32
commit 68ff69e8e4
5 changed files with 279 additions and 22 deletions
@@ -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'],
+31 -8
View File
@@ -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);
}