コード例 #1
0
        private IORItemRet QueryItem(QBSessionManager session, string itemFullName)
        {
            // query for the customer information

            IMsgSetRequest requestMsgSet = getLatestMsgSetRequest(session);

            requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;

            IItemQuery pItemQuery = requestMsgSet.AppendItemQueryRq();

            pItemQuery.ORListQuery.ListFilter.ORNameFilter.NameFilter.Name.SetValue(itemFullName);
            pItemQuery.ORListQuery.ListFilter.ORNameFilter.NameFilter.MatchCriterion.SetValue(ENMatchCriterion.mcContains);

            pItemQuery.OwnerIDList.Add("0");

            IMsgSetResponse responseMsgSet = session.DoRequests(requestMsgSet);

            // Uncomment the following to see the request and response XML for debugging
            //string rq = requestMsgSet.ToXMLString();
            //string rs = responseMsgSet.ToXMLString();

            //m_application.Messenger.AddInfo("Item Resquest: " + rq);
            //m_application.Messenger.AddInfo("Item Response: " + rs);

            // Interpret the response

            IResponseList rsList = responseMsgSet.ResponseList;

            //  Retrieve the one response corresponding to our single request

            IResponse response = rsList.GetAt(0);

            if (response.StatusCode != 0)
            {
                string msg = "";
                if (response.StatusCode == 1)  //No record found
                {
                    msg = "Item not found: " + itemFullName;
                }
                else
                {
                    msg = "Error getting item.  Status: " + response.StatusCode.ToString() + ", Message: " + response.StatusMessage;
                }

                throw new Exception(msg);
            }

            // We have one or more customers (expect one)

            IORItemRetList orItemRetList = response.Detail as IORItemRetList;

            int itemCount = orItemRetList.Count;

            if (itemCount > 1)
            {
                m_application.Messenger.AddWarning("Multiple items found: " + itemFullName);
            }

            return(orItemRetList.GetAt(0));
        }
コード例 #2
0
        private List <ItemQB> readItems(IORItemRetList itemRetList)
        {
            List <ItemQB> items = new List <ItemQB>();

            for (int i = 0; i < itemRetList.Count; i++)
            {
                IORItemRet cutomerRet = itemRetList.GetAt(i);
                ItemQB     item       = readItem.readItemFromCustomerRet(cutomerRet);
                items.Add(item);
            }
            return(items);
        }
コード例 #3
0
        /// <summary>
        /// Parses the returned item list for inventory items.
        /// </summary>
        /// <param name="itemList"></param>
        private async Task <ObservableCollection <InventoryItem> > ParseItemList(IORItemRetList itemList)
        {
            ObservableCollection <InventoryItem> items = new ObservableCollection <InventoryItem>();
            int itemCount = await GetCount("ItemQueryRq");

            for (int i = 0; i < itemCount; i++)
            {
                var item = itemList.GetAt(i);

                if (item.ItemInventoryRet != null && item.ItemInventoryRet.IsActive.GetValue())
                {
                    items.Add(ExtractInventoryItem(item.ItemInventoryRet));
                }
            }

            return(items);
        }
コード例 #4
0
        /// <summary>
        /// Processes the returned query response.
        /// </summary>
        /// <param name="queryResponse"></param>
        private async Task <ObservableCollection <InventoryItem> > ProcessItemQuery(IMsgSetResponse queryResponse)
        {
            ObservableCollection <InventoryItem> inventoryItems = null;
            IResponse response   = queryResponse.ResponseList.GetAt(0);
            int       statusCode = response.StatusCode;

            if (statusCode == 0)
            {
                if (response.Detail != null)
                {
                    ENResponseType resopnseType = (ENResponseType)response.Type.GetValue();
                    if (resopnseType == ENResponseType.rtItemQueryRs)
                    {
                        IORItemRetList itemList = response.Detail as IORItemRetList;
                        inventoryItems = await ParseItemList(itemList);
                    }
                }
            }

            return(inventoryItems);
        }
コード例 #5
0
        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();
                }
            }
        }
コード例 #6
0
        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);
                }
            }
        }
