From 151e4a2d7a3bd4c514eb52de4d04ea4991fe1558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Fri, 25 Apr 2025 17:50:30 +0200 Subject: [PATCH 1/2] cambios --- ci4/app/Config/Routes.php | 1 + .../Logistica/LogisticaController.php | 15 +++ ci4/app/Services/LogisticaService.php | 56 +++++++++ .../js/safekat/pages/logistica/envioEdit.js | 109 ++++++++++++++++++ 4 files changed, 181 insertions(+) diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index d94ce4c8..0fd28026 100755 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -809,6 +809,7 @@ $routes->group('logistica', ['namespace' => 'App\Controllers\Logistica'], functi $routes->post('generateEnvio', 'LogisticaController::generarEnvio'); $routes->get('selectPedidosForEnvio', 'LogisticaController::findPedidosNewEnvio'); $routes->get('selectDireccionForEnvio', 'LogisticaController::selectDireccionForEnvio'); + $routes->post('imprimirEtiquetas', 'LogisticaController::imprimirEtiquetas'); }); /* diff --git a/ci4/app/Controllers/Logistica/LogisticaController.php b/ci4/app/Controllers/Logistica/LogisticaController.php index 8dbd4630..5b542e62 100755 --- a/ci4/app/Controllers/Logistica/LogisticaController.php +++ b/ci4/app/Controllers/Logistica/LogisticaController.php @@ -137,6 +137,21 @@ class LogisticaController extends BaseController } } + public function imprimirEtiquetas() + { + if ($this->request->isAJAX()) { + $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); + + } else { + return $this->failUnauthorized('Invalid request', 403); + } + } + public function selectAddEnvioLinea() { diff --git a/ci4/app/Services/LogisticaService.php b/ci4/app/Services/LogisticaService.php index 9124a3d7..cb7692b2 100644 --- a/ci4/app/Services/LogisticaService.php +++ b/ci4/app/Services/LogisticaService.php @@ -436,6 +436,62 @@ 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) diff --git a/httpdocs/assets/js/safekat/pages/logistica/envioEdit.js b/httpdocs/assets/js/safekat/pages/logistica/envioEdit.js index 5e85f9bd..62d766f5 100644 --- a/httpdocs/assets/js/safekat/pages/logistica/envioEdit.js +++ b/httpdocs/assets/js/safekat/pages/logistica/envioEdit.js @@ -92,6 +92,63 @@ class EnvioEdit { ] }); + $('#btnImprimirEtiquetas').on('click', () => { + const table = this.table; + const selectedRows = table.rows({ page: 'current' }).nodes().filter((node) => { + const checkbox = $(node).find('.checkbox-linea-envio'); + return checkbox.is(':checked'); + } + ); + const ids = selectedRows.map((node) => { + const rowData = table.row(node).data(); + return rowData.id; + }).toArray(); + if (ids.length <= 0) { + Swal.fire({ + title: 'Atención!', + text: 'Debe seleccionar al menos una línea de envío para imprimir etiquetas.', + icon: 'info', + confirmButtonColor: '#3085d6', + confirmButtonText: 'Ok', + customClass: { + confirmButton: 'btn btn-primary me-1', + }, + buttonsStyling: false + }); + return; + } + const idEnvio = $('#id').val(); + let num_cajas = this.cajas.val(); + if(ids.length != table.rows().count()){ + // se preguntará el numero de cajas en un swal con un input para obtener el valor + Swal.fire({ + title: 'Atención!', + text: 'No se ha seleccionado todas las líneas de envío. Ingrese el número de cajas a imprimir.', + icon: 'info', + input: 'text', + inputLabel: 'Número de cajas', + inputValue: num_cajas, + showCancelButton: true, + confirmButtonColor: '#3085d6', + confirmButtonText: 'Imprimir etiquetas', + cancelButtonText: 'Cancelar', + customClass: { + confirmButton: 'btn btn-primary me-1', + cancelButton: 'btn btn-secondary' + }, + buttonsStyling: false + }).then((result) => { + if (result.isConfirmed) { + num_cajas = result.value; + this._imprimirEtiquetas(idEnvio, ids, num_cajas); + } + }); + } + else{ + this._imprimirEtiquetas(idEnvio, ids, num_cajas); + } + }); + this.cajas.on('change', (e) => { const value = $(e.currentTarget).val(); if (value < 0) { @@ -429,6 +486,58 @@ class EnvioEdit { this._getAlbaranes(); } + _imprimirEtiquetas(envio_id, ids, num_cajas) { + + $.post('/logistica/imprimirEtiquetas', { + + envio_id: envio_id, + envio_lineas: ids, + cajas: num_cajas + }, function (response) { + if (response.status) { + Swal.fire({ + title: 'Etiquetas generadas', + text: 'Las etiquetas se han generado correctamente.', + icon: 'success', + confirmButtonColor: '#3085d6', + confirmButtonText: 'Ok', + customClass: { + confirmButton: 'btn btn-primary me-1', + }, + buttonsStyling: false + }).then(() => { + + }); + } + else { + Swal.fire({ + title: 'Error', + text: response.message, + icon: 'error', + confirmButtonColor: '#3085d6', + confirmButtonText: 'Ok', + customClass: { + confirmButton: 'btn btn-primary me-1', + }, + buttonsStyling: false + }); + } + } + ).fail(() => { + Swal.fire({ + title: 'Error', + text: 'No se pudo generar las etiquetas.', + icon: 'error', + confirmButtonColor: '#3085d6', + confirmButtonText: 'Ok', + customClass: { + confirmButton: 'btn btn-primary me-1', + }, + buttonsStyling: false + }); + }); + } + _checkDatosFinalizar() { if (this.codigoSeguimiento.val().length <= 0 || this.proveedor.getVal() <= 0) { From b5b86b41647dc53d5b3c5c65655fe015bddbfcb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Sat, 26 Apr 2025 10:50:59 +0200 Subject: [PATCH 2/2] terminado a falta de pruebas --- .../Logistica/LogisticaController.php | 58 ++++++++- ci4/app/Language/es/Logistica.php | 1 + ci4/app/Services/ImpresoraEtiquetaService.php | 4 +- ci4/app/Services/LogisticaService.php | 119 +++++++++--------- .../form/logistica/viewEnvioEditForm.php | 27 +++- .../js/safekat/pages/logistica/envioEdit.js | 19 ++- 6 files changed, 159 insertions(+), 69 deletions(-) diff --git a/ci4/app/Controllers/Logistica/LogisticaController.php b/ci4/app/Controllers/Logistica/LogisticaController.php index d48d84a3..ecc38ba2 100755 --- a/ci4/app/Controllers/Logistica/LogisticaController.php +++ b/ci4/app/Controllers/Logistica/LogisticaController.php @@ -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' => '' . ' ' . lang('Logistica.envio') . ' [' . $envioEntity->id . ']: ' . $envioEntity->direccion, diff --git a/ci4/app/Language/es/Logistica.php b/ci4/app/Language/es/Logistica.php index f0e2d6bc..5b6331a4 100755 --- a/ci4/app/Language/es/Logistica.php +++ b/ci4/app/Language/es/Logistica.php @@ -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', diff --git a/ci4/app/Services/ImpresoraEtiquetaService.php b/ci4/app/Services/ImpresoraEtiquetaService.php index 2af5a75a..41be8605 100755 --- a/ci4/app/Services/ImpresoraEtiquetaService.php +++ b/ci4/app/Services/ImpresoraEtiquetaService.php @@ -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(); diff --git a/ci4/app/Services/LogisticaService.php b/ci4/app/Services/LogisticaService.php index 3d572353..fe8929d2 100644 --- a/ci4/app/Services/LogisticaService.php +++ b/ci4/app/Services/LogisticaService.php @@ -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 + ]; + } + } } diff --git a/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php b/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php index e012b79d..23229291 100644 --- a/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php +++ b/ci4/app/Views/themes/vuexy/form/logistica/viewEnvioEditForm.php @@ -9,7 +9,9 @@
-

finalizado == 0)?'':'FINALIZADO' ?>

+

+ finalizado == 0) ? '' : 'FINALIZADO' ?> +

@@ -162,7 +164,7 @@

-
+
+
+
+ + +
+
+
@@ -288,9 +306,8 @@ finalizado == 0) ? "" : "readonly" ?> - value="codigo_seguimiento) ?>"> + maxlength="100" class="form-control" finalizado == 0) ? '' : 'readonly' ?> + value="codigo_seguimiento)) ?>">