namespace App\Jobs; use App\Models\TicketImport; use App\Models\TicketImportRaw; use App\Models\Product; use App\Models\ProductBatch; use App\Models\Ticket; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class TransformTicketImport implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $importId; public function __construct($importId) { $this->importId = $importId; } public function handle() { $import = TicketImport::findOrFail($this->importId); $raws = TicketImportRaw::where('ticket_import_id', $this->importId)->get(); foreach ($raws as $raw) { // 1️⃣ Vérification ou création du produit $product = Product::firstOrCreate( ['name' => $raw->product], // ['slug' => \Str::slug($raw->product)] ); // 2️⃣ Vérification ou création de la batch $batch = ProductBatch::firstOrCreate( ['product_id' => $product->id, 'name' => $raw->batch], [ 'serial_start' => $raw->serial_number, 'serial_end' => $raw->serial_number, // tu mettras à jour après aggregation 'total_tickets'=> 1, ] ); // 3️⃣ Créer le ticket Ticket::create([ 'product_id' => $product->id, 'batch_id' => $batch->id, 'serial_number' => $raw->serial_number, 'price' => $raw->price, 'type' => $raw->type, 'lot_cash' => $raw->lot_cash, 'lot_nature' => $raw->lot_nature, 'discount_rate' => $raw->discount_rate, 'commission' => $raw->commission, 'is_winner' => $raw->is_winner, 'payment_date' => $raw->payment_date, ]); } // Ici tu peux faire un update pour les serial_end et total_tickets par batch si besoin } }