routes + route_stops tables. Admin CRUD with stops submitted as an ordered array (drag-reorder is client-side; sequence is array-index). Cloning produces an inactive copy with -COPY-XXXX suffix. RouteCalculator recomputes total_distance_km via ST_Distance_Sphere across stop coordinates + dumpsite, and estimated_duration_minutes from dwell time + travel time at config-driven avg speed (default 25 km/h). default_team_id stays nullable bigint until Module 9 adds the FK. 139 feature tests passing. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
75 lines
1.7 KiB
PHP
75 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Route extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
public const STATUS_ACTIVE = 'active';
|
|
public const STATUS_INACTIVE = 'inactive';
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'name',
|
|
'code',
|
|
'area_id',
|
|
'default_dumpsite_id',
|
|
'default_team_id',
|
|
'estimated_duration_minutes',
|
|
'total_distance_km',
|
|
'status',
|
|
'created_by_admin_id',
|
|
'notes',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'estimated_duration_minutes' => 'integer',
|
|
'total_distance_km' => 'float',
|
|
];
|
|
}
|
|
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'uuid';
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $r): void {
|
|
if (empty($r->uuid)) {
|
|
$r->uuid = (string) Str::uuid();
|
|
}
|
|
});
|
|
}
|
|
|
|
public function area(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ServiceArea::class, 'area_id');
|
|
}
|
|
|
|
public function defaultDumpsite(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Dumpsite::class, 'default_dumpsite_id');
|
|
}
|
|
|
|
public function createdBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by_admin_id');
|
|
}
|
|
|
|
public function stops(): HasMany
|
|
{
|
|
return $this->hasMany(RouteStop::class)->orderBy('sequence');
|
|
}
|
|
}
|