terminado a falta de pruebas

This commit is contained in:
2025-04-26 10:50:59 +02:00
parent ef173ab0fc
commit b5b86b4164
6 changed files with 159 additions and 69 deletions

View File

@ -128,9 +128,53 @@ class LogisticaController extends BaseController
$envio_id = $this->request->getPost('envio_id');
$ids = $this->request->getPost('envio_lineas');
$cajas = $this->request->getPost('cajas');
$result = ['status' => true];//LogisticaService::imprimirEtiquetas($ids);
return $this->response->setJSON($result);
$printer_id = $this->request->getPost('printer_id');
if($cajas == null || $cajas == 0){
return $this->response->setJSON([
'status' => false,
'message' => 'Cajas no válidas'
]);
}
$model = model('App\Models\Logistica\EnvioModel');
$envio = $model->select('envios.*, clientes.nombre as cliente')
->join('clientes', 'clientes.id = envios.cliente_id', 'left')
->where('envios.id', $envio_id)
->first();
if($envio == null){
return $this->response->setJSON([
'status' => false,
'message' => 'Envio no válido'
]);
}
$model = model('App\Models\Logistica\EnvioLineaModel');
$lineas = $model->select('envios_lineas.*, presupuestos.titulo as titulo, presupuestos.referencia_cliente as referencia_cliente')
->join('presupuestos', 'presupuestos.id = envios_lineas.presupuesto_id', 'left')
->whereIn('envios_lineas.id', $ids)->findAll();
if($lineas == null){
return $this->response->setJSON([
'status' => false,
'message' => 'Lineas no válidas'
]);
}
$modelImpresora = model('App\Models\Configuracion\ImpresoraEtiquetaModel');
$impresora = $modelImpresora->select('id, name')
->where('deleted_at', null)
->where('id', $printer_id)
->orderBy('name', 'asc')
->first();
if($impresora == null){
return $this->response->setJSON([
'status' => false,
'message' => 'Impresora no válida'
]);
}
$response = LogisticaService::generateEtiquetasTitulos($envio, $lineas, $impresora, $cajas);
return $this->response->setJSON($response);
} else {
return $this->failUnauthorized('Invalid request', 403);
@ -236,6 +280,14 @@ class LogisticaController extends BaseController
$envioEntity->proveedor_nombre = $proveedor->nombre;
}
$modelImpresora = model('App\Models\Configuracion\ImpresoraEtiquetaModel');
$impresoras = $modelImpresora->select('id, name')
->where('deleted_at', null)
->where('tipo', 1)
->orderBy('name', 'asc')
->findAll();
$envioEntity->impresoras = $impresoras;
$viewData = [
'currentModule' => static::$controllerSlug,
'boxTitle' => '<i class="ti ti-truck ti-xl"></i>' . ' ' . lang('Logistica.envio') . ' [' . $envioEntity->id . ']: ' . $envioEntity->direccion,

View File

@ -30,6 +30,7 @@ return [
'no' => 'No',
'si' => 'Sí',
'todos' => 'Todos',
'impresoraEtiquetas' => 'Impresora de etiquetas',
'envio' => 'Envío',
'addLineasEnvio' => 'Añadir líneas al envío',

View File

@ -57,7 +57,7 @@ class ImpresoraEtiquetaService extends BaseService
return ["impresora" => $impresora, "content" => $th->getMessage(), "status" => $status];
}
}
protected function createEtiqueta(array $data_label = []): ?string
public function createEtiqueta(array $data_label = []): ?string
{
$xml = new DOMDocument('1.0', 'utf-8');
$labels = $xml->createElement("labels");
@ -76,7 +76,7 @@ class ImpresoraEtiquetaService extends BaseService
$xml->appendChild($labels);
return $xml->saveXML();
}
protected function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool
public function sendToImpresoraEtiqueta(string $name, string $content, ImpresoraEtiquetaEntity $impresoraEtiqueta): bool
{
$tmpFile = tmpfile();

View File

@ -331,63 +331,6 @@ class LogisticaService
];
}
public static function imprimirEtiquetas($envio_id, $ids)
{
$array = [
"printer" => "LABPRINT-1",
"header" => [
"_FORMAT" => "E:PEDIDO.ZPL",
"_QUANTITY" => 1,
"_PRINBTERNAME" => "Printer 1",
"_JOBNAME" => "LBL101"
],
"labels" => [
/* Ejemplo etiqueta
[
"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"
]
*/
]
];
$EnvioModel = model('App\Models\Logistica\EnvioModel');
$EnvioLineasModel = model('App\Models\Logistica\EnvioLineaModel');
$envios = $EnvioModel->whereIn('id', $ids)->findAll();
if (empty($envios)) {
return [
'status' => false,
'message' => lang('Logistica.errors.noEnvio'),
];
}
foreach ($envios as $envio) {
$lineasEnvio = $EnvioLineasModel->where('envio_id', $envio->id)->findAll();
if (empty($lineasEnvio)) {
return [
'status' => false,
'message' => lang('Logistica.errors.noEnvioLineas'),
];
}
}
return [
'status' => true,
'data' => $envios,
];
}
public static function finalizarEnvio($envio_id, $finalizar_ot = false)
{
@ -475,4 +418,66 @@ class LogisticaService
}
return $data_return;
}
public static function generateEtiquetasTitulos($envio, $lineas, $printer, $cajas)
{
$data = [
"printer" => $printer->name,
"header" => [
"_FORMAT" => "E:PEDIDO.ZPL",
"_QUANTITY" => 1,
"_PRINBTERNAME" => $printer->name,
"_JOBNAME" => "LBL101"
],
];
foreach ($lineas as $linea) {
$data["labels"][] = [
"cliente" => $envio->cliente,
"titulo" => "[" . $linea->pedido_id . "] - " . $linea->titulo,
"cantidad" => $linea->unidades_envio,
"tirada" => $linea->unidades_total,
"cajas" => $cajas,
"ean" => null,
"nombre" => $envio->att,
"direccion" => $envio->direccion,
"notas" => "",
"refcliente" => $linea->refcliente,
"npedido" => $linea->pedido_id
];
}
$servicioImpresora = new ImpresoraEtiquetaService();
$xml = $servicioImpresora->createEtiqueta($data);
if($xml == null){
return [
'status' => false,
'message' => lang('Logistica.errors.noEtiquetas'),
];
}
$sk_environment = getenv('SK_ENVIRONMENT');
if($sk_environment == 'production'){
$status = $servicioImpresora->sendToImpresoraEtiqueta("ETIQUETA", $xml, $printer);
if ($status) {
return [
'status' => true,
'message' => lang('Logistica.success.imprimirEtiquetas'),
'data' => $xml
];
} else {
return [
'status' => false,
'message' => lang('Logistica.errors.noEtiquetas'),
];
}
}else{
return [
'status' => true,
'message' => lang('Logistica.success.imprimirEtiquetas'),
'data' => $xml
];
}
}
}

