Exemple #1
0
        private void Task_ImportPayments(Objsearch oQuery, Invoice oInvoice, Customer oCustomer, Receivepayment oReceivePayment, Quickbooks oQuickbooks)
        {
            try
             {
            oQuery.OpenQBConnection();
            oInvoice.OpenQBConnection();
            oCustomer.OpenQBConnection();
            oReceivePayment.OpenQBConnection();

            PaymentResult oPaymentResult = oQuickbooks.GetPayments();
            if (oPaymentResult.IsSuccessfull)
            {
               m_iTotal = oPaymentResult.Payments.Count();
               m_iCurrent = 0;
               var errorCount = 0;
               var successCount = 0;

               foreach (SpeedySpots.API.Interfaces.Payment oPayment in oPaymentResult.Payments)
               {
                  decimal fTotal = 0;
                  string sInvoices = string.Empty;
                  oReceivePayment.Reset();

                  foreach (SpeedySpots.API.Interfaces.PaymentLineItem oPaymentLineItem in oPayment.LineItems)
                  {
                     oQuery.Reset();
                     oQuery.QBXMLVersion = "5.0";
                     oQuery.QueryType = ObjsearchQueryTypes.qtInvoiceSearch;
                     oQuery.SearchCriteria = new SearchCriteria();
                     oQuery.SearchCriteria.RefNumber = oPaymentLineItem.InvoiceID;
                     oQuery.IterateResults = true;
                     oQuery.MaxResults = 0;
                     oQuery.Search();

                     if (oQuery.Results.Count > 0)
                     {
                        oInvoice.Get(oQuery.Results[0].ResultId);

                        AppliedTo oAppliedTo = new AppliedTo();
                        oAppliedTo.RefId = oQuery.Results[0].ResultId;
                        oAppliedTo.PaymentAmount = oPaymentLineItem.Amount.ToString("0.00");

                        oReceivePayment.AppliedTo.Add(oAppliedTo);

                        fTotal += oPaymentLineItem.Amount;
                        sInvoices += string.Format("{0}, ", oPaymentLineItem.InvoiceID);
                     }
                  }
                  try
                  {
                     oReceivePayment.AutoApply = ReceivepaymentAutoApplies.aaCustom;
                     oReceivePayment.CustomerName = oInvoice.CustomerName;
                     oReceivePayment.Amount = fTotal.ToString();
                     oReceivePayment.TransactionDate = oPayment.CreatedDateTime.ToString("d");

                     if (oPayment.CreditCardFirstName != string.Empty)
                     {
                        oReceivePayment.RefNumber = string.Format("{0} {1}", oPayment.CreditCardFirstName, oPayment.CreditCardLastName);
                     }

                     if (oPayment.CreditCardType == "Visa")
                     {
                        oReceivePayment.PaymentMethodName = "Visa";
                     }
                     else if (oPayment.CreditCardType == "MasterCard")
                     {
                        oReceivePayment.PaymentMethodName = "Mastercard";
                     }
                     else if (oPayment.CreditCardType == "American Express")
                     {
                        oReceivePayment.PaymentMethodName = "American Express";
                     }
                     else if (oPayment.CreditCardType == "Discover")
                     {
                        oReceivePayment.PaymentMethodName = "Discover";
                     }

                     // Remove last ', '
                     if (sInvoices.Length > 2)
                     {
                        sInvoices = sInvoices.Substring(0, sInvoices.Length - 2);
                     }

                     oReceivePayment.Memo = string.Format("Payment for invoice {0} - AuthID: {1}", sInvoices, oPayment.AuthorizeNetID);

                     oReceivePayment.Add();
                     successCount++;
                  }
                  catch (Exception e)
                  {
                     errorCount++;
                     MessageBox.Show(e.Message + System.Environment.NewLine + System.Environment.NewLine + e.StackTrace);
                  }

                  this.UpdateProgress();

                  if (m_oWorkerProcess.CancellationPending)
                  {
                     break;
                  }
               }

               if (successCount > 0 || errorCount > 0)
               {
                  MessageBox.Show("Finished Payment Import" + Environment.NewLine + "Success: " + successCount + Environment.NewLine + "Error: " + errorCount);
               }
            }
            else
            {
               MessageBox.Show(oPaymentResult.ErrorMessage, "API/Service Error");
            }

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