creo que pude instalar select2, create sales y crud client o casi crud
This commit is contained in:
@@ -3,15 +3,28 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use App\Models\Client;
|
||||||
|
|
||||||
class ClientController extends Controller
|
class ClientController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
//
|
$query = $request->input('search');
|
||||||
|
|
||||||
|
$clients = Client::query()
|
||||||
|
->when($query, function ($q) use ($query) {
|
||||||
|
// Si hay búsqueda, filtra por nombre o SKU
|
||||||
|
return $q->where('name', 'like', "%{$query}%");
|
||||||
|
})
|
||||||
|
->orderBy('name', 'asc')
|
||||||
|
//->orderBy('create_at', 'asc')
|
||||||
|
->paginate(10) // Paginamos de a 10
|
||||||
|
->withQueryString(); // Mantiene el filtro de búsqueda al cambiar de página
|
||||||
|
|
||||||
|
return view('clients.index', compact('clients'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,7 +32,7 @@ class ClientController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
//
|
return view('clients.create');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,7 +40,28 @@ class ClientController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
//
|
$validated = $request->validate([
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'phone' => 'nullable|string|max:50',
|
||||||
|
'email' => 'nullable|email|max:255|unique:clients,email',
|
||||||
|
'address' => 'nullable|string|max:255',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 1. Guardamos el cliente en una variable para tener su ID
|
||||||
|
$client = Client::create($validated);
|
||||||
|
|
||||||
|
// 2. Verificamos el origen
|
||||||
|
if ($request->input('origin') === 'sales') {
|
||||||
|
|
||||||
|
// Si vino de ventas, volvemos a ventas
|
||||||
|
// Y pasamos el ID del nuevo cliente para auto-seleccionarlo
|
||||||
|
return redirect()->route('sales.create', ['new_client_id' => $client->id])
|
||||||
|
->with('success', 'Cliente creado. Ya puedes seleccionarlo.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Si no, comportamiento normal (volver al index de clientes)
|
||||||
|
return redirect()->route('clients.index')
|
||||||
|
->with('success', 'Cliente creado correctamente.');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -38,27 +72,38 @@ class ClientController extends Controller
|
|||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function edit(Client $client)
|
||||||
* Show the form for editing the specified resource.
|
|
||||||
*/
|
|
||||||
public function edit(string $id)
|
|
||||||
{
|
{
|
||||||
//
|
// Reutilizamos la vista de create, o creamos una edit.blade.php similar
|
||||||
|
return view('clients.edit', compact('client'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function update(Request $request, Client $client)
|
||||||
* Update the specified resource in storage.
|
|
||||||
*/
|
|
||||||
public function update(Request $request, string $id)
|
|
||||||
{
|
{
|
||||||
//
|
$validated = $request->validate([
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'phone' => 'nullable|string|max:50',
|
||||||
|
'email' => 'nullable|email|max:255|unique:clients,email,' . $client->id, // Ignorar email propio
|
||||||
|
'address' => 'nullable|string|max:255',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$client->update($validated);
|
||||||
|
|
||||||
|
return redirect()->route('clients.index')->with('success', 'Cliente actualizado correctamente.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the specified resource from storage.
|
* Remove the specified resource from storage.
|
||||||
*/
|
*/
|
||||||
public function destroy(string $id)
|
public function destroy(Client $client)
|
||||||
{
|
{
|
||||||
//
|
try {
|
||||||
|
$client->delete();
|
||||||
|
return redirect()->route('clients.index')->with('success', 'Cliente eliminado correctamente.');
|
||||||
|
} catch (\Illuminate\Database\QueryException $e) {
|
||||||
|
|
||||||
|
return back()->with('error', 'No se puede eliminar el cliente porque tiene ventas registradas.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,29 +4,28 @@ namespace App\Http\Controllers;
|
|||||||
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
|
use App\Models\Client;
|
||||||
use App\Models\Sale;
|
use App\Models\Sale;
|
||||||
use App\Models\SaleDetail;
|
use App\Models\SaleDetail;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
//
|
|
||||||
use App\Models\Client;
|
|
||||||
|
|
||||||
class SaleController extends Controller
|
class SaleController extends Controller
|
||||||
{
|
{
|
||||||
public function create()
|
public function create()
|
||||||
{
|
{
|
||||||
// Buscamos productos que tengan stock mayor a 0 para mostrar en el selector
|
// Traemos productos con stock y clientes para los selectores
|
||||||
$products = Product::where('stock_quantity', '>', 0)->get();
|
$products = Product::where('stock_quantity', '>', 0)->get();
|
||||||
|
$clients = Client::orderBy('name')->get();
|
||||||
$clients = \App\Models\Client::orderBy('name')->get();
|
|
||||||
|
|
||||||
return view('sales.create', compact('products', 'clients'));
|
return view('sales.create', compact('products', 'clients'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
|
// Validación estricta
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'payment_method' => 'required|string',
|
|
||||||
'client_id' => 'nullable|exists:clients,id',
|
'client_id' => 'nullable|exists:clients,id',
|
||||||
|
'payment_method' => 'required|string',
|
||||||
'items' => 'required|array',
|
'items' => 'required|array',
|
||||||
'items.*.product_id' => 'required|exists:products,id',
|
'items.*.product_id' => 'required|exists:products,id',
|
||||||
'items.*.quantity' => 'required|integer|min:1',
|
'items.*.quantity' => 'required|integer|min:1',
|
||||||
@@ -34,47 +33,47 @@ class SaleController extends Controller
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
DB::transaction(function () use ($request) {
|
DB::transaction(function () use ($request) {
|
||||||
$totalSale = 0;
|
|
||||||
|
|
||||||
foreach ($request->items as $item) {
|
$totalVenta = 0;
|
||||||
$product = Product::find($item['product_id']);
|
|
||||||
$totalSale += $product->precio * $item['quantity'];
|
|
||||||
|
|
||||||
if ($product->stock_quantity < $item['quantity']) {
|
// 1. Calcular total previo (Solo habrá 1 item por ahora, pero el bucle sirve igual)
|
||||||
throw new \Exception("No hay suficiente stock de " . $product->nombre);
|
foreach ($request->items as $itemData) {
|
||||||
}
|
$product = Product::findOrFail($itemData['product_id']);
|
||||||
|
|
||||||
|
if ($product->stock_quantity < $itemData['quantity']) {
|
||||||
|
throw new \Exception("Stock insuficiente para: " . $product->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
//cabecera
|
$totalVenta += $product->price * $itemData['quantity'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Crear Venta
|
||||||
$sale = Sale::create([
|
$sale = Sale::create([
|
||||||
//'user_id' => auth()->id(), //empleado logueado
|
|
||||||
'client_id' => $request->client_id,
|
'client_id' => $request->client_id,
|
||||||
'total' => $totalSale,
|
'total' => $totalVenta,
|
||||||
'payment_method' => $request->payment_method,
|
'payment_method' => $request->payment_method,
|
||||||
|
// 'user_id' => auth()->id(), // Descomenta si usas autenticación
|
||||||
]);
|
]);
|
||||||
|
|
||||||
foreach ($request->items as $item) {
|
// 3. Guardar Detalle y Restar Stock
|
||||||
$product = Product::find($item['product_id']);
|
foreach ($request->items as $itemData) {
|
||||||
|
$product = Product::findOrFail($itemData['product_id']);
|
||||||
|
|
||||||
SaleDetail::create([
|
SaleDetail::create([
|
||||||
'sale_id' => $sale->id,
|
'sale_id' => $sale->id,
|
||||||
'product_id' => $product->id,
|
'product_id' => $product->id,
|
||||||
'quantity' => $item['quantity'],
|
'quantity' => $itemData['quantity'],
|
||||||
'price' => $product->price,
|
'price' => $product->price,
|
||||||
]);
|
]);
|
||||||
$nuevoStock = $product->stock_quantity - $item['quantity'];
|
|
||||||
$product->stock_quantity = $nuevoStock;
|
$product->decrement('stock_quantity', $itemData['quantity']);
|
||||||
$product->save();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 5. REDIRECCIÓN DE ÉXITO
|
|
||||||
// Redirigimos al usuario con un mensaje flash [cite: 668]
|
|
||||||
return redirect()->route('sales.create')->with('success', '¡Venta registrada correctamente!');
|
return redirect()->route('sales.create')->with('success', '¡Venta registrada correctamente!');
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// Si algo falló (ej: falta stock), volvemos atrás con el error
|
return back()->with('error', $e->getMessage())->withInput();
|
||||||
return back()->with('error', 'Error en la venta: ' . $e->getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+8
-1
@@ -6,7 +6,8 @@
|
|||||||
"": {
|
"": {
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tailwindcss/postcss": "^4.1.18",
|
"@tailwindcss/postcss": "^4.1.18",
|
||||||
"jquery": "^3.7.1"
|
"jquery": "^3.7.1",
|
||||||
|
"select2": "^4.1.0-rc.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tailwindcss/vite": "^4.1.18",
|
"@tailwindcss/vite": "^4.1.18",
|
||||||
@@ -2243,6 +2244,12 @@
|
|||||||
"tslib": "^2.1.0"
|
"tslib": "^2.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/select2": {
|
||||||
|
"version": "4.1.0-rc.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/select2/-/select2-4.1.0-rc.0.tgz",
|
||||||
|
"integrity": "sha512-Hr9TdhyHCZUtwznEH2CBf7967mEM0idtJ5nMtjvk3Up5tPukOLXbHUNmh10oRfeNIhj+3GD3niu+g6sVK+gK0A==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/shell-quote": {
|
"node_modules/shell-quote": {
|
||||||
"version": "1.8.3",
|
"version": "1.8.3",
|
||||||
"resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.8.3.tgz",
|
"resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.8.3.tgz",
|
||||||
|
|||||||
+2
-1
@@ -18,6 +18,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tailwindcss/postcss": "^4.1.18",
|
"@tailwindcss/postcss": "^4.1.18",
|
||||||
"jquery": "^3.7.1"
|
"jquery": "^3.7.1",
|
||||||
|
"select2": "^4.1.0-rc.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,13 @@
|
|||||||
import './bootstrap';
|
import './bootstrap';
|
||||||
|
// 1. Importar jQuery
|
||||||
|
import jQuery from 'jquery';
|
||||||
|
|
||||||
|
// 2. Hacerlo global (IMPORTANTE para que funcione $(document).ready en Blade)
|
||||||
|
window.$ = window.jQuery = jQuery;
|
||||||
|
|
||||||
|
// 3. Importar Select2
|
||||||
|
import select2 from 'select2';
|
||||||
|
select2(); // Inicializar el plugin
|
||||||
|
|
||||||
|
// 4. Importar los estilos de Select2 (Opcional aquí, o en CSS)
|
||||||
|
import 'select2/dist/css/select2.css';
|
||||||
|
|||||||
@@ -1,5 +1,49 @@
|
|||||||
@extends('layouts.app')
|
<x-layout title="Lauck - Nuevo Cliente">
|
||||||
@section('main')
|
|
||||||
<h1>formulario de crear cliente</h1>
|
|
||||||
|
|
||||||
@endsection
|
<x-section-header subtitle="Gestión de Clientes" title="Nuevo " highlight="Cliente" />
|
||||||
|
|
||||||
|
<div class="w-full max-w-4xl mx-auto bg-panel-bg border border-neutral-800 rounded-xl p-8 shadow-lg">
|
||||||
|
|
||||||
|
<form action="{{ route('clients.store') }}" method="POST">
|
||||||
|
@csrf
|
||||||
|
|
||||||
|
<input type="hidden" name="origin" value="{{ request('origin') }}">
|
||||||
|
|
||||||
|
<div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
|
||||||
|
|
||||||
|
<div class="md:col-span-4">
|
||||||
|
<x-forms.label for="name" value="Nombre Completo" />
|
||||||
|
<x-forms.input id="name" name="name" type="text" :value="old('name')" required autofocus placeholder="Ej: Sergio Lauck" :error="$errors->first('name')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="md:col-span-2">
|
||||||
|
<x-forms.label for="phone" value="Teléfono / WhatsApp" />
|
||||||
|
<x-forms.input id="phone" name="phone" type="text" :value="old('phone')" placeholder="Ej: 343 154..." :error="$errors->first('phone')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="md:col-span-2">
|
||||||
|
<x-forms.label for="email" value="Correo Electrónico" />
|
||||||
|
<x-forms.input id="email" name="email" type="email" :value="old('email')" placeholder="cliente@ejemplo.com" :error="$errors->first('email')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="md:col-span-4">
|
||||||
|
<x-forms.label for="address" value="Dirección / Domicilio" />
|
||||||
|
<textarea id="address" name="address" rows="3" class="bg-neutral-800 border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5 placeholder-gray-500" placeholder="Calle, número, piso...">{{ old('address') }}</textarea>
|
||||||
|
@error('address')
|
||||||
|
<span class="text-red-500 text-xs mt-1">{{ $message }}</span>
|
||||||
|
@enderror
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-between md:justify-end space-x-4 border-t border-neutral-800 pt-6">
|
||||||
|
<a href="{{ route('sales.create') }}" class="text-gray-400 hover:text-white font-medium text-sm transition-colors">Cancelar</a>
|
||||||
|
|
||||||
|
<button type="submit" class="px-6 py-2.5 bg-neon-lime text-neutral-900 font-bold rounded-lg hover:bg-[#b3e600] transition-colors shadow-lg shadow-neon-lime/20">
|
||||||
|
Guardar Cliente
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</x-layout>
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
<x-layout title="Lauck - Editar Cliente">
|
||||||
|
|
||||||
|
<x-section-header subtitle="Gestión de Clientes" title="Editar " highlight="Cliente" />
|
||||||
|
|
||||||
|
<div class="w-full max-w-4xl mx-auto bg-panel-bg border border-neutral-800 rounded-xl p-8 shadow-lg">
|
||||||
|
|
||||||
|
<form action="{{ route('clients.update', $client) }}" method="POST">
|
||||||
|
@csrf
|
||||||
|
@method('PUT') <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
|
||||||
|
|
||||||
|
<div class="md:col-span-4">
|
||||||
|
<x-forms.label for="name" value="Nombre Completo" />
|
||||||
|
<x-forms.input
|
||||||
|
id="name"
|
||||||
|
name="name"
|
||||||
|
type="text"
|
||||||
|
:value="old('name', $client->name)"
|
||||||
|
required
|
||||||
|
autofocus
|
||||||
|
placeholder="Ej: Sergio Lauck"
|
||||||
|
:error="$errors->first('name')"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="md:col-span-2">
|
||||||
|
<x-forms.label for="phone" value="Teléfono / WhatsApp" />
|
||||||
|
<x-forms.input
|
||||||
|
id="phone"
|
||||||
|
name="phone"
|
||||||
|
type="text"
|
||||||
|
:value="old('phone', $client->phone)"
|
||||||
|
placeholder="Ej: 343 154..."
|
||||||
|
:error="$errors->first('phone')"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="md:col-span-2">
|
||||||
|
<x-forms.label for="email" value="Correo Electrónico" />
|
||||||
|
<x-forms.input
|
||||||
|
id="email"
|
||||||
|
name="email"
|
||||||
|
type="email"
|
||||||
|
:value="old('email', $client->email)"
|
||||||
|
placeholder="cliente@ejemplo.com"
|
||||||
|
:error="$errors->first('email')"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="md:col-span-4">
|
||||||
|
<x-forms.label for="address" value="Dirección / Domicilio" />
|
||||||
|
<textarea
|
||||||
|
id="address"
|
||||||
|
name="address"
|
||||||
|
rows="3"
|
||||||
|
class="bg-neutral-800 border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5 placeholder-gray-500"
|
||||||
|
placeholder="Calle, número, piso..."
|
||||||
|
>{{ old('address', $client->address) }}</textarea>
|
||||||
|
|
||||||
|
@error('address')
|
||||||
|
<span class="text-red-500 text-xs mt-1">{{ $message }}</span>
|
||||||
|
@enderror
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-between md:justify-end space-x-4 border-t border-neutral-800 pt-6">
|
||||||
|
|
||||||
|
<a href="{{ route('clients.index') }}" class="text-gray-400 hover:text-white font-medium text-sm transition-colors">
|
||||||
|
Cancelar
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<button type="submit" class="px-6 py-2.5 bg-neon-lime text-neutral-900 font-bold rounded-lg hover:bg-[#b3e600] transition-colors shadow-lg shadow-neon-lime/20">
|
||||||
|
Actualizar Cliente
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</x-layout>
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
<x-layout title="Lauck - Clientes">
|
||||||
|
|
||||||
|
<x-section-header subtitle="Gestión de Clientes" title="Cartera de " highlight="Clientes" />
|
||||||
|
|
||||||
|
@if(session('success'))
|
||||||
|
<div class="bg-green-900/50 border border-green-500 text-green-300 px-4 py-3 rounded-lg mb-6 shadow-sm">
|
||||||
|
{{ session('success') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
<div class="w-full flex flex-col md:flex-row justify-between items-center gap-4 mb-6">
|
||||||
|
|
||||||
|
<form action="{{ route('clients.index') }}" method="GET" class="w-full md:w-1/2">
|
||||||
|
<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" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" 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="block w-full p-3 ps-10 text-sm text-white border border-neutral-700 rounded-lg bg-neutral-800 focus:ring-neon-lime focus:border-neon-lime placeholder-gray-500"
|
||||||
|
placeholder="Buscar por nombre, teléfono o email...">
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<a href="{{ route('clients.create') }}" class="w-full md:w-auto text-center px-5 py-3 text-sm font-bold text-neutral-900 bg-neon-lime rounded-lg hover:bg-[#b3e600] transition-colors uppercase tracking-wide shadow-lg shadow-neon-lime/20">
|
||||||
|
+ Nuevo Cliente
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="relative overflow-x-auto shadow-md sm:rounded-lg border border-neutral-800 w-full">
|
||||||
|
<table class="w-full text-sm text-left rtl:text-right text-gray-400">
|
||||||
|
<thead class="text-xs text-gray-300 uppercase bg-neutral-800 border-b border-neutral-700">
|
||||||
|
<tr>
|
||||||
|
<th scope="col" class="px-6 py-3">Cliente / Email</th>
|
||||||
|
<th scope="col" class="px-6 py-3">Teléfono / WhatsApp</th>
|
||||||
|
<th scope="col" class="px-6 py-3">Dirección</th>
|
||||||
|
<th scope="col" class="px-6 py-3 text-right">Acciones</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@forelse($clients as $client)
|
||||||
|
<tr class="bg-neutral-900/50 border-b border-neutral-800 hover:bg-neutral-800 transition-colors group">
|
||||||
|
|
||||||
|
<td class="px-6 py-4 whitespace-nowrap">
|
||||||
|
<div class="text-base font-bold text-white">{{ $client->name }}</div>
|
||||||
|
<div class="text-xs text-gray-500 font-mono">
|
||||||
|
{{ $client->email ?? 'Sin email registrado' }}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td class="px-6 py-4">
|
||||||
|
@if($client->phone)
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<span class="font-mono text-gray-300">{{ $client->phone }}</span>
|
||||||
|
<span class="text-green-500 text-xs bg-green-900/30 px-1.5 py-0.5 rounded">WA</span>
|
||||||
|
</div>
|
||||||
|
@else
|
||||||
|
<span class="text-gray-600 italic">No registrado</span>
|
||||||
|
@endif
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td class="px-6 py-4">
|
||||||
|
<span class="text-gray-400 truncate max-w-xs block" title="{{ $client->address }}">
|
||||||
|
{{ $client->address ?? '-' }}
|
||||||
|
</span>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td class="px-6 py-4 text-right flex items-center justify-end gap-3">
|
||||||
|
<a href="{{ route('clients.edit', $client) }}" class="font-medium text-blue-400 hover:text-blue-300 hover:underline transition-colors">
|
||||||
|
Editar
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<form action="{{ route('clients.destroy', $client) }}" method="POST" class="inline-block" onsubmit="return confirm('¿Estás seguro de que deseas eliminar este cliente? Esta acción no se puede deshacer.');">
|
||||||
|
@csrf
|
||||||
|
@method('DELETE')
|
||||||
|
|
||||||
|
<button type="submit" class="font-medium text-red-500 hover:text-red-400 hover:underline transition-colors cursor-pointer bg-transparent border-0 p-0">
|
||||||
|
Eliminar
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
</tr>
|
||||||
|
@empty
|
||||||
|
<tr>
|
||||||
|
<td colspan="4" class="px-6 py-10 text-center text-gray-500">
|
||||||
|
<div class="flex flex-col items-center justify-center">
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 mb-2 opacity-50" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
|
||||||
|
</svg>
|
||||||
|
<p>No se encontraron clientes.</p>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endforelse
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-4 w-full">
|
||||||
|
{{ $clients->links() }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</x-layout>
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
</x-ui.card>
|
</x-ui.card>
|
||||||
|
|
||||||
<!-- Tarjeta Clientes -->
|
<!-- Tarjeta Clientes -->
|
||||||
<x-ui.card href="#" title="Clientes" description="Base de datos de ciclistas." linkText="Buscar usuario">
|
<x-ui.card href="{{ url('/clients') }}" title="Clientes" description="Base de datos de ciclistas." linkText="Buscar usuario">
|
||||||
<svg class="w-12 h-12 text-neon-lime" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" /></svg>
|
<svg class="w-12 h-12 text-neon-lime" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" /></svg>
|
||||||
</x-ui.card>
|
</x-ui.card>
|
||||||
|
|
||||||
|
|||||||
@@ -1,82 +1,124 @@
|
|||||||
<x-layout>
|
<x-layout title="Lauck - Nueva Venta">
|
||||||
<div class="py-12 bg-gray-100 min-h-screen">
|
@push('styles')
|
||||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
<style>
|
||||||
|
/* Select2 adaptado al Tema 'Lauck' (Neutral + Neon Lime) */
|
||||||
|
.select2-container--default .select2-selection--single {
|
||||||
|
background-color: #262626 !important; /* neutral-800 */
|
||||||
|
border-color: #404040 !important; /* neutral-700 */
|
||||||
|
color: white !important;
|
||||||
|
height: 42px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
border-radius: 0.5rem; /* rounded-lg */
|
||||||
|
}
|
||||||
|
.select2-container--default .select2-selection--single .select2-selection__rendered {
|
||||||
|
color: white !important;
|
||||||
|
line-height: 42px;
|
||||||
|
padding-left: 12px;
|
||||||
|
}
|
||||||
|
.select2-dropdown {
|
||||||
|
background-color: #262626 !important; /* neutral-800 */
|
||||||
|
border-color: #404040 !important; /* neutral-700 */
|
||||||
|
color: white !important;
|
||||||
|
}
|
||||||
|
/* Color de resalto al pasar el mouse u opción seleccionada */
|
||||||
|
.select2-container--default .select2-results__option--highlighted.select2-results__option--selectable {
|
||||||
|
background-color: #ccff00 !important; /* neon-lime */
|
||||||
|
color: #171717 !important; /* neutral-900 */
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
/* Ajuste del placeholder */
|
||||||
|
.select2-container--default .select2-selection--single .select2-selection__placeholder {
|
||||||
|
color: #9ca3af !important; /* gray-400 */
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
@endpush
|
||||||
|
|
||||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
<x-section-header subtitle="Punto de Venta" title="Nueva " highlight="Venta" />
|
||||||
<div class="mb-6 border-b pb-2">
|
|
||||||
<h2 class="text-2xl font-bold text-gray-800">Nueva Venta en Mostrador</h2>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
@if (session('success'))
|
<div class="w-full max-w-5xl mx-auto bg-panel-bg border border-neutral-800 rounded-xl p-8 shadow-lg">
|
||||||
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4">
|
|
||||||
<strong class="font-bold">¡Éxito!</strong> {{ session('success') }}
|
@if(session('success'))
|
||||||
|
<div class="bg-green-900/50 border border-green-500 text-green-300 px-4 py-3 rounded-lg mb-6 shadow-sm">
|
||||||
|
{{ session('success') }}
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
@if(session('error'))
|
||||||
@if (session('error'))
|
<div class="bg-red-900/50 border border-red-500 text-red-300 px-4 py-3 rounded-lg mb-6 shadow-sm">
|
||||||
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4">
|
{{ session('error') }}
|
||||||
<strong class="font-bold">Error:</strong> {{ session('error') }}
|
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
@if($errors->any())
|
||||||
@if ($errors->any())
|
<div class="bg-red-900/50 border border-red-500 text-red-300 px-4 py-3 rounded-lg mb-6 shadow-sm">
|
||||||
<div class="bg-red-50 text-red-600 p-4 mb-4 rounded border border-red-200">
|
<ul class="list-disc list-inside">
|
||||||
<ul>
|
@foreach($errors->all() as $error)
|
||||||
@foreach ($errors->all() as $error)
|
<li>{{ $error }}</li>
|
||||||
<li>• {{ $error }}</li>
|
|
||||||
@endforeach
|
@endforeach
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@endif
|
@endif
|
||||||
|
|
||||||
|
<form action="{{ route('sales.store') }}" method="POST">
|
||||||
|
|
||||||
<form action="{{ route('sales.store') }}" method="POST" id="sale-form">
|
|
||||||
@csrf
|
@csrf
|
||||||
|
|
||||||
<div class="mb-6 bg-gray-50 p-4 rounded-lg border border-gray-200">
|
<div class="grid grid-cols-1 md:grid-cols-12 gap-6 mb-8 border-b border-neutral-800 pb-8">
|
||||||
<label class="block text-sm font-medium text-gray-700 mb-1">Cliente</label>
|
<div class="md:col-span-10">
|
||||||
<div class="flex gap-2">
|
<x-forms.label for="client_id" value="Cliente" />
|
||||||
<select name="client_id" class="select2-enable w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2 px-3 border">
|
<div class="relative">
|
||||||
|
<select name="client_id" class="w-full select2-basic">
|
||||||
<option value="">-- Consumidor Final (Anónimo) --</option>
|
<option value="">-- Consumidor Final (Anónimo) --</option>
|
||||||
@foreach($clients as $client)
|
@foreach($clients as $client)
|
||||||
<option value="{{ $client->id }}">{{ $client->name }} ({{ $client->phone }})</option>
|
<option value="{{ $client->id }}">{{ $client->name }}</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
|
</div>
|
||||||
<a href="{{ route('clients.create') }}" target="_blank" class="bg-indigo-100 text-indigo-700 hover:bg-indigo-200 px-4 py-2 rounded-md font-bold flex items-center border border-indigo-200" title="Crear nuevo cliente">
|
</div>
|
||||||
|
<div class="md:col-span-2 flex items-end">
|
||||||
|
<a href="{{ route('clients.create', ['origin' => 'sales']) }}" target="_blank" class="bg-indigo-100 text-indigo-700 hover:bg-indigo-200 px-4 py-2 rounded-md font-bold flex items-center border border-indigo-200" title="Crear nuevo cliente">
|
||||||
+
|
+
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="items-container" class="space-y-4">
|
<div class="mb-8">
|
||||||
<div class="item-row grid grid-cols-1 md:grid-cols-12 gap-4 items-end bg-gray-50 p-4 rounded-lg border border-gray-200">
|
<div class="flex justify-between items-end mb-4">
|
||||||
|
<x-forms.label value="Detalle de Productos" />
|
||||||
|
<div class="text-sm text-gray-400">
|
||||||
|
Total Items: <span class="text-white font-mono" id="item-count">1</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="items-container" class="space-y-3">
|
||||||
|
|
||||||
|
<div class="item-row grid grid-cols-1 md:grid-cols-12 gap-4 items-start bg-neutral-800/50 p-4 rounded-xl border border-neutral-800">
|
||||||
|
|
||||||
<div class="md:col-span-6">
|
<div class="md:col-span-6">
|
||||||
<label class="block text-sm font-medium text-gray-700 mb-1">Producto</label>
|
<label class="block text-gray-400 text-xs mb-1.5 uppercase tracking-wider font-semibold">Producto</label>
|
||||||
<select name="items[0][product_id]" class="product-select select2-enable w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2 px-3 border" required onchange="calcularTotal()">
|
<select name="items[0][product_id]" class="w-full select2-product product-select">
|
||||||
<option value="" data-price="0" selected disabled>Seleccione...</option>
|
<option value="" data-price="0" selected disabled>Buscar producto...</option>
|
||||||
@foreach($products as $product)
|
@foreach($products as $product)
|
||||||
<option value="{{ $product->id }}" data-price="{{ $product->price }}">
|
<option value="{{ $product->id }}" data-price="{{ $product->price }}">
|
||||||
{{ $product->sku }} - {{ $product->name }} (${{ $product->price }})
|
{{ $product->sku }} - {{ $product->name }} (Stock: {{ $product->stock_quantity }})
|
||||||
</option>
|
</option>
|
||||||
@endforeach
|
@endforeach
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="md:col-span-3">
|
<div class="md:col-span-2">
|
||||||
<label class="block text-sm font-medium text-gray-700 mb-1">Cantidad</label>
|
<label class="block text-gray-400 text-xs mb-1.5 uppercase tracking-wider font-semibold">Cant.</label>
|
||||||
<input type="number" name="items[0][quantity]" value="1" min="1" class="quantity-input w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2 px-3 border" required oninput="calcularTotal()">
|
<input type="number" name="items[0][quantity]" value="1" min="1"
|
||||||
|
class="quantity-input bg-neutral-800 border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5 h-[42px]">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="md:col-span-2 text-right font-mono text-gray-600 self-center pt-6">
|
<div class="md:col-span-3 text-right flex flex-col justify-center h-[42px] mt-6">
|
||||||
$<span class="row-subtotal">0.00</span>
|
<span class="text-xs text-gray-500">Subtotal</span>
|
||||||
|
<span class="row-total text-lg font-bold text-neon-lime">$0.00</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="md:col-span-1 text-right">
|
<div class="md:col-span-1 flex items-end justify-end h-[42px] mt-6">
|
||||||
<button type="button" class="text-gray-400 cursor-not-allowed" disabled>
|
<button type="button" class="btn-remove text-neutral-500 hover:text-red-500 transition-colors p-2" title="Quitar item">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 inline">
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
|
||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -84,24 +126,27 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-4">
|
<div class="mt-4">
|
||||||
<button type="button" onclick="agregarFila()" class="flex items-center text-indigo-600 hover:text-indigo-800 font-semibold">
|
<button type="button" id="btn-add-row" class="inline-flex items-center text-neon-lime hover:text-[#b3e600] font-bold text-sm transition-colors">
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 mr-1">
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-1" viewBox="0 0 20 20" fill="currentColor">
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
|
<path fill-rule="evenodd" d="M10 3a1 1 0 011 1v5h5a1 1 0 110 2h-5v5a1 1 0 11-2 0v-5H4a1 1 0 110-2h5V4a1 1 0 011-1z" clip-rule="evenodd" />
|
||||||
</svg>
|
</svg>
|
||||||
Agregar otro producto
|
Agregar otro producto
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex justify-end mt-6 border-t pt-4">
|
<div class="mt-5 flex justify-end items-center border-t border-neutral-700 pt-6">
|
||||||
<div class="text-2xl font-bold text-gray-800">
|
<div class="text-right">
|
||||||
Total a Pagar: <span class="text-indigo-600">$<span id="total-display">0.00</span></span>
|
<span class="block text-gray-400 text-sm mb-1">Monto Total a Pagar</span>
|
||||||
|
<span id="grand-total" class="text-3xl font-bold text-white">$0.00</span>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-6 grid grid-cols-1 md:grid-cols-2 gap-6 items-center">
|
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 items-end border-t border-neutral-800 pt-6">
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label class="block text-sm font-medium text-gray-700 mb-1">Método de Pago</label>
|
<x-forms.label for="payment_method" value="Método de Pago" />
|
||||||
<select name="payment_method" class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2 px-3 border">
|
<select name="payment_method" class="bg-neutral-800 border-neutral-700 text-white text-sm rounded-lg focus:ring-neon-lime focus:border-neon-lime block w-full p-2.5">
|
||||||
<option value="Efectivo">Efectivo</option>
|
<option value="Efectivo">Efectivo</option>
|
||||||
<option value="Tarjeta de Débito">Tarjeta de Débito</option>
|
<option value="Tarjeta de Débito">Tarjeta de Débito</option>
|
||||||
<option value="Tarjeta de Crédito">Tarjeta de Crédito</option>
|
<option value="Tarjeta de Crédito">Tarjeta de Crédito</option>
|
||||||
@@ -109,115 +154,136 @@
|
|||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex items-end gap-4 justify-end">
|
<div class="flex justify-end gap-4">
|
||||||
<a href="{{ route('sales.create') }}" class="bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border border-gray-400 rounded shadow">
|
<a href="{{ route('sales.create') }}" class="px-6 py-2.5 text-gray-400 hover:text-white font-medium text-sm transition-colors flex items-center">
|
||||||
Cancelar
|
Cancelar
|
||||||
</a>
|
</a>
|
||||||
<button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2 px-6 rounded shadow-lg">
|
<button type="submit" class="px-8 py-2.5 bg-neon-lime text-neutral-900 font-bold rounded-lg hover:bg-[#b3e600] transition-colors shadow-lg shadow-neon-lime/20 transform hover:scale-105">
|
||||||
Confirmar Venta
|
Confirmar Venta
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</x-layout>
|
|
||||||
|
|
||||||
@push('scripts')
|
<script type="module">
|
||||||
<script>
|
let rowCount = 0;
|
||||||
let itemIndex = 0;
|
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
inicializarSelect2();
|
initSelect2();
|
||||||
|
actualizarBloqueos(); // Bloqueo inicial
|
||||||
|
|
||||||
|
// --- AGREGAR FILA ---
|
||||||
|
$('#btn-add-row').click(function() {
|
||||||
|
rowCount++;
|
||||||
|
const container = $('#items-container');
|
||||||
|
const firstRow = container.find('.item-row').first();
|
||||||
|
|
||||||
|
$('.select2-product').select2('destroy'); // Destruir
|
||||||
|
|
||||||
|
const newRow = firstRow.clone(); // Clonar
|
||||||
|
|
||||||
|
initSelect2(); // Reactivar originales
|
||||||
|
|
||||||
|
// Limpiar nueva fila
|
||||||
|
newRow.find('input').val(1);
|
||||||
|
newRow.find('.row-total').text('$0.00');
|
||||||
|
|
||||||
|
// Actualizar atributos
|
||||||
|
newRow.find('select').attr('name', `items[${rowCount}][product_id]`).val('');
|
||||||
|
newRow.find('input').attr('name', `items[${rowCount}][quantity]`);
|
||||||
|
|
||||||
|
// Desbloquear opciones en la nueva fila (vital)
|
||||||
|
newRow.find('option').prop('disabled', false);
|
||||||
|
|
||||||
|
container.append(newRow);
|
||||||
|
initSelect2(); // Activar nueva
|
||||||
|
actualizarBloqueos(); // Recalcular bloqueos
|
||||||
|
actualizarContadorItems();
|
||||||
});
|
});
|
||||||
|
|
||||||
function inicializarSelect2() {
|
// --- ELIMINAR FILA ---
|
||||||
$('.select2-enable').select2({
|
$(document).on('click', '.btn-remove', function() {
|
||||||
|
if ($('.item-row').length > 1) {
|
||||||
|
$(this).closest('.item-row').remove();
|
||||||
|
recalcularTodo();
|
||||||
|
actualizarBloqueos();
|
||||||
|
actualizarContadorItems();
|
||||||
|
} else {
|
||||||
|
alert("Debe haber al menos un producto.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- EVENTOS ---
|
||||||
|
$(document).on('select2:select input', '.product-select, .quantity-input', function() {
|
||||||
|
let row = $(this).closest('.item-row');
|
||||||
|
calcularFila(row);
|
||||||
|
recalcularTodo();
|
||||||
|
|
||||||
|
if ($(this).hasClass('product-select')) {
|
||||||
|
actualizarBloqueos();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- FUNCIONES ---
|
||||||
|
function initSelect2() {
|
||||||
|
$('.select2-basic').select2({ width: '100%' });
|
||||||
|
$('.select2-product').select2({
|
||||||
width: '100%',
|
width: '100%',
|
||||||
placeholder: "Escribe para buscar...",
|
placeholder: "Buscar producto...",
|
||||||
allowClear: true
|
language: { noResults: () => "Sin resultados" }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function agregarFila() {
|
function calcularFila(row) {
|
||||||
itemIndex++;
|
let select = row.find('.product-select');
|
||||||
const container = document.getElementById('items-container');
|
let input = row.find('.quantity-input');
|
||||||
const firstRow = container.querySelector('.item-row');
|
let option = select.find(':selected');
|
||||||
|
let price = parseFloat(option.data('price')) || 0;
|
||||||
// 1. Destruimos Select2 temporalmente para clonar limpio
|
let qty = parseInt(input.val()) || 0;
|
||||||
$('.select2-enable').select2('destroy');
|
let subtotal = price * qty;
|
||||||
|
row.find('.row-total').text('$' + subtotal.toFixed(2));
|
||||||
// 2. Clonamos
|
|
||||||
const newRow = firstRow.cloneNode(true);
|
|
||||||
|
|
||||||
// 3. Reactivamos en los originales
|
|
||||||
inicializarSelect2();
|
|
||||||
|
|
||||||
// Limpiamos valores de la copia
|
|
||||||
const select = newRow.querySelector('select');
|
|
||||||
const input = newRow.querySelector('input');
|
|
||||||
const subtotal = newRow.querySelector('.row-subtotal');
|
|
||||||
|
|
||||||
select.name = `items[${itemIndex}][product_id]`;
|
|
||||||
select.value = "";
|
|
||||||
|
|
||||||
input.name = `items[${itemIndex}][quantity]`;
|
|
||||||
input.value = 1;
|
|
||||||
|
|
||||||
subtotal.innerText = "0.00";
|
|
||||||
|
|
||||||
// Botón Eliminar
|
|
||||||
const deleteBtnDiv = newRow.querySelector('div:last-child');
|
|
||||||
deleteBtnDiv.innerHTML = `
|
|
||||||
<button type="button" onclick="eliminarFila(this)" class="text-red-500 hover:text-red-700">
|
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6 inline">
|
|
||||||
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
|
|
||||||
</svg>
|
|
||||||
</button>
|
|
||||||
`;
|
|
||||||
|
|
||||||
container.appendChild(newRow);
|
|
||||||
|
|
||||||
// 4. Inicializamos en el nuevo
|
|
||||||
inicializarSelect2();
|
|
||||||
|
|
||||||
// Evento extra para Select2
|
|
||||||
$(select).on('select2:select', function (e) {
|
|
||||||
calcularTotal();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function eliminarFila(button) {
|
function recalcularTodo() {
|
||||||
const row = button.closest('.item-row');
|
let grandTotal = 0;
|
||||||
row.remove();
|
$('.item-row').each(function() {
|
||||||
calcularTotal();
|
let row = $(this);
|
||||||
|
let price = parseFloat(row.find('.product-select').find(':selected').data('price')) || 0;
|
||||||
|
let qty = parseInt(row.find('.quantity-input').val()) || 0;
|
||||||
|
grandTotal += (price * qty);
|
||||||
|
});
|
||||||
|
$('#grand-total').text('$' + grandTotal.toFixed(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
function calcularTotal() {
|
function actualizarContadorItems() {
|
||||||
let total = 0;
|
$('#item-count').text($('.item-row').length);
|
||||||
const rows = document.querySelectorAll('.item-row');
|
|
||||||
|
|
||||||
rows.forEach(row => {
|
|
||||||
const select = row.querySelector('.product-select');
|
|
||||||
const input = row.querySelector('.quantity-input');
|
|
||||||
const subtotalSpan = row.querySelector('.row-subtotal');
|
|
||||||
|
|
||||||
const selectedOption = $(select).find(':selected');
|
|
||||||
const price = parseFloat(selectedOption.attr('data-price')) || 0;
|
|
||||||
const quantity = parseInt(input.value) || 0;
|
|
||||||
|
|
||||||
const subtotal = price * quantity;
|
|
||||||
subtotalSpan.innerText = subtotal.toFixed(2);
|
|
||||||
|
|
||||||
total += subtotal;
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById('total-display').innerText = total.toFixed(2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$(document).on('select2:select', '.product-select', function (e) {
|
function actualizarBloqueos() {
|
||||||
calcularTotal();
|
let seleccionados = [];
|
||||||
|
$('.product-select').each(function() {
|
||||||
|
let valor = $(this).val();
|
||||||
|
if (valor) seleccionados.push(valor);
|
||||||
});
|
});
|
||||||
</script>
|
|
||||||
@endpush
|
$('.product-select').each(function() {
|
||||||
|
let selectActual = $(this);
|
||||||
|
let miValor = selectActual.val();
|
||||||
|
selectActual.find('option').each(function() {
|
||||||
|
let opcion = $(this);
|
||||||
|
let valorOpcion = opcion.val();
|
||||||
|
if (!valorOpcion) return;
|
||||||
|
|
||||||
|
if (seleccionados.includes(valorOpcion) && valorOpcion != miValor) {
|
||||||
|
opcion.prop('disabled', true);
|
||||||
|
} else {
|
||||||
|
opcion.prop('disabled', false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</x-layout>
|
||||||
+1
-1
@@ -47,7 +47,7 @@ Route::middleware(['auth'])->group(function () {
|
|||||||
|
|
||||||
//Route::get('/sales', [SaleController::class, 'index'])->name('sales.index');
|
//Route::get('/sales', [SaleController::class, 'index'])->name('sales.index');
|
||||||
Route::get('/sales/create', [SaleController::class, 'create'])->name('sales.create');
|
Route::get('/sales/create', [SaleController::class, 'create'])->name('sales.create');
|
||||||
Route::get('/sales/store', [SaleController::class, 'store'])->name('sales.store');
|
Route::post('/sales/store', [SaleController::class, 'store'])->name('sales.store');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user