forked from ZochNet/QBMigrationTool
/
CustomerTypeDAL.cs
178 lines (149 loc) · 7.08 KB
/
CustomerTypeDAL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using rototrack_data_access;
using rototrack_model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace QBMigrationTool
{
class CustomerTypeDAL
{
public static XmlDocument BuildQueryRequest(string activeStatus, string fromModifiedDate, string toModifiedDate)
{
XmlDocument doc = XmlUtils.MakeRequestDocument();
XmlElement parent = XmlUtils.MakeRequestParentElement(doc);
XmlElement queryElement = doc.CreateElement("CustomerTypeQueryRq");
parent.AppendChild(queryElement);
queryElement.AppendChild(XmlUtils.MakeSimpleElem(doc, "ActiveStatus", activeStatus));
queryElement.AppendChild(XmlUtils.MakeSimpleElem(doc, "FromModifiedDate", fromModifiedDate));
queryElement.AppendChild(XmlUtils.MakeSimpleElem(doc, "ToModifiedDate", toModifiedDate));
return doc;
}
public static XmlDocument BuildAddRq(BillingInstruction bi)
{
XmlDocument doc = XmlUtils.MakeRequestDocument();
XmlElement parent = XmlUtils.MakeRequestParentElement(doc);
XmlElement CustomerTypeAddRq = doc.CreateElement("CustomerTypeAddRq");
parent.AppendChild(CustomerTypeAddRq);
XmlElement CustomerTypeAdd = doc.CreateElement("CustomerTypeAdd");
CustomerTypeAddRq.AppendChild(CustomerTypeAdd);
CustomerTypeAdd.AppendChild(XmlUtils.MakeSimpleElem(doc, "Name", bi.Name));
CustomerTypeAdd.AppendChild(XmlUtils.MakeSimpleElem(doc, "IsActive", "1"));
return doc;
}
public static void HandleResponse(string response)
{
WalkCustomerTypeQueryRs(response);
}
public static void HandleAddResponse(string response)
{
WalkCustomerTypeAddRs(response);
}
private static void WalkCustomerTypeQueryRs(string response)
{
//Parse the response XML string into an XmlDocument
XmlDocument responseXmlDoc = new XmlDocument();
responseXmlDoc.LoadXml(response);
//Get the response for our request
XmlNodeList CustomerTypeQueryRsList = responseXmlDoc.GetElementsByTagName("CustomerTypeQueryRs");
if (CustomerTypeQueryRsList.Count == 1) //Should always be true since we only did one request in this sample
{
XmlNode responseNode = CustomerTypeQueryRsList.Item(0);
//Check the status code, info, and severity
XmlAttributeCollection rsAttributes = responseNode.Attributes;
string statusCode = rsAttributes.GetNamedItem("statusCode").Value;
string statusSeverity = rsAttributes.GetNamedItem("statusSeverity").Value;
string statusMessage = rsAttributes.GetNamedItem("statusMessage").Value;
//status code = 0 all OK, > 0 is warning
if (Convert.ToInt32(statusCode) >= 0)
{
XmlNodeList CustomerTypeRetList = responseNode.SelectNodes("//CustomerTypeRet");//XPath Query
for (int i = 0; i < CustomerTypeRetList.Count; i++)
{
XmlNode CustomerTypeRet = CustomerTypeRetList.Item(i);
WalkCustomerTypeRet(CustomerTypeRet);
}
}
}
}
private static void WalkCustomerTypeRet(XmlNode CustomerTypeRet)
{
if (CustomerTypeRet == null) return;
BillingInstruction bi = null;
RotoTrackDb db = new RotoTrackDb();
string ListID = CustomerTypeRet.SelectSingleNode("./ListID").InnerText;
if (db.BillingInstructions.Any(f => f.QBListId == ListID))
{
bi = db.BillingInstructions.First(f => f.QBListId == ListID);
}
else
{
bi = new BillingInstruction();
db.BillingInstructions.Add(bi);
}
bi.QBListId = ListID;
string Name = CustomerTypeRet.SelectSingleNode("./Name").InnerText;
bi.Name = Name;
string FullName = CustomerTypeRet.SelectSingleNode("./FullName").InnerText;
string IsActive = "false";
if (CustomerTypeRet.SelectSingleNode("./IsActive") != null)
{
IsActive = CustomerTypeRet.SelectSingleNode("./IsActive").InnerText;
}
bi.IsActive = (IsActive == "true") ? true : false;
if (bi != null)
{
db.SaveChanges();
}
}
private static void WalkCustomerTypeAddRs(string response)
{
//Parse the response XML string into an XmlDocument
XmlDocument responseXmlDoc = new XmlDocument();
responseXmlDoc.LoadXml(response);
//Get the response for our request
XmlNodeList CustomerTypeAddRsList = responseXmlDoc.GetElementsByTagName("CustomerTypeAddRs");
if (CustomerTypeAddRsList.Count == 1) //Should always be true since we only did one request in this sample
{
XmlNode responseNode = CustomerTypeAddRsList.Item(0);
//Check the status code, info, and severity
XmlAttributeCollection rsAttributes = responseNode.Attributes;
string statusCode = rsAttributes.GetNamedItem("statusCode").Value;
string statusSeverity = rsAttributes.GetNamedItem("statusSeverity").Value;
string statusMessage = rsAttributes.GetNamedItem("statusMessage").Value;
// Check status and log any errors
QBUtils.CheckStatus(statusCode, statusSeverity, statusMessage);
//status code = 0 all OK, > 0 is warning
if (Convert.ToInt32(statusCode) >= 0)
{
XmlNodeList CustomerTypeRetList = responseNode.SelectNodes("//CustomerTypeRet");//XPath Query
for (int i = 0; i < CustomerTypeRetList.Count; i++)
{
XmlNode CustomerTypeRet = CustomerTypeRetList.Item(i);
WalkCustomerTypeRetForAdd(CustomerTypeRet);
}
}
}
}
private static void WalkCustomerTypeRetForAdd(XmlNode CustomerTypeRet)
{
// Update the QBListID for the newly added BillingInstruction
if (CustomerTypeRet == null) return;
BillingInstruction bi = null;
RotoTrackDb db = new RotoTrackDb();
string ListID = CustomerTypeRet.SelectSingleNode("./ListID").InnerText;
string Name = CustomerTypeRet.SelectSingleNode("./Name").InnerText;
if (db.BillingInstructions.Any(f => f.Name == Name))
{
bi = db.BillingInstructions.First(f => f.Name == Name);
}
bi.QBListId = ListID;
if (bi != null)
{
db.SaveChanges();
}
}
}
}