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
+16
View File
@@ -88,6 +88,22 @@ func GetContratosProximosVencer(diasAntes int) ([]Contrato, error) {
return items, nil
}
// GetContratosYaVencidos retorna contratos cuya fecha de vencimiento es hoy o anterior
// y su estado sigue siendo 'activo' (aún no han sido marcados como vencidos)
func GetContratosYaVencidos() ([]Contrato, error) {
var items []Contrato
hoy := time.Now().UTC()
startOfDay := time.Date(hoy.Year(), hoy.Month(), hoy.Day(), 0, 0, 0, 0, time.UTC)
if err := app.Http.Database.DB.Preload("Cliente").Preload("Servicios").
Where("estado = 'activo' AND fecha_vencimiento < ?", startOfDay).
Find(&items).Error; err != nil {
log.Printf("Error getting contratos vencidos: %v", err)
return nil, err
}
return items, nil
}
// syncContratoServicios sincroniza la tabla join contrato_servicios usando SQL directo
// para evitar que GORM intente hacer upsert de los servicios existentes.
func syncContratoServicios(contratoID uint, servicioIDs []uint) error {
+12
View File
@@ -8,6 +8,7 @@ import (
type NotificacionRegla struct {
gorm.Model
Nombre string `json:"nombre" gorm:"column:nombre"`
TipoEvento string `json:"tipo_evento" gorm:"column:tipo_evento;default:'vencimiento_proximo'"` // vencimiento_proximo | ya_vencido | bienvenida | pago_recibido | manual
DiasAntes int `json:"dias_antes" gorm:"column:dias_antes"`
PlantillaID uint `json:"plantilla_id" gorm:"column:plantilla_id"`
Plantilla PlantillaCorreo `json:"plantilla" gorm:"foreignKey:PlantillaID"`
@@ -35,6 +36,16 @@ func GetReglasActivas() ([]NotificacionRegla, error) {
return items, nil
}
func GetReglasByTipoEvento(tipoEvento string) ([]NotificacionRegla, error) {
var items []NotificacionRegla
if err := app.Http.Database.DB.Preload("Plantilla").
Where("activo = ? AND tipo_evento = ?", true, tipoEvento).
Order("dias_antes DESC").Find(&items).Error; err != nil {
return nil, err
}
return items, nil
}
func GetReglaByID(id uint) (*NotificacionRegla, error) {
var item NotificacionRegla
if err := app.Http.Database.DB.Preload("Plantilla").First(&item, id).Error; err != nil {
@@ -50,6 +61,7 @@ func CreateRegla(r NotificacionRegla) error {
func UpdateRegla(r NotificacionRegla) error {
return app.Http.Database.DB.Model(&r).Updates(map[string]interface{}{
"nombre": r.Nombre,
"tipo_evento": r.TipoEvento,
"dias_antes": r.DiasAntes,
"plantilla_id": r.PlantillaID,
"activo": r.Activo,