|
|
@@ -46,39 +46,9 @@ namespace BulkPrinting
|
|
46
|
46
|
return result.Length > 15 ? result.Substring(result.Length - 15, 15) : result;
|
|
47
|
47
|
}
|
|
48
|
48
|
|
|
49
|
|
- public static SQLiteConnection OpenDBConnection()
|
|
|
49
|
+ public static DBHelper OpenDBConnection()
|
|
50
|
50
|
{
|
|
51
|
|
- var conn = new SQLiteConnection(String.Format("Data Source={0};", Globals.MaxDBFilePath));
|
|
52
|
|
- conn.SetPassword(Globals.SessionDatabasePassword);
|
|
53
|
|
- conn.Open();
|
|
54
|
|
- return conn;
|
|
55
|
|
- }
|
|
56
|
|
-
|
|
57
|
|
- public static int DBExecuteNonQuery(SQLiteConnection conn, string query, params SQLiteParameter[] queryParams)
|
|
58
|
|
- {
|
|
59
|
|
- lock (Globals.DBWriteLock)
|
|
60
|
|
- {
|
|
61
|
|
- using (var command = new SQLiteCommand(query, conn))
|
|
62
|
|
- {
|
|
63
|
|
- foreach (var param in queryParams)
|
|
64
|
|
- {
|
|
65
|
|
- command.Parameters.Add(param);
|
|
66
|
|
- }
|
|
67
|
|
- return command.ExecuteNonQuery();
|
|
68
|
|
- }
|
|
69
|
|
- }
|
|
70
|
|
- }
|
|
71
|
|
-
|
|
72
|
|
- public static object DBExecuteScalar(SQLiteConnection conn, string query, params SQLiteParameter[] queryParams)
|
|
73
|
|
- {
|
|
74
|
|
- using (var command = new SQLiteCommand(query, conn))
|
|
75
|
|
- {
|
|
76
|
|
- foreach (var param in queryParams)
|
|
77
|
|
- {
|
|
78
|
|
- command.Parameters.Add(param);
|
|
79
|
|
- }
|
|
80
|
|
- return command.ExecuteScalar();
|
|
81
|
|
- }
|
|
|
51
|
+ return new DBHelper(String.Format("Data Source={0};", Globals.MaxDBFilePath), Globals.SessionDatabasePassword);
|
|
82
|
52
|
}
|
|
83
|
53
|
|
|
84
|
54
|
public static bool Login(LoginData UserLoginData, bool Offline, bool RememberMe) {
|
|
|
@@ -334,16 +304,14 @@ namespace BulkPrinting
|
|
334
|
304
|
return InternalReferenceRequest.InternalReference;
|
|
335
|
305
|
}
|
|
336
|
306
|
|
|
337
|
|
- public static void SaveBatch(SQLiteConnection conn, Batch batch)
|
|
|
307
|
+ public static void SaveBatch(DBHelper db, Batch batch)
|
|
338
|
308
|
{
|
|
339
|
|
- lock (Globals.DBWriteLock)
|
|
|
309
|
+ lock (db.WriteLock)
|
|
340
|
310
|
{
|
|
341
|
|
- using (var trans = conn.BeginTransaction())
|
|
|
311
|
+ using (var trans = db.BeginTransaction())
|
|
342
|
312
|
{
|
|
343
|
|
- using (var command = new SQLiteCommand(conn))
|
|
|
313
|
+ using (var command = db.CreateCommand(trans))
|
|
344
|
314
|
{
|
|
345
|
|
- command.Transaction = trans;
|
|
346
|
|
-
|
|
347
|
315
|
command.Parameters.AddWithValue("@Id", batch.Id);
|
|
348
|
316
|
command.Parameters.AddWithValue("@OrderDate", batch.OrderDate);
|
|
349
|
317
|
command.Parameters.AddWithValue("@OrderGuid", batch.OrderGuid);
|
|
|
@@ -383,31 +351,31 @@ namespace BulkPrinting
|
|
383
|
351
|
}
|
|
384
|
352
|
}
|
|
385
|
353
|
|
|
386
|
|
- public static string GetSavedParameter(SQLiteConnection conn, string key)
|
|
|
354
|
+ public static string GetSavedParameter(DBHelper db, string key)
|
|
387
|
355
|
{
|
|
388
|
|
- return (string)DBExecuteScalar(conn, "SELECT Value FROM Parameters WHERE Key=@Key",
|
|
|
356
|
+ return (string)db.ExecuteScalar("SELECT Value FROM Parameters WHERE Key=@Key",
|
|
389
|
357
|
new SQLiteParameter("@Key", key));
|
|
390
|
358
|
}
|
|
391
|
359
|
|
|
392
|
|
- public static int GetSavedParameterAsInt(SQLiteConnection conn, string key)
|
|
|
360
|
+ public static int GetSavedParameterAsInt(DBHelper db, string key)
|
|
393
|
361
|
{
|
|
394
|
|
- return Convert.ToInt32(GetSavedParameter(conn, key));
|
|
|
362
|
+ return Convert.ToInt32(GetSavedParameter(db, key));
|
|
395
|
363
|
}
|
|
396
|
364
|
|
|
397
|
|
- public static bool GetSavedParameterAsBoolean(SQLiteConnection conn, string key)
|
|
|
365
|
+ public static bool GetSavedParameterAsBoolean(DBHelper db, string key)
|
|
398
|
366
|
{
|
|
399
|
|
- return Convert.ToBoolean(GetSavedParameter(conn, key));
|
|
|
367
|
+ return Convert.ToBoolean(GetSavedParameter(db, key));
|
|
400
|
368
|
}
|
|
401
|
369
|
|
|
402
|
|
- public static bool UpdateSavedParameter(SQLiteConnection conn, string key, object value)
|
|
|
370
|
+ public static bool UpdateSavedParameter(DBHelper db, string key, object value)
|
|
403
|
371
|
{
|
|
404
|
|
- return DBExecuteNonQuery(conn,
|
|
|
372
|
+ return db.ExecuteNonQuery(
|
|
405
|
373
|
"UPDATE Parameters SET Value=@Value WHERE Key=@Key",
|
|
406
|
374
|
new SQLiteParameter("@Key", key),
|
|
407
|
375
|
new SQLiteParameter("@Value", Convert.ToString(value))) > 0;
|
|
408
|
376
|
}
|
|
409
|
377
|
|
|
410
|
|
- public static void PrintVouchers(SQLiteConnection conn, int BatchId, int StartSeqNo, int EndSeqNo)
|
|
|
378
|
+ public static void PrintVouchers(DBHelper db, int BatchId, int StartSeqNo, int EndSeqNo)
|
|
411
|
379
|
{
|
|
412
|
380
|
|
|
413
|
381
|
int VoucherCount = 0;
|
|
|
@@ -427,13 +395,12 @@ namespace BulkPrinting
|
|
427
|
395
|
List<EventLog> LogEvents = new List<EventLog>();
|
|
428
|
396
|
|
|
429
|
397
|
IList<PrintVoucher> VoucherRow = new List<PrintVoucher>();
|
|
430
|
|
- using (var Command = new SQLiteCommand("SELECT DISTINCT v.Id,v.SequenceNumber,v.Serial,v.EncryptedPIN,v.BatchId,b.ProductDescription,l.VoucherId From Voucher v LEFT JOIN Batch b on v.BatchId = b.Id LEFT JOIN Logs l ON v.Id = l.VoucherId AND l.EventType = @eventtype WHERE v.BatchId=@batch_id AND v.SequenceNumber BETWEEN @seqstartno AND @seqendno ORDER BY v.SequenceNumber", conn))
|
|
|
398
|
+ using (var Command = db.CreateCommand("SELECT DISTINCT v.Id,v.SequenceNumber,v.Serial,v.EncryptedPIN,v.BatchId,b.ProductDescription,l.VoucherId From Voucher v LEFT JOIN Batch b on v.BatchId = b.Id LEFT JOIN Logs l ON v.Id = l.VoucherId AND l.EventType = @eventtype WHERE v.BatchId=@batch_id AND v.SequenceNumber BETWEEN @seqstartno AND @seqendno ORDER BY v.SequenceNumber",
|
|
|
399
|
+ new SQLiteParameter("@batch_id", BatchId),
|
|
|
400
|
+ new SQLiteParameter("@eventtype", VendorEvent.VendorEventType.PrintVoucher),
|
|
|
401
|
+ new SQLiteParameter("@seqstartno", StartSeqNo),
|
|
|
402
|
+ new SQLiteParameter("@seqendno", EndSeqNo)))
|
|
431
|
403
|
{
|
|
432
|
|
- Command.Parameters.AddWithValue("@batch_id", BatchId);
|
|
433
|
|
- Command.Parameters.AddWithValue("@eventtype", VendorEvent.VendorEventType.PrintVoucher);
|
|
434
|
|
- Command.Parameters.AddWithValue("@seqstartno", StartSeqNo);
|
|
435
|
|
- Command.Parameters.AddWithValue("@seqendno", EndSeqNo);
|
|
436
|
|
-
|
|
437
|
404
|
using (SQLiteDataReader read = Command.ExecuteReader())
|
|
438
|
405
|
{
|
|
439
|
406
|
int JobID = Globals.MaxPrinter.Open("Vouchers");
|
|
|
@@ -515,7 +482,7 @@ namespace BulkPrinting
|
|
515
|
482
|
Globals.MaxPrinter.Close();
|
|
516
|
483
|
}
|
|
517
|
484
|
}
|
|
518
|
|
- LogBulkEvents(conn, LogEvents);
|
|
|
485
|
+ LogBulkEvents(db, LogEvents);
|
|
519
|
486
|
}
|
|
520
|
487
|
|
|
521
|
488
|
public enum UserPermissions {
|
|
|
@@ -558,13 +525,13 @@ namespace BulkPrinting
|
|
558
|
525
|
public static void Logout() {
|
|
559
|
526
|
if (Globals.SessionData != null)
|
|
560
|
527
|
{
|
|
561
|
|
- DBExecuteNonQuery(Globals.DBConnection, "DELETE FROM SessionData"); //Destroy stored session data
|
|
|
528
|
+ Globals.DB.ExecuteNonQuery("DELETE FROM SessionData"); //Destroy stored session data
|
|
562
|
529
|
string SessionDataJson = JsonConvert.SerializeObject(Globals.SessionData);
|
|
563
|
|
- DBExecuteNonQuery(Globals.DBConnection,
|
|
|
530
|
+ Globals.DB.ExecuteNonQuery(
|
|
564
|
531
|
"INSERT INTO SessionData (Key,Value) VALUES (@key,@value)",
|
|
565
|
532
|
new SQLiteParameter("@key", "SessionDataJson"),
|
|
566
|
533
|
new SQLiteParameter("@value", SessionDataJson));
|
|
567
|
|
- LogEvent(Globals.DBConnection, VendorEvent.VendorEventType.Logout);
|
|
|
534
|
+ LogEvent(Globals.DB, VendorEvent.VendorEventType.Logout);
|
|
568
|
535
|
|
|
569
|
536
|
CancelLogUploadWorker();
|
|
570
|
537
|
CancelLogDownloadWorker();
|
|
|
@@ -581,8 +548,12 @@ namespace BulkPrinting
|
|
581
|
548
|
}
|
|
582
|
549
|
|
|
583
|
550
|
Globals.UploadNewLogs = false;
|
|
|
551
|
+ Globals.LogUploadThreadCancelled = false;
|
|
|
552
|
+ Globals.LogDownloadThreadCancelled = false;
|
|
584
|
553
|
|
|
585
|
|
- Globals.DBConnection.Close();
|
|
|
554
|
+ Globals.DB.Close();
|
|
|
555
|
+ Globals.DB.Dispose();
|
|
|
556
|
+ Globals.DB = null;
|
|
586
|
557
|
Globals.SessionData = null;
|
|
587
|
558
|
Globals.SessionDatabasePassword = null;
|
|
588
|
559
|
Globals.SessionVoucherKey = null;
|
|
|
@@ -603,32 +574,29 @@ namespace BulkPrinting
|
|
603
|
574
|
return (int)MetaData.LastVendorEventRemoteId;
|
|
604
|
575
|
}
|
|
605
|
576
|
|
|
606
|
|
- public static void CheckLogSynchronisation(SQLiteConnection conn)
|
|
|
577
|
+ public static void CheckLogSynchronisation(DBHelper db)
|
|
607
|
578
|
{
|
|
608
|
|
- if (!GetSavedParameterAsBoolean(conn, "LoggingInitialised"))
|
|
|
579
|
+ if (!GetSavedParameterAsBoolean(db, "LoggingInitialised"))
|
|
609
|
580
|
{
|
|
610
|
581
|
var lastSyncedLogId = GetLastSyncedLogID();
|
|
611
|
582
|
|
|
612
|
583
|
// If we already have logs then there is no need to synchronise.
|
|
613
|
584
|
// NOTE: logs are synchronised in reverse from SyncBackwardsFromLogId downwards
|
|
614
|
|
- if ((long)DBExecuteScalar(conn, "SELECT COUNT(*) FROM Logs") == 0)
|
|
|
585
|
+ if ((long)db.ExecuteScalar("SELECT COUNT(*) FROM Logs") == 0)
|
|
615
|
586
|
{
|
|
616
|
|
- UpdateSavedParameter(conn, "SyncBackwardsFromLogId", lastSyncedLogId);
|
|
|
587
|
+ UpdateSavedParameter(db, "SyncBackwardsFromLogId", lastSyncedLogId);
|
|
617
|
588
|
}
|
|
618
|
589
|
|
|
619
|
590
|
// If there are logs on the server then ensure that the log autoincrement value is set to the last
|
|
620
|
591
|
// log ID received by the server so new local logs don't clash with existing log entries on the server.
|
|
621
|
592
|
if (lastSyncedLogId != 0)
|
|
622
|
593
|
{
|
|
623
|
|
- lock (Globals.DBWriteLock)
|
|
|
594
|
+ lock (db.WriteLock)
|
|
624
|
595
|
{
|
|
625
|
|
- using (var trans = conn.BeginTransaction())
|
|
|
596
|
+ using (var trans = db.BeginTransaction())
|
|
626
|
597
|
{
|
|
627
|
|
- using (var command = new SQLiteCommand(conn))
|
|
|
598
|
+ using (var command = db.CreateCommand(trans, new SQLiteParameter("@Id", lastSyncedLogId)))
|
|
628
|
599
|
{
|
|
629
|
|
- command.Transaction = trans;
|
|
630
|
|
- command.Parameters.AddWithValue("@Id", lastSyncedLogId);
|
|
631
|
|
-
|
|
632
|
600
|
command.CommandText = "UPDATE sqlite_sequence SET seq=CASE WHEN IFNULL(seq, 0)<@Id THEN @Id ELSE seq END WHERE name='Logs'";
|
|
633
|
601
|
if (command.ExecuteNonQuery() == 0)
|
|
634
|
602
|
{
|
|
|
@@ -641,7 +609,7 @@ namespace BulkPrinting
|
|
641
|
609
|
}
|
|
642
|
610
|
}
|
|
643
|
611
|
|
|
644
|
|
- UpdateSavedParameter(conn, "LoggingInitialised", true);
|
|
|
612
|
+ UpdateSavedParameter(db, "LoggingInitialised", true);
|
|
645
|
613
|
}
|
|
646
|
614
|
}
|
|
647
|
615
|
|
|
|
@@ -659,27 +627,26 @@ namespace BulkPrinting
|
|
659
|
627
|
const int vendorEventPageSize = 1000;
|
|
660
|
628
|
const int retryInterval = 30000;
|
|
661
|
629
|
|
|
662
|
|
- var conn = Globals.DBConnection;
|
|
663
|
|
- //using (var conn = OpenDBConnection())
|
|
|
630
|
+ var db = Globals.DB;
|
|
|
631
|
+ //using (var db = OpenDBConnection())
|
|
664
|
632
|
{
|
|
665
|
633
|
bool cancelled = false;
|
|
666
|
|
- int maxRemoteId = GetSavedParameterAsInt(conn, "SyncBackwardsFromLogId");
|
|
|
634
|
+ int maxRemoteId = GetSavedParameterAsInt(db, "SyncBackwardsFromLogId");
|
|
667
|
635
|
while (!cancelled && (maxRemoteId > 0))
|
|
668
|
636
|
{
|
|
669
|
637
|
var EventPage = new Page<VendorEvent>();
|
|
670
|
638
|
if (RESTRequest(ref EventPage, String.Format("/api/vendorevents/?maxRemoteId={0}&pageSize={1}", maxRemoteId, vendorEventPageSize)))
|
|
671
|
639
|
{
|
|
672
|
|
- lock (Globals.DBWriteLock)
|
|
|
640
|
+ lock (db.WriteLock)
|
|
673
|
641
|
{
|
|
674
|
|
- using (var trans = conn.BeginTransaction())
|
|
|
642
|
+ using (var trans = db.BeginTransaction())
|
|
675
|
643
|
{
|
|
676
|
|
- using (var insertCommand = new SQLiteCommand(
|
|
|
644
|
+ using (var insertCommand = db.CreateCommand(
|
|
677
|
645
|
"INSERT INTO Logs (Id, UserId, VoucherId, EventDate, EventType, Retry) " +
|
|
678
|
646
|
"VALUES (@id, @userid, @voucherid, @eventdate, @eventtype, @retry)",
|
|
679
|
|
- conn, trans))
|
|
680
|
|
- using (var updateCommand = new SQLiteCommand(
|
|
681
|
|
- "UPDATE Parameters SET Value=@Value WHERE Key='SyncBackwardsFromLogId'",
|
|
682
|
|
- conn, trans))
|
|
|
647
|
+ trans))
|
|
|
648
|
+ using (var updateCommand = db.CreateCommand(
|
|
|
649
|
+ "UPDATE Parameters SET Value=@Value WHERE Key='SyncBackwardsFromLogId'", trans))
|
|
683
|
650
|
{
|
|
684
|
651
|
foreach (VendorEvent Event in EventPage.Items)
|
|
685
|
652
|
{
|
|
|
@@ -756,8 +723,8 @@ namespace BulkPrinting
|
|
756
|
723
|
const int uploadPageSize = 1000;
|
|
757
|
724
|
var retryInterval = 30000;
|
|
758
|
725
|
|
|
759
|
|
- var conn = Globals.DBConnection;
|
|
760
|
|
- //using (var conn = OpenDBConnection())
|
|
|
726
|
+ var db = Globals.DB;
|
|
|
727
|
+ //using (var db = OpenDBConnection())
|
|
761
|
728
|
{
|
|
762
|
729
|
var cancelled = false;
|
|
763
|
730
|
while (! cancelled)
|
|
|
@@ -765,7 +732,7 @@ namespace BulkPrinting
|
|
765
|
732
|
var uploadFailed = false;
|
|
766
|
733
|
int lastSyncedLogId = GetLastSyncedLogID();
|
|
767
|
734
|
|
|
768
|
|
- var result = DBExecuteScalar(conn, "SELECT MAX(Id) FROM Logs")?.ToString() ?? "0";
|
|
|
735
|
+ var result = db.ExecuteScalar("SELECT MAX(Id) FROM Logs")?.ToString() ?? "0";
|
|
769
|
736
|
if (string.IsNullOrWhiteSpace(result))
|
|
770
|
737
|
{
|
|
771
|
738
|
result = "0";
|
|
|
@@ -776,9 +743,9 @@ namespace BulkPrinting
|
|
776
|
743
|
{
|
|
777
|
744
|
List<RemoteVendorEvent> eventList = new List<RemoteVendorEvent>();
|
|
778
|
745
|
int counter = 0;
|
|
779
|
|
- using (var Command = new SQLiteCommand("Select Id,UserId,VoucherId,EventDate,EventType From Logs WHERE Id > @id", conn))
|
|
|
746
|
+ using (var Command = db.CreateCommand("Select Id,UserId,VoucherId,EventDate,EventType From Logs WHERE Id > @id",
|
|
|
747
|
+ new SQLiteParameter("@id", lastSyncedLogId)))
|
|
780
|
748
|
{
|
|
781
|
|
- Command.Parameters.AddWithValue("@id", lastSyncedLogId);
|
|
782
|
749
|
using (SQLiteDataReader read = Command.ExecuteReader())
|
|
783
|
750
|
{
|
|
784
|
751
|
while (read.Read())
|
|
|
@@ -818,7 +785,7 @@ namespace BulkPrinting
|
|
818
|
785
|
if (counter > 0)
|
|
819
|
786
|
{
|
|
820
|
787
|
var response = new RemoteVendorEventResponse();
|
|
821
|
|
- if (! RESTRequest(eventList, ref response, "/api/vendorevents/"))
|
|
|
788
|
+ if (!RESTRequest(eventList, ref response, "/api/vendorevents/"))
|
|
822
|
789
|
{
|
|
823
|
790
|
uploadFailed = true;
|
|
824
|
791
|
}
|
|
|
@@ -861,15 +828,15 @@ namespace BulkPrinting
|
|
861
|
828
|
}
|
|
862
|
829
|
}
|
|
863
|
830
|
|
|
864
|
|
- public static void LogBulkEvents(SQLiteConnection conn, List<EventLog> EventLogs)
|
|
|
831
|
+ public static void LogBulkEvents(DBHelper db, List<EventLog> EventLogs)
|
|
865
|
832
|
{
|
|
866
|
833
|
string Sql = "INSERT INTO Logs (UserId, VoucherId, EventDate, EventType, Retry) VALUES (@userid, @voucherid, @eventdate, @eventtype, @retry)";
|
|
867
|
834
|
|
|
868
|
|
- lock (Globals.DBWriteLock)
|
|
|
835
|
+ lock (db.WriteLock)
|
|
869
|
836
|
{
|
|
870
|
|
- using (var trans = conn.BeginTransaction())
|
|
|
837
|
+ using (var trans = db.BeginTransaction())
|
|
871
|
838
|
{
|
|
872
|
|
- using (var Command = new SQLiteCommand(Sql, conn, trans))
|
|
|
839
|
+ using (var Command = db.CreateCommand(Sql, trans))
|
|
873
|
840
|
{
|
|
874
|
841
|
foreach (var EventLog in EventLogs)
|
|
875
|
842
|
{
|
|
|
@@ -892,9 +859,9 @@ namespace BulkPrinting
|
|
892
|
859
|
}
|
|
893
|
860
|
}
|
|
894
|
861
|
|
|
895
|
|
- public static void LogEvent(SQLiteConnection conn, VendorEvent.VendorEventType EventType, int? VoucherId = null, bool Retry = false)
|
|
|
862
|
+ public static void LogEvent(DBHelper db, VendorEvent.VendorEventType EventType, int? VoucherId = null, bool Retry = false)
|
|
896
|
863
|
{
|
|
897
|
|
- DBExecuteNonQuery(conn,
|
|
|
864
|
+ db.ExecuteNonQuery(
|
|
898
|
865
|
"INSERT INTO Logs (UserId, VoucherId, EventDate, EventType, Retry) " +
|
|
899
|
866
|
"VALUES (@userid, @voucherid, @eventdate, @eventtype, @retry)",
|
|
900
|
867
|
new SQLiteParameter("@userid", Globals.SessionData.Credentials.Payload.User.Id),
|
|
|
@@ -908,12 +875,12 @@ namespace BulkPrinting
|
|
908
|
875
|
}
|
|
909
|
876
|
}
|
|
910
|
877
|
|
|
911
|
|
- private static void SaveCurrentUserUsage(SQLiteConnection conn) {
|
|
912
|
|
- DBExecuteNonQuery(conn,
|
|
|
878
|
+ private static void SaveCurrentUserUsage(DBHelper db) {
|
|
|
879
|
+ db.ExecuteNonQuery(
|
|
913
|
880
|
"DELETE FROM AccessControlTracking WHERE UserID = @userid",
|
|
914
|
881
|
new SQLiteParameter("@userid", Globals.SessionData.Credentials.Payload.User.Id));
|
|
915
|
882
|
|
|
916
|
|
- DBExecuteNonQuery(conn,
|
|
|
883
|
+ db.ExecuteNonQuery(
|
|
917
|
884
|
"INSERT INTO AccessControlTracking (UserID, Date, Permission, CurrentUsage) VALUES " +
|
|
918
|
885
|
"(@userid, datetime('now'), 'OfflinePrint', @offlineprintvalue)," +
|
|
919
|
886
|
"(@userid, datetime('now'), 'OfflineReprint', @offlinereprintvalue)," +
|
|
|
@@ -930,7 +897,7 @@ namespace BulkPrinting
|
|
930
|
897
|
new SQLiteParameter("@bulkexport", Globals.UserCurrentUsage.BulkExportValue));
|
|
931
|
898
|
}
|
|
932
|
899
|
|
|
933
|
|
- public static void AddUserUsage(SQLiteConnection conn, UserLimits.UserLimitTypes UserLimitType, decimal Value) {
|
|
|
900
|
+ public static void AddUserUsage(DBHelper db, UserLimits.UserLimitTypes UserLimitType, decimal Value) {
|
|
934
|
901
|
DateTime ServerDate = Globals.SessionData.Credentials.Payload.Date;
|
|
935
|
902
|
DateTime Today = DateTime.Now;
|
|
936
|
903
|
if (ServerDate.Date != Today.Date) //prevent system time tampering
|
|
|
@@ -961,7 +928,7 @@ namespace BulkPrinting
|
|
961
|
928
|
}
|
|
962
|
929
|
|
|
963
|
930
|
//Immediately write to db to avoid cheating
|
|
964
|
|
- SaveCurrentUserUsage(conn);
|
|
|
931
|
+ SaveCurrentUserUsage(db);
|
|
965
|
932
|
}
|
|
966
|
933
|
|
|
967
|
934
|
public static Boolean IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes UserLimitType, decimal Value) {
|
|
|
@@ -1080,31 +1047,31 @@ namespace BulkPrinting
|
|
1080
|
1047
|
return 0;
|
|
1081
|
1048
|
}
|
|
1082
|
1049
|
|
|
1083
|
|
- private static int GetVoucherPrintCountFromLogs(SQLiteConnection conn, int VoucherId)
|
|
|
1050
|
+ private static int GetVoucherPrintCountFromLogs(DBHelper db, int VoucherId)
|
|
1084
|
1051
|
{
|
|
1085
|
|
- return int.Parse(DBExecuteScalar(conn,
|
|
|
1052
|
+ return int.Parse(db.ExecuteScalar(
|
|
1086
|
1053
|
"SELECT COUNT(*) FROM Logs WHERE VoucherId=@voucherid AND EventType=@eventtype",
|
|
1087
|
1054
|
new SQLiteParameter("@voucherid", VoucherId),
|
|
1088
|
1055
|
new SQLiteParameter("@eventtype", (int)VendorEvent.VendorEventType.PrintVoucher)).ToString());
|
|
1089
|
1056
|
}
|
|
1090
|
1057
|
|
|
1091
|
|
- private static DateTime GetVoucherFirstPrintDateFromLogs(SQLiteConnection conn, int VoucherId)
|
|
|
1058
|
+ private static DateTime GetVoucherFirstPrintDateFromLogs(DBHelper db, int VoucherId)
|
|
1092
|
1059
|
{
|
|
1093
|
|
- return (DateTime)DBExecuteScalar(conn,
|
|
|
1060
|
+ return (DateTime)db.ExecuteScalar(
|
|
1094
|
1061
|
"SELECT MIN(EventDate) FROM Logs WHERE VoucherId=@voucherid AND EventType=@eventtype",
|
|
1095
|
1062
|
new SQLiteParameter("@voucherid", VoucherId),
|
|
1096
|
1063
|
new SQLiteParameter("@eventtype", (int)VendorEvent.VendorEventType.PrintVoucher));
|
|
1097
|
1064
|
}
|
|
1098
|
1065
|
|
|
1099
|
|
- private static decimal GetVoucherFaceValue(SQLiteConnection conn, int VoucherId)
|
|
|
1066
|
+ private static decimal GetVoucherFaceValue(DBHelper db, int VoucherId)
|
|
1100
|
1067
|
{
|
|
1101
|
|
- return (decimal)DBExecuteScalar(conn,
|
|
|
1068
|
+ return (decimal)db.ExecuteScalar(
|
|
1102
|
1069
|
"SELECT b.FaceValue FROM Voucher v LEFT JOIN Batch b on v.BatchId = b.Id WHERE v.Id=@voucherid",
|
|
1103
|
1070
|
new SQLiteParameter("@voucherid", VoucherId));
|
|
1104
|
1071
|
}
|
|
1105
|
1072
|
|
|
1106
|
1073
|
|
|
1107
|
|
- private static UserLimits CalculateUsageFromLogs(SQLiteConnection conn) {
|
|
|
1074
|
+ private static UserLimits CalculateUsageFromLogs(DBHelper db) {
|
|
1108
|
1075
|
UserLimits CalculateUsage = new UserLimits();
|
|
1109
|
1076
|
CalculateUsage.OfflinePrintValue = 0;
|
|
1110
|
1077
|
CalculateUsage.OfflineReprintValue = 0;
|
|
|
@@ -1113,12 +1080,12 @@ namespace BulkPrinting
|
|
1113
|
1080
|
var CurrentDate = DateTime.Today.Date;
|
|
1114
|
1081
|
CultureInfo IVC = CultureInfo.InvariantCulture;
|
|
1115
|
1082
|
string Sql = "SELECT VoucherId FROM Logs WHERE UserId=@userid AND EventDate BETWEEN @eventdatea AND @eventdateb AND EventType=@eventtype";
|
|
1116
|
|
- using (var Command = new SQLiteCommand(Sql, conn))
|
|
|
1083
|
+ using (var Command = db.CreateCommand(Sql,
|
|
|
1084
|
+ new SQLiteParameter("@userid", Globals.SessionData.Credentials.Payload.User.Id),
|
|
|
1085
|
+ new SQLiteParameter("@eventdatea", CurrentDate.ToString("yyyy-MM-dd 00:00:00", IVC)),
|
|
|
1086
|
+ new SQLiteParameter("@eventdateb", CurrentDate.ToString("yyyy-MM-dd 23:59:59", IVC)),
|
|
|
1087
|
+ new SQLiteParameter("@eventtype", (int)VendorEvent.VendorEventType.PrintVoucher)))
|
|
1117
|
1088
|
{
|
|
1118
|
|
- Command.Parameters.AddWithValue("@userid", Globals.SessionData.Credentials.Payload.User.Id);
|
|
1119
|
|
- Command.Parameters.AddWithValue("@eventdatea", CurrentDate.ToString("yyyy-MM-dd 00:00:00", IVC));
|
|
1120
|
|
- Command.Parameters.AddWithValue("@eventdateb", CurrentDate.ToString("yyyy-MM-dd 23:59:59", IVC));
|
|
1121
|
|
- Command.Parameters.AddWithValue("@eventtype", (int)VendorEvent.VendorEventType.PrintVoucher);
|
|
1122
|
1089
|
int VoucherId;
|
|
1123
|
1090
|
decimal VoucherFaceValue;
|
|
1124
|
1091
|
using (SQLiteDataReader read = Command.ExecuteReader())
|
|
|
@@ -1126,11 +1093,11 @@ namespace BulkPrinting
|
|
1126
|
1093
|
while (read.Read())
|
|
1127
|
1094
|
{
|
|
1128
|
1095
|
VoucherId = read.GetInt32(0);
|
|
1129
|
|
- VoucherFaceValue = GetVoucherFaceValue(conn, VoucherId);
|
|
|
1096
|
+ VoucherFaceValue = GetVoucherFaceValue(db, VoucherId);
|
|
1130
|
1097
|
//Don't distinguish between online and offline usage for log recalculations
|
|
1131
|
1098
|
CalculateUsage.OfflinePrintValue += VoucherFaceValue;
|
|
1132
|
1099
|
CalculateUsage.OnlinePrintValue += VoucherFaceValue;
|
|
1133
|
|
- if (GetVoucherPrintCountFromLogs(conn, VoucherId) > 1)
|
|
|
1100
|
+ if (GetVoucherPrintCountFromLogs(db, VoucherId) > 1)
|
|
1134
|
1101
|
{ //For reprinted vouchers just assume all prints were reprints - log recalculations should be stricter to prevent cheating
|
|
1135
|
1102
|
CalculateUsage.OfflineReprintValue += VoucherFaceValue;
|
|
1136
|
1103
|
CalculateUsage.OnlineReprintValue += VoucherFaceValue;
|
|
|
@@ -1141,16 +1108,16 @@ namespace BulkPrinting
|
|
1141
|
1108
|
return CalculateUsage;
|
|
1142
|
1109
|
}
|
|
1143
|
1110
|
|
|
1144
|
|
- public static void InitialiseUserLimits(SQLiteConnection conn)
|
|
|
1111
|
+ public static void InitialiseUserLimits(DBHelper db)
|
|
1145
|
1112
|
{
|
|
1146
|
1113
|
Globals.UserCurrentUsage = new UserLimits();
|
|
1147
|
1114
|
UserLimits CurrentUserLimits = new UserLimits();
|
|
1148
|
1115
|
|
|
1149
|
|
- int Result = int.Parse(DBExecuteScalar(conn,
|
|
|
1116
|
+ int Result = int.Parse(db.ExecuteScalar(
|
|
1150
|
1117
|
"SELECT COUNT(*) FROM AccessControlTracking WHERE UserID = @userid",
|
|
1151
|
1118
|
new SQLiteParameter("@userid", Globals.SessionData.Credentials.Payload.User.Id)).ToString());
|
|
1152
|
1119
|
|
|
1153
|
|
- object ResultDate = DBExecuteScalar(conn,
|
|
|
1120
|
+ object ResultDate = db.ExecuteScalar(
|
|
1154
|
1121
|
"SELECT Date FROM AccessControlTracking WHERE UserID = @userid",
|
|
1155
|
1122
|
new SQLiteParameter("@userid", Globals.SessionData.Credentials.Payload.User.Id));
|
|
1156
|
1123
|
|
|
|
@@ -1163,17 +1130,17 @@ namespace BulkPrinting
|
|
1163
|
1130
|
}
|
|
1164
|
1131
|
if (DatePassed)
|
|
1165
|
1132
|
{ //Fresh day since last db write or data missing - fetch usage from logs incase of tampering
|
|
1166
|
|
- Globals.UserCurrentUsage = CalculateUsageFromLogs(conn);
|
|
1167
|
|
- SaveCurrentUserUsage(conn); //Immediately write calculated usage to DB
|
|
|
1133
|
+ Globals.UserCurrentUsage = CalculateUsageFromLogs(db);
|
|
|
1134
|
+ SaveCurrentUserUsage(db); //Immediately write calculated usage to DB
|
|
1168
|
1135
|
}
|
|
1169
|
1136
|
else
|
|
1170
|
1137
|
{
|
|
1171
|
1138
|
if (Result == 6)
|
|
1172
|
1139
|
{
|
|
1173
|
1140
|
var Sql = "SELECT UserID, Permission, CurrentUsage FROM AccessControlTracking WHERE UserID = @userid";
|
|
1174
|
|
- using (var Command = new SQLiteCommand(Sql, conn))
|
|
|
1141
|
+ using (var Command = db.CreateCommand(Sql,
|
|
|
1142
|
+ new SQLiteParameter("@userid", Globals.SessionData.Credentials.Payload.User.Id)))
|
|
1175
|
1143
|
{
|
|
1176
|
|
- Command.Parameters.AddWithValue("@userid", Globals.SessionData.Credentials.Payload.User.Id);
|
|
1177
|
1144
|
using (SQLiteDataReader read = Command.ExecuteReader())
|
|
1178
|
1145
|
{
|
|
1179
|
1146
|
while (read.Read())
|
|
|
@@ -1205,17 +1172,17 @@ namespace BulkPrinting
|
|
1205
|
1172
|
}
|
|
1206
|
1173
|
else
|
|
1207
|
1174
|
{
|
|
1208
|
|
- Globals.UserCurrentUsage = CalculateUsageFromLogs(conn);
|
|
1209
|
|
- SaveCurrentUserUsage(conn); //Immediately write calculated usage to DB
|
|
|
1175
|
+ Globals.UserCurrentUsage = CalculateUsageFromLogs(db);
|
|
|
1176
|
+ SaveCurrentUserUsage(db); //Immediately write calculated usage to DB
|
|
1210
|
1177
|
}
|
|
1211
|
1178
|
}
|
|
1212
|
1179
|
}
|
|
1213
|
1180
|
|
|
1214
|
|
- public static int GetNumberOfUnprintedVouchersInRange(SQLiteConnection conn, int StartSeqNo, int EndSeqNo, int BatchId) {
|
|
|
1181
|
+ public static int GetNumberOfUnprintedVouchersInRange(DBHelper db, int StartSeqNo, int EndSeqNo, int BatchId) {
|
|
1215
|
1182
|
int UnprintedVouchers = 0;
|
|
1216
|
1183
|
int VoucherId;
|
|
1217
|
1184
|
string Sql = "SELECT Id FROM Voucher WHERE SequenceNumber=@sequencenumber AND BatchId=@batchid";
|
|
1218
|
|
- using (var Command = new SQLiteCommand(Sql, conn))
|
|
|
1185
|
+ using (var Command = db.CreateCommand(Sql))
|
|
1219
|
1186
|
{
|
|
1220
|
1187
|
for (int SeqNo = StartSeqNo; SeqNo <= EndSeqNo; SeqNo++)
|
|
1221
|
1188
|
{
|
|
|
@@ -1223,7 +1190,7 @@ namespace BulkPrinting
|
|
1223
|
1190
|
Command.Parameters.AddWithValue("@sequencenumber", SeqNo);
|
|
1224
|
1191
|
Command.Parameters.AddWithValue("@batchid", BatchId);
|
|
1225
|
1192
|
VoucherId = int.Parse(Command.ExecuteScalar().ToString());
|
|
1226
|
|
- int NumPrintedVouchers = GetVoucherPrintCountFromLogs(conn, VoucherId);
|
|
|
1193
|
+ int NumPrintedVouchers = GetVoucherPrintCountFromLogs(db, VoucherId);
|
|
1227
|
1194
|
if (NumPrintedVouchers == 0)
|
|
1228
|
1195
|
{
|
|
1229
|
1196
|
UnprintedVouchers++;
|