Files
ByteCenter/app/helpers/email_finalizado.php
T
2026-08-04 19:21:38 -03:00

156 lines
5.5 KiB
PHP

<?php
require_once __DIR__ . '/../helpers/crypto.php';
require_once __DIR__ . '/../libs/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/../libs/phpmailer/src/SMTP.php';
require_once __DIR__ . '/../libs/phpmailer/src/Exception.php';
require_once __DIR__ . '/../controllers/configuracionController.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function enviarMailServicioFinalizado($servicio, $insumos, $cliente) {
// VERIFICAMOS SI LAS NOTIFICACIONES ESTAN ACTIVADAS
// OBTENEMOS MAIL Y CONTRASEÑA
$configuracionmodel = new configuracionModel();
$datosConfig = $configuracionmodel->obtenerDatosEnvioMail();
if (isset($datosConfig['contrasenia_emisor'])) {
$datosConfig['contrasenia_emisor'] = decryptData($datosConfig['contrasenia_emisor']);
}
if ((int) $datosConfig['mail_automatico'] === 1) {
$mail = new PHPMailer(true);
try {
// CONFIG SMTP
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = $datosConfig['email_emisor'];
$mail->Password = $datosConfig['contrasenia_emisor'];
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
// CODIFICACIÓN (evita problemas con tildes)
$mail->CharSet = 'UTF-8';
// REMITENTE
$mail->setFrom($datosConfig['email_emisor'], 'ByteCenter');
// DESTINATARIO
if (empty($cliente['email'])) {
return false;
}
$mail->addAddress($cliente['email'], $cliente['nombre'] . ' ' . $cliente['apellido']);
// DEFINIMOS ARRAY HTML DE INSUMOS UTILIZADOS
$productos = "";
if (!empty($insumos)) {
foreach ($insumos as $i) {
$productos .= "
<tr>
<td style='text-align: center;'>{$i['marca']} {$i['modelo']}</td>
<td style='text-align: center;'>$ {$i['precio_unitario']}</td>
<td style='text-align: center;'>{$i['cantidad']}</td>
<td style='text-align: center;'>$ {$i['subtotal']}</td>
</tr>
";
}
}
// POR SI LA LISTA VIENE VACIA
if (empty($insumos)) {
$productos = "
<tr>
<td colspan='4' align='center' style='text-align: center; padding: 10px;'>
-
</td>
</tr>
";
}
// SANEAMIENTO DE HORA (substraer segundos)
$datosConfig['hora_apertura_maniana'] = substr($datosConfig['hora_apertura_maniana'], 0, 5);
$datosConfig['hora_cierre_maniana'] = substr($datosConfig['hora_cierre_maniana'], 0, 5);
$datosConfig['hora_apertura_tarde'] = substr($datosConfig['hora_apertura_tarde'], 0, 5);
$datosConfig['hora_cierre_tarde'] = substr($datosConfig['hora_cierre_tarde'], 0, 5);
// CONTENIDO
$numeroServicio = '0001-' . str_pad($servicio['id'], 8, '0', STR_PAD_LEFT);
$mail->isHTML(true);
$mail->Subject = 'ByteCenter - Servicio Finalizado - N°' . $numeroServicio;
$mail->Body = "
<h2>Servicio Finalizado</h2>
<p><strong>Cliente: </strong> ". $cliente['nombre'] . ' ' . $cliente['apellido'] ."</p>
<p><strong>Equipo: </strong> ". $servicio['equipo'] ."</p>
<p><strong>Problema: </strong> ". $servicio['problema']. "</p>
<br>
<h2>Detalles de Servicio</h2>
<h3>Trabajo Realizado</h3>
<p>". $servicio['detalle_realizado']. "</p>
<h3>Insumos Utilizados</h3>
<table border='1' cellpadding='6' cellspacing='0' style='border-collapse: collapse; text-align: center;'>
<tr>
<th>Modelo</th>
<th>Precio Unitario</th>
<th>Cantidad</th>
<th>Subtotal</th>
</tr>
$productos
</table>
<h3>Totales</h3>
<p><strong>Mano de obra: </strong> ". "$" . number_format($servicio['mano_obra'], 2, ',', '.'). "</p>
<p><strong>Total: </strong> ". "$" . number_format($servicio['total'], 2, ',', '.'). "</p>
<br>
<p>Cuando desee puede pasar por nuestro local para retirar su equipo, nos encontramos en {$datosConfig['direccion']}, {$datosConfig['ciudad']}.</p>
<p>Nuestros horarios de atención son: {$datosConfig['hora_apertura_maniana']} - {$datosConfig['hora_cierre_maniana']} y {$datosConfig['hora_apertura_tarde']} - {$datosConfig['hora_cierre_tarde']}.</p>
<p>Por cualquier duda o consulta: {$datosConfig['telefono']} - {$datosConfig['email']}.</p>
<p>Gracias por confiar en nuestro servicio técnico.</p>
<p>ByteCenter</p>
";
// Versión texto plano (por compatibilidad)
$mail->AltBody = "Servicio finalizado - Total: $ {$servicio['total']}";
$mail->send();
return true;
} catch (Exception $e) {
// Log interno
error_log($e->getMessage());
return false;
}
} else {
return true;
}
}