public void Execute(params object[] args) { m_application.Messenger.AddInfo("Executing import from QuickBooks."); int countOrdersAdded = 0; QBSessionManager session = null; try { // Start session with QuickBooks session = new QBSessionManager(); session.OpenConnection("123", "ArcLogistics PlugIn: QuickBooksPlugIns.ImportOrdersFromInvoicesCmd"); session.BeginSession("", ENOpenMode.omDontCare); IInvoiceRetList invoiceList = QueryInvoices(session); int invoiceCount = invoiceList.Count; for (int i = 0; i < invoiceCount; i++) { IInvoiceRet invoiceRet = invoiceList.GetAt(i); ESRI.ArcLogistics.DomainObjects.Order newOrder = MakeOrderFromInvoice(invoiceRet, session); m_application.Project.Orders.Add(newOrder); countOrdersAdded++; } } catch (Exception e) { m_application.Messenger.AddError("Error executing QuickBooksPlugIns.ImportOrdersFromInvoicesCmd: " + e.Message); } finally { // Close connection because we don't need it anymore if (session != null) { session.EndSession(); session.CloseConnection(); } m_application.Project.Save(); m_application.Messenger.AddInfo(countOrdersAdded.ToString() + " orders added."); } }
private List <Invoice> walkInvoice(IMsgSetResponse responseMsgSet) { if (responseMsgSet == null) { return(null); } IResponseList responseList = responseMsgSet.ResponseList; if (responseList == null) { return(null); } IResponse response = responseList.GetAt(0); //check the status code of the response, 0=ok, >0 is warning if (response.StatusCode == 0) { //the request-specific response is in the details, make sure we have some if (response.Detail != null) { //make sure the response is the type we're expecting ENResponseType responseType = (ENResponseType)response.Type.GetValue(); if (responseType == ENResponseType.rtInvoiceQueryRs) { //upcast to more specific type here, this is safe because we checked with response.Type check above IInvoiceRetList InvoiceRet = (IInvoiceRetList)response.Detail; int count = InvoiceRet.Count; if (count > 0) { InvoiceList = new List <Invoice>(); } for (int a = 0; a < count; a++) { InvoiceList.Add(WalkInvoiceSearchRet(InvoiceRet.GetAt(a))); } } } } else { throw new QBException(response.StatusCode, response.StatusMessage.ToString(), requestMsgSet.ToXMLString()); } return(InvoiceList); }
IList <Invoice> InvoiceRet(IInvoiceRetList InvoiceList) { // InvoiceList = new List<QBEngine.Invoice>(); for (int i = 0; i < InvoiceList.Count; i++) { #region comments // dsCustomer.InvoiceRow row = dataset.Invoice.NewInvoiceRow(); // IInvoiceRet InvoiceRet = InvoiceList.GetAt(i); // if (InvoiceRet == null) return null; // //string TxnID7 = (string)InvoiceRet.TxnID.GetValue(); // row.TimeCreated = InvoiceRet.TimeCreated.GetValue().ToString(); // row.TimeModified = InvoiceRet.TimeModified.GetValue().ToString(); // row.EditSequence = (string)InvoiceRet.EditSequence.GetValue(); // if (InvoiceRet.CustomerMsgRef != null) // row.CustomerMsg = InvoiceRet.CustomerMsgRef.FullName.GetValue().ToString(); //if (InvoiceRet.TxnNumber!= null) // row.TxnNumber = InvoiceRet.TxnNumber.GetValue().ToString(); //if (InvoiceRet.TermsRef != null) // row.Terms= InvoiceRet.TermsRef.FullName.GetValue().ToString(); // if (InvoiceRet.RefNumber != null) // row.InvoiceNo = InvoiceRet.RefNumber.GetValue(); // else // row.InvoiceNo = (string)InvoiceRet.EditSequence.GetValue(); // if (InvoiceRet.TxnNumber != null) // { // row.TxnNumber = InvoiceRet.TxnNumber.GetValue().ToString(); // } // //Get value of FullName // if (InvoiceRet.CustomerRef.FullName != null) // { // row.CustomerName = (string)InvoiceRet.CustomerRef.FullName.GetValue(); // } // if (InvoiceRet.CustomerSalesTaxCodeRef != null ) // row.CustomerTaxCode = InvoiceRet.CustomerSalesTaxCodeRef.FullName.GetValue().ToString(); // if (InvoiceRet.ClassRef != null) // { // //Get value of FullName // if (InvoiceRet.ClassRef.FullName != null) // { // row.ClassRef = (string)InvoiceRet.ClassRef.FullName.GetValue(); // } // if (InvoiceRet.ClassRef.ListID != null) // { // row.ClassRef_listID = (string)InvoiceRet.ClassRef.ListID.GetValue(); // } // } // #region attributes // if (InvoiceRet.BillAddress != null) // { // ///Get value of Addr1 // if (InvoiceRet.BillAddress.Addr1 != null) // { // row.BillAddress1 = (string)InvoiceRet.BillAddress.Addr1.GetValue(); // } // if (InvoiceRet.BillAddress.Addr2 != null) // { // row.BillAddress2 = (string)InvoiceRet.BillAddress.Addr2.GetValue(); // } // if (InvoiceRet.BillAddress.Addr3 != null) // { // row.BillAddress3 = (string)InvoiceRet.BillAddress.Addr3.GetValue(); // } // if (InvoiceRet.BillAddress.Addr4 != null) // { // row.BillAddress4 = (string)InvoiceRet.BillAddress.Addr4.GetValue(); // } // ////Get value of City // if (InvoiceRet.BillAddress.City != null) // { // row.City = (string)InvoiceRet.BillAddress.City.GetValue(); // } // ////Get value of State // if (InvoiceRet.BillAddress.State != null) // { // row.State = (string)InvoiceRet.BillAddress.State.GetValue(); // } // ////Get value of PostalCode // if (InvoiceRet.BillAddress.PostalCode != null) // { // row.PostalCode = (string)InvoiceRet.BillAddress.PostalCode.GetValue(); // } // ////Get value of Country // if (InvoiceRet.BillAddress.Country != null) // { // row.Country = (string)InvoiceRet.BillAddress.Country.GetValue(); // } // ////Get value of Note // } // double amount = Math.Abs( double.Parse(InvoiceRet.AppliedAmount.GetValue().ToString())); // row.Amount = Convert.ToString(amount); // if (InvoiceRet.ShipAddress != null) // { // ////Get value of Addr1 // if (InvoiceRet.ShipAddress.Addr1 != null) // { // row.Ship_Address1 = (string)InvoiceRet.ShipAddress.Addr1.GetValue(); // } // ////Get value of Addr1 // if (InvoiceRet.ShipAddress.Addr2 != null) // { // row.ShipAddress2 = (string)InvoiceRet.ShipAddress.Addr2.GetValue(); // } // ////Get value of Addr1 // if (InvoiceRet.ShipAddress.Addr3 != null) // { // row.ShipAddress3 = (string)InvoiceRet.ShipAddress.Addr3.GetValue(); // } // ////Get value of Addr1 // if (InvoiceRet.ShipAddress.Addr4 != null) // { // row.ShipAddress4 = (string)InvoiceRet.ShipAddress.Addr4.GetValue(); // } // ////Get value of City // if (InvoiceRet.ShipAddress.City != null) // { // row.Ship_City = (string)InvoiceRet.ShipAddress.City.GetValue(); // } // ////Get value of State // if (InvoiceRet.ShipAddress.State != null) // { // row.Ship_State = (string)InvoiceRet.ShipAddress.State.GetValue(); // } // ////Get value of PostalCode // if (InvoiceRet.ShipAddress.PostalCode != null) // { // row.Ship_PostalCode = (string)InvoiceRet.ShipAddress.PostalCode.GetValue(); // } // ////Get value of Country // if (InvoiceRet.ShipAddress.Country != null) // { // row.Ship_Country = (string)InvoiceRet.ShipAddress.Country.GetValue(); // } // } // ////Get value of DueDate // if (InvoiceRet.DueDate != null) // { // row.DueDate = InvoiceRet.DueDate.GetValue().ToString(); // } // ////Get value of ShipDate // if (InvoiceRet.ShipDate != null) // { // row.ShipDate = InvoiceRet.ShipDate.GetValue().ToString(); // } // if (InvoiceRet.PONumber != null) // row.PONumber = InvoiceRet.PONumber.GetValue().ToString(); // if (InvoiceRet.IsPaid != null) // row.IsPaid = InvoiceRet.IsPaid.GetValue().ToString(); // if (InvoiceRet.SalesRepRef != null) // { // if (InvoiceRet.SalesRepRef.FullName != null) // row.SalesRep_Name = InvoiceRet.SalesRepRef.FullName.ToString(); // if (InvoiceRet.SalesRepRef.ListID != null) // row.SalesRep_listID = InvoiceRet.SalesRepRef.ListID.GetValue().ToString(); // } // if (InvoiceRet.ShipMethodRef != null) // { // if (InvoiceRet.ShipMethodRef.ListID != null) // row.ShipMethod_listID = InvoiceRet.ShipMethodRef.ListID.GetValue().ToString(); // if (InvoiceRet.ShipMethodRef.FullName != null ) // row.ShipMethod_Name = InvoiceRet.ShipMethodRef.FullName.GetValue().ToString(); // } // if (InvoiceRet.CustomerRef != null) // { // row.Customer_listID = InvoiceRet.CustomerRef.ListID.GetValue().ToString(); // } // if (InvoiceRet.Other != null) // row.Other = InvoiceRet.Other.ToString(); // ////Get value of Memo // if (InvoiceRet.Memo != null) // { // row.Memo = (string)InvoiceRet.Memo.GetValue(); // } // for (int a = 0; a < InvoiceRet.ORInvoiceLineRetList.Count; a++) // { // IORInvoiceLineRet ORInvoiceLineRet = InvoiceRet.ORInvoiceLineRetList.GetAt(a); // //InvoiceLineItem item = new InvoiceLineItem(); // dsCustomer.InvoiceLineItemRow item = dataset.InvoiceLineItem.NewInvoiceLineItemRow(); // item.InvoiceNo = row.InvoiceNo; // if (ORInvoiceLineRet.InvoiceLineRet != null) // { // ////Get value of TxnLineID // //string TxnLineID90 = (string)ORInvoiceLineRet.InvoiceLineRet.TxnLineID.GetValue(); // if (ORInvoiceLineRet.InvoiceLineRet.ItemRef != null) // { // if (ORInvoiceLineRet.InvoiceLineRet.ItemRef.FullName != null) // { // item.Item = (string)ORInvoiceLineRet.InvoiceLineRet.ItemRef.FullName.GetValue(); // } // } // ////Get value of Desc // if (ORInvoiceLineRet.InvoiceLineRet.Desc != null) // { // item.Desc = (string)ORInvoiceLineRet.InvoiceLineRet.Desc.GetValue(); // } // ////Get value of Quantity // if (ORInvoiceLineRet.InvoiceLineRet.Quantity != null) // { // item.Quantity = ORInvoiceLineRet.InvoiceLineRet.Quantity.GetValue().ToString(); // } // //} // if (ORInvoiceLineRet.InvoiceLineRet.ClassRef != null) // { // ////Get value of FullName // if (ORInvoiceLineRet.InvoiceLineRet.ClassRef.FullName != null) // { // item.ClassRef = (string)ORInvoiceLineRet.InvoiceLineRet.ClassRef.FullName.GetValue(); // } // if (ORInvoiceLineRet.InvoiceLineRet.IsTaxable.GetValue()) // item.SalesTaxCodeRef = ORInvoiceLineRet.InvoiceLineRet.SalesTaxCodeRef.FullName.GetValue().ToString(); // if (ORInvoiceLineRet.InvoiceLineRet.ClassRef.ListID != null) // { // item.ClassRef = (string)ORInvoiceLineRet.InvoiceLineRet.ClassRef.FullName.GetValue(); // } // } // if (ORInvoiceLineRet.InvoiceLineRet.Amount != null) // { // item.Amount = ORInvoiceLineRet.InvoiceLineRet.Amount.GetValue().ToString(); // } // #endregion // } // dataset.InvoiceLineItem.AddInvoiceLineItemRow(item); // } // dataset.Invoice.AddInvoiceRow(row); //} //return dataset; #endregion } return(null); }
public static void DoQbXml() { bool sessionBegun = false; bool connectionOpen = false; QBSessionManager sessionManager = null; sessionManager = new QBSessionManager(); //Connect to QuickBooks and begin a session sessionManager.OpenConnection("", "GenerateInvoicePDFs"); connectionOpen = true; sessionManager.BeginSession("", ENOpenMode.omDontCare); sessionBegun = true; //Create the message set request object to hold our request IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13, 0); requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue; IInvoiceQuery invoiceQueryRq = requestMsgSet.AppendInvoiceQueryRq(); invoiceQueryRq.IncludeLineItems.SetValue(true); //Send the request and get the response from QuickBooks IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet); IResponse response = responseMsgSet.ResponseList.GetAt(0); IInvoiceRetList invoiceRetList = (IInvoiceRetList)response.Detail; var invoices = new List <Invoice>(); if (invoiceRetList != null) { for (int i = 0; i < invoiceRetList.Count; i++) { IInvoiceRet invoiceRet = invoiceRetList.GetAt(i); var invoice = new Invoice { QuickBooksID = invoiceRet.TxnID.GetValue(), EditSequence = invoiceRet.EditSequence.GetValue(), InvoiceNumber = invoiceRet.RefNumber?.GetValue(), Memo = invoiceRet.Memo?.GetValue(), JobNumber = invoiceRet.Other?.GetValue() }; Console.WriteLine($"INv:{invoice.InvoiceNumber}, EditSequence: {invoice.JobNumber}"); requestMsgSet.ClearRequests(); ICustomerQuery customerQueryRq = requestMsgSet.AppendCustomerQueryRq(); customerQueryRq.ORCustomerListQuery.ListIDList.Add(invoiceRet.CustomerRef.ListID.GetValue()); //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); ICustomerRetList customerRetList = (ICustomerRetList)response.Detail; ICustomerRet customerRet = customerRetList.GetAt(0); if (i > 200) { return; } invoice.Customer = new Customer { Name = customerRet.Name.GetValue(), QuickBooksID = customerRet.ListID.GetValue(), EditSequence = customerRet.EditSequence.GetValue() }; if (invoiceRet.ORInvoiceLineRetList != null) { for (int j = 0; j < invoiceRet.ORInvoiceLineRetList.Count; j++) { IORInvoiceLineRet ORInvoiceLineRet = invoiceRet.ORInvoiceLineRetList.GetAt(j); try { var invoiceItem = new InvoiceItem { Amount = ORInvoiceLineRet.InvoiceLineRet.Amount.GetValue(), QuickBooksID = ORInvoiceLineRet.InvoiceLineRet.TxnLineID.GetValue() }; requestMsgSet.ClearRequests(); IItemQuery itemQueryRq = requestMsgSet.AppendItemQueryRq(); itemQueryRq.ORListQuery.ListIDList.Add(ORInvoiceLineRet.InvoiceLineRet.ItemRef.ListID.GetValue()); //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); IORItemRetList itemRetList = (IORItemRetList)response.Detail; IORItemRet itemRet = itemRetList.GetAt(0); if (itemRet.ItemInventoryRet != null) { IItemInventoryRet itemInventoryRet = itemRet.ItemInventoryRet; var item = new Item { Name = itemInventoryRet.Name.GetValue(), Description = itemInventoryRet.SalesDesc.GetValue(), Rate = itemInventoryRet.SalesPrice.GetValue(), //ItemType = ItemType.Inventory, QuickBooksID = itemInventoryRet.ListID.GetValue(), EditSequence = itemInventoryRet.EditSequence.GetValue() }; invoiceItem.Item = item; } else if (itemRet.ItemServiceRet != null) { IItemServiceRet itemServiceRet = itemRet.ItemServiceRet; var item = new Item { Name = itemServiceRet.Name.GetValue(), Description = itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue(), Rate = itemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue(), //ItemType = ItemType.Service, QuickBooksID = itemServiceRet.ListID.GetValue(), EditSequence = itemServiceRet.EditSequence.GetValue() }; invoiceItem.Item = item; } invoice.InvoiceItems.Add(invoiceItem); } catch (Exception ex) { Console.WriteLine(invoice.Customer.Name); throw; } } } invoices.Add(invoice); } } }
void bw_DoWork(object sender, DoWorkEventArgs e) { if (_invoices == null) { return; // Haven't parsed any invoices yet, or there was an error } sessionManager.BeginSession(COMPANY_FILE, ENOpenMode.omDontCare); _sessionBegun = true; _failed = false; // Grab the list of unpaid invoices from the selected customer IMsgSetRequest messageSet = sessionManager.CreateMsgSetRequest("CA", 12, 0); IInvoiceQuery invoiceQuery = messageSet.AppendInvoiceQueryRq(); invoiceQuery.ORInvoiceQuery.InvoiceFilter.PaidStatus.SetValue(ENPaidStatus.psNotPaidOnly); invoiceQuery.ORInvoiceQuery.InvoiceFilter.EntityFilter.OREntityFilter.FullNameList.Add(e.Argument.ToString()); try { IMsgSetResponse responseSet = sessionManager.DoRequests(messageSet); IResponse response; ENResponseType responseType; for (int i = 0; i < responseSet.ResponseList.Count; i++) { response = responseSet.ResponseList.GetAt(i); if (response.Detail == null) { continue; } responseType = (ENResponseType)response.Type.GetValue(); if (responseType == ENResponseType.rtInvoiceQueryRs) { IInvoiceRetList invoiceList = (IInvoiceRetList)response.Detail; for (int invoiceIndex = 0; invoiceIndex < invoiceList.Count; invoiceIndex++) { IInvoiceRet invoice = (IInvoiceRet)invoiceList.GetAt(invoiceIndex); if (invoice != null) { //unpaid_listbox.Items.Add(invoice.CustomerRef.FullName.GetValue() + " - " + invoice.RefNumber.GetValue() + " ( " + invoice.TxnID.GetValue() + " )"); _unpaidInvoices.Add(new Invoice(invoice.RefNumber.GetValue(), invoice.TxnID.GetValue())); int index = _invoices.FindIndex(x => x.Probill == invoice.RefNumber.GetValue()); if (index >= 0) { _invoices[index].TxnID = invoice.TxnID.GetValue(); Trace.WriteLine("Applying TxnID " + invoice.TxnID.GetValue() + " to Invoice " + _invoices[index].Probill); } } } } } messageSet = sessionManager.CreateMsgSetRequest("CA", 12, 0); messageSet.Attributes.OnError = ENRqOnError.roeContinue; IReceivePaymentAdd payment = messageSet.AppendReceivePaymentAddRq(); // Populate the request with all the details payment.CustomerRef.FullName.SetValue(e.Argument.ToString()); payment.RefNumber.SetValue(_invoices.First().ChequeNumber); payment.PaymentMethodRef.FullName.SetValue("Cheque"); IAppliedToTxnAdd txn; double totalAmt = 0.0; int count = 0; foreach (Invoice i in _invoices) { // Don't add payments that are negative (IE. back charges, etc) if (i.Total < 0) { Trace.WriteLine(String.Format("Rejecting negative transaction {0} for ${1}", i.Probill, i.Total)); // Subtract it from our total // TODO: add an expense? //totalAmt -= i.Total; continue; } if (i.Total == 0) { // Discard any empty entries Trace.WriteLine("Rejecting empty entry"); continue; } if (i.TxnID == "") { // Discard any invalid entries (couldn't find an unpaid invoice that matches) Trace.WriteLine("Rejecting entry with no TxnID"); continue; } txn = payment.ORApplyPayment.AppliedToTxnAddList.Append(); txn.PaymentAmount.SetValue(i.Total); txn.TxnID.SetValue(i.TxnID); totalAmt += i.Total; count += 1; Trace.WriteLine("Adding Transaction of " + i.Total + " with TxnID " + i.TxnID + " ( " + count + " )"); } Trace.WriteLine("Total amount received: " + totalAmt); // Round off the total to two decimal places, otherwise it will sometimes inexplicably fail totalAmt = Math.Round(totalAmt, 2); payment.TotalAmount.SetValue(totalAmt); // TODO: fix hardcoded version number payment.Memo.SetValue("Automatically generated by qbFixer 0.1 beta"); //payment.TxnDate.SetValue(DateTime.Today); responseSet = sessionManager.DoRequests(messageSet); for (int i = 0; i < responseSet.ResponseList.Count; i++) { response = responseSet.ResponseList.GetAt(i); if (response.StatusCode > 0) { MessageBox.Show(response.StatusMessage); return; } } _bw.ReportProgress(100); _bw.CancelAsync(); } catch (System.Runtime.InteropServices.COMException comEx) { MessageBox.Show(comEx.Message); _failed = true; } finally { sessionManager.EndSession(); _sessionBegun = false; } }
public IList <Invoice> GetInvoiceDetail(DateTime fromDate, DateTime toDate) { bool sessionBegun = false; QBSessionManager sessionManager = null; var invoices = new List <Invoice>(); sessionManager = new QBSessionManager(); IMsgSetRequest requestMsgSet = null; try { //Connect to QuickBooks and begin a session sessionManager.OpenConnection("", "GenerateInvoiceSummary"); //connectionOpen = true; sessionManager.BeginSession("", ENOpenMode.omDontCare); sessionBegun = true; //Create the message set request object to hold our request requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13, 0); requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue; IInvoiceQuery invoiceQueryRq = requestMsgSet.AppendInvoiceQueryRq(); // all invoices modified in the month of August 2016 // get all invoices for the month of august 2016 invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.FromTxnDate.SetValue(fromDate); invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.TxnDateRangeFilter.ORTxnDateRangeFilter.TxnDateFilter.ToTxnDate.SetValue(toDate); // invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(new DateTime(2017, 12, 1), true); // invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.ToModifiedDate.SetValue(new DateTime(2017, 12, 31), true); invoiceQueryRq.IncludeLineItems.SetValue(true); //Send the request and get the response from QuickBooks IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet); IResponse response = responseMsgSet.ResponseList.GetAt(0); IInvoiceRetList invoiceRetList = (IInvoiceRetList)response.Detail; Console.WriteLine($"Invoices found: {invoiceRetList.Count}"); if (invoiceRetList != null) { for (int i = 0; i < invoiceRetList.Count; i++) { IInvoiceRet invoiceRet = invoiceRetList.GetAt(i); var invoice = new Invoice { QuickBooksID = invoiceRet.TxnID.GetValue(), EditSequence = invoiceRet.EditSequence.GetValue(), InvoiceNumber = invoiceRet.RefNumber?.GetValue(), InvoiceDate = invoiceRet.TimeCreated?.GetValue(), Memo = invoiceRet.Memo?.GetValue(), JobNumber = invoiceRet.Other?.GetValue(), CustomerName = invoiceRet.CustomerRef.FullName?.GetValue(), Amount = invoiceRet.BalanceRemaining?.GetValue() }; if (invoice.Amount == 0) { invoice.Amount = invoiceRet.Subtotal?.GetValue(); } var customerListId = invoiceRet.CustomerRef?.ListID?.GetValue(); if (customerListId != null) { // Console.WriteLine($"{i}\tInv:{invoice.InvoiceNumber}, Job: {invoice.JobNumber}, Name:{invoice.CustomerName}"); requestMsgSet.ClearRequests(); ICustomerQuery customerQueryRq = requestMsgSet.AppendCustomerQueryRq(); customerQueryRq.ORCustomerListQuery.ListIDList.Add(customerListId); //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); ICustomerRetList customerRetList = (ICustomerRetList)response.Detail; ICustomerRet customerRet = customerRetList.GetAt(0); invoice.Customer = new Customer { Name = customerRet.Name?.GetValue(), QuickBooksID = customerRet.ListID?.GetValue(), EditSequence = customerRet.EditSequence?.GetValue(), FullName = customerRet.FullName?.GetValue(), CompanyName = customerRet.CompanyName?.GetValue(), }; // Console.WriteLine($"{i}\t{invoice.Customer.Name}\t{invoice.Customer.FullName}\t{invoice.Customer.CompanyName}"); // Console.WriteLine($"{i}\t\tInvoice detail starts"); if (invoiceRet.ORInvoiceLineRetList != null) { invoice.Description = ""; if (invoiceRet.ORInvoiceLineRetList.Count > 0) { try { IORInvoiceLineRet orInvoiceLineRet = invoiceRet.ORInvoiceLineRetList.GetAt(1); invoice.Description = orInvoiceLineRet?.InvoiceLineRet?.Desc?.GetValue(); if (string.IsNullOrEmpty(invoice.Description)) { orInvoiceLineRet = invoiceRet.ORInvoiceLineRetList.GetAt(0); invoice.Description = orInvoiceLineRet?.InvoiceLineRet?.Desc?.GetValue(); } } catch (Exception ex) { Console.WriteLine(ex.Message); try { IORInvoiceLineRet orInvoiceLineRet = invoiceRet.ORInvoiceLineRetList.GetAt(0); invoice.Description = orInvoiceLineRet?.InvoiceLineRet?.Desc?.GetValue(); } catch (Exception ex2) { Console.WriteLine(ex2.Message); } } Console.WriteLine($"{invoice.InvoiceNumber}\t{invoice.Amount} \t{invoice.JobNumber}\t{invoice.Description}"); } // Console.WriteLine($"InvoiceList Count: {invoiceRet.ORInvoiceLineRetList.Count}"); /* * for (int j = 0; j < invoiceRet.ORInvoiceLineRetList.Count; j++) * { * IORInvoiceLineRet orInvoiceLineRet = invoiceRet.ORInvoiceLineRetList.GetAt(j); * * * if (orInvoiceLineRet != null && orInvoiceLineRet.InvoiceLineRet != null) * { * var invoiceItem = new InvoiceItem * { * Amount = orInvoiceLineRet.InvoiceLineRet.Amount?.GetValue(), * QuickBooksID = orInvoiceLineRet.InvoiceLineRet.TxnLineID?.GetValue() * }; * * requestMsgSet.ClearRequests(); * IItemQuery itemQueryRq = requestMsgSet.AppendItemQueryRq(); * itemQueryRq.ORListQuery.ListIDList.Add(orInvoiceLineRet.InvoiceLineRet.ItemRef?.ListID?.GetValue()); * * //Send the request and get the response from QuickBooks * responseMsgSet = sessionManager.DoRequests(requestMsgSet); * response = responseMsgSet.ResponseList.GetAt(0); * IORItemRetList itemRetList = (IORItemRetList)response.Detail; * // Console.WriteLine($"ItemRetList.Count: {itemRetList.Count}"); * * IORItemRet itemRet = itemRetList.GetAt(0); * var ortype = itemRet.ortype; * if (itemRet.ItemInventoryRet != null) * { * IItemInventoryRet itemInventoryRet = itemRet.ItemInventoryRet; * * var item = new Item * { * Name = itemInventoryRet.Name?.GetValue(), * Description = itemInventoryRet.SalesDesc?.GetValue(), * Rate = itemInventoryRet.SalesPrice?.GetValue(), * ItemType = ortype.ToString(), * QuickBooksID = itemInventoryRet.ListID?.GetValue(), * EditSequence = itemInventoryRet.EditSequence?.GetValue() * * }; * if (string.IsNullOrEmpty(item.Name)) * { * item.Name = itemInventoryRet.FullName?.GetValue(); * } * * invoiceItem.Item = item; * } * else if (itemRet.ItemServiceRet != null) * { * IItemServiceRet itemServiceRet = itemRet.ItemServiceRet; * * var item = new Item * { * Name = itemServiceRet.Name.GetValue(), * Description = itemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc?.GetValue(), * Rate = itemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price?.GetValue(), * //ItemType = ItemType.Service, * ItemType = ortype.ToString(), * QuickBooksID = itemServiceRet.ListID?.GetValue(), * EditSequence = itemServiceRet.EditSequence?.GetValue(), * // FullName = itemServiceRet.ToString() * }; * if (string.IsNullOrEmpty(item.Name)) * { * item.Name = itemServiceRet.FullName?.GetValue(); * } * invoiceItem.Item = item; * } * else if (itemRet.ItemOtherChargeRet != null) * { * IItemOtherChargeRet itemOtherChargeRet = itemRet.ItemOtherChargeRet; * var item = new Item * { * Name = itemOtherChargeRet.Name?.GetValue(), * Description = itemOtherChargeRet.ORSalesPurchase.SalesOrPurchase.Desc?.GetValue(), * Rate = itemOtherChargeRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price?.GetValue(), * ItemType = ortype.ToString() * * }; * if (string.IsNullOrEmpty(item.Name)) * { * item.Name = itemOtherChargeRet.FullName?.GetValue(); * } * invoiceItem.Item = item; * } * else if (itemRet.ItemNonInventoryRet != null) * { * IItemNonInventoryRet itemNonInventoryRet = itemRet.ItemNonInventoryRet; * var item = new Item * { * Name = itemNonInventoryRet.Name?.GetValue(), * Description = itemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.Desc?.GetValue(), * ItemType = ortype.ToString() * * }; * if (string.IsNullOrEmpty(item.Name)) * { * item.Name = itemNonInventoryRet.FullName?.GetValue(); * } * invoiceItem.Item = item; * } * Console.WriteLine($"{invoiceItem.Item.FullName}\t{invoice.InvoiceNumber}\t{invoiceItem.Amount}\t{invoiceItem.Item.Description}"); * invoice.InvoiceItems.Add(invoiceItem); * * } * } */ } } invoices.Add(invoice); } } if (requestMsgSet != null) { Marshal.FinalReleaseComObject(requestMsgSet); } sessionManager.EndSession(); sessionBegun = false; sessionManager.CloseConnection(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString() + "\nStack Trace: \n" + ex.StackTrace + "\nExiting the application"); if (requestMsgSet != null) { Marshal.FinalReleaseComObject(requestMsgSet); } if (sessionBegun) { sessionManager.EndSession(); sessionManager.CloseConnection(); } } return(invoices); }
public IList <Invoice> GetInvoiceWithCustomer() { bool sessionBegun = false; QBSessionManager sessionManager = null; var invoices = new List <Invoice>(); sessionManager = new QBSessionManager(); try { //Connect to QuickBooks and begin a session sessionManager.OpenConnection("", "GenerateInvoicePDFs"); //connectionOpen = true; sessionManager.BeginSession("", ENOpenMode.omDontCare); sessionBegun = true; //Create the message set request object to hold our request IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13, 0); requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue; IInvoiceQuery invoiceQueryRq = requestMsgSet.AppendInvoiceQueryRq(); // all invoices modified in the month of August 2016 invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(new DateTime(2017, 12, 1), true); invoiceQueryRq.ORInvoiceQuery.InvoiceFilter.ORDateRangeFilter.ModifiedDateRangeFilter.ToModifiedDate.SetValue(new DateTime(2017, 12, 31), true); //invoiceQueryRq.IncludeLineItems.SetValue(true); //Send the request and get the response from QuickBooks IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet); IResponse response = responseMsgSet.ResponseList.GetAt(0); IInvoiceRetList invoiceRetList = (IInvoiceRetList)response.Detail; if (invoiceRetList != null) { for (int i = 0; i < invoiceRetList.Count; i++) { IInvoiceRet invoiceRet = invoiceRetList.GetAt(i); var invoice = new Invoice { QuickBooksID = invoiceRet.TxnID.GetValue(), EditSequence = invoiceRet.EditSequence.GetValue(), InvoiceNumber = invoiceRet.RefNumber?.GetValue(), Memo = invoiceRet.Memo?.GetValue(), JobNumber = invoiceRet.Other?.GetValue(), CustomerName = invoiceRet.CustomerRef.FullName?.GetValue() }; var customerListId = invoiceRet.CustomerRef?.ListID?.GetValue(); if (customerListId != null) { Console.WriteLine($"INv:{invoice.InvoiceNumber}, Job: {invoice.JobNumber}, Name:{invoice.CustomerName}"); requestMsgSet.ClearRequests(); ICustomerQuery customerQueryRq = requestMsgSet.AppendCustomerQueryRq(); customerQueryRq.ORCustomerListQuery.ListIDList.Add(customerListId); //Send the request and get the response from QuickBooks responseMsgSet = sessionManager.DoRequests(requestMsgSet); response = responseMsgSet.ResponseList.GetAt(0); ICustomerRetList customerRetList = (ICustomerRetList)response.Detail; ICustomerRet customerRet = customerRetList.GetAt(0); invoice.Customer = new Customer { Name = customerRet.Name?.GetValue(), QuickBooksID = customerRet.ListID?.GetValue(), EditSequence = customerRet.EditSequence?.GetValue(), FullName = customerRet.FullName?.GetValue(), CompanyName = customerRet.CompanyName?.GetValue() }; Console.WriteLine($"{i}\t{invoice.Customer.Name}\t{invoice.Customer.FullName}\t{invoice.Customer.CompanyName}"); } invoices.Add(invoice); } } if (requestMsgSet != null) { Marshal.FinalReleaseComObject(requestMsgSet); } sessionManager.EndSession(); sessionBegun = false; sessionManager.CloseConnection(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString() + "\nStack Trace: \n" + ex.StackTrace + "\nExiting the application"); if (sessionBegun) { sessionManager.EndSession(); sessionManager.CloseConnection(); } } return(invoices); }
public Invoice Populate(string invoiceNumber) { IMsgSetRequest requestMsgSet = _MySessionManager.CreateMsgSetRequest("US", 13, 0); IInvoiceQuery invoiceQuery = requestMsgSet.AppendInvoiceQueryRq(); invoiceQuery.ORInvoiceQuery.RefNumberList.Add(invoiceNumber); invoiceQuery.IncludeLineItems.SetValue(true); IMsgSetResponse responseMsgSet = _MySessionManager.DoRequests(requestMsgSet); IResponseList rsList = responseMsgSet.ResponseList; IResponse response = rsList.GetAt(0); IInvoiceRetList InvoiceList = (IInvoiceRetList)response.Detail; if (InvoiceList == null) { throw new Exception("Invoice not found."); } try { IInvoiceRet QBInvoices = InvoiceList.GetAt(0); _invoice = new Invoice(); _invoice.Number = QBInvoices.RefNumber.GetValue(); _invoice.Total = QBInvoices.Subtotal.GetAsString(); _invoice.Date = QBInvoices.TxnDate.GetValue(); _invoice.Terms = QBInvoices.TermsRef.FullName.GetValue(); if (QBInvoices.PONumber != null) { _invoice.PONumber = QBInvoices.PONumber.GetValue(); } if (QBInvoices.ShipDate != null) { _invoice.ShipDate = QBInvoices.ShipDate.GetValue(); } _invoice.CustomerFullName = QBInvoices.CustomerRef.FullName.GetValue(); Address address = new Address(); _invoice.BillingAddress = address.getAddress(QBInvoices.BillAddress); _invoice.ShippingAddress = address.getAddress(QBInvoices.ShipAddress); IORInvoiceLineRetList InvoiceItems = QBInvoices.ORInvoiceLineRetList; if (InvoiceItems != null) { for (int i = 0; i <= InvoiceItems.Count - 1; i++) { IORInvoiceLineRet InvoiceItem = InvoiceItems.GetAt(i); if (InvoiceItem.ortype == ENORInvoiceLineRet.orilrInvoiceLineRet) { InventoryItem inventoryItem = new InventoryItem(); inventoryItem.ItemCode = InvoiceItem.InvoiceLineRet.ItemRef.FullName.GetValue(); inventoryItem.Description = InvoiceItem.InvoiceLineRet.Desc.GetValue(); if (InvoiceItem.InvoiceLineRet.Quantity != null) { _invoice.TotalQty += InvoiceItem.InvoiceLineRet.Quantity.GetValue(); inventoryItem.Quantity = InvoiceItem.InvoiceLineRet.Quantity.GetAsString(); } else { inventoryItem.Quantity = ""; } inventoryItem.Price = InvoiceItem.InvoiceLineRet.ORRate.Rate.GetValue(); inventoryItem.Amount = InvoiceItem.InvoiceLineRet.Amount.GetAsString(); _invoice.InventoryItems.Add(inventoryItem); } } //Update the items UPC and List in the sales order from itemsWithUPC list //The reason is that the salesOrder items don't have these properties //So we make another request to QB to get items with all properties InventoryHelper inventoryHelper = new InventoryHelper(_MySessionManager); _invoice.InventoryItems = inventoryHelper.UpdateItemUPCAndListID(_invoice.InventoryItems); } return(_invoice); } catch (Exception) { throw new Exception("Failed to read Invoice from QuickBooks."); } }