2026-05-11 13:30:24 +03:00

73 lines
2.3 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Avoir;
use App\Models\Invoice;
use Illuminate\Database\Seeder;
class AvoirSeeder extends Seeder
{
private const REASON_TYPES = [
'remboursement_total',
'remboursement_partiel',
'reduction',
'erreur_facturation',
'retour_marchandise',
'accord_commercial',
'autre',
];
public function run(): void
{
// Pick paid/emitted invoices to credit
$invoices = Invoice::whereIn('status', ['payee', 'emise', 'envoyee'])
->orderByRaw('RAND()')
->limit(22)
->get();
if ($invoices->isEmpty()) {
$this->command->warn('AvoirSeeder: aucune facture éligible trouvée — skip.');
return;
}
// Status pool: mostly emis / some applique
$statusPool = array_merge(
array_fill(0, 13, 'emis'),
array_fill(0, 9, 'applique')
);
shuffle($statusPool);
foreach ($invoices as $idx => $invoice) {
$reason = self::REASON_TYPES[array_rand(self::REASON_TYPES)];
$status = $statusPool[$idx % count($statusPool)];
$avoirDate = $invoice->invoice_date
->copy()
->addDays(rand(5, 60))
->format('Y-m-d');
// Partial or total credit
$fraction = ($reason === 'remboursement_total') ? 1.0 : round(rand(20, 80) / 100, 2);
$totalHt = round((float) $invoice->total_ht * $fraction, 2);
$totalTva = round($totalHt * 0.20, 2);
$totalTtc = round($totalHt + $totalTva, 2);
Avoir::create([
'client_id' => $invoice->client_id,
'invoice_id' => $invoice->id,
'status' => $status,
'avoir_date' => $avoirDate,
'currency' => 'EUR',
'total_ht' => $totalHt,
'total_tva' => $totalTva,
'total_ttc' => $totalTtc,
'reason_type' => $reason,
'reason_description' => null,
'refund_status' => 'non_rembourse',
]);
}
$this->command->info('AvoirSeeder: ' . $invoices->count() . ' avoirs créés.');
}
}