etiqueta impresora

This commit is contained in:
amazuecos
2025-04-02 08:16:58 +02:00
parent bb78f9cbd8
commit d42f46d280
8 changed files with 343 additions and 1 deletions

View 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;
}
}