mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
etiqueta impresora
This commit is contained in:
@ -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
|
||||
|
||||
@ -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) {
|
||||
|
||||
31
ci4/app/Controllers/Logistica/LogisticaController.php
Normal file
31
ci4/app/Controllers/Logistica/LogisticaController.php
Normal 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"]]);
|
||||
}
|
||||
}
|
||||
@ -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");
|
||||
}
|
||||
}
|
||||
45
ci4/app/Database/Seeds/EtiquetaImpresoraSeeder.php
Normal file
45
ci4/app/Database/Seeds/EtiquetaImpresoraSeeder.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
ci4/app/Entities/Configuracion/ImpresoraEtiquetaEntity.php
Normal file
27
ci4/app/Entities/Configuracion/ImpresoraEtiquetaEntity.php
Normal 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"
|
||||
];
|
||||
|
||||
}
|
||||
57
ci4/app/Models/Configuracion/ImpresoraEtiquetaModel.php
Normal file
57
ci4/app/Models/Configuracion/ImpresoraEtiquetaModel.php
Normal 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 = [];
|
||||
|
||||
|
||||
}
|
||||
93
ci4/app/Services/ImpresoraEtiquetaService.php
Normal file
93
ci4/app/Services/ImpresoraEtiquetaService.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
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;
|
||||
|
||||
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] 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user