Backend models + DB: - Adds tenant_id to 13 more parent tables: service_areas, routes, collection_teams, trucks, trips, qr_code_batches, qr_codes, collection_logs, dumpsite_releases, payments, daily_collection_stats, weekly_route_performance, monthly_store_sales. Child tables (route_stops, trip_stops, team_members, truck_location_history, etc.) inherit scoping via their parent relation — they aren't queried directly in production code. - HasTenant trait applied to all 13 models, plus tenant_id added to each fillable. Creating any one of these now auto-fills tenant_id from Tenancy::current(). - BatchGenerator's bulk QrCode insert was bypassing model events (and therefore the HasTenant creating hook). Now sets tenant_id inline from the just-created batch's tenant_id, which itself goes through the trait. Fixes "0 codes returned for resident in same tenant" regression. Backfill: - SanPascualTenantSeeder extended to backfill all 18 tenant-aware tables. Idempotent — only updates rows where tenant_id is null. Production live data: backfilled 1 route, 1 team, 1 truck, 3 trips, 1 batch, 200 codes, 5 collection logs. Tests: - TestCase base now creates a default "TEST-LGU" tenant in setUp, sets it as Tenancy::current(), and attaches X-Tenant-Code on every JSON request. Tear-down clears the tenant. - UserFactory defaults tenant_id to Tenancy::current()->id, so factory-created users land in the test tenant. - Net result: 202 tests still passing, no per-test changes required. Phase A scope (5 tables) + Phase B scope (13 tables) = 18 of the ~22 tenant-aware tables. The remaining child tables (route_stops, trip_stops, team_members, truck_location_history, household_members, trip_timeline_events, store_purchases, store_sales) are scoped implicitly through their parent. What's still un-tenant-aware: - Admin web (Blade-based) — single-tenant context for now - Driver/scanner endpoints — they currently rely on the bearer token's user.tenant_id, which works correctly via ResolveTenant's fallback. No action needed. - Super-admin onboarding UI — Phase C. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
105 lines
3.1 KiB
PHP
105 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Tenancy\HasTenant;
|
|
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;
|
|
use Spatie\Activitylog\LogOptions;
|
|
use Spatie\Activitylog\Traits\LogsActivity;
|
|
|
|
class Trip extends Model
|
|
{
|
|
use HasFactory, HasTenant, LogsActivity, SoftDeletes;
|
|
|
|
public const STATUS_SCHEDULED = 'scheduled';
|
|
public const STATUS_IN_PROGRESS = 'in_progress';
|
|
public const STATUS_AT_DUMPSITE = 'at_dumpsite';
|
|
public const STATUS_COMPLETED = 'completed';
|
|
public const STATUS_CANCELLED = 'cancelled';
|
|
|
|
protected $fillable = [
|
|
'uuid', 'tenant_id', 'trip_number', 'route_id', 'team_id', 'truck_id', 'dumpsite_id',
|
|
'scheduled_date', 'scheduled_start_time',
|
|
'actual_start_time', 'actual_end_time',
|
|
'dumpsite_arrival_time', 'dumpsite_departure_time',
|
|
'status', 'total_load_kg', 'notes', 'created_by_admin_id',
|
|
];
|
|
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'scheduled_date' => 'date',
|
|
'actual_start_time' => 'datetime',
|
|
'actual_end_time' => 'datetime',
|
|
'dumpsite_arrival_time' => 'datetime',
|
|
'dumpsite_departure_time' => 'datetime',
|
|
];
|
|
}
|
|
|
|
public function getRouteKeyName(): string
|
|
{
|
|
return 'uuid';
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logOnly(['status', 'team_id', 'truck_id', 'scheduled_date', 'total_load_kg'])
|
|
->logOnlyDirty()
|
|
->dontSubmitEmptyLogs()
|
|
->useLogName('trip');
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $t): void {
|
|
if (empty($t->uuid)) $t->uuid = (string) Str::uuid();
|
|
if (empty($t->trip_number)) {
|
|
$date = ($t->scheduled_date ? \Carbon\Carbon::parse($t->scheduled_date) : now())->format('Ymd');
|
|
$seq = self::where('trip_number', 'like', "TRIP-{$date}-%")->count() + 1;
|
|
$t->trip_number = sprintf('TRIP-%s-%03d', $date, $seq);
|
|
}
|
|
});
|
|
}
|
|
|
|
public function route(): BelongsTo
|
|
{
|
|
return $this->belongsTo(\App\Models\Route::class);
|
|
}
|
|
|
|
public function team(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CollectionTeam::class, 'team_id');
|
|
}
|
|
|
|
public function truck(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Truck::class);
|
|
}
|
|
|
|
public function dumpsite(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Dumpsite::class);
|
|
}
|
|
|
|
public function stops(): HasMany
|
|
{
|
|
return $this->hasMany(TripStop::class)->orderBy('sequence');
|
|
}
|
|
|
|
public function timelineEvents(): HasMany
|
|
{
|
|
return $this->hasMany(TripTimelineEvent::class)->orderBy('event_at');
|
|
}
|
|
|
|
public function dumpsiteReleases(): HasMany
|
|
{
|
|
return $this->hasMany(DumpsiteRelease::class);
|
|
}
|
|
}
|