feat(qma): implement unit position tracking, gate auto-sync, history log, and exports

This commit is contained in:
Ray1111-aa
2026-10-20 20:15:24 +07:00
parent e45f6ce581
commit 1590c5f1c6
11 changed files with 2133 additions and 3 deletions
+60
View File
@@ -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)