| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- using System.Data.SQLite;
- using System.Threading;
- using System.Globalization;
- namespace BulkPrinting
- {
- public partial class BatchForm : ObservedForm
- {
- Thread LoadingThread;
- public BatchForm()
- {
- InitializeComponent();
- }
- private class BatchEvent
- {
- public int PrintCount=0;
- public int ReprintCount=0;
- public int ExportCount=0;
- }
- public void PopulateGrid()
- {
- Dictionary<int, BatchEvent> BatchEvents = new Dictionary<int, BatchEvent>();
- 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";
- SQLiteCommand Command = new SQLiteCommand(Sql, Globals.DBConnection);
- Command.Parameters.Add(new SQLiteParameter("@printevent", VendorEvent.VendorEventType.PrintVoucher));
- Command.Parameters.Add(new SQLiteParameter("@exportevent", VendorEvent.VendorEventType.ExportVoucher));
- using (SQLiteDataReader read = Command.ExecuteReader())
- {
- while (read.Read())
- {
- int BatchId = int.Parse(read["BatchId"].ToString());
- if (!BatchEvents.ContainsKey(BatchId))
- {
- BatchEvents.Add(BatchId, new BatchEvent());
- }
- if (read["EventType"].ToString() == "6")
- {
- if (read["Retry"].ToString() == "True")
- {
- BatchEvents[BatchId].ReprintCount += int.Parse(read["Total"].ToString());
- }
- else
- {
- BatchEvents[BatchId].PrintCount += int.Parse(read["Total"].ToString());
- }
- }
- else if (read["EventType"].ToString() == "8")
- {
- BatchEvents[BatchId].ExportCount += int.Parse(read["Total"].ToString());
- }
- }
- }
- bool CanOrder = Utility.CheckUserAccess(Utility.UserPermissions.BulkOrder);
- bool CanExport = Utility.CheckUserAccess(Utility.UserPermissions.BulkExport);
- dgvBatches.Rows.Clear();
- dgvBatches.Columns.Clear();
- dgvBatches.Columns.Add("Id", "ID");
- dgvBatches.Columns.Add("OrderDate", "Order Date");
- if (CanOrder) dgvBatches.Columns.Add("OrderReference", "Order Reference");
- dgvBatches.Columns.Add("NetworkName", "Network Name");
- dgvBatches.Columns.Add("ProductDescription", "Product Description");
- dgvBatches.Columns.Add("VoucherType", "Voucher Type");
- dgvBatches.Columns.Add("FaceValue", "Face Value");
- if (CanOrder) dgvBatches.Columns.Add("RequestedQuantity", "Quantity Ordered");
- dgvBatches.Columns.Add("DeliveredQuantity", "Quantity Delivered");
- if (CanOrder) dgvBatches.Columns.Add("ReadyForDownload", "Batch Ready for Download");
- dgvBatches.Columns.Add("PrintedQuantity", "Quantity Printed");
- if (CanExport) dgvBatches.Columns.Add("Exported", "Exported Batch");
- Sql = "Select Id,OrderDate,OrderReference,NetworkName,ProductDescription,VoucherType,FaceValue,RequestedQuantity,DeliveredQuantity,ReadyForDownload From Batch WHERE OrderDate BETWEEN @startdate AND @enddate";
- SQLiteCommand comm = new SQLiteCommand(Sql, Globals.DBConnection);
- CultureInfo IVC = CultureInfo.InvariantCulture;
- comm.Parameters.Add(new SQLiteParameter("@startdate", dtpFilterStartDate.Value.Date.ToString("yyyy-MM-dd 00:00:00", IVC)));
- comm.Parameters.Add(new SQLiteParameter("@enddate", dtpFilterEndDate.Value.Date.ToString("yyyy-MM-dd 23:59:59", IVC)));
- lblLoading.Hide();
- using (SQLiteDataReader read = comm.ExecuteReader())
- {
- while (read.Read())
- {
- int BatchId = int.Parse(read["Id"].ToString());
- if (!BatchEvents.ContainsKey(BatchId))
- { //For batches with no events
- BatchEvents.Add(BatchId, new BatchEvent());
- }
- int DeliveredQuantity = int.Parse(read["DeliveredQuantity"].ToString());
- if (rdoFilterPrinted.Checked)
- {
- if (BatchEvents[BatchId].PrintCount == 0)
- {
- continue;
- }
- }
- if (rdoFilterUnprinted.Checked) {
- if (BatchEvents[BatchId].PrintCount >= DeliveredQuantity)
- {
- continue;
- }
- }
- if (rdoFilterReprinted.Checked) {
- if (BatchEvents[BatchId].ReprintCount == 0)
- {
- continue;
- }
- }
- if (rdoFilterExported.Checked)
- {
- if (BatchEvents[BatchId].ExportCount == 0)
- {
- continue;
- }
- }
- else {
- if (BatchEvents[BatchId].ExportCount > 0)
- {
- continue;
- }
- }
- var BatchRow = new object[] {
- BatchId,
- read["OrderDate"],
- read["OrderReference"],
- read["NetworkName"].ToString(),
- read["ProductDescription"].ToString(),
- Enum.Parse(typeof(Vouchertype), read["VoucherType"].ToString()).ToString(),
- "R" +((decimal)read["FaceValue"]).ToString("0.##")};
- if (CanOrder)
- BatchRow = BatchRow.Concat(new[] { read["RequestedQuantity"] }).ToArray();
- BatchRow = BatchRow.Concat(new[] { read["DeliveredQuantity"] }).ToArray();
- if (CanOrder)
- BatchRow = BatchRow.Concat(new[] { ((bool)read["ReadyForDownload"] == true ? "Yes" : "No") }).ToArray();
- BatchRow = BatchRow.Concat(new[] { (object)BatchEvents[BatchId].PrintCount }).ToArray();
- if (CanExport)
- BatchRow = BatchRow.Concat(new[] { (BatchEvents[BatchId].ExportCount >0?"Exported":"") }).ToArray();
- dgvBatches.Rows.Add(BatchRow);
- }
- dgvBatches.Sort(dgvBatches.Columns["OrderDate"], ListSortDirection.Descending);
- }
- }
- private void BatchForm_Load(object sender, EventArgs e)
- {
- List<Button> LeftButtonList = new List<Button>();
- List<Button> RightButtonList = new List<Button>();
- LeftButtonList.Add(this.btnLogout);
- LeftButtonList.Add(this.btnOrderReport);
- LeftButtonList.Add(this.btnOrder);
- LeftButtonList.Add(this.btnPrint);
- LeftButtonList.Add(this.btnExport);
- RightButtonList.Add(this.btnViewPINs);
- RightButtonList.Add(this.btnReExport);
- RightButtonList.Add(this.btnReprint);
- foreach (Button b in LeftButtonList) {
- b.Hide();
- }
- foreach (Button b in RightButtonList) {
- b.Hide();
- }
- if (Globals.SessionMode == SessionModes.Online)
- {
- if (!Utility.CheckUserAccess(Utility.UserPermissions.BulkOrder))
- LeftButtonList.Remove(this.btnOrder);
- if (!Utility.CheckUserAccess(Utility.UserPermissions.CanPrintOnline))
- LeftButtonList.Remove(this.btnPrint);
- if (!Utility.CheckUserAccess(Utility.UserPermissions.CanReprintOnline)) {
- RightButtonList.Remove(this.btnReprint);
- rdoFilterReprinted.Hide();
- }
- }
- else if (Globals.SessionMode == SessionModes.Offline)
- {
- LeftButtonList.Remove(this.btnOrder);
- if (!Utility.CheckUserAccess(Utility.UserPermissions.CanPrintOffline))
- LeftButtonList.Remove(this.btnPrint);
- if (!Utility.CheckUserAccess(Utility.UserPermissions.CanReprintOffline)) {
- RightButtonList.Remove(this.btnReprint);
- rdoFilterReprinted.Hide();
- }
- }
- if (!Utility.CheckUserAccess(Utility.UserPermissions.BulkExport)) {
- LeftButtonList.Remove(this.btnExport);
- RightButtonList.Remove(this.btnReExport);
- rdoFilterExported.Hide();
- }
- if (!Utility.CheckUserAccess(Utility.UserPermissions.BulkViewPins))
- RightButtonList.Remove(this.btnViewPINs);
- int ButtonX = 12;
- foreach (Button b in LeftButtonList)
- {
- b.Location = new Point(ButtonX, 0);
- b.Show();
- ButtonX += 106;
- }
- ButtonX = pnlSplitGrids.Width - 378;
- RightButtonList.Reverse();
- foreach (Button b in RightButtonList)
- {
- b.Location = new Point(ButtonX, 0);
- b.Show();
- ButtonX -= 106;
- }
- lblLoading.Show();
- dtpFilterStartDate.Value = DateTime.Now.AddMonths(-3);
- Globals.OpenBatches = new List<int>();
- LoadingThread = new Thread(() =>
- {
- Invoke(new Action(() =>
- {
- this.Enabled = false;
- PopulateGrid();
- this.Enabled = true;
- }));
- if (Globals.SessionMode == SessionModes.Online)
- {
- Utility.SyncAllBatches();
- }
- Invoke(new Action(() =>
- {
- PopulateGrid();
- }));
- });
- LoadingThread.Start();
- Utility.InitialiseUserLimits();
- }
- private void btnOrder_Click(object sender, EventArgs e)
- {
- OrderForm orderform = new OrderForm();
- orderform.SourceForm = this;
- orderform.Show();
- }
- private void btnPrint_Click(object sender, EventArgs e)
- {
- if (dgvBatches.SelectedRows.Count == 0)
- {
- MessageBox.Show("Please choose a batch to print.", "No batch selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- if ((string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["Exported"].Index].Value != "")
- {
- MessageBox.Show("This batch has been exported previously. Exported batches cannot be printed.", "Cannot print batch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- if (Utility.CheckUserAccess(Utility.UserPermissions.BulkOrder)) { //Only check for undownloaded vouchers if the user is able to see them
- if ((string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["ReadyForDownload"].Index].Value == "No") {
- MessageBox.Show("This batch is not ready to downloaded and cannot be printed.", "Cannot print batch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- }
- int BatchID = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["Id"].Index].Value;
- if (Globals.OpenBatches.Contains(BatchID)) {
- MessageBox.Show("A print dialog is already open for this batch","Cannot open print dialog",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
- return;
- }
- this.BeginInvoke((Action)delegate {
- PrintForm PrintFormInstance = new PrintForm();
- Globals.OpenBatches.Add(BatchID);
- int TotalVouchers = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["DeliveredQuantity"].Index].Value;
- int VouchersPrinted = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["PrintedQuantity"].Index].Value;
- PrintFormInstance.PrintDescription=(string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["ProductDescription"].Index].Value;
- PrintFormInstance.BatchOrderDate=(DateTime)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["OrderDate"].Index].Value;
- PrintFormInstance.UnprintedVouchers= TotalVouchers-VouchersPrinted;
- PrintFormInstance.TotalVouchers=TotalVouchers;
- PrintFormInstance.FaceValue=(string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["FaceValue"].Index].Value;
- PrintFormInstance.VoucherTypeName=(string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["VoucherType"].Index].Value;
- PrintFormInstance.BatchID=BatchID;
- PrintFormInstance.NetworkName = (string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["NetworkName"].Index].Value;
- PrintFormInstance.Show();
- });
- }
- private void btnLogout_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void pnlSplitGrids_Paint(object sender, PaintEventArgs e)
- {
- }
- private void BatchForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (Globals.OpenBatches.Count > 0) {
- MessageBox.Show("There are printing jobs in progress - pelase wait for them to complete before logging out.", "Busy printing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- e.Cancel = true;
- return;
- }
- if (LoadingThread.IsAlive)
- {
- LoadingThread.Abort();
- }
- Utility.Logout();
- }
- private void LogoutTimer_Tick(object sender, EventArgs e)
- {
- Utility.Logout();
- }
- public void ResetTimer()
- {
- LogoutTimer.Stop();
- LogoutTimer.Start();
- }
- private void btnReExport_Click(object sender, EventArgs e)
- {
- MessageBox.Show("Please be aware that this function is used for exporting vouchers that have already been exported previously. Use this function with caution.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
- ExportForm exportform = new ExportForm(ExportForm.ExportMode.Reexport);
- exportform.Show();
- }
- private void dtpFilterStartDate_CloseUp(object sender, EventArgs e)
- {
- PopulateGrid();
- }
- private void dtpFilterEndDate_CloseUp(object sender, EventArgs e)
- {
- PopulateGrid();
- }
- private void btnExport_Click(object sender, EventArgs e)
- {
- ExportForm exportform = new ExportForm(ExportForm.ExportMode.Export);
- exportform.Show();
- }
- private void dgvBatches_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- }
- private void rdoFilterUnprinted_CheckedChanged(object sender, EventArgs e)
- {
- if (rdoFilterUnprinted.Checked)
- {
- PopulateGrid();
- btnPrint.Enabled = true;
- btnReprint.Enabled = false;
- }
- }
- private void rdoFilterPrinted_CheckedChanged(object sender, EventArgs e)
- {
- if (rdoFilterPrinted.Checked)
- {
- PopulateGrid();
- btnPrint.Enabled = false;
- btnReprint.Enabled = true;
- }
- }
- private void rdoFilterReprinted_CheckedChanged(object sender, EventArgs e)
- {
- if (rdoFilterReprinted.Checked)
- {
- PopulateGrid();
- btnPrint.Enabled = false;
- btnReprint.Enabled = true;
- }
- }
- private void rdoFilterExported_CheckedChanged(object sender, EventArgs e)
- {
- if (rdoFilterExported.Checked)
- {
- PopulateGrid();
- btnPrint.Enabled = false;
- btnReprint.Enabled = false;
- }
- }
- private void btnOrderReport_Click(object sender, EventArgs e)
- {
- OrderReport OrderReportForm = new OrderReport();
- OrderReportForm.Show();
- }
- private void btnReprint_Click(object sender, EventArgs e)
- {
- if (dgvBatches.SelectedRows.Count == 0) {
- MessageBox.Show("Please choose a batch to reprint.", "No batch selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- MessageBox.Show("Please be aware that this function is used for exporting vouchers that have already been exported previously. Use this function with caution.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
- int BatchID = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["Id"].Index].Value;
- if ((string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["Exported"].Index].Value != "")
- {
- MessageBox.Show("This batch has been exported previously. Exported batches cannot be printed.", "Cannot print batch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- 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);
- Command.Parameters.Add(new SQLiteParameter("@batchid", BatchID));
- Command.Parameters.Add(new SQLiteParameter("@eventtype", VendorEvent.VendorEventType.PrintVoucher));
- int PrintCount = int.Parse(Command.ExecuteScalar().ToString());
- if (PrintCount == 0) {
- 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);
- return;
- }
- if (Utility.CheckUserAccess(Utility.UserPermissions.BulkOrder))
- { //Only check for undownloaded vouchers if the user is able to see them
- if ((string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["ReadyForDownload"].Index].Value == "No")
- {
- MessageBox.Show("This batch is not ready to downloaded and cannot be printed.", "Cannot print batch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- }
- if (Globals.OpenBatches.Contains(BatchID))
- {
- MessageBox.Show("A print dialog is already open for this batch", "Cannot open print dialog", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- this.BeginInvoke((Action)delegate {
- ReprintForm PrintFormInstance = new ReprintForm();
- Globals.OpenBatches.Add(BatchID);
- int TotalVouchers = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["DeliveredQuantity"].Index].Value;
- int VouchersPrinted = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["PrintedQuantity"].Index].Value;
- PrintFormInstance.PrintDescription = (string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["ProductDescription"].Index].Value;
- PrintFormInstance.BatchOrderDate = (DateTime)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["OrderDate"].Index].Value;
- PrintFormInstance.UnprintedVouchers = TotalVouchers - VouchersPrinted;
- PrintFormInstance.TotalVouchers = TotalVouchers;
- PrintFormInstance.FaceValue = (string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["FaceValue"].Index].Value;
- PrintFormInstance.VoucherTypeName = (string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["VoucherType"].Index].Value;
- PrintFormInstance.BatchID = BatchID;
- PrintFormInstance.NetworkName = (string)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["NetworkName"].Index].Value;
- PrintFormInstance.Show();
- });
- }
- private void btnViewPINs_Click(object sender, EventArgs e)
- {
- VoucherForm ViewPINs = new VoucherForm();
- if (dgvBatches.SelectedRows.Count == 0)
- {
- ViewPINs.BatchID = 0;
- }
- else
- {
- ViewPINs.BatchID = (int)dgvBatches.SelectedRows[0].Cells[dgvBatches.Columns["Id"].Index].Value;
- }
- ViewPINs.Show();
- }
- private void btnLogout_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- private void btnOrderReport_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- private void btnOrder_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- private void btnPrint_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- private void btnExport_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- private void btnViewPINs_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- private void btnReExport_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- private void btnReprint_Paint(object sender, PaintEventArgs e)
- {
- Button b = sender as Button;
- if (!b.Enabled)
- {
- e.Graphics.Clear(Color.Gray);
- e.Graphics.DrawImage(b.BackgroundImage, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);
- }
- }
- }
- }
|