This commit is contained in:
gianella
2026-03-13 11:34:11 -03:00
parent 5adea83517
commit 15df818f1d
3 changed files with 163 additions and 23 deletions
+36 -5
View File
@@ -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()
@@ -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
@@ -23,8 +24,8 @@ return new class extends Migration
$table->integer('stock_quantity')->default(0);
$table->integer('min_stock_alert'); // Alerta
$table->enum('type', ['bike', 'accessory', 'service']);
$table->string('serial_number')->nullable(); // Solo para bicis
$table->string('rolled')->nullable(); // Solo para bicis
$table->foreignId('suppliers_id')->constrained()->default(1);
$table->timestamps();
+122 -14
View File
@@ -2,21 +2,70 @@
<x-section-header subtitle="Finanzas" title="Historial de " highlight="Ventas" />
<!-- Barra de Herramientas -->
<div class="w-full flex justify-between items-center mb-6">
<!-- Buscador simple (opcional visualmente, funcionalidad futura) -->
<div class="relative w-1/3">
<input type="text" placeholder="Buscar por N° Venta o Cliente..." class="bg-neutral-800 border border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5">
</div>
<div class="mb-6 bg-neutral-800/50 p-4 rounded-xl border border-neutral-800 shadow-sm">
<form action="{{ route('sales.index') }}" method="GET" class="grid grid-cols-1 md:grid-cols-12 gap-4 items-end">
<!-- Botón Nueva Venta -->
<a href="{{ route('sales.create') }}" class="px-5 py-2.5 bg-neon-lime text-neutral-900 font-bold rounded-lg hover:bg-[#b3e600] transition-colors shadow-lg shadow-neon-lime/20 flex items-center gap-2">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
Registrar Venta
</a>
<div class="md:col-span-3">
<label class="block text-xs text-gray-500 mb-1 uppercase font-bold tracking-wider"> Venta</label>
<div class="relative">
<div class="absolute inset-y-0 start-0 flex items-center ps-3 pointer-events-none">
<svg class="w-4 h-4 text-gray-500" fill="none" viewBox="0 0 20 20">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m19 19-4-4m0-7A7 7 0 1 1 1 8a7 7 0 0 1 14 0Z"/>
</svg>
</div>
<input type="text" name="search" value="{{ request('search') }}"
class="bg-neutral-800 border border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5 ps-10 placeholder-gray-600"
placeholder="Ej: 1420">
</div>
</div>
<div class="md:col-span-4">
<label class="block text-xs text-gray-500 mb-1 uppercase font-bold tracking-wider">Cliente</label>
<select name="client_id" class="w-full select2-filter">
<option value="">-- Todos --</option>
@foreach($clients as $client)
<option value="{{ $client->id }}" {{ request('client_id') == $client->id ? 'selected' : '' }}>
{{ $client->name }}
</option>
@endforeach
</select>
</div>
<div class="md:col-span-2">
<label class="block text-xs text-gray-500 mb-1 uppercase font-bold tracking-wider">Desde</label>
<input type="date" name="date_from" value="{{ request('date_from') }}"
class="bg-neutral-800 border border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5">
</div>
<div class="md:col-span-2">
<label class="block text-xs text-gray-500 mb-1 uppercase font-bold tracking-wider">Hasta</label>
<input type="date" name="date_to" value="{{ request('date_to') }}"
class="bg-neutral-800 border border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5">
</div>
<div class="md:col-span-1 flex gap-2">
<button type="submit" class="w-full bg-neon-lime hover:bg-[#b3e600] text-neutral-900 font-bold rounded-lg text-sm p-2.5 transition-colors shadow-lg shadow-neon-lime/20 flex items-center justify-center" title="Aplicar Filtros">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
</svg>
</button>
@if(request()->hasAny(['search', 'client_id', 'date_from', 'date_to']))
<a href="{{ route('sales.index') }}" class="w-full bg-neutral-700 hover:bg-neutral-600 text-white font-bold rounded-lg text-sm p-2.5 flex items-center justify-center transition-colors" title="Limpiar Filtros">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
</svg>
</a>
@endif
</div>
</form>
</div>
<!-- Tabla de Ventas -->
<div class="flex justify-end mb-6">
<a href="{{ route('sales.create') }}" class="px-6 py-2.5 bg-neutral-800 border border-neon-lime text-neon-lime hover:bg-neon-lime hover:text-neutral-900 font-bold rounded-lg transition-all shadow-lg shadow-neon-lime/10 flex items-center gap-2 transform hover:-translate-y-0.5">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
Nueva Venta
</a>
</div>
<div class="relative overflow-x-auto shadow-md sm:rounded-lg border border-neutral-800 bg-panel-bg">
<table class="w-full text-sm text-left text-gray-400">
<thead class="text-xs text-gray-300 uppercase bg-neutral-800 border-b border-neutral-700">
@@ -29,14 +78,73 @@
<th scope="col" class="px-6 py-3 text-center">Acciones</th>
</tr>
</thead>
<tbody>
<tbody class="divide-y divide-neutral-800">
@forelse($sales as $sale)
<tr class="bg-neutral-900/50 hover:bg-neutral-800 transition-colors">
<td class="px-6 py-4 font-mono text-white font-bold">
#{{ str_pad($sale->id, 5, '0', STR_PAD_LEFT) }}
</td>
<td class="px-6 py-4 text-gray-300">
{{ $sale->created_at->format('d/m/Y H:i') }}
</td>
<td class="px-6 py-4">
@if($sale->client)
<div class="font-bold text-white">{{ $sale->client->name }}</div>
<div class="text-xs text-gray-500">{{ $sale->client->phone ?? '' }}</div>
@else
<span class="italic text-gray-500">Consumidor Final</span>
@endif
</td>
<td class="px-6 py-4">
@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
<span class="{{ $badgeClass }} border px-2.5 py-0.5 rounded text-xs font-medium">
{{ $sale->payment_method }}
</span>
</td>
<td class="px-6 py-4 text-right font-bold text-lg text-neon-lime">
${{ number_format($sale->total, 2) }}
</td>
<td class="px-6 py-4 text-center">
<a href="{{ route('sales.show', $sale) }}"
class="inline-flex items-center justify-center w-8 h-8 rounded bg-neutral-700 hover:bg-neutral-600 text-gray-300 hover:text-white transition-colors"
title="Ver Detalle">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
</svg>
</a>
</td>
</tr>
@empty
<tr>
<td colspan="6" class="px-6 py-12 text-center text-gray-500">
<div class="flex flex-col items-center justify-center">
<svg class="w-12 h-12 mb-3 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg>
<p class="text-base">No se encontraron ventas registradas.</p>
</div>
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
<!-- Paginación -->
<div class="mt-4 w-full flex justify-center">
{{ $sales->links() }}
</div>
</x-layout>