feat(qma): implement unit position tracking, gate auto-sync, history log, and exports
This commit is contained in:
@@ -183,6 +183,11 @@ class User extends Authenticatable
|
||||
return null;
|
||||
}
|
||||
|
||||
public function locationHistories(): HasMany
|
||||
{
|
||||
return $this->hasMany(VehicleLocationHistory::class);
|
||||
}
|
||||
|
||||
public function isEmployee(): bool
|
||||
{
|
||||
if ($this->isSuperAdmin()) {
|
||||
|
||||
@@ -32,6 +32,10 @@ class Vehicle extends Model
|
||||
'model_number',
|
||||
'production_year',
|
||||
'location',
|
||||
'site_status',
|
||||
'location_updated_at',
|
||||
'location_updated_by',
|
||||
'location_note',
|
||||
'fuel_consumption',
|
||||
'silo_document',
|
||||
'stnk_document',
|
||||
@@ -44,12 +48,29 @@ class Vehicle extends Model
|
||||
'fuel_consumption' => 'decimal:2',
|
||||
'production_year' => 'integer',
|
||||
'parent_id' => 'integer',
|
||||
'location_updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'photo_url',
|
||||
'site_status_label',
|
||||
];
|
||||
|
||||
public function isInsideSite(): bool
|
||||
{
|
||||
return ($this->site_status ?? 'inside_site') === 'inside_site';
|
||||
}
|
||||
|
||||
public function isOutsideSite(): bool
|
||||
{
|
||||
return ($this->site_status ?? 'inside_site') === 'outside_site';
|
||||
}
|
||||
|
||||
public function getSiteStatusLabelAttribute(): string
|
||||
{
|
||||
return $this->isOutsideSite() ? 'Di Luar Site' : 'Di Dalam Site';
|
||||
}
|
||||
|
||||
public function isLightVehicle(): bool
|
||||
{
|
||||
return $this->category === 'light_vehicle';
|
||||
@@ -103,6 +124,35 @@ class Vehicle extends Model
|
||||
return $query->where('category', 'unit_support')->whereNotNull('parent_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope untuk seluruh unit operasional yang dapat dilacak posisinya:
|
||||
* - Seluruh Light Vehicle
|
||||
* - Seluruh Unit Support fisik (anak kategori) ATAU kategori tunggal yang belum memiliki anak unit
|
||||
*/
|
||||
public function scopeTrackableUnits(Builder $query): Builder
|
||||
{
|
||||
return $query->where(function (Builder $q) {
|
||||
$q->where('category', 'light_vehicle')
|
||||
->orWhere(function (Builder $sq) {
|
||||
$sq->where('category', 'unit_support')
|
||||
->where(function (Builder $sub) {
|
||||
$sub->whereNotNull('parent_id')
|
||||
->orWhereDoesntHave('units');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeInsideSite(Builder $query): Builder
|
||||
{
|
||||
return $query->where('site_status', 'inside_site');
|
||||
}
|
||||
|
||||
public function scopeOutsideSite(Builder $query): Builder
|
||||
{
|
||||
return $query->where('site_status', 'outside_site');
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Vehicle::class, 'parent_id');
|
||||
@@ -167,6 +217,16 @@ class Vehicle extends Model
|
||||
return $this->hasOne(VehicleActivity::class)->latestOfMany('recorded_at');
|
||||
}
|
||||
|
||||
public function locationHistories(): HasMany
|
||||
{
|
||||
return $this->hasMany(VehicleLocationHistory::class)->orderByDesc('recorded_at');
|
||||
}
|
||||
|
||||
public function latestLocationHistory(): HasOne
|
||||
{
|
||||
return $this->hasOne(VehicleLocationHistory::class)->latestOfMany('recorded_at');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tentukan arah gerbang berikutnya yang sah:
|
||||
* - 'ANY' jika belum pernah ada riwayat (kendaraan baru)
|
||||
|
||||
@@ -6,6 +6,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\HasMany;
|
||||
|
||||
class VehicleActivity extends Model
|
||||
{
|
||||
@@ -69,6 +70,11 @@ class VehicleActivity extends Model
|
||||
return $this->belongsTo(User::class, 'corrected_by_user_id');
|
||||
}
|
||||
|
||||
public function locationHistories(): HasMany
|
||||
{
|
||||
return $this->hasMany(VehicleLocationHistory::class, 'vehicle_activity_id');
|
||||
}
|
||||
|
||||
public function isPending(): bool
|
||||
{
|
||||
return $this->validation_status === 'pending';
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class VehicleLocationHistory extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'vehicle_id',
|
||||
'site_status',
|
||||
'location',
|
||||
'note',
|
||||
'source',
|
||||
'vehicle_activity_id',
|
||||
'user_id',
|
||||
'user_name',
|
||||
'recorded_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recorded_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function vehicle(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Vehicle::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function activity(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(VehicleActivity::class, 'vehicle_activity_id');
|
||||
}
|
||||
|
||||
public function isInsideSite(): bool
|
||||
{
|
||||
return $this->site_status === 'inside_site';
|
||||
}
|
||||
|
||||
public function isOutsideSite(): bool
|
||||
{
|
||||
return $this->site_status === 'outside_site';
|
||||
}
|
||||
|
||||
public function getSiteStatusLabelAttribute(): string
|
||||
{
|
||||
return $this->site_status === 'outside_site' ? 'Di Luar Site' : 'Di Dalam Site';
|
||||
}
|
||||
|
||||
public function getSourceLabelAttribute(): string
|
||||
{
|
||||
return $this->source === 'gate_crossing' ? 'Pos Gerbang (Otomatis)' : 'Manual QMA';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user