diff --git a/ci4/app/Services/EmailService.php b/ci4/app/Services/EmailService.php index 5b6f7f99..be7dc57a 100755 --- a/ci4/app/Services/EmailService.php +++ b/ci4/app/Services/EmailService.php @@ -13,7 +13,7 @@ class EmailService // Si no estamos en producción, forzar el destinatario a uno fijo if ($skEnv !== 'production') { - $recipient = env('MAIL_DEV_RECIPIENT', 'imnavajas@coit.es'); // fallback opcional + $recipient = env('MAIL_DEV_RECIPIENT', 'imnavajas@coit.es,info@jjimenez.eu'); // fallback opcional } $settings_model = model('App\Models\Configuracion\ConfigVariableModel'); @@ -43,7 +43,13 @@ class EmailService $email->setSubject($subject); $email->setMessage($body); - return $email->send(); + if (!$email->send()) { + log_message('error', 'Error enviando email: ' . $email->printDebugger(['headers', 'subject', 'body'])); + return false; + } + + + return true; } catch (\Throwable $e) { log_message('error', 'EmailService failed: ' . $e->getMessage()); return false; diff --git a/ci4/app/Services/LogisticaService.php b/ci4/app/Services/LogisticaService.php index 5e2fe8e1..b5f28237 100644 --- a/ci4/app/Services/LogisticaService.php +++ b/ci4/app/Services/LogisticaService.php @@ -148,7 +148,7 @@ class LogisticaService $builder = $db->table("({$subBuilder->getCompiledSelect(false)}) AS sub"); $builder->select('ot, fechaEncuadernado'); $builder->where('cantidad > unidades_enviadas'); - + return $builder; } @@ -362,6 +362,81 @@ class LogisticaService } + public static function sendConfirmacionEnvio($envio, $lineaEnvio, $isFerro = false) + { + + $view = \Config\Services::renderer(); + + if ($isFerro) + $subject = '[Safekat]' . " El envio del ferro de su pedido se ha realizado"; + else + $subject = '[Safekat]' . " El envio de su pedido se ha realizado"; + + $presupuestoModel = model('App\Models\Presupuestos\PresupuestoModel'); + $presupuesto = $presupuestoModel->find($lineaEnvio->presupuesto_id); + $proveedorModel = model('App\Models\Compras\ProveedorModel'); + $proveedor = $proveedorModel->find($envio->proveedor_id); + $userModel = model('App\Models\Usuarios\UserModel'); + $datos_correo = $userModel->select("CONCAT(users.first_name, ' ', users.last_name) as comercial_nombre, auth_identities.secret as comercial_correo, clientes.email as cliente_email") + ->join('auth_identities', 'auth_identities.user_id = users.id') + ->join('clientes', 'clientes.comercial_id = users.id') + ->where('clientes.id', $presupuesto->cliente_id) + ->first(); + + + + $pedido = (object) [ + 'pedido_id' => $lineaEnvio->pedido_id, + 'titulo' => $presupuesto->titulo, + 'cp' => $envio->cp, + 'proveedor_nombre' => $proveedor->nombre, + 'codigo_seguimiento' => $envio->codigo_seguimiento, + 'comercial_nombre' => $datos_correo->comercial_nombre, + 'comercial_correo' => $datos_correo->comercial_correo, + ]; + + if ($proveedor->nombre == "GLS") { + $pedido->url = 'https://m.asmred.com/e/' . $envio->codigo_seguimiento . '/' . $envio->cp; + } + + $content = $view->setVar('datos_pedido', $pedido) + ->render('themes/vuexy/mail/envio_pedido'); + // Renderiza la plantilla completa + if ($isFerro) + $finalBody = $view->setVar('emailTitle2', "El ferro de su pedido " . $lineaEnvio->pedido_id . " ha sido enviado el " . date('d/m/Y')) + ->setVar('content', $content) + ->render('themes/vuexy/mail/mail_layout_2'); + else + $finalBody = $view->setVar('emailTitle2', "Su pedido " . $lineaEnvio->pedido_id . " ha sido enviado el " . date('d/m/Y')) + ->setVar('content', $content) + ->render('themes/vuexy/mail/mail_layout_2'); + + + $email = service('emailService'); + $result = $email->send($subject, $finalBody, $datos_correo->cliente_email); + + $chat = Service('chat'); + $data = [ + 'chat_department_id' => 5, + 'client' => $presupuesto->cliente_id, + 'message' => "El pedido " . $lineaEnvio->pedido_id . " ha sido enviado el " . date('d/m/Y') . ".

" . + "CP:" . $envio->cp . ".
" . + "Proveedor envío: " . $proveedor->nombre . ".
" . + "Código de seguimiento: " . $envio->codigo_seguimiento . ".
" + ]; + if ($proveedor->nombre == "GLS") { + $data['message'] = $data['message'] . 'URL segumiento: ' . + 'https://m.asmred.com/e/' . $envio->codigo_seguimiento . '/' . $envio->cp . ''; + } + $chat->storeChatMessage(5, "pedido", $lineaEnvio->pedido_id, $data); + + return [ + 'status' => $result, + 'message' => $result ? lang('Logistica.success.emailSent') : lang('Logistica.errors.emailNotSent'), + ]; + } + + public static function generateEnvio($ot_id, $direccion = null) { @@ -606,8 +681,11 @@ class LogisticaService "name" => "ferro_en_cliente_at", "ferro_en_cliente_at" => date('Y-m-d H:i:s') ]); + + LogisticaService::sendConfirmacionEnvio($envio, $linea, true); + } else { - if ($cantidad_enviada + $linea->unidades_envio == $pedido->total_tirada) { + if ($cantidad_enviada + $linea->unidades_envio >= $pedido->total_tirada) { $otModel = model('App\Models\OrdenTrabajo\OrdenTrabajoModel'); $ot = $otModel->where('pedido_id', $linea->pedido_id) ->first(); @@ -623,6 +701,9 @@ class LogisticaService "name" => "envio_at", "envio_at" => date('Y-m-d H:i:s') ]); + + LogisticaService::sendConfirmacionEnvio($envio, $linea); + if ($finalizar_ot) { $ps->updateOrdenTrabajo( [ diff --git a/ci4/app/Views/themes/vuexy/mail/envio_pedido.php b/ci4/app/Views/themes/vuexy/mail/envio_pedido.php new file mode 100644 index 00000000..7ab3d799 --- /dev/null +++ b/ci4/app/Views/themes/vuexy/mail/envio_pedido.php @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + url)): ?> + + + + + + + +
+ Número pedido: pedido_id) ?>
+ Título: titulo) ?>
+ CP: cp) ?>
+ Proveedor envío: proveedor_nombre) ?>
+ Código seguimiento: codigo_seguimiento) ?>
+ URL seguimiento: url) ?> +
+

Consulte con su comercial comercial_nombre) ?> en el correo + comercial_correo) ?>

+
\ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/mail/mail_layout_2.php b/ci4/app/Views/themes/vuexy/mail/mail_layout_2.php new file mode 100644 index 00000000..32570469 --- /dev/null +++ b/ci4/app/Views/themes/vuexy/mail/mail_layout_2.php @@ -0,0 +1,156 @@ + + + + + + <?= esc($emailTitle ?? 'Correo de Safekat') ?> + + + + + + + + + + + + + + + + + + +
+ + + Safekat + +
+

+
+ +
+ + + \ No newline at end of file