예제 #1
0
        public IHttpActionResult Get(int RolId)
        {
            try
            {
                int             usuarioId = int.Parse(DbContextAIVH.GetUserName(User));
                var             gastosDb  = RolId == 1 ? db.Gasto.Where(a => a.UsuarioId == usuarioId).ToList() : db.Gasto.ToList();
                List <GetGasto> gastos    = gastosDb.Select(b => new GetGasto
                {
                    gastoId       = b.GastoId,
                    centroCostos  = b.CentroCosto.Descripcion,
                    resposable    = b.Usuario1.Nombre + " " + b.Usuario1.Paterno + " " + b.Usuario1.Materno,
                    fechaInicial  = b.FechaInicio,
                    fechaFinal2   = b.FechaFin,
                    anticipo      = (b.Efectivo + b.ChequeTans).ToString(),
                    gastos        = b.GastoDetalle.Sum(c => c.Total).ToString(),
                    saldo         = Math.Abs((b.Efectivo + b.ChequeTans) - b.GastoDetalle.Sum(c => c.Total)).ToString(),
                    observaciones = (b.Efectivo + b.ChequeTans) > b.GastoDetalle.Sum(c => c.Total) ? "Devolucion" : (b.Efectivo + b.ChequeTans) == b.GastoDetalle.Sum(c => c.Total) ? "" : "Reembolso",
                    fecha2        = b.Fecha,
                    estatus       = b.Estatus.Descripcion,
                    estatusId     = b.EstatusId
                }).ToList();

                var periodos = gastosDb.Select(x => new
                {
                    value = x.FechaInicio.ToString("MM/yyyy"),
                    text  = Business.General.General.MonthName(x.FechaInicio.Month) + " " + x.FechaInicio.Year
                }).Distinct().ToList();

                return(Ok(new { gastos, periodos }));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error"));
            }
        }
예제 #2
0
        public IHttpActionResult SaveEvento(Models.DTO.Evento evento)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));

                // insertar evento
                if (evento.eventoId == 0)
                {
                    db.Evento.Add(new Models.Evento
                    {
                        CentroCostoId      = evento.centroCostosId,
                        LugarEvento        = evento.lugarEvento,
                        NombreEvento       = evento.nombreEvento,
                        EventoTipoId       = evento.eventoTipoId,
                        UsuarioId          = evento.instructorId,
                        FechaEvento        = DateTime.ParseExact((evento.fechaEvento.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        Efectivo           = 0,
                        ChequeTans         = 0,
                        NumeroChequeTans   = "",
                        Fecha              = DateTime.Now,
                        Hora               = DateTime.Now.TimeOfDay,
                        UsuarioIdActualizo = usuarioId,
                        EstatusId          = 1
                    });
                }
                else
                {
                    var eventoDb = db.Evento.Where(a => a.EventoId == evento.eventoId).FirstOrDefault();

                    eventoDb.CentroCostoId      = evento.centroCostosId;
                    eventoDb.LugarEvento        = evento.lugarEvento;
                    eventoDb.NombreEvento       = evento.nombreEvento;
                    eventoDb.EventoTipoId       = evento.eventoTipoId;
                    eventoDb.UsuarioId          = evento.instructorId;
                    eventoDb.FechaEvento        = DateTime.ParseExact((evento.fechaEvento.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    eventoDb.Fecha              = DateTime.Now;
                    eventoDb.Hora               = DateTime.Now.TimeOfDay;
                    eventoDb.UsuarioIdActualizo = usuarioId;
                    eventoDb.EstatusId          = 1;
                }

                db.SaveChanges();

                int eventoId = db.Evento.Local.FirstOrDefault().EventoId;

                return(Ok(new
                {
                    message = "El evento se guardó correctamente.",
                    eventoId
                }));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error al guardar evento"));
            }
        }
예제 #3
0
        public async Task <IHttpActionResult> ChangePassword(DTO.UsuarioPassword usuarioPassword)
        {
            try
            {
                string usuarioId = DbContextAIVH.GetUserName(User);

                AccountController accountController = new AccountController();
                IHttpActionResult result            = await accountController.FindUser(usuarioId, usuarioPassword.password);

                if (result is OkNegotiatedContentResult <bool> status)
                {
                    if (usuarioPassword.newPassword != usuarioPassword.confirmPassword)
                    {
                        return(BadRequest("El nuevo password y su confirmación no son iguales."));
                    }

                    DTO.Usuario usuario = new
                                          DTO.Usuario
                    {
                        NickName = usuarioId,
                        Password = usuarioPassword.newPassword
                    };

                    result = await accountController.UpdateUserAsync(usuario);

                    if (result is OkNegotiatedContentResult <bool> status2)
                    {
                        return(Ok("Cambio de password correcto. <br/><br/> Es necesario iniciar sesión nuevamente"));
                    }
                    else
                    {
                        return(BadRequest("Error al cambiar password."));
                    }
                }
                else
                {
                    return(BadRequest("El password actual es incorrecto."));
                }
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error al cambiar password."));
            }
        }
예제 #4
0
        public IHttpActionResult GetPerfil()
        {
            int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));
            var data      = db.Usuario
                            .Where(x => x.UsuarioId == usuarioId)
                            .Select(x => new
            {
                nombre = x.Nombre + " " + x.Paterno + " " + x.Materno,
                rol    = x.UsuarioRol.Descripcion
            }).FirstOrDefault();

            if (data != null)
            {
                return(Ok(data));
            }
            else
            {
                return(NotFound());
            }
        }
