-
Notifications
You must be signed in to change notification settings - Fork 0
/
CraftUtil.cs
465 lines (437 loc) · 14.5 KB
/
CraftUtil.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Decompiled with JetBrains decompiler
// Type: WebOrderImportCraft.CraftUtil
// Assembly: WebOrderImportCraft, Version=1000.510.8894.24695, Culture=neutral, PublicKeyToken=bf11c4f15ab4e1ef
// MVID: F965BC17-D6AC-43D0-B104-3E53F0C380F8
// Assembly location: P:\Synergy\VMAWS Custom Apps\WebOrderImportCraft\WebOrderImportCraft.exe
using CraftCMS;
using MsgBox;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using WebOrderImportCraft.Properties;
namespace WebOrderImportCraft
{
internal class CraftUtil
{
public Binding binding = (Binding) null;
public string accessToken = (string) null;
internal static string ClientURL = Settings.Default.EComSite_Service + "?services=";
internal static string GetCustomerCustomAttr(object custWeb, string code) => (string) null;
internal static void SetCustomerCustomAttr(object custWeb, string code, string val)
{
}
internal object GetSalesExtensionAttr(object extAttrs) => (object) null;
internal object CustomerInfo(int id, object attributes)
{
object obj = (object) null;
try
{
}
catch (Exception ex)
{
int num = (int) MBox.Show("Get CustomerInfo Failed " + ex.ToString());
}
return obj;
}
internal bool CustomerUpdate(Customer customerData, string VisualCustID)
{
bool flag1 = false;
bool flag2 = false;
if (!string.IsNullOrWhiteSpace(VisualCustID))
{
try
{
string[] strArray1;
if (customerData == null)
{
strArray1 = (string[]) null;
}
else
{
User user = customerData.user;
if (user == null)
{
strArray1 = (string[]) null;
}
else
{
string visualId = user.visualId;
if (visualId == null)
strArray1 = (string[]) null;
else
strArray1 = visualId.Split(',');
}
}
string[] strArray2 = strArray1;
if (strArray2 != null)
{
foreach (string str in strArray2)
{
string strB = str?.Trim();
if (!string.IsNullOrWhiteSpace(VisualCustID) && !string.IsNullOrWhiteSpace(strB) && string.Compare(VisualCustID, strB, true) == 0)
{
flag2 = true;
break;
}
}
}
if (customerData.userId != null && !flag2)
{
string str = VisualCustID;
if (!string.IsNullOrWhiteSpace(customerData?.user?.visualId))
str = customerData.user.visualId + ", " + str;
flag1 = this.webPost(Settings.Default.EComSite_Service + "update-user?userId=" + customerData.userId, "{\"visualId\": \"" + str + "\"}", "PATCH") == "{\"success\":true}";
}
}
catch (Exception ex)
{
int num = (int) MBox.Show("CustomerUpdate Failed " + ex.ToString());
}
}
return flag1;
}
internal object[] customerGroupList() => new object[0];
internal CraftUtil.SearchFilterGroup[] AddFilter2(
CraftUtil.SearchFilterGroup[] filtresIn,
string key,
string op,
string value)
{
CraftUtil.Filter filter = new CraftUtil.Filter()
{
field = key,
conditionType = op,
value = value
};
CraftUtil.SearchFilterGroup searchFilterGroup = new CraftUtil.SearchFilterGroup();
searchFilterGroup.filters = new CraftUtil.Filter[1];
searchFilterGroup.filters[0] = filter;
List<CraftUtil.SearchFilterGroup> list = ((IEnumerable<CraftUtil.SearchFilterGroup>) (filtresIn ?? new CraftUtil.SearchFilterGroup[0])).ToList<CraftUtil.SearchFilterGroup>();
list.Add(searchFilterGroup);
return list.ToArray();
}
internal object[] Stores()
{
try
{
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.ToString());
}
return new object[0];
}
internal Order[] Orders(CraftUtil.SearchFilterGroup[] filterGroups)
{
CraftSalesOrders craftSalesOrders = new CraftSalesOrders();
EndpointAddress endpointAddress = new EndpointAddress(Settings.Default.EComSite_Service + "?services=salesOrderRepositoryV1");
try
{
craftSalesOrders = JsonConvert.DeserializeObject<CraftSalesOrders>(this.WebGet("orders" + this.QueryDecode(filterGroups)).Replace("\"options\":[],", "").Replace("\"sourceSnapshot\":[],", "").Replace("\"location\":[null,null,null],", "\"location\":null,"));
}
catch (Exception ex)
{
int num = (int) MBox.Show(filterGroups.ToString() + " Failed " + ex.ToString());
}
return craftSalesOrders?.orders;
}
internal Order OrderInfo(string id)
{
CraftSalesOrders craftSalesOrders = new CraftSalesOrders();
try
{
craftSalesOrders = JsonConvert.DeserializeObject<CraftSalesOrders>(this.WebGet("orders?id=" + id));
}
catch (Exception ex)
{
int num = (int) MBox.Show("Get OrderInfo Failed " + ex.ToString());
}
return craftSalesOrders?.orders?[0];
}
internal Quote[] Quotes(CraftUtil.SearchFilterGroup[] filterGroups)
{
CraftQuotes craftQuotes = new CraftQuotes();
try
{
craftQuotes = JsonConvert.DeserializeObject<CraftQuotes>(this.WebGet("quotes" + this.QueryDecode(filterGroups)).Replace("\"options\":[],", "").Replace("\"sourceSnapshot\":[],", "").Replace("\"location\":[null,null,null],", "\"location\":null,"));
}
catch (Exception ex)
{
int num = (int) MBox.Show(filterGroups.ToString() + " Failed " + ex.ToString());
}
return craftQuotes?.quotes;
}
internal Quote QuoteInfo(string number)
{
CraftQuotes craftQuotes = new CraftQuotes();
try
{
craftQuotes = JsonConvert.DeserializeObject<CraftQuotes>(this.WebGet("quotes?number=" + number));
}
catch (Exception ex)
{
int num = (int) MBox.Show("Get QuoteInfo Failed " + ex.ToString());
}
return craftQuotes?.quotes?[0];
}
internal bool updateQuoteStatus(string quoteID, string stage, string visualQuoteId)
{
bool flag = false;
string postURL = string.Empty;
string postData = string.Empty;
try
{
postURL = Settings.Default.EComSite_Service + "update-quote?quoteId=" + quoteID;
if (string.IsNullOrWhiteSpace(visualQuoteId))
postData = "{\"stage\": \"" + stage + "\"}";
else
postData = "{\"stage\": \"" + stage + "\",\"visualQuoteId\": \"" + visualQuoteId + "\"}";
flag = this.webPost(postURL, postData, "PATCH") == "{\"success\":true}";
}
catch (Exception ex)
{
int num = (int) MBox.Show("updateQuoteStatus Failed \r" + postURL + "\r" + postData + "\r" + ex.ToString());
}
return flag;
}
internal bool updateQuote(string quoteID, QuoteUpdateReq quote)
{
bool flag = false;
string postURL = string.Empty;
string postData = string.Empty;
try
{
postURL = Settings.Default.EComSite_Service + "update-quote?quoteId=" + quoteID;
postData = JsonConvert.SerializeObject((object) quote);
flag = this.webPost(postURL, postData, "PATCH") == "{\"success\":true}";
}
catch (Exception ex)
{
int num = (int) MBox.Show("updateOrderStatus Failed \r" + postURL + "\r" + postData + "\r" + ex.ToString());
}
return flag;
}
internal bool updateOrderStatus(string orderID, string status, string comment)
{
bool flag = false;
try
{
flag = this.webPost(Settings.Default.EComSite_Service + "update-order?orderId=" + orderID, "{\"visualId\": \"" + comment + "\", \"status\": \"" + status + "\"}", "PATCH") == "{\"success\":true}";
}
catch (Exception ex)
{
int num = (int) MBox.Show("updateOrderStatus Failed " + ex.ToString());
}
return flag;
}
internal string invoiceOrder(string orderID, string comment) => "";
internal bool updateShipmentTracking(string order, string trackingNo) => false;
internal object[] CountryList()
{
try
{
}
catch (Exception ex)
{
int num = (int) MBox.Show("Get Country List Failed " + ex.ToString());
}
return new object[0];
}
internal object[] RegionList(string id)
{
try
{
}
catch (Exception ex)
{
int num = (int) MBox.Show("Get Region List Failed " + ex.ToString());
}
return new object[0];
}
private string WebGet(string getURL)
{
HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(new Uri(new Uri(Settings.Default.EComSite_Service), getURL));
httpWebRequest.Method = "GET";
httpWebRequest.Accept = "application/xml";
string apikey = Settings.Default.APIKEY;
httpWebRequest.Headers.Add("Authorization", "Bearer " + apikey);
httpWebRequest.Accept = "text/html, application/xhtml+xml, application/xml, application/json, */*";
string str = string.Empty;
using (HttpWebResponse response = (HttpWebResponse) httpWebRequest.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
str = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
}
return str;
}
private string QueryDecode(CraftUtil.SearchFilterGroup[] filterGroups)
{
string str1 = string.Empty;
string empty1 = string.Empty;
string empty2 = string.Empty;
string str2 = "?";
foreach (CraftUtil.SearchFilterGroup filterGroup in filterGroups)
{
foreach (CraftUtil.Filter filter in filterGroup.filters)
{
string empty3 = string.Empty;
string str3;
switch (filter.conditionType?.ToLower())
{
case "eq":
str3 = "=";
break;
case "finset":
str3 = "X";
break;
case "from":
str3 = ">=";
break;
case "gt":
str3 = ">";
break;
case "gteq":
str3 = ">=";
break;
case "in":
str3 = "in";
break;
case "like":
str3 = "like";
break;
case "lt":
str3 = "<";
break;
case "lteq":
str3 = "<=";
break;
case "moreq":
str3 = ">=";
break;
case "neq":
str3 = "<>";
break;
case "nfinset":
str3 = "not_in";
break;
case "nin":
str3 = "not in";
break;
case "notnull":
str3 = "not_null";
break;
case "null":
str3 = "null";
break;
case "to":
str3 = "<=";
break;
default:
throw new Exception("\r\n\r\nBad Filter Condition: " + filter.conditionType + "\r\n\r\n");
}
if (str1 != string.Empty)
str2 = "&";
switch (filter.field)
{
case "created_at":
switch (filter.conditionType)
{
case "from":
empty1 = filter.value;
break;
case "to":
empty2 = filter.value;
break;
}
if (empty1 != string.Empty && empty2 != string.Empty)
{
str1 = str1 + str2 + "dateCreated=and,>=" + empty1.Replace(" 00:00:00", "") + ",<=" + empty2.Replace(" 00:00:00", "");
break;
}
break;
case "ordered_at":
switch (filter.conditionType)
{
case "from":
empty1 = filter.value;
break;
case "to":
empty2 = filter.value;
break;
}
if (empty1 != string.Empty && empty2 != string.Empty)
{
str1 = str1 + str2 + "dateOrdered=and,>=" + empty1.Replace(" 00:00:00", "") + ",<=" + empty2.Replace(" 00:00:00", "");
break;
}
break;
case "dateCompleted":
switch (filter.conditionType)
{
case "from":
empty1 = filter.value;
break;
case "to":
empty2 = filter.value;
break;
}
if (empty1 != string.Empty && empty2 != string.Empty)
{
str1 = str1 + str2 + "dateCompleted=and,>=" + empty1.Replace(" 00:00:00", "") + ",<=" + empty2.Replace(" 00:00:00", "");
break;
}
break;
default:
str1 = str1 + str2 + filter.field + str3 + filter.value;
break;
}
}
}
return str1;
}
private string webPost(string postURL, string postData, string method = "POST")
{
byte[] numArray = new byte[postData.Length];
byte[] bytes = new UTF8Encoding().GetBytes(postData);
CraftUtil.MyWebClient myWebClient = new CraftUtil.MyWebClient();
myWebClient.Headers.Add("accept", "text/xml, text/plain, application/json");
myWebClient.Headers.Add("Content-Type", "application/json");
myWebClient.Headers.Add("Cache-Control", "no-cache");
myWebClient.Headers.Add("Pragma", "no-cache");
string apikey = Settings.Default.APIKEY;
myWebClient.Headers.Add("Authorization", "Bearer " + apikey);
return Encoding.UTF8.GetString(myWebClient.UploadData(postURL, method, bytes));
}
private class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri uri)
{
WebRequest webRequest = base.GetWebRequest(uri);
webRequest.Timeout = 600000;
return webRequest;
}
}
public class OrderFilter
{
}
public class SearchFilterGroup
{
public CraftUtil.Filter[] filters;
}
public class Filter
{
public string field;
public string conditionType;
public string value;
}
}
}