View File

@ -9,7 +9,9 @@
<div class="col-12">
<div class="card">
<div class="card-header">
<h4><?= $boxTitle ?> <?= ($envioEntity->finalizado == 0)?'':'<span class="badge text-bg-success fw-lg">FINALIZADO</span>' ?></h4>
<h4><?= $boxTitle ?>
<?= ($envioEntity->finalizado == 0) ? '' : '<span class="badge text-bg-success fw-lg">FINALIZADO</span>' ?>
</h4>
</div>
<div class="card-body">
@ -162,7 +164,7 @@
<div class="d-flex flex-row">
<p><?= lang('Logistica.buttonsActions') ?></p>
</div>
<div class="d-flex flex-row mb-3">
<div class="d-flex flex-row mb-3 align-items-end">
<div class="col-sm-2 px-3">
<button id="btnSelectAll" name="btnSelectAll" tabindex="1"
class="btn btn-primary w-100">
@ -194,6 +196,22 @@
</button>
</div>
<div class="col-sm-2 px-3 d-flex flex-column justify-content-end">
<div class="d-flex flex-column justify-content-end h-100">
<label for="impresoraEtiquetas" class="form-label">
<?= lang("Logistica.impresoraEtiquetas") ?>
</label>
<select id="impresoraEtiquetas" name="impresora_etiquetas" tabindex="1"
maxlength="50" class="form-control select2bs2" style="width: 100%;">
<?php foreach ($envioEntity->impresoras as $impresora): ?>
<option value="<?= $impresora->id ?>">
<?= $impresora->name ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<div class="row mb-3">
@ -288,9 +306,8 @@
<?= lang("Logistica.codigoSeguimiento") ?>
</label>
<input type="text" id="codigoSeguimiento" name="codigo_seguimiento" tabindex="1"
maxlength="100" class="form-control"
<?= ($envioEntity->finalizado == 0) ? "" : "readonly" ?>
value="<?= old('codigo_seguimiento', $envioEntity->codigo_seguimiento) ?>">
maxlength="100" class="form-control" <?= ($envioEntity->finalizado == 0) ? '' : 'readonly' ?>
value="<?= esc(old('codigo_seguimiento', $envioEntity->codigo_seguimiento)) ?>">
</div>
<div class="col-sm-3 px-3">
<label for="empresaMensajeria" class="form-label">

View File

@ -32,6 +32,8 @@ class EnvioEdit {
if (!$("#empresaMensajeriaInput").length) {
this.proveedor = new ClassSelect($("#empresaMensajeria"), '/compras/proveedores/getProveedores', "", true, { 'tipo_id': 2 });
}
this.impresoraEtiquetas = $("#impresoraEtiquetas");
}
init() {
@ -492,7 +494,9 @@ class EnvioEdit {
envio_id: envio_id,
envio_lineas: ids,
cajas: num_cajas
cajas: num_cajas,
printer_id: this.impresoraEtiquetas.val(),
}, function (response) {
if (response.status) {
Swal.fire({
@ -506,7 +510,18 @@ class EnvioEdit {
},
buttonsStyling: false
}).then(() => {
if(response.data){
// show xml in a new tab
const blob = new Blob([response.data], { type: 'application/xml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'etiquetas.xml';
a.click();
URL.revokeObjectURL(url);
a.remove();
}
});
}
else {