public SalesOrder Populate(string salesOrderNumber)
        {
            IMsgSetRequest requestMsgSet = _MySessionManager.CreateMsgSetRequest("US", 13, 0);

            ISalesOrderQuery soQuery = requestMsgSet.AppendSalesOrderQueryRq();

            soQuery.ORTxnNoAccountQuery.RefNumberList.Add(salesOrderNumber);

            soQuery.IncludeLineItems.SetValue(true);

            IMsgSetResponse responseMsgSet = _MySessionManager.DoRequests(requestMsgSet);

            IResponseList rsList = responseMsgSet.ResponseList;

            IResponse response = rsList.GetAt(0);

            ISalesOrderRetList SalesOrderList = (ISalesOrderRetList)response.Detail;

            if (SalesOrderList == null)
            {
                throw new Exception("Sales order not found.");
            }

            try
            {
                ISalesOrderRet QBSalesOrder = SalesOrderList.GetAt(0);

                _salesOrder          = new SalesOrder();
                _salesOrder.Number   = QBSalesOrder.RefNumber.GetValue();
                _salesOrder.Date     = QBSalesOrder.TxnDate.GetValue();
                _salesOrder.ShipDate = QBSalesOrder.ShipDate.GetValue();
                _salesOrder.Total    = QBSalesOrder.TotalAmount.GetAsString();

                _salesOrder.CustomerFullName = QBSalesOrder.CustomerRef.FullName.GetValue();

                Address address = new Address();
                _salesOrder.BillingAddress  = address.getAddress(QBSalesOrder.BillAddress);
                _salesOrder.ShippingAddress = address.getAddress(QBSalesOrder.ShipAddress);

                IORSalesOrderLineRetList SalesOrderItems = QBSalesOrder.ORSalesOrderLineRetList;

                if (SalesOrderItems != null)
                {
                    for (int i = 0; i <= SalesOrderItems.Count - 1; i++)
                    {
                        IORSalesOrderLineRet SalesOrderItem = SalesOrderItems.GetAt(i);

                        if (SalesOrderItem.ortype == ENORSalesOrderLineRet.orsolrSalesOrderLineRet)
                        {
                            InventoryItem inventoryItem = new InventoryItem();

                            inventoryItem.ItemCode = SalesOrderItem.SalesOrderLineRet.ItemRef.FullName.GetValue();

                            inventoryItem.Description = SalesOrderItem.SalesOrderLineRet.Desc.GetValue();

                            if (SalesOrderItem.SalesOrderLineRet.Quantity != null)
                            {
                                _salesOrder.TotalQty  += SalesOrderItem.SalesOrderLineRet.Quantity.GetValue();
                                inventoryItem.Quantity = SalesOrderItem.SalesOrderLineRet.Quantity.GetAsString();
                            }
                            else
                            {
                                inventoryItem.Quantity = "";
                            }

                            inventoryItem.Price  = SalesOrderItem.SalesOrderLineRet.ORRate.Rate.GetValue();
                            inventoryItem.Amount = SalesOrderItem.SalesOrderLineRet.Amount.GetAsString();
                            _salesOrder.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);
                    _salesOrder.InventoryItems = inventoryHelper.UpdateItemUPCAndListID(_salesOrder.InventoryItems);
                }

                return(_salesOrder);
            }
            catch (Exception)
            {
                throw new Exception("Failed to read Sales Order from QuickBooks.");
            }
        }
        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.");
            }
        }