diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index d17d574..cb8327e 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -48,6 +48,34 @@ class ReportController extends Controller $monthlyOutcomeData[] = $out; } + // 4. Top 5 Productos más vendidos + $topProducts = \App\Models\SaleDetail::with('product') + ->select('product_id', DB::raw('SUM(quantity) as total_qty')) + ->groupBy('product_id') + ->orderByDesc('total_qty') + ->take(5) + ->get(); + + // 5. Top 5 Repuestos más usados/vendidos + $topSpares = \App\Models\SaleDetail::with('product') + ->whereHas('product', function ($query) { + $query->where('type', 'spare'); + }) + ->select('product_id', DB::raw('SUM(quantity) as total_qty')) + ->groupBy('product_id') + ->orderByDesc('total_qty') + ->take(5) + ->get(); + + // 6. Mejores Clientes (Top Spenders) + $topClients = Sale::with('client') + ->whereNotNull('client_id') + ->select('client_id', DB::raw('SUM(total) as total_spent')) + ->groupBy('client_id') + ->orderByDesc('total_spent') + ->take(5) + ->get(); + return view('reports.index', compact( 'totalIncome', 'totalOutcome', @@ -55,7 +83,10 @@ class ReportController extends Controller 'expensesByCategory', 'months', 'monthlyIncomeData', - 'monthlyOutcomeData' + 'monthlyOutcomeData', + 'topProducts', + 'topSpares', + 'topClients' )); } } diff --git a/database/seeders/ReportTestSeeder.php b/database/seeders/ReportTestSeeder.php new file mode 100644 index 0000000..60198fd --- /dev/null +++ b/database/seeders/ReportTestSeeder.php @@ -0,0 +1,104 @@ + $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]); + } + } +} diff --git a/resources/views/reports/index.blade.php b/resources/views/reports/index.blade.php index 0872b02..49982ed 100644 --- a/resources/views/reports/index.blade.php +++ b/resources/views/reports/index.blade.php @@ -69,6 +69,48 @@ + +
{{ $clientSale->client->name ?? 'Cliente Eliminado' }}
+{{ $clientSale->client->phone ?? 'Sin teléfono' }}
+