From d42f46d280f0367ba74c6d0f712012bc48fa3273 Mon Sep 17 00:00:00 2001 From: amazuecos Date: Wed, 2 Apr 2025 08:16:58 +0200 Subject: [PATCH] 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; + } +}