From d42f46d280f0367ba74c6d0f712012bc48fa3273 Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 2 Apr 2025 08:16:58 +0200 Subject: [PATCH 1/7] etiqueta impresora --- ci4/app/Config/Routes.php | 5 + ci4/app/Config/Services.php | 6 +- .../Logistica/LogisticaController.php | 31 +++++++ ...5-04-02-074000_ImpresoraEtiquetasTable.php | 80 ++++++++++++++++ .../Seeds/EtiquetaImpresoraSeeder.php | 45 +++++++++ .../Configuracion/ImpresoraEtiquetaEntity.php | 27 ++++++ .../Configuracion/ImpresoraEtiquetaModel.php | 57 ++++++++++++ ci4/app/Services/ImpresoraEtiquetaService.php | 93 +++++++++++++++++++ 8 files changed, 343 insertions(+), 1 deletion(-) create mode 100644 ci4/app/Controllers/Logistica/LogisticaController.php create mode 100644 ci4/app/Database/Migrations/2025-04-02-074000_ImpresoraEtiquetasTable.php create mode 100644 ci4/app/Database/Seeds/EtiquetaImpresoraSeeder.php create mode 100644 ci4/app/Entities/Configuracion/ImpresoraEtiquetaEntity.php create mode 100644 ci4/app/Models/Configuracion/ImpresoraEtiquetaModel.php create mode 100644 ci4/app/Services/ImpresoraEtiquetaService.php diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 58bba4ba..a2a63805 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -1021,6 +1021,11 @@ $routes->group('produccion', ['namespace' => 'App\Controllers\Produccion'], func }); }); }); +$routes->group('logistica', ['namespace' => 'App\Controllers\Logistica'], function ($routes) { + + $routes->get('print/label/test','LogisticaController::print_test_label'); + +}); /* * -------------------------------------------------------------------- * APIs Route Definitions diff --git a/ci4/app/Config/Services.php b/ci4/app/Config/Services.php index cb9b038f..070f7014 100755 --- a/ci4/app/Config/Services.php +++ b/ci4/app/Config/Services.php @@ -4,6 +4,7 @@ namespace Config; use App\Services\ChatService; use App\Services\FTPService; +use App\Services\ImpresoraEtiquetaService; use App\Services\MaquinaService; use App\Services\MessageService; use App\Services\PapelImpresionService; @@ -60,7 +61,10 @@ class Services extends BaseService { return new ChatService(); } - + public static function impresora_etiqueta() + { + return new ImpresoraEtiquetaService(); + } public static function emailService($getShared = true) { if ($getShared) { diff --git a/ci4/app/Controllers/Logistica/LogisticaController.php b/ci4/app/Controllers/Logistica/LogisticaController.php new file mode 100644 index 00000000..25f5819d --- /dev/null +++ b/ci4/app/Controllers/Logistica/LogisticaController.php @@ -0,0 +1,31 @@ +impresoraEtiquetaService = service('impresora_etiqueta'); + $this->locale = session()->get('lang'); + + parent::initController($request, $response, $logger); + } + public function print_test_label() + { + $etiquetaData = $this->impresoraEtiquetaService->test(); + $responseMessage = $etiquetaData["status"] ? "OK" : "ERROR"; + return $this->response->setJSON(["message" => $responseMessage, "data" => $etiquetaData, "status" => $etiquetaData["status"]]); + } +} diff --git a/ci4/app/Database/Migrations/2025-04-02-074000_ImpresoraEtiquetasTable.php b/ci4/app/Database/Migrations/2025-04-02-074000_ImpresoraEtiquetasTable.php new file mode 100644 index 00000000..4b76dac9 --- /dev/null +++ b/ci4/app/Database/Migrations/2025-04-02-074000_ImpresoraEtiquetasTable.php @@ -0,0 +1,80 @@ + [ + 'type' => 'INT', + 'unsigned' => true, + 'auto_increment' => true, + ], + 'name' => [ + 'type' => 'VARCHAR', + 'constraint' => 255 + ], + 'tipo' => [ + 'type' => 'INT', + 'unsigned' => true, + 'default' => 1, + ], + 'ip' => [ + 'type' => 'VARCHAR', + 'constraint' => 255 + ], + 'port' => [ + 'type' => 'INT', + 'unsigned' => true, + ], + 'user' => [ + 'type' => 'VARCHAR', + 'constraint' => 255, + 'null' => true, + ], + 'pass' => [ + 'type' => 'VARCHAR', + 'constraint' => 255, + 'null' => true, + + ], + 'description' => [ + 'type' => 'LONGTEXT', + 'null' => true, + ], + + + ]; + + public function up() + { + $this->forge->addField($this->COLUMNS); + $currenttime = new RawSql('CURRENT_TIMESTAMP'); + $this->forge->addField([ + 'created_at' => [ + 'type' => 'TIMESTAMP', + 'default' => $currenttime, + ], + 'updated_at' => [ + 'type' => 'TIMESTAMP', + 'null' => true, + ], + 'deleted_at' => [ + 'type' => 'TIMESTAMP', + 'null' => true, + + ], + ]); + $this->forge->addPrimaryKey('id'); + $this->forge->createTable("etiqueta_impresoras"); + } + + public function down() + { + $this->forge->dropTable("etiqueta_impresoras"); + } +} diff --git a/ci4/app/Database/Seeds/EtiquetaImpresoraSeeder.php b/ci4/app/Database/Seeds/EtiquetaImpresoraSeeder.php new file mode 100644 index 00000000..391d7b40 --- /dev/null +++ b/ci4/app/Database/Seeds/EtiquetaImpresoraSeeder.php @@ -0,0 +1,45 @@ + "GC420d", + "tipo" => 0, + "ip" => "83.61.19.226", + "port" => 9101, + "description" => "cat rutadocumento | netcat -w 1 83.61.19.226 9101" + ], + [ + "name" => "LABPRINT-1", + "tipo" => 1, + "ip" => "83.61.19.226", + "port" => 9102, + "user" => "52J153500357" + ], + [ + "name" => "LABPRINT-2", + "tipo" => 1, + "ip" => "83.61.19.226", + "port" => 9103, + "user" => "52J153500357" + + ] + ]; + $etiquetaImpresoraModel = model(ImpresoraEtiquetaModel::class); + foreach ($data as $key => $impresoraData) { + $impresoraCount = $etiquetaImpresoraModel->where('name',$impresoraData["name"])->countAllResults(); + if($impresoraCount == 0){ + $etiquetaImpresoraModel->insert($impresoraData); + } + } + } +} diff --git a/ci4/app/Entities/Configuracion/ImpresoraEtiquetaEntity.php b/ci4/app/Entities/Configuracion/ImpresoraEtiquetaEntity.php new file mode 100644 index 00000000..8ab17273 --- /dev/null +++ b/ci4/app/Entities/Configuracion/ImpresoraEtiquetaEntity.php @@ -0,0 +1,27 @@ + null, + "name" => null, + "tipo" => null, + "ip" => null, + "port" => null, + "user" => null, + "pass" => null, + "description" => null + ]; + protected $casts = [ + "name" => "string", + "tipo" => "integer", + "ip" => "string", + "port" => "integer", + "user" => "string", + "pass" => "?string", + "description" => "?string" + ]; + +} diff --git a/ci4/app/Models/Configuracion/ImpresoraEtiquetaModel.php b/ci4/app/Models/Configuracion/ImpresoraEtiquetaModel.php new file mode 100644 index 00000000..6524c41f --- /dev/null +++ b/ci4/app/Models/Configuracion/ImpresoraEtiquetaModel.php @@ -0,0 +1,57 @@ + "LABPRINT-1", + "header" => [ + "_FORMAT" => "E:PEDIDO.ZPL", + "_QUANTITY" => 1, + "_PRINBTERNAME" => "Printer 1", + "_JOBNAME" => "LBL101" + ], + "labels" => [ + [ + "cliente" => "Cliente Potencial", + "titulo" => "[1234] Ejemplo", + "cantidad" => 100, + "tirada" => 50, + "cajas" => 1, + "ean" => null, + "nombre" => "___Nombre___", + "direccion" => "C/ test n10, Madrid, 12345, España", + "notas" => "Nota....", + "refcliente" => "Refclient:1234", + "npedido" => "1234" + ] + ] + ]; + + public function __construct() + { + $this->impresoraEtiquetaModel = model(ImpresoraEtiquetaModel::class); + } + public function test(): array + { + try { + $status = false; + $content = ""; + $impresora = $this->impresoraEtiquetaModel->where("name", $this->TEST["printer"])->first(); + if ($impresora) { + $content = $this->createEtiqueta($this->TEST); + $status = $this->sendToImpresoraEtiqueta(1234, $content, $impresora); + } + return ["impresora" => $impresora, "content" => $content, "status" => $status]; + } catch (Throwable $th) { + return ["impresora" => $impresora, "content" => $th->getMessage(), "status" => $status]; + } + } + protected function createEtiqueta(array $data_label = []): ?string + { + $xml = new DOMDocument('1.0', 'utf-8'); + $labels = $xml->createElement("labels"); + foreach ($data_label["header"] as $key => $value) { + $labels->setAttribute($key, $value); + } + foreach ($data_label["labels"] as $label) { + $labelChild = $labels->appendChild($xml->createElement('label')); + foreach ($label as $attrName => $attributeValue) { + $variableChild = $labels->appendChild($xml->createElement('variable')); + $variableChild->setAttribute("name", $attrName); + $variableChild->append((string) $attributeValue); + $labelChild->appendChild($variableChild); + } + } + $xml->appendChild($labels); + return $xml->saveXML(); + } + protected function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool + { + $ftp = new SFTP($impresoraEtiqueta->ip, $impresoraEtiqueta->port); + $isLoginSuccess = $ftp->login(username: $impresoraEtiqueta->user, password: $impresoraEtiqueta->pass ?? ''); + if ($isLoginSuccess) { + $status = $ftp->put($name, $content); + } else { + $status = false; + } + return $status; + } +} From b4a835bbfd80f40d70e3db8b6f69d529e79e8760 Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 2 Apr 2025 13:42:24 +0200 Subject: [PATCH 2/7] fix ftp send printer command --- ci4/app/Services/ImpresoraEtiquetaService.php | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/ci4/app/Services/ImpresoraEtiquetaService.php b/ci4/app/Services/ImpresoraEtiquetaService.php index 19e4e851..5a797f3a 100644 --- a/ci4/app/Services/ImpresoraEtiquetaService.php +++ b/ci4/app/Services/ImpresoraEtiquetaService.php @@ -9,6 +9,8 @@ use DOMDocument; use DOMElement; use DOMText; use DOMNode; +use Error; +use Exception; use phpseclib3\Net\SFTP; use Throwable; @@ -81,13 +83,25 @@ class ImpresoraEtiquetaService extends BaseService } protected function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool { - $ftp = new SFTP($impresoraEtiqueta->ip, $impresoraEtiqueta->port); - $isLoginSuccess = $ftp->login(username: $impresoraEtiqueta->user, password: $impresoraEtiqueta->pass ?? ''); - if ($isLoginSuccess) { - $status = $ftp->put($name, $content); - } else { - $status = false; + $conn = @ftp_connect($impresoraEtiqueta->ip,$impresoraEtiqueta->port); + if(!$conn){ + throw new Exception('Error al establecer conexión FTP'); } + $isLoginSuccess = @ftp_login($conn, $impresoraEtiqueta->user, $impresoraEtiqueta->pass); + if ($isLoginSuccess) { + } else { + throw new Exception('Error haciendo login en el servidor FTP'); + } + if (!ftp_pasv($conn, true)) { + throw new Exception('Error entrando en modo pasivo'); + } + if (ftp_put($conn, $name, $content, FTP_ASCII) === FALSE) { + $status = false; + ftp_close($conn); + }else{ + $status = true; + } + return $status; } } From 41291c82fdca670cf8bb20859d34b76974658810 Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 2 Apr 2025 13:53:41 +0200 Subject: [PATCH 3/7] fx --- ci4/app/Services/ImpresoraEtiquetaService.php | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/ci4/app/Services/ImpresoraEtiquetaService.php b/ci4/app/Services/ImpresoraEtiquetaService.php index 5a797f3a..19e4e851 100644 --- a/ci4/app/Services/ImpresoraEtiquetaService.php +++ b/ci4/app/Services/ImpresoraEtiquetaService.php @@ -9,8 +9,6 @@ use DOMDocument; use DOMElement; use DOMText; use DOMNode; -use Error; -use Exception; use phpseclib3\Net\SFTP; use Throwable; @@ -83,25 +81,13 @@ class ImpresoraEtiquetaService extends BaseService } protected function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool { - $conn = @ftp_connect($impresoraEtiqueta->ip,$impresoraEtiqueta->port); - if(!$conn){ - throw new Exception('Error al establecer conexión FTP'); - } - $isLoginSuccess = @ftp_login($conn, $impresoraEtiqueta->user, $impresoraEtiqueta->pass); + $ftp = new SFTP($impresoraEtiqueta->ip, $impresoraEtiqueta->port); + $isLoginSuccess = $ftp->login(username: $impresoraEtiqueta->user, password: $impresoraEtiqueta->pass ?? ''); if ($isLoginSuccess) { + $status = $ftp->put($name, $content); } else { - throw new Exception('Error haciendo login en el servidor FTP'); - } - if (!ftp_pasv($conn, true)) { - throw new Exception('Error entrando en modo pasivo'); - } - if (ftp_put($conn, $name, $content, FTP_ASCII) === FALSE) { $status = false; - ftp_close($conn); - }else{ - $status = true; } - return $status; } } From 492c7366cff2033eb7db351a987a0b09492fca73 Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 2 Apr 2025 13:54:51 +0200 Subject: [PATCH 4/7] add ftp send printer --- ci4/app/Services/ImpresoraEtiquetaService.php | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/ci4/app/Services/ImpresoraEtiquetaService.php b/ci4/app/Services/ImpresoraEtiquetaService.php index 19e4e851..63f8ae9a 100644 --- a/ci4/app/Services/ImpresoraEtiquetaService.php +++ b/ci4/app/Services/ImpresoraEtiquetaService.php @@ -6,11 +6,8 @@ use App\Entities\Configuracion\ImpresoraEtiquetaEntity; use App\Models\Configuracion\ImpresoraEtiquetaModel; use CodeIgniter\Config\BaseService; use DOMDocument; -use DOMElement; -use DOMText; -use DOMNode; -use phpseclib3\Net\SFTP; use Throwable; +use Exception; class ImpresoraEtiquetaService extends BaseService { @@ -81,13 +78,25 @@ class ImpresoraEtiquetaService extends BaseService } protected function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool { - $ftp = new SFTP($impresoraEtiqueta->ip, $impresoraEtiqueta->port); - $isLoginSuccess = $ftp->login(username: $impresoraEtiqueta->user, password: $impresoraEtiqueta->pass ?? ''); - if ($isLoginSuccess) { - $status = $ftp->put($name, $content); - } else { - $status = false; + $conn = @ftp_connect($impresoraEtiqueta->ip,$impresoraEtiqueta->port); + if(!$conn){ + throw new Exception('Error al establecer conexión FTP'); } + $isLoginSuccess = @ftp_login($conn, $impresoraEtiqueta->user, $impresoraEtiqueta->pass); + if ($isLoginSuccess) { + } else { + throw new Exception('Error haciendo login en el servidor FTP'); + } + if (!ftp_pasv($conn, true)) { + throw new Exception('Error entrando en modo pasivo'); + } + if (ftp_put($conn, $name, $content, FTP_ASCII) === FALSE) { + $status = false; + ftp_close($conn); + }else{ + $status = true; + } + return $status; } } From 9924f27b5bff724eeba53a5974d9a0de17657b56 Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 2 Apr 2025 14:00:10 +0200 Subject: [PATCH 5/7] tmp file --- ci4/app/Services/ImpresoraEtiquetaService.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ci4/app/Services/ImpresoraEtiquetaService.php b/ci4/app/Services/ImpresoraEtiquetaService.php index 63f8ae9a..3e5d3408 100644 --- a/ci4/app/Services/ImpresoraEtiquetaService.php +++ b/ci4/app/Services/ImpresoraEtiquetaService.php @@ -78,6 +78,12 @@ class ImpresoraEtiquetaService extends BaseService } protected function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool { + $tmpFile = tmpfile(); + + fwrite($tmpFile, $content); + rewind($tmpFile); + + $tmpMetaData = stream_get_meta_data($tmpFile); $conn = @ftp_connect($impresoraEtiqueta->ip,$impresoraEtiqueta->port); if(!$conn){ throw new Exception('Error al establecer conexión FTP'); @@ -90,7 +96,7 @@ class ImpresoraEtiquetaService extends BaseService if (!ftp_pasv($conn, true)) { throw new Exception('Error entrando en modo pasivo'); } - if (ftp_put($conn, $name, $content, FTP_ASCII) === FALSE) { + if (ftp_put($conn, $name, $tmpMetaData['uri'], FTP_ASCII) === FALSE) { $status = false; ftp_close($conn); }else{ From c960fd853996a2f278a50c02d15a4fbc1bf632c3 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 2 Apr 2025 14:03:32 +0200 Subject: [PATCH 6/7] Tunning parameters! --- .vscode/sftp.json | 5 +++++ ci4/app/Services/ImpresoraEtiquetaService.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.vscode/sftp.json b/.vscode/sftp.json index c955066f..89ef7722 100755 --- a/.vscode/sftp.json +++ b/.vscode/sftp.json @@ -23,6 +23,11 @@ "host": "sk-dev.imnavajas.es", "username": "sk-dev", "password": "KXvYsubai9v*g61~" + }, + "prod":{ + "host": "erp.safekat.es", + "username": "erp-demo", + "password": "lNkEyukTc1~*3yy9" } } diff --git a/ci4/app/Services/ImpresoraEtiquetaService.php b/ci4/app/Services/ImpresoraEtiquetaService.php index 3e5d3408..2af5a75a 100644 --- a/ci4/app/Services/ImpresoraEtiquetaService.php +++ b/ci4/app/Services/ImpresoraEtiquetaService.php @@ -24,7 +24,7 @@ class ImpresoraEtiquetaService extends BaseService "labels" => [ [ "cliente" => "Cliente Potencial", - "titulo" => "[1234] Ejemplo", + "titulo" => "[1234] TEST OLIVEROS", "cantidad" => 100, "tirada" => 50, "cajas" => 1, From 4872e498a56ef3d8bb758444f618f30f31f01919 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 2 Apr 2025 15:53:30 +0200 Subject: [PATCH 7/7] Modificado el controlador --- .../Logistica/Logisticacontroller.php | 71 +++++-------------- 1 file changed, 16 insertions(+), 55 deletions(-) diff --git a/ci4/app/Controllers/Logistica/Logisticacontroller.php b/ci4/app/Controllers/Logistica/Logisticacontroller.php index 9ada7ecf..25f5819d 100644 --- a/ci4/app/Controllers/Logistica/Logisticacontroller.php +++ b/ci4/app/Controllers/Logistica/Logisticacontroller.php @@ -2,69 +2,30 @@ namespace App\Controllers\Logistica; -use App\Models\Sistema\SettingsModel; -use CodeIgniter\Controller; +use App\Controllers\BaseController; +use App\Services\ImpresoraEtiquetaService; +use CodeIgniter\HTTP\RequestInterface; +use CodeIgniter\HTTP\ResponseInterface; +use Psr\Log\LoggerInterface; - -use App\Models\Collection; - -class Logisticacontroller extends \App\Controllers\BaseResourceController +class LogisticaController extends BaseController { - protected $modelName = TicketModel::class; - protected $format = 'json'; + protected ImpresoraEtiquetaService $impresoraEtiquetaService; + protected string $locale; + protected array $viewData; - protected static $singularObjectNameCc = 'logistica'; - protected static $singularObjectName = 'Logistica'; - protected static $pluralObjectName = 'Logistica'; - protected static $controllerSlug = 'logistica'; - - protected static $viewPath = 'themes/vuexy/form/logistica/'; - - protected $indexRoute = 'panel'; - - - public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) + public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { - $this->viewData['pageTitle'] = lang('Logistica.logistica'); - - // Breadcrumbs - $this->viewData['breadcrumb'] = [ - ['title' => lang("App.menu_logistica"), 'route' => "javascript:void(0);", 'active' => false], - ]; + $this->impresoraEtiquetaService = service('impresora_etiqueta'); + $this->locale = session()->get('lang'); parent::initController($request, $response, $logger); } - - public function index() + public function print_test_label() { - //checkPermission('tickets.menu'); - - /*$viewData = [ - 'currentModule' => static::$controllerSlug, - 'pageSubTitle' => lang('Basic.global.ManageAllRecords', [lang('Tickets.tickets')]), - 'usingServerSideDataTable' => true, - 'userType' => auth()->user()->can('tickets.edit') ? 1 : 0, - ]; - - $viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class - - return view(static::$viewPath . 'viewTicketList', $viewData);*/ - } - - public function panel() - { - //checkPermission('tickets.menu'); - - $viewData = [ - 'currentModule' => static::$controllerSlug, - 'boxTitle' => lang('Logistica.panel'), - 'pageSubTitle' => 'Panel', - 'usingServerSideDataTable' => true, - ]; - - $viewData = array_merge($this->viewData, $viewData); // merge any possible values from the parent controller class - - return view(static::$viewPath . 'viewPanelLogistica', $viewData); + $etiquetaData = $this->impresoraEtiquetaService->test(); + $responseMessage = $etiquetaData["status"] ? "OK" : "ERROR"; + return $this->response->setJSON(["message" => $responseMessage, "data" => $etiquetaData, "status" => $etiquetaData["status"]]); } }