public async Task <RespuestaProcesoEventoLogistico> GuardarEventoLogistico(EventoLogistico evento)
        {
            RespuestaProcesoEventoLogistico respuesta = new RespuestaProcesoEventoLogistico();



            if (await ParametrosSistema.isOnline)
            {
                DataService.AppDataService ds = new DataService.AppDataService(ParametrosSistema.TokenUsuarioActual);
                respuesta = await ds.RealizarPeticionApi <RespuestaProcesoEventoLogistico>("Evento/GuardarEventoLogistico", DataService.TipoPeticionApi.Post, evento);

                evento.ID                  = respuesta.IdEvento;
                evento.Sincronizado        = true;
                evento.FechaSincronizacion = DateTime.Now;
                if (respuesta.ProcesadoCorrectamente == false)
                {
                    evento.Estado = "E";
                    evento.ErrorSincronizacion = respuesta.Respuesta;
                }
                else
                {
                    evento.Estado = "S";
                }
            }
            else
            {
                evento.Sincronizado = false;
                respuesta.ProcesadoCorrectamente = true;
                respuesta.Respuesta = "Se ha guardado el evento en la memoria interna del teléfono.";
            }

            //Se determina si se debe guardar el evento en la base de datos local
            EventoLogisticoDAO eventoDAO = new EventoLogisticoDAO();

            eventoDAO.GuardarEventoLogistico(evento);

            //Si es un evento de activación se debe registrar el manifiesto en el historial de activaciones
            if (evento.IdTipoEvento == (int)TipoEventoLogisticoEnum.ActivarViaje)
            {
                if (respuesta.ProcesadoCorrectamente == true)
                {
                    Transporte transporte = new Transporte();
                    HistorialActivacionManifiesto historial = new HistorialActivacionManifiesto();

                    historial.Sincronizado        = true;
                    historial.FechaSincronizacion = DateTime.Now;
                    historial.Activo            = true;
                    historial.FechaActivacion   = evento.FechaEvento;
                    historial.UsuarioActivacion = ParametrosSistema.UsuarioActual;
                    TransporteBLL transporteBLL = new TransporteBLL();
                    transporte = await transporteBLL.SeleccionarTransporte(evento.NumeroManifiesto);

                    //if (await ParametrosSistema.isOnline)
                    //{
                    //    historial.Sincronizado = true;
                    //    historial.FechaSincronizacion = DateTime.Now;

                    //    TransporteBLL transporteBLL = new TransporteBLL();
                    //    transporte = await transporteBLL.SeleccionarTransporte(evento.NumeroManifiesto);

                    //}
                    //else
                    //{
                    //    historial.Sincronizado = false;
                    //    transporte.NumeroDocConductor = Convert.ToInt32(ParametrosSistema.NumeroIdentificacionUsuarioActual);
                    //    transporte.NumeroTransporte = evento.NumeroManifiesto;
                    //}


                    //se guarda un registro en historial activación viaje
                    if (transporte != null)
                    {
                        historial.Placa = transporte.Placa;
                        historial.NumeroDocConductor = transporte.NumeroDocConductor.ToString();
                        historial.NombreRuta         = transporte.NombreRuta;
                        historial.NumeroManifiesto   = transporte.NumeroTransporte;
                        historial.ClaseTransporte    = transporte.ClaseTransporte;
                    }
                    else
                    {
                        historial.NumeroDocConductor = historial.UsuarioActivacion;
                    }
                    HistorialActivacionManifiestoDAO historialActivacionDAO = new HistorialActivacionManifiestoDAO();

                    //si en la bd local el conductor aun tiene un viaje activo, se elimina
                    HistorialActivacionManifiesto historialAnterior = historialActivacionDAO.SeleccionarHistorialManifiestoActivoPorConductor(evento.NumeroDocumentoConductor);
                    if (historialAnterior != null)
                    {
                        historialActivacionDAO.EliminarHistorialActivacionManifiesto(historialAnterior.NumeroManifiesto);
                    }

                    //se fuarda el registro de activacion viaje
                    historialActivacionDAO.GuardarHistorialActivacionManifiesto(historial);

                    //Se guardan las entregas del transporte
                    if (transporte != null && transporte.Entregas != null && transporte.Entregas.Count > 0)
                    {
                        EntregaBLL entregaBLL = new EntregaBLL();
                        foreach (Entrega entrega in transporte.Entregas)
                        {
                            entregaBLL.GuardarEntrega(entrega);
                        }
                    }
                }
                else if (evento.Sincronizado == true && evento.Estado == "E")
                {
                    //Si el evento se sincronizó pero devolvió error, se elimina el manifiesto del historial de activacion
                    HistorialActivacionManifiestoDAO historialActivacionDAO = new HistorialActivacionManifiestoDAO();
                    historialActivacionDAO.EliminarHistorialActivacionManifiesto(evento.NumeroManifiesto);
                }
            }
            else if (evento.IdTipoEvento == (int)TipoEventoLogisticoEnum.SuspenderViaje)
            {
                HistorialActivacionManifiestoBLL historialBLL = new HistorialActivacionManifiestoBLL();
                historialBLL.EliminarHistorialActivacionManifiesto(evento.NumeroManifiesto);
            }
            else if (evento.IdTipoEvento == (int)TipoEventoLogisticoEnum.ReanudarViaje)
            {
                Transporte transporte = new Transporte();
                HistorialActivacionManifiesto historial = new HistorialActivacionManifiesto();

                historial.Sincronizado        = true;
                historial.FechaSincronizacion = DateTime.Now;
                historial.Activo            = true;
                historial.FechaActivacion   = evento.FechaEvento;
                historial.UsuarioActivacion = ParametrosSistema.UsuarioActual;
                TransporteBLL transporteBLL = new TransporteBLL();
                transporte = await transporteBLL.SeleccionarTransporte(evento.NumeroManifiesto);

                if (transporte != null)
                {
                    historial.Placa = transporte.Placa;
                    historial.NumeroDocConductor = transporte.NumeroDocConductor.ToString();
                    historial.NombreRuta         = transporte.NombreRuta;
                    historial.NumeroManifiesto   = transporte.NumeroTransporte;
                    historial.ClaseTransporte    = transporte.ClaseTransporte;
                }
                else
                {
                    historial.NumeroDocConductor = historial.UsuarioActivacion;
                }
                HistorialActivacionManifiestoDAO historialActivacionDAO = new HistorialActivacionManifiestoDAO();

                //si en la bd local el conductor aun tiene un viaje activo, se elimina
                HistorialActivacionManifiesto historialAnterior = historialActivacionDAO.SeleccionarHistorialManifiestoActivoPorConductor(evento.NumeroDocumentoConductor);
                if (historialAnterior != null)
                {
                    historialActivacionDAO.EliminarHistorialActivacionManifiesto(historialAnterior.NumeroManifiesto);
                }

                //se guarda el registro de reanudación viaje
                historialActivacionDAO.GuardarHistorialActivacionManifiesto(historial);
            }
            else if (evento.IdTipoEvento == (int)TipoEventoLogisticoEnum.FinViaje)
            {
                HistorialActivacionManifiestoDAO historialActivacionDAO = new HistorialActivacionManifiestoDAO();
                var historialActivacion = historialActivacionDAO.SeleccionarHistorialActivacionManifiesto(evento.NumeroManifiesto);
                if (historialActivacion != null)
                {
                    if (evento.Sincronizado == false || respuesta.ProcesadoCorrectamente == true)
                    {
                        //Se debe marcar como finalizado el viaje
                        historialActivacion.Activo              = false;
                        historialActivacion.FechaInactivacion   = evento.FechaEvento;
                        historialActivacion.UsuarioInactivacion = evento.NumeroDocumentoConductor;
                        historialActivacionDAO.GuardarHistorialActivacionManifiesto(historialActivacion);
                    }
                    else if (evento.Sincronizado == true && evento.Estado == "E")
                    {
                        //Si el evento se sincronizó pero devolvió error, se elimina el manifiesto del historial de activacion
                        historialActivacion.Activo              = true;
                        historialActivacion.FechaInactivacion   = null;
                        historialActivacion.UsuarioInactivacion = null;
                        historialActivacionDAO.GuardarHistorialActivacionManifiesto(historialActivacion);
                    }
                }
            }
            return(respuesta);
        }
        private List <TipoEventoLogistico> ValidarEventosLogisticosSucesoresPorUltimoEvento(EventoLogistico ultimoEventoRegistrado, List <EventoLogistico> eventosRegistrados, List <TipoEventoLogistico> tipoEventos, long numeroManifiesto)
        {
            List <TipoEventoLogistico> eventosSucesores = new List <TipoEventoLogistico>();

            //Si el ultimo evento registrado fue activación, valida si es un vacio para mostrar el evento inicio de viaje
            if (ultimoEventoRegistrado.IdTipoEvento == (int)TipoEventoLogisticoEnum.ActivarViaje)
            {
                HistorialActivacionManifiestoDAO historialActivacionDAO = new HistorialActivacionManifiestoDAO();
                HistorialActivacionManifiesto    transporte             = historialActivacionDAO.SeleccionarHistorialActivacionManifiesto(ultimoEventoRegistrado.NumeroManifiesto);
                if (transporte != null)
                {
                    if (transporte.ClaseTransporte == "ZT17" || transporte.ClaseTransporte == "ZT18")
                    {
                        eventosSucesores = tipoEventos.Where(t => t.CodigoEvento == (int)TipoEventoLogisticoEnum.IniciaViaje ||
                                                             t.CodigoEvento == (int)TipoEventoLogisticoEnum.Otro).ToList();
                        return(eventosSucesores);
                    }
                }
            }

            //si tiene inicios de cargue sin fin cargue, muestra solo fin cargue
            if (ultimoEventoRegistrado.IdTipoEvento == (int)TipoEventoLogisticoEnum.IniciaCargue)
            {
                eventosSucesores = tipoEventos.Where(t => t.CodigoEvento == (int)TipoEventoLogisticoEnum.FinCargue ||
                                                     t.CodigoEvento == (int)TipoEventoLogisticoEnum.Otro).ToList();
                return(eventosSucesores);
            }

            //si valida tiene mas entregas pendientes por registrarle datos de cargue
            EntregaDAO     entregaDAO         = new EntregaDAO();
            List <Entrega> entregasManifiesto = entregaDAO.SeleccionarEntregasPorTransporte(numeroManifiesto);

            if (ultimoEventoRegistrado.IdTipoEvento == (int)TipoEventoLogisticoEnum.FinCargue)
            {
                int entregasCargadas = 0;
                foreach (Entrega entrega in entregasManifiesto)
                {
                    var llegadaCargue = (from e in eventosRegistrados
                                         where e.IdTipoEvento == (int)TipoEventoLogisticoEnum.LlegadaCargue
                                         where e.campo1 == entrega.NumeroEntrega.ToString()
                                         select e).ToList();
                    var finCargue = (from e in eventosRegistrados
                                     where e.IdTipoEvento == (int)TipoEventoLogisticoEnum.FinCargue
                                     where e.campo1 == entrega.NumeroEntrega.ToString()
                                     select e).ToList();
                    if (llegadaCargue != null && finCargue != null)
                    {
                        entregasCargadas += 1;
                    }
                }
                if (entregasCargadas < entregasManifiesto.Count)
                {
                    eventosSucesores = tipoEventos.Where(t => t.CodigoEvento == (int)TipoEventoLogisticoEnum.LlegadaCargue ||
                                                         t.CodigoEvento == (int)TipoEventoLogisticoEnum.Otro).ToList();
                    return(eventosSucesores);
                }
            }
            //si tiene inicios de descargue sin fin descargue, muestra solo fin descargue
            if (ultimoEventoRegistrado.IdTipoEvento == (int)TipoEventoLogisticoEnum.IniciaDescargue)
            {
                eventosSucesores = tipoEventos.Where(t => t.CodigoEvento == (int)TipoEventoLogisticoEnum.FinDescargue ||
                                                     t.CodigoEvento == (int)TipoEventoLogisticoEnum.Otro).ToList();
                return(eventosSucesores);
            }
            //si valida tiene mas entregas pendientes por registrarle datos de descargue
            if (ultimoEventoRegistrado.IdTipoEvento == (int)TipoEventoLogisticoEnum.FinDescargue)
            {
                bool existenEntregasSinDescargar = false;


                int entregasDescargadasoDesegachadas = 0;

                foreach (Entrega entrega in entregasManifiesto)
                {
                    var llegadaDescargue = (from e in eventosRegistrados
                                            where e.IdTipoEvento == (int)TipoEventoLogisticoEnum.LlegadaDestino
                                            where e.campo1 == entrega.NumeroEntrega.ToString()
                                            select e).FirstOrDefault();
                    var finDescargue = (from e in eventosRegistrados
                                        where e.IdTipoEvento == (int)TipoEventoLogisticoEnum.FinDescargue
                                        where e.campo1 == entrega.NumeroEntrega.ToString()
                                        select e).FirstOrDefault();

                    if (llegadaDescargue != null && finDescargue != null)
                    {
                        entregasDescargadasoDesegachadas = entregasDescargadasoDesegachadas + 1;
                    }
                    else
                    {
                        var desenganche = (from e in eventosRegistrados
                                           where e.IdTipoEvento == (int)TipoEventoLogisticoEnum.Desenganche
                                           select e).FirstOrDefault();

                        if (desenganche != null)
                        {
                            int remesesasDesengachadas = 0;
                            if (int.TryParse(desenganche.campo1, out remesesasDesengachadas))
                            {
                                entregasDescargadasoDesegachadas = entregasDescargadasoDesegachadas + remesesasDesengachadas;
                            }
                        }
                    }
                }
                if (entregasDescargadasoDesegachadas < entregasManifiesto.Count())
                {
                    existenEntregasSinDescargar = true;
                }

                if (existenEntregasSinDescargar)
                {
                    eventosSucesores = tipoEventos.Where(t => t.CodigoEvento == (int)TipoEventoLogisticoEnum.LlegadaDestino ||
                                                         t.CodigoEvento == (int)TipoEventoLogisticoEnum.Otro).ToList();
                    return(eventosSucesores);
                }
            }
            //si tiene inicios de paradas sin continuacion viaje, muestra solo eventos que puede registrar en esta parado
            if (ultimoEventoRegistrado.IdTipoEvento == (int)TipoEventoLogisticoEnum.ParadaCamino)
            {
                eventosSucesores = tipoEventos.Where(t => t.CodigoEvento == (int)TipoEventoLogisticoEnum.ContinuacionViaje ||
                                                     t.CodigoEvento == (int)TipoEventoLogisticoEnum.Otro).ToList();
                return(eventosSucesores);
            }

            return(eventosSucesores);
        }