From cb17d661a8209e03f413650857ffeefa57ab2113 Mon Sep 17 00:00:00 2001 From: imnavajas Date: Tue, 20 May 2025 14:31:17 +0200 Subject: [PATCH] Implementada funcionalidad --- .../Importadores/ImportadorBubok.php | 71 +++++++++++++++++++ ci4/app/Libraries/SafekatFtpClient.php | 39 ++++++---- .../pages/importadores/bubok/bubok_tool.js | 2 +- 3 files changed, 97 insertions(+), 15 deletions(-) diff --git a/ci4/app/Controllers/Importadores/ImportadorBubok.php b/ci4/app/Controllers/Importadores/ImportadorBubok.php index 27641c64..09527b3a 100644 --- a/ci4/app/Controllers/Importadores/ImportadorBubok.php +++ b/ci4/app/Controllers/Importadores/ImportadorBubok.php @@ -450,6 +450,76 @@ class ImportadorBubok extends BaseResourceController ], 400); } + + // Descarga y subida de archivos al SFTP + $presupuestoFicheroModel = model('App\Models\Presupuestos\PresupuestoFicheroModel'); + $ftp = new \App\Libraries\SafekatFtpClient(); + + $archivoUrls = [ + 'cover' => $producto->cover->file ?? null, + 'body' => $producto->body->file ?? null, + ]; + + foreach ($archivoUrls as $tipo => $url) { + if (!$url) + continue; + + try { + $contenido = @file_get_contents($url); // silenciar errores de PHP + + if ($contenido === false || strlen($contenido) === 0) { + // No se pudo descargar el archivo: generar archivo de error para FTP + $errorMessage = "ERROR: No se pudo descargar el archivo remoto para $tipo desde la URL: $url"; + + $remoteDir = $ftp->getPresupuestoRemotePath($response['sk_id']); // crea esta función si no existe + $remoteErrorFile = $remoteDir . '/ERROR_' . strtoupper($tipo) . '.txt'; + + // Crear archivo temporal con el mensaje de error + $tempErrorFile = WRITEPATH . 'uploads/presupuestos/ERROR_' . $tipo . '.txt'; + file_put_contents($tempErrorFile, $errorMessage); + + if (!$ftp->is_dir($remoteDir)) { + $ftp->mkdir($remoteDir, recursive: true); + } + + $ftp->put($remoteErrorFile, $tempErrorFile, $ftp::SOURCE_LOCAL_FILE); + + continue; // no procesar este archivo + } + + // ✅ Procesar normalmente si la descarga tuvo éxito + $nombreOriginal = basename(parse_url($url, PHP_URL_PATH)); + $extension = pathinfo($nombreOriginal, PATHINFO_EXTENSION); + + $nombreLimpio = $presupuestoFicheroModel->saveFileInBBDD( + $response['sk_id'], + $nombreOriginal, + $extension, + auth()->id() + ); + + if (is_null($nombreLimpio)) + continue; + + $rutaLocal = WRITEPATH . 'uploads/presupuestos/'; + if (!is_dir($rutaLocal)) { + mkdir($rutaLocal, 0777, true); + } + + file_put_contents($rutaLocal . $nombreLimpio, $contenido); + } catch (\Throwable $e) { + //log_message('error', 'Error inesperado en descarga de archivo remoto: ' . $e->getMessage()); + } + } + + + // Subir al FTP después de guardar localmente + try { + $ftp->uploadFilePresupuesto($response['sk_id']); + } catch (\Throwable $e) { + log_message('error', 'Error subiendo archivos al FTP: ' . $e->getMessage()); + } + return $this->respond([ 'status' => 200, 'data' => [ @@ -458,6 +528,7 @@ class ImportadorBubok extends BaseResourceController ] ]); + } catch (\Throwable $e) { return $this->respond([ 'status' => 500, diff --git a/ci4/app/Libraries/SafekatFtpClient.php b/ci4/app/Libraries/SafekatFtpClient.php index 663e8c7a..c69b2d85 100755 --- a/ci4/app/Libraries/SafekatFtpClient.php +++ b/ci4/app/Libraries/SafekatFtpClient.php @@ -40,13 +40,14 @@ class SafekatFtpClient public function uploadXML(string $content, string $filename): bool { try { - if ($this->xml_enabled == false) return false; - $remotePath = implode("/", [$this->base_dir,'pedidos','xml_nuevos']); + if ($this->xml_enabled == false) + return false; + $remotePath = implode("/", [$this->base_dir, 'pedidos', 'xml_nuevos']); $this->ftp->login(username: $this->username, password: $this->password); - if(!$this->ftp->is_dir($remotePath)){ - $this->ftp->mkdir($remotePath,recursive:true); + if (!$this->ftp->is_dir($remotePath)) { + $this->ftp->mkdir($remotePath, recursive: true); } - $this->ftp->put($remotePath.'/'.$filename, $content); + $this->ftp->put($remotePath . '/' . $filename, $content); return true; } catch (\Throwable $th) { @@ -58,22 +59,23 @@ class SafekatFtpClient public function uploadFilePresupuesto(int $presupuesto_id) { try { - if ($this->xml_enabled == false) return false; + if ($this->xml_enabled == false) + return false; $model = model(PresupuestoFicheroModel::class); $modelPedidoLinea = model(PedidoLineaModel::class); $pedidoLinea = $modelPedidoLinea->findByPresupuesto($presupuesto_id); $rootIdExtern = $this->pedido_xml_config->id_offset + $pedidoLinea->pedido_id; $presupuestoFiles = $model->getFiles($presupuesto_id); $this->ftp->login(username: $this->username, password: $this->password); - + foreach ($presupuestoFiles as $key => $value) { $filename = array_reverse(explode("/", $value->file_path))[0]; - $remoteDir = implode("/", [$this->base_dir,"pedidos_files",$rootIdExtern]); - $remoteFile = implode("/", [$this->base_dir,"pedidos_files",$rootIdExtern,$filename]); - if(!$this->ftp->is_dir($remoteDir)){ - $this->ftp->mkdir($remoteDir,recursive:true); + $remoteDir = implode("/", [$this->base_dir, "pedidos_files", $rootIdExtern]); + $remoteFile = implode("/", [$this->base_dir, "pedidos_files", $rootIdExtern, $filename]); + if (!$this->ftp->is_dir($remoteDir)) { + $this->ftp->mkdir($remoteDir, recursive: true); } - $this->ftp->put($remoteFile,$value->file_path,mode:$this->ftp::SOURCE_LOCAL_FILE); + $this->ftp->put($remoteFile, $value->file_path, mode: $this->ftp::SOURCE_LOCAL_FILE); } $this->ftp->disconnect(); } catch (Exception $e) { @@ -91,10 +93,10 @@ class SafekatFtpClient $rootIdExtern = $this->pedido_xml_config->id_offset + $pedidoLinea->pedido_id; $presupuestoFiles = $model->getFiles($presupuesto_id); $this->ftp->login(username: $this->username, password: $this->password); - + foreach ($presupuestoFiles as $key => $value) { $filename = array_reverse(explode("/", $value->file_path))[0]; - $remoteFile = implode("/", [$this->base_dir,"pedidos_files",$rootIdExtern,$filename]); + $remoteFile = implode("/", [$this->base_dir, "pedidos_files", $rootIdExtern, $filename]); $this->ftp->delete($remoteFile); } $this->ftp->disconnect(); @@ -103,4 +105,13 @@ class SafekatFtpClient throw $e; } } + + public function getPresupuestoRemotePath(int $presupuesto_id): string + { + $modelPedidoLinea = model(PedidoLineaModel::class); + $pedidoLinea = $modelPedidoLinea->findByPresupuesto($presupuesto_id); + $rootIdExtern = $this->pedido_xml_config->id_offset + $pedidoLinea->pedido_id; + + return implode('/', [$this->base_dir, 'pedidos_files', $rootIdExtern]); + } } diff --git a/httpdocs/assets/js/safekat/pages/importadores/bubok/bubok_tool.js b/httpdocs/assets/js/safekat/pages/importadores/bubok/bubok_tool.js index b86902b1..70077708 100644 --- a/httpdocs/assets/js/safekat/pages/importadores/bubok/bubok_tool.js +++ b/httpdocs/assets/js/safekat/pages/importadores/bubok/bubok_tool.js @@ -49,7 +49,7 @@ document.addEventListener('DOMContentLoaded', function () { } Swal.close(); - Swal.fire('Éxito', `${xmlFiles.length} archivos XML cargados.`, 'success'); + Swal.fire('Éxito', `${xmlFiles.length} archivos XML detectados, proceda a la importación.`, 'success'); } catch (err) { console.error(err); Swal.fire('Error', 'No se pudo procesar el ZIP.', 'error');