Ejemplo n.º 1
0
        private void Task_ImportCustomers(Objsearch oQuery, Quickbooks oQuickbooks)
        {
            try
             {
            oQuery.OpenQBConnection();

            oQuery.QBXMLVersion = "5.0";
            oQuery.QueryType = ObjsearchQueryTypes.qtCustomerSearch;

            CustomerResult oCustomerResult = oQuickbooks.GetCustomers();

            List<int> oSMSIDImported = new List<int>();

            if (oCustomerResult.IsSuccessfull)
            {
               m_iTotal = oCustomerResult.Customers.Count();
               m_iCurrent = 0;

               int customerCount = 0;
               int importedCount = 0;
               int duplicateNameCount = 0;
               int errorCount = 0;

               customerCount = oCustomerResult.Customers.Count;

               foreach (var oCustomer in oCustomerResult.Customers)
               {
                  oQuery.SearchCriteria = new SearchCriteria();
                  oQuery.SearchCriteria.NameStartsWith = oCustomer.Name;
                  oQuery.Search();

                  CompanyUniqueness companyUnique = this.IsUniqueCustomerName(oCustomer.Name, oQuery.Results);

                  if (companyUnique == CompanyUniqueness.Unique)
                  {
                     // Add new customer into Quickbooks
                     Customer oQuickbooksCustomer = new Customer();
                     oQuickbooksCustomer.OpenQBConnection();

                     oQuickbooksCustomer.CustomerName = oCustomer.Name;
                     oQuickbooksCustomer.CompanyName = oCustomer.Name;
                     oQuickbooksCustomer.AltContactName = oCustomer.BillingName;
                     oQuickbooksCustomer.AltPhone = oCustomer.BillingPhone;
                     oQuickbooksCustomer.ContactName = oCustomer.Contact;
                     oQuickbooksCustomer.Phone = oCustomer.Phone;
                     oQuickbooksCustomer.Fax = oCustomer.Fax;

                     if (oCustomer.ShippingCountry == "United States")
                     {
                        oQuickbooksCustomer.ShippingAddress = string.Format("<Addr1>{0}</Addr1><Addr2>{1}</Addr2><Addr3>{2}</Addr3><City>{3}</City><State>{4}</State><PostalCode>{5}</PostalCode><Country>{6}</Country>", oCustomer.Name, oCustomer.ShippingAddressLine1, oCustomer.ShippingAddressLine2, oCustomer.ShippingCity, oCustomer.ShippingState, oCustomer.ShippingZip, oCustomer.ShippingCountry);
                     }
                     else
                     {
                        // Not the United States, ignore the state value completely
                        oQuickbooksCustomer.ShippingAddress = string.Format("<Addr1>{0}</Addr1><Addr2>{1}</Addr2><Addr3>{2}</Addr3><City>{3}</City><PostalCode>{4}</PostalCode><Country>{5}</Country>", oCustomer.Name, oCustomer.ShippingAddressLine1, oCustomer.ShippingAddressLine2, oCustomer.ShippingCity, oCustomer.ShippingZip, oCustomer.ShippingCountry);
                     }

                     if (oCustomer.BillingCountry == "United States")
                     {
                        oQuickbooksCustomer.BillingAddress = string.Format("<Addr1>{0}</Addr1><Addr2>{1}</Addr2><Addr3>{2}</Addr3><City>{3}</City><State>{4}</State><PostalCode>{5}</PostalCode><Country>{6}</Country>", oCustomer.Name, oCustomer.BillingAddressLine1, oCustomer.BillingAddressLine2, oCustomer.BillingCity, oCustomer.BillingState, oCustomer.BillingZip, oCustomer.BillingCountry);
                     }
                     else
                     {
                        // Not the United States, ignore the state value completely
                        oQuickbooksCustomer.BillingAddress = string.Format("<Addr1>{0}</Addr1><Addr2>{1}</Addr2><Addr3>{2}</Addr3><City>{3}</City><PostalCode>{4}</PostalCode><Country>{5}</Country>", oCustomer.Name, oCustomer.BillingAddressLine1, oCustomer.BillingAddressLine2, oCustomer.BillingCity, oCustomer.BillingZip, oCustomer.BillingCountry);
                     }

                     oQuickbooksCustomer.Email = oCustomer.InvoiceEmails.Replace(",", ";");
                     oQuickbooksCustomer.TermsName = "NET 30 DAYS";

                     oQuickbooksCustomer.Config(string.Format("FirstName={0}", oCustomer.Name));
                     oQuickbooksCustomer.Config("LastName=");

                     try
                     {
                        oQuickbooksCustomer.Add();

                        oQuickbooksCustomer.SetCustomField("SMSID", oCustomer.SMSID.ToString());
                        oSMSIDImported.Add(oCustomer.SMSID);
                        importedCount += 1;
                     }
                     catch (Exception oException)
                     {
                        errorCount += 1;
                        MessageBox.Show(oException.Message + System.Environment.NewLine + System.Environment.NewLine + oException.StackTrace);
                     }
                     finally
                     {
                        oQuickbooksCustomer.CloseQBConnection();
                     }
                  }
                  else if (companyUnique == CompanyUniqueness.DuplicateMarkImported)
                  {
                     oSMSIDImported.Add(oCustomer.SMSID);
                     duplicateNameCount += 1;
                  }
                  else
                  {
                     duplicateNameCount += 1;
                  }

                  this.UpdateProgress();

                  if (m_oWorkerProcess.CancellationPending)
                  {
                     break;
                  }
               }

               if (customerCount > 0)
               {
                  MessageBox.Show("Customer import results:" + System.Environment.NewLine + "  Attempted: " + customerCount.ToString() + System.Environment.NewLine + "  Imported: " + importedCount.ToString() + System.Environment.NewLine + "  Duplicates: " + duplicateNameCount.ToString() + System.Environment.NewLine + "  Errors: " + errorCount.ToString(), "Import Results");
               }
            }
            else
            {
               MessageBox.Show(oCustomerResult.ErrorMessage, "API/Service Error");
            }

            oQuickbooks.UpdateWebServiceOfImportedCustomer(oSMSIDImported);

            m_oWorkerProcess.ReportProgress(100);
             }
             catch (Exception e)
             {
            MessageBox.Show(e.Message + System.Environment.NewLine + System.Environment.NewLine + e.StackTrace);
             }
             finally
             {
            oQuery.CloseQBConnection();
             }
        }