|
|
@@ -1,26 +1,21 @@
|
|
1
|
1
|
using System;
|
|
2
|
2
|
using System.Collections.Generic;
|
|
3
|
3
|
using System.ComponentModel;
|
|
4
|
|
-using System.Data;
|
|
5
|
|
-using System.Drawing;
|
|
6
|
|
-using System.Linq;
|
|
7
|
|
-using System.Text;
|
|
8
|
|
-using System.Threading.Tasks;
|
|
9
|
|
-using System.Windows.Forms;
|
|
10
|
|
-using System.IO;
|
|
11
|
|
-using System.Net;
|
|
12
|
|
-using Newtonsoft.Json;
|
|
13
|
|
-using Newtonsoft.Json.Serialization;
|
|
14
|
4
|
using System.Data.SQLite;
|
|
15
|
|
-using System.Threading;
|
|
|
5
|
+using System.Diagnostics;
|
|
|
6
|
+using System.Drawing;
|
|
16
|
7
|
using System.Globalization;
|
|
|
8
|
+using System.Threading;
|
|
|
9
|
+using System.Windows.Forms;
|
|
17
|
10
|
|
|
18
|
11
|
namespace BulkPrinting
|
|
19
|
12
|
{
|
|
20
|
13
|
public partial class BatchForm : ObservedForm
|
|
21
|
14
|
{
|
|
22
|
|
- EventWaitHandle LoadingThreadEvent;
|
|
23
|
15
|
Thread LoadingThread;
|
|
|
16
|
+ readonly object LoadingThreadLock = new object();
|
|
|
17
|
+ bool CheckForNewBatches;
|
|
|
18
|
+ bool RetryDownloads;
|
|
24
|
19
|
|
|
25
|
20
|
public BatchForm()
|
|
26
|
21
|
{
|
|
|
@@ -38,32 +33,34 @@ namespace BulkPrinting
|
|
38
|
33
|
{
|
|
39
|
34
|
Dictionary<int, BatchEvent> BatchEvents = new Dictionary<int, BatchEvent>();
|
|
40
|
35
|
string Sql = "Select COUNT(v.BatchId) AS Total, v.BatchId,l.EventType,l.Retry FROM Voucher v LEFT JOIN Logs l on v.Id = l.VoucherId WHERE l.EventType in (@printevent,@exportevent) GROUP BY v.BatchId,l.EventType,l.Retry ORDER BY BatchId";
|
|
41
|
|
- SQLiteCommand Command = new SQLiteCommand(Sql, Globals.DBConnection);
|
|
42
|
|
- Command.Parameters.Add(new SQLiteParameter("@printevent", VendorEvent.VendorEventType.PrintVoucher));
|
|
43
|
|
- Command.Parameters.Add(new SQLiteParameter("@exportevent", VendorEvent.VendorEventType.ExportVoucher));
|
|
44
|
|
- using (SQLiteDataReader read = Command.ExecuteReader())
|
|
|
36
|
+ using (var Command = new SQLiteCommand(Sql, Globals.DBConnection))
|
|
45
|
37
|
{
|
|
46
|
|
- while (read.Read())
|
|
|
38
|
+ Command.Parameters.AddWithValue("@printevent", VendorEvent.VendorEventType.PrintVoucher);
|
|
|
39
|
+ Command.Parameters.AddWithValue("@exportevent", VendorEvent.VendorEventType.ExportVoucher);
|
|
|
40
|
+ using (SQLiteDataReader read = Command.ExecuteReader())
|
|
47
|
41
|
{
|
|
48
|
|
- int BatchId = int.Parse(read["BatchId"].ToString());
|
|
49
|
|
- if (!BatchEvents.ContainsKey(BatchId))
|
|
50
|
|
- {
|
|
51
|
|
- BatchEvents.Add(BatchId, new BatchEvent());
|
|
52
|
|
- }
|
|
53
|
|
- if (read["EventType"].ToString() == "6")
|
|
|
42
|
+ while (read.Read())
|
|
54
|
43
|
{
|
|
55
|
|
- if (read["Retry"].ToString() == "True")
|
|
|
44
|
+ int BatchId = int.Parse(read["BatchId"].ToString());
|
|
|
45
|
+ if (!BatchEvents.ContainsKey(BatchId))
|
|
56
|
46
|
{
|
|
57
|
|
- BatchEvents[BatchId].ReprintCount += int.Parse(read["Total"].ToString());
|
|
|
47
|
+ BatchEvents.Add(BatchId, new BatchEvent());
|
|
58
|
48
|
}
|
|
59
|
|
- else
|
|
|
49
|
+ if (read["EventType"].ToString() == "6")
|
|
60
|
50
|
{
|
|
61
|
|
- BatchEvents[BatchId].PrintCount += int.Parse(read["Total"].ToString());
|
|
|
51
|
+ if (read["Retry"].ToString() == "True")
|
|
|
52
|
+ {
|
|
|
53
|
+ BatchEvents[BatchId].ReprintCount += int.Parse(read["Total"].ToString());
|
|
|
54
|
+ }
|
|
|
55
|
+ else
|
|
|
56
|
+ {
|
|
|
57
|
+ BatchEvents[BatchId].PrintCount += int.Parse(read["Total"].ToString());
|
|
|
58
|
+ }
|
|
|
59
|
+ }
|
|
|
60
|
+ else if (read["EventType"].ToString() == "8")
|
|
|
61
|
+ {
|
|
|
62
|
+ BatchEvents[BatchId].ExportCount += int.Parse(read["Total"].ToString());
|
|
62
|
63
|
}
|
|
63
|
|
- }
|
|
64
|
|
- else if (read["EventType"].ToString() == "8")
|
|
65
|
|
- {
|
|
66
|
|
- BatchEvents[BatchId].ExportCount += int.Parse(read["Total"].ToString());
|
|
67
|
64
|
}
|
|
68
|
65
|
}
|
|
69
|
66
|
}
|
|
|
@@ -83,95 +80,105 @@ namespace BulkPrinting
|
|
83
|
80
|
if (CanOrder) dgvBatches.Columns.Add("RequestedQuantity", "Quantity Ordered");
|
|
84
|
81
|
dgvBatches.Columns.Add("DeliveredQuantity", "Quantity Delivered");
|
|
85
|
82
|
if (CanOrder) dgvBatches.Columns.Add("ReadyForDownload", "Batch Ready for Download");
|
|
|
83
|
+ dgvBatches.Columns.Add("Downloaded", "Downloaded");
|
|
86
|
84
|
dgvBatches.Columns.Add("PrintedQuantity", "Quantity Printed");
|
|
87
|
85
|
if (CanExport) dgvBatches.Columns.Add("Exported", "Exported Batch");
|
|
88
|
|
- Sql = "Select Id,OrderDate,OrderReference,InternalReference,NetworkName,ProductDescription,VoucherType,FaceValue,RequestedQuantity,DeliveredQuantity,ReadyForDownload From Batch WHERE OrderDate BETWEEN @startdate AND @enddate";
|
|
89
|
|
-
|
|
|
86
|
+ Sql = "Select Id,OrderDate,OrderReference,InternalReference,NetworkName,ProductDescription,VoucherType,FaceValue,RequestedQuantity,DeliveredQuantity,ReadyForDownload,Downloaded From Batch WHERE OrderDate BETWEEN @startdate AND @enddate";
|
|
90
|
87
|
|
|
|
88
|
+ //Sql = "Select Id,OrderDate,OrderReference,InternalReference,NetworkName,ProductDescription,VoucherType,FaceValue,RequestedQuantity,DeliveredQuantity,ReadyForDownload,Downloaded,(SELECT COUNT(*) FROM Voucher WHERE Voucher.BatchId=Batch.Id) AS VoucherCount From Batch WHERE OrderDate BETWEEN @startdate AND @enddate";
|
|
|
89
|
+ //dgvBatches.Columns.Add("VoucherCount", "Voucher Count");
|
|
91
|
90
|
|
|
92
|
|
- SQLiteCommand comm = new SQLiteCommand(Sql, Globals.DBConnection);
|
|
93
|
91
|
|
|
94
|
|
- CultureInfo IVC = CultureInfo.InvariantCulture;
|
|
95
|
|
- comm.Parameters.Add(new SQLiteParameter("@startdate", dtpFilterStartDate.Value.Date.ToString("yyyy-MM-dd 00:00:00", IVC)));
|
|
96
|
|
- comm.Parameters.Add(new SQLiteParameter("@enddate", dtpFilterEndDate.Value.Date.ToString("yyyy-MM-dd 23:59:59", IVC)));
|
|
97
|
|
-
|
|
98
|
|
-
|
|
99
|
|
- lblLoading.Hide();
|
|
100
|
|
- using (SQLiteDataReader read = comm.ExecuteReader())
|
|
|
92
|
+ using (var Command = new SQLiteCommand(Sql, Globals.DBConnection))
|
|
101
|
93
|
{
|
|
102
|
|
- while (read.Read())
|
|
103
|
|
- {
|
|
104
|
|
- int BatchId = int.Parse(read["Id"].ToString());
|
|
105
|
|
- if (!BatchEvents.ContainsKey(BatchId))
|
|
106
|
|
- { //For batches with no events
|
|
107
|
|
- BatchEvents.Add(BatchId, new BatchEvent());
|
|
108
|
|
- }
|
|
109
|
|
- int DeliveredQuantity = int.Parse(read["DeliveredQuantity"].ToString());
|
|
|
94
|
+ CultureInfo IVC = CultureInfo.InvariantCulture;
|
|
|
95
|
+ Command.Parameters.AddWithValue("@startdate", dtpFilterStartDate.Value.Date.ToString("yyyy-MM-dd 00:00:00", IVC));
|
|
|
96
|
+ Command.Parameters.AddWithValue("@enddate", dtpFilterEndDate.Value.Date.ToString("yyyy-MM-dd 23:59:59", IVC));
|
|
110
|
97
|
|
|
111
|
|
- //Filters to exclude rows that don't match
|
|
112
|
|
- if (rdoFilterPrinted.Checked)
|
|
|
98
|
+ lblLoading.Hide();
|
|
|
99
|
+ using (SQLiteDataReader read = Command.ExecuteReader())
|
|
|
100
|
+ {
|
|
|
101
|
+ while (read.Read())
|
|
113
|
102
|
{
|
|
114
|
|
- if (BatchEvents[BatchId].PrintCount == 0)
|
|
|
103
|
+ int BatchId = int.Parse(read["Id"].ToString());
|
|
|
104
|
+ if (!BatchEvents.ContainsKey(BatchId))
|
|
|
105
|
+ { //For batches with no events
|
|
|
106
|
+ BatchEvents.Add(BatchId, new BatchEvent());
|
|
|
107
|
+ }
|
|
|
108
|
+ int DeliveredQuantity = int.Parse(read["DeliveredQuantity"].ToString());
|
|
|
109
|
+
|
|
|
110
|
+ //Filters to exclude rows that don't match
|
|
|
111
|
+ if (rdoFilterPrinted.Checked)
|
|
115
|
112
|
{
|
|
116
|
|
- continue;
|
|
|
113
|
+ if (BatchEvents[BatchId].PrintCount == 0)
|
|
|
114
|
+ {
|
|
|
115
|
+ continue;
|
|
|
116
|
+ }
|
|
117
|
117
|
}
|
|
118
|
|
- }
|
|
119
|
|
- if (rdoFilterUnprinted.Checked) {
|
|
120
|
|
- if (BatchEvents[BatchId].PrintCount >= DeliveredQuantity)
|
|
|
118
|
+ if (rdoFilterUnprinted.Checked)
|
|
121
|
119
|
{
|
|
122
|
|
- continue;
|
|
|
120
|
+ if (BatchEvents[BatchId].PrintCount >= DeliveredQuantity)
|
|
|
121
|
+ {
|
|
|
122
|
+ continue;
|
|
|
123
|
+ }
|
|
123
|
124
|
}
|
|
124
|
|
- }
|
|
125
|
|
- if (rdoFilterReprinted.Checked) {
|
|
126
|
|
- if (BatchEvents[BatchId].ReprintCount == 0)
|
|
|
125
|
+ if (rdoFilterReprinted.Checked)
|
|
127
|
126
|
{
|
|
128
|
|
- continue;
|
|
|
127
|
+ if (BatchEvents[BatchId].ReprintCount == 0)
|
|
|
128
|
+ {
|
|
|
129
|
+ continue;
|
|
|
130
|
+ }
|
|
129
|
131
|
}
|
|
130
|
|
- }
|
|
131
|
|
- if (rdoFilterExported.Checked)
|
|
132
|
|
- {
|
|
133
|
|
- if (BatchEvents[BatchId].ExportCount == 0)
|
|
|
132
|
+ if (rdoFilterExported.Checked)
|
|
134
|
133
|
{
|
|
135
|
|
- continue;
|
|
|
134
|
+ if (BatchEvents[BatchId].ExportCount == 0)
|
|
|
135
|
+ {
|
|
|
136
|
+ continue;
|
|
|
137
|
+ }
|
|
136
|
138
|
}
|
|
137
|
|
- }
|
|
138
|
|
- else {
|
|
139
|
|
- if (BatchEvents[BatchId].ExportCount > 0)
|
|
|
139
|
+ else
|
|
140
|
140
|
{
|
|
141
|
|
- continue;
|
|
|
141
|
+ if (BatchEvents[BatchId].ExportCount > 0)
|
|
|
142
|
+ {
|
|
|
143
|
+ continue;
|
|
|
144
|
+ }
|
|
142
|
145
|
}
|
|
143
|
|
- }
|
|
144
|
146
|
|
|
145
|
|
- var BatchRow = new object[] {
|
|
146
|
|
- BatchId,
|
|
147
|
|
- read["OrderDate"] };
|
|
148
|
|
- if (CanOrder)
|
|
149
|
|
- {
|
|
150
|
|
- BatchRow = BatchRow.Concat(new[] { read["OrderReference"] }).ToArray();
|
|
151
|
|
- BatchRow = BatchRow.Concat(new[] { read["InternalReference"] }).ToArray();
|
|
152
|
|
- }
|
|
153
|
|
- BatchRow = BatchRow.Concat(new[] { read["NetworkName"].ToString() }).ToArray();
|
|
154
|
|
- BatchRow = BatchRow.Concat(new[] { read["ProductDescription"].ToString() }).ToArray();
|
|
155
|
|
- BatchRow = BatchRow.Concat(new[] { Enum.Parse(typeof(Vouchertype), read["VoucherType"].ToString()).ToString() }).ToArray();
|
|
156
|
|
- BatchRow = BatchRow.Concat(new[] { "R" + ((decimal)read["FaceValue"]).ToString("0.##") }).ToArray();
|
|
|
147
|
+ var BatchRow = new List<object>
|
|
|
148
|
+ {
|
|
|
149
|
+ BatchId,
|
|
|
150
|
+ read["OrderDate"]
|
|
|
151
|
+ };
|
|
|
152
|
+ if (CanOrder)
|
|
|
153
|
+ {
|
|
|
154
|
+ BatchRow.Add(read["OrderReference"]);
|
|
|
155
|
+ BatchRow.Add(read["InternalReference"]);
|
|
|
156
|
+ }
|
|
|
157
|
+ BatchRow.Add(read["NetworkName"].ToString());
|
|
|
158
|
+ BatchRow.Add(read["ProductDescription"].ToString());
|
|
|
159
|
+ BatchRow.Add(Enum.Parse(typeof(Vouchertype), read["VoucherType"].ToString()).ToString());
|
|
|
160
|
+ BatchRow.Add("R" + ((decimal)read["FaceValue"]).ToString("0.##"));
|
|
|
161
|
+
|
|
|
162
|
+ if (CanOrder)
|
|
|
163
|
+ BatchRow.Add(read["RequestedQuantity"]);
|
|
157
|
164
|
|
|
158
|
|
-
|
|
159
|
|
- if (CanOrder)
|
|
160
|
|
- BatchRow = BatchRow.Concat(new[] { read["RequestedQuantity"] }).ToArray();
|
|
|
165
|
+ BatchRow.Add(read["DeliveredQuantity"]);
|
|
161
|
166
|
|
|
162
|
|
- BatchRow = BatchRow.Concat(new[] { read["DeliveredQuantity"] }).ToArray();
|
|
|
167
|
+ if (CanOrder)
|
|
|
168
|
+ BatchRow.Add(((bool)read["ReadyForDownload"] == true ? "Yes" : "No"));
|
|
163
|
169
|
|
|
164
|
|
- if (CanOrder)
|
|
165
|
|
- BatchRow = BatchRow.Concat(new[] { ((bool)read["ReadyForDownload"] == true ? "Yes" : "No") }).ToArray();
|
|
|
170
|
+ BatchRow.Add((bool)read["Downloaded"] ? "Yes" : "No");
|
|
|
171
|
+ BatchRow.Add((object)BatchEvents[BatchId].PrintCount);
|
|
166
|
172
|
|
|
167
|
|
- BatchRow = BatchRow.Concat(new[] { (object)BatchEvents[BatchId].PrintCount }).ToArray();
|
|
|
173
|
+ if (CanExport)
|
|
|
174
|
+ BatchRow.Add((BatchEvents[BatchId].ExportCount > 0 ? "Exported" : ""));
|
|
168
|
175
|
|
|
169
|
|
- if (CanExport)
|
|
170
|
|
- BatchRow = BatchRow.Concat(new[] { (BatchEvents[BatchId].ExportCount >0?"Exported":"") }).ToArray();
|
|
|
176
|
+ //BatchRow.Add(read["VoucherCount"]);
|
|
171
|
177
|
|
|
172
|
|
- dgvBatches.Rows.Add(BatchRow);
|
|
|
178
|
+ dgvBatches.Rows.Add(BatchRow.ToArray());
|
|
|
179
|
+ }
|
|
|
180
|
+ dgvBatches.Sort(dgvBatches.Columns["OrderDate"], ListSortDirection.Descending);
|
|
173
|
181
|
}
|
|
174
|
|
- dgvBatches.Sort(dgvBatches.Columns["OrderDate"], ListSortDirection.Descending);
|
|
175
|
182
|
}
|
|
176
|
183
|
}
|
|
177
|
184
|
|
|
|
@@ -228,51 +235,272 @@ namespace BulkPrinting
|
|
228
|
235
|
if (!Utility.CheckUserAccess(Utility.UserPermissions.BulkViewPins))
|
|
229
|
236
|
RightButtonList.Remove(this.btnViewPINs);
|
|
230
|
237
|
|
|
231
|
|
- int ButtonX = 12;
|
|
|
238
|
+ int ButtonX = btnLogout.Location.X;
|
|
|
239
|
+ int ButtonY = btnLogout.Location.Y;
|
|
|
240
|
+ int ButtonSpacing = btnOrderReport.Location.X - ButtonX;
|
|
232
|
241
|
foreach (Button b in LeftButtonList)
|
|
233
|
242
|
{
|
|
234
|
|
- b.Location = new Point(ButtonX, 0);
|
|
|
243
|
+ b.Location = new Point(ButtonX, ButtonY);
|
|
235
|
244
|
b.Show();
|
|
236
|
|
- ButtonX += 106;
|
|
|
245
|
+ ButtonX += ButtonSpacing;
|
|
237
|
246
|
}
|
|
238
|
247
|
|
|
239
|
|
- ButtonX = pnlSplitGrids.Width - 378;
|
|
|
248
|
+ ButtonX = btnReprint.Location.X;
|
|
240
|
249
|
RightButtonList.Reverse();
|
|
241
|
250
|
foreach (Button b in RightButtonList)
|
|
242
|
251
|
{
|
|
243
|
|
- b.Location = new Point(ButtonX, 0);
|
|
|
252
|
+ b.Location = new Point(ButtonX, ButtonY);
|
|
244
|
253
|
b.Show();
|
|
245
|
|
- ButtonX -= 106;
|
|
|
254
|
+ ButtonX -= ButtonSpacing;
|
|
246
|
255
|
}
|
|
247
|
256
|
|
|
248
|
257
|
lblLoading.Show();
|
|
249
|
258
|
dtpFilterStartDate.Value = DateTime.Now.AddMonths(-3);
|
|
250
|
259
|
Globals.OpenBatches = new List<int>();
|
|
251
|
260
|
|
|
252
|
|
- LoadingThreadEvent = new EventWaitHandle(true, EventResetMode.AutoReset);
|
|
253
|
|
- LoadingThread = new Thread(() =>
|
|
|
261
|
+ RetryDownloads = true;
|
|
|
262
|
+ CheckForNewBatches = true;
|
|
|
263
|
+
|
|
|
264
|
+ LoadingThread = new Thread(LoadingThreadWorker);
|
|
|
265
|
+ LoadingThread.Start();
|
|
|
266
|
+ Utility.InitialiseUserLimits(Globals.DBConnection);
|
|
|
267
|
+ ReSyncTimer.Enabled = true;
|
|
|
268
|
+ }
|
|
|
269
|
+
|
|
|
270
|
+ private void LoadingThreadWorker()
|
|
|
271
|
+ {
|
|
|
272
|
+ const int voucherPageSize = 5000;
|
|
|
273
|
+ var conn = Globals.DBConnection;
|
|
|
274
|
+ //using (var conn = Utility.OpenDBConnection())
|
|
254
|
275
|
{
|
|
255
|
276
|
Invoke(new Action(() =>
|
|
256
|
277
|
{
|
|
257
|
|
- this.Enabled = false;
|
|
258
|
278
|
PopulateGrid();
|
|
259
|
|
- this.Enabled = true;
|
|
260
|
279
|
}));
|
|
261
|
280
|
|
|
262
|
|
- while (LoadingThreadEvent.WaitOne())
|
|
|
281
|
+ var lastSyncedBatchId = Utility.GetSavedParameterAsInt(conn, "LastSyncedBatchId");
|
|
|
282
|
+
|
|
|
283
|
+ bool skipNewBatchCheck = false;
|
|
|
284
|
+ while (true)
|
|
263
|
285
|
{
|
|
|
286
|
+ bool forceNewBatchCheck;
|
|
|
287
|
+ lock (LoadingThreadLock)
|
|
|
288
|
+ {
|
|
|
289
|
+ while (!RetryDownloads)
|
|
|
290
|
+ {
|
|
|
291
|
+ Monitor.Wait(LoadingThreadLock);
|
|
|
292
|
+ }
|
|
|
293
|
+ forceNewBatchCheck = CheckForNewBatches;
|
|
|
294
|
+ RetryDownloads = false;
|
|
|
295
|
+ CheckForNewBatches = false;
|
|
|
296
|
+ }
|
|
264
|
297
|
if (Globals.SessionMode == SessionModes.Online)
|
|
265
|
298
|
{
|
|
266
|
|
- Utility.SyncAllBatches();
|
|
267
|
|
- Invoke(new Action(() =>
|
|
|
299
|
+ Debug.WriteLine("Checking for downloads");
|
|
|
300
|
+
|
|
|
301
|
+ if (forceNewBatchCheck)
|
|
|
302
|
+ {
|
|
|
303
|
+ skipNewBatchCheck = false;
|
|
|
304
|
+ }
|
|
|
305
|
+
|
|
|
306
|
+ // Check for new batches if required.
|
|
|
307
|
+ var refreshGrid = false;
|
|
|
308
|
+ try
|
|
|
309
|
+ {
|
|
|
310
|
+ while (!skipNewBatchCheck)
|
|
|
311
|
+ {
|
|
|
312
|
+ Debug.WriteLine("Querying batches");
|
|
|
313
|
+ Page<Batch> BatchPage = new Page<Batch>();
|
|
|
314
|
+ if (Utility.RESTRequest(ref BatchPage, String.Format("/api/batches/?lastBatchId={0}", lastSyncedBatchId)))
|
|
|
315
|
+ {
|
|
|
316
|
+ if (BatchPage.Items.Count == 0)
|
|
|
317
|
+ {
|
|
|
318
|
+ // No more new batches, so stop checking until an order is placed.
|
|
|
319
|
+ skipNewBatchCheck = true;
|
|
|
320
|
+ }
|
|
|
321
|
+ else
|
|
|
322
|
+ {
|
|
|
323
|
+ foreach (var batch in BatchPage.Items)
|
|
|
324
|
+ {
|
|
|
325
|
+ refreshGrid = true;
|
|
|
326
|
+ Utility.SaveBatch(conn, batch);
|
|
|
327
|
+ lastSyncedBatchId = batch.Id;
|
|
|
328
|
+ if (!Utility.UpdateSavedParameter(conn, "LastSyncedBatchId", lastSyncedBatchId))
|
|
|
329
|
+ {
|
|
|
330
|
+ // Shouldn't happen.
|
|
|
331
|
+ throw new Exception("Failed to update LastSyncedBatchId");
|
|
|
332
|
+ }
|
|
|
333
|
+ }
|
|
|
334
|
+ }
|
|
|
335
|
+ }
|
|
|
336
|
+ else
|
|
|
337
|
+ {
|
|
|
338
|
+ // Error, so leave skipNewBatchCheck unchanged.
|
|
|
339
|
+ break;
|
|
|
340
|
+ }
|
|
|
341
|
+ }
|
|
|
342
|
+ }
|
|
|
343
|
+ catch (Exception e)
|
|
|
344
|
+ {
|
|
|
345
|
+ Debug.WriteLine("Error while downloading batches");
|
|
|
346
|
+ Debug.WriteLine(e.ToString());
|
|
|
347
|
+ }
|
|
|
348
|
+
|
|
|
349
|
+ if (refreshGrid)
|
|
268
|
350
|
{
|
|
269
|
|
- PopulateGrid();
|
|
270
|
|
- }));
|
|
|
351
|
+ Invoke(new Action(() =>
|
|
|
352
|
+ {
|
|
|
353
|
+ PopulateGrid();
|
|
|
354
|
+ }));
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ // Continue downloading incomplete batches if any.
|
|
|
358
|
+ try
|
|
|
359
|
+ {
|
|
|
360
|
+ using (var command = new SQLiteCommand(
|
|
|
361
|
+ "SELECT Id FROM Batch WHERE Downloaded=0 ORDER BY Downloaded, Id",
|
|
|
362
|
+ conn))
|
|
|
363
|
+ {
|
|
|
364
|
+ using (SQLiteDataReader row = command.ExecuteReader())
|
|
|
365
|
+ {
|
|
|
366
|
+ while (row.Read())
|
|
|
367
|
+ {
|
|
|
368
|
+ refreshGrid = false;
|
|
|
369
|
+
|
|
|
370
|
+ int batchId = (int)row["Id"];
|
|
|
371
|
+ try
|
|
|
372
|
+ {
|
|
|
373
|
+ Batch batch = new Batch();
|
|
|
374
|
+ if (Utility.RESTRequest(ref batch, String.Format("/api/batches/{0}", batchId)))
|
|
|
375
|
+ {
|
|
|
376
|
+ refreshGrid = true;
|
|
|
377
|
+ Utility.SaveBatch(conn, batch);
|
|
|
378
|
+
|
|
|
379
|
+ if (batch.ReadyForDownload)
|
|
|
380
|
+ {
|
|
|
381
|
+ long result = (long)Utility.DBExecuteScalar(conn,
|
|
|
382
|
+ "SELECT COUNT(*) FROM Voucher WHERE BatchId=@BatchId",
|
|
|
383
|
+ new SQLiteParameter("@BatchId", batchId));
|
|
|
384
|
+ int voucherCount = (int)result;
|
|
|
385
|
+
|
|
|
386
|
+ while (voucherCount < batch.DeliveredQuantity)
|
|
|
387
|
+ {
|
|
|
388
|
+ Debug.WriteLine(String.Format("Downloading vouchers for batch {0} (got {1})", batchId, voucherCount));
|
|
|
389
|
+
|
|
|
390
|
+ int page = voucherCount / voucherPageSize + 1;
|
|
|
391
|
+ int offset = voucherCount % voucherPageSize;
|
|
|
392
|
+
|
|
|
393
|
+ Page<Voucher> voucherPage = new Page<Voucher>();
|
|
|
394
|
+ if (!Utility.RESTRequest(ref voucherPage, String.Format("/api/batches/{0}/vouchers/?page={1}&pageSize={2}", batchId, page, voucherPageSize)))
|
|
|
395
|
+ {
|
|
|
396
|
+ break;
|
|
|
397
|
+ }
|
|
|
398
|
+ if (voucherPage.Items.Count == 0)
|
|
|
399
|
+ {
|
|
|
400
|
+ throw new Exception(String.Format("Too few vouchers were returned for batch {0}", batchId));
|
|
|
401
|
+ }
|
|
|
402
|
+
|
|
|
403
|
+ Debug.WriteLine(String.Format("Downloaded vouchers for batch {0} (got {1})", batchId, voucherCount + voucherPage.Items.Count));
|
|
|
404
|
+
|
|
|
405
|
+ lock (Globals.DBWriteLock)
|
|
|
406
|
+ {
|
|
|
407
|
+ using (var trans = conn.BeginTransaction())
|
|
|
408
|
+ {
|
|
|
409
|
+ using (var insert = new SQLiteCommand(
|
|
|
410
|
+ "INSERT INTO Voucher (Id, SequenceNumber, ExpiryDate, Serial, EncryptedPIN, BatchId)" +
|
|
|
411
|
+ "VALUES (@Id,@SequenceNumber,@ExpiryDate,@Serial,@EncryptedPin,@BatchId)",
|
|
|
412
|
+ conn, trans))
|
|
|
413
|
+ {
|
|
|
414
|
+ for (var i = offset; i < voucherPage.Items.Count; i++)
|
|
|
415
|
+ {
|
|
|
416
|
+ var voucher = voucherPage.Items[i];
|
|
|
417
|
+ if (voucher.SequenceNumber != voucherCount + 1)
|
|
|
418
|
+ {
|
|
|
419
|
+ throw new Exception(String.Format("Vouchers for batch {0} are not sequential: expecting {1}, got {2}", batchId, voucherCount + 1, voucher.SequenceNumber));
|
|
|
420
|
+ }
|
|
|
421
|
+ insert.Parameters.Clear();
|
|
|
422
|
+ insert.Parameters.AddWithValue("@Id", voucher.Id);
|
|
|
423
|
+ insert.Parameters.AddWithValue("@SequenceNumber", voucher.SequenceNumber);
|
|
|
424
|
+ insert.Parameters.AddWithValue("@ExpiryDate", voucher.ExpiryDate.Date);
|
|
|
425
|
+ insert.Parameters.AddWithValue("@Serial", voucher.Serial);
|
|
|
426
|
+ insert.Parameters.AddWithValue("@EncryptedPIN", voucher.EncryptedPIN);
|
|
|
427
|
+ insert.Parameters.AddWithValue("@BatchId", batchId);
|
|
|
428
|
+ insert.ExecuteNonQuery();
|
|
|
429
|
+ voucherCount++;
|
|
|
430
|
+ }
|
|
|
431
|
+ }
|
|
|
432
|
+ trans.Commit();
|
|
|
433
|
+ }
|
|
|
434
|
+ }
|
|
|
435
|
+
|
|
|
436
|
+ Debug.WriteLine(String.Format("Inserted vouchers for batch {0}", batchId));
|
|
|
437
|
+ }
|
|
|
438
|
+
|
|
|
439
|
+ if (voucherCount > batch.DeliveredQuantity)
|
|
|
440
|
+ {
|
|
|
441
|
+ throw new Exception("Batch contains more than the specified number of vouchers");
|
|
|
442
|
+ }
|
|
|
443
|
+ else
|
|
|
444
|
+ {
|
|
|
445
|
+ Debug.WriteLine(String.Format("Batch {0} complete", batchId));
|
|
|
446
|
+
|
|
|
447
|
+ Utility.DBExecuteNonQuery(conn,
|
|
|
448
|
+ "UPDATE Batch SET Downloaded=1 WHERE Id=@BatchId",
|
|
|
449
|
+ new SQLiteParameter("@BatchId", batchId));
|
|
|
450
|
+ }
|
|
|
451
|
+ }
|
|
|
452
|
+ }
|
|
|
453
|
+ else if (batch == null)
|
|
|
454
|
+ {
|
|
|
455
|
+ // Batch will only be null if we got a 404.
|
|
|
456
|
+ Debug.WriteLine(String.Format("Removing deleted batch {0}", batchId));
|
|
|
457
|
+
|
|
|
458
|
+ Utility.DBExecuteNonQuery(conn,
|
|
|
459
|
+ "DELETE FROM Voucher WHERE BatchId=@BatchId",
|
|
|
460
|
+ new SQLiteParameter("@BatchId", batchId));
|
|
|
461
|
+
|
|
|
462
|
+ Utility.DBExecuteNonQuery(conn,
|
|
|
463
|
+ "DELETE FROM Batch WHERE Id=@BatchId",
|
|
|
464
|
+ new SQLiteParameter("@BatchId", batchId));
|
|
|
465
|
+
|
|
|
466
|
+ refreshGrid = true;
|
|
|
467
|
+ }
|
|
|
468
|
+ }
|
|
|
469
|
+ catch (Exception e)
|
|
|
470
|
+ {
|
|
|
471
|
+ Debug.WriteLine(String.Format("Error while downloading vouchers for batch {0}", batchId));
|
|
|
472
|
+ Debug.WriteLine(e.ToString());
|
|
|
473
|
+ }
|
|
|
474
|
+
|
|
|
475
|
+ if (refreshGrid)
|
|
|
476
|
+ {
|
|
|
477
|
+ Invoke(new Action(() =>
|
|
|
478
|
+ {
|
|
|
479
|
+ PopulateGrid();
|
|
|
480
|
+ }));
|
|
|
481
|
+ }
|
|
|
482
|
+ }
|
|
|
483
|
+ }
|
|
|
484
|
+ }
|
|
|
485
|
+ }
|
|
|
486
|
+ catch (Exception e)
|
|
|
487
|
+ {
|
|
|
488
|
+ Debug.WriteLine("Error while downloading vouchers");
|
|
|
489
|
+ Debug.WriteLine(e.ToString());
|
|
|
490
|
+ }
|
|
271
|
491
|
}
|
|
272
|
492
|
}
|
|
273
|
|
- });
|
|
274
|
|
- LoadingThread.Start();
|
|
275
|
|
- Utility.InitialiseUserLimits();
|
|
|
493
|
+ }
|
|
|
494
|
+ }
|
|
|
495
|
+
|
|
|
496
|
+ public void NewBatchAvailable()
|
|
|
497
|
+ {
|
|
|
498
|
+ lock (LoadingThreadLock)
|
|
|
499
|
+ {
|
|
|
500
|
+ RetryDownloads = true;
|
|
|
501
|
+ CheckForNewBatches = true;
|
|
|
502
|
+ Monitor.Pulse(LoadingThreadLock);
|
|
|
503
|
+ }
|
|
276
|
504
|
}
|
|
277
|
505
|
|
|
278
|
506
|
private void btnOrder_Click(object sender, EventArgs e)
|
|
|
@@ -309,10 +537,9 @@ namespace BulkPrinting
|
|
309
|
537
|
}
|
|
310
|
538
|
|
|
311
|
539
|
int DeliveredVoucherCount = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["DeliveredQuantity"].Index].Value;
|
|
312
|
|
- string Sql = "SELECT COUNT(*) FROM Voucher WHERE BatchId=@id";
|
|
313
|
|
- SQLiteCommand Command = new SQLiteCommand(Sql, Globals.DBConnection);
|
|
314
|
|
- Command.Parameters.Add(new SQLiteParameter("@id", BatchID));
|
|
315
|
|
- int DownloadedVoucherCount = Convert.ToInt32(Command.ExecuteScalar());
|
|
|
540
|
+ int DownloadedVoucherCount = Convert.ToInt32(Utility.DBExecuteScalar(Globals.DBConnection,
|
|
|
541
|
+ "SELECT COUNT(*) FROM Voucher WHERE BatchId=@id",
|
|
|
542
|
+ new SQLiteParameter("@id", BatchID)));
|
|
316
|
543
|
if (DownloadedVoucherCount < DeliveredVoucherCount) {
|
|
317
|
544
|
MessageBox.Show(string.Format("Batch {0} has not finished downloading.\nDownloaded {1} of {2} vouchers in batch.",BatchID,DownloadedVoucherCount,DeliveredVoucherCount), "Cannot open print dialog", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
318
|
545
|
return;
|
|
|
@@ -359,6 +586,7 @@ namespace BulkPrinting
|
|
359
|
586
|
if ((LoadingThread != null) && (LoadingThread.IsAlive))
|
|
360
|
587
|
{
|
|
361
|
588
|
LoadingThread.Abort();
|
|
|
589
|
+ LoadingThread.Join();
|
|
362
|
590
|
}
|
|
363
|
591
|
Utility.Logout();
|
|
364
|
592
|
}
|
|
|
@@ -461,10 +689,10 @@ namespace BulkPrinting
|
|
461
|
689
|
MessageBox.Show("This batch has been exported previously. Exported batches cannot be printed.", "Cannot print batch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
462
|
690
|
return;
|
|
463
|
691
|
}
|
|
464
|
|
- SQLiteCommand Command = new SQLiteCommand("SELECT COUNT(*) FROM Voucher v LEFT JOIN Logs l ON v.Id = l.VoucherId AND l.EventType = @eventtype WHERE l.VoucherId IS NOT NULL AND v.BatchId=@batchid AND Retry=0", Globals.DBConnection);
|
|
465
|
|
- Command.Parameters.Add(new SQLiteParameter("@batchid", BatchID));
|
|
466
|
|
- Command.Parameters.Add(new SQLiteParameter("@eventtype", VendorEvent.VendorEventType.PrintVoucher));
|
|
467
|
|
- int PrintCount = int.Parse(Command.ExecuteScalar().ToString());
|
|
|
692
|
+ int PrintCount = int.Parse(Utility.DBExecuteScalar(Globals.DBConnection,
|
|
|
693
|
+ "SELECT COUNT(*) FROM Voucher v LEFT JOIN Logs l ON v.Id = l.VoucherId AND l.EventType = @eventtype WHERE l.VoucherId IS NOT NULL AND v.BatchId=@batchid AND Retry=0",
|
|
|
694
|
+ new SQLiteParameter("@batchid", BatchID),
|
|
|
695
|
+ new SQLiteParameter("@eventtype", VendorEvent.VendorEventType.PrintVoucher)).ToString());
|
|
468
|
696
|
|
|
469
|
697
|
if (PrintCount == 0) {
|
|
470
|
698
|
MessageBox.Show("You can only use the Reprint feature for vouchers that have already been printed. Please choose a batch with previously printed vouchers.", "Cannot reprint batch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
|
@@ -472,10 +700,9 @@ namespace BulkPrinting
|
|
472
|
700
|
}
|
|
473
|
701
|
|
|
474
|
702
|
int DeliveredVoucherCount = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["DeliveredQuantity"].Index].Value;
|
|
475
|
|
- string Sql = "SELECT COUNT(*) FROM Voucher WHERE BatchId=@id";
|
|
476
|
|
- Command = new SQLiteCommand(Sql, Globals.DBConnection);
|
|
477
|
|
- Command.Parameters.Add(new SQLiteParameter("@id", BatchID));
|
|
478
|
|
- int DownloadedVoucherCount = Convert.ToInt32(Command.ExecuteScalar());
|
|
|
703
|
+ int DownloadedVoucherCount = Convert.ToInt32(Utility.DBExecuteScalar(Globals.DBConnection,
|
|
|
704
|
+ "SELECT COUNT(*) FROM Voucher WHERE BatchId=@id",
|
|
|
705
|
+ new SQLiteParameter("@id", BatchID)));
|
|
479
|
706
|
if (DownloadedVoucherCount < DeliveredVoucherCount)
|
|
480
|
707
|
{
|
|
481
|
708
|
MessageBox.Show(string.Format("Batch {0} has not finished downloading.\nDownloaded {1} of {2} vouchers in batch.", BatchID, DownloadedVoucherCount, DeliveredVoucherCount), "Cannot open print dialog", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
|
@@ -538,13 +765,18 @@ namespace BulkPrinting
|
|
538
|
765
|
if (!b.Enabled)
|
|
539
|
766
|
{
|
|
540
|
767
|
e.Graphics.Clear(Color.Gray);
|
|
541
|
|
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
|
|
|
768
|
+ //e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
|
|
|
769
|
+ e.Graphics.DrawImage(b.BackgroundImage, b.ClientRectangle);
|
|
542
|
770
|
}
|
|
543
|
771
|
}
|
|
544
|
772
|
|
|
545
|
773
|
private void ReSyncTimer_Tick(object sender, EventArgs e)
|
|
546
|
774
|
{
|
|
547
|
|
- LoadingThreadEvent.Set();
|
|
|
775
|
+ lock (LoadingThreadLock)
|
|
|
776
|
+ {
|
|
|
777
|
+ RetryDownloads = true;
|
|
|
778
|
+ Monitor.Pulse(LoadingThreadLock);
|
|
|
779
|
+ }
|
|
548
|
780
|
}
|
|
549
|
781
|
}
|
|
550
|
782
|
}
|