feat: initial commit Sistem RKM Laravel (HSE, QMA, Pos Gerbang, QR Activity)
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?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\HasMany;
|
||||
|
||||
class Employee extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'nik',
|
||||
'name',
|
||||
'unit_kerja',
|
||||
'division',
|
||||
'position',
|
||||
'is_operator',
|
||||
'simper_expired_at',
|
||||
'photo_path',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_operator' => 'boolean',
|
||||
'simper_expired_at' => 'date',
|
||||
];
|
||||
|
||||
/**
|
||||
* Cek apakah SIMPER masih aktif hari ini.
|
||||
*/
|
||||
public function hasValidSimper(): bool
|
||||
{
|
||||
if (!$this->simper_expired_at) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->simper_expired_at->isFuture() || $this->simper_expired_at->isToday();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope untuk mengambil hanya karyawan yang berstatus operator/driver.
|
||||
*/
|
||||
public function scopeOperators(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_operator', true);
|
||||
}
|
||||
|
||||
public function activities(): HasMany
|
||||
{
|
||||
return $this->hasMany(VehicleActivity::class, 'driver_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Gate extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function users(): HasMany
|
||||
{
|
||||
return $this->hasMany(User::class);
|
||||
}
|
||||
|
||||
public function activities(): HasMany
|
||||
{
|
||||
return $this->hasMany(VehicleActivity::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class SecurityGuard extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
|
||||
public function activities(): HasMany
|
||||
{
|
||||
return $this->hasMany(VehicleActivity::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'username',
|
||||
'email',
|
||||
'password',
|
||||
'role',
|
||||
'gate_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
public function gate(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Gate::class);
|
||||
}
|
||||
|
||||
public function isGateOfficer(): bool
|
||||
{
|
||||
return $this->role === 'gate_officer';
|
||||
}
|
||||
|
||||
public function isHseAdmin(): bool
|
||||
{
|
||||
return $this->role === 'hse_admin';
|
||||
}
|
||||
|
||||
public function isQmaAdmin(): bool
|
||||
{
|
||||
return $this->role === 'qma_admin';
|
||||
}
|
||||
|
||||
public function isPgAdmin(): bool
|
||||
{
|
||||
return $this->role === 'pg_admin';
|
||||
}
|
||||
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->role === 'admin';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class Vehicle extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'category',
|
||||
'brand_type',
|
||||
'license_plate',
|
||||
'ownership',
|
||||
'division',
|
||||
'status',
|
||||
'current_km',
|
||||
'photo_path',
|
||||
'unit_category',
|
||||
'unit_type',
|
||||
'brand',
|
||||
'model_number',
|
||||
'production_year',
|
||||
'location',
|
||||
'fuel_consumption',
|
||||
'silo_document',
|
||||
'stnk_document',
|
||||
'pajak_document',
|
||||
'kir_document',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'current_km' => 'decimal:2',
|
||||
'fuel_consumption' => 'decimal:2',
|
||||
'production_year' => 'integer',
|
||||
];
|
||||
|
||||
public function isLightVehicle(): bool
|
||||
{
|
||||
return $this->category === 'light_vehicle';
|
||||
}
|
||||
|
||||
public function isUnitSupport(): bool
|
||||
{
|
||||
return $this->category === 'unit_support';
|
||||
}
|
||||
|
||||
public function scopeLightVehicles(Builder $query): Builder
|
||||
{
|
||||
return $query->where('category', 'light_vehicle');
|
||||
}
|
||||
|
||||
public function scopeUnitSupports(Builder $query): Builder
|
||||
{
|
||||
return $query->where('category', 'unit_support');
|
||||
}
|
||||
|
||||
public function activities(): HasMany
|
||||
{
|
||||
return $this->hasMany(VehicleActivity::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktivitas gerbang terakhir dari kendaraan ini.
|
||||
*/
|
||||
public function latestActivity(): HasOne
|
||||
{
|
||||
return $this->hasOne(VehicleActivity::class)->latestOfMany('recorded_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tentukan arah gerbang berikutnya yang sah:
|
||||
* - 'ANY' jika belum pernah ada riwayat (kendaraan baru)
|
||||
* - 'in' jika aktivitas terakhir adalah 'out'
|
||||
* - 'out' jika aktivitas terakhir adalah 'in'
|
||||
*/
|
||||
public function determineNextAllowedDirection(): string
|
||||
{
|
||||
$last = $this->latestActivity;
|
||||
|
||||
if (!$last) {
|
||||
return 'ANY';
|
||||
}
|
||||
|
||||
return ($last->direction === 'out') ? 'in' : 'out';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?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\BelongsTo;
|
||||
|
||||
class VehicleActivity extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'transaction_no',
|
||||
'vehicle_id',
|
||||
'driver_id',
|
||||
'gate_id',
|
||||
'direction',
|
||||
'previous_km',
|
||||
'current_km',
|
||||
'distance',
|
||||
'purpose',
|
||||
'recorded_at',
|
||||
'security_guard_id',
|
||||
'validation_status',
|
||||
'rejection_note',
|
||||
'validated_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'previous_km' => 'decimal:2',
|
||||
'current_km' => 'decimal:2',
|
||||
'distance' => 'decimal:2',
|
||||
'recorded_at' => 'datetime',
|
||||
'validated_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function vehicle(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Vehicle::class);
|
||||
}
|
||||
|
||||
public function driver(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Employee::class, 'driver_id');
|
||||
}
|
||||
|
||||
public function gate(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Gate::class);
|
||||
}
|
||||
|
||||
public function securityGuard(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SecurityGuard::class, 'security_guard_id');
|
||||
}
|
||||
|
||||
public function scopePending(Builder $query): Builder
|
||||
{
|
||||
return $query->where('validation_status', 'pending');
|
||||
}
|
||||
|
||||
public function scopeApproved(Builder $query): Builder
|
||||
{
|
||||
return $query->where('validation_status', 'approved');
|
||||
}
|
||||
|
||||
public function scopeForGate(Builder $query, int $gateId): Builder
|
||||
{
|
||||
return $query->where('gate_id', $gateId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user