예제 #5
0
        public IHttpActionResult Aprobar(int gastoId)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));

                var gasto = db.Gasto.Where(a => a.GastoId == gastoId).FirstOrDefault();
                gasto.EstatusId          = 3;
                gasto.Fecha              = DateTime.Now;
                gasto.Hora               = DateTime.Now.TimeOfDay;
                gasto.UsuarioIdActualizo = usuarioId;

                db.SaveChanges();

                return(Ok("La rendicion se aprobo correctamente."));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error"));
            }
        }
예제 #6
0
        public IHttpActionResult Save(Models.DTO.Gasto gastos)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));

                List <Models.GastoDetalle> gastoDetalle = new List <Models.GastoDetalle>();
                int consecutivoId = 1;
                gastos.gastoDetalle.ForEach(n =>
                {
                    if (gastos.gastoId == 0)
                    {
                        gastoDetalle.Add(new Models.GastoDetalle
                        {
                            ConsecutivoId     = consecutivoId,
                            ComprobanteTipoId = n.comprobanteTipoId,
                            Fecha             = DateTime.ParseExact((n.fecha.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                            SubConceptoId     = n.subConceptoId,
                            Descripcion       = n.descripcion,
                            Proveedor         = n.proveedor,
                            SubTotal          = n.subTotal,
                            Iva   = n.iva,
                            Total = n.total
                        });
                    }
                    else
                    {
                        gastoDetalle.Add(new Models.GastoDetalle
                        {
                            GastoId           = gastos.gastoId,
                            ConsecutivoId     = consecutivoId,
                            ComprobanteTipoId = n.comprobanteTipoId,
                            Fecha             = DateTime.ParseExact((n.fecha.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                            SubConceptoId     = n.subConceptoId,
                            Descripcion       = n.descripcion,
                            Proveedor         = n.proveedor,
                            SubTotal          = n.subTotal,
                            Iva   = n.iva,
                            Total = n.total
                        });
                    }
                    consecutivoId += 1;
                });

                DateTime?nulo = null;

                if (gastos.gastoId == 0)
                {
                    db.Gasto.Add(new Models.Gasto
                    {
                        CentroCostoId      = gastos.centroCostosId,
                        UsuarioId          = usuarioId,
                        FechaInicio        = DateTime.ParseExact((gastos.fechaInicio.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        FechaFin           = DateTime.ParseExact((gastos.fechaFin.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        Efectivo           = gastos.efectivo,
                        ChequeTans         = gastos.chequeTransNuevo,
                        FechasChequeTans   = gastos.fechaNuevo != "" ? DateTime.ParseExact((gastos.fechaNuevo.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture) : nulo,
                        NumeroChequeTans   = gastos.numeroNuevo,
                        Fecha              = DateTime.Now,
                        Hora               = DateTime.Now.TimeOfDay,
                        UsuarioIdActualizo = usuarioId,
                        EstatusId          = 1,
                        GastoDetalle       = gastoDetalle
                    });
                }
                else
                {
                    var gastoDb = db.Gasto.Where(a => a.GastoId == gastos.gastoId).FirstOrDefault();
                    db.GastoDetalle.RemoveRange(gastoDb.GastoDetalle);

                    gastoDb.CentroCostoId      = gastos.centroCostosId;
                    gastoDb.FechaInicio        = DateTime.ParseExact((gastos.fechaInicio.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    gastoDb.FechaFin           = DateTime.ParseExact((gastos.fechaFin.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    gastoDb.Efectivo           = gastos.efectivo;
                    gastoDb.ChequeTans         = gastos.chequeTransNuevo;
                    gastoDb.FechasChequeTans   = gastos.fechaNuevo != "" ? DateTime.ParseExact((gastos.fechaNuevo.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture) : nulo;
                    gastoDb.NumeroChequeTans   = gastos.numeroNuevo;
                    gastoDb.Fecha              = DateTime.Now;
                    gastoDb.Hora               = DateTime.Now.TimeOfDay;
                    gastoDb.UsuarioIdActualizo = usuarioId;
                    db.GastoDetalle.AddRange(gastoDetalle);
                }

                db.SaveChanges();

                int gastoId = db.Gasto.Local.FirstOrDefault().GastoId;

                return(Ok(new
                {
                    message = "La rendición de gastos se guardó correctamente.",
                    gastoId
                }));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error al guardar rendicion de gastos"));
            }
        }
예제 #7
0
 public IHttpActionResult GetMenu()
 {
     return(Ok(Business.General.Modulo.Get(db, int.Parse(DbContextAIVH.GetUserName(User)))));
 }
예제 #8
0
        public async Task <IHttpActionResult> SaveUsuario(DTO.Usuario usuario)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));


                if (usuario.UsuarioId != 0)
                {
                    var usuarioDb = db.Usuario.Where(a => a.UsuarioId == usuario.UsuarioId).FirstOrDefault();
                    usuarioDb.Nombre          = usuario.Nombre;
                    usuarioDb.Paterno         = usuario.Paterno;
                    usuarioDb.Materno         = usuario.Materno;
                    usuarioDb.UsuarioRolId    = usuario.RolId;
                    usuarioDb.Fecha           = DateTime.Now;
                    usuarioDb.Hora            = DateTime.Now.TimeOfDay;
                    usuarioDb.UsuarioIdGenero = usuarioId;
                    usuarioDb.EstatusId       = usuario.EstatusId;
                }
                else
                {
                    db.Usuario.Add(new Usuario
                    {
                        Nombre          = usuario.Nombre,
                        Paterno         = usuario.Paterno,
                        Materno         = usuario.Materno,
                        UsuarioRolId    = usuario.RolId,
                        Fecha           = DateTime.Now,
                        Hora            = DateTime.Now.TimeOfDay,
                        UsuarioIdGenero = usuarioId,
                        EstatusId       = usuario.EstatusId
                    });
                }

                db.SaveChanges();
                AccountController accountController = new AccountController();
                IHttpActionResult result            = null;
                if (usuario.UsuarioId != 0 && usuario.Password != "")
                {
                    result = await accountController.UpdateUserAsync(usuario);
                }
                else if (usuario.UsuarioId == 0)
                {
                    usuario.NickName = db.Usuario.Local.FirstOrDefault().UsuarioId.ToString();
                    result           = await accountController.Register(usuario);
                }


                if (result is OkNegotiatedContentResult <bool> status || result == null)
                {
                    return(Ok(new
                    {
                        message = "El usuario se guardó correctamente."
                    }));
                }
                else
                {
                    return(BadRequest("Error al guardar usuario"));
                }
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error al guardar usuario"));
            }
        }
예제 #9
0
        public IHttpActionResult Get(int RolId)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));
                var EventosDb = RolId == 1 ? db.Evento.Where(a => a.UsuarioId == usuarioId).ToList() : db.Evento.ToList();
                List <Models.DTO.Evento> eventos = EventosDb.Select(b => new Models.DTO.Evento
                {
                    folio          = b.CentroCosto.Nomenglatura + b.EventoId,
                    eventoId       = b.EventoId,
                    centroCostosId = b.CentroCostoId,
                    centroCostos   = b.CentroCosto.Descripcion,
                    lugarEvento    = b.LugarEvento,
                    nombreEvento   = b.NombreEvento,
                    eventoTipoId   = b.EventoTipo.EventoTipoId,
                    eventoTipo     = b.EventoTipo.Descripcion,
                    instructorId   = b.UsuarioId,
                    instructor     = b.Usuario.Nombre + " " + b.Usuario.Paterno + " " + b.Usuario.Materno,
                    fechaEvento2   = b.FechaEvento,
                    estatusId      = b.EstatusId,
                    estatus        = b.Estatus.Descripcion,
                    donantes       = b.EventoDonante.Select(c => new Donantes
                    {
                        eventoId  = c.EventoId,
                        donanteId = c.DonanteId,
                        nombre    = c.Nombre,
                        apellido  = c.Apellido,
                        efectivo  = c.Efectivo,
                        deposito  = c.DepositooTransferencia,
                        cheque    = c.Cheque,
                        tarjeta   = c.TarjetaCredito,
                        email     = c.Email,
                        celular   = c.Celular
                    }).ToList(),
                    efectivo         = b.Efectivo,
                    chequeTans       = b.ChequeTans,
                    fechachequeTans  = b.FechasChequeTans != null ? b.FechasChequeTans?.ToString("dd/MM/yyyy", Cultura) : "",
                    numeroChequeTans = b.NumeroChequeTans,
                    anticipo         = b.Efectivo + b.ChequeTans,
                    gastos           = b.EventoGastoDetalle.Select(d => new Models.DTO.EventoGastoDetalle
                    {
                        eventoId          = d.EventoId,
                        consecutivoId     = d.ConsecutivoId,
                        comprobanteTipoId = d.ComprobanteTipoId,
                        comprobanteTipo   = d.ComprobanteTipo.Descripcion,
                        fecha2            = d.Fecha,
                        conceptoId        = d.SubConcepto.Concepto.ConceptoId,
                        concepto          = d.SubConcepto.Concepto.Descripcion,
                        subConceptoId     = d.SubConceptoId,
                        subConcepto       = d.SubConcepto.Descripcion,
                        descripcion       = d.Descripcion,
                        proveedor         = d.Proveedor,
                        subTotal          = d.SubTotal,
                        iva   = d.Iva,
                        total = d.Total
                    }).ToList(),
                    totalRecaudado = b.EventoDonante.Sum(s => s.Efectivo + s.DepositooTransferencia + s.Cheque + s.TarjetaCredito).ToString(),
                    totalGastos    = b.EventoGastoDetalle.Sum(c => c.Total).ToString(),
                    saldo          = Math.Abs((b.Efectivo + b.ChequeTans) - b.EventoGastoDetalle.Sum(c => c.Total)).ToString(),
                    observaciones  = (b.Efectivo + b.ChequeTans) > b.EventoGastoDetalle.Sum(c => c.Total) ? "Devolucion" : (b.Efectivo + b.ChequeTans) == b.EventoGastoDetalle.Sum(c => c.Total) ? "" : "Reembolso"
                }).ToList();

                var periodos = EventosDb.Select(x => new
                {
                    value = x.FechaEvento.ToString("MM/yyyy"),
                    text  = Business.General.General.MonthName(x.FechaEvento.Month) + " " + x.FechaEvento.Year
                }).Distinct().ToList();

                return(Ok(new{ eventos, periodos }));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error"));
            }
        }
예제 #10
0
        public IHttpActionResult SaveRendicion(Models.DTO.Evento evento)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));

                //insertar participantes
                List <EventoDonante> eventoDonante = new List <EventoDonante>();
                int donanteId = 1;
                evento.donantes.ForEach(n =>
                {
                    eventoDonante.Add(new EventoDonante
                    {
                        EventoId  = evento.eventoId,
                        DonanteId = donanteId,
                        Nombre    = n.nombre,
                        Apellido  = n.apellido,
                        Efectivo  = n.efectivo,
                        DepositooTransferencia = n.deposito,
                        Cheque         = n.cheque,
                        TarjetaCredito = n.tarjeta,
                        Email          = n.email,
                        Celular        = n.celular
                    });
                    donanteId += 1;
                });

                //insertar gastos
                List <Models.EventoGastoDetalle> eventoGastoDetalle = new List <Models.EventoGastoDetalle>();
                int consecutivoId = 1;
                evento.gastos.ForEach(n =>
                {
                    eventoGastoDetalle.Add(new Models.EventoGastoDetalle
                    {
                        EventoId          = evento.eventoId,
                        ConsecutivoId     = consecutivoId,
                        ComprobanteTipoId = n.comprobanteTipoId,
                        Fecha             = DateTime.ParseExact((n.fecha.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        SubConceptoId     = n.subConceptoId,
                        Descripcion       = n.descripcion,
                        Proveedor         = n.proveedor,
                        SubTotal          = n.subTotal,
                        Iva   = n.iva,
                        Total = n.total
                    });
                    consecutivoId += 1;
                });
                DateTime?nulo = null;

                var eventoDb = db.Evento.Where(a => a.EventoId == evento.eventoId).FirstOrDefault();
                db.EventoGastoDetalle.RemoveRange(eventoDb.EventoGastoDetalle);
                db.EventoDonante.RemoveRange(eventoDb.EventoDonante);

                eventoDb.Efectivo           = evento.efectivo;
                eventoDb.ChequeTans         = evento.chequeTans;
                eventoDb.FechasChequeTans   = evento.fechachequeTans != "" ? DateTime.ParseExact((evento.fechachequeTans.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture) : nulo;
                eventoDb.NumeroChequeTans   = evento.numeroChequeTans;
                eventoDb.Fecha              = DateTime.Now;
                eventoDb.Hora               = DateTime.Now.TimeOfDay;
                eventoDb.UsuarioIdActualizo = usuarioId;

                if (eventoGastoDetalle.Count > 0)
                {
                    db.EventoGastoDetalle.AddRange(eventoGastoDetalle);
                }
                if (eventoDonante.Count > 0)
                {
                    db.EventoDonante.AddRange(eventoDonante);
                }



                db.SaveChanges();

                int eventoId = db.Evento.Local.FirstOrDefault().EventoId;

                return(Ok(new
                {
                    message = "El evento se guardó correctamente.",
                    eventoId
                }));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error al guardar evento"));
            }
        }
예제 #11
0
        public IHttpActionResult SaveRendicion(Models.DTO.Curso curso)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));

                List <Models.CursoGastoDetalle> cursoGastoDetalle = new List <Models.CursoGastoDetalle>();
                int consecutivoId = 1;
                curso.gastos.ForEach(n =>
                {
                    cursoGastoDetalle.Add(new Models.CursoGastoDetalle
                    {
                        CursoId           = curso.cursoId,
                        ConsecutivoId     = consecutivoId,
                        UsuarioId         = n.instructorId,
                        ComprobanteTipoId = n.comprobanteTipoId,
                        Fecha             = DateTime.ParseExact((n.fecha.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        SubConceptoId     = n.subConceptoId,
                        Descripcion       = n.descripcion,
                        Proveedor         = n.proveedor,
                        SubTotal          = n.subTotal,
                        Iva   = n.iva,
                        Total = n.total
                    });
                    consecutivoId += 1;
                });
                DateTime?nulo = null;

                var cursoDb = db.Curso.Where(a => a.CursoId == curso.cursoId).FirstOrDefault();
                db.CursoGastoDetalle.RemoveRange(cursoDb.CursoGastoDetalle);

                cursoDb.Efectivo           = curso.efectivo;
                cursoDb.ChequeTans         = curso.chequeTans;
                cursoDb.FechasChequeTans   = curso.fechachequeTans != "" ? DateTime.ParseExact((curso.fechachequeTans.Replace("-", "/")), "dd/MM/yyyy", CultureInfo.InvariantCulture) : nulo;
                cursoDb.NumeroChequeTans   = curso.numeroChequeTans;
                cursoDb.Fecha              = DateTime.Now;
                cursoDb.Hora               = DateTime.Now.TimeOfDay;
                cursoDb.UsuarioIdActualizo = usuarioId;

                if (cursoGastoDetalle.Count > 0)
                {
                    db.CursoGastoDetalle.AddRange(cursoGastoDetalle);
                }



                db.SaveChanges();

                int cursoId = db.Curso.Local.FirstOrDefault().CursoId;

                return(Ok(new
                {
                    message = "El curso se guardó correctamente.",
                    cursoId
                }));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error al guardar curso"));
            }
        }
예제 #12
0
        public IHttpActionResult SaveCurso(Models.DTO.Curso curso)
        {
            try
            {
                int usuarioId = int.Parse(DbContextAIVH.GetUserName(User));

                //insertar participantes
                List <CursoParticipante> cursoParticipante = new List <CursoParticipante>();
                int consecutivoId = 1;
                curso.participantes.ForEach(n =>
                {
                    if (curso.cursoId == 0)
                    {
                        cursoParticipante.Add(new CursoParticipante
                        {
                            ParticipanteId         = consecutivoId,
                            Nombre                 = n.nombre,
                            Apellido               = n.apellido,
                            Efectivo               = n.efectivo,
                            DepositooTransferencia = n.deposito,
                            Cheque                 = n.cheque,
                            TarjetaCredito         = n.tarjeta,
                            Email   = n.email,
                            Celular = n.celular
                        });
                    }
                    else
                    {
                        cursoParticipante.Add(new CursoParticipante
                        {
                            CursoId                = curso.cursoId,
                            ParticipanteId         = consecutivoId,
                            Nombre                 = n.nombre,
                            Apellido               = n.apellido,
                            Efectivo               = n.efectivo,
                            DepositooTransferencia = n.deposito,
                            Cheque                 = n.cheque,
                            TarjetaCredito         = n.tarjeta,
                            Email   = n.email,
                            Celular = n.celular
                        });
                    }
                    consecutivoId += 1;
                });

                //insertar gastos de comision de instructores
                var pagoParticipantes = curso.participantes.Sum(a => a.efectivo + a.cheque + a.deposito + a.tarjeta);

                List <Models.CursoGastoDetalle> cursoGastoDetalle = new List <Models.CursoGastoDetalle>();

                if (curso.cursoId == 0)
                {
                    cursoGastoDetalle.Add(new Models.CursoGastoDetalle
                    {
                        ConsecutivoId     = 1,
                        UsuarioId         = curso.instructorId1,
                        ComprobanteTipoId = 3,
                        Fecha             = DateTime.Now,
                        SubConceptoId     = 3,
                        Descripcion       = "Comisión por impartición de curso " + curso.comision1 + " %.",
                        Proveedor         = db.Usuario.Where(a => a.UsuarioId == curso.instructorId1).Select(b => b.Nombre + " " + b.Paterno + " " + b.Materno).FirstOrDefault(),
                        SubTotal          = (curso.comision1 * pagoParticipantes) / 100,
                        Iva   = 0,
                        Total = (curso.comision1 * pagoParticipantes) / 100
                    });

                    if (curso.instructorId2 != 0)
                    {
                        cursoGastoDetalle.Add(new Models.CursoGastoDetalle
                        {
                            ConsecutivoId     = 2,
                            UsuarioId         = curso.instructorId2,
                            ComprobanteTipoId = 3,
                            Fecha             = DateTime.Now,
                            SubConceptoId     = 3,
                            Descripcion       = "Comisión por impartición de curso " + curso.comision2 + " %.",
                            Proveedor         = db.Usuario.Where(a => a.UsuarioId == curso.instructorId2).Select(b => b.Nombre + " " + b.Paterno + " " + b.Materno).FirstOrDefault(),
                            SubTotal          = (curso.comision2 * pagoParticipantes) / 100,
                            Iva   = 0,
                            Total = (curso.comision2 * pagoParticipantes) / 100
                        });
                    }
                }
                else
                {
                    int consecutivoGastos = 1;

                    cursoGastoDetalle.Add(new Models.CursoGastoDetalle
                    {
                        CursoId           = curso.cursoId,
                        ConsecutivoId     = consecutivoGastos,
                        UsuarioId         = curso.instructorId1,
                        ComprobanteTipoId = 3,
                        Fecha             = DateTime.Now,
                        SubConceptoId     = 3,
                        Descripcion       = "Comisión por impartición de curso " + curso.comision1 + "%.",
                        Proveedor         = db.Usuario.Where(a => a.UsuarioId == curso.instructorId1).Select(b => b.Nombre + " " + b.Paterno + " " + b.Materno).FirstOrDefault(),
                        SubTotal          = (curso.comision1 * pagoParticipantes) / 100,
                        Iva   = 0,
                        Total = (curso.comision1 * pagoParticipantes) / 100
                    });
                    if (curso.instructorId2 != 0)
                    {
                        consecutivoGastos += 1;
                        cursoGastoDetalle.Add(new Models.CursoGastoDetalle
                        {
                            CursoId           = curso.cursoId,
                            ConsecutivoId     = consecutivoGastos,
                            UsuarioId         = curso.instructorId2,
                            ComprobanteTipoId = 3,
                            Fecha             = DateTime.Now,
                            SubConceptoId     = 3,
                            Descripcion       = "Comisión por impartición de curso " + curso.comision2 + " %.",
                            Proveedor         = db.Usuario.Where(a => a.UsuarioId == curso.instructorId2).Select(b => b.Nombre + " " + b.Paterno + " " + b.Materno).FirstOrDefault(),
                            SubTotal          = (curso.comision2 * pagoParticipantes) / 100,
                            Iva   = 0,
                            Total = (curso.comision2 * pagoParticipantes) / 100
                        });
                    }

                    var gastos = db.CursoGastoDetalle.Where(a => a.CursoId == curso.cursoId && a.SubConceptoId != 3).ToList();

                    gastos.ForEach(n =>
                    {
                        consecutivoGastos += 1;
                        cursoGastoDetalle.Add(new Models.CursoGastoDetalle
                        {
                            CursoId           = n.CursoId,
                            ConsecutivoId     = consecutivoGastos,
                            UsuarioId         = n.UsuarioId,
                            ComprobanteTipoId = n.ComprobanteTipoId,
                            Fecha             = n.Fecha,
                            SubConceptoId     = n.SubConceptoId,
                            Descripcion       = n.Descripcion,
                            Proveedor         = n.Proveedor,
                            SubTotal          = n.SubTotal,
                            Iva   = n.Iva,
                            Total = n.Total
                        });
                    });
                }


                // insertar curso
                if (curso.cursoId == 0)
                {
                    db.Curso.Add(new Models.Curso
                    {
                        CentroCostoId      = curso.centroCostosId,
                        SedeId             = curso.sedeId,
                        LugarCurso         = curso.lugarCurso,
                        CursoTipoId        = curso.cursoTipoId,
                        UsuarioId1         = curso.instructorId1,
                        Comision1          = curso.comision1,
                        UsuarioId2         = curso.instructorId2,
                        Comision2          = curso.instructorId2 > 0 ? curso.comision2 : 0,
                        FechaCurso         = DateTime.ParseExact((curso.fechaCurso.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        Efectivo           = 0,
                        ChequeTans         = 0,
                        NumeroChequeTans   = "",
                        Fecha              = DateTime.Now,
                        Hora               = DateTime.Now.TimeOfDay,
                        UsuarioIdActualizo = usuarioId,
                        EstatusId          = 1,
                        CursoParticipante  = cursoParticipante,
                        CursoGastoDetalle  = cursoGastoDetalle
                    });
                }
                else
                {
                    var cursoDb = db.Curso.Where(a => a.CursoId == curso.cursoId).FirstOrDefault();
                    db.CursoParticipante.RemoveRange(cursoDb.CursoParticipante);
                    db.CursoGastoDetalle.RemoveRange(cursoDb.CursoGastoDetalle);

                    cursoDb.CentroCostoId      = curso.centroCostosId;
                    cursoDb.SedeId             = curso.sedeId;
                    cursoDb.LugarCurso         = curso.lugarCurso;
                    cursoDb.CursoTipoId        = curso.cursoTipoId;
                    cursoDb.UsuarioId1         = curso.instructorId1;
                    cursoDb.Comision1          = curso.comision1;
                    cursoDb.UsuarioId2         = curso.instructorId2;
                    cursoDb.Comision2          = curso.instructorId2 > 0 ? curso.comision2 : 0;
                    cursoDb.FechaCurso         = DateTime.ParseExact((curso.fechaCurso.Replace('-', '/')), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                    cursoDb.Fecha              = DateTime.Now;
                    cursoDb.Hora               = DateTime.Now.TimeOfDay;
                    cursoDb.UsuarioIdActualizo = usuarioId;
                    cursoDb.EstatusId          = 1;
                    db.CursoParticipante.AddRange(cursoParticipante);
                    db.CursoGastoDetalle.AddRange(cursoGastoDetalle);
                }

                db.SaveChanges();

                int cursoId = db.Curso.Local.FirstOrDefault().CursoId;

                return(Ok(new
                {
                    message = "El curso se guardó correctamente.",
                    cursoId
                }));
            }
            catch (Exception Ex)
            {
                return(BadRequest("Error al guardar curso"));
            }
        }