-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateOrdersScheduledTask.cs
More file actions
456 lines (394 loc) · 18 KB
/
UpdateOrdersScheduledTask.cs
File metadata and controls
456 lines (394 loc) · 18 KB
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
using Dynamicweb.Content.Files.Information;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Connectors;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Extensibility.AddIns;
using Dynamicweb.Extensibility.Editors;
using Dynamicweb.SystemTools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.ScheduledTasks
{
[AddInName("LiveIntegrationUpdateOrders")]
[AddInLabel("Update orders based on custom fields")]
[AddInDescription("Update orders based on custom fields to update order state and this will generate emails notifications.")]
[AddInIgnore(false)]
[AddInUseParameterGrouping(true)]
public class UpdateOrdersScheduledTask : BatchIntegrationScheduledTaskAddinBase, IDropDownOptions
{
private string _processErrorForEmail;
public UpdateOrdersScheduledTask()
{
// default values
MaxOrdersToProcess = 25;
MinutesCompleted = 5;
UpdateCustomField = true;
NewState = "";
FilterState = "";
ForceStateUpdate = false;
NotificationEmailFailureOnly = true;
SkipCustomFieldUpdateOnErpFailure = false;
}
#region Parameters
#region A) General
[AddInParameter("Finished for X minutes")]
[AddInParameterEditor(typeof(IntegerNumberParameterEditor), "allowNegativeValues=false;NewGUI=true;")]
[AddInParameterGroup("A) General Filter")]
public int MinutesCompleted { get; set; }
[AddInParameter("Maximum orders per execution")]
[AddInParameterEditor(typeof(IntegerNumberParameterEditor), "allowNegativeValues=false;NewGUI=true;")]
[AddInParameterGroup("A) General Filter")]
public int MaxOrdersToProcess { get; set; }
[AddInParameter("State Filter")]
[AddInParameterEditor(typeof(DropDownParameterEditor), "NewGUI=true;multiple=true;SortBy=Key;height=160;width=250;")]
[AddInParameterGroup("A) General Filter")]
public string FilterState { get; set; }
#endregion
#region B) Fields to compare
[AddInParameter("Field A")]
[AddInParameterEditor(typeof(DropDownParameterEditor), "NewGUI=true;multiple=false;SortBy=Key")]
[AddInParameterGroup("B) Fields to compare")]
public string FieldA { get; set; }
[AddInParameter("Default value for NULL")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("B) Fields to compare")]
public string DefaultForNull { get; set; }
[AddInParameter("Value to compare (not equal)")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;infoText=This option takes precedence over Field B")]
[AddInParameterGroup("B) Fields to compare")]
public string ValueToCompare { get; set; }
[AddInParameter("Field B")]
[AddInParameterEditor(typeof(DropDownParameterEditor), "NewGUI=true;multiple=false;SortBy=Key")]
[AddInParameterGroup("B) Fields to compare")]
public string FieldB { get; set; }
#endregion
#region C) Action on update
[AddInParameter("Update custom fields (Field B is replaced by Field A)")]
[AddInParameterEditor(typeof(YesNoParameterEditor), "NewGUI=false")]
[AddInParameterGroup("C) Action on update")]
public bool UpdateCustomField { get; set; }
[AddInParameter("Order State")]
[AddInParameterEditor(typeof(DropDownParameterEditor), "NewGUI=true;none=false;multiple=false;SortBy=Key")]
[AddInParameterGroup("C) Action on update")]
public string NewState { get; set; }
[AddInParameter("Force State update")]
[AddInParameterEditor(typeof(YesNoParameterEditor), "NewGUI=false")]
[AddInParameterGroup("C) Action on update")]
public bool ForceStateUpdate { get; set; }
[AddInParameter("Value to update (success update => field A gets this value)")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("C) Action on update")]
public string ValueToUpdate { get; set; }
[AddInParameter("Skip Custom Field Update on ERP Failure")]
[AddInParameterEditor(typeof(YesNoParameterEditor), "NewGUI=false")]
[AddInParameterGroup("C) Action on update")]
public bool SkipCustomFieldUpdateOnErpFailure { get; set; }
#endregion
#region D) Notification
[AddInParameter("Subject")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("D) Notification")]
public string Subject { get; set; }
[AddInParameter("Sender Name")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("D) Notification")]
public string SenderName { get; set; }
[AddInParameter("Sender e-mail")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("D) Notification")]
public string SenderEmail { get; set; }
[AddInParameter("Send to")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("D) Notification")]
public string SendTo { get; set; }
[AddInParameter("Send email to customer")]
[AddInParameterEditor(typeof(YesNoParameterEditor), "NewGUI=false;")]
[AddInParameterGroup("D) Notification")]
public bool SendToCustomer { get; set; }
[AddInParameter("Email template")]
[AddInParameterEditor(typeof(TemplateParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("D) Notification")]
public string EmailTemplate { get; set; }
#endregion
#region E) Admin Notifications
[AddInParameter("Log all requests and responses")]
[AddInParameterEditor(typeof(YesNoParameterEditor), "NewGUI=false;")]
[AddInParameterGroup("E) Admin Notifications")]
public new bool LogRequestAndResponse { get; set; }
[AddInParameter("Notification recipient e-mail")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("E) Admin Notifications")]
public new string NotificationEmail { get; set; }
[AddInParameter("On failure only")]
[AddInParameterEditor(typeof(YesNoParameterEditor), "NewGUI=false;")]
[AddInParameterGroup("E) Admin Notifications")]
public new bool NotificationEmailFailureOnly { get; set; }
[AddInParameter("Notification sender e - mail")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("E) Admin Notifications")]
public new string NotificationEmailSenderEmail { get; set; }
[AddInParameter("Notification e-mail sender name")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("E) Admin Notifications")]
public new string NotificationEmailSenderName { get; set; }
[AddInParameter("Notification e-mail subject")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("E) Admin Notifications")]
public new string NotificationEmailSubject { get; set; }
[AddInParameter("Notification e-mail template")]
[AddInParameterEditor(typeof(TextParameterEditor), "NewGUI=true;")]
[AddInParameterGroup("E) Admin Notifications")]
public new string NotificationTemplate { get; set; }
#endregion
#endregion
/// <summary>
/// Main method in ScheduledTask Addin - is run when scheduled Task is run
/// </summary>
/// <exception cref="ArgumentException">When Field A property or Field B property is not selected</exception>
/// <returns></returns>
public override bool Run()
{
SetupLogging();
bool processResult = true;
_processErrorForEmail = "";
var settings = GetSettings();
try
{
// validate parameters
if (string.IsNullOrEmpty(FieldA))
{
throw new ArgumentException("Invalid field parameter!", "Field A");
}
if (string.IsNullOrEmpty(FieldB) && string.IsNullOrEmpty(ValueToCompare))
{
throw new Exception("Invalid parameters setup!");
}
// get the relevant data from the DB
var ordersToSync = ReadOrders();
if (ordersToSync != null && ordersToSync.Count > 0)
{
ProcessOrders(ordersToSync);
}
else
{
Logger.Log("No orders to be processed.");
}
}
catch (Exception ex)
{
_processErrorForEmail = ex.Message;
Logger.Log($"Error processing Update Orders job {ex.Message}\n {ex}");
}
finally
{
// handled errors during process.
if (_processErrorForEmail != "")
{
// Sendmail with error
processResult = false;
SendMailWithFrequency(_processErrorForEmail, settings, MessageType.Error);
}
else
{
// Send mail with success
SendMailWithFrequency("Scheduled task completed successfully", settings);
}
SetSuccessfulRun();
}
WriteTaskResultToLog(processResult);
return processResult;
}
private List<Order> ReadOrders()
{
OrderSearchFilter filter = new OrderSearchFilter
{
PageSize = MaxOrdersToProcess,
ToDate = DateTime.Now.AddMinutes(-1 * MinutesCompleted),
ShowUntransferred = true,
ShowNotExported = true,
Completed = OrderSearchFilter.CompletedStates.Completed
};
if (!string.IsNullOrEmpty(FilterState))
{
filter.OrderStateIds = FilterState.Split(',', StringSplitOptions.RemoveEmptyEntries);
}
//Todo: implement Custom Fields in OrderSearch
// var sbSql = new System.Text.StringBuilder();
// var fieldTypeId = Services.OrderFields.GetOrderFields().FirstOrDefault(of => of?.SystemName == FieldA)?.TypeId;
// var valueToCompare = string.IsNullOrEmpty(ValueToCompare) ? FieldB : GetCustomFieldSqlValue(fieldTypeId, ValueToCompare);
// sbSql.AppendFormat(
//@"SELECT top {0} *
//FROM EcomOrders
//WHERE OrderComplete = 1 AND OrderDeleted = 0
// and OrderCompletedDate < DATEADD(MINUTE, -{1}, GETDATE())
// and IsNull ({2}, {4}) != {3}"
// , MaxOrdersToProcess, MinutesCompleted, FieldA, valueToCompare, DefaultForNull);
return new OrderService().GetOrdersBySearch(filter)?.GetResultOrders()?.ToList();
}
private void ProcessOrders(List<Order> ordersToSync)
{
Logger.Log($"The list of order to run: {string.Join(",", ordersToSync.Select(x => x.Id))}");
foreach (var order in ordersToSync)
{
if (OrderHandler.IsOrderCurrentlyProcessing(order))
{
Logger.Log($"Skipping order: {order.Id} as it is currently processing");
continue;
}
var erpUpdated = SendToErp(order);
if (!SkipCustomFieldUpdateOnErpFailure || erpUpdated)
UpdateOrderCustomField(order);
}
Logger.Log("All orders done!");
}
private void UpdateOrderCustomField(Order order)
{
try
{
// update custom field (if wanted)
if (UpdateCustomField)
{
var fieldA = order.OrderFieldValues.First(of => of.OrderField.SystemName == FieldA);
var fieldB = order.OrderFieldValues.First(of => of.OrderField.SystemName == FieldB);
fieldB.Value = fieldA.Value;
}
else if (!string.IsNullOrEmpty(ValueToUpdate))
{
var fieldA = order.OrderFieldValues.First(of => of.OrderField.SystemName == FieldA);
fieldA.Value = ValueToUpdate;
}
// update order state
if (!string.IsNullOrWhiteSpace(NewState))
{
// check if it's needed to force the change state
if (order.StateId == NewState && ForceStateUpdate)
{
order.StateId = "";
}
order.StateId = NewState;
}
// save changes to fire notifications
Services.Orders.Save(order);
// send new notification from batch parameters
SendNotification(order);
Logger.Log($"Order Id {order.Id} done!");
}
catch (Exception ex)
{
var msg = $"Error in Update. Order Id = {order.Id}, Exception = {ex}";
_processErrorForEmail += $"{msg}\n";
Logger.Log(msg);
}
}
private bool SendToErp(Order order)
{
try
{
var settings = GetSettings(order.ShopId);
// save capture changes and try to update ERP
if (settings != null && settings.IsLiveIntegrationEnabled && Connector.IsWebServiceConnectionAvailable(settings, SubmitType.ScheduledTask))
{
var result = OrderHandler.UpdateOrder(settings, order, SubmitType.ScheduledTask);
if (result == null || !result.Value)
{
_processErrorForEmail += $"Order Id {order.Id} was not updated to the ERP.\n";
Logger.Log($"Order Id {order.Id} was not updated to the ERP.");
return false;
}
}
}
catch (Exception e)
{
var msg = $"Failure trying to update the ERP for Order Id {order.Id}: {e}";
_processErrorForEmail += $"{msg}\n";
Logger.Log(msg);
return false;
}
return true;
}
private void SendNotification(Order order)
{
// confirm that settings are valid to send
if (string.IsNullOrEmpty(Subject) || string.IsNullOrEmpty(SenderEmail) || string.IsNullOrEmpty(SendTo) && !SendToCustomer)
{
return;
}
if (string.IsNullOrWhiteSpace(SenderName))
{
SenderName = null;
}
// prepare data to send
var slTo = new List<string>();
var page = Dynamicweb.Frontend.PageView.GetPageviewByPageID(order.CheckoutPageId);
var template = new Rendering.Template(EmailTemplate.Replace("Templates/", ""));
if (!string.IsNullOrEmpty(SendTo))
{
slTo.AddRange(SendTo.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
}
if (SendToCustomer)
{
slTo.Add(order.CustomerEmail);
}
Services.Orders.SendEmail(order, ref page, Subject, SendTo, SenderEmail, SenderName, ref template);
}
/// <summary>
/// Gets options for the various drop down lists in this addin.
/// </summary>
/// <param name="name">The label of the control for which the options should be returned.</param>
/// <returns></returns>
public Hashtable GetOptions(string name)
{
var options = new Hashtable();
switch (name)
{
case "Field A":
case "Field B":
foreach (var field in Services.OrderFields.GetOrderFields().Where(of => of != null))
{
if (!options.ContainsKey(field.SystemName))
options.Add(field.SystemName, field.Name);
}
break;
case "Order State":
options.Add("", "Leave unchanged");
goto case "State Filter";
case "State Filter":
foreach (var state in Services.OrderStates.GetStatesByOrderType(OrderType.Order))
{
if (state.IsDeleted)
continue;
if (!options.ContainsKey(state.Id))
{
options.Add(state.Id, Helpers.GetStateLabel(state));
}
}
break;
}
return options;
}
private string GetCustomFieldSqlValue(int? fieldTypeId, string value)
{
int type = fieldTypeId != null ? fieldTypeId.Value : 0;
string result = value;
switch (type)
{
case 6://"integer
case 7://"double
return result;
case 3: //"checkbox"
result = Core.Converter.ToBoolean(value) ? "1" : "0";
break;
case 4: //"date"
case 5: // "datetime"
result = Core.Converter.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss.fff");
break;
default:
result = $"'{value}'";
break;
}
return result;
}
}
}