New-Thanasoft/thanasoft-back/database/seeders/InterventionSeeder.php
2026-04-29 09:15:58 +03:00

103 lines
4.0 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Client;
use App\Models\ClientLocation;
use App\Models\Deceased;
use App\Models\Intervention;
use App\Models\Product;
use App\Models\Thanatopractitioner;
use App\Models\User;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
class InterventionSeeder extends Seeder
{
public function run(): void
{
$faker = Faker::create('fr_FR');
$products = Product::query()
->whereHas('category', fn ($query) => $query->where('intervention', true))
->get();
$practitioners = Thanatopractitioner::query()->get();
$clients = Client::query()->limit(12)->get();
$creatorId = User::query()->value('id');
$types = ['thanatopraxie', 'toilette_mortuaire', 'exhumation', 'retrait_pacemaker', 'retrait_bijoux', 'autre'];
$statuses = ['demande', 'planifie', 'en_cours', 'termine', 'annule'];
if ($products->isEmpty() || $clients->isEmpty()) {
return;
}
foreach ($clients as $index => $client) {
$deceased = Deceased::updateOrCreate(
[
'last_name' => sprintf('Défunt Client %d', $client->id),
'first_name' => 'Dossier',
],
[
'birth_date' => now()->subYears(55 + $index)->subDays($index)->format('Y-m-d'),
'death_date' => now()->subDays($index + 1)->format('Y-m-d'),
'place_of_death' => $client->billing_city ?: 'Antananarivo',
'notes' => sprintf('Défunt de démonstration lié au client #%d pour les interventions seedées.', $client->id),
]
);
$location = ClientLocation::updateOrCreate(
[
'client_id' => $client->id,
'name' => 'Site principal',
],
[
'address_line1' => $client->billing_address_line1,
'address_line2' => $client->billing_address_line2,
'postal_code' => $client->billing_postal_code,
'city' => $client->billing_city,
'country_code' => $client->billing_country_code ?: 'FR',
'is_default' => true,
]
);
$intervention = Intervention::updateOrCreate(
[
'client_id' => $client->id,
'scheduled_at' => now()->subDays($index)->format('Y-m-d H:i:s'),
'type' => $types[$index % count($types)],
],
[
'deceased_id' => $deceased->id,
'order_giver' => $faker->name,
'location_id' => $location->id,
'product_id' => optional($products->get($index % $products->count()))->id,
'duration_min' => [60, 75, 90, 120][$index % 4],
'status' => $statuses[$index % count($statuses)],
'attachments_count' => 0,
'notes' => $faker->sentence,
'created_by' => $creatorId,
]
);
if ($practitioners->isNotEmpty()) {
$principal = $practitioners->get($index % $practitioners->count());
$assistant = $practitioners->get(($index + 1) % $practitioners->count());
$syncData = [
$principal->id => [
'role' => 'principal',
'assigned_at' => now()->subDays($index),
],
];
if ($assistant && $assistant->id !== $principal->id) {
$syncData[$assistant->id] = [
'role' => 'assistant',
'assigned_at' => now()->subDays($index)->addMinutes(10),
];
}
$intervention->practitioners()->sync($syncData);
}
}
}
}