mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
145 lines
5.3 KiB
PHP
Executable File
145 lines
5.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Libraries;
|
|
|
|
use App\Models\Pedidos\PedidoLineaModel;
|
|
use App\Models\Presupuestos\PresupuestoFicheroModel;
|
|
use Exception;
|
|
use phpseclib3\Net\SFTP;
|
|
|
|
class SafekatFtpClient
|
|
{
|
|
protected SFTP $ftp;
|
|
protected string $host;
|
|
protected int $port;
|
|
protected string $username;
|
|
protected string $password;
|
|
protected string $base_dir;
|
|
protected object $pedido_xml_config;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->pedido_xml_config = config("PresupuestoSFTP");
|
|
$this->host = $this->pedido_xml_config->host;
|
|
$this->username = $this->pedido_xml_config->username;
|
|
$this->password = $this->pedido_xml_config->password;
|
|
$this->port = $this->pedido_xml_config->port;
|
|
$this->base_dir = $this->pedido_xml_config->base_dir;
|
|
$this->ftp = new SFTP($this->host);
|
|
|
|
}
|
|
|
|
public function uploadFilePresupuesto(int $presupuesto_id)
|
|
{
|
|
try {
|
|
$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);
|
|
}
|
|
$this->ftp->put($remoteFile, $value->file_path, mode: $this->ftp::SOURCE_LOCAL_FILE);
|
|
}
|
|
$this->ftp->disconnect();
|
|
} catch (Exception $e) {
|
|
log_message('error', $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
public function removeFiles(int $presupuesto_id)
|
|
{
|
|
try {
|
|
$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];
|
|
$remoteFile = implode("/", [$this->base_dir, "pedidos_files", $rootIdExtern, $filename]);
|
|
$this->ftp->delete($remoteFile);
|
|
}
|
|
$this->ftp->disconnect();
|
|
} catch (Exception $e) {
|
|
log_message('error', $e->getMessage());
|
|
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]);
|
|
}
|
|
|
|
public function downloadZipPresupuesto(int $presupuesto_id, ?string $prefijo = null): ?string
|
|
{
|
|
$modelPedidoLinea = model(PedidoLineaModel::class);
|
|
$model = model(PresupuestoFicheroModel::class);
|
|
|
|
$pedidoLinea = $modelPedidoLinea->findByPresupuesto($presupuesto_id);
|
|
$rootIdExtern = $this->pedido_xml_config->id_offset + $pedidoLinea->pedido_id;
|
|
|
|
$remotePath = implode('/', [$this->base_dir, 'pedidos_files', $rootIdExtern]);
|
|
|
|
$this->ftp->login(username: $this->username, password: $this->password);
|
|
|
|
if (!$this->ftp->is_dir($remotePath)) {
|
|
return null;
|
|
}
|
|
|
|
$files = $model->getFiles($presupuesto_id);
|
|
if (empty($files)) {
|
|
return null;
|
|
}
|
|
|
|
$localTempDir = WRITEPATH . 'zip_presupuestos/' . uniqid("presupuesto_");
|
|
if (!is_dir($localTempDir)) {
|
|
mkdir($localTempDir, 0777, true);
|
|
}
|
|
|
|
foreach ($files as $file) {
|
|
$originalName = $file->nombre ?? basename($file->file_path);
|
|
$prefixedName = $prefijo ? $prefijo . '_' . $originalName : $originalName;
|
|
|
|
$localFile = $localTempDir . '/' . $prefixedName;
|
|
$remoteFile = $remotePath . '/' . basename($file->file_path);
|
|
|
|
$this->ftp->get($remoteFile, $localFile);
|
|
}
|
|
|
|
$zipPath = $localTempDir . '.zip';
|
|
$zip = new \ZipArchive();
|
|
if ($zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) {
|
|
foreach (glob($localTempDir . '/*') as $localFile) {
|
|
$zip->addFile($localFile, basename($localFile));
|
|
}
|
|
$zip->close();
|
|
}
|
|
|
|
// Limpieza temporal
|
|
foreach (glob($localTempDir . '/*') as $localFile) {
|
|
unlink($localFile);
|
|
}
|
|
rmdir($localTempDir);
|
|
|
|
return $zipPath;
|
|
}
|
|
|
|
|
|
|
|
}
|