Merge branch 'main' into 'mod/envio_resumen_admin'

Main

See merge request jjimenez/safekat!639
This commit is contained in:
2025-04-02 16:39:27 +00:00
10 changed files with 379 additions and 56 deletions

5
.vscode/sftp.json vendored
View File

@ -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"
}
}

View File

@ -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

View File

@ -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) {

View File

@ -0,0 +1,31 @@
<?php
namespace App\Controllers\Logistica;
use App\Controllers\BaseController;
use App\Services\ImpresoraEtiquetaService;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
class LogisticaController extends BaseController
{
protected ImpresoraEtiquetaService $impresoraEtiquetaService;
protected string $locale;
protected array $viewData;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
$this->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"]]);
}
}

View File

@ -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"]]);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
use CodeIgniter\Database\RawSql;
class ImpresoraEtiquetasTable extends Migration
{
protected array $COLUMNS = [
'id' => [
'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");
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Database\Seeds;
use App\Models\Configuracion\ImpresoraEtiquetaModel;
use CodeIgniter\Database\Seeder;
class EtiquetaImpresoraSeeder extends Seeder
{
public function run()
{
$data = [
[
"name" => "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);
}
}
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Entities\Configuracion;
class ImpresoraEtiquetaEntity extends \CodeIgniter\Entity\Entity
{
protected $attributes = [
"id" => 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"
];
}

View File

@ -0,0 +1,57 @@
<?php
namespace App\Models\Configuracion;
use App\Entities\Configuracion\ImpresoraEtiquetaEntity;
use App\Models\BaseModel;
class ImpresoraEtiquetaModel extends BaseModel
{
protected $table = "etiqueta_impresoras";
/**
* Whether primary key uses auto increment.
*
* @var bool
*/
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = ImpresoraEtiquetaEntity::class;
protected $useSoftDeletes = true;
protected $protectFields = true;
protected $allowedFields = [
"name",
"tipo",
"ip",
"port",
"user",
"pass",
"description"
];
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = "created_at";
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = false;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}

View File

@ -0,0 +1,108 @@
<?php
namespace App\Services;
use App\Entities\Configuracion\ImpresoraEtiquetaEntity;
use App\Models\Configuracion\ImpresoraEtiquetaModel;
use CodeIgniter\Config\BaseService;
use DOMDocument;
use Throwable;
use Exception;
class ImpresoraEtiquetaService extends BaseService
{
protected ImpresoraEtiquetaModel $impresoraEtiquetaModel;
protected array $TEST = [
"printer" => "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;
}
}