intento
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ClientController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ use App\Models\Product;
|
||||
use App\Models\Sale;
|
||||
use App\Models\SaleDetail;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
//
|
||||
use App\Models\Client;
|
||||
|
||||
class SaleController extends Controller
|
||||
{
|
||||
@@ -15,13 +17,16 @@ class SaleController extends Controller
|
||||
// Buscamos productos que tengan stock mayor a 0 para mostrar en el selector
|
||||
$products = Product::where('stock', '>', 0)->get();
|
||||
|
||||
return view('sales.create', compact('products'));
|
||||
$clients = \App\Models\Client::orderBy('nombre')->get();
|
||||
|
||||
return view('sales.create', compact('products', 'clients'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'payment_method' => 'required|string',
|
||||
'client_id' => 'nullable|exists:clients,id',
|
||||
'items' => 'required|array',
|
||||
'items.*.product_id' => 'required|exists:products,id',
|
||||
'items.*.quantity' => 'required|integer|min:1',
|
||||
@@ -30,32 +35,36 @@ class SaleController extends Controller
|
||||
try {
|
||||
DB::transaction(function () use ($request) {
|
||||
$totalVenta = 0;
|
||||
|
||||
foreach ($request->items as $item) {
|
||||
$producto = Product::find($item['product_id']);
|
||||
$totalVenta += $producto->price * $item['quantity'];
|
||||
$product = Product::find($item['product_id']);
|
||||
$totalVenta += $product->precio * $item['quantity'];
|
||||
|
||||
if ($producto->stock < $item['quantity']) {
|
||||
throw new \Exception("No hay suficiente stock de " . $producto->name);
|
||||
if ($product->stock < $item['quantity']) {
|
||||
throw new \Exception("No hay suficiente stock de " . $product->nombre);
|
||||
}
|
||||
}
|
||||
|
||||
// raro arreglar
|
||||
//cabecera
|
||||
$sale = Sale::create([
|
||||
'user_id' => auth()->id(), // El empleado logueado
|
||||
//'user_id' => auth()->id(), //empleado logueado
|
||||
'client_id' => $request->client_id,
|
||||
'total' => $totalVenta,
|
||||
'payment_method' => $request->payment_method,
|
||||
]);
|
||||
|
||||
foreach ($request->items as $item) {
|
||||
$producto = Product::find($item['product_id']);
|
||||
$product = Product::find($item['product_id']);
|
||||
|
||||
SaleDetail::create([
|
||||
'sale_id' => $sale->id,
|
||||
'product_id' => $producto->id,
|
||||
'quantity' => $item['quantity'],
|
||||
'price' => $producto->price,
|
||||
'product_id' => $product->id,
|
||||
'cantidad' => $item['quantity'],
|
||||
'precio' => $product->precio,
|
||||
]);
|
||||
$producto->decrement('stock', $item['quantity']);
|
||||
$nuevoStock = $product->stock - $item['quantity'];
|
||||
$product->stock = $nuevoStock;
|
||||
$product->save();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+1
-10
@@ -8,16 +8,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
class Product extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $fillable = [
|
||||
'codigo',
|
||||
'nombre',
|
||||
'costo',
|
||||
'precio',
|
||||
'stock',
|
||||
'min_stock',
|
||||
'categoria',
|
||||
'descripcion'
|
||||
];
|
||||
protected $guarded = [];
|
||||
public function supplier()
|
||||
{
|
||||
return $this->belongsTo(Suppliers::class);
|
||||
|
||||
+2
-5
@@ -3,13 +3,10 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
class Sale extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
protected $fillable = [
|
||||
'metodo_pago'
|
||||
];
|
||||
//use HasFactory;
|
||||
|
||||
// Permitimos asignación masiva para poder guardar rápido
|
||||
protected $guarded = [];
|
||||
|
||||
@@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SaleDetail extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
//use HasFactory;
|
||||
protected $guarded = [];
|
||||
|
||||
// este detalle pertenece a una Venta específica
|
||||
|
||||
@@ -13,13 +13,11 @@ return new class extends Migration
|
||||
{
|
||||
Schema::create('sales', function (Blueprint $table) {
|
||||
$table->id();
|
||||
// 1. Cliente que compra
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
// 2. Datos generales de la venta
|
||||
$table->decimal('total', 10, 2); // Total final del ticket
|
||||
$table->string('metodo_pago'); // Ej: "Efectivo", "Tarjeta", "MercadoPago"
|
||||
//capaz van mas datos ni idea
|
||||
// 3. Fecha y Hora (created_at servirá como fecha de venta)
|
||||
//$table->foreignId('user_id')->constrained();
|
||||
// nullable para que no sea obligatrio
|
||||
$table->foreignId('client_id')->nullable()->constrained();
|
||||
$table->decimal('total', 10, 2);
|
||||
$table->string('payment_method');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
@extends('layouts.app')
|
||||
@section('main')
|
||||
<h1>formulario de crear cliente</h1>
|
||||
|
||||
@endsection
|
||||
@@ -3,15 +3,28 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
|
||||
<title>@yield('title','Lauck - Home')</title>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
|
||||
|
||||
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
|
||||
<title>@yield('title','Lauck - Home')</title>
|
||||
</head>
|
||||
<body>
|
||||
<header>Cabeza</header>
|
||||
<header class="bg-white shadow p-4 mb-4">
|
||||
<h1 class="text-xl font-bold">Lauck Bicicletería</h1>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
@yield('main')
|
||||
</main>
|
||||
|
||||
<footer>Pies</footer>
|
||||
<footer class="text-center p-4 text-gray-500 text-sm mt-8">
|
||||
© {{ date('Y') }} Sistema Lauck
|
||||
</footer>
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
||||
|
||||
@stack('scripts')
|
||||
</body>
|
||||
</html>
|
||||
@@ -7,44 +7,102 @@
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||
<h2 class="text-2xl font-bold mb-6 text-gray-800 border-b pb-2">Nueva Venta en Mostrador</h2>
|
||||
<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="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
|
||||
<strong class="font-bold">¡Éxito!</strong>
|
||||
<span class="block sm:inline">{{ session('success') }}</span>
|
||||
<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') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@if (session('error'))
|
||||
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
|
||||
<strong class="font-bold">Error:</strong>
|
||||
<span class="block sm:inline">{{ session('error') }}</span>
|
||||
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4">
|
||||
<strong class="font-bold">Error:</strong> {{ session('error') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form action="{{ route('sales.store') }}" method="POST">
|
||||
@if ($errors->any())
|
||||
<div class="bg-red-50 text-red-600 p-4 mb-4 rounded border border-red-200">
|
||||
<ul>
|
||||
@foreach ($errors->all() as $error)
|
||||
<li>• {{ $error }}</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
|
||||
|
||||
<form action="{{ route('sales.store') }}" method="POST" id="sale-form">
|
||||
@csrf
|
||||
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div class="mb-6 bg-gray-50 p-4 rounded-lg border border-gray-200">
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Cliente</label>
|
||||
<div class="flex gap-2">
|
||||
<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">
|
||||
<option value="">-- Consumidor Final (Anónimo) --</option>
|
||||
@foreach($clients as $client)
|
||||
<option value="{{ $client->id }}">{{ $client->nombre }} ({{ $client->telefono }})</option>
|
||||
@endforeach
|
||||
</select>
|
||||
|
||||
<div class="col-span-1">
|
||||
<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">
|
||||
+
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="items-container" class="space-y-4">
|
||||
<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="md:col-span-6">
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Producto</label>
|
||||
<select name="items[0][product_id]" class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2 px-3 border" required>
|
||||
<option value="" selected disabled>Seleccione un producto...</option>
|
||||
<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()">
|
||||
<option value="" data-price="0" selected disabled>Seleccione...</option>
|
||||
@foreach($products as $product)
|
||||
<option value="{{ $product->id }}">
|
||||
{{ $product->code }} - {{ $product->name }} (${{ $product->price }})
|
||||
<option value="{{ $product->id }}" data-price="{{ $product->precio }}">
|
||||
{{ $product->codigo }} - {{ $product->nombre }} (${{ $product->precio }})
|
||||
</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="md:col-span-3">
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Cantidad</label>
|
||||
<input type="number" name="items[0][quantity]" value="1" min="1" class="w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 py-2 px-3 border" required>
|
||||
<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()">
|
||||
</div>
|
||||
|
||||
<div class="md:col-span-2 text-right font-mono text-gray-600 self-center pt-6">
|
||||
$<span class="row-subtotal">0.00</span>
|
||||
</div>
|
||||
|
||||
<div class="md:col-span-1 text-right">
|
||||
<button type="button" class="text-gray-400 cursor-not-allowed" disabled>
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
<button type="button" onclick="agregarFila()" class="flex items-center text-indigo-600 hover:text-indigo-800 font-semibold">
|
||||
<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">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
|
||||
</svg>
|
||||
Agregar otro producto
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="flex justify-end mt-6 border-t pt-4">
|
||||
<div class="text-2xl font-bold text-gray-800">
|
||||
Total a Pagar: <span class="text-indigo-600">$<span id="total-display">0.00</span></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-6 grid grid-cols-1 md:grid-cols-2 gap-6 items-center">
|
||||
<div>
|
||||
<label class="block text-sm font-medium text-gray-700 mb-1">Método de Pago</label>
|
||||
<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">
|
||||
@@ -54,19 +112,116 @@
|
||||
<option value="Transferencia">Transferencia</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-8 flex gap-4">
|
||||
<button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2 px-6 rounded shadow-lg transition duration-150 ease-in-out">
|
||||
Confirmar Venta
|
||||
</button>
|
||||
|
||||
<div class="flex items-end gap-4 justify-end">
|
||||
<a href="{{ url('/') }}" class="bg-white hover:bg-gray-100 text-gray-800 font-semibold py-2 px-4 border border-gray-400 rounded shadow">
|
||||
Cancelar
|
||||
</a>
|
||||
<button type="submit" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2 px-6 rounded shadow-lg">
|
||||
Confirmar Venta
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
let itemIndex = 0;
|
||||
|
||||
$(document).ready(function() {
|
||||
inicializarSelect2();
|
||||
});
|
||||
|
||||
function inicializarSelect2() {
|
||||
$('.select2-enable').select2({
|
||||
width: '100%',
|
||||
placeholder: "Escribe para buscar...",
|
||||
allowClear: true
|
||||
});
|
||||
}
|
||||
|
||||
function agregarFila() {
|
||||
itemIndex++;
|
||||
const container = document.getElementById('items-container');
|
||||
const firstRow = container.querySelector('.item-row');
|
||||
|
||||
// 1. Destruimos Select2 temporalmente para clonar limpio
|
||||
$('.select2-enable').select2('destroy');
|
||||
|
||||
// 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) {
|
||||
const row = button.closest('.item-row');
|
||||
row.remove();
|
||||
calcularTotal();
|
||||
}
|
||||
|
||||
function calcularTotal() {
|
||||
let total = 0;
|
||||
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) {
|
||||
calcularTotal();
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
@endsection
|
||||
+2
-1
@@ -3,12 +3,12 @@
|
||||
use App\Http\Controllers\AuthController;
|
||||
use App\Http\Controllers\CatalogoController;
|
||||
use App\Http\Controllers\SaleController;
|
||||
use App\Http\Controllers\ClientController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\HomeController;
|
||||
use App\Http\Controllers\LoginController;
|
||||
use App\Http\Controllers\ProductosController;
|
||||
use App\Http\Controllers\RegisterController;
|
||||
use App\Http\Controllers\SaleController;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
@@ -50,6 +50,7 @@ Route::middleware(['auth'])->group(function () {
|
||||
|
||||
Route::get('/sales', [SaleController::class, 'index'])->name('sales.index');
|
||||
Route::get('/sales/create', [SaleController::class, 'create'])->name('sales.create');
|
||||
Route::resource('clients', ClientController::class);
|
||||
});
|
||||
|
||||
Route::middleware(['auth'])->group(function () {
|
||||
|
||||
Reference in New Issue
Block a user