mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Merge branch 'feat/bubok_files' into 'main'
Implementada funcionalidad See merge request jjimenez/safekat!817
This commit is contained in:
@ -450,6 +450,76 @@ class ImportadorBubok extends BaseResourceController
|
|||||||
], 400);
|
], 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([
|
return $this->respond([
|
||||||
'status' => 200,
|
'status' => 200,
|
||||||
'data' => [
|
'data' => [
|
||||||
@ -458,6 +528,7 @@ class ImportadorBubok extends BaseResourceController
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return $this->respond([
|
return $this->respond([
|
||||||
'status' => 500,
|
'status' => 500,
|
||||||
|
|||||||
@ -40,7 +40,8 @@ class SafekatFtpClient
|
|||||||
public function uploadXML(string $content, string $filename): bool
|
public function uploadXML(string $content, string $filename): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if ($this->xml_enabled == false) return false;
|
if ($this->xml_enabled == false)
|
||||||
|
return false;
|
||||||
$remotePath = implode("/", [$this->base_dir, 'pedidos', 'xml_nuevos']);
|
$remotePath = implode("/", [$this->base_dir, 'pedidos', 'xml_nuevos']);
|
||||||
$this->ftp->login(username: $this->username, password: $this->password);
|
$this->ftp->login(username: $this->username, password: $this->password);
|
||||||
if (!$this->ftp->is_dir($remotePath)) {
|
if (!$this->ftp->is_dir($remotePath)) {
|
||||||
@ -58,7 +59,8 @@ class SafekatFtpClient
|
|||||||
public function uploadFilePresupuesto(int $presupuesto_id)
|
public function uploadFilePresupuesto(int $presupuesto_id)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if ($this->xml_enabled == false) return false;
|
if ($this->xml_enabled == false)
|
||||||
|
return false;
|
||||||
$model = model(PresupuestoFicheroModel::class);
|
$model = model(PresupuestoFicheroModel::class);
|
||||||
$modelPedidoLinea = model(PedidoLineaModel::class);
|
$modelPedidoLinea = model(PedidoLineaModel::class);
|
||||||
$pedidoLinea = $modelPedidoLinea->findByPresupuesto($presupuesto_id);
|
$pedidoLinea = $modelPedidoLinea->findByPresupuesto($presupuesto_id);
|
||||||
@ -103,4 +105,13 @@ class SafekatFtpClient
|
|||||||
throw $e;
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,7 +49,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Swal.close();
|
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) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
Swal.fire('Error', 'No se pudo procesar el ZIP.', 'error');
|
Swal.fire('Error', 'No se pudo procesar el ZIP.', 'error');
|
||||||
|
|||||||
Reference in New Issue
Block a user