private List <Despachos.Elementos> CalcularDistanciaCorta()
        {
            string clientes   = "";
            string NoClientes = "";
            string prueba     = "";
            string Codinicio  = "";

            Distribucion.RutaCorta RutaCorta = new Distribucion.RutaCorta();
            var ru = new Distribucion.RutaCorta();

            for (int l = 0; l < Despachos.Count; l++)
            {
                if (l == 0)
                {
                    for (int k = 0; k < Despachos.Count; k++)
                    {
                        Codinicio = "13249";
                        if (!(Codinicio.Equals(Despachos[l].CustNum)))
                        {
                            clientes = clientes + "," + Despachos[k].CustNum;
                        }
                    }
                    clientes = clientes.ToString().Substring(1, clientes.Length - 1);
                    var aux = new Distribucion.RutaCorta()
                    {
                        codoridiscli = Codinicio,
                        coddesdiscli = clientes
                    };
                    Buscar(aux);
                }
                else
                {
                    Codinicio  = "";
                    clientes   = "";
                    NoClientes = "";
                    int contador = l;
                    Codinicio = lista[l - 1].coddesdiscli.ToString();

                    for (int n = 0; n < lista.Count; n++)
                    {
                        NoClientes = NoClientes + "," + lista[n].codoridiscli;
                    }
                    NoClientes = NoClientes.ToString().Substring(1, NoClientes.Length - 1);

                    for (int m = 0; m < Despachos.Count; m++)
                    {
                        clientes = clientes + "," + Despachos[m].CustNum;
                    }
                    clientes = clientes.ToString().Substring(1, clientes.Length - 1);
                    var aux2 = new Distribucion.RutaCorta()
                    {
                        codoridiscli = Codinicio,
                        coddesdiscli = clientes,
                        disdiscli    = NoClientes
                    };

                    Buscar2(aux2);
                }
            }


            int    cuenta_cliente = 0;
            string cliente        = "";

            for (int xxx = 0; xxx < lista.Count; xxx++)
            {
                int contador = 0;

                for (int yyy = 0; yyy < Despachos.Count; yyy++)
                {
                    if (lista[xxx].codoridiscli == Despachos[yyy].CustNum)
                    {
                        contador = 1;
                        cliente  = lista[xxx].codoridiscli.ToString();
                    }
                }

                if (contador == 1)
                {
                    cuenta_cliente = 0;
                    for (int aaa = 0; aaa < Despacho_ordenado.Count; aaa++)
                    {
                        if (cliente == Despacho_ordenado[aaa].CustNum)
                        {
                            cuenta_cliente = 1;
                        }
                    }

                    if (cuenta_cliente == 0)
                    {
                        var despacho = new Despachos.Elementos()
                        {
                            CustNum = cliente
                        }
                        ;
                        Despacho_ordenado.Add(despacho);
                    }
                }

                contador = 0;
                for (int zzz = 0; zzz < Despachos.Count; zzz++)
                {
                    if (lista[xxx].codoridiscli == Despachos[zzz].CustNum)
                    {
                        contador = 1;
                    }
                }
                if (contador == 1)
                {
                    cuenta_cliente = 0;
                    for (int bbb = 0; bbb < Despacho_ordenado.Count; bbb++)
                    {
                        if (cliente == Despacho_ordenado[bbb].CustNum)
                        {
                            cuenta_cliente = 1;
                        }
                    }

                    if (cuenta_cliente == 0)
                    {
                        var despacho = new Despachos.Elementos()
                        {
                            CustNum = cliente
                        }
                        ;
                        Despacho_ordenado.Add(despacho);
                    }
                }
            }


            for (int xyz = 0; xyz < Despacho_ordenado.Count; xyz++)
            {
                for (int zyx = 0; zyx < Despachos.Count; zyx++)
                {
                    if (Despacho_ordenado[xyz].CustNum == Despachos[zyx].CustNum)
                    {
                        Despacho_ordenado[xyz].ShiptoNum         = Despachos[zyx].ShiptoNum.ToString();
                        Despacho_ordenado[xyz].Latitude_c        = Despachos[zyx].Latitude_c.ToString();
                        Despacho_ordenado[xyz].Longitude_c       = Despachos[zyx].Longitude_c.ToString();
                        Despacho_ordenado[xyz].CustId            = Despachos[zyx].CustId.ToString();
                        Despacho_ordenado[xyz].Name              = Despachos[zyx].Name.ToString();
                        Despacho_ordenado[xyz].Address_c         = Despachos[zyx].Address_c.ToString();
                        Despacho_ordenado[xyz].Ubigeo            = Despachos[zyx].Ubigeo.ToString();
                        Despacho_ordenado[xyz].LegalNumber       = Despachos[zyx].LegalNumber.ToString();
                        Despacho_ordenado[xyz].PackNum           = Despachos[zyx].PackNum.ToString();
                        Despacho_ordenado[xyz].CustNum           = Despachos[zyx].CustNum.ToString();
                        Despacho_ordenado[xyz].Company           = Despachos[zyx].Company.ToString();
                        Despacho_ordenado[xyz].State             = Despachos[zyx].State.ToString();
                        Despacho_ordenado[xyz].SalesRepCode      = Despachos[zyx].SalesRepCode.ToString();
                        Despacho_ordenado[xyz].SalesRepName      = Despachos[zyx].SalesRepName.ToString();
                        Despacho_ordenado[xyz].Date              = Despachos[zyx].Date.ToString();
                        Despacho_ordenado[xyz].SalesRepName      = Despachos[zyx].SalesRepName.ToString();
                        Despacho_ordenado[xyz].OrderDispatch     = (xyz + 1).ToString();
                        Despacho_ordenado[xyz].PhoneNum          = Despachos[zyx].PhoneNum.ToString();
                        Despacho_ordenado[xyz].FechaDespacho     = Despachos[zyx].FechaDespacho.ToString();
                        Despacho_ordenado[xyz].chkEntregado_c    = Despachos[zyx].chkEntregado_c.ToString();
                        Despacho_ordenado[xyz].chkAnulado_c      = Despachos[zyx].chkAnulado_c.ToString();
                        Despacho_ordenado[xyz].chkPendiente_c    = Despachos[zyx].chkPendiente_c.ToString();
                        Despacho_ordenado[xyz].chkReprogramado_c = Despachos[zyx].chkReprogramado_c.ToString();
                        Despacho_ordenado[xyz].State             = Despachos[zyx].State.ToString();
                    }
                }
            }

            return(Despacho_ordenado);

            DataTable table = ConvertListToDataTable(lista);
            //dgvprueba.DataSource = table;
        }
        public List <Despachos.Elementos> ObtenerDataDespacho(String company, String vend, String fprog)
        {
            var           JSON = "";
            SqlCommand    cmd;
            SqlConnection cn = Conexion.cnxdistribucion();

            JavaScriptSerializer javaScripSerializer = new JavaScriptSerializer();

            try
            {
                cmd = new SqlCommand("USP_WS_Despacho_Chofer", cn);
                cmd.Parameters.AddWithValue("@SalesRepCode", vend);
                cmd.Parameters.AddWithValue("@FechaProg", fprog);
                cmd.Parameters.AddWithValue("@company", company);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Despachos.Elementos d = new Despachos.Elementos()
                    {
                        Company      = reader["Company"].ToString(),
                        SalesRepCode = reader["SalesRepCode"].ToString(),
                        Date         = reader["Date"].ToString(),
                        //Day = reader["Day"].ToString(),
                        CustNum       = reader["CustNum"].ToString(),
                        ShiptoNum     = reader["ShiptoNum"].ToString(),
                        SalesRepName  = reader["SalesRepName"].ToString(),
                        OrderDispatch = reader["OrderDispatch"].ToString(),
                        CustId        = reader["Custid"].ToString(),
                        Name          = reader["Name"].ToString(),
                        Address_c     = reader["Address_c"].ToString(),
                        PhoneNum      = reader["PhoneNum"].ToString(),
                        //ReservePriorityCode = reader["ReservePriorityCode"].ToString(),
                        //CreditLimit = reader["CreditLimit"].ToString(),
                        //Description = reader["Description"].ToString(),
                        Latitude_c    = reader["Latitude_c"].ToString(),
                        Longitude_c   = reader["Longitude_c"].ToString(),
                        FechaDespacho = reader["FechaDespacho"].ToString(),
                        //TerritoryCode_c = reader["TerritoryCode_c"].ToString(),
                        chkEntregado_c    = reader["chkEntregado_c"].ToString(),
                        chkAnulado_c      = reader["chkAnulado_c"].ToString(),
                        chkReprogramado_c = reader["chkReprogramado_c"].ToString(),
                        chkPendiente_c    = reader["chkPendiente_c"].ToString(),
                        //invoicedate = reader["invoicedate"].ToString(),
                        //Monto = reader["Monto"].ToString()
                        Ubigeo      = reader["Ubigeo"].ToString(),
                        State       = reader["State"].ToString(),
                        LegalNumber = reader["LegalNumber"].ToString(),
                        PackNum     = reader["PackNum"].ToString(),
                    };
                    Despachos.Add(d);
                }
            }


            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.Message);
                Console.WriteLine(ex.Message);
            }
            finally
            {
                cn.Close();
            }

            return(Despachos);
        }