120 lines
4.2 KiB
C#
120 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using System.Xml.Linq;
|
|
using Interop.QBFC13;
|
|
using Interop.QBXMLRP2;
|
|
|
|
namespace BodyshopPartner.Utils
|
|
{
|
|
public static class QuickBooksInterop
|
|
{
|
|
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
private static string ticket;
|
|
private static RequestProcessor2 rp;
|
|
private static string maxVersion;
|
|
private static string companyFile = "";
|
|
private static QBFileMode mode = QBFileMode.qbFileOpenDoNotCare;
|
|
private static string appID = "ImEXBSP";
|
|
private static string appName = "BodyshopPartner";
|
|
|
|
public static void ConnectToQuickBooks()
|
|
{
|
|
try
|
|
{
|
|
rp = new RequestProcessor2Class();
|
|
rp.OpenConnection(appID, appName);
|
|
ticket = rp.BeginSession(companyFile, mode);
|
|
string[] versions = rp.get_QBXMLVersionsForSession(ticket);
|
|
maxVersion = versions[versions.Length - 1];
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Error(ex);
|
|
}
|
|
|
|
}
|
|
|
|
public static void DisconnectFromQuickBooks()
|
|
{
|
|
if (ticket != null)
|
|
{
|
|
try
|
|
{
|
|
rp.EndSession(ticket);
|
|
ticket = null;
|
|
rp.CloseConnection();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.Error(ex);
|
|
}
|
|
}
|
|
}
|
|
public static string ProcessQBXmlRequest(string request)
|
|
{
|
|
try
|
|
{
|
|
ConnectToQuickBooks();
|
|
var ret = rp.ProcessRequest(ticket, request);
|
|
DisconnectFromQuickBooks();
|
|
return ret;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.Error(e.Message);
|
|
DisconnectFromQuickBooks();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Models.QbXmlResponse ParseResponseXml(XDocument response)
|
|
{
|
|
XElement QueryResponse = response.Root.Descendants("QBXMLMsgsRs").Descendants().FirstOrDefault();
|
|
XAttribute StatusCode = QueryResponse.Attribute("statusCode");
|
|
XAttribute StatusSeverity = QueryResponse.Attribute("statusSeverity");
|
|
XAttribute statusMessage = QueryResponse.Attribute("statusMessage");
|
|
|
|
return new Models.QbXmlResponse()
|
|
{
|
|
StatusCode = System.Text.RegularExpressions.Regex.Unescape(StatusCode.Value),
|
|
StatusMessage = System.Text.RegularExpressions.Regex.Unescape(statusMessage.Value),
|
|
StatusSeverity = System.Text.RegularExpressions.Regex.Unescape(StatusSeverity.Value),
|
|
ResponseType = System.Text.RegularExpressions.Regex.Unescape(QueryResponse.Name.LocalName),
|
|
Response = response
|
|
};
|
|
//response.
|
|
}
|
|
|
|
//public static string[] parseInvoiceAddRs(string xml)
|
|
//{
|
|
// string[] retVal = new string[3];
|
|
// try
|
|
// {
|
|
// XmlNodeList RsNodeList = null;
|
|
// XmlDocument Doc = new XmlDocument();
|
|
// Doc.LoadXml(xml);
|
|
// RsNodeList = Doc.GetElementsByTagName("InvoiceAddRs");
|
|
// XmlAttributeCollection rsAttributes = RsNodeList.Item(0).Attributes;
|
|
// XmlNode statusCode = rsAttributes.GetNamedItem("statusCode");
|
|
// retVal[0] = Convert.ToString(statusCode.Value);
|
|
// XmlNode statusSeverity = rsAttributes.GetNamedItem("statusSeverity");
|
|
// retVal[1] = Convert.ToString(statusSeverity.Value);
|
|
// XmlNode statusMessage = rsAttributes.GetNamedItem("statusMessage");
|
|
// retVal[2] = Convert.ToString(statusMessage.Value);
|
|
// }
|
|
// catch (Exception e)
|
|
// {
|
|
// logger.Error("Error encountered when parsing Invoice info returned from QuickBooks: " + e.Message);
|
|
// retVal = null;
|
|
// }
|
|
// return retVal;
|
|
//}
|
|
|
|
|
|
}
|
|
}
|