$faker->name, 'phone' => $faker->phoneNumber, 'email' => $faker->unique()->safeEmail, 'address' => $faker->address, ]); } // 2. Ensure we have some products and spare parts $supplier = \App\Models\Supplier::first() ?? \App\Models\Supplier::create(['name' => 'Test Supplier', 'phone' => '123']); $products = []; // Create 3 Spares for ($i = 1; $i <= 3; $i++) { $products[] = Product::create([ 'name' => 'Repuesto Test ' . $i, 'type' => 'spare', 'cost' => rand(500, 2000), 'price' => rand(3000, 8000), 'stock_quantity' => 100, 'suppliers_id' => $supplier->id, ]); } // Create 3 Accessories for ($i = 1; $i <= 3; $i++) { $products[] = Product::create([ 'name' => 'Accesorio Test ' . $i, 'type' => 'accessory', 'cost' => rand(1000, 4000), 'price' => rand(5000, 15000), 'stock_quantity' => 50, 'suppliers_id' => $supplier->id, ]); } $allProducts = Product::all(); $paymentMethods = ['Efectivo', 'Tarjeta de Débito', 'Tarjeta de Crédito', 'Transferencia']; // 3. Create 20 Sales for ($i = 1; $i <= 20; $i++) { // Pick a random client (some clients will buy more often, making them Top Spenders) // Weight the randomness a bit by picking from array_rand with duplicates $weightedClients = array_merge($clients, array_slice($clients, 0, 3)); $client = $weightedClients[array_rand($weightedClients)]; // Random date in the last 4 months $date = now()->subDays(rand(1, 120)); $sale = Sale::create([ 'client_id' => $client->id, 'total' => 0, // We'll calculate this 'payment_method' => $paymentMethods[array_rand($paymentMethods)], 'created_at' => $date, 'updated_at' => $date, ]); // Add 1 to 3 random products to this sale $numProducts = rand(1, 3); $totalSale = 0; $shuffledProducts = $allProducts->shuffle(); for ($j = 0; $j < $numProducts; $j++) { $product = $shuffledProducts[$j]; $quantity = rand(1, 5); $price = $product->price ?? 5000; SaleDetail::create([ 'sale_id' => $sale->id, 'product_id' => $product->id, 'quantity' => $quantity, 'price' => $price, 'created_at' => $date, 'updated_at' => $date, ]); $totalSale += ($quantity * $price); } // Update total $sale->update(['total' => $totalSale]); } } }