Esempio n. 1
0
        // GET: api/Routes/1234567890A
        public IHttpActionResult Get(int id)
        {
            try
            {
                var            deviceId    = GetDeviceId(Request);
                EmpleadoDAO    empleado    = new EmpleadoDAO();// emple //fecha
                DispositivoDAO dispositivo = new DispositivoDAO();
                var            device      = dispositivo.FindByImei(deviceId);
                var            employee    = empleado.FindEmpleadoByDevice(device);

                var trip = RouteService.GetDistributionRouteById(id);

                if (trip == null)
                {
                    return(NotFound());
                }

                var route = new Route
                {
                    Code   = trip.Codigo,
                    Id     = trip.Id,
                    Status = trip.Estado
                };

                var jobs = new List <Job>();
                foreach (var detail in trip.Detalles)
                {
                    var job = new Job();
                    job.Id        = detail.Id;
                    job.StartDate = detail.Programado.ToString("yyyy-MM-ddTHH:mm:ss");
                    job.EndDate   = detail.ProgramadoHasta.ToString("yyyy-MM-ddTHH:mm:ss");
                    job.State     = detail.Estado == 3 ? 0 : detail.Estado;
                    if (detail.PuntoEntrega != null && detail.PuntoEntrega.Descripcion != null)
                    {
                        job.Code       = detail.PuntoEntrega.Codigo;
                        job.Name       = detail.PuntoEntrega.Descripcion;
                        job.clienttype = detail.PuntoEntrega.Cliente.Descripcion;
                    }
                    else
                    {
                        if (detail.Cliente != null)
                        {
                            job.ClientName = detail.Cliente.Descripcion;
                            job.Code       = detail.Cliente.Codigo;
                        }
                    }
                    if (detail.ReferenciaGeografica != null &&
                        detail.ReferenciaGeografica.Direccion != null &&
                        detail.ReferenciaGeografica.Direccion.Descripcion != null &&
                        !String.IsNullOrEmpty(detail.ReferenciaGeografica.Direccion.Calle))
                    {
                        if (detail.ReferenciaGeografica.Direccion.Altura > 0)
                        {
                            job.direccionreal = detail.ReferenciaGeografica.Direccion.Calle.ToString() + " "
                                                + detail.ReferenciaGeografica.Direccion.Altura + " , " + detail.ReferenciaGeografica.Direccion.Partido;
                        }
                        else
                        {
                            job.direccionreal = detail.ReferenciaGeografica.Direccion.Descripcion;
                        }
                    }
                    job.Description = detail.Descripcion;

                    job.Order    = detail.Orden;
                    job.Location = new Location();

                    job.Volumen  = (float)detail.Volumen;
                    job.Quantity = detail.Bultos;
                    job.Value    = (float)detail.Valor;
                    job.Weight   = (float)detail.Peso;

                    if (detail.PuntoEntrega != null)
                    {
                        job.Location.Latitude  = (float)detail.PuntoEntrega.ReferenciaGeografica.Latitude;
                        job.Location.Longitude = (float)detail.PuntoEntrega.ReferenciaGeografica.Longitude;
                    }
                    else
                    {
                        job.Location.Latitude  = (float)detail.Linea.ReferenciaGeografica.Latitude;
                        job.Location.Longitude = (float)detail.Linea.ReferenciaGeografica.Longitude;
                    }

                    if (detail.PuntoEntrega != null)
                    {
                        jobs.Add(job);
                    }
                }
                route.Jobs = jobs.ToArray();

                if (trip.Recepcion == null)
                {
                    ViajeDistribucionDAO vd = new ViajeDistribucionDAO();
                    trip.Recepcion = DateTime.UtcNow;
                    if (trip.Empleado == null)
                    {
                        trip.Empleado = employee;
                    }
                    vd.SaveOrUpdate(trip);
                }

                return(Ok(route));
            }
            catch (Exception error)
            {
                LogicTracker.App.Web.Api.Providers.LogWritter.writeLog(error);
                return(BadRequest());
            }
        }
