Items WalkNonInventoryItem(IItemNonInventoryRet ItemRet)
        {
            if (ItemRet == null)
            {
                return(null);
            }
            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.IsActive.GetValue())
            {
                return(item);
            }
            else
            {
                return(null);
            }
        }
        QuickBooksProductMapDef GetMap(IItemNonInventoryRet item)
        {
            bool status = ReadBool(item.IsActive);
            QuickBooksProductMapDef result = new QuickBooksProductMapDef()
            {
                QuickBooksCode         = ReadString(item.Name),
                QuickBooksDescription  = ReadString(item.ORSalesPurchase.SalesOrPurchase.Desc),
                QuickBooksListId       = ReadQbId(item.ListID),
                QuickBooksParentCode   = item.ParentRef == null ? string.Empty : ReadString(item.ParentRef.FullName),
                QuickBooksParentListId = item.ParentRef == null ? string.Empty : ReadQbId(item.ParentRef.ListID),
                //Price = ReadPrice(item.ORSalesPurchase.SalesOrPurchase.ORPrice.Price),
                CreateDate   = DateTime.Now,
                UpdateDate   = DateTime.Now,
                CreatedUser  = context.UserName,
                UpdatedUser  = context.UserName,
                RecordStatus = status ? "A" : "P",
                Unit         = um.GetUnknownItem()
            };

            return(result);
        }
        MaestroProduct GetMaestroProduct(IItemNonInventoryRet item)
        {
            bool           status          = ReadBool(item.IsActive);
            string         qbID            = ReadQbId(item.ListID);
            MaestroProduct existingProduct = ProductCache.Instance.GetByQbId(qbID);

            MaestroProduct result = new MaestroProduct()
            {
                Name                 = ReadString(item.ORSalesPurchase.SalesOrPurchase.Desc),
                Description          = ReadString(item.Name),
                ProductGroup         = pgm.GetUnknownItem(),
                MinimumOrderQuantity = 0,
                Price                = ReadPrice(item.ORSalesPurchase.SalesOrPurchase.ORPrice.Price),
                QuickBooksProductId  = ReadQbId(item.ListID),
                CreateDate           = DateTime.Now,
                UpdateDate           = DateTime.Now,
                UpdatedUser          = context.UserName,
                CreatedUser          = context.UserName,
                RecordStatus         = status ? "A" : "P"
            };

            return(result);
        }
        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 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.");
            }
        }
        public override List <ITransactionEntity> Import()
        {
            StartSession();

            IMsgSetRequest         request = GetLatestMsgSetRequest();
            IItemNonInventoryQuery query   = request.AppendItemNonInventoryQueryRq();

            query.ORListQueryWithOwnerIDAndClass.ListWithClassFilter.ActiveStatus.SetValue(ENActiveStatus.asAll);
            //IItemQuery query = request.AppendItemQueryRq();
            IResponse res = GetResponse(request);

            //IORItemRetList returnList = res.Detail as IORItemRetList;
            IItemNonInventoryRetList returnList = res.Detail as IItemNonInventoryRetList;

            List <MaestroProduct> plist = new List <MaestroProduct>();

            List <IItemNonInventoryRet> mapSourceList = new List <IItemNonInventoryRet>();

            MaestroUnitType unknowUnitType = new UnitTypeManager(context).GetUnknownItem();


            int cnt = returnList.Count;

            for (int i = 0; i <= cnt - 1; i++)
            {
                IItemNonInventoryRet inv = returnList.GetAt(i);
                if (inv != null)
                {
                    string parentId = string.Empty;
                    if (inv.ParentRef != null)
                    {
                        parentId = ReadQbId(inv.ParentRef.ListID);
                    }

                    if (string.IsNullOrEmpty(parentId))
                    {
                        MaestroProduct product = GetMaestroProduct(inv);
                        plist.Add(product);
                    }

                    mapSourceList.Add(inv);//for late processing

                    /*
                     * if (ReadBool(inv.IsActive))
                     * {
                     *  string parentId = string.Empty;
                     *  if (inv.ParentRef != null)
                     *      parentId = ReadQbId(inv.ParentRef.ListID);
                     *
                     *  if (string.IsNullOrEmpty(parentId))
                     *  {
                     *      MaestroProduct product = GetMaestroProduct(inv);
                     *      plist.Add(product);
                     *  }
                     *
                     *  mapSourceList.Add(inv);//for late processing
                     * }
                     */
                    /*
                     * else
                     * {
                     *  string parentId = string.Empty;
                     *  if (inv.ParentRef != null)
                     *      parentId = ReadQbId(inv.ParentRef.ListID);
                     *
                     *  if (string.IsNullOrEmpty(parentId))
                     *  {
                     *      MaestroProduct product = GetMaestroProduct(inv);
                     *      //plist.Add(product);
                     *
                     *  }
                     *
                     *
                     *
                     *
                     * }*/
                }

                #region commented
                //WalkItemNonInventoryRet(inv);

                //IORItemRet qbc = returnList.GetAt(i);

                /*
                 * if (qbc.ItemNonInventoryRet != null)
                 * {
                 *  WalkItemNonInventoryRet(qbc.ItemNonInventoryRet);
                 *  //GetMaestroProduct(qbc.ItemNonInventoryRet);
                 * }*/
                #endregion commented
            }

            ExecuteBulkInsert(plist);
            ImportMaps(mapSourceList, plist);
            QuickBooksProductMapCache.Instance.Reload(true);

            return(plist.Cast <ITransactionEntity>().ToList());
        }
        void WalkItemNonInventoryRet(IItemNonInventoryRet ItemNonInventoryRet)
        {
            if (ItemNonInventoryRet == null)
            {
                return;
            }

            //Go through all the elements of IItemNonInventoryRetList
            //Get value of ListID
            string ListID48 = (string)ItemNonInventoryRet.ListID.GetValue();
            //Console.WriteLine(string.Format("ListID48:{0}", ListID48));
            //Get value of TimeCreated
            DateTime TimeCreated49 = (DateTime)ItemNonInventoryRet.TimeCreated.GetValue();
            //Console.WriteLine(string.Format("TimeCreated49:{0}", TimeCreated49));

            DateTime TimeModified50 = (DateTime)ItemNonInventoryRet.TimeModified.GetValue();
            //Get value of EditSequence
            string EditSequence51 = (string)ItemNonInventoryRet.EditSequence.GetValue();
            //Get value of Name
            string Name52 = (string)ItemNonInventoryRet.Name.GetValue();
            //Get value of FullName
            string FullName53 = (string)ItemNonInventoryRet.FullName.GetValue();
            //Get value of BarCodeValue
            string BarCodeValue54 = string.Empty;

            if (ItemNonInventoryRet.BarCodeValue != null)
            {
                BarCodeValue54 = (string)ItemNonInventoryRet.BarCodeValue.GetValue();
            }
            //Get value of IsActive
            bool IsActive55 = false;

            if (ItemNonInventoryRet.IsActive != null)
            {
                IsActive55 = (bool)ItemNonInventoryRet.IsActive.GetValue();
            }

            string ListID56   = string.Empty;
            string FullName57 = string.Empty;

            if (ItemNonInventoryRet.ClassRef != null)
            {
                //Get value of ListID
                if (ItemNonInventoryRet.ClassRef.ListID != null)
                {
                    ListID56 = (string)ItemNonInventoryRet.ClassRef.ListID.GetValue();
                }
                //Get value of FullName
                if (ItemNonInventoryRet.ClassRef.FullName != null)
                {
                    FullName57 = (string)ItemNonInventoryRet.ClassRef.FullName.GetValue();
                }
            }

            string ListID58   = string.Empty;
            string FullName59 = string.Empty;

            if (ItemNonInventoryRet.ParentRef != null)
            {
                //Get value of ListID
                if (ItemNonInventoryRet.ParentRef.ListID != null)
                {
                    ListID58 = (string)ItemNonInventoryRet.ParentRef.ListID.GetValue();
                }
                //Get value of FullName
                if (ItemNonInventoryRet.ParentRef.FullName != null)
                {
                    FullName59 = (string)ItemNonInventoryRet.ParentRef.FullName.GetValue();
                }
            }
            //Get value of Sublevel
            int Sublevel60 = (int)ItemNonInventoryRet.Sublevel.GetValue();
            //Get value of ManufacturerPartNumber
            string ManufacturerPartNumber61 = string.Empty;

            if (ItemNonInventoryRet.ManufacturerPartNumber != null)
            {
                ManufacturerPartNumber61 = (string)ItemNonInventoryRet.ManufacturerPartNumber.GetValue();
            }
            string ListID62, FullName63;

            if (ItemNonInventoryRet.UnitOfMeasureSetRef != null)
            {
                //Get value of ListID
                if (ItemNonInventoryRet.UnitOfMeasureSetRef.ListID != null)
                {
                    ListID62 = (string)ItemNonInventoryRet.UnitOfMeasureSetRef.ListID.GetValue();
                }
                //Get value of FullName
                if (ItemNonInventoryRet.UnitOfMeasureSetRef.FullName != null)
                {
                    FullName63 = (string)ItemNonInventoryRet.UnitOfMeasureSetRef.FullName.GetValue();
                }
            }
            string ListID64, FullName65;

            if (ItemNonInventoryRet.SalesTaxCodeRef != null)
            {
                //Get value of ListID
                if (ItemNonInventoryRet.SalesTaxCodeRef.ListID != null)
                {
                    ListID64 = (string)ItemNonInventoryRet.SalesTaxCodeRef.ListID.GetValue();
                }
                //Get value of FullName
                if (ItemNonInventoryRet.SalesTaxCodeRef.FullName != null)
                {
                    FullName65 = (string)ItemNonInventoryRet.SalesTaxCodeRef.FullName.GetValue();
                }
            }
            string Desc66, ListID69, FullName70;
            double Price67 = 0, PricePercent68 = 0;
            string SalesDesc71, ListID73, FullName74, PurchaseDesc75, ListID77;
            string FullName78, ListID79, FullName80;
            double SalesPrice72 = 0, PurchaseCost76 = 0;

            if (ItemNonInventoryRet.ORSalesPurchase != null)
            {
                if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase != null)
                {
                    if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase != null)
                    {
                        //Get value of Desc
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.Desc != null)
                        {
                            Desc66 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.Desc.GetValue();
                        }
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice != null)
                        {
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null)
                            {
                                //Get value of Price
                                if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price != null)
                                {
                                    Price67 = (double)ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.Price.GetValue();
                                }
                            }
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.PricePercent != null)
                            {
                                //Get value of PricePercent
                                if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.PricePercent != null)
                                {
                                    PricePercent68 = (double)ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.ORPrice.PricePercent.GetValue();
                                }
                            }
                        }
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.AccountRef != null)
                        {
                            //Get value of ListID
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.AccountRef.ListID != null)
                            {
                                ListID69 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.AccountRef.ListID.GetValue();
                            }
                            //Get value of FullName
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.AccountRef.FullName != null)
                            {
                                FullName70 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesOrPurchase.AccountRef.FullName.GetValue();
                            }
                        }
                    }
                }

                if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase != null)
                {
                    if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase != null)
                    {
                        //Get value of SalesDesc
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.SalesDesc != null)
                        {
                            SalesDesc71 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.SalesDesc.GetValue();
                        }
                        //Get value of SalesPrice
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.SalesPrice != null)
                        {
                            SalesPrice72 = (double)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.SalesPrice.GetValue();
                        }
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef != null)
                        {
                            //Get value of ListID
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.ListID != null)
                            {
                                ListID73 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.ListID.GetValue();
                            }
                            //Get value of FullName
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.FullName != null)
                            {
                                FullName74 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.FullName.GetValue();
                            }
                        }
                        //Get value of PurchaseDesc
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PurchaseDesc != null)
                        {
                            PurchaseDesc75 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PurchaseDesc.GetValue();
                        }
                        //Get value of PurchaseCost
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PurchaseCost != null)
                        {
                            PurchaseCost76 = (double)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PurchaseCost.GetValue();
                        }
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef != null)
                        {
                            //Get value of ListID
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.ListID != null)
                            {
                                ListID77 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.ListID.GetValue();
                            }
                            //Get value of FullName
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.FullName != null)
                            {
                                FullName78 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.FullName.GetValue();
                            }
                        }
                        if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef != null)
                        {
                            //Get value of ListID
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.ListID != null)
                            {
                                ListID79 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.ListID.GetValue();
                            }
                            //Get value of FullName
                            if (ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.FullName != null)
                            {
                                FullName80 = (string)ItemNonInventoryRet.ORSalesPurchase.SalesAndPurchase.PrefVendorRef.FullName.GetValue();
                            }
                        }
                    }
                }
            }
            //Get value of ExternalGUID
            string ExternalGUID81;

            if (ItemNonInventoryRet.ExternalGUID != null)
            {
                ExternalGUID81 = (string)ItemNonInventoryRet.ExternalGUID.GetValue();
            }
            if (ItemNonInventoryRet.DataExtRetList != null)
            {
                for (int i82 = 0; i82 < ItemNonInventoryRet.DataExtRetList.Count; i82++)
                {
                    IDataExtRet DataExtRet = ItemNonInventoryRet.DataExtRetList.GetAt(i82);
                    //Get value of OwnerID
                    if (DataExtRet.OwnerID != null)
                    {
                        string OwnerID83 = (string)DataExtRet.OwnerID.GetValue();
                    }
                    //Get value of DataExtName
                    string DataExtName84 = (string)DataExtRet.DataExtName.GetValue();
                    //Get value of DataExtType
                    ENDataExtType DataExtType85 = (ENDataExtType)DataExtRet.DataExtType.GetValue();
                    //Get value of DataExtValue
                    string DataExtValue86 = (string)DataExtRet.DataExtValue.GetValue();
                }
            }
        }
예제 #9
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);
        }