private void establecerEdad(ConsolidacionBoleta entidad)
 {
     if (entidad.FechaDeNacimiento.HasValue)
     {
         entidad.Edad = DateTime.Now.Subtract(entidad.FechaDeNacimiento.Value).Days / 365;
     }
 }
        private void validarEntidad(System.Data.Objects.ObjectStateEntry entry)
        {
            ConsolidacionBoleta entidad = (ConsolidacionBoleta)entry.Entity;

            Validaciones.ValidarEmail(entidad.Email);
            validarEstatus(entidad);
            establecerEdad(entidad);
            establecerCategoria(entidad);
            validarUnicidad(entry);
        }
        private void cerrarBoleta(CelulaMiembroAsistencia entidad)
        {
            IglesiaEntities     contexto = new IglesiaEntities();
            ConsolidacionBoleta boleta   = (from o in contexto.ConsolidacionBoleta where o.Email == entidad.Miembro.Email select o).SingleOrDefault();

            if (boleta != null && !ConsolidacionBoleta.Estatus.Cerrada(boleta.BoletaEstatusId))
            {
                boleta.BoletaEstatusId = ConsolidacionBoleta.Estatus.ASISTE_A_CELULA.Key;
                contexto.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave, true);
            }
        }
        private void validarUnicidad(System.Data.Objects.ObjectStateEntry entry)
        {
            // Validamos si no existe ya una boleta con el mismo correo
            ConsolidacionBoleta entidadPreexistente = (from o in SesionActual.Instance.getContexto <IglesiaEntities>().ConsolidacionBoleta where o.Email == ((ConsolidacionBoleta)entry.Entity).Email select o).SingleOrDefault();

            if (entidadPreexistente != null)
            {
                string registroExistenteMsg = string.Format("Ya existe una boleta para ese email [{0}], la cual su ID es [{1}]. De ser necesario, buscarla y modificarla o utilizar un email distinto.", entidadPreexistente.Email, entidadPreexistente.Id);

                if (entry.State == System.Data.EntityState.Added)
                {
                    throw new ExcepcionReglaNegocio(registroExistenteMsg);
                }
                else if (entry.State == System.Data.EntityState.Modified)
                {
                    if (Validaciones.ValidarCambiosEnCampo(entry, "email"))
                    {
                        throw new ExcepcionReglaNegocio(registroExistenteMsg);
                    }
                }
            }
        }
        private void establecerCategoria(ConsolidacionBoleta entidad)
        {
            int categoriaId;

            // Jovenes
            if (entidad.EstadoCivilId != 2 && entidad.Edad < 38)
            {
                categoriaId = 1;
            }
            // Matrimonios Jovenes
            else if (entidad.EstadoCivilId == 2 && entidad.Edad < 38)
            {
                categoriaId = 2;
            }
            // Familiar
            else if (entidad.Edad >= 38 && entidad.Edad < 50)
            {
                categoriaId = 3;
            }
            // Red Dorada
            else if (entidad.Edad >= 50)
            {
                categoriaId = 4;
            }
            // Otra
            else
            {
                categoriaId = 99;
            }
            entidad.CategoriaBoletaId = categoriaId;

            // Si la boleta no se encuentra asignada a alguna celula, se asigna a la celula predeterminada de acuerdo a su categoria

            /* if (!entidad.AsignadaACelulaId.HasValue)
             * {
             *  entidad.AsignadaACelulaId = entidad.ConsolidacionBoletaCategoria.Celula.CelulaId;
             * } */
        }
        private void validarEstatus(ConsolidacionBoleta entidad)
        {
            // El valor predeterminado...
            if (entidad.BoletaEstatusId.HasValue == false)
            {
                entidad.BoletaEstatusId = Estatus.SIN_REPORTE.Key;
            }

            // Si se asigna a alguna celula y NO esta cerrada... se marca con el estatus correspondiente y se crea y asigna el miembro en la celula correspondiente
            if (entidad.AsignadaACelulaId.HasValue == true && entidad.AsignadaACelulaId.Value > 0 && !Estatus.Cerrada(entidad.BoletaEstatusId))
            {
                entidad.BoletaEstatusId = Estatus.ASIGNADO_A_CELULA.Key;

                // Crea/Agrega el miembro a la celula
                crearMiembroDesdeBoleta(entidad);
            }

            // Si se marca como asignado a alguna celula pero NO tiene célula asignada se notifica al usuario
            if (entidad.BoletaEstatusId == Estatus.ASIGNADO_A_CELULA.Key && entidad.AsignadaACelulaId.HasValue == false)
            {
                throw new ExcepcionReglaNegocio("Es necesario seleccionar una célula a quien se le asignara la boleta para continuar.");
            }
        }
        private void crearMiembroDesdeBoleta(ConsolidacionBoleta entidad)
        {
            IglesiaEntities contexto = new IglesiaEntities();

            Miembro miembro = (from o in contexto.Miembro where o.Email == entidad.Email select o).SingleOrDefault();

            if (miembro == null)
            {
                miembro                      = new Miembro();
                miembro.CelulaId             = entidad.AsignadaACelulaId.Value;
                miembro.Email                = entidad.Email;
                miembro.Contrasena           = string.Empty;
                miembro.Primer_Nombre        = entidad.PrimerNombre;
                miembro.Segundo_Nombre       = entidad.SegundoNombre;
                miembro.Apellido_Paterno     = entidad.ApellidoPaterno;
                miembro.Apellido_Materno     = entidad.ApellidoMaterno;
                miembro.GeneroId             = entidad.GeneroId;
                miembro.EstadoCivilId        = entidad.EstadoCivilId;
                miembro.Fecha_Nacimiento     = (entidad.FechaDeNacimiento.HasValue ? entidad.FechaDeNacimiento : DateTime.Now);
                miembro.UbicacionMunicipioId = entidad.UbicacionMunicipioId;
                miembro.Colonia              = entidad.Colonia;
                miembro.Direccion            = entidad.Direccion;
                miembro.Tel_Casa             = entidad.TelefonoCasa;
                miembro.Tel_Movil            = entidad.TelefonoMovil;
                miembro.Tel_Trabajo          = entidad.TelefonoTrabajo;
                miembro.Comentario           = entidad.Observaciones;

                miembro.Creacion       = DateTime.Now;
                miembro.Modificacion   = DateTime.Now;
                miembro.CreacionId     = SesionActual.Instance.UsuarioId;
                miembro.ModificacionId = SesionActual.Instance.UsuarioId;

                contexto.AddObject(miembro.GetType().Name, miembro);
                contexto.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave, true);
            }
        }