This commit is contained in:
Lizandro Guarnizo
2026-05-12 18:34:20 -05:00
parent ee31ac978f
commit 387485e65b
5 changed files with 212 additions and 76 deletions
+32 -3
View File
@@ -80,11 +80,13 @@ func EnviarCorreoPrueba(email string, p *models.PlantillaCorreo) error {
func EnviarCorreoManual(contrato *models.Contrato) error {
// Intentar obtener plantilla y pasarela desde la primera regla activa
var p *models.PlantillaCorreo
var reglaID uint
gateway := "bold"
reglas, err := models.GetReglasActivas()
if err == nil && len(reglas) > 0 {
regla := reglas[0]
reglaID = regla.ID
gateway = regla.PasarelaEnlace
p, err = models.GetPlantillaByID(regla.PlantillaID)
if err != nil {
@@ -107,7 +109,7 @@ func EnviarCorreoManual(contrato *models.Contrato) error {
// Generar o reutilizar el enlace de pago
enlacePago := ObtenerOCrearEnlacePago(contrato, gateway)
dias := int(contrato.FechaVencimiento.Sub(time.Now()).Hours() / 24)
dias := int(time.Until(contrato.FechaVencimiento).Hours() / 24)
var items []ItemServicio
for _, s := range contrato.Servicios {
items = append(items, ItemServicio{
@@ -131,7 +133,34 @@ func EnviarCorreoManual(contrato *models.Contrato) error {
if err != nil {
return err
}
return app.Http.Mail.Send(contrato.Cliente.Email, p.Asunto, html)
// Crear log previo (pendiente)
idsJSON, _ := json.Marshal([]uint{contrato.ID})
logEntry := models.NotificacionLog{
ClienteID: contrato.ClienteID,
ReglaID: reglaID,
ContratosIDs: string(idsJSON),
FechaEnvio: time.Now(),
Estado: "pendiente",
Asunto: p.Asunto,
PreviewHTML: html,
}
savedLog, _ := models.CreateNotificacionLog(logEntry)
sendErr := app.Http.Mail.Send(contrato.Cliente.Email, p.Asunto, html)
// Actualizar estado del log
if savedLog != nil {
if sendErr != nil {
savedLog.Estado = "fallido"
savedLog.ErrorMsg = sendErr.Error()
} else {
savedLog.Estado = "enviado"
}
models.UpdateNotificacionLog(*savedLog)
}
return sendErr
}
// EnviarNotificacionGrupo envía un correo agrupado para un cliente con múltiples contratos
@@ -160,7 +189,7 @@ func EnviarNotificacionGrupo(regla *models.NotificacionRegla, cliente *models.Cl
}
}
dias := int(fechaVenc.Sub(time.Now()).Hours() / 24)
dias := int(time.Until(fechaVenc).Hours() / 24)
// Generar o reutilizar el enlace de pago para el primer contrato del grupo
enlacePago := ""