Items WalkServiceItem(IItemServiceRet ItemRet) { Items item = new Items(); item.ItemType = ItemRet.Type.GetAsString(); item.Name = ItemRet.FullName.GetValue(); item.EditSequence = ItemRet.EditSequence.GetValue(); item.TxnID = ItemRet.ListID.GetValue(); if (ItemRet.ORSalesPurchase.SalesAndPurchase != null) { if (ItemRet.ORSalesPurchase.SalesAndPurchase.SalesPrice != null) { item.Rate = ItemRet.ORSalesPurchase.SalesAndPurchase.SalesPrice.GetValue(); } if (ItemRet.ORSalesPurchase.SalesAndPurchase.SalesDesc != null) { item.Description = ItemRet.ORSalesPurchase.SalesAndPurchase.SalesDesc.GetValue(); } } if (ItemRet.ORSalesPurchase.SalesOrPurchase != null) { if (ItemRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null) { item.Rate = ItemRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue(); } if (ItemRet.ORSalesPurchase.SalesOrPurchase.Desc != null) { item.Description = ItemRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue(); } } //if(ItemRet. != null) //item.Description = ItemRet.SalesDesc.GetValue(); if (ItemRet.IsActive != null) { if (ItemRet.IsActive.GetValue()) { return(item); } else { return(null); } } else { return(null); } }
private void InputItem_Load(object sender, System.EventArgs e) { // IY: Create the session manager object using QBFC QBSessionManager sessionManager = new QBSessionManager(); // IY: We want to know if we begun a session so we can end it if an // error happens bool booSessionBegun = false; try { // IY: Get the RequestMsgSet based on the correct QB Version IMsgSetRequest requestSet = getLatestMsgSetRequest(sessionManager); // IY: Initialize the message set request object requestSet.Attributes.OnError = ENRqOnError.roeStop; // IY: Add the request to the message set request object IItemQuery ItemQ = requestSet.AppendItemQueryRq(); // IY: Optionally, you can put filter on it. // ItemQ.ORListQuery.ListFilter.MaxReturned.SetValue(30); // IY: Open the connection and begin a session to QuickBooks sessionManager.OpenConnection("", "IDN InvoiceAdd C# sample"); sessionManager.BeginSession("", ENOpenMode.omDontCare); booSessionBegun = true; // IY: Do the request and get the response message set object IMsgSetResponse responseSet = sessionManager.DoRequests(requestSet); // Uncomment the following to view and save the request and response XML //string requestXML = requestSet.ToXMLString(); //MessageBox.Show(requestXML); // SaveXML(requestXML); //string responseXML = responseSet.ToXMLString(); //MessageBox.Show(responseXML); // SaveXML(responseXML); IResponse response = responseSet.ResponseList.GetAt(0); //int statusCode = response.StatusCode; //string statusMessage = response.StatusMessage; //string statusSeverity = response.StatusSeverity; //MessageBox.Show("Status:\nCode = " + statusCode + "\nMessage = " + statusMessage + "\nSeverity = " + statusSeverity); IORItemRetList orItemRetList = response.Detail as IORItemRetList; if (!(orItemRetList.Count == 0)) { for (int ndx = 0; ndx <= (orItemRetList.Count - 1); ndx++) { IORItemRet orItemRet = orItemRetList.GetAt(ndx); // IY: The ortype property returns an enum // of the elements that can be contained in the OR object switch (orItemRet.ortype) { case ENORItemRet.orirItemServiceRet: { // orir prefix comes from OR + Item + Ret IItemServiceRet ItemServiceRet = orItemRet.ItemServiceRet; isTaxable = ItemServiceRet?.SalesTaxCodeRef?.FullName?.GetValue(); SetTaxableDefaultIfEmpty(ref isTaxable); cmboBx2_Item.Items.Add(ItemServiceRet?.FullName?.GetValue() + ":" + isTaxable); } break; case ENORItemRet.orirItemInventoryRet: { IItemInventoryRet ItemInventoryRet = orItemRet.ItemInventoryRet; isTaxable = ItemInventoryRet?.SalesTaxCodeRef?.FullName?.GetValue(); SetTaxableDefaultIfEmpty(ref isTaxable); cmboBx2_Item.Items.Add(ItemInventoryRet?.FullName?.GetValue() + ":" + isTaxable); } break; case ENORItemRet.orirItemNonInventoryRet: { IItemNonInventoryRet ItemNonInventoryRet = orItemRet.ItemNonInventoryRet; isTaxable = ItemNonInventoryRet?.SalesTaxCodeRef?.FullName?.GetValue(); SetTaxableDefaultIfEmpty(ref isTaxable); cmboBx2_Item.Items.Add(ItemNonInventoryRet?.FullName?.GetValue() + ":" + isTaxable); } break; } } // for loop } // if // IY: Close the session and connection with QuickBooks sessionManager.EndSession(); booSessionBegun = false; sessionManager.CloseConnection(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString() + "\nStack Trace: \n" + ex.StackTrace + "\nExiting the application"); if (booSessionBegun) { sessionManager.EndSession(); sessionManager.CloseConnection(); } } }
private ESRI.ArcLogistics.DomainObjects.Order MakeOrderFromInvoice(IInvoiceRet invoiceRet, QBSessionManager session) { ESRI.ArcLogistics.DomainObjects.Order resultOrder = null; ICustomerRet customerRet = QueryCustomer(session, invoiceRet.CustomerRef.FullName.GetValue()); CapacitiesInfo capInfo = m_application.Project.CapacitiesInfo; OrderCustomPropertiesInfo propInfo = m_application.Project.OrderCustomPropertiesInfo; resultOrder = new ESRI.ArcLogistics.DomainObjects.Order(capInfo, propInfo); resultOrder.PlannedDate = m_application.CurrentDate; if (customerRet.ParentRef != null) { resultOrder.Name = customerRet.ParentRef.FullName.GetValue(); } else { resultOrder.Name = customerRet.FullName.GetValue(); } IAddress useAddress = null; if (customerRet.ShipAddress != null) { useAddress = customerRet.ShipAddress; } else if (customerRet.BillAddress != null) { useAddress = customerRet.BillAddress; } else { m_application.Messenger.AddWarning("No address for: " + resultOrder.Name); } if (useAddress != null) { if (useAddress.Addr2 != null) { resultOrder.Address.AddressLine = useAddress.Addr2.GetValue(); } else { resultOrder.Address.AddressLine = useAddress.Addr1.GetValue(); } resultOrder.Address.Locality3 = useAddress.City.GetValue(); resultOrder.Address.StateProvince = useAddress.State.GetValue(); resultOrder.Address.PostalCode1 = useAddress.PostalCode.GetValue(); AddressCandidate candidate = m_application.Geocoder.Geocode(resultOrder.Address); resultOrder.GeoLocation = candidate.GeoLocation; } // Look in the order custom properties for matching invoice detail items (by item description). // Look in the order capacities for matching item type custom fields. OrderCustomPropertiesInfo orderPropertiesInfo = resultOrder.CustomPropertiesInfo; OrderCustomProperties orderProperties = resultOrder.CustomProperties; CapacitiesInfo orderCapacitiesInfo = resultOrder.CapacitiesInfo; Capacities orderCapacities = resultOrder.Capacities; // Retrieve invoice line list // Each line can be either InvoiceLineRet OR InvoiceLineGroupRet IORInvoiceLineRetList orInvoiceLineRetList = invoiceRet.ORInvoiceLineRetList; if (orInvoiceLineRetList != null && (orderProperties.Count > 0 || orderCapacities.Count > 0)) { int lineCount = orInvoiceLineRetList.Count; for (int i = 0; i < lineCount; i++) { IORInvoiceLineRet orInvoiceLineRet = orInvoiceLineRetList.GetAt(i); // Check what to retrieve from the orInvoiceLineRet object // based on the "ortype" property. Skip summary lines. if (orInvoiceLineRet.ortype != ENORInvoiceLineRet.orilrInvoiceLineRet) { continue; } if (orInvoiceLineRet.InvoiceLineRet.ItemRef.FullName != null) { string itemName = orInvoiceLineRet.InvoiceLineRet.ItemRef.FullName.GetValue(); double itemQuantity = 0; if (orInvoiceLineRet.InvoiceLineRet.ItemRef != null) { itemQuantity = System.Convert.ToDouble(orInvoiceLineRet.InvoiceLineRet.Quantity.GetValue()); } // look for matching custom order property OrderCustomProperty orderPropertyInfoItem = null; for (int j = 0; j < orderPropertiesInfo.Count; j++) { orderPropertyInfoItem = orderPropertiesInfo.ElementAt(j) as OrderCustomProperty; if (orderPropertyInfoItem.Name == itemName) { if (orderPropertyInfoItem.Type == OrderCustomPropertyType.Numeric) { orderProperties[j] = itemQuantity; } else { orderProperties[j] = itemQuantity.ToString(); } break; } } // look for matching capacity // need to lookup item record so we get the extra field(s) // TODO: It might be a good idea to cache these locally to avoid // excess QB queries. IORItemRet orItemRet = QueryItem(session, itemName); IDataExtRetList custItemFieldsRetList = null; switch (orItemRet.ortype) { case ENORItemRet.orirItemServiceRet: { // orir prefix comes from OR + Item + Ret IItemServiceRet ItemServiceRet = orItemRet.ItemServiceRet; custItemFieldsRetList = ItemServiceRet.DataExtRetList; } break; case ENORItemRet.orirItemInventoryRet: { IItemInventoryRet ItemInventoryRet = orItemRet.ItemInventoryRet; custItemFieldsRetList = ItemInventoryRet.DataExtRetList; } break; case ENORItemRet.orirItemNonInventoryRet: { IItemNonInventoryRet ItemNonInventoryRet = orItemRet.ItemNonInventoryRet; custItemFieldsRetList = ItemNonInventoryRet.DataExtRetList; } break; } int custItemFieldCount = 0; if (custItemFieldsRetList != null) { custItemFieldCount = custItemFieldsRetList.Count; } for (int j = 0; j < custItemFieldCount; j++) { IDataExtRet custItemField = custItemFieldsRetList.GetAt(j); string custItemFieldName = custItemField.DataExtName.GetValue(); CapacityInfo orderCapacityInfoItem = null; for (int k = 0; k < orderCapacitiesInfo.Count; k++) { orderCapacityInfoItem = orderCapacitiesInfo.ElementAt(k); if (orderCapacityInfoItem.Name == custItemFieldName) { orderCapacities[k] += System.Convert.ToDouble(custItemField.DataExtValue.GetValue()) * itemQuantity; break; } } } } } } resultOrder.CustomProperties = orderProperties; resultOrder.Capacities = orderCapacities; return(resultOrder); }
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 WalkItemServiceRet(IItemServiceRet ItemServiceRet) { if (ItemServiceRet == null) { return; } //Go through all the elements of IItemServiceRetList //Get value of ListID string ListID45 = (string)ItemServiceRet.ListID.GetValue(); //Get value of TimeCreated DateTime TimeCreated46 = (DateTime)ItemServiceRet.TimeCreated.GetValue(); //Get value of TimeModified DateTime TimeModified47 = (DateTime)ItemServiceRet.TimeModified.GetValue(); //Get value of EditSequence string EditSequence48 = (string)ItemServiceRet.EditSequence.GetValue(); //Get value of Name string Name49 = (string)ItemServiceRet.Name.GetValue(); //Get value of FullName string FullName50 = (string)ItemServiceRet.FullName.GetValue(); //Get value of BarCodeValue if (ItemServiceRet.BarCodeValue != null) { string BarCodeValue51 = (string)ItemServiceRet.BarCodeValue.GetValue(); } //Get value of IsActive if (ItemServiceRet.IsActive != null) { bool IsActive52 = (bool)ItemServiceRet.IsActive.GetValue(); } if (ItemServiceRet.ClassRef != null) { //Get value of ListID if (ItemServiceRet.ClassRef.ListID != null) { string ListID53 = (string)ItemServiceRet.ClassRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.ClassRef.FullName != null) { string FullName54 = (string)ItemServiceRet.ClassRef.FullName.GetValue(); } } if (ItemServiceRet.ParentRef != null) { //Get value of ListID if (ItemServiceRet.ParentRef.ListID != null) { string ListID55 = (string)ItemServiceRet.ParentRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.ParentRef.FullName != null) { string FullName56 = (string)ItemServiceRet.ParentRef.FullName.GetValue(); } } //Get value of Sublevel int Sublevel57 = (int)ItemServiceRet.Sublevel.GetValue(); if (ItemServiceRet.UnitOfMeasureSetRef != null) { //Get value of ListID if (ItemServiceRet.UnitOfMeasureSetRef.ListID != null) { string ListID58 = (string)ItemServiceRet.UnitOfMeasureSetRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.UnitOfMeasureSetRef.FullName != null) { string FullName59 = (string)ItemServiceRet.UnitOfMeasureSetRef.FullName.GetValue(); } } if (ItemServiceRet.SalesTaxCodeRef != null) { //Get value of ListID if (ItemServiceRet.SalesTaxCodeRef.ListID != null) { string ListID60 = (string)ItemServiceRet.SalesTaxCodeRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.SalesTaxCodeRef.FullName != null) { string FullName61 = (string)ItemServiceRet.SalesTaxCodeRef.FullName.GetValue(); } } if (ItemServiceRet.ORSalesPurchase != null) { if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase != null) { if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase != null) { //Get value of Desc if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc != null) { string Desc62 = (string)ItemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue(); } if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice != null) { if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null) { //Get value of Price if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null) { double Price63 = (double)ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue(); } } if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.PricePercent != null) { //Get value of PricePercent if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.PricePercent != null) { double PricePercent64 = (double)ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.PricePercent.GetValue(); } } } if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.AccountRef != null) { //Get value of ListID if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.AccountRef.ListID != null) { string ListID65 = (string)ItemServiceRet.ORSalesPurchase.SalesOrPurchase.AccountRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.ORSalesPurchase.SalesOrPurchase.AccountRef.FullName != null) { string FullName66 = (string)ItemServiceRet.ORSalesPurchase.SalesOrPurchase.AccountRef.FullName.GetValue(); } } } } if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase != null) { if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase != null) { //Get value of SalesDesc if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.SalesDesc != null) { string SalesDesc67 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.SalesDesc.GetValue(); } //Get value of SalesPrice if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.SalesPrice != null) { double SalesPrice68 = (double)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.SalesPrice.GetValue(); } if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef != null) { //Get value of ListID if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.ListID != null) { string ListID69 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.FullName != null) { string FullName70 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.FullName.GetValue(); } } //Get value of PurchaseDesc if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PurchaseDesc != null) { string PurchaseDesc71 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PurchaseDesc.GetValue(); } //Get value of PurchaseCost if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PurchaseCost != null) { double PurchaseCost72 = (double)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PurchaseCost.GetValue(); } if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef != null) { //Get value of ListID if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.ListID != null) { string ListID73 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.FullName != null) { string FullName74 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.FullName.GetValue(); } } if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef != null) { //Get value of ListID if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.ListID != null) { string ListID75 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.ListID.GetValue(); } //Get value of FullName if (ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.FullName != null) { string FullName76 = (string)ItemServiceRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.FullName.GetValue(); } } } } } //Get value of ExternalGUID if (ItemServiceRet.ExternalGUID != null) { string ExternalGUID77 = (string)ItemServiceRet.ExternalGUID.GetValue(); } if (ItemServiceRet.DataExtRetList != null) { for (int i78 = 0; i78 < ItemServiceRet.DataExtRetList.Count; i78++) { IDataExtRet DataExtRet = ItemServiceRet.DataExtRetList.GetAt(i78); //Get value of OwnerID if (DataExtRet.OwnerID != null) { string OwnerID79 = (string)DataExtRet.OwnerID.GetValue(); } //Get value of DataExtName string DataExtName80 = (string)DataExtRet.DataExtName.GetValue(); //Get value of DataExtType ENDataExtType DataExtType81 = (ENDataExtType)DataExtRet.DataExtType.GetValue(); //Get value of DataExtValue string DataExtValue82 = (string)DataExtRet.DataExtValue.GetValue(); } } }
public IList <Invoice> GetInvoiceDetail() { bool sessionBegun = false; QBSessionManager sessionManager = null; var invoices = new List <Invoice>(); sessionManager = new QBSessionManager(); IMsgSetRequest requestMsgSet = null; var fromDate = new DateTime(2018, 1, 5); var toDate = new DateTime(2018, 1, 5); 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 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(), Memo = invoiceRet.Memo?.GetValue(), JobNumber = invoiceRet.Other?.GetValue(), CustomerName = invoiceRet.CustomerRef.FullName?.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) { 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(), Description = orInvoiceLineRet.InvoiceLineRet.Desc?.GetValue() }; Console.WriteLine($"j: {j}\tDescription: {invoiceItem.Description}"); 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); WalkItemServiceRet(itemRet.ItemServiceRet); 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); }