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] 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)) ?>">