public List<Gen4Transaction> GetIdaLeads(DateTime StartDate, DateTime EndDate, bool? IsReported, out ActionResult actionResult)
        {
            actionResult = new ActionResult();

            Gen4Transactions = new List<Gen4Transaction>();

            Gen4LiveEntities gen4Ctx = new Gen4LiveEntities();
            if (IsReported.HasValue)
            {
                Gen4PplLedgerTransactions = (from l in gen4Ctx.Gen4PplLedger
                                             where l.Created >= StartDate && l.Created <= EndDate
                                             && l.IsReported == IsReported
                                             select l).ToList();
            }
            else
            {
                Gen4PplLedgerTransactions = (from l in gen4Ctx.Gen4PplLedger
                                             where l.Created >= StartDate && l.Created <= EndDate
                                             select l).ToList();
            }
            var CustomerLocationWebsiteIDs = (from w in gen4Ctx.Websites
                                              join l in gen4Ctx.CustomerLocations on w.LocationID equals l.LocationID
                                              join c in gen4Ctx.IdaCustomers on l.CustomerID equals c.CustomerID
                                              select new
                                              {
                                                  CustomerID = c.CustomerID,
                                                  LocationID = l.LocationID,
                                                  WebsiteID = w.WebsiteID
                                              }).Distinct().ToList();

            StringBuilder sbErrorMessages = new StringBuilder();
            foreach (var ledgerTransaction in Gen4PplLedgerTransactions)
            {
                var customer = (from c in Gen4Customers
                                where c.CustomerID == ledgerTransaction.CustomerID
                                select c).SingleOrDefault();

                if (customer == null)
                {
                    if (ExcludedCustomers.ContainsKey(ledgerTransaction.CustomerID))
                    {
                        actionResult.IsErrorPresent = true;
                        sbErrorMessages.AppendLine("TrxID: " + ledgerTransaction.TransactionID + " belongs to an excluded Customer. Customer: " + ledgerTransaction.CustomerID + " - " + ExcludedCustomers[ledgerTransaction.CustomerID]);
                        sbErrorMessages.AppendLine("  If you would like to process this Transaction remove CustomerID from Excluded Customers XML file.\r\n");
                        continue;
                    }
                }

                Gen4Transaction newLead = new Gen4Transaction();
                newLead.TransactionID = ledgerTransaction.TransactionID;
                newLead.ParentTransactionID = ledgerTransaction.ParentTransactionID;
                newLead.CustomerID = ledgerTransaction.CustomerID;
                newLead.Company = customer.CompanyName;
                newLead.CustomerName = customer.FirstName + " " + customer.LastName;
                newLead.CustomerEmail = customer.Email;
                newLead.CustomerPhone = customer.Phone;
                newLead.LeadID = ledgerTransaction.LeadID;
                newLead.LeadType = ledgerTransaction.LeadType;
                newLead.MarketID = ledgerTransaction.MarketID;
                newLead.Source = ledgerTransaction.Source;
                newLead.Description = ledgerTransaction.Description;
                newLead.TransactionType = ledgerTransaction.TransactionType;
                newLead.IsReported = ledgerTransaction.IsReported;
                newLead.Created = ledgerTransaction.Created;
                newLead.ReportStatus = ledgerTransaction.ReportStatus;

                if (ledgerTransaction.IsReported)
                {
                    newLead.Action = QuickbooksAction.None;
                    //override any errors for already reported transactions
                    newLead.IsErrorPresent = false;
                    newLead.ErrorMessage = null;
                }
                else
                {
                    newLead.Action = customer.Action;
                    newLead.IsErrorPresent = customer.IsErrorPresent;
                    newLead.ErrorMessage = customer.ErrorMessage;
                }

                Gen4Transactions.Add(newLead);
            }

            if (actionResult.IsErrorPresent)
            {
                actionResult.ErrorMessage = sbErrorMessages.ToString();
            }

            return Gen4Transactions;
        }
        private List<Gen4Customer> GenerateIdaCustomers(out ActionResult actionResult)
        {
            actionResult = new ActionResult();
            Gen4LiveEntities gen4Ctx = new Gen4LiveEntities();
            Gen4Customers = (from c in gen4Ctx.IdaCustomers
                             join b in gen4Ctx.ContactPersons on c.CustomerID equals b.EntityID
                             join f in gen4Ctx.Principals on b.PrincipalID equals f.PrincipalID
                             join d in gen4Ctx.CustomerStatuses on c.Status equals d.CustomerStatusID
                             where c.CustomerID == b.EntityID && b.EntityScope == 5
                             && f.PrincipalType == 2 && b.ContactPersonType == 0
                             select new Gen4Customer
                             {
                                 CustomerID = c.CustomerID,
                                 CompanyName = c.CompanyName,
                                 Status = d.DisplayName,
                                 FirstName = b.FirstName,
                                 LastName = b.LastName,
                                 Email = b.Email,
                                 Phone = b.Phone,
                                 City = b.City,
                                 State = b.State,
                                 PostalCode = b.PostalCode,
                                 LoginName = f.LoginName,
                                 //QuickbooksMatch = CustomerQuickbookMatch.NotFound,
                                 //Action = QuickbooksAction.None,
                                 QuickbookPaymentPlan = null,
                                 IsErrorPresent = false,
                                 ErrorMessage = null,
                             }).ToList();

            // Get the list of all Test and Play Customer Account IDs
            var excludedTestCustomersIDs = (from c in gen4Ctx.CustomerDataFlakes
                                            where c.JsonData.Contains("\"IsPlayAccount\":\"True\"")
                                            || c.JsonData.Contains("\"IsTestAccount\":\"True\"")
                                            select c.CustomerID).ToList();
            // Getting the list of Excluded Gen4 Customers - this customers will be ignore from
            // any processing.
            System.Xml.Linq.XElement a = System.Xml.Linq.XElement.Load("ExcludedGen4Customers.xml");
            var gen4ExcludedCustomers = (from x in a.Descendants("Gen4Customer")
                                         select x).ToList();
            ExcludedCustomers = new Dictionary<int, string>();
            foreach (var gen4ExcludedCustomer in gen4ExcludedCustomers)
            {
                if (gen4ExcludedCustomer.Attribute("id") != null) 
                {
                    int excludedCustomerID;
                    if (System.Int32.TryParse(gen4ExcludedCustomer.Attribute("id").Value, out excludedCustomerID))
                    {
                        string excludedCustomerName = "";
                        if (gen4ExcludedCustomer.Attribute("name") != null)
                        {
                            excludedCustomerName = gen4ExcludedCustomer.Attribute("name").Value;
                        }
                        ExcludedCustomers.Add(excludedCustomerID, excludedCustomerName);
                    }
                }
            }
            // Update Gen4 Customer's list by removing all excluded customers from it.
            for (int i = Gen4Customers.Count - 1; i >= 0; i--)
            {
                if ((from x in ExcludedCustomers
                     where x.Key == Gen4Customers[i].CustomerID
                     select x).Count() > 0 
                    || excludedTestCustomersIDs.Contains(Gen4Customers[i].CustomerID))
                {
                    var aa = Gen4Customers[i].CustomerID;
                    Gen4Customers.Remove(Gen4Customers[i]);
                }
            }

            IdaQuickbookEntities ctxQuickbook = new IdaQuickbookEntities();
            QuickbooksCustomers = (from c in ctxQuickbook.Customers
                                  select c).ToList();
            foreach (var customer in QuickbooksCustomers)
            {
                int Gen4CustomerID = 0;
                string PplPlanName = null;
                string Gen4Action = null;
 
                var xElement = System.Xml.Linq.XElement.Parse("<CustomFields>" + customer.CustomFields + "</CustomFields>");
                foreach (var b in xElement.Descendants("CustomField"))
                {
                    if (b.Element("Name").Value == "Gen4CustomerID")
                    {
                        Gen4CustomerID = Convert.ToInt32(b.Element("Value").Value);
                    }

                    if (b.Element("Name").Value == "Gen4 Action")
                    {
                        Gen4Action = b.Element("Value").Value;
                    }

                    if (b.Element("Name").Value == "PPL Plan")
                    {
                        PplPlanName = b.Element("Value").Value;
                    }
                }

                if (Gen4CustomerID != 0)
                {
                    var gen4Customer = (from c in Gen4Customers
                                        where c.CustomerID == Gen4CustomerID
                                        select c).SingleOrDefault();
                    if (gen4Customer != null)
                    {
                        gen4Customer.QuickbookCustomerID = customer.ID;
                        if (gen4Customer.CompanyName.ToLower() == customer.Company.ToLower() ||
                            gen4Customer.LastName.ToLower() == customer.LastName.ToLower()
                            || gen4Customer.FirstName.ToLower() == customer.FirstName.ToLower()
                            || gen4Customer.Phone.Replace(" ", "") == customer.Phone.Replace(" ", "")
                            || gen4Customer.Email.ToLower() == customer.Email.ToLower())
                        {
                            gen4Customer.QuickbooksMatch = CustomerQuickbookMatch.Matched;
                        }
                        else
                        {
                            gen4Customer.QuickbooksMatch = CustomerQuickbookMatch.Mismatched;
                        }

                        if (!String.IsNullOrEmpty(Gen4Action))
                        {
                            gen4Customer.QuickbooksGen4Action = Gen4Action;
                        }
                        else
                        {
                            gen4Customer.Action = QuickbooksAction.None;
                        }

                        if (!String.IsNullOrEmpty(PplPlanName))
                        {
                            gen4Customer.QuickbookPaymentPlan = PplPlanName;
                        }

                    }
                }
            }

            IdaQuickbookEntities ctxQuickbooks = new IdaQuickbookEntities();
            QuickbookItems = (from q in ctxQuickbooks.Items
                              select q).ToList();

            StringBuilder sbErrorMessages = new StringBuilder();
            sbErrorMessages.AppendLine("Following ACTIVE Gen4 Customers have errors:");

            foreach (var customer in Gen4Customers)
            {
                if (customer.Status == "Active")
                {
                    if (customer.QuickbooksMatch == CustomerQuickbookMatch.NotFound)
                    {
                        actionResult.IsErrorPresent = true;
                        sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - Not Found in Quickbooks.");
                        customer.Action = QuickbooksAction.Error;
                        customer.IsErrorPresent = true;
                        customer.ErrorMessage = "Not Found in Quickbooks";
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(customer.QuickbooksGen4Action))
                        {
                            actionResult.IsErrorPresent = true;
                            sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - Property \"Gen4 Action\" not set in Quickbooks.");
                            customer.Action = QuickbooksAction.Error;
                            customer.IsErrorPresent = true;
                            customer.ErrorMessage = "Property \"Gen4 Action\" not set in Quickbooks.";
                        }
                        else
                        {
                            switch (customer.QuickbooksGen4Action)
                            {
                                case "Ignore":
                                    customer.Action = QuickbooksAction.Ignore;
                                    break;
                                case "Import to QB":
                                    customer.Action = QuickbooksAction.Import;
                                    break;
                                case "Export to CSV":
                                    customer.Action = QuickbooksAction.Export;
                                    break;
                                default:
                                    actionResult.IsErrorPresent = true;
                                    sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - Property \"Gen4 Action\" not set in Quickbooks.");
                                    customer.Action = QuickbooksAction.Error;
                                    customer.IsErrorPresent = true;
                                    customer.ErrorMessage = "Invalid Property \"Gen4 Action\" in Quickbooks: [" + customer.QuickbooksGen4Action + "].";
                                    break;
                            }
                        }
                        //else if (customer.Action == QuickbooksAction.Error)
                        //{
                        //    actionResult.IsErrorPresent = true;
                        //    sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - Property \"Gen4 Action\" not set in Quickbooks.");
                        //    customer.IsErrorPresent = true;
                        //    customer.ErrorMessage = "Invalid Property \"Gen4 Action\" in Quickbooks.";
                        //}

                        if (customer.Action == QuickbooksAction.Import)
                        {
                            // if customer action is Import then QuickbookPaymentPlan has to be a valid QuickBooks List Item.
                            if (String.IsNullOrEmpty(customer.QuickbookPaymentPlan))
                            {
                                actionResult.IsErrorPresent = true;
                                sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - Property \"PPL Plan\" not set in Quickbooks.");
                                customer.Action = QuickbooksAction.Error;
                                customer.IsErrorPresent = true;
                                customer.ErrorMessage = "Property \"PPL Plan\" not set in Quickbooks.";
                            }
                            else if (customer.QuickbookPaymentPlan == "None")
                            {
                                actionResult.IsErrorPresent = true;
                                sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - If \"Gen4 Action\" is \"Import\" then Property \"PPL Plan\" can not be \"None\" in Quickbooks.");
                                customer.Action = QuickbooksAction.Error;
                                customer.IsErrorPresent = true;
                                customer.ErrorMessage = "If \"Gen4 Action\" is \"Import\" then Property \"PPL Plan\" can not be \"None\" in Quickbooks.";
                            }
                            else
                            {
                                var item = (from q in QuickbookItems
                                            where q.Name == customer.QuickbookPaymentPlan
                                            select q).SingleOrDefault();
                                if (item == null)
                                {
                                    actionResult.IsErrorPresent = true;
                                    sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - Property \"PPL Plan\" is set to an Invalid List Item in Quickbooks.");
                                    customer.Action = QuickbooksAction.Error;
                                    customer.IsErrorPresent = true;
                                    customer.ErrorMessage = "Property \"PPL Plan\" is not valid List Item in Quickbooks.";
                                }
                            }
                        }
                        else
                        {
                            //if customer action is NOT Import and QuickbookPaymentPlan is neither empty or "None"
                            // then QuickbookPaymentPlan has to be a valid QuickBooks List Item.
                            if (!String.IsNullOrEmpty(customer.QuickbookPaymentPlan) && customer.QuickbookPaymentPlan != "None")
                            {
                                var item = (from q in QuickbookItems
                                            where q.Name == customer.QuickbookPaymentPlan
                                            select q).SingleOrDefault();
                                if (item == null)
                                {
                                    actionResult.IsErrorPresent = true;
                                    sbErrorMessages.AppendLine(customer.CustomerID + ", " + customer.CompanyName + ", " + customer.FirstName + " " + customer.LastName + " - Property \"PPL Plan\" is set to an Invalid List Item in Quickbooks.");
                                    customer.Action = QuickbooksAction.Error;
                                    customer.IsErrorPresent = true;
                                    customer.ErrorMessage = "Property \"PPL Plan\" is not valid List Item in Quickbooks.";
                                }
                            }
                        }
                    }
                }
                else
                {
                    customer.Action = QuickbooksAction.None;
                }
            }

            //ErrorMessage = null;
            if (actionResult.IsErrorPresent)
            {
                actionResult.ErrorMessage = sbErrorMessages.ToString();
            }

            return Gen4Customers;
        }
 public List<Gen4Customer> GetIdaCustomers(out ActionResult actionResult)
 {
     this.Gen4Customers = GenerateIdaCustomers(out actionResult);
     return this.Gen4Customers;
 }
 void _backgroundWorker_DoWork_GetCustomers(object sender, DoWorkEventArgs e)
 {
     ActionResult actionResult = new ActionResult();
     try
     {
         Gen4Customers = LeadManager.GetIdaCustomers(out actionResult);
         actionResult.Result = Gen4Customers;
     }
     catch (SystemException ex)
     {
         actionResult.IsErrorPresent = true;
         actionResult.ErrorMessage = ex.ToString();
     }
     e.Result = actionResult;
 }
        void _backgroundWorker_DoWork_ProcessPayPerLeadTransactions(object sender, DoWorkEventArgs e)
        {
            ActionResult actionResult = new ActionResult();

            var gen4ImportTransactions = (from t in Gen4Transactions
                                          where t.IsReported == false && t.Action == QuickbooksAction.Import
                                          select t).ToList();

            int Counter = 1;
            int counterSuccessfullyImported = 0;
            foreach (var gen4Transaction in gen4ImportTransactions)
            {
                try
                {
                    this.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                            delegate()
                            {
                                this.textBox_ImportProgress.Text = "Importing " + Counter + " of " + gen4ImportTransactions.Count;
                            }
                            ));
                    LeadManager.ImportToQuickbooks(gen4Transaction);
                    this.WriteToLogFile(EventLogFile, DateTime.Now.ToString() + " - Imported TransactionID: " + gen4Transaction.TransactionID + " for Customer: " + gen4Transaction.CustomerID + " - " + gen4Transaction.CustomerName);
                    counterSuccessfullyImported++;
                    Counter++;
                }
                catch (System.Exception ex1)
                {
                    actionResult.IsErrorPresent = true;
                    string importErrorMessage = "Error Importing Gen4 TransactionID: " + gen4Transaction.TransactionID + ".\r\n" +
                                                "Exception Details: " + ex1.ToString() + "\r\n\r\n";
                    actionResult.ErrorMessage += importErrorMessage;
                    this.WriteToLogFile(this.ErrorLogFile, DateTime.Now.ToString() + " - " + importErrorMessage);
                }
            }

            actionResult.Message = "Imported " + counterSuccessfullyImported + " of " + gen4ImportTransactions.Count + " transactions into Quickbooks\r\n";

            var gen4ExportTransactions = (from t in Gen4Transactions
                                          where t.IsReported == false && t.Action == QuickbooksAction.Export
                                          select t).ToList();
            if (gen4ExportTransactions.Count > 0)
            {
                this.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                            delegate()
                            {
                                this.textBox_ImportProgress.Text = "Exporting Records to CSV File";
                            }
                            ));
                try
                {
                    LeadManager.ExportToCsv(gen4ExportTransactions);
                    this.WriteToLogFile(EventLogFile, DateTime.Now.ToString() + " - Exorted " + gen4ExportTransactions.Count + " transactions to CSV File.");
                    actionResult.Message += "Exorted " + gen4ExportTransactions.Count + " transactions to CSV File.";
                }
                catch (System.Exception ex2)
                {
                    actionResult.IsErrorPresent = true;
                    string exportErrorMessage = "Error Exporting Gen4 Transactions to CSV file.\r\n" +
                                                "Exception Details: " + ex2.ToString() + "\r\n\r\n";
                    actionResult.ErrorMessage += exportErrorMessage;
                    this.WriteToLogFile(this.ErrorLogFile, DateTime.Now.ToString() + " - " + exportErrorMessage);
                }
                
            }

            e.Result = actionResult;
        }
        private void _backgroundWorker_DoWork_DisplayPayPerLeadTransactions(object sender, DoWorkEventArgs e)
        {
            try
            {
                DateTime startDate = new DateTime();
                DateTime endDate = new DateTime();
                string selectedTrxReportedStatus = null;
                this.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                            delegate()
                            {
                                startDate = (DateTime)this.datePickerFrom.SelectedDate;
                                endDate = ((DateTime)this.datePickerTo.SelectedDate).AddDays(1);
                                selectedTrxReportedStatus = ((ComboBoxItem)this.comboBox_TrxReportedStatus.SelectedItem).Content.ToString();
                            }
                            ));
                if (startDate == DateTime.MinValue)
                {
                    startDate = DateTime.Now.AddMonths(-1);
                }
                if (endDate == DateTime.MinValue)
                {
                    endDate = DateTime.Now.AddDays(1);
                }
                ActionResult actionResult;

                bool? IsReported;

                
                switch (selectedTrxReportedStatus)
                {
                    case "Show Non-Reported Transactions Only":
                        IsReported = false;
                        break;
                    case "Snow Reported Transactions Only":
                        IsReported = true;
                        break;
                    case "Show All Transactions":
                        IsReported = null;
                        break;
                    default:
                        IsReported = null;
                        break;
                }

                Gen4Transactions = LeadManager.GetIdaLeads(startDate, endDate, IsReported, out actionResult);

                actionResult.Result = Gen4Transactions;
                e.Result = actionResult;
            }
            catch (System.Exception ex)
            {
                ActionResult result = new ActionResult();
                result.IsErrorPresent = true;
                result.ErrorMessage = ex.ToString();
                e.Result = result;
            }
        }