creo que pude instalar select2, create sales y crud client o casi crud
This commit is contained in:
@@ -3,78 +3,77 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Product;
|
||||
use App\Models\Product;
|
||||
use App\Models\Client;
|
||||
use App\Models\Sale;
|
||||
use App\Models\SaleDetail;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
//
|
||||
use App\Models\Client;
|
||||
|
||||
class SaleController extends Controller
|
||||
{
|
||||
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();
|
||||
|
||||
$clients = \App\Models\Client::orderBy('name')->get();
|
||||
$clients = Client::orderBy('name')->get();
|
||||
|
||||
return view('sales.create', compact('products', 'clients'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
// Validación estricta
|
||||
$request->validate([
|
||||
'client_id' => 'nullable|exists:clients,id',
|
||||
'payment_method' => 'required|string',
|
||||
'client_id' => 'nullable|exists:clients,id',
|
||||
'items' => 'required|array',
|
||||
'items' => 'required|array',
|
||||
'items.*.product_id' => 'required|exists:products,id',
|
||||
'items.*.quantity' => 'required|integer|min:1',
|
||||
'items.*.quantity' => 'required|integer|min:1',
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::transaction(function () use ($request) {
|
||||
$totalSale = 0;
|
||||
|
||||
$totalVenta = 0;
|
||||
|
||||
foreach ($request->items as $item) {
|
||||
$product = Product::find($item['product_id']);
|
||||
$totalSale += $product->precio * $item['quantity'];
|
||||
// 1. Calcular total previo (Solo habrá 1 item por ahora, pero el bucle sirve igual)
|
||||
foreach ($request->items as $itemData) {
|
||||
$product = Product::findOrFail($itemData['product_id']);
|
||||
|
||||
if ($product->stock_quantity < $item['quantity']) {
|
||||
throw new \Exception("No hay suficiente stock de " . $product->nombre);
|
||||
if ($product->stock_quantity < $itemData['quantity']) {
|
||||
throw new \Exception("Stock insuficiente para: " . $product->name);
|
||||
}
|
||||
|
||||
$totalVenta += $product->price * $itemData['quantity'];
|
||||
}
|
||||
|
||||
//cabecera
|
||||
// 2. Crear Venta
|
||||
$sale = Sale::create([
|
||||
//'user_id' => auth()->id(), //empleado logueado
|
||||
'client_id' => $request->client_id,
|
||||
'total' => $totalSale,
|
||||
'client_id' => $request->client_id,
|
||||
'total' => $totalVenta,
|
||||
'payment_method' => $request->payment_method,
|
||||
// 'user_id' => auth()->id(), // Descomenta si usas autenticación
|
||||
]);
|
||||
|
||||
foreach ($request->items as $item) {
|
||||
$product = Product::find($item['product_id']);
|
||||
// 3. Guardar Detalle y Restar Stock
|
||||
foreach ($request->items as $itemData) {
|
||||
$product = Product::findOrFail($itemData['product_id']);
|
||||
|
||||
SaleDetail::create([
|
||||
'sale_id' => $sale->id,
|
||||
'sale_id' => $sale->id,
|
||||
'product_id' => $product->id,
|
||||
'quantity' => $item['quantity'],
|
||||
'price' => $product->price,
|
||||
'quantity' => $itemData['quantity'],
|
||||
'price' => $product->price,
|
||||
]);
|
||||
$nuevoStock = $product->stock_quantity - $item['quantity'];
|
||||
$product->stock_quantity = $nuevoStock;
|
||||
$product->save();
|
||||
|
||||
$product->decrement('stock_quantity', $itemData['quantity']);
|
||||
}
|
||||
});
|
||||
|
||||
// 5. REDIRECCIÓN DE ÉXITO
|
||||
// Redirigimos al usuario con un mensaje flash [cite: 668]
|
||||
return redirect()->route('sales.create')->with('success', '¡Venta registrada correctamente!');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// Si algo falló (ej: falta stock), volvemos atrás con el error
|
||||
return back()->with('error', 'Error en la venta: ' . $e->getMessage());
|
||||
return back()->with('error', $e->getMessage())->withInput();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user