static public List <OfficialInspectorClass> Longwork4(IProgress <string> progress, SalesforceClient client, List <OfficialInspectorClass> workingList, List <String> autoqueue)
        {
            InspectionJSONClass currentInspection = new InspectionJSONClass();
            List <String>       assignedarray     = new List <String>();
            List <String>       skippedarray      = new List <String>();
            string inspectorAssign  = "";
            string inspectorAssign1 = "";

            //List<OfficialInspectorClass> tempList = new List<OfficialInspectorClass>();
            //List<OfficialInspectorClass> templist2 = new List<OfficialInspectorClass>();
            for (int i = 0; i < autoqueue.Count; i++)
            {
                List <OfficialInspectorClass> tempList  = new List <OfficialInspectorClass>();
                List <OfficialInspectorClass> templist2 = new List <OfficialInspectorClass>();
                currentInspection = findInspectionbyOrderNumber(autoqueue[i].Substring(0, 6), client);

                if (currentInspection.Auto_Assign_Skip__c == false)
                {
                    List <string> historyList = new List <string>();
                    inspectorAssign  = "";
                    inspectorAssign1 = "";

                    workingList = sortByDistance(client, workingList, currentInspection);
                    for (int j = 0; j < workingList.Count; j++)
                    {
                        if (workingList[j].currentDistance <= 500)
                        {
                            tempList.Add(workingList[j]);
                        }
                    }
                    //Console.WriteLine(tempList.Count);
                    //Console.ReadLine();
                    var history = client.Query <HistoryClass>("SELECT CreatedDate, Field, OldValue, NewValue From Inspection__History WHERE ParentId='" + currentInspection.Id + "' AND Field='Rep_ID_Inspector_history_tracking__c'");
                    for (int j = 0; j < history.Count; j++)
                    {
                        if (history[j].NewValue.Equals("-") && history[j].OldValue != null)
                        {
                            historyList.Add(history[j].OldValue);
                        }
                    }
                    for (int j = 0; j < tempList.Count; j++)
                    {
                        String compareString = tempList[j].Rep_ID__c + " - " + tempList[j].Name;
                        if (!historyList.Contains(compareString) && tempList[j].Status__c != "On Hold")
                        {
                            templist2.Add(tempList[j]);
                        }
                    }
                    tempList  = templist2;
                    templist2 = new List <OfficialInspectorClass>();
                    if (tempList.Count > 0)
                    {
                        inspectorAssign1 = tempList[0].contactID;
                        inspectorAssign  = tempList[0].Name;
                        updateInspectorCount(tempList[0].contactID, workingList);
                        UpdateInspectorClass updateInspector = new UpdateInspectorClass();
                        updateInspector.Inspector__c = inspectorAssign1;
                        assignedarray.Add((currentInspection.Name + ": " + inspectorAssign));
                        //updateInspectorCount(tempList[0].contactID);
                        client.Update("Inspection__c", currentInspection.Id, updateInspector);
                    }
                    else
                    {
                        skippedarray.Add((currentInspection.Name + ": Skipped"));
                        UpdateAdhocClass repad = new UpdateAdhocClass();
                        if (currentInspection.ADHOC__c == null)
                        {
                            currentInspection.ADHOC__c = "";
                        }
                        if (!currentInspection.ADHOC__c.Contains("Rep Needed"))
                        {
                            repad.ADHOC__c = "Rep Needed " + currentInspection.ADHOC__c;
                            client.Update("Inspection__c", currentInspection.Id, repad);
                        }
                    }
                }
                progress.Report("Orders Assigned: " + i + " of " + autoqueue.Count);
            }
            System.IO.File.WriteAllLines(@"C:\Users\Public\S2 Inspections\HUD Assigned.txt", assignedarray);
            System.IO.File.WriteAllLines(@"C:\Users\Public\S2 Inspections\HUD Skipped.txt", skippedarray);
            return(workingList);
        }
        public static List <OfficialInspectorClass> Longwork3(IProgress <string> progress, SalesforceClient client, List <OfficialInspectorClass> workingList, List <String> autoqueue)
        {
            InspectionJSONClass currentInspection = new InspectionJSONClass();
            List <String>       assignedarray     = new List <String>();
            List <String>       skippedarray      = new List <String>();
            string inspectorAssign  = "";
            string inspectorAssign1 = "";
            List <OfficialInspectorClass> tempList  = new List <OfficialInspectorClass>();
            List <OfficialInspectorClass> templist2 = new List <OfficialInspectorClass>();

            //SortNumber = 0;
            //List<String> autoqueue = sortAssignQueue();
            //these two lines can test the logic for a specific order
            //List<String> autoqueue = new List<string>();
            //autoqueue.Add("163707");
            for (int i = 0; i < autoqueue.Count; i++)
            {
                tempList  = new List <OfficialInspectorClass>();
                templist2 = new List <OfficialInspectorClass>();
                String searchnumber = autoqueue[i].Substring(0, 6);
                currentInspection = findInspectionbyOrderNumber(searchnumber, client);
                if (currentInspection.ADHOC__c == null)
                {
                    currentInspection.ADHOC__c = "";
                }
                if (currentInspection.Auto_Assign_Skip__c == false)
                {
                    List <string> historyList = new List <string>();
                    inspectorAssign  = "";
                    inspectorAssign1 = "";

                    workingList = sortByDistance(client, workingList, currentInspection);
                    for (int j = 0; j < workingList.Count; j++)
                    {
                        if (workingList[j].currentDistance <= Convert.ToInt32(workingList[j].Coverage_Area_Radius__c) && workingList[j].assignedInspections < workingList[j].Max_Insp_Count__c)
                        {
                            tempList.Add(workingList[j]);
                        }
                    }
                    //Console.WriteLine(tempList.Count);
                    //Console.ReadLine();
                    var history = client.Query <HistoryClass>("SELECT CreatedDate, Field, OldValue, NewValue From Inspection__History WHERE ParentId='" + currentInspection.Id + "' AND Field='Rep_ID_Inspector_history_tracking__c'");
                    for (int j = 0; j < history.Count; j++)
                    {
                        if (history[j].NewValue.Equals("-") && history[j].OldValue != null)
                        {
                            historyList.Add(history[j].OldValue);
                        }
                    }
                    for (int j = 0; j < tempList.Count; j++)
                    {
                        String compareString = tempList[j].Rep_ID__c + " - " + tempList[j].Name;
                        if (tempList[j].Blacklist__c == null)
                        {
                            tempList[j].Blacklist__c = "";
                        }
                        if (!historyList.Contains(compareString) && tempList[j].Status__c != "On Hold" && tempList[j].feeDictionary[currentInspection.Fee_Type_Text__c] != null && !tempList[j].Blacklist__c.Contains(currentInspection.Division__c))
                        {
                            templist2.Add(tempList[j]);
                        }
                    }
                    tempList  = templist2;
                    templist2 = new List <OfficialInspectorClass>();
                    if (tempList.Count > 0)
                    {
                        if (tempList.Count > 1)
                        {
                            for (int j = 0; j < tempList.Count; j++)
                            {
                                if (tempList[j].Status__c == "New Rep")
                                {
                                    templist2.Add(tempList[j]);
                                }
                            }
                            if (templist2.Count == 0)
                            {
                                //TOP REP LOGIC GOES HERE!!!
                                templist2 = new List <OfficialInspectorClass>();
                                for (int j = 0; j < tempList.Count; j++)
                                {
                                    if (tempList[j].Status__c == "Top Rep")
                                    {
                                        templist2.Add(tempList[j]);
                                    }
                                }
                                if (templist2.Count == 0)
                                {
                                    tempList.Sort((x, y) => y.Inspector_Ranking__c.CompareTo(x.Inspector_Ranking__c));
                                    if (tempList[0].Inspector_Ranking__c == tempList[1].Inspector_Ranking__c)
                                    {
                                        if (tempList[0].feeDictionary[currentInspection.Fee_Type_Text__c] < tempList[1].feeDictionary[currentInspection.Fee_Type_Text__c])
                                        {
                                            inspectorAssign1 = tempList[0].contactID;
                                            inspectorAssign  = tempList[0].Name;
                                            updateInspectorCount(tempList[0].contactID, workingList);
                                        }
                                        else if (tempList[0].feeDictionary[currentInspection.Fee_Type_Text__c] > tempList[1].feeDictionary[currentInspection.Fee_Type_Text__c])
                                        {
                                            inspectorAssign1 = tempList[1].contactID;
                                            inspectorAssign  = tempList[1].Name;
                                            updateInspectorCount(tempList[1].contactID, workingList);
                                        }
                                        else
                                        {
                                            if (tempList[0].currentDistance < tempList[1].currentDistance)
                                            {
                                                inspectorAssign1 = tempList[0].contactID;
                                                inspectorAssign  = tempList[0].Name;
                                                updateInspectorCount(tempList[0].contactID, workingList);
                                            }
                                            else if (tempList[0].currentDistance > tempList[1].currentDistance)
                                            {
                                                inspectorAssign1 = tempList[1].contactID;
                                                inspectorAssign  = tempList[1].Name;
                                                updateInspectorCount(tempList[1].contactID, workingList);
                                            }
                                            else
                                            {
                                                inspectorAssign1 = tempList[0].contactID;
                                                inspectorAssign  = tempList[0].Name;
                                                updateInspectorCount(tempList[0].contactID, workingList);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        inspectorAssign1 = tempList[0].contactID;
                                        inspectorAssign  = tempList[0].Name;
                                        updateInspectorCount(tempList[0].contactID, workingList);
                                    }
                                }
                                else if (templist2.Count == 1)
                                {
                                    inspectorAssign1 = templist2[0].contactID;
                                    inspectorAssign  = templist2[0].Name;
                                    updateInspectorCount(templist2[0].contactID, workingList);
                                }
                                else
                                {
                                    tempList = templist2;
                                    tempList.Sort((x, y) => x.Inspector_Ranking__c.CompareTo(y.Inspector_Ranking__c));
                                    if (tempList[0].Inspector_Ranking__c == tempList[1].Inspector_Ranking__c)
                                    {
                                        if (tempList[0].feeDictionary[currentInspection.Fee_Type_Text__c] < tempList[1].feeDictionary[currentInspection.Fee_Type_Text__c])
                                        {
                                            inspectorAssign1 = tempList[0].contactID;
                                            inspectorAssign  = tempList[0].Name;
                                            updateInspectorCount(tempList[0].contactID, workingList);
                                        }
                                        else if (tempList[0].feeDictionary[currentInspection.Fee_Type_Text__c] > tempList[1].feeDictionary[currentInspection.Fee_Type_Text__c])
                                        {
                                            inspectorAssign1 = tempList[1].contactID;
                                            inspectorAssign  = tempList[1].Name;
                                            updateInspectorCount(tempList[1].contactID, workingList);
                                        }
                                        else
                                        {
                                            if (tempList[0].currentDistance < tempList[1].currentDistance)
                                            {
                                                inspectorAssign1 = tempList[0].contactID;
                                                inspectorAssign  = tempList[0].Name;
                                                updateInspectorCount(tempList[0].contactID, workingList);
                                            }
                                            else if (tempList[0].currentDistance > tempList[1].currentDistance)
                                            {
                                                inspectorAssign1 = tempList[1].contactID;
                                                inspectorAssign  = tempList[1].Name;
                                                updateInspectorCount(tempList[1].contactID, workingList);
                                            }
                                            else
                                            {
                                                inspectorAssign1 = tempList[0].contactID;
                                                inspectorAssign  = tempList[0].Name;
                                                updateInspectorCount(tempList[0].contactID, workingList);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        inspectorAssign1 = tempList[0].contactID;
                                        inspectorAssign  = tempList[0].Name;
                                        updateInspectorCount(tempList[0].contactID, workingList);
                                    }
                                }
                            }
                            else if (templist2.Count == 1)
                            {
                                inspectorAssign1 = templist2[0].contactID;
                                inspectorAssign  = templist2[0].Name;
                                updateInspectorCount(templist2[0].contactID, workingList);
                            }
                            else
                            {
                                //NEW REP MULTIPLE GOES HERE
                                tempList = templist2;
                                tempList.Sort((x, y) => x.Inspector_Ranking__c.CompareTo(y.Inspector_Ranking__c));
                                if (tempList[0].Inspector_Ranking__c == tempList[1].Inspector_Ranking__c)
                                {
                                    if (tempList[0].feeDictionary[currentInspection.Fee_Type_Text__c] < tempList[1].feeDictionary[currentInspection.Fee_Type_Text__c])
                                    {
                                        inspectorAssign1 = tempList[0].contactID;
                                        inspectorAssign  = tempList[0].Name;
                                        updateInspectorCount(tempList[0].contactID, workingList);
                                    }
                                    else if (tempList[0].feeDictionary[currentInspection.Fee_Type_Text__c] > tempList[1].feeDictionary[currentInspection.Fee_Type_Text__c])
                                    {
                                        inspectorAssign1 = tempList[1].contactID;
                                        inspectorAssign  = tempList[1].Name;
                                        updateInspectorCount(tempList[1].contactID, workingList);
                                    }
                                    else
                                    {
                                        if (tempList[0].currentDistance < tempList[1].currentDistance)
                                        {
                                            inspectorAssign1 = tempList[0].contactID;
                                            inspectorAssign  = tempList[0].Name;
                                            updateInspectorCount(tempList[0].contactID, workingList);
                                        }
                                        else if (tempList[0].currentDistance > tempList[1].currentDistance)
                                        {
                                            inspectorAssign1 = tempList[1].contactID;
                                            inspectorAssign  = tempList[1].Name;
                                            updateInspectorCount(tempList[1].contactID, workingList);
                                        }
                                        else
                                        {
                                            inspectorAssign1 = tempList[0].contactID;
                                            inspectorAssign  = tempList[0].Name;
                                            updateInspectorCount(tempList[0].contactID, workingList);
                                        }
                                    }
                                }
                                else
                                {
                                    inspectorAssign1 = tempList[0].contactID;
                                    inspectorAssign  = tempList[0].Name;
                                    updateInspectorCount(tempList[0].contactID, workingList);
                                }
                            }
                        }
                        else
                        {
                            inspectorAssign1 = tempList[0].contactID;
                            inspectorAssign  = tempList[0].Name;
                            updateInspectorCount(tempList[0].contactID, workingList);
                        }
                        UpdateInspectorClass updateInspector = new UpdateInspectorClass();
                        updateInspector.Inspector__c = inspectorAssign1;
                        assignedarray.Add((currentInspection.Name + ": " + inspectorAssign));
                        //updateInspectorCount(tempList[0].contactID);
                        client.Update("Inspection__c", currentInspection.Id, updateInspector);
                    }
                    else
                    {
                        skippedarray.Add((currentInspection.Name + ": Skipped"));
                        UpdateAdhocClass repad = new UpdateAdhocClass();
                        if (currentInspection.ADHOC__c == null)
                        {
                            currentInspection.ADHOC__c = "";
                        }
                        if (!currentInspection.ADHOC__c.Contains("Rep Needed"))
                        {
                            repad.ADHOC__c = "Rep Needed " + currentInspection.ADHOC__c;
                            client.Update("Inspection__c", currentInspection.Id, repad);
                        }
                        //currentInspection.ADHOC__c = "Rep Needed " + currentInspection.ADHOC__c; this code isn't even correct, make sure to fix
                    }
                }
                //put progress here, end of for loop
                progress.Report("Orders Assigned: " + i + " of " + autoqueue.Count);
            }
            System.IO.File.WriteAllLines(@"C:\Users\Public\S2 Inspections\Assigned.txt", assignedarray);
            System.IO.File.WriteAllLines(@"C:\Users\Public\S2 Inspections\Skipped.txt", skippedarray);
            return(workingList);
        }
        private static List <OfficialInspectorClass> sortByDistance(SalesforceClient client, List <OfficialInspectorClass> workingList, InspectionJSONClass currentInspection)
        {
            String        mapKey = "On5gRfRDnoozDkk8zKjo5GpXGbvYCycm";
            GeoCoordinate currentGeopoint;

            if (currentInspection.Property_Latitude__c == null)
            {
                String currentInspectionAddress;
                String JsonReturn = ("");
                LatLngClass.RootObject coordinates = new LatLngClass.RootObject();
                //var array;
                if (!currentInspection.Street_Address__c.Contains(","))
                {
                    String modifiedstreet = currentInspection.Street_Address__c.Replace('&', '-');
                    currentInspectionAddress = (modifiedstreet + ", " + currentInspection.City__c + ", " + currentInspection.State__c);
                }
                else
                {
                    currentInspectionAddress = (currentInspection.City__c + ", " + currentInspection.State__c);
                }
                JsonReturn = ("");
                using (var client1 = new HttpClient())
                {
                    String restRequest = ("http://www.mapquestapi.com/geocoding/v1/address?key="
                                          + mapKey + "&location=" + currentInspectionAddress);
                    var request = new HttpRequestMessage(HttpMethod.Get, restRequest);
                    request.Headers.Add("X-PrettyPrint", "1");
                    var response = client1.SendAsync(request).Result;
                    JsonReturn = response.Content.ReadAsStringAsync().Result;
                }
                coordinates = new LatLngClass.RootObject();
                coordinates = JsonConvert.DeserializeObject <LatLngClass.RootObject>(JsonReturn);
                var array  = coordinates.results.ToArray();
                var array2 = array[0].locations.ToArray();
                if (array2.Length == 1)
                {
                }
                else
                {
                    currentInspectionAddress = (currentInspection.City__c + ", " + currentInspection.State__c + ", " + currentInspection.Zip_Code__c);
                    JsonReturn = ("");
                    using (var client1 = new HttpClient())
                    {
                        String restRequest = ("http://www.mapquestapi.com/geocoding/v1/address?key="
                                              + mapKey + "&location=" + currentInspectionAddress);
                        var request = new HttpRequestMessage(HttpMethod.Get, restRequest);
                        request.Headers.Add("X-PrettyPrint", "1");
                        var response = client1.SendAsync(request).Result;
                        JsonReturn = response.Content.ReadAsStringAsync().Result;
                    }
                    coordinates = new LatLngClass.RootObject();
                    coordinates = JsonConvert.DeserializeObject <LatLngClass.RootObject>(JsonReturn);
                    array       = coordinates.results.ToArray();
                    array2      = array[0].locations.ToArray();
                    if (array2.Length == 1)
                    {
                    }
                    else
                    {
                        currentInspectionAddress = (currentInspection.City__c + ", " + currentInspection.State__c);
                        JsonReturn = ("");
                        using (var client1 = new HttpClient())
                        {
                            String restRequest = ("http://www.mapquestapi.com/geocoding/v1/address?key="
                                                  + mapKey + "&location=" + currentInspectionAddress);
                            var request = new HttpRequestMessage(HttpMethod.Get, restRequest);
                            request.Headers.Add("X-PrettyPrint", "1");
                            var response = client1.SendAsync(request).Result;
                            JsonReturn = response.Content.ReadAsStringAsync().Result;
                        }
                        coordinates = new LatLngClass.RootObject();
                        coordinates = JsonConvert.DeserializeObject <LatLngClass.RootObject>(JsonReturn);
                        array       = coordinates.results.ToArray();
                        array2      = array[0].locations.ToArray();
                        if (array2.Length == 1)
                        {
                        }
                        else
                        {
                            currentInspectionAddress = (currentInspection.Zip_Code__c);
                            JsonReturn = ("");
                            using (var client1 = new HttpClient())
                            {
                                String restRequest = ("http://www.mapquestapi.com/geocoding/v1/address?key="
                                                      + mapKey + "&location=" + currentInspectionAddress);
                                var request = new HttpRequestMessage(HttpMethod.Get, restRequest);
                                request.Headers.Add("X-PrettyPrint", "1");
                                var response = client1.SendAsync(request).Result;
                                JsonReturn = response.Content.ReadAsStringAsync().Result;
                            }
                            coordinates = new LatLngClass.RootObject();
                            coordinates = JsonConvert.DeserializeObject <LatLngClass.RootObject>(JsonReturn);
                            array       = coordinates.results.ToArray();
                            array2      = array[0].locations.ToArray();
                        }
                    }
                }
                double currentInspectionLatitude  = array2[0].displayLatLng.lat;
                double currentInspectionLongitute = array2[0].displayLatLng.lng;
                string savelat = currentInspectionLatitude.ToString();
                string savelon = currentInspectionLongitute.ToString();
                currentGeopoint = new GeoCoordinate(currentInspectionLatitude, currentInspectionLongitute);
                SaveCoordinatesClass updateCoordinates = new SaveCoordinatesClass();
                updateCoordinates.Property_Latitude__c  = savelat;
                updateCoordinates.Property_Longitude__c = savelon;
                client.Update("Inspection__c", currentInspection.Id, updateCoordinates);
            }
            else
            {
                double currentInspectionLatitude  = Convert.ToDouble(currentInspection.Property_Latitude__c);
                double currentInspectionLongitute = Convert.ToDouble(currentInspection.Property_Longitude__c);
                currentGeopoint = new GeoCoordinate(currentInspectionLatitude, currentInspectionLongitute);
            }
            for (int i = 0; i < workingList.Count; i++)
            {
                GeoCoordinate compareVal = new GeoCoordinate(workingList[i].latitude.GetValueOrDefault(), workingList[i].longitute.GetValueOrDefault());
                workingList[i].currentDistance = (0.00062137 * currentGeopoint.GetDistanceTo(compareVal));
            }
            workingList.Sort((x, y) => x.currentDistance.CompareTo(y.currentDistance));
            return(workingList);
        }
Beispiel #4
0
        public WebWindow(List <OfficialInspectorClass> inspectors, List <InspectionMapItem> assign, List <InspectionMapItem> with, List <InspectionMapItem> val, List <InspectionMapItem> accept, InspectionJSONClass currentInspection)
        {
            InitializeComponent();

            string curDir = Directory.GetCurrentDirectory();
            Uri    MapUrl = new Uri(String.Format("file:///{0}/Leaflet/testleaflet.html", curDir));

            Mapbrowser.Navigate(MapUrl);
            Object[] test = { 51.505, -0.09 };
            Mapbrowser.LoadCompleted += webb_LoadCompleted;
            void webb_LoadCompleted(object sender, NavigationEventArgs e)
            {
                for (int i = 0; i < inspectors.Count; i++)
                {
                    Mapbrowser.InvokeScript("mapInspectors", new Object[] { inspectors[i].latitude, inspectors[i].longitute, inspectors[i].Name, inspectors[i].Inspector_Ranking__c });
                }
                for (int i = 0; i < assign.Count; i++)
                {
                    if (currentInspection.Name == assign[i].Name)
                    {
                    }
                    else
                    {
                        Mapbrowser.InvokeScript("mapAssign", new Object[] { Convert.ToDouble(assign[i].Property_Latitude__c), Convert.ToDouble(assign[i].Property_Longitude__c), assign[i].Name });
                    }
                }
                for (int i = 0; i < with.Count; i++)
                {
                    Mapbrowser.InvokeScript("mapWith", new Object[] { Convert.ToDouble(with[i].Property_Latitude__c), Convert.ToDouble(with[i].Property_Longitude__c), with[i].Name, with[i].Rep_ID_Inspector_Formula__c });
                }
                for (int i = 0; i < val.Count; i++)
                {
                    Mapbrowser.InvokeScript("mapVal", new Object[] { Convert.ToDouble(val[i].Property_Latitude__c), Convert.ToDouble(val[i].Property_Longitude__c), val[i].Name });
                }
                for (int i = 0; i < accept.Count; i++)
                {
                    Mapbrowser.InvokeScript("mapAccept", new Object[] { Convert.ToDouble(accept[i].Property_Latitude__c), Convert.ToDouble(accept[i].Property_Longitude__c), accept[i].Name, accept[i].Rep_ID_Inspector_Formula__c });
                }
                Mapbrowser.InvokeScript("mapcurrent", new Object[] { currentInspection.Property_Latitude__c, currentInspection.Property_Longitude__c, currentInspection.Name });
                Closed += MainWindow.MapClose;
            }
        }