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