Няма описание

ReprintForm.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Data.SQLite;
  3. using System.Windows.Forms;
  4. namespace BulkPrinting
  5. {
  6. public partial class ReprintForm : Form
  7. {
  8. public ReprintForm()
  9. {
  10. InitializeComponent();
  11. }
  12. public string PrintDescription;
  13. public DateTime BatchOrderDate;
  14. public int UnprintedVouchers;
  15. public int TotalVouchers;
  16. public string FaceValue;
  17. public string VoucherTypeName;
  18. public string NetworkName;
  19. public int BatchID;
  20. private Boolean CanReprintThisSession;
  21. private int MinSeqNo;
  22. private int MaxSeqNo;
  23. private int MinPageNo;
  24. private int MaxPageNo;
  25. private void ReprintForm_Load(object sender, EventArgs e)
  26. {
  27. RadioButtonChanged();
  28. CanReprintThisSession =
  29. ((Globals.SessionMode == SessionModes.Online &&
  30. Utility.CheckUserAccess(Utility.UserPermissions.CanReprintOnline)) ||
  31. (Globals.SessionMode == SessionModes.Offline &&
  32. Utility.CheckUserAccess(Utility.UserPermissions.CanReprintOffline)));
  33. lblPrintDescription.Text = PrintDescription;
  34. lblBatchNumber.Text = BatchID.ToString();
  35. lblBatchOrderDate.Text = BatchOrderDate.Date.ToString("dd/MM/yyyy");
  36. lblTotalVouchers.Text = TotalVouchers.ToString();
  37. lblFaceValue.Text = FaceValue;
  38. lblVoucherType.Text = VoucherTypeName;
  39. lblNetwork.Text = NetworkName;
  40. using (var Command = Globals.DB.CreateCommand(
  41. "SELECT MIN(v.SequenceNumber), MAX(v.SequenceNumber) 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",
  42. new SQLiteParameter("@batchid", BatchID),
  43. new SQLiteParameter("@eventtype", VendorEvent.VendorEventType.PrintVoucher)))
  44. {
  45. using (SQLiteDataReader read = Command.ExecuteReader())
  46. {
  47. read.Read();
  48. MinSeqNo = int.Parse(read.GetValue(0).ToString());
  49. MaxSeqNo = int.Parse(read.GetValue(1).ToString());
  50. }
  51. }
  52. // TODO: what if the sequence numbers are contiguous from MinSeqNo to MaxSeqNo.
  53. MinPageNo = 1;
  54. MaxPageNo = (MaxSeqNo - MinSeqNo + 20) / 20;
  55. numFirstSeqNum.Minimum = MinSeqNo;
  56. numFirstSeqNum.Maximum = MaxSeqNo;
  57. numLastSeqNum.Minimum = MinSeqNo;
  58. numLastSeqNum.Maximum = MaxSeqNo;
  59. numFirstPageNum.Minimum = MinPageNo;
  60. numFirstPageNum.Maximum = MaxPageNo;
  61. numLastPageNum.Minimum = MinPageNo;
  62. numLastPageNum.Maximum = MaxPageNo;
  63. lblMinSeqNo.Text = MinSeqNo.ToString();
  64. lblMaxSeqNo.Text = MaxSeqNo.ToString();
  65. }
  66. private void btnCancel_Click(object sender, EventArgs e)
  67. {
  68. Globals.OpenBatches.Remove(BatchID);
  69. this.Close();
  70. }
  71. private void btnNext_Click(object sender, EventArgs e)
  72. {
  73. int SelectedRangeStart = 0;
  74. int SelectedRangeEnd = 0;
  75. int SelectedBatchID = -1;
  76. decimal VoucherCount;
  77. if (rdoRange.Checked)
  78. {
  79. if (numFirstSeqNum.Value < MinSeqNo || numLastSeqNum.Value > MaxSeqNo)
  80. {
  81. MessageBox.Show(String.Format("Valid sequence numbers are from {0} to {1}", MinSeqNo.ToString(), MaxSeqNo.ToString()), "Invalid print value", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  82. return;
  83. }
  84. if (numFirstSeqNum.Value > numLastSeqNum.Value)
  85. {
  86. MessageBox.Show("The first sequence number in the range must be less than or equal to the last.", "Invalid range", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  87. return;
  88. }
  89. if (numLastSeqNum.Value > TotalVouchers)
  90. {
  91. MessageBox.Show("You have a final sequence number higher than the available vouchers to print. Please reduce the last sequence number to equal to or below the total number of vouchers.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  92. return;
  93. }
  94. SelectedBatchID = BatchID;
  95. SelectedRangeStart = (int)numFirstSeqNum.Value;
  96. SelectedRangeEnd = (int)numLastSeqNum.Value;
  97. }
  98. else if (rdoPageRange.Checked)
  99. {
  100. if (numFirstPageNum.Value < MinPageNo || numLastPageNum.Value > MaxPageNo)
  101. {
  102. MessageBox.Show(String.Format("Valid page numbers are from {0} to {1}", MinPageNo.ToString(), MaxPageNo.ToString()), "Invalid print value", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  103. return;
  104. }
  105. if (numFirstPageNum.Value > numLastPageNum.Value)
  106. {
  107. MessageBox.Show("The first page number in the range must be less than or equal to the last.", "Invalid range", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  108. return;
  109. }
  110. SelectedBatchID = BatchID;
  111. SelectedRangeStart = ((int)numFirstPageNum.Value - 1) * 20 + MinSeqNo;
  112. SelectedRangeEnd = (int)numLastPageNum.Value * 20 + MinSeqNo - 1;
  113. if (SelectedRangeEnd > MaxSeqNo)
  114. {
  115. SelectedRangeEnd = MaxSeqNo;
  116. }
  117. }
  118. else if (rdoSerial.Checked) {
  119. using (var command = Globals.DB.CreateCommand("SELECT * FROM Voucher WHERE Serial=@Serial AND PrintCount>0",
  120. new SQLiteParameter("@Serial", txtSerialNum.Text)))
  121. {
  122. using (var reader = command.ExecuteReader())
  123. {
  124. if (reader.Read())
  125. {
  126. SelectedBatchID = (int)reader["BatchID"];
  127. SelectedRangeStart = (int)reader["SequenceNumber"];
  128. SelectedRangeEnd = SelectedRangeStart;
  129. }
  130. }
  131. }
  132. if (SelectedBatchID < 0) {
  133. MessageBox.Show("No voucher with that serial number was found to have been printed. Please provide a previously printed voucher number or use the 'Print' function to print new vouchers.", "Cannot find printed voucher", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  134. return;
  135. }
  136. }
  137. VoucherCount = SelectedRangeEnd - SelectedRangeStart + 1;
  138. decimal VoucherFaceValue = (decimal)Globals.DB.ExecuteScalar(
  139. "SELECT FaceValue FROM Batch WHERE Id=@id",
  140. new SQLiteParameter("@id", BatchID));
  141. int UnprintedVouchersInRange = int.Parse(Globals.DB.ExecuteScalar(
  142. "SELECT COUNT(v.ID) FROM Voucher v LEFT JOIN Logs l ON v.Id=l.VoucherId AND l.EventType=@event WHERE v.BatchId=@batchid AND v.SequenceNumber BETWEEN @startseq AND @endseq AND l.id IS NULL",
  143. new SQLiteParameter("@event", (int) VendorEvent.VendorEventType.PrintVoucher),
  144. new SQLiteParameter("@batchid", BatchID),
  145. new SQLiteParameter("@startseq", SelectedRangeStart),
  146. new SQLiteParameter("@endseq", SelectedRangeEnd)).ToString());
  147. decimal PrintedVoucherValue = (VoucherCount - UnprintedVouchersInRange) * VoucherFaceValue;
  148. decimal UnprintedVoucherValue = UnprintedVouchersInRange * VoucherFaceValue;
  149. if (Globals.SessionMode == SessionModes.Online)
  150. {
  151. if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.OnlinePrint, UnprintedVoucherValue))
  152. {
  153. MessageBox.Show("You have selected more vouchers than your permissions allow you to print today.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  154. return;
  155. }
  156. if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.OnlineReprint, PrintedVoucherValue))
  157. {
  158. MessageBox.Show("You have selected more previously printed vouchers than your permissions allow you to reprint today.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  159. return;
  160. }
  161. }
  162. if (Globals.SessionMode == SessionModes.Offline)
  163. {
  164. if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.OfflinePrint, UnprintedVoucherValue))
  165. {
  166. MessageBox.Show("You have selected more vouchers than your permissions allow you to print today.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  167. return;
  168. }
  169. if (!Utility.IsValueWithinRemainingUserLimit(UserLimits.UserLimitTypes.OfflineReprint, PrintedVoucherValue))
  170. {
  171. MessageBox.Show("You have selected more previously printed vouchers than your permissions allow you to reprint today.", "Cannot print that many vouchers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  172. return;
  173. }
  174. }
  175. if (Globals.MaxPrinter.ChoosePrinter())
  176. {
  177. PrintAlignmentDialog printdialog = new PrintAlignmentDialog();
  178. printdialog.PrintDescription = PrintDescription;
  179. printdialog.BatchID = SelectedBatchID;
  180. printdialog.BatchOrderDate = BatchOrderDate;
  181. printdialog.FaceValue = FaceValue;
  182. printdialog.VoucherTypeName = VoucherTypeName;
  183. printdialog.NetworkName = NetworkName;
  184. printdialog.NumVouchers = null;
  185. printdialog.FirstSeqNo = SelectedRangeStart;
  186. printdialog.LastSeqNo = SelectedRangeEnd;
  187. printdialog.Show();
  188. this.Close();
  189. }
  190. }
  191. private void txtSerialNum_TextChanged(object sender, EventArgs e)
  192. {
  193. }
  194. private void RadioButtonChanged()
  195. {
  196. numFirstSeqNum.Enabled = rdoRange.Checked;
  197. numLastSeqNum.Enabled = rdoRange.Checked;
  198. numFirstPageNum.Enabled = rdoPageRange.Checked;
  199. numLastPageNum.Enabled = rdoPageRange.Checked;
  200. txtSerialNum.Enabled = rdoSerial.Checked;
  201. }
  202. private void rdoRange_CheckedChanged(object sender, EventArgs e)
  203. {
  204. RadioButtonChanged();
  205. }
  206. private void rdoPageRange_CheckedChanged(object sender, EventArgs e)
  207. {
  208. RadioButtonChanged();
  209. }
  210. private void rdoSerial_CheckedChanged(object sender, EventArgs e)
  211. {
  212. RadioButtonChanged();
  213. }
  214. }
  215. }