Merge branch 'giane' of https://github.com/BryamE/ProyectoLauck into bryam
This commit is contained in:
@@ -3,15 +3,28 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Client;
|
||||
|
||||
class ClientController extends Controller
|
||||
{
|
||||
/**
|
||||
* 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'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,7 +40,28 @@ class ClientController extends Controller
|
||||
*/
|
||||
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
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
public function edit(Client $client)
|
||||
{
|
||||
//
|
||||
// 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)
|
||||
{
|
||||
$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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
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
|
||||
{
|
||||
@@ -22,22 +22,22 @@ 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 = 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 {
|
||||
@@ -52,6 +52,7 @@ class SaleController extends Controller
|
||||
if ($product->stock_quantity < $item['quantity']) {
|
||||
throw new \Exception("No hay suficiente stock de " . $product->name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//cabecera
|
||||
@@ -62,29 +63,27 @@ class SaleController extends Controller
|
||||
'payment_method' => $request->payment_method,
|
||||
]);
|
||||
|
||||
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' => $newSale->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']);
|
||||
}
|
||||
return $newSale; // Retornamos la venta creada fuera de la transacción
|
||||
});
|
||||
|
||||
// 4. REDIRECCIÓN AL DETALLE (FACTURA)
|
||||
// Usamos la variable $sale que nos devolvió la transacción
|
||||
// 4. REDIRECCIÓN AL DETALLE
|
||||
return redirect()->route('sales.show', $sale)->with('success', '¡Venta registrada exitosamente!');
|
||||
|
||||
} 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