cambios para importar el excel
This commit is contained in:
@@ -3,16 +3,14 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\Supplier;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ProductosController extends Controller
|
||||
{
|
||||
/**
|
||||
* Muestra la lista de productos con buscador y paginación.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
/*public function index(Request $request)
|
||||
{
|
||||
$query = $request->input('search');
|
||||
$status = $request->input('stock_status');
|
||||
@@ -40,89 +38,79 @@ class ProductosController extends Controller
|
||||
->where('type', '!=', 'service');
|
||||
}
|
||||
})
|
||||
->orderBy('stock_quantity', 'asc') // Ordena primero los que tienen poco stock
|
||||
->paginate(10) // Muestra de a 10 productos
|
||||
->withQueryString(); // Mantiene los filtros al pasar a la página 2, 3, etc.
|
||||
->orderBy('stock_quantity', 'asc')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
|
||||
return view('productos.index', compact('products'));
|
||||
}*/
|
||||
public function index()
|
||||
{
|
||||
// Traemos todos los productos ordenados (sin paginar, para que JS los pueda buscar todos)
|
||||
$products = Product::orderBy('name', 'asc')->get();
|
||||
|
||||
return view('productos.index', compact('products'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Muestra el formulario de creación.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('productos.create');
|
||||
$suppliers = Supplier::orderBy('name')->get();
|
||||
return view('productos.create', compact('suppliers'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Guarda el producto nuevo en la base de datos.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
// 1. Validamos los datos con las nuevas columnas
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255', // CORREGIDO: description en lugar de name
|
||||
'name' => 'required|string|max:255',
|
||||
'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,service,children,skate,rollers,other',
|
||||
'serial_number' => 'nullable|string|max:100',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
|
||||
'suppliers_id' => 'required|exists:suppliers,id',
|
||||
'description' => 'nullable|string'
|
||||
]);
|
||||
|
||||
// 2. Validacion de imagenes
|
||||
if ($request->hasFile('image')) {
|
||||
$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;
|
||||
|
||||
// 3. Creamos el producto
|
||||
Product::create($validated);
|
||||
|
||||
// 4. Redireccionamos
|
||||
return redirect()->route('productos.index')
|
||||
->with('success', 'Producto creado correctamente.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Muestra el detalle de un producto.
|
||||
*/
|
||||
public function show(Product $product)
|
||||
{
|
||||
return view('productos.show', compact('product'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Muestra el formulario de edición.
|
||||
*/
|
||||
public function edit(Product $product)
|
||||
{
|
||||
return view('productos.edit', compact('product'));
|
||||
$suppliers = Supplier::orderBy('name')->get();
|
||||
return view('productos.edit', compact('product', 'suppliers'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Actualiza el producto existente.
|
||||
*/
|
||||
public function update(Request $request, Product $product)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => 'required|string|max:255', // CORREGIDO
|
||||
'type' => 'required|in:bike,accessory,clothing,spare,service,children,skate,rollers,other', // CORREGIDO
|
||||
'name' => 'required|string|max:255',
|
||||
'type' => 'required|in:bike,accessory,clothing,spare,service,children,skate,rollers,other',
|
||||
'price' => 'required|numeric|min:0',
|
||||
'cost' => 'nullable|numeric|min:0',
|
||||
'stock_quantity' => 'required|integer|min:0',
|
||||
'min_stock_alert' => 'required|integer|min:0',
|
||||
'serial_number' => 'nullable|string|max:100',
|
||||
'image' => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
|
||||
'suppliers_id' => 'required|exists:suppliers,id',
|
||||
'description' => 'nullable|string'
|
||||
]);
|
||||
|
||||
// 2. Manejo de imagen al actualizar
|
||||
if ($request->hasFile('image')) {
|
||||
if ($product->image_path) {
|
||||
Storage::disk('public')->delete($product->image_path);
|
||||
@@ -132,8 +120,6 @@ class ProductosController extends Controller
|
||||
$validated['image_path'] = $path;
|
||||
}
|
||||
unset($validated['image']);
|
||||
|
||||
$validated['suppliers_id'] = 1;
|
||||
|
||||
$product->update($validated);
|
||||
|
||||
@@ -141,9 +127,6 @@ class ProductosController extends Controller
|
||||
->with('success', 'Producto actualizado exitosamente.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Elimina el producto.
|
||||
*/
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
if ($product->image_path) {
|
||||
|
||||
Reference in New Issue
Block a user