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/Config/Routes.php b/ci4/app/Config/Routes.php index ebd240e3..6e6aba9b 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -1024,6 +1024,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/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"]]); } } 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] TEST OLIVEROS", + "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 + { + $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'); + } + $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, $tmpMetaData['uri'], FTP_ASCII) === FALSE) { + $status = false; + ftp_close($conn); + }else{ + $status = true; + } + + return $status; + } +}