Files
Verde-Web/database/seeders/DevelopmentSeeder.php

260 lines
10 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Barangay;
use App\Models\CollectionTeam;
use App\Models\DriverProfile;
use App\Models\DropOffPoint;
use App\Models\Dumpsite;
use App\Models\HelperProfile;
use App\Models\Household;
use App\Models\NotificationPreference;
use App\Models\PartnerStore;
use App\Models\ResidentProfile;
use App\Models\Route as CollectionRoute;
use App\Models\RouteStop;
use App\Models\ScannerProfile;
use App\Models\StoreInventory;
use App\Models\StorePartnerProfile;
use App\Models\TeamMember;
use App\Models\Tenant;
use App\Models\Trip;
use App\Models\TripStop;
use App\Models\Truck;
use App\Models\User;
use App\Tenancy\Tenancy;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use MatanYadaev\EloquentSpatial\Objects\Point;
class DevelopmentSeeder extends Seeder
{
public function run(): void
{
$password = Hash::make('password1');
// 1. Exactly 1 Super Admin
User::firstOrCreate(
['email' => 'super@verde.local'],
[
'phone' => '+639000000000',
'password' => $password,
'first_name' => 'System',
'last_name' => 'Super Admin',
'role' => 'super_admin',
'status' => User::STATUS_ACTIVE,
'tenant_id' => null,
'email_verified_at' => now(),
]
)->syncRoles(['super_admin']);
// 2. Three LGUs (Tenants)
$lguConfigs = [
['code' => 'SPB', 'name' => 'San Pascual, Batangas'],
['code' => 'BAU', 'name' => 'Bauan, Batangas'],
['code' => 'LEM', 'name' => 'Lemery, Batangas'],
];
foreach ($lguConfigs as $index => $config) {
$tenant = Tenant::firstOrCreate(
['code' => $config['code']],
[
'name' => $config['name'],
'status' => Tenant::STATUS_ACTIVE,
'qr_retail_price_centavos' => 1000,
'uuid' => (string) Str::uuid(),
]
);
Tenancy::setCurrent($tenant);
$this->seedTenantData($tenant, $password, $index);
}
}
private function seedTenantData(Tenant $tenant, string $password, int $lguIndex): void
{
// NO LGU Admin seeded as requested
// 1. Infrastructure (3 Dumpsites, 5 DOPs)
$barangay = Barangay::where('name', 'like', '%Poblacion%')->first() ?? Barangay::first();
$dumpsites = [];
for ($i = 1; $i <= 3; $i++) {
$dumpsites[] = Dumpsite::create([
'name' => "{$tenant->code} Landfill {$i}",
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'code' => "DS-{$tenant->code}-00{$i}",
'address_line' => "Zone {$i}",
'coordinates' => new Point(14.67 + ($i * 0.01), 121.08, 4326),
]);
}
$dops = [];
for ($i = 1; $i <= 5; $i++) {
$dops[] = DropOffPoint::create([
'name' => "{$tenant->code} Plaza {$i}",
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'code' => "DOP-{$tenant->code}-00{$i}",
'barangay_id' => $barangay->id,
'address_line' => "Point {$i}",
'coordinates' => new Point(14.65 + ($i * 0.01), 121.06, 4326),
]);
}
// 2. Staff & Residents (10 each)
$drivers = $this->seedStaff($tenant, $password, $lguIndex, 'driver', 10);
$scanners = $this->seedStaff($tenant, $password, $lguIndex, 'scanner', 10);
$helpers = $this->seedStaff($tenant, $password, $lguIndex, 'helper', 10);
$residents = $this->seedStaff($tenant, $password, $lguIndex, 'resident', 10);
// 3. Households (10 per LGU)
foreach ($residents as $i => $resident) {
ResidentProfile::create(['user_id' => $resident->id, 'verification_status' => 'approved', 'verified_at' => now()]);
Household::create([
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'name' => "{$resident->last_name} Household",
'address_line' => "Street {$i}",
'barangay_id' => $barangay->id,
'resident_user_id' => $resident->id,
'assigned_drop_off_point_id' => $dops[rand(0, 4)]->id,
]);
}
// 4. Store Admins (5 per LGU)
for ($i = 1; $i <= 5; $i++) {
$owner = $this->createStaff("store{$i}.{$tenant->code}@verde.local", "+63921{$lguIndex}000{$i}", "Store", "Owner {$i}", User::ROLE_STORE_PARTNER, $tenant, $password);
StorePartnerProfile::create(['user_id' => $owner->id, 'business_name' => "Store {$i} - {$tenant->code}", 'verification_status' => 'approved', 'verified_at' => now()]);
$store = PartnerStore::create([
'owner_user_id' => $owner->id,
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'business_name' => "Store {$i} - {$tenant->code}",
'business_permit_number' => "BP-{$tenant->code}-{$i}",
'address_line' => "Market St {$i}",
'barangay_id' => $barangay->id,
'coordinates' => new Point(14.651 + ($i * 0.001), 121.061, 4326),
'status' => PartnerStore::STATUS_ACTIVE,
]);
StoreInventory::create(['store_id' => $store->id, 'current_code_balance' => 500]);
}
// 5. Operations (5 Trucks, 5 Teams, 5 Routes, mixed Trips)
for ($i = 0; $i < 5; $i++) {
$truck = Truck::create([
'plate_number' => "VRD-{$tenant->code}-{$i}",
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'capacity_kg' => 3000,
'status' => Truck::STATUS_ACTIVE,
]);
$team = CollectionTeam::create([
'name' => "{$tenant->code} Team " . ($i + 1),
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'driver_id' => $drivers[$i]->id,
'scanner_id' => $scanners[$i]->id,
'truck_id' => $truck->id,
'status' => CollectionTeam::STATUS_ACTIVE,
]);
TeamMember::create(['team_id' => $team->id, 'user_id' => $helpers[$i]->id, 'role_in_team' => 'helper', 'assigned_from' => now()]);
$route = CollectionRoute::create([
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'code' => "RT-{$tenant->code}-{$i}",
'name' => "{$tenant->code} Route " . ($i + 1),
'default_dumpsite_id' => $dumpsites[rand(0, 2)]->id,
'default_team_id' => $team->id,
'status' => CollectionRoute::STATUS_ACTIVE,
]);
RouteStop::create(['route_id' => $route->id, 'drop_off_point_id' => $dops[rand(0, 4)]->id, 'sequence' => 1]);
$this->seedTrips($tenant, $route, $team, $truck, $dumpsites[rand(0, 2)], $dops);
}
}
private function seedStaff($tenant, $password, $lguIndex, $role, $count): array
{
$users = [];
$prefix = [
'driver' => '918',
'scanner' => '920',
'helper' => '919',
'resident' => '945'
][$role];
for ($i = 1; $i <= $count; $i++) {
$user = $this->createStaff("{$role}{$i}.{$tenant->code}@verde.local", "+63{$prefix}{$lguIndex}000{$i}", ucfirst($role), "{$i} ({$tenant->code})", "{$role}", $tenant, $password);
if ($role === 'driver') DriverProfile::create(['user_id' => $user->id, 'license_number' => "L-{$tenant->code}-{$i}", 'verification_status' => 'approved', 'verified_at' => now()]);
if ($role === 'scanner') ScannerProfile::create(['user_id' => $user->id, 'verification_status' => 'approved', 'verified_at' => now()]);
if ($role === 'helper') HelperProfile::create(['user_id' => $user->id, 'verification_status' => 'approved', 'verified_at' => now()]);
$users[] = $user;
}
return $users;
}
private function seedTrips($tenant, $route, $team, $truck, $dumpsite, $dops): void
{
$scenarios = [
['date' => now(), 'status' => Trip::STATUS_IN_PROGRESS],
['date' => now()->subDay(), 'status' => Trip::STATUS_COMPLETED],
['date' => now()->subMonth(), 'status' => Trip::STATUS_COMPLETED],
];
foreach ($scenarios as $scenario) {
$isComp = $scenario['status'] === Trip::STATUS_COMPLETED;
$trip = Trip::create([
'uuid' => (string) Str::uuid(),
'tenant_id' => $tenant->id,
'trip_number' => "TRIP-{$tenant->code}-" . $scenario['date']->format('YmdHi') . rand(10, 99),
'route_id' => $route->id,
'team_id' => $team->id,
'truck_id' => $truck->id,
'dumpsite_id' => $dumpsite->id,
'scheduled_date' => $scenario['date']->format('Y-m-d'),
'status' => $scenario['status'],
'current_load_kg' => $isComp ? rand(1000, 2500) : 0,
'actual_start_time' => $scenario['date']->copy()->setHour(8),
'actual_end_time' => $isComp ? $scenario['date']->copy()->setHour(12) : null,
]);
foreach ($dops as $idx => $dop) {
TripStop::create([
'trip_id' => $trip->id,
'drop_off_point_id' => $dop->id,
'sequence' => $idx + 1,
'status' => $isComp ? 'completed' : 'pending',
'total_scans' => $isComp ? rand(10, 30) : 0,
]);
}
}
}
private function createStaff($email, $phone, $first, $last, $role, $tenant, $password): User
{
$u = User::create([
'email' => $email,
'phone' => $phone,
'password' => $password,
'first_name' => $first,
'last_name' => $last,
'role' => $role,
'status' => User::STATUS_ACTIVE,
'tenant_id' => $tenant ? $tenant->id : null,
'email_verified_at' => now(),
]);
$u->syncRoles([$role]);
NotificationPreference::firstOrCreate(['user_id' => $u->id]);
return $u;
}
}