feat: implement flexible hse simper types, multi-simper personil, and mobile heavy timesheet integration
This commit is contained in:
@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Employee extends Model
|
||||
@@ -73,6 +74,102 @@ class Employee extends Model
|
||||
return $this->simper_expired_at->isFuture() || $this->simper_expired_at->isToday();
|
||||
}
|
||||
|
||||
public function simpers(): HasMany
|
||||
{
|
||||
return $this->hasMany(EmployeeSimper::class)->with('simperType');
|
||||
}
|
||||
|
||||
public function simperTypes(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(SimperType::class, 'employee_simpers')
|
||||
->withPivot(['simper_number', 'expired_at', 'status', 'notes'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cek apakah personil memiliki SIMPER aktif untuk kode jenis SIMPER tertentu.
|
||||
*/
|
||||
public function hasValidSimperForType(string $code): bool
|
||||
{
|
||||
$hasAnySimper = $this->relationLoaded('simpers') ? $this->simpers->isNotEmpty() : $this->simpers()->exists();
|
||||
|
||||
// Jika belum ada record di employee_simpers, gunakan fallback kolom simper_expired_at
|
||||
if (! $hasAnySimper) {
|
||||
return $this->hasValidSimper();
|
||||
}
|
||||
|
||||
$simpers = $this->relationLoaded('simpers') ? $this->simpers : $this->simpers()->with('simperType')->get();
|
||||
|
||||
foreach ($simpers as $s) {
|
||||
if ($s->simperType && $s->simperType->code === $code && $s->isValid()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cek apakah personil memiliki SIMPER dengan cakupan scope tertentu.
|
||||
*/
|
||||
public function hasValidSimperForScope(string $scope): bool
|
||||
{
|
||||
$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()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cek apakah personil memiliki izin SIMPER yang sah untuk kendaraan tertentu.
|
||||
*/
|
||||
public function hasValidSimperForVehicle(Vehicle $vehicle): bool
|
||||
{
|
||||
if (! $this->is_operator) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hasAnySimper = $this->relationLoaded('simpers') ? $this->simpers->isNotEmpty() : $this->simpers()->exists();
|
||||
|
||||
// Fallback jika belum dimigrasikan ke employee_simpers
|
||||
if (! $hasAnySimper) {
|
||||
return $this->hasValidSimper();
|
||||
}
|
||||
|
||||
// 1. Jika kendaraan mensyaratkan spesifik required_simper_type_id
|
||||
if ($vehicle->required_simper_type_id) {
|
||||
$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()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. Berdasarkan kategori kendaraan
|
||||
if ($vehicle->isLightVehicle()) {
|
||||
return $this->hasValidSimperForType('SIMPER-LV') || $this->hasValidSimperForScope(SimperType::SCOPE_ALL);
|
||||
}
|
||||
|
||||
if ($vehicle->isUnitSupport() && $vehicle->is_timesheet_mobile) {
|
||||
return $this->hasValidSimperForType('SIMPER-DT')
|
||||
|| $this->hasValidSimperForType('SIMPER-HEAVY')
|
||||
|| $this->hasValidSimperForScope(SimperType::SCOPE_ALL);
|
||||
}
|
||||
|
||||
if ($vehicle->isUnitSupport()) {
|
||||
return $this->hasValidSimperForType('SIMPER-HEAVY')
|
||||
|| $this->hasValidSimperForType('SIMPER-DT')
|
||||
|| $this->hasValidSimperForScope(SimperType::SCOPE_ALL);
|
||||
}
|
||||
|
||||
return $this->hasValidSimper();
|
||||
}
|
||||
|
||||
public function getPhotoUrlAttribute(): ?string
|
||||
{
|
||||
$path = $this->photo_path;
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class EmployeeSimper extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const STATUS_ACTIVE = 'active';
|
||||
public const STATUS_EXPIRED = 'expired';
|
||||
public const STATUS_SUSPENDED = 'suspended';
|
||||
|
||||
protected $fillable = [
|
||||
'employee_id',
|
||||
'simper_type_id',
|
||||
'simper_number',
|
||||
'expired_at',
|
||||
'status',
|
||||
'notes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'expired_at' => 'date',
|
||||
];
|
||||
|
||||
public function employee(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Employee::class);
|
||||
}
|
||||
|
||||
public function simperType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SimperType::class);
|
||||
}
|
||||
|
||||
public function isValid(): bool
|
||||
{
|
||||
if ($this->status !== self::STATUS_ACTIVE || ! $this->expired_at) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->expired_at->isFuture() || $this->expired_at->isToday();
|
||||
}
|
||||
|
||||
public function scopeActive(Builder $query): Builder
|
||||
{
|
||||
return $query->where('status', self::STATUS_ACTIVE);
|
||||
}
|
||||
|
||||
public function scopeValid(Builder $query): Builder
|
||||
{
|
||||
$today = Carbon::today()->toDateString();
|
||||
|
||||
return $query->where('status', self::STATUS_ACTIVE)
|
||||
->whereNotNull('expired_at')
|
||||
->where('expired_at', '>=', $today);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class SimperType extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const SCOPE_LIGHT_VEHICLE = 'light_vehicle';
|
||||
public const SCOPE_UNIT_SUPPORT_MOBILE = 'unit_support_mobile';
|
||||
public const SCOPE_UNIT_SUPPORT_HEAVY = 'unit_support_heavy';
|
||||
public const SCOPE_ALL = 'all';
|
||||
|
||||
public const SCOPES = [
|
||||
self::SCOPE_LIGHT_VEHICLE => 'Armada Ringan (Light Vehicle)',
|
||||
self::SCOPE_UNIT_SUPPORT_MOBILE => 'Armada Berat Bergerak (Heavy Mobile / Dump Truck)',
|
||||
self::SCOPE_UNIT_SUPPORT_HEAVY => 'Alat Berat Operasional (Statis / Tambang)',
|
||||
self::SCOPE_ALL => 'Semua Kategori Armada',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'category_scope',
|
||||
'description',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function employeeSimpers(): HasMany
|
||||
{
|
||||
return $this->hasMany(EmployeeSimper::class);
|
||||
}
|
||||
|
||||
public function employees(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Employee::class, 'employee_simpers')
|
||||
->withPivot(['simper_number', 'expired_at', 'status', 'notes'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function scopeActive(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_active', true);
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,8 @@ class Vehicle extends Model
|
||||
'production_year',
|
||||
'location',
|
||||
'site_status',
|
||||
'is_timesheet_mobile',
|
||||
'required_simper_type_id',
|
||||
'location_updated_at',
|
||||
'location_updated_by',
|
||||
'location_note',
|
||||
@@ -48,6 +50,7 @@ class Vehicle extends Model
|
||||
'fuel_consumption' => 'decimal:2',
|
||||
'production_year' => 'integer',
|
||||
'parent_id' => 'integer',
|
||||
'is_timesheet_mobile' => 'boolean',
|
||||
'location_updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
@@ -76,6 +79,32 @@ class Vehicle extends Model
|
||||
return $this->category === 'light_vehicle';
|
||||
}
|
||||
|
||||
public function isTimesheetMobile(): bool
|
||||
{
|
||||
return (bool) ($this->is_timesheet_mobile ?? false);
|
||||
}
|
||||
|
||||
public function requiredSimperType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SimperType::class, 'required_simper_type_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope unit yang diizinkan untuk formulir timesheet mobilisasi gerbang:
|
||||
* - Seluruh Light Vehicle (LV)
|
||||
* - Unit Support yang ditandai sebagai Timesheet Mobile (Dump Truck, dsb.)
|
||||
*/
|
||||
public function scopeTimesheetEligible(Builder $query): Builder
|
||||
{
|
||||
return $query->where(function (Builder $q) {
|
||||
$q->where('category', 'light_vehicle')
|
||||
->orWhere(function (Builder $sq) {
|
||||
$sq->where('category', 'unit_support')
|
||||
->where('is_timesheet_mobile', true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function getPhotoUrlAttribute(): ?string
|
||||
{
|
||||
if (!$this->photo_path) {
|
||||
|
||||
Reference in New Issue
Block a user