using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Printing; using System.Globalization; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using System.Windows.Forms; namespace Neo.Legitimate.Printing { class AppReceiptPrinter { #region Properties private string ReceiptNumber; private string cashierReceiptNumber; private string paymentType; private string applicantData; private string receiptItems; private double total; private string province; private string printedBy; private DateTime printedDate; private string AssociationName; private short copies; private string printerPath; private int height; private int width; private int leftMargin; private bool preview; private bool isReprint; private Font normal = new Font("Arial", 8); private Font bold = new Font("Arial", 8, FontStyle.Bold); //private #endregion #region ReceiptPrinter public AppReceiptPrinter(string ReceiptNumber, string cashierReceiptNumber, string paymentType, string applicantData, string receiptItems, string province, string associationName, string printedBy, short copies, string printerPath, int height, int width, bool preview, bool isReprint) { this.ReceiptNumber = ReceiptNumber; this.AssociationName = associationName; this.cashierReceiptNumber = cashierReceiptNumber; this.paymentType = paymentType; this.applicantData = applicantData; this.receiptItems = receiptItems; this.province = province; this.total = 0; this.printedBy = printedBy; this.printedDate = System.DateTime.Now; this.copies = copies; this.printerPath = printerPath; this.height = height; //in mm this.width = width; //in mm leftMargin = 20; this.preview = preview; this.isReprint = isReprint; } #endregion #region Print public void Print() { PrintDocument tmpprndoc = new PrintDocument(); tmpprndoc.PrinterSettings.Copies = this.copies; tmpprndoc.PrintPage += new PrintPageEventHandler(toPrinter); tmpprndoc.PrinterSettings.PrinterName = printerPath; PaperSize CustomSize1 = new PaperSize("A4", Convert.ToInt32(width / 0.254), Convert.ToInt32(height / 0.254)); //convert to 100ths of an inch //, 394, 197); tmpprndoc.DefaultPageSettings.PaperSize = CustomSize1; if (preview) { PrintPreviewDialog tmpprdiag = new PrintPreviewDialog(); tmpprdiag.Document = tmpprndoc; tmpprdiag.ShowDialog(); } else { tmpprndoc.Print(); } } #endregion #region toPrinter private void toPrinter(Object sender, PrintPageEventArgs e) { var receiptItems = fillArray(); Graphics g = e.Graphics; Brush br = new SolidBrush(Color.Black); Pen pen = new Pen(br, 2); Pen recPen = new Pen(br, 1); Rectangle rec = new Rectangle(leftMargin, 380, 115, 30); Rectangle textRec = new Rectangle(leftMargin + 10, rec.Y + 8, rec.Width - 15, rec.Height); var currentY = 0; #region Receipt Item Index variables var rdApplicationNumberIndex = 0; var rdIDNumberOrBusinessRegistrationNoIndex = 1; var rdApplicationDescriptionIndex = 3; var rdCostIndex = 5; var rdAssociationNameIndex = 6; var rdVehicleRegNumberIndex = 7; var rdVehicleChassisNumberIndex = 8; var rdRegionIndex = 9; var rdExistingVehicleRegNumberIndex = 10; var rdExistingVehicleChassisNumberIndex = 11; #endregion #region Table column width variables var applicationNumberColumnWidth = 105; var paymentForColumnWidth = 150; var vehicleColumnWidth = 180; var amountColumnWidth = 120; #endregion #region Page Header currentY += 5; var logo = (Image)Properties.Resources.ResourceManager.GetObject("TransportDepartmentLogo"); if (logo != null) g.DrawImage(logo, leftMargin - 5, currentY); currentY += 180; g.DrawLine(pen, leftMargin, currentY, leftMargin + 740, currentY); currentY += 10; g.DrawString("Provincial Regulatory Entity: KZN", new Font("Arial", 14, FontStyle.Bold), br, 245, currentY); currentY += 30; g.DrawString("RECEIPT OF PAYMENT", bold, br, 345, currentY); currentY += 30; #endregion #region Sumarry block at the top g.DrawString("Receipt Number :", bold, br, leftMargin, currentY); g.DrawString(ReceiptNumber, normal, br, leftMargin + 175, currentY); currentY += 15; g.DrawString("Payment Type: ", bold, br, leftMargin, currentY); g.DrawString(paymentType.ToUpper(), normal, br, leftMargin + 175, currentY); currentY += 15; g.DrawString("Applicant Name: ", bold, br, leftMargin, currentY); g.DrawString(applicantData, normal, br, leftMargin + 175, currentY); currentY += 15; g.DrawString("ID No./Bus Reg No.: ", bold, br, leftMargin, currentY); g.DrawString(receiptItems[0, rdIDNumberOrBusinessRegistrationNoIndex], normal, br, leftMargin + 175, currentY); currentY += 15; g.DrawString("Association Name: ", bold, br, leftMargin, currentY); g.DrawString(receiptItems[0, rdAssociationNameIndex], normal, br, leftMargin + 175, currentY); currentY += 15; g.DrawString("Province: ", bold, br, leftMargin, currentY); g.DrawString(province.ToUpper(), normal, br, leftMargin + 175, currentY); currentY += 15; g.DrawString("Region: ", bold, br, leftMargin, currentY); g.DrawString(receiptItems[0, rdRegionIndex], normal, br, leftMargin + 175, currentY); #endregion #region table header currentY += 30; rec.Y = currentY; textRec.Y = currentY + 10; rec.Width = applicationNumberColumnWidth; textRec.Width = applicationNumberColumnWidth - 10; g.DrawRectangle(recPen, rec); g.DrawString("Application No.", bold, br, textRec); rec.X = rec.Right; textRec.X = rec.X + 10; rec.Width = paymentForColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString("Application Type", bold, br, textRec); rec.X = rec.Right; textRec.X = rec.X + 10; rec.Width = vehicleColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString("Existing Vehicle", bold, br, textRec); rec.X = rec.Right; textRec.X = rec.X + 10; rec.Width = vehicleColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString("New Vehicle", bold, br, textRec); rec.X = rec.Right; textRec.X = rec.X + 10; rec.Width = amountColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString("Amount Received", bold, br, textRec); #endregion rec.X = leftMargin; textRec.X = rec.X + 10; rec.Y = rec.Bottom; textRec.Y = rec.Y + 4; rec.Height = 35; textRec.Height = 35; total = 0; for (var i = 0; i < receiptItems.Length / 12; i++) { var amount = Double.Parse(receiptItems[i, rdCostIndex], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture); total += amount; if (receiptItems[i, rdApplicationDescriptionIndex].Length > 35) { var tempCheck = Math.Round((double)(receiptItems[i, rdApplicationDescriptionIndex].Length - 35) / 17); rec.Height = (int)(rec.Height + (15 * tempCheck)); textRec.Height = rec.Height; } #region Applicaton number rec.Width = applicationNumberColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString(receiptItems[i, rdApplicationNumberIndex], normal, br, textRec); #endregion #region Application description rec.X = rec.Right; textRec.X = rec.X + 10; rec.Width = paymentForColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString(receiptItems[i, rdApplicationDescriptionIndex], normal, br, textRec); #endregion #region Existing vehicle rec.X = rec.Right; textRec.X = rec.X + 10; rec.Width = vehicleColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); if (!string.IsNullOrEmpty(receiptItems[i, rdExistingVehicleRegNumberIndex]) && receiptItems[i, rdExistingVehicleRegNumberIndex] != "Not Available") { g.DrawString("Registration No. " + receiptItems[i, rdExistingVehicleRegNumberIndex] + Environment.NewLine + "VIN No. " + receiptItems[i, rdExistingVehicleChassisNumberIndex], normal, br, textRec); } else { g.DrawString("Not Available", normal, br, textRec); } #endregion #region New vehicle rec.X = rec.Right; textRec.X = rec.X + 10; rec.Width = vehicleColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); if (!string.IsNullOrEmpty(receiptItems[i, rdVehicleRegNumberIndex]) && receiptItems[i, rdVehicleRegNumberIndex] != "Not Available") { g.DrawString("Registration No. " + receiptItems[i, rdVehicleRegNumberIndex] + Environment.NewLine + "VIN No. " + receiptItems[i, rdVehicleChassisNumberIndex], normal, br, textRec); } else { g.DrawString("Not Available", normal, br, textRec); } #endregion #region Amount rec.X = rec.Right; textRec.X = rec.X + ((amount < 1000) ? 62 : 50); rec.Width = amountColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString(amount.ToString("C", CultureInfo.GetCultureInfo("en-ZA")), normal, br, textRec); #endregion #region Reset row rec.X = leftMargin; textRec.X = rec.X + 10; rec.Y = rec.Bottom; textRec.Y = rec.Y + 4; rec.Height = 35; textRec.Height = rec.Height; #endregion } rec.Width = leftMargin + 595; textRec.Width = rec.Width - 10; textRec.Y = rec.Y + 12; g.DrawRectangle(recPen, rec); g.DrawString("Total Amount Received", normal, br, textRec); rec.X = rec.Right; textRec.X = rec.X + ((total < 1000) ? 62 : 50); ; rec.Width = amountColumnWidth; textRec.Width = rec.Width - 10; g.DrawRectangle(recPen, rec); g.DrawString(total.ToString("C", CultureInfo.GetCultureInfo("en-ZA")), normal, br, textRec); g.DrawString("This receipt does not authorize the conveyance of passengers", bold, br, 250, rec.Bottom + 15); if (isReprint) { g.DrawString("Duplicate issued by:", normal, br, leftMargin, rec.Bottom + 80); } else { g.DrawString("Receipt issued by:", normal, br, leftMargin, rec.Bottom + 80); } g.DrawLine(pen, leftMargin, rec.Bottom + 165, leftMargin + 180, rec.Bottom + 165); g.DrawString(printedBy, normal, br, leftMargin, rec.Bottom + 170); g.DrawString(printedDate.ToString("dd MMM yyyy"), normal, br, leftMargin + 550, rec.Bottom + 150); g.DrawLine(pen, leftMargin + 550, rec.Bottom + 165, leftMargin + 740, rec.Bottom + 165); if (isReprint) { g.DrawString("Date re-printed", normal, br, leftMargin + 550, rec.Bottom + 170); } else { g.DrawString("Date issued", normal, br, leftMargin + 550, rec.Bottom + 170); } #region Test Watermark if (isReprint) { g.TranslateTransform(200, 175); g.RotateTransform(e.PageSettings.Landscape ? 30 : 45); g.DrawString("DUPLICATE", new Font("Arial", 100, FontStyle.Regular), new SolidBrush(Color.FromArgb(64, Color.Black)), 0, 0); } #endregion } #endregion #region fillArray private string[,] fillArray() { var numItems = 1 + receiptItems.Count(c => c == '#'); var rItems = new string[numItems, 12]; var temp = receiptItems.Split('#'); for (var i = 0; i < temp.Length; i++) { var temp2 = temp[i].Split(','); rItems[i, 0] = temp2[0]; rItems[i, 1] = temp2[1]; rItems[i, 2] = temp2[2]; rItems[i, 3] = temp2[3]; rItems[i, 4] = temp2[4]; rItems[i, 5] = temp2[5]; rItems[i, 6] = temp2[6]; rItems[i, 7] = temp2[7]; rItems[i, 8] = temp2[8]; if (temp2.Length >= 10) //some of the legacy data did not have all the fields { rItems[i, 9] = temp2[9]; } else { rItems[i, 9] = "Not Available"; } if (temp2.Length >= 11) { rItems[i, 10] = temp2[10]; } else { rItems[i, 10] = "Not Available"; } if (temp2.Length >= 12) { rItems[i, 11] = temp2[11]; } else { rItems[i, 11] = "Not Available"; } } return rItems; } } #endregion }