diff --git a/app/Http/Controllers/SaleController.php b/app/Http/Controllers/SaleController.php index f491de7..49149a1 100644 --- a/app/Http/Controllers/SaleController.php +++ b/app/Http/Controllers/SaleController.php @@ -11,13 +11,44 @@ use Illuminate\Support\Facades\DB; class SaleController extends Controller { - public function index() + public function index(Request $request) { - $sales = Sale::with('client') - ->orderBy('created_at', 'desc') - ->paginate(15); + // Recuperar filtros + $search = $request->input('search'); // Búsqueda por ID + $clientId = $request->input('client_id'); // Filtro Cliente + $dateFrom = $request->input('date_from'); // Fecha Desde + $dateTo = $request->input('date_to'); // Fecha Hasta - return view('sales.index', compact('sales')); + // Query principal + $sales = Sale::query() + ->with(['client', 'details']) + + // 1. Filtro por ID de venta (si escriben en el buscador texto) + ->when($search, function ($query, $search) { + return $query->where('id', 'like', "%{$search}%"); + }) + + // 2. Filtro por Cliente (Select2) + ->when($clientId, function ($query, $clientId) { + return $query->where('client_id', $clientId); + }) + + // 3. Filtro por Rango de Fechas + ->when($dateFrom, function ($query, $dateFrom) { + return $query->whereDate('created_at', '>=', $dateFrom); + }) + ->when($dateTo, function ($query, $dateTo) { + return $query->whereDate('created_at', '<=', $dateTo); + }) + + ->latest() // Ordenar por fecha descendente + ->paginate(10) + ->withQueryString(); // Mantener filtros al cambiar de página + + // Traemos clientes para el select del filtro (solo id y nombre para optimizar) + $clients = Client::orderBy('name')->get(['id', 'name']); + + return view('sales.index', compact('sales', 'clients')); } public function create() diff --git a/database/migrations/2025_12_06_182358_create_products_table.php b/database/migrations/2025_12_06_182358_create_products_table.php index 69f735f..326973c 100644 --- a/database/migrations/2025_12_06_182358_create_products_table.php +++ b/database/migrations/2025_12_06_182358_create_products_table.php @@ -13,8 +13,9 @@ return new class extends Migration { Schema::create('products', function (Blueprint $table) { $table->id(); - $table->string('name'); - $table->string('sku')->unique()->nullable(); // Código de barras o interno + $table->enum('type', ['bike', 'accessory', 'clothing', 'service']); + //$table->string('name'); + //$table->string('sku')->unique()->nullable(); // Código de barras o interno $table->text('description')->nullable(); $table->decimal('price', 10, 2); // Precio venta diff --git a/resources/views/sales/index.blade.php b/resources/views/sales/index.blade.php index faee08e..53f0564 100644 --- a/resources/views/sales/index.blade.php +++ b/resources/views/sales/index.blade.php @@ -2,21 +2,70 @@ - -
- -
- -
+
+
+ +
+ +
+
+ + + +
+ +
+
- - - - Registrar Venta - +
+ + +
+ +
+ + +
+
+ + +
+ +
+ + + @if(request()->hasAny(['search', 'client_id', 'date_from', 'date_to'])) + + + + + + @endif +
+
- +
@@ -29,14 +78,73 @@ - + + @forelse($sales as $sale) + + + + + + + + + + + + + + @empty + + + + @endforelse
Acciones
+ #{{ str_pad($sale->id, 5, '0', STR_PAD_LEFT) }} + + {{ $sale->created_at->format('d/m/Y H:i') }} + + @if($sale->client) +
{{ $sale->client->name }}
+
{{ $sale->client->phone ?? '' }}
+ @else + Consumidor Final + @endif +
+ @php + $colors = [ + 'Efectivo' => 'text-green-400 bg-green-900/20 border-green-800', + 'Transferencia' => 'text-blue-400 bg-blue-900/20 border-blue-800', + 'Tarjeta de Débito' => 'text-purple-400 bg-purple-900/20 border-purple-800', + 'Tarjeta de Crédito' => 'text-orange-400 bg-orange-900/20 border-orange-800', + ]; + $badgeClass = $colors[$sale->payment_method] ?? 'text-gray-400 bg-gray-800 border-gray-700'; + @endphp + + {{ $sale->payment_method }} + + + ${{ number_format($sale->total, 2) }} + + + + + + + +
+
+ +

No se encontraron ventas registradas.

+
+
-
+ {{ $sales->links() }}
\ No newline at end of file