diff --git a/app/Http/Controllers/App/InventoryController.php b/app/Http/Controllers/App/InventoryController.php index d517be1..5db920d 100644 --- a/app/Http/Controllers/App/InventoryController.php +++ b/app/Http/Controllers/App/InventoryController.php @@ -133,22 +133,31 @@ public function downloadTemplate() $exampleData = [ [ - 'nombre' => 'Producto Ejemplo 1', - 'sku' => 'PROD-001', + 'nombre' => 'Samsung Galaxy A55', + 'sku' => 'SAM-A55-BLK', 'categoria' => 'Electrónica', - 'stock' => 10, - 'costo' => 100.00, - 'precio_venta' => 150.00, + 'stock' => 15, + 'costo' => 5000.00, + 'precio_venta' => 7500.00, 'impuesto' => 16 ], [ - 'nombre' => 'Producto Ejemplo 2', - 'sku' => 'PROD-002', - 'categoria' => 'Alimentos', - 'stock' => 50, - 'costo' => 25.50, - 'precio_venta' => 35.00, - 'impuesto' => 0 + 'nombre' => 'Coca Cola 600ml', + 'sku' => 'COCA-600', + 'categoria' => 'Bebidas', + 'stock' => 100, + 'costo' => 12.50, + 'precio_venta' => 18.00, + 'impuesto' => 8 + ], + [ + 'nombre' => 'Laptop HP Pavilion 15', + 'sku' => 'HP-LAP-15', + 'categoria' => 'Computadoras', + 'stock' => 5, + 'costo' => 8500.00, + 'precio_venta' => 12000.00, + 'impuesto' => 16 ], ]; diff --git a/app/Http/Requests/App/InventoryImportRequest.php b/app/Http/Requests/App/InventoryImportRequest.php index f117ba7..26dcfab 100644 --- a/app/Http/Requests/App/InventoryImportRequest.php +++ b/app/Http/Requests/App/InventoryImportRequest.php @@ -53,7 +53,7 @@ public static function rowRules(): array 'categoria' => ['nullable', 'string', 'max:100'], 'stock' => ['required', 'integer', 'min:0'], 'costo' => ['required', 'numeric', 'min:0'], - 'precio_venta' => ['required', 'numeric', 'min:0', 'gt:costo'], + 'precio_venta' => ['required', 'numeric', 'min:0'], 'impuesto' => ['nullable', 'numeric', 'min:0', 'max:100'], ]; } diff --git a/app/Imports/ProductsImport.php b/app/Imports/ProductsImport.php index 34c9887..8d9676f 100644 --- a/app/Imports/ProductsImport.php +++ b/app/Imports/ProductsImport.php @@ -10,8 +10,9 @@ use Maatwebsite\Excel\Concerns\WithHeadingRow; use Maatwebsite\Excel\Concerns\WithValidation; use Maatwebsite\Excel\Concerns\Importable; -use Maatwebsite\Excel\Concerns\WithBatchInserts; use Maatwebsite\Excel\Concerns\WithChunkReading; +use Maatwebsite\Excel\Concerns\SkipsEmptyRows; +use Illuminate\Support\Facades\Log; /** * Import de productos desde Excel @@ -25,7 +26,7 @@ * - precio_venta: Precio de venta (requerido, mayor que costo) * - impuesto: Porcentaje de impuesto (opcional, 0-100) */ -class ProductsImport implements ToModel, WithHeadingRow, WithValidation, WithBatchInserts, WithChunkReading +class ProductsImport implements ToModel, WithHeadingRow, WithValidation, WithChunkReading, SkipsEmptyRows { use Importable; @@ -38,7 +39,22 @@ class ProductsImport implements ToModel, WithHeadingRow, WithValidation, WithBat */ public function model(array $row) { + // Ignorar filas completamente vacías + if (empty($row['nombre']) && empty($row['sku']) && empty($row['stock'])) { + return null; + } + try { + // Validar que el precio de venta sea mayor que el costo + $costo = (float) $row['costo']; + $precioVenta = (float) $row['precio_venta']; + + if ($precioVenta <= $costo) { + $this->skipped++; + $this->errors[] = "Fila con producto '{$row['nombre']}': El precio de venta ($precioVenta) debe ser mayor que el costo ($costo)"; + return null; + } + // Buscar o crear categoría si se proporciona $categoryId = null; if (!empty($row['categoria'])) { @@ -49,20 +65,20 @@ public function model(array $row) $categoryId = $category->id; } - // Crear el producto en inventario - $inventory = Inventory::create([ - 'name' => trim($row['nombre']), - 'sku' => !empty($row['sku']) ? trim($row['sku']) : null, - 'category_id' => $categoryId, - 'stock' => (int) $row['stock'], - 'is_active' => true, - ]); + // Crear el producto en inventario (solo con campos específicos) + $inventory = new Inventory(); + $inventory->name = trim($row['nombre']); + $inventory->sku = !empty($row['sku']) ? trim($row['sku']) : null; + $inventory->category_id = $categoryId; + $inventory->stock = (int) $row['stock']; + $inventory->is_active = true; + $inventory->save(); // Crear el precio del producto Price::create([ 'inventory_id' => $inventory->id, - 'cost' => (float) $row['costo'], - 'retail_price' => (float) $row['precio_venta'], + 'cost' => $costo, + 'retail_price' => $precioVenta, 'tax' => !empty($row['impuesto']) ? (float) $row['impuesto'] : 0, ]); @@ -92,14 +108,6 @@ public function customValidationMessages() return InventoryImportRequest::rowMessages(); } - /** - * Batch insert size - */ - public function batchSize(): int - { - return 100; - } - /** * Chunk size for reading */