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 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.");
            }
        }