Merge branch 'feat/bubok_files' into 'main'

Implementada funcionalidad

See merge request jjimenez/safekat!817
This commit is contained in:
Ignacio Martinez Navajas
2025-05-21 06:24:11 +00:00
3 changed files with 97 additions and 15 deletions

View File

@ -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,

View File

@ -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]);
}
}

View File

@ -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');