$row['nombre'] ?? null, 'sku' => isset($row['sku']) ? (string) $row['sku'] : null, 'codigo_barras' => isset($row['codigo_barras']) ? (string) $row['codigo_barras'] : null, 'categoria' => $row['categoria'] ?? null, 'stock' => $row['stock'] ?? null, 'costo' => $row['costo'] ?? null, 'precio_venta' => $row['precio_venta'] ?? null, 'impuesto' => $row['impuesto'] ?? null, 'numeros_serie' => $row['numeros_serie'] ?? null, // Nueva columna: separados por comas ]; } /** * Procesa cada fila del Excel */ 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'])) { $category = Category::firstOrCreate( ['name' => trim($row['categoria'])], ['is_active' => true] ); $categoryId = $category->id; } // Crear el producto en inventario $inventory = new Inventory(); $inventory->name = trim($row['nombre']); $inventory->sku = !empty($row['sku']) ? trim($row['sku']) : null; $inventory->barcode = !empty($row['codigo_barras']) ? trim($row['codigo_barras']) : null; $inventory->category_id = $categoryId; $inventory->stock = 0; // Se calculará automáticamente $inventory->is_active = true; $inventory->save(); // Crear el precio del producto Price::create([ 'inventory_id' => $inventory->id, 'cost' => $costo, 'retail_price' => $precioVenta, 'tax' => !empty($row['impuesto']) ? (float) $row['impuesto'] : 0, ]); // Crear números de serie si se proporcionan if (!empty($row['numeros_serie'])) { $serials = explode(',', $row['numeros_serie']); foreach ($serials as $serial) { $serial = trim($serial); if (!empty($serial)) { InventorySerial::create([ 'inventory_id' => $inventory->id, 'serial_number' => $serial, 'status' => 'disponible', ]); } } } else { // Si no se proporcionan seriales, generar automáticamente $stockQuantity = (int) $row['stock']; for ($i = 1; $i <= $stockQuantity; $i++) { InventorySerial::create([ 'inventory_id' => $inventory->id, 'serial_number' => $inventory->sku . '-' . str_pad($i, 4, '0', STR_PAD_LEFT), 'status' => 'disponible', ]); } } // Sincronizar stock $inventory->syncStock(); $this->imported++; return $inventory; } catch (\Exception $e) { $this->skipped++; $this->errors[] = "Error en fila: " . $e->getMessage(); return null; } } /** * Reglas de validación para cada fila */ public function rules(): array { return InventoryImportRequest::rowRules(); } /** * Mensajes personalizados de validación */ public function customValidationMessages() { return InventoryImportRequest::rowMessages(); } /** * Chunk size for reading */ public function chunkSize(): int { return 100; } /** * Obtener estadísticas de la importación */ public function getStats(): array { return [ 'imported' => $this->imported, 'skipped' => $this->skipped, 'errors' => $this->errors, ]; } }