91 lines
3.5 KiB
PHP
91 lines
3.5 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();
|
|
$deceasedCollection = Deceased::query()->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() || $deceasedCollection->isEmpty() || $clients->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
foreach ($clients as $index => $client) {
|
|
$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' => optional($deceasedCollection->get($index % $deceasedCollection->count()))->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);
|
|
}
|
|
}
|
|
}
|
|
} |