コード例 #7
0
        private string getItemInfo(string itemName)
        {
            //Cases when naming is different from square to qb
            if (itemName == "Gift Certificate- VIP Tour & Tasting")
            {
                itemName = "Gift Certificate - VIP tasting";
            }

            connectToQB();
            IMsgSetResponse responseMsgSet;

            try
            {
                responseMsgSet = processRequestFromQB(buildItemQueryRq(itemName));
            }
            catch (Exception ex)
            {
                consoleOutput.AppendText("\r\n" + ex);
                return("");
            }

            disconnectFromQB();
            IResponse      response = responseMsgSet.ResponseList.GetAt(0);
            IORItemRetList OR       = response.Detail as IORItemRetList;

            //check if item exists
            if (response.StatusCode == 1)
            {
                consoleOutput.AppendText("\r\n Could not find item:" + itemName);
                notFoundItems.Add(itemName);
                return("");
            }
            //group Items are a special case
            if (OR.GetAt(0).ItemGroupRet != null)
            {
                consoleOutput.AppendText("\r\n Group Item:" + itemName);
                groupItems.Add(itemName);
                return("");
            }

            var fullName = "";

            //grab fullname from qb, each type has to be accessed seperately
            if (OR.GetAt(0).ItemInventoryRet != null && OR.GetAt(0).ItemInventoryRet.FullName != null)
            {
                fullName = OR.GetAt(0).ItemInventoryRet.FullName.GetValue();
            }
            else if (OR.GetAt(0).ItemOtherChargeRet != null && OR.GetAt(0).ItemOtherChargeRet.FullName != null)
            {
                fullName = OR.GetAt(0).ItemOtherChargeRet.FullName.GetValue();
            }
            else if (OR.GetAt(0).ItemDiscountRet != null && OR.GetAt(0).ItemDiscountRet.FullName != null)
            {
                fullName = OR.GetAt(0).ItemDiscountRet.FullName.GetValue();
            }
            else if (OR.GetAt(0).ItemNonInventoryRet != null && OR.GetAt(0).ItemNonInventoryRet.FullName != null)
            {
                fullName = OR.GetAt(0).ItemNonInventoryRet.FullName.GetValue();
            }
            else if (OR.GetAt(0).ItemServiceRet != null && OR.GetAt(0).ItemServiceRet.FullName != null)
            {
                fullName = OR.GetAt(0).ItemServiceRet.FullName.GetValue();
            }
            else if (OR.GetAt(0).ItemGroupRet != null && OR.GetAt(0).ItemGroupRet.Name != null)
            {
                fullName = OR.GetAt(0).ItemGroupRet.Name.GetValue();
            }
            return(fullName);
        }
