g
This commit is contained in:
@@ -12,26 +12,36 @@ class CatalogoController extends Controller
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
// Consulta base
|
||||
// --- 1. CONSULTA PARA LA GRILLA (PAGINADA) ---
|
||||
$query = Product::query();
|
||||
|
||||
// Lógica del Buscador: Si recibimos algo en el input "search"
|
||||
// Lógica del Buscador tradicional (por si seguís usando un input de texto además de Select2)
|
||||
if ($request->has('search')) {
|
||||
$searchTerm = $request->input('search');
|
||||
|
||||
$query->where(function($q) use ($searchTerm) {
|
||||
$q->where('name', 'like', "%{$searchTerm}%")
|
||||
->orWhere('sku', 'like', "%{$searchTerm}%");
|
||||
->orWhere('sku', 'like', "%{$searchTerm}%");
|
||||
});
|
||||
}
|
||||
|
||||
// Filtros base para la grilla
|
||||
$query->whereIn('type', ['bike', 'accessory', 'clothing', 'spare']);
|
||||
$query->where('stock_quantity', '>', 0); // Solo mostrar si tiene stock
|
||||
|
||||
// Resultados paginados
|
||||
$products = $query->paginate(12)->withQueryString(); // withQueryString mantiene la búsqueda al cambiar de página
|
||||
$products = $query->paginate(12)->withQueryString();
|
||||
|
||||
// Devuelve la vista
|
||||
return view('catalogo.index', compact('products'));
|
||||
// --- 2. CONSULTA PARA EL BUSCADOR SELECT2 (TODOS) ---
|
||||
// Traemos todos los productos válidos para llenar el desplegable de búsqueda rápida.
|
||||
// Hacemos la misma validación de stock y tipo para no mostrar cosas agotadas.
|
||||
$allProducts = Product::whereIn('type', ['bike', 'accessory', 'clothing', 'spare'])
|
||||
->where('stock_quantity', '>', 0)
|
||||
->orderBy('name') // Ordenados alfabéticamente
|
||||
->get();
|
||||
|
||||
// Devuelve la vista pasando ambas variables
|
||||
return view('catalogo.index', compact('products', 'allProducts'));
|
||||
}
|
||||
|
||||
public function show(Product $product)
|
||||
|
||||
@@ -14,35 +14,32 @@ class ProductosController extends Controller
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
// Recuperamos lo escrito en el buscador (si aplica)
|
||||
$query = $request->input('search');
|
||||
$status = $request->input('stock_status');
|
||||
|
||||
// Construimos la consulta
|
||||
$products = Product::query()
|
||||
->when($query, function ($q) use ($query) {
|
||||
// Filtra por nombre o SKU
|
||||
return $q->where('name', 'like', "%{$query}%")
|
||||
->orWhere('sku', 'like', "%{$query}%");
|
||||
// CORREGIDO: Filtramos solo por description
|
||||
return $q->where('name', 'like', "%{$query}%");
|
||||
})
|
||||
->when($status, function ($q) use ($status) {
|
||||
if ($status === 'low') {
|
||||
// Rojo: Menor o igual a la alerta
|
||||
return $q->whereColumn('stock_quantity', '<', 'min_stock_alert');
|
||||
return $q->whereColumn('stock_quantity', '<', 'min_stock_alert')
|
||||
->where('type', '!=', 'service');
|
||||
}
|
||||
elseif ($status === 'medium') {
|
||||
// Amarillo: Mayor al min y menor o igual al min + 5
|
||||
return $q->whereColumn('stock_quantity', '>=', 'min_stock_alert')
|
||||
->whereRaw('stock_quantity <= (min_stock_alert + 5)');
|
||||
->whereRaw('stock_quantity <= (min_stock_alert + 1)')
|
||||
->where('type', '!=', 'service');
|
||||
}
|
||||
elseif ($status === 'ok') {
|
||||
// Verde: Stock saludable
|
||||
return $q->whereRaw('stock_quantity > (min_stock_alert + 5)');
|
||||
return $q->whereRaw('stock_quantity > (min_stock_alert + 1)')
|
||||
->where('type', '!=', 'service');
|
||||
}
|
||||
})
|
||||
->orderBy('stock_quantity', 'asc') // Ordenamos primero los que tienen poco stock (Alerta visual)
|
||||
->paginate(10) // Paginamos de a 10
|
||||
->withQueryString(); // Mantiene el filtro de búsqueda al cambiar de página
|
||||
->orderBy('stock_quantity', 'asc')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
|
||||
return view('productos.index', compact('products'));
|
||||
}
|
||||
@@ -62,30 +59,21 @@ class ProductosController extends Controller
|
||||
{
|
||||
// 1. Validamos los datos con las nuevas columnas
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'sku' => 'nullable|string|unique:products,sku|max:50', // SKU único
|
||||
'description' => 'nullable|string',
|
||||
'name' => 'required|string|max:255', // CORREGIDO: description en lugar de name
|
||||
'price' => 'required|numeric|min:0',
|
||||
'cost' => 'nullable|numeric|min:0', // Costo opcional
|
||||
'cost' => 'nullable|numeric|min:0',
|
||||
'stock_quantity' => 'required|integer|min:0',
|
||||
'min_stock_alert' => 'required|integer|min:0',
|
||||
'type' => 'required|in:bike,accessory,clothing,spare', // Solo permite estos valores
|
||||
'type' => 'required|in:bike,accessory,clothing,spare,service', // CORREGIDO: agregados nuevos tipos
|
||||
'serial_number' => 'nullable|string|max:100',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
|
||||
]);
|
||||
|
||||
// 2. Si no viene SKU, generamos uno automático (Opcional pero útil)
|
||||
if (empty($validated['sku'])) {
|
||||
$validated['sku'] = 'GEN-' . strtoupper(uniqid());
|
||||
}
|
||||
|
||||
// 3. Validacion de imagenes
|
||||
// 2. Validacion de imagenes
|
||||
if ($request->hasFile('image')) {
|
||||
// Guarda el archivo en storage/app/public/products y devuelve la ruta
|
||||
$path = $request->file('image')->store('products', 'public');
|
||||
$validated['image_path'] = $path;
|
||||
}
|
||||
|
||||
unset($validated['image']);
|
||||
|
||||
// Para no romper la logica del supplier
|
||||
@@ -94,7 +82,7 @@ class ProductosController extends Controller
|
||||
// 3. Creamos el producto
|
||||
Product::create($validated);
|
||||
|
||||
// 4. Redireccionamos con mensaje de éxito (Necesitas el componente Alert en el layout)
|
||||
// 4. Redireccionamos
|
||||
return redirect()->route('productos.index')
|
||||
->with('success', 'Producto creado correctamente.');
|
||||
}
|
||||
@@ -121,34 +109,27 @@ class ProductosController extends Controller
|
||||
public function update(Request $request, Product $product)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
// Validamos que el SKU sea único PERO ignoramos el ID de este producto actual
|
||||
'sku' => ['nullable', 'string', Rule::unique('products')->ignore($product->id)],
|
||||
'description' => 'nullable|string',
|
||||
'name' => 'required|string|max:255', // CORREGIDO
|
||||
'type' => 'required|in:bike,accessory,clothing,spare,service', // CORREGIDO
|
||||
'price' => 'required|numeric|min:0',
|
||||
'cost' => 'nullable|numeric|min:0',
|
||||
'stock_quantity' => 'required|integer|min:0',
|
||||
'min_stock_alert' => 'required|integer|min:0',
|
||||
'type' => 'required|in:bike,accessory,clothing,spare',
|
||||
'serial_number' => 'nullable|string|max:100',
|
||||
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
|
||||
]);
|
||||
|
||||
// 2. Manejo de imagen al actualizar
|
||||
if ($request->hasFile('image')) {
|
||||
// Borrar la imagen anterior
|
||||
if ($product->image_path) {
|
||||
Storage::disk('public')->delete($product->image_path);
|
||||
}
|
||||
|
||||
// Guardar la nueva
|
||||
$path = $request->file('image')->store('products', 'public');
|
||||
$validated['image_path'] = $path;
|
||||
}
|
||||
unset($validated['image']);
|
||||
|
||||
// Para no romper la logica del supplier
|
||||
$validated['suppliers_id'] = 1;
|
||||
|
||||
$product->update($validated);
|
||||
@@ -162,6 +143,10 @@ class ProductosController extends Controller
|
||||
*/
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
if ($product->image_path) {
|
||||
Storage::disk('public')->delete($product->image_path);
|
||||
}
|
||||
|
||||
$product->delete();
|
||||
return redirect()->route('productos.index')
|
||||
->with('success', 'Producto eliminado.');
|
||||
|
||||
Reference in New Issue
Block a user