diff --git a/ci4/app/Libraries/SafekatFtpClient.php b/ci4/app/Libraries/SafekatFtpClient.php index a5ee2f7e..3b96781e 100644 --- a/ci4/app/Libraries/SafekatFtpClient.php +++ b/ci4/app/Libraries/SafekatFtpClient.php @@ -2,27 +2,33 @@ namespace App\Libraries; -use FtpClient\FtpClient; +use App\Models\Pedidos\PedidoLineaModel; +use App\Models\Presupuestos\PresupuestoFicheroModel; +use Exception; +use phpseclib3\Net\SFTP; -class SafekatFtpClient +class SafekatFtpClient { - protected FtpClient $ftp; + protected SFTP $ftp; protected string $host; protected int $port; protected string $username; protected string $password; protected string $base_dir; - protected object $ftp_config; + protected bool $xml_enabled; + protected object $pedido_xml_config; public function __construct() { - $this->ftp = new FtpClient(); - $this->ftp_config = config("FTP"); - $this->host = $this->ftp_config->host; - $this->username = $this->ftp_config->username; - $this->password = $this->ftp_config->password; - $this->port = $this->ftp_config->port; - $this->base_dir = $this->ftp_config->base_dir; + $this->pedido_xml_config = config("PedidoXML"); + $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->xml_enabled = $this->pedido_xml_config->xml_enabled; + $this->ftp = new SFTP($this->host); + } /** * Upload the content of $filename to the base directory declared in App\Config\FTP.php @@ -34,15 +40,42 @@ class SafekatFtpClient public function uploadXML(string $content, string $filename): bool { try { - $remotePath = implode("/", [$this->base_dir, $filename]); - $this->ftp->connect(host: $this->host, port: $this->port); + if ($this->xml_enabled == false) return false; + $remotePath = implode("/", [$this->base_dir,'xml_nuevos']); $this->ftp->login(username: $this->username, password: $this->password); - $this->ftp->putFromString($remotePath, $content); + if(!$this->ftp->is_dir($remotePath)){ + $this->ftp->mkdir($remotePath,recursive:true); + } + $this->ftp->put($remotePath.'/'.$filename, $content); + return true; } catch (\Throwable $th) { throw $th; - log_message('error',$th->getMessage()); + log_message('error', $th->getMessage()); return false; } } + public function uploadFilePresupuesto(int $presupuesto_id) + { + try { + $model = model(PresupuestoFicheroModel::class); + $modelPedidoLinea = model(PedidoLineaModel::class); + $pedidoLinea = $modelPedidoLinea->findByPresupuesto($presupuesto_id); + $rootIdExtern = 1e6 + $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,$rootIdExtern]); + $remoteFile = implode("/", [$this->base_dir,$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); + } + } catch (Exception $e) { + log_message('error', $e->getMessage()); + throw $e; + } + } }