コード例 #8
0
        public List <InventoryItem> getAllItems()
        {
            try
            {
                //Item Request
                IMsgSetRequest itemRequestset = _MySessionManager.CreateMsgSetRequest("US", 13, 0);

                IItemQuery itemQuery = itemRequestset.AppendItemQueryRq();

                itemQuery.OwnerIDList.Add("0");

                ////Get item codes from sales order and add to item request
                //for (int i = 0; i < SalesOrderItems.Count; ++i)
                //{
                //    IORSalesOrderLineRet SalesOrderItem = SalesOrderItems.GetAt(i);
                //    itemQuery.ORListQuery.FullNameList.Add(SalesOrderItem.SalesOrderLineRet.ItemRef.FullName.GetValue());
                //}

                //itemQuery.ORListQuery.FullNameList.Add("17531");
                //itemQuery.ORListQuery.FullNameList.Add("17534");
                //itemQuery.ORListQuery.FullNameList.Add("17535");
                //itemQuery.ORListQuery.FullNameList.Add("17536");
                //itemQuery.ORListQuery.FullNameList.Add("17537");
                //itemQuery.ORListQuery.FullNameList.Add("17538");
                //itemQuery.ORListQuery.FullNameList.Add("62231");
                //itemQuery.ORListQuery.FullNameList.Add("12061");

                IMsgSetResponse responseItemRq = _MySessionManager.DoRequests(itemRequestset);

                IResponseList itemResponseList = responseItemRq.ResponseList;

                IResponse itemResponse = itemResponseList.GetAt(0);

                ENResponseType responseType = (ENResponseType)itemResponse.Type.GetValue();
                IORItemRetList QBItemList   = (IORItemRetList)itemResponse.Detail;

                for (int i = 0; i <= QBItemList.Count - 1; i++)
                {
                    InventoryItem   item = new InventoryItem();
                    IDataExtRetList customFieldsList;

                    if (QBItemList.GetAt(i).ItemNonInventoryRet != null)
                    {
                        IItemNonInventoryRet inventoryItem = QBItemList.GetAt(i).ItemNonInventoryRet;
                        customFieldsList = QBItemList.GetAt(i).ItemNonInventoryRet.DataExtRetList;

                        item.ListID = inventoryItem.ListID != null?inventoryItem.ListID.GetValue() : "";

                        item.ItemCode = inventoryItem.Name != null?inventoryItem.Name.GetValue() : "";

                        item.Description = inventoryItem.FullName.GetValue() != null?inventoryItem.FullName.GetValue() : "";

                        item.MPN = inventoryItem.ManufacturerPartNumber != null?inventoryItem.ManufacturerPartNumber.GetValue() : "";
                    }
                    else if (QBItemList.GetAt(i).ItemInventoryRet != null)
                    {
                        IItemInventoryRet inventoryItem = QBItemList.GetAt(i).ItemInventoryRet;
                        customFieldsList = QBItemList.GetAt(i).ItemInventoryRet.DataExtRetList;

                        item.ListID = inventoryItem.ListID != null?inventoryItem.ListID.GetValue() : "";

                        item.ItemCode = inventoryItem.Name != null?inventoryItem.Name.GetValue() : "";

                        item.Description = inventoryItem.SalesDesc != null?inventoryItem.SalesDesc.GetValue() : "";

                        item.MPN = inventoryItem.ManufacturerPartNumber != null?inventoryItem.ManufacturerPartNumber.GetValue() : "";

                        item.Price = inventoryItem.SalesPrice.GetValue();
                    }
                    else if (QBItemList.GetAt(i).ItemInventoryAssemblyRet != null)
                    {
                        IItemInventoryAssemblyRet inventoryItem = QBItemList.GetAt(i).ItemInventoryAssemblyRet;
                        customFieldsList = QBItemList.GetAt(i).ItemInventoryAssemblyRet.DataExtRetList;

                        item.ListID = inventoryItem.ListID != null?inventoryItem.ListID.GetValue() : "";

                        item.ItemCode = inventoryItem.Name != null?inventoryItem.Name.GetValue() : "";

                        item.Description = inventoryItem.SalesDesc.GetValue() != null?inventoryItem.SalesDesc.GetValue() : "";

                        item.MPN = inventoryItem.ManufacturerPartNumber != null?inventoryItem.ManufacturerPartNumber.GetValue() : "";
                    }
                    else
                    {
                        customFieldsList = null;
                    }

                    //get item external data (custom fields)
                    if (customFieldsList != null)
                    {
                        for (int iCustomField = 0; iCustomField <= customFieldsList.Count - 1; iCustomField++)
                        {
                            string fieldName  = customFieldsList.GetAt(iCustomField).DataExtName.GetValue();
                            string fieldValue = customFieldsList.GetAt(iCustomField).DataExtValue.GetValue();

                            if (fieldName == "Inner")
                            {
                                item.Inner = fieldValue;
                            }

                            if (fieldName == "Case")
                            {
                                item.Case = fieldValue;
                            }

                            if (fieldName == "Price2")
                            {
                                item.Price2 = Convert.ToDouble(fieldValue);
                            }

                            if (fieldName == "Volume")
                            {
                                item.Volume = fieldValue;
                            }

                            if (fieldName == "Price3")
                            {
                                item.Price3 = Convert.ToDouble(fieldValue);
                            }
                        }
                    }

                    if (item.ItemCode != null)
                    {
                        _inventoryItems.Add(item);
                    }
                }

                return(_inventoryItems);
            }
            catch (Exception)
            {
                throw new Exception("Failed to read Items from QuickBooks.");
            }
        }
