95 lines
4.3 KiB
Python
95 lines
4.3 KiB
Python
from app import db
|
|
from datetime import datetime, timezone, date
|
|
from typing import Optional, Dict, Any
|
|
|
|
|
|
class InstitutionalBenefit(db.Model):
|
|
"""
|
|
Modelo de Beneficios y Convenios Comerciales Institucionales para UniCABA (SDD-012).
|
|
Permite a Bedelía y Administradores cargar promociones, descuentos y convenios
|
|
con comercios (cafeterías, librerías, tecnología, salud) para alumnos y docentes.
|
|
"""
|
|
__tablename__ = 'institutional_benefits'
|
|
__allow_unmapped__ = True
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
title = db.Column(db.String(150), nullable=False, index=True)
|
|
partner_name = db.Column(db.String(120), nullable=False, index=True)
|
|
category = db.Column(db.String(50), nullable=False, default='GENERAL', index=True)
|
|
discount_label = db.Column(db.String(80), nullable=False)
|
|
description = db.Column(db.Text, nullable=True)
|
|
terms_conditions = db.Column(db.Text, nullable=True)
|
|
target_roles = db.Column(db.String(100), nullable=False, default='ALL')
|
|
building_id = db.Column(db.Integer, db.ForeignKey('buildings.id', ondelete='SET NULL'), nullable=True, index=True)
|
|
coupon_code = db.Column(db.String(50), nullable=True)
|
|
valid_from = db.Column(db.Date, nullable=False, default=date.today)
|
|
valid_until = db.Column(db.Date, nullable=True)
|
|
is_active = db.Column(db.Boolean, default=True, nullable=False, index=True)
|
|
image_url = db.Column(db.String(255), nullable=True)
|
|
created_by_user_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='SET NULL'), nullable=True)
|
|
created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc).replace(tzinfo=None))
|
|
updated_at = db.Column(
|
|
db.DateTime,
|
|
default=lambda: datetime.now(timezone.utc).replace(tzinfo=None),
|
|
onupdate=lambda: datetime.now(timezone.utc).replace(tzinfo=None)
|
|
)
|
|
|
|
# Relaciones
|
|
building = db.relationship('Building', backref=db.backref('institutional_benefits', lazy='dynamic'))
|
|
creator = db.relationship('User', foreign_keys=[created_by_user_id])
|
|
|
|
def __init__(self, **kwargs):
|
|
super().__init__(**kwargs)
|
|
|
|
@property
|
|
def is_currently_valid(self) -> bool:
|
|
"""Determina si el convenio está activo y dentro de su rango de vigencia."""
|
|
if not self.is_active:
|
|
return False
|
|
today = date.today()
|
|
if self.valid_from and self.valid_from > today:
|
|
return False
|
|
if self.valid_until and self.valid_until < today:
|
|
return False
|
|
return True
|
|
|
|
def applies_to_role(self, role: str) -> bool:
|
|
"""Verifica si el beneficio aplica al rol indicado (alumno, docente, bedelia, admin)."""
|
|
if not role:
|
|
return False
|
|
target = (self.target_roles or 'ALL').upper()
|
|
if 'ALL' in target or 'TODOS' in target:
|
|
return True
|
|
r = role.strip().upper()
|
|
if r in target:
|
|
return True
|
|
if r in ['ESTUDIANTE', 'ALUMNO'] and ('ALUMNO' in target or 'ESTUDIANTE' in target):
|
|
return True
|
|
if r in ['DOCENTE', 'PROFESOR', 'TEACHER'] and ('DOCENTE' in target or 'PROFESOR' in target):
|
|
return True
|
|
return False
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
"""Serializa la entidad a diccionario compatible con JSON."""
|
|
return {
|
|
'id': self.id,
|
|
'title': self.title,
|
|
'partner_name': self.partner_name,
|
|
'category': self.category,
|
|
'discount_label': self.discount_label,
|
|
'description': self.description,
|
|
'terms_conditions': self.terms_conditions,
|
|
'target_roles': self.target_roles,
|
|
'building_id': self.building_id,
|
|
'building_name': self.building.name if self.building else 'Todas las Sedes',
|
|
'coupon_code': self.coupon_code,
|
|
'valid_from': self.valid_from.isoformat() if self.valid_from else None,
|
|
'valid_until': self.valid_until.isoformat() if self.valid_until else None,
|
|
'is_active': self.is_active,
|
|
'is_currently_valid': self.is_currently_valid,
|
|
'image_url': self.image_url,
|
|
'created_by_user_id': self.created_by_user_id,
|
|
'created_at': self.created_at.isoformat() if self.created_at else None,
|
|
'updated_at': self.updated_at.isoformat() if self.updated_at else None,
|
|
}
|