private int?GetPointZoneId(List <Gen_Zone_PolyVertice> listofVertices, string postCode, int?selectedZoneId)
        {
            Gen_Coordinate objCoord = listOfCoordinates.FirstOrDefault(c => c.PostCode == postCode);

            if (objCoord != null)
            {
                if (FindPoint(Convert.ToDouble(objCoord.Latitude), Convert.ToDouble(objCoord.Longitude), listofVertices))
                {
                    return(selectedZoneId);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
        public static int?GetZoneId(string address)
        {
            if (address != "AS DIRECTED" && string.IsNullOrEmpty(General.GetPostCodeMatch(address)))
            {
                return(null);
            }

            if (address.Contains(", UK"))
            {
                address = address.Remove(address.LastIndexOf(", UK"));
            }



            int?zoneId = null;

            try
            {
                if (address == "AS DIRECTED")
                {
                    zoneId = General.GetObject <Gen_Zone>(c => c.ZoneName == address).DefaultIfEmpty().Id;

                    if (zoneId == 0)
                    {
                        zoneId = null;
                    }
                }
                else
                {
                    zoneId = AppVars.listOfAddress.FirstOrDefault(c => c.AddressLine1.Contains(address.ToStr().ToUpper())).DefaultIfEmpty().ZoneId;
                    if (zoneId == null)
                    {
                        string postCode = General.GetPostCode(address);

                        Gen_Coordinate objCoord = General.GetObject <Gen_Coordinate>(c => c.PostCode == postCode);


                        if (objCoord != null)
                        {
                            double latitude = 0, longitude = 0;

                            latitude  = Convert.ToDouble(objCoord.Latitude);
                            longitude = Convert.ToDouble(objCoord.Longitude);



                            var plot = (from a in General.GetQueryable <Gen_Zone>(c => c.MinLatitude != null && (latitude >= c.MinLatitude && latitude <= c.MaxLatitude) &&
                                                                                  (longitude <= c.MaxLongitude && longitude >= c.MinLongitude))
                                        orderby a.PlotKind

                                        select a.Id).ToArray <int>();


                            if (plot.Count() > 0)
                            {
                                var list = (from p in plot
                                            join a in General.GetQueryable <Gen_Zone_PolyVertice>(null) on p equals a.ZoneId
                                            select a).ToList();



                                foreach (int plotId in plot)
                                {
                                    if (FindPoint(latitude, longitude, list.Where(c => c.ZoneId == plotId).ToList()))
                                    {
                                        zoneId = plotId;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                if (AppVars.objPolicyConfiguration.PriorityPostCodes.ToStr().Length > 0)
                                {
                                    string[] arr = AppVars.objPolicyConfiguration.PriorityPostCodes.Split(new char[] { ',' });



                                    if (objCoord.PostCode.ToStr().Contains(" ") && arr.Contains(objCoord.PostCode.Split(new char[] { ' ' })[0]))
                                    {
                                        var zone = (from a in General.GetQueryable <Gen_Zone_PolyVertice>(null).AsEnumerable()


                                                    select new
                                        {
                                            a.Gen_Zone.Id,
                                            a.Gen_Zone.ZoneName,
                                            DistanceMin = new LatLng(Convert.ToDouble(a.Latitude), Convert.ToDouble(a.Longitude)).DistanceMiles(new LatLng(Convert.ToDouble(objCoord.Latitude), Convert.ToDouble(objCoord.Longitude))),
                                        }).OrderBy(c => c.DistanceMin).FirstOrDefault();



                                        if (zone != null)
                                        {
                                            zoneId = zone.Id;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(zoneId);
        }
        private void PopulatePlottedLocations(int?SelectedZoneId)
        {
            char[] splitArr = new char[] { ' ' };

            string[] arrPriority = AppVars.objPolicyConfiguration.PriorityPostCodes.Split(new char[] { ',' });


            List <Gen_Location> priorityAddresses = null;

            if (SelectedZoneId == -1)
            {
                priorityAddresses = General.GetQueryable <Gen_Location>(C => C.ZoneId != null && C.LocationTypeId != Enums.LOCATION_TYPES.POSTCODE).OrderBy(C => C.PostCode).ToList();
            }
            else
            {
                priorityAddresses = listofLocations.Where(c => c.ZoneId == SelectedZoneId || c.ZoneId == null).OrderBy(c => c.PostCode).ToList();
            }

            List <stp_GetFullAddressesResult> PlottedAddresses = new List <stp_GetFullAddressesResult>();
            var listofVertices = listofPolyVertices.Where(c => c.ZoneId == SelectedZoneId).ToList();


            int cnter = 0;

            for (int i = 0; i < priorityAddresses.Count; i++)
            {
                try
                {
                    cnter++;

                    if (priorityAddresses[i].ZoneId.ToInt() == 0)
                    {
                        Gen_Coordinate objCoord = listOfCoordinates.FirstOrDefault(c => c.PostCode == priorityAddresses[i].PostCode);

                        if (objCoord != null)
                        {
                            if (FindPoint(Convert.ToDouble(objCoord.Latitude), Convert.ToDouble(objCoord.Longitude), listofVertices))
                            {
                                PlottedAddresses.Add(new stp_GetFullAddressesResult {
                                    AddressLine1 = priorityAddresses[i].LocationName, PostalCode = priorityAddresses[i].PostCode, ZoneId = SelectedZoneId
                                });
                            }
                        }
                    }
                    else
                    {
                        PlottedAddresses.Add(new stp_GetFullAddressesResult {
                            AddressLine1 = priorityAddresses[i].LocationName, PostalCode = priorityAddresses[i].PostCode, ZoneId = priorityAddresses[i].ZoneId
                        });
                    }
                }
                catch
                {
                }
            }



            int cnt = PlottedAddresses.Count;

            grdLocations.RowCount = cnt;


            for (int i = 0; i < cnt; i++)
            {
                grdLocations.Rows[i].Cells["Street"].Value    = PlottedAddresses[i].AddressLine1.Replace(PlottedAddresses[i].PostalCode, "").Trim();
                grdLocations.Rows[i].Cells["PostCode"].Value  = PlottedAddresses[i].PostalCode;
                grdLocations.Rows[i].Cells["Zone"].Value      = PlottedAddresses[i].ZoneId;
                grdLocations.Rows[i].Cells["OldZoneId"].Value = PlottedAddresses[i].ZoneId;
            }
        }
        private void PopulateUnPlottedLocation(string postcode)
        {
            char[] splitArr = new char[] { ' ' };



            var priorityAddresses = listofLocations.Where(c => c.PostCode.Split(splitArr)[0] == postcode && c.ZoneId == null).OrderBy(c => c.PostCode).ToList();


            List <stp_GetFullAddressesResult> unPlottedAddresses = new List <stp_GetFullAddressesResult>();



            int?zoneId = null;
            int cnter  = 0;

            for (int i = 0; i < priorityAddresses.Count; i++)
            {
                try
                {
                    cnter++;

                    zoneId = null;

                    //  string postCode = General.GetPostCode(priorityAddresses[i]..ToUpper());

                    Gen_Coordinate objCoord = listOfCoordinates.FirstOrDefault(c => c.PostCode == priorityAddresses[i].PostCode);


                    if (objCoord != null)
                    {
                        double latitude = 0, longitude = 0;

                        latitude  = Convert.ToDouble(objCoord.Latitude);
                        longitude = Convert.ToDouble(objCoord.Longitude);



                        var plot = (from a in listofZones.Where(c => c.MinLatitude != null && (latitude >= c.MinLatitude && latitude <= c.MaxLatitude) &&
                                                                (longitude <= c.MaxLongitude && longitude >= c.MinLongitude))
                                    orderby a.PlotKind

                                    select a.Id).ToArray <int>();


                        if (plot.Count() > 0)
                        {
                            var list = (from p in plot
                                        join a in listofPolyVertices on p equals a.ZoneId
                                        select a).ToList();


                            foreach (int plotId in plot)
                            {
                                if (FindPoint(latitude, longitude, list.Where(c => c.ZoneId == plotId).ToList()))
                                {
                                    zoneId = plotId;
                                    break;
                                }
                            }
                        }
                    }


                    // zoneId= General.GetZoneId(addresses[i].AddressLine1.ToStr().ToUpper());

                    if (zoneId == null)
                    {
                        unPlottedAddresses.Add(new stp_GetFullAddressesResult {
                            AddressLine1 = priorityAddresses[i].LocationName, PostalCode = priorityAddresses[i].PostCode, ZoneId = priorityAddresses[i].ZoneId
                        });
                    }
                }
                catch
                {
                }
            }



            int cnt = unPlottedAddresses.Count;

            grdLocations.RowCount = cnt;


            for (int i = 0; i < cnt; i++)
            {
                grdLocations.Rows[i].Cells["Street"].Value    = unPlottedAddresses[i].AddressLine1.Replace(unPlottedAddresses[i].PostalCode, "").Trim();
                grdLocations.Rows[i].Cells["PostCode"].Value  = unPlottedAddresses[i].PostalCode;
                grdLocations.Rows[i].Cells["Zone"].Value      = unPlottedAddresses[i].ZoneId;
                grdLocations.Rows[i].Cells["OldZoneId"].Value = unPlottedAddresses[i].ZoneId;
            }
        }