private T ProcessQueryAsXML <T>(IMsgSetResponse queryResponse, string root)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            var           result     = default(T);

            try
            {
                if (_debug)
                {
                    using (FileStream fs = new FileStream(@"C:\Users\Computron\Documents\xml_response.xml", FileMode.OpenOrCreate))
                    {
                        byte[] info = new UTF8Encoding(true).GetBytes(queryResponse.ToXMLString());
                        fs.Write(info, 0, info.Length);
                    }
                }

                using (XmlReader reader = XmlReader.Create(new StringReader(queryResponse.ToXMLString())))
                {
                    reader.MoveToContent();
                    reader.ReadToDescendant(root);
                    result = (T)serializer.Deserialize(reader);
                }
            }
            catch (Exception ex)
            {
                throw new QuickBooksClientException(ex.ToString());
            }

            return(result);
        }
Beispiel #2
0
        private void WalkInvoiceAddRs(IMsgSetResponse responseMsgSet)
        {
            if (responseMsgSet == null)
            {
                return;
            }
            IResponseList responseList = responseMsgSet.ResponseList;

            if (responseList == null)
            {
                return;
            }
            for (int i = 0; i < responseList.Count; i++)
            {
                IResponse response = responseList.GetAt(i);
                if (response.StatusCode >= 0)
                {
                    if (response.Detail != null)
                    {
                        ENResponseType responseType = (ENResponseType)response.Type.GetValue();
                        if (responseType == ENResponseType.rtInvoiceAddRs)
                        {
                            IInvoiceRet invoiceRet = (IInvoiceRet)response.Detail;
                            WalkInvoiceRet(invoiceRet);
                        }
                    }
                }
            }
            Console.WriteLine(responseMsgSet.ToXMLString());
        }
        public void DoAccountQuery()
        {
            bool             sessionBegun   = false;
            bool             connectionOpen = false;
            QBSessionManager sessionManager = null;
            QBSessionMgr     QBMgr          = null;

            try
            {
                //Create the session Manager object
                QBMgr = new QBSessionMgr();
                QBMgr.CreateQBSession(out sessionManager);

                // Get the RequestMsgSet based on the correct QB Version
                IMsgSetRequest requestMsgSet = QBMgr.getLatestMsgSetRequest(sessionManager);

                requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;

                // BuildAccountQueryRq(requestMsgSet);

                IAccountQuery AccountQueryRq = requestMsgSet.AppendAccountQueryRq();

                // Uncomment the following to view and save the request and response XML
                // string requestXML = requestSet.ToXMLString();
                // MessageBox.Show(requestXML);

                //Send the request and get the response from QuickBooks
                IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet);

                string responseXML = responseMsgSet.ToXMLString();
                //MessageBox.Show(responseXML);

                WalkAccountQueryRs(responseMsgSet);

                //End the session and close the connection to QuickBooks
                //Close the session and connection with QuickBooks
                QBMgr.CloseQBConnection(sessionManager);
            }
            catch (Exception ex)
            {
                // MessageBox.Show(e.Message, "Error");
                // MessageBox.Show(ex.Message.ToString() + "\nStack Trace: \n" + ex.StackTrace + "\nExiting the application");
                if (sessionBegun)
                {
                    sessionManager.EndSession();
                }
                if (connectionOpen)
                {
                    sessionManager.CloseConnection();
                }
            }
            finally
            {
                QBMgr.CloseQBConnection(sessionManager);
            }
        }
 private IMsgSetResponse processRequestFromQB(IMsgSetRequest requestSet)
 {
     try
     {
         consoleOutput.AppendText("\r\n" + requestSet.ToXMLString());
         IMsgSetResponse responseSet = sessionManager.doRequest(true, ref requestSet);
         consoleOutput.AppendText("\r\n Response:" + responseSet.ToXMLString());
         return(responseSet);
     }
     catch (Exception e)
     {
         consoleOutput.AppendText("\r\n" + e.Message);
         return(null);
     }
 }
        private IMsgSetRequest Build_AddSalesReciept()
        {
            QBSessionManager sessionManager = new QBSessionManager();

            sessionManager.OpenConnection("", "Quickbooks Input Tool");
            sessionManager.BeginSession("", ENOpenMode.omDontCare);

            IMsgSetRequest requestMsgSet = getLatestMsgSetRequest(sessionManager);

            requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue;
            ISalesReceiptAdd salesRecieptAdd = requestMsgSet.AppendSalesReceiptAddRq();

            if (locationDropdown.Text != "")
            {
                salesRecieptAdd.CustomerRef.FullName.SetValue(locationDropdown.Text);
            }
            else
            {
                consoleOutput.AppendText("\r\nPlease select a customer");
                return(null);
            }

            if (fileStream != null)
            {
                try
                {
                    using (var fs = File.OpenRead(fileStream.ToString()))
                        using (var reader = new StreamReader(fs))
                        {
                            List <string> items = new List <string>();
                            List <string> qtys  = new List <string>();


                            while (!reader.EndOfStream)
                            {
                                var line   = reader.ReadLine();
                                var values = line.Split(',');
                                items.Add(values[0]);
                                qtys.Add(values[4]);
                            }
                            for (int i = 1; i < items.Count; i++)
                            {
                                if (getItemInfo(items[i]) != "")
                                {
                                    addBottleReturn(items[i]);
                                    IORSalesReceiptLineAdd salesRecieptLine = salesRecieptAdd.ORSalesReceiptLineAddList.Append();
                                    salesRecieptLine.SalesReceiptLineAdd.ItemRef.FullName.SetValue(getItemInfo(items[i]));
                                    salesRecieptLine.SalesReceiptLineAdd.Quantity.SetValue(Convert.ToDouble(qtys[i]));
                                    //salesRecieptLine.SalesReceiptLineAdd.ClassRef.FullName.SetValue("RETAIL STORE"); //Redstone only
                                }
                                else
                                {
                                    consoleOutput.AppendText("\r\nError finding " + items[i] + " in Quickbooks");
                                }
                            }
                            if (numLargeBottles != 0)
                            {
                                if (getItemInfo("BOTTLE DEPOSIT 20") != "")
                                {
                                    consoleOutput.AppendText("\r\nNumber of 20 cent bottles added: " + numLargeBottles);
                                    IORSalesReceiptLineAdd salesRecieptLine = salesRecieptAdd.ORSalesReceiptLineAddList.Append();
                                    salesRecieptLine.SalesReceiptLineAdd.ItemRef.FullName.SetValue(getItemInfo("BOTTLE DEPOSIT 20"));
                                    salesRecieptLine.SalesReceiptLineAdd.Quantity.SetValue(Convert.ToDouble(numLargeBottles));
                                    //salesRecieptLine.SalesReceiptLineAdd.ClassRef.FullName.SetValue("RETAIL STORE"); //Redstone only
                                }
                                else
                                {
                                    consoleOutput.AppendText("\r\nError finding BOTTLE DEPOSIT 20 in Quickbooks");
                                }
                            }
                            if (numSmallBottles != 0)
                            {
                                if (getItemInfo("BOTTLE DEPOSIT 10") != "")
                                {
                                    consoleOutput.AppendText("\r\nNumber of 10 cent bottles added: " + numLargeBottles);
                                    IORSalesReceiptLineAdd salesRecieptLine = salesRecieptAdd.ORSalesReceiptLineAddList.Append();
                                    salesRecieptLine.SalesReceiptLineAdd.ItemRef.FullName.SetValue(getItemInfo("BOTTLE DEPOSIT 10"));
                                    salesRecieptLine.SalesReceiptLineAdd.Quantity.SetValue(Convert.ToDouble(numSmallBottles));
                                    //salesRecieptLine.SalesReceiptLineAdd.ClassRef.FullName.SetValue("RETAIL STORE"); //Redstone only
                                }
                                else
                                {
                                    consoleOutput.AppendText("\r\nError finding BOTTLE DEPOSIT 10 in Quickbooks");
                                }
                            }
                        }
                }
                catch (Exception ex)
                {
                    consoleOutput.AppendText("\r\n" + ex);
                }
            }
            else
            {
                consoleOutput.AppendText("\r\nPlease select a file first");
                return(null);
            }
            try
            {
                IMsgSetResponse responseSet = sessionManager.DoRequests(requestMsgSet);
                consoleOutput.AppendText("\r\n" + responseSet.ToXMLString());
            }
            catch (Exception e)
            {
                consoleOutput.AppendText("\r\n this error " + e);
            }

            notFoundItems.ForEach(delegate(String item)
            {
                consoleOutput.AppendText("\r\n" + item + " was not found in Quickbooks");
            });
            groupItems.ForEach(delegate(String item)
            {
                consoleOutput.AppendText("\r\n" + item + " is a group item, it needs to be entered manually");
            });
            return(requestMsgSet);
        }