Esempio n. 2
0
        public XmlDocument ObtenerEstadoEntrega(String Distrito, String Base, String Entrega, String Cliente)
        {
            var address = HttpContext.Current.Request.UserHostAddress;

            try
            {
                using (StreamWriter sw = File.AppendText(Directory.GetCurrentDirectory() + @"\LOGIP.txt"))
                {
                    sw.WriteLine(address.ToString() + "\t" + DateTime.Now.ToString("ddMMyyyyHHmmssfff"));
                }
            }
            catch (Exception)
            {
            }

            if (!String.IsNullOrEmpty(Distrito) &&
                !String.IsNullOrEmpty(Base) &&
                (!String.IsNullOrEmpty(Entrega) ||
                 !String.IsNullOrEmpty(Cliente)))
            {
                var EstadoActual            = "";
                var TiempoLlegada           = "";
                var DistanciakmtsLlegada    = "";
                var linkGoogleMaps          = "https://www.google.com.ar/maps/dir/-34.598198,-58.3841437/-34.5972089,-58.3780497";
                ViajeDistribucionDAO dao    = new ViajeDistribucionDAO();
                LogPosicionDAO       daopos = new LogPosicionDAO();
                var sinbase = dao.FindAll().Where(x => x.Inicio.Day.Equals(DateTime.Now.Day) &&
                                                  x.Inicio.Month.Equals(DateTime.Now.Month) &&
                                                  x.Inicio.Year.Equals(DateTime.Now.Year) && x.Empresa.RazonSocial.ToString().Trim().ToUpper().Contains(Distrito.Trim().ToUpper()));
                if (!String.IsNullOrEmpty(Base))
                {
                    sinbase = sinbase.Where(x => x.Linea == null ||
                                            x.Linea != null &&
                                            x.Linea.Descripcion.ToString().Trim().ToUpper().Contains(Base.Trim().ToUpper()));
                }
                bool closeLoop = false;
                foreach (var item in sinbase)
                {
                    foreach (EntregaDistribucion detalle in item.Detalles.Where(x => x.PuntoEntrega != null))
                    {
                        //codigo = entrega
                        //descripcion cliente
                        if (detalle.PuntoEntrega.Codigo.Trim().ToUpper().Equals(Entrega.Trim().ToUpper()) ||
                            detalle.PuntoEntrega.Descripcion.Trim().ToUpper().Equals(Cliente.Trim().ToUpper()))
                        {
                            switch (detalle.Estado)
                            {
                            case EntregaDistribucion.Estados.EnSitio:
                            {
                                EstadoActual = "EnSitio";
                                break;
                            }

                            case EntregaDistribucion.Estados.Completado:
                            {
                                EstadoActual = "Completado";
                                break;
                            }

                            case EntregaDistribucion.Estados.Cancelado:
                            {
                                EstadoActual = "Cancelado";
                                break;
                            }

                            case EntregaDistribucion.Estados.EnZona:
                            {
                                EstadoActual = "EnZona";
                                break;
                            }

                            case EntregaDistribucion.Estados.NoCompletado:
                            {
                                EstadoActual = "NoCompletado";
                                break;
                            }

                            case EntregaDistribucion.Estados.Pendiente:
                            {
                                EstadoActual = "Pendiente";
                                break;
                            }

                            case EntregaDistribucion.Estados.Restaurado:
                            {
                                EstadoActual = "Restaurado";
                                break;
                            }

                            case EntregaDistribucion.Estados.SinVisitar:
                            {
                                EstadoActual = "SinVisitar";
                                break;
                            }

                            case EntregaDistribucion.Estados.Visitado:
                            {
                                EstadoActual = "Visitado";
                                break;
                            }

                            default:
                                break;
                            }

                            LogUltimaPosicionVo ult = daopos.GetLastVehiclePosition(item.Vehiculo);
                            if (ult == null)
                            {
                                ult = daopos.GetLastOnlineVehiclePosition(item.Vehiculo);
                            }
                            LatLon origen  = new LatLon(ult.Latitud, ult.Longitud);
                            LatLon destino = new LatLon(detalle.PuntoEntrega.ReferenciaGeografica.Latitude, detalle.PuntoEntrega.ReferenciaGeografica.Longitude);

                            linkGoogleMaps = "https://www.google.com.ar/maps/dir/" + origen.Latitud.ToString().Replace(',', '.') + "," + origen.Longitud.ToString().Replace(',', '.') + "/" + destino.Latitud.ToString().Replace(',', '.') + "," + destino.Longitud.ToString().Replace(',', '.');

                            var waypoints = new List <LatLon>();
                            waypoints.Add(origen);
                            waypoints.Add(destino);
                            var directions = GoogleDirections.GetDirections(origen, destino, GoogleDirections.Modes.Driving, string.Empty, waypoints.ToArray());
                            int reintentos = 0;
                            while (directions == null && reintentos < 4)
                            {
                                reintentos++;
                                Thread.Sleep(2000);
                                directions = GoogleDirections.GetDirections(origen, destino, GoogleDirections.Modes.Driving, string.Empty, waypoints.ToArray());
                            }
                            if (directions != null)
                            {
                                TiempoLlegada        = new DateTime(directions.Duration.Ticks).ToString("HH:mm:ss");
                                DistanciakmtsLlegada = (directions.Distance / 1000).ToString();
                            }
                            closeLoop = true;
                            break;
                        }
                    }
                    if (closeLoop)
                    {
                        break;
                    }
                }
                XmlDocument    doc            = new XmlDocument();
                XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                XmlElement     root           = doc.DocumentElement;
                doc.InsertBefore(xmlDeclaration, root);

                //(2) string.Empty makes cleaner code
                XmlElement element1 = doc.CreateElement(string.Empty, "body", string.Empty);
                doc.AppendChild(element1);

                XmlElement element2 = doc.CreateElement(string.Empty, "EstadoActual", string.Empty);
                XmlText    text1    = doc.CreateTextNode(EstadoActual);
                element2.AppendChild(text1);
                element1.AppendChild(element2);

                XmlElement element3 = doc.CreateElement(string.Empty, "TiempoLlegadaHHMMSS", string.Empty);
                XmlText    text2    = doc.CreateTextNode(TiempoLlegada);
                element3.AppendChild(text2);
                element1.AppendChild(element3);

                XmlElement element4 = doc.CreateElement(string.Empty, "DistanciakmtsLlegada", string.Empty);
                XmlText    text3    = doc.CreateTextNode(DistanciakmtsLlegada);
                element4.AppendChild(text3);
                element1.AppendChild(element4);

                XmlElement element5 = doc.CreateElement(string.Empty, "linkGoogleMaps", string.Empty);
                XmlText    text4    = doc.CreateTextNode(linkGoogleMaps);
                element5.AppendChild(text4);
                element1.AppendChild(element5);

                return(doc);
            }
            else
            {
                return(new XmlDocument());
            }
        }