コード例 #9
0
        private string[,] parseItemQueryRs(IMsgSetResponse responseMsgSet, int countOfRows, int arraySize)
        {
            /*
             * <?xml version="1.0" ?>
             * - <QBXML>
             * - <QBXMLMsgsRs>
             * - <ItemQueryRs requestID="2" statusCode="0" statusSeverity="Info" statusMessage="Status OK">
             * - <ItemServiceRet>
             *  <ListID>20000-933272655</ListID>
             *  <TimeCreated>1999-07-29T11:24:15-08:00</TimeCreated>
             *  <TimeModified>2007-12-15T11:32:53-08:00</TimeModified>
             *  <EditSequence>1197747173</EditSequence>
             *  <Name>Installation</Name>
             *  <FullName>Installation</FullName>
             *  <IsActive>true</IsActive>
             *  <Sublevel>0</Sublevel>
             * -    <SalesTaxCodeRef>
             *      <ListID>20000-999022286</ListID>
             *      <FullName>Non</FullName>
             *  </SalesTaxCodeRef>
             * -    <SalesOrPurchase>
             *      <Desc>Installation labor</Desc>
             *      <Price>35.00</Price>
             * -        <AccountRef>
             *          <ListID>190000-933270541</ListID>
             *          <FullName>Construction Income:Labor Income</FullName>
             *      </AccountRef>
             *  </SalesOrPurchase>
             * </ItemServiceRet>
             * </ItemQueryRs>
             * </QBXMLMsgsRs>
             * </QBXML>
             */

            string[,] retVal = new string[countOfRows, arraySize];
            IResponse response   = responseMsgSet.ResponseList.GetAt(0);
            int       statusCode = response.StatusCode;

            if (statusCode == 0)
            {
                if (response.Detail == null)
                {
                    return(null);
                }
                ENResponseType responseType = (ENResponseType)response.Type.GetValue();
                IORItemRetList OR           = null;
                if (responseType == ENResponseType.rtItemQueryRs)
                {
                    OR = response.Detail as IORItemRetList;
                }
                else
                {
                    return(null);
                }
                for (int i = 0; i < countOfRows; i++)
                {
                    if (OR.GetAt(i) == null)
                    {
                        break;
                    }
                    if (OR.GetAt(i).ItemServiceRet != null)
                    {
                        string fullName = null, desc = null;
                        double price = 0.0;

                        if (OR.GetAt(i).ItemServiceRet.FullName != null)
                        {
                            fullName = OR.GetAt(i).ItemServiceRet.FullName.GetValue();
                            populateRetVal(ref retVal, i, 0, "fullName", fullName);
                        }
                        if (OR.GetAt(i).ItemServiceRet.ORSalesPurchase != null)
                        {
                            if (OR.GetAt(i).ItemServiceRet.ORSalesPurchase.SalesOrPurchase != null)
                            {
                                //Get value of Desc
                                if (OR.GetAt(i).ItemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc != null)
                                {
                                    desc = (string)OR.GetAt(i).ItemServiceRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();
                                    populateRetVal(ref retVal, i, 0, "desc", desc);
                                }
                                if (OR.GetAt(i).ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice != null)
                                {
                                    if (OR.GetAt(i).ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null)
                                    {
                                        //Get value of Price
                                        price = (double)OR.GetAt(i).ItemServiceRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue();
                                        populateRetVal(ref retVal, i, 1, "price", price.ToString());
                                    }
                                }
                            }
                        }
                    }

                    if (OR.GetAt(i).ItemNonInventoryRet != null)
                    {
                        string fullName = null, desc = null;
                        double price = 0.0;

                        if (OR.GetAt(i).ItemNonInventoryRet.FullName != null)
                        {
                            fullName = OR.GetAt(i).ItemNonInventoryRet.FullName.GetValue();
                            populateRetVal(ref retVal, i, 0, "fullName", fullName);
                        }
                        if (OR.GetAt(i).ItemNonInventoryRet.ORSalesPurchase != null)
                        {
                            if (OR.GetAt(i).ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase != null)
                            {
                                //Get value of Desc
                                if (OR.GetAt(i).ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.Desc != null)
                                {
                                    desc = (string)OR.GetAt(i).ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();
                                    populateRetVal(ref retVal, i, 0, "desc", desc);
                                }
                                if (OR.GetAt(i).ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice != null)
                                {
                                    if (OR.GetAt(i).ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null)
                                    {
                                        //Get value of Price
                                        price = (double)OR.GetAt(i).ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue();
                                        populateRetVal(ref retVal, i, 1, "price", price.ToString());
                                    }
                                }
                            }
                        }
                    }
                    if (OR.GetAt(i).ItemOtherChargeRet != null)
                    {
                        string fullName = null, desc = null;
                        double price = 0.0;

                        if (OR.GetAt(i).ItemOtherChargeRet.FullName != null)
                        {
                            fullName = OR.GetAt(i).ItemOtherChargeRet.FullName.GetValue();
                            populateRetVal(ref retVal, i, 0, "fullName", fullName);
                        }
                        if (OR.GetAt(i).ItemOtherChargeRet.ORSalesPurchase != null)
                        {
                            if (OR.GetAt(i).ItemOtherChargeRet.ORSalesPurchase.SalesOrPurchase != null)
                            {
                                //Get value of Desc
                                if (OR.GetAt(i).ItemOtherChargeRet.ORSalesPurchase.SalesOrPurchase.Desc != null)
                                {
                                    desc = (string)OR.GetAt(i).ItemOtherChargeRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();
                                    populateRetVal(ref retVal, i, 0, "desc", desc);
                                }
                                if (OR.GetAt(i).ItemOtherChargeRet.ORSalesPurchase.SalesOrPurchase.ORPrice != null)
                                {
                                    if (OR.GetAt(i).ItemOtherChargeRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null)
                                    {
                                        //Get value of Price
                                        price = (double)OR.GetAt(i).ItemOtherChargeRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue();
                                        populateRetVal(ref retVal, i, 1, "price", price.ToString());
                                    }
                                }
                            }
                        }
                    }
                    if (OR.GetAt(i).ItemInventoryRet != null)
                    {
                        string fullName = null, desc = null;
                        double price = 0.0;

                        if (OR.GetAt(i).ItemInventoryRet.FullName != null)
                        {
                            fullName = OR.GetAt(i).ItemInventoryRet.FullName.GetValue();
                            populateRetVal(ref retVal, i, 0, "fullName", fullName);
                        }
                        if (OR.GetAt(i).ItemInventoryRet.SalesDesc != null)
                        {
                            //Get value of Desc
                            desc = (string)OR.GetAt(i).ItemInventoryRet.SalesDesc.GetValue();
                            populateRetVal(ref retVal, i, 0, "desc", desc);
                        }
                        if (OR.GetAt(i).ItemInventoryRet.SalesPrice != null)
                        {
                            //Get value of Price
                            price = (double)OR.GetAt(i).ItemInventoryRet.SalesPrice.GetValue();
                            populateRetVal(ref retVal, i, 1, "price", price.ToString());
                        }
                    }
                    if (OR.GetAt(i).ItemInventoryAssemblyRet != null)
                    {
                        string fullName = null, desc = null;
                        double price = 0.0;

                        if (OR.GetAt(i).ItemInventoryAssemblyRet.FullName != null)
                        {
                            fullName = OR.GetAt(i).ItemInventoryAssemblyRet.FullName.GetValue();
                            populateRetVal(ref retVal, i, 0, "fullName", fullName);
                        }
                        if (OR.GetAt(i).ItemInventoryAssemblyRet.SalesDesc != null)
                        {
                            //Get value of Desc
                            desc = (string)OR.GetAt(i).ItemInventoryAssemblyRet.SalesDesc.GetValue();
                            populateRetVal(ref retVal, i, 0, "desc", desc);
                        }
                        if (OR.GetAt(i).ItemInventoryAssemblyRet.SalesPrice != null)
                        {
                            //Get value of Price
                            price = (double)OR.GetAt(i).ItemInventoryAssemblyRet.SalesPrice.GetValue();
                            populateRetVal(ref retVal, i, 1, "price", price.ToString());
                        }
                    }
                }
            }
            return(retVal);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        public List <InventoryItem> UpdateItemUPCAndListID(List <InventoryItem> inventoryItems)
        {
            //Item Request
            IMsgSetRequest itemRequestset = _MySessionManager.CreateMsgSetRequest("US", 13, 0);

            IItemQuery itemQuery = itemRequestset.AppendItemQueryRq();

            itemQuery.OwnerIDList.Add("0");

            //Get item codes from sales order and add to item request
            foreach (InventoryItem item in inventoryItems)
            {
                itemQuery.ORListQuery.FullNameList.Add(item.ItemCode);
            }

            IMsgSetResponse responseItemRq = _MySessionManager.DoRequests(itemRequestset);

            IResponseList itemResponseList = responseItemRq.ResponseList;

            IResponse itemResponse = itemResponseList.GetAt(0);

            ENResponseType responseType = (ENResponseType)itemResponse.Type.GetValue();
            IORItemRetList QBItemList   = (IORItemRetList)itemResponse.Detail;

            for (int i = 0; i <= QBItemList.Count - 1; i++)
            {
                if (QBItemList.GetAt(i).ItemNonInventoryRet != null)
                {
                    if (QBItemList.GetAt(i).ItemNonInventoryRet.Name != null)
                    {
                        string itemCode = QBItemList.GetAt(i).ItemNonInventoryRet.Name.GetValue();

                        if (QBItemList.GetAt(i).ItemNonInventoryRet.ManufacturerPartNumber != null)
                        {
                            inventoryItems.First(a => a.ItemCode == itemCode).MPN = QBItemList.GetAt(i).ItemNonInventoryRet.ManufacturerPartNumber.GetValue();
                        }
                        if (QBItemList.GetAt(i).ItemNonInventoryRet.ListID != null)
                        {
                            inventoryItems.First(a => a.ItemCode == itemCode).ListID = QBItemList.GetAt(i).ItemNonInventoryRet.ListID.GetValue();
                        }
                    }
                }

                if (QBItemList.GetAt(i).ItemInventoryRet != null)
                {
                    IItemInventoryRet iInventory = QBItemList.GetAt(i).ItemInventoryRet;

                    //get item external data (custom fields)
                    //IDataExtRetList dataExtRetList = OR.GetAt(i).ItemInventoryRet.DataExtRetList;
                    //if (dataExtRetList != null)
                    //{
                    //    Console.WriteLine(dataExtRetList.Count);
                    //    IDataExtRet dataExtRet = dataExtRetList.GetAt(0);
                    //    Console.WriteLine(dataExtRet.DataExtName.GetValue() + " === " + dataExtRet.DataExtValue.GetValue());
                    //}

                    if (QBItemList.GetAt(i).ItemInventoryRet.Name != null)
                    {
                        string itemCode = QBItemList.GetAt(i).ItemInventoryRet.Name.GetValue();

                        if (QBItemList.GetAt(i).ItemInventoryRet.ManufacturerPartNumber != null)
                        {
                            inventoryItems.First(a => a.ItemCode == itemCode).MPN = QBItemList.GetAt(i).ItemInventoryRet.ManufacturerPartNumber.GetValue();
                        }

                        if (QBItemList.GetAt(i).ItemInventoryRet.ListID != null)
                        {
                            inventoryItems.First(a => a.ItemCode == itemCode).ListID = QBItemList.GetAt(i).ItemInventoryRet.ListID.GetValue();
                        }
                    }
                }

                if (QBItemList.GetAt(i).ItemInventoryAssemblyRet != null)
                {
                    if (QBItemList.GetAt(i).ItemInventoryAssemblyRet.Name != null)
                    {
                        string itemCode = QBItemList.GetAt(i).ItemInventoryAssemblyRet.Name.GetValue();

                        if (QBItemList.GetAt(i).ItemInventoryAssemblyRet.ManufacturerPartNumber != null)
                        {
                            inventoryItems.First(a => a.ItemCode == itemCode).MPN = QBItemList.GetAt(i).ItemInventoryAssemblyRet.ManufacturerPartNumber.GetValue();
                        }

                        if (QBItemList.GetAt(i).ItemInventoryAssemblyRet.ListID != null)
                        {
                            inventoryItems.First(a => a.ItemCode == itemCode).ListID = QBItemList.GetAt(i).ItemInventoryAssemblyRet.ListID.GetValue();
                        }
                    }
                }

                //if (item.ItemCode != null)
                //    _inventoryItems.Add(item);
            }

            return(inventoryItems);
        }