public async Task Update(PlanNegocioEvolutivo model, String clavePersona, String nombrePersona)
        {
            try
            {
                var _model = await dbGI.DbSetPlanNegocioEvolutivo.FirstOrDefaultAsync(e => e.PlanNegocioEvolutivoId == model.PlanNegocioEvolutivoId);

                if (_model != null)
                {
                    var fecha = DateTime.Now;
                    dbGI.Entry(_model).CurrentValues.SetValues(model);
                    await dbGI.SaveChangesAsync();

                    await this.UpdateArchivos(model);

                    await this.UpdateGerencias(model);

                    await this.UpdateAutores(model);

                    await this.AddBitacoraMovimiento(model, clavePersona, nombrePersona, "Actualización del registro");
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task <IHttpActionResult> Update([FromBody] PlanNegocioEvolutivo model)
        {
            var fecha = DateTime.Now;

            model.FechaModificacion = fecha;

            try
            {
                var clavePersona  = SimpleSessionPersister.PersonaId;
                var nombrePersona = SimpleSessionPersister.nombreUsuario;
                if (String.IsNullOrEmpty(clavePersona) || String.IsNullOrEmpty(nombrePersona))
                {
                    throw new Exception("No se identifica al usuario");
                }
                else
                {
                    await _repo.Update(model, clavePersona, nombrePersona);

                    return(Ok("Registro actualizado exitosamente!"));
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
        public async Task UpdateTipoAcceso(PlanNegocioEvolutivo model, String clavePersona, String nombrePersona)
        {
            try
            {
                var _model = await dbGI.DbSetPlanNegocioEvolutivo.FirstOrDefaultAsync(e => e.PlanNegocioEvolutivoId == model.PlanNegocioEvolutivoId);

                if (_model != null)
                {
                    _model.TipoAcceso   = model.TipoAcceso;
                    _model.TipoAccesoGI = null;
                    //if(model.TipoAcceso==1 && model.EstadoFlujoId == 10){
                    //    NuevoOCRepository nuevo = new NuevoOCRepository();
                    //    await nuevo.Create(new NuevoOC("GI", "II", _model.Titulo,
                    //    "indexGI.html#/buscarIdeaInnovadoraDetalles/" + _model.PlanNegocioEvolutivoId,
                    //     _model.PlanNegocioEvolutivoId.ToString()
                    //    ));
                    //}
                    await dbGI.SaveChangesAsync();

                    await this.AddBitacoraMovimiento(model, clavePersona, nombrePersona, "Se cambia el tipo de acceso (" + model.TipoAcceso + ")");
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task <IHttpActionResult> GetConsultaCartera(PlanNegocioEvolutivo p)
        {
            try
            {
                var PlanNegocioEvolutivo = await _repo.GetConsultaCartera(p);

                return(Ok(PlanNegocioEvolutivo));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
        private async Task UpdateArchivos(PlanNegocioEvolutivo model)
        {
            try
            {
                var _model = await dbGI.DbSetPlanNegocioEvolutivo
                             .Where(e => e.PlanNegocioEvolutivoId == model.PlanNegocioEvolutivoId)
                             .Include(x => x.PlanNegocioEvolArchivos)
                             .FirstOrDefaultAsync();

                if (_model != null)
                {
                    if (model.PlanNegocioEvolArchivos != null && model.PlanNegocioEvolArchivos.Count() > 0)
                    {
                        //string regNew = JsonConvert.SerializeObject(model.PlanNegocioEvolArchivos);
                        //string regAnt = JsonConvert.SerializeObject(_model.PlanNegocioEvolArchivos);
                        //if (regAnt.Equals(regNew))
                        //{
                        //    return;
                        //}

                        PlanNegocioEvolArchivosRepository gerDB = new PlanNegocioEvolArchivosRepository(dbGI);
                        var fecha = DateTime.Now;
                        foreach (var e in model.PlanNegocioEvolArchivos)
                        {
                            if (e.Id < 1)//nuevo
                            {
                                e.Fecha = fecha;
                                e.PlanNegocioEvolutivoId = model.PlanNegocioEvolutivoId;
                                await gerDB.Create(e);
                            }
                            else
                            {
                                if (e.Adjunto != null && e.Adjunto.nombre.Equals("eliminar"))
                                {
                                    await gerDB.Delete(e.Id);
                                }
                                //else
                                //    //await gerDB.Update(e);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task UpdateEstado(PlanNegocioEvolutivo model)
        {
            try
            {
                var _model = await dbGI.DbSetPlanNegocioEvolutivo.FirstOrDefaultAsync(e => e.PlanNegocioEvolutivoId == model.PlanNegocioEvolutivoId);

                if (_model != null)
                {
                    _model.EstadoFlujoId = model.EstadoFlujoId;
                    await dbGI.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        /// <summary>
        /// Obtiene todos los planes de negocio por persona
        /// </summary>
        /// <returns>Object</returns>
        public async Task <IEnumerable <Object> > GetMisPlanesAnualesConsulta(PlanNegocioEvolutivo parametros)
        {
            try
            {
                var fechaActual = DateTime.Now;
                var entities    = await dbGI.DbSetPlanNegocioEvolutivo
                                  .Where(e => (e.EstadoFlujoId == 10 || e.EstadoFlujoId == 6) && e.ClavePersona == parametros.ClavePersona)
                                  .AsNoTracking()
                                  .Include(x => x.PlanNegocioEvolAutores)
                                  .Include(x => x.Propuesta)
                                  .Include(x => x.Proyecto)
                                  .Include(x => x.EstadoFlujo)
                                  .Include(x => x.TipoAccesoGI)
                                  .Select(x => new
                {
                    x.PlanNegocioEvolutivoId,
                    x.Titulo,
                    propuesta = x.Propuesta.NombreTecnico,
                    x.ProyectoId,
                    Proyecto = x.Proyecto.Nombre,
                    x.Tema,
                    x.OfertaDeValor,
                    x.TipoAcceso,
                    AccesoPublico = x.TipoAcceso == 1 ? true : false,
                    TipoAccesoGI  = x.TipoAccesoGI.Nombre,
                    x.FechaRegistro,
                    PlanNegocioEvolAutores   = x.PlanNegocioEvolAutores.Select(aut => aut.ClavePersona + " " + aut.Nombre).ToList(),
                    estadoflujo              = x.EstadoFlujo.Descripcion,
                    PlanNegocioEvolGerencias =
                        (from unidad in dbGI.DbSetUnidadOrganizacional
                         where x.PlanNegocioEvolGerencias.Select(g => g.ClaveUnidad).ToList().Contains(unidad.ClaveUnidad) &&
                         unidad.FechaEfectiva == dbGI.DbSetUnidadOrganizacional.Where(
                             p => p.FechaEfectiva <= fechaActual &&
                             p.ClaveUnidad == unidad.ClaveUnidad
                             ).Max(e => e.FechaEfectiva)
                         select unidad.NombreUnidad).ToList()
                })
                                  .ToListAsync();

                return(entities);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        private async Task UpdateAutores(PlanNegocioEvolutivo model)
        {
            try
            {
                var _model = await dbGI.DbSetPlanNegocioEvolutivo
                             .Include(x => x.PlanNegocioEvolAutores)
                             .FirstOrDefaultAsync(e => e.PlanNegocioEvolutivoId == model.PlanNegocioEvolutivoId);

                if (_model != null)
                {
                    if (model.PlanNegocioEvolAutores != null && model.PlanNegocioEvolAutores.Count() > 0)
                    {
                        PlanNegocioEvolAutoresRepository gerDB = new PlanNegocioEvolAutoresRepository(dbGI);
                        foreach (var e in model.PlanNegocioEvolAutores)
                        {
                            if (e.Id < 1)//nuevo
                            {
                                await gerDB.Create(e);
                            }
                            else
                            {
                                if (e.Nombre == null)
                                {
                                    ////
                                }
                                else
                                {
                                    if (e.Nombre.Equals("eliminar") || e.ClavePersona.Equals("eliminar"))
                                    {
                                        await gerDB.Delete(e.Id);
                                    }
                                    else
                                    {//await gerDB.Update(e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task <IHttpActionResult> UpdateEstado(PlanNegocioEvolutivo model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                await _repo.UpdateEstado(model);

                return(Ok("Registro actualizado exitosamente!"));
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }
        private async Task UpdateGerencias(PlanNegocioEvolutivo model)
        {
            try
            {
                var _model = await dbGI.DbSetPlanNegocioEvolutivo
                             .Include(x => x.PlanNegocioEvolGerencias)
                             .FirstOrDefaultAsync(e => e.PlanNegocioEvolutivoId == model.PlanNegocioEvolutivoId);

                if (_model != null)
                {
                    if (model.PlanNegocioEvolGerencias != null && model.PlanNegocioEvolGerencias.Count() > 0)
                    {
                        PlanNegocioEvolGerenciasRepository gerDB = new PlanNegocioEvolGerenciasRepository(dbGI);
                        foreach (var g in model.PlanNegocioEvolGerencias)
                        {
                            if (g.Id < 1)//nuevo
                            {
                                await gerDB.Create(g);
                            }
                            else
                            {
                                if (g.ClaveUnidad.Equals("eliminar"))
                                {
                                    await gerDB.Delete(g.Id);
                                }
                                else
                                { //await gerDB.Update(g);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        public async Task Create(PlanNegocioEvolutivo model, String clavePersona, String nombrePersona)
        {
            try
            {
                var fecha = DateTime.Now;
                if (model != null && model.PlanNegocioEvolArchivos != null && model.PlanNegocioEvolArchivos.Count() > 0)
                {
                    foreach (var a in model.PlanNegocioEvolArchivos)
                    {
                        a.Fecha = fecha;
                    }
                }

                dbGI.DbSetPlanNegocioEvolutivo.Add(model);
                await dbGI.SaveChangesAsync();

                await this.AddBitacoraMovimiento(model, clavePersona, nombrePersona, "Creación del registro");
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        private async Task AddBitacoraMovimiento(PlanNegocioEvolutivo model,
                                                 String clavePersona, String nombrePersona, String movimiento)
        {
            try
            {
                if (model != null)
                {
                    var fecha = DateTime.Now;

                    BitacoraMovimientosGI bita = new BitacoraMovimientosGI();
                    bita.Fecha        = fecha;
                    bita.ClavePersona = clavePersona;
                    bita.Movimiento   = movimiento;
                    bita.OcsId        = "PNE";
                    bita.RegistroId   = model.PlanNegocioEvolutivoId;
                    dbGI.DbSetBitacoraMovimientosGI.Add(bita);
                    await dbGI.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }
        /// <summary>
        /// Obtener todos las planes de negocio de acuerdo a los parametros ingresados por el usuario
        /// </summary>
        /// <returns><IEnumerable<Object>></returns>
        public async Task <IEnumerable <Object> > GetConsultaCartera(PlanNegocioEvolutivo parametros)
        {
            try
            {
                var fechaActual = DateTime.Now;
                var entities    = dbGI.DbSetPlanNegocioEvolutivo
                                  .Where(e => e.EstadoFlujoId == 10)
                                  .AsNoTracking()
                                  .Include(x => x.PlanNegocioEvolAutores)
                                  .Include(x => x.Propuesta)
                                  .Include(x => x.Proyecto)
                                  .Include(x => x.EstadoFlujo)
                                  .Include(x => x.TipoAccesoGI);

                if (entities != null)
                {
                    if (!String.IsNullOrEmpty(parametros.ClavePersona))  //busqueda por los planes de negocio de la persona logueada en el sigco (seccion "mis planes de negocio")
                    {
                        entities = entities.Where(e => e.ClavePersona == parametros.ClavePersona);
                    }
                    if (!String.IsNullOrEmpty(parametros.Titulo)) //busqueda por plan de negocio evolutivo
                    {
                        var fksplanes = await GetPlanesLikeNombreLatin1(parametros.Titulo);

                        entities = entities.Where(e => fksplanes.Contains(e.PlanNegocioEvolutivoId));
                    }
                    if (!String.IsNullOrEmpty(parametros.NombreProyecto)) //busqueda por nombre de la propuesta asociada
                    {
                        var fkspropuestas = await GetPropuestasLikeNombreLatin1(parametros.NombreProyecto);

                        entities = entities.Where(e => fkspropuestas.Contains(e.PropuestaClave)); //*******verificar
                    }
                    if (!String.IsNullOrEmpty(parametros.ProyectoId))                             //busqueda por numero de proyecto
                    {
                        entities = entities.Where(e => e.ProyectoId != null && e.ProyectoId == parametros.ProyectoId);
                    }
                    if (!String.IsNullOrEmpty(parametros.OfertaDeValor)) //busqueda por oferta de valor
                    {
                        var fksplanes = await GetPlanesByOferLikeNombreLatin1(parametros.OfertaDeValor);

                        entities = entities.Where(e => fksplanes.Contains(e.PlanNegocioEvolutivoId));
                    }
                    if (!String.IsNullOrEmpty(parametros.NombrePersona))  //busqueda por autores del plan de negocio
                    {
                        PersonasRepository p = new PersonasRepository();
                        //   var fksautores= await p.GetAllClavesByLikeNombreLatin1(parametros.NombrePersona);
                        //   entities= entities.Where(e=> fksautores.Contains(e.ClavePersona));
                        var fksautores = await GetPlanesByAutorLikeNombreLatin1(parametros.NombrePersona);

                        entities = entities.Where(e => fksautores.Contains(e.PlanNegocioEvolutivoId));
                    }
                    if (!String.IsNullOrEmpty(parametros.ClaveUnidad))  //busqueda por plan de negocio evolutivo
                    {
                        var fksunidades = await dbGI.DbSetPlanNegocioEvolGerencias.AsNoTracking().Where(e => e.ClaveUnidad == parametros.ClaveUnidad).Select(x => x.PlanNegocioEvolutivoId).ToListAsync();

                        entities = entities.Where(e => fksunidades.Contains(e.PlanNegocioEvolutivoId));
                    }
                    if (parametros.TipoAcceso != 0)  //busqueda por tipo de acceso
                    {
                        entities = entities.Where(e => e.TipoAcceso == parametros.TipoAcceso);
                    }

                    var datos = entities.Select(x => new
                    {
                        PlanNegocioEvolutivoId = x.PlanNegocioEvolutivoId,
                        Titulo                   = x.Titulo,
                        propuesta                = x.Propuesta.NombreTecnico,
                        ProyectoId               = x.ProyectoId,
                        Proyecto                 = x.Proyecto.Nombre,
                        Tema                     = x.Tema,
                        OfertaDeValor            = x.OfertaDeValor,
                        TipoAcceso               = x.TipoAcceso,
                        AccesoPublico            = x.TipoAcceso == 1 ? true : false,
                        TipoAccesoGI             = x.TipoAccesoGI.Nombre,
                        FechaRegistro            = x.FechaRegistro,
                        PlanNegocioEvolAutores   = x.PlanNegocioEvolAutores.Select(aut => aut.ClavePersona + " " + aut.Nombre).ToList(),
                        estadoflujo              = x.EstadoFlujo.Descripcion,
                        PlanNegocioEvolGerencias =
                            (from unidad in dbGI.DbSetUnidadOrganizacional
                             where x.PlanNegocioEvolGerencias.Select(g => g.ClaveUnidad).ToList().Contains(unidad.ClaveUnidad) &&
                             unidad.FechaEfectiva == dbGI.DbSetUnidadOrganizacional.Where(
                                 p => p.FechaEfectiva <= fechaActual &&
                                 p.ClaveUnidad == unidad.ClaveUnidad
                                 ).Max(e => e.FechaEfectiva)
                             select unidad.NombreUnidad).ToList()
                    }).ToList();

                    return(datos);
                }

                return(entities);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }