using System; using System.Collections.Generic; using System.Linq; using Tanshu.Accounts.Contracts; using Tanshu.Accounts.BI; using System.Windows.Forms; namespace Tanshu.Accounts.Print { enum Align { None = 0, Left = 1, Centre = 2, Right = 4 } public static class Thermal { private static readonly string CutPrinter = System.Configuration.ConfigurationManager.AppSettings["Printer"].Trim().EndsWith("Epson") ? "\n\r\n\r\n\r\n\r\n\r\n\r" + (char)29 + (char)86 + (char)49 : "\n\r" + (char)29 + (char)86 + (char)49; private static readonly bool IsTwenty = System.Configuration.ConfigurationManager.AppSettings["Twenty"].Trim().ToLowerInvariant() == "true"; private const string DrawLine = "\n\r------------------------------------------"; private const string DrawEqual = "\n\r=========================================="; private static string DesignKot(VoucherBO voucher, IEnumerable billItems, string title) { var billText = title; billText += title; billText += DrawLine; billText += string.Format("\n\rKOT ID : {0,-13} {1:dd-MMM-yyyy HH:mm}", voucher.KotID, voucher.Date); billText += string.Format("\n\rTable No.: {0} / {1}", voucher.TableID, voucher.Waiter.Name); billText += DrawLine; billText += "\n\r Qty. x Name "; billText += DrawLine; foreach (var item in billItems) { var additional = item.Quantity - item.Printed; if (additional > 0) { billText += string.Format("\n\r{0,6} x {1,-27} @ {2,6}", additional, item.Name, item.Price); } } billText += DrawLine; if (voucher.Narration != "") { billText += "\n\r" + FormatText(voucher.Narration, 42, false, Align.Centre); billText += DrawLine; } billText += CutPrinter; return billText; } private static string DesignWaiterKot(VoucherBO voucher, IEnumerable billItems) { return DesignKot(voucher, billItems, string.Format("\n\rWAITER WAITER WAITER WAITER WAITER\n\rWAITER WAITER WAITER WAITER WAITER")); } private static string DesignPickupKot(VoucherBO voucher, IEnumerable billItems) { return DesignKot(voucher, billItems, string.Format("\n\rPICK-UP PICK-UP PICK-UP PICK-UP PICK-UP\n\rPICK-UP PICK-UP PICK-UP PICK-UP PICK-UP")); } private static string DesignCustomerKot(VoucherBO voucher, IEnumerable billItems) { return DesignKot(voucher, billItems, string.Format("\n\r Counter / Order Slip ")); } private static string DesignBill(VoucherBO voucher) { var billNo = voucher.BillID.Substring(voucher.BillID.IndexOf("-") + 1); var billText = "\n\r" + FormatText("Bengali Sweets", 42, false, Align.Centre); billText += "\n\r" + FormatText("+ V2C Multi Cuisine", 42, false, Align.Centre); if (!IsTwenty) billText += "\n\r" + FormatText("SCO 1, Pocket 1, Manimajra, Chandigarh", 42, false, Align.Centre); else billText += "\n\r" + FormatText("SCO 399, Sector 20, Panchkula", 42, false, Align.Centre); billText += "\n\r" + FormatText("A Unit of AA Herbal Biotech Pvt. Ltd.", 42, false, Align.Centre); billText += "\n\r" + FormatText("CIN: U24230CH2004PTC026903", 42, false, Align.Centre); billText += "\n\r" + FormatText("Reg Add:705, Indl. Area, Ph II, Chandigarh", 42, false, Align.Centre); if (!IsTwenty) { billText += "\n\r" + FormatText("TIN: 04400023630", 42, false, Align.Centre); billText += "\n\r" + FormatText("Service Tax: AAFCA1727JSD002", 42, false, Align.Centre); } else { billText += "\n\r" + FormatText("TIN: 06892504733", 42, false, Align.Centre); billText += "\n\r" + FormatText("Service Tax: AAFCA1727JSD003", 42, false, Align.Centre); } billText += "\n\r" + FormatText("Retail Invoice", 42, false, Align.Centre); billText += "\n\r"; billText += "\n\r" + "Bill No: " + FormatText(billNo, 13, false, Align.Centre) + string.Format("{0:dd-MMM-yyyy HH:mm:ss}", voucher.Date); billText += "\n\r" + "Table No.: " + voucher.TableID; billText += "\n\r" + "------------------------------------------"; billText += "\n\r" + "Qty. Particulars Price Amount"; billText += "\n\r" + "------------------------------------------"; foreach (var item in voucher.Inventories) { if (Math.Round((item.Rate * (1 + item.Vat + item.ServiceTax)), 0) == 1) { billText += "\n\r" + " "; billText += FormatText(item.ProductName, 22, false, Align.Left) + " "; billText += " "; billText += FormatBillNum(item.Rate * item.Quantity, 6); } else { billText += "\n\r" + FormatBillNum(item.Quantity, 5) + " "; billText += FormatText(item.ProductName, 22, false, Align.Left) + " "; billText += FormatBillNum(item.Rate, 6) + " "; billText += FormatBillNum(item.Rate * item.Quantity, 6); } } billText += "\n\r" + "------------------------------------------"; decimal amount = voucher.Inventories.Sum(b => b.Vat * b.Quantity * b.Rate * (1 - b.Discount)); if (amount != 0) billText += "\n\r" + FormatText("VAT : ", 33, false, Align.Right) + FormatBillNum(amount, 9); amount = voucher.Inventories.Sum(b => b.ServiceTax * b.Quantity * b.Rate * (1 - b.Discount)); if (amount != 0) billText += "\n\r" + FormatText("Service Tax : ", 33, false, Align.Right) + FormatBillNum(amount, 9); amount = voucher.Inventories.Sum(b => b.Quantity * b.Rate * b.Discount); if (amount != 0) billText += "\n\r" + FormatText("Discount : ", 33, false, Align.Right) + FormatBillNum(amount, 9); decimal advance = 0; if (voucher.AdvanceID.HasValue) { advance = voucher.Advance.Amount; billText += "\n\r" + FormatText("Less Advance : ", 33, false, Align.Right) + FormatBillNum(advance, 9); } amount = voucher.Inventories.Sum(b => b.Amount) - advance; billText += string.Format("\n\r Final Amount : {0,9:#,##0.00;(#,##0.00);0}", Math.Round(amount, 0)); billText += DrawLine; if (voucher.Narration != "") { billText += "\n\r" + FormatText(voucher.Narration, 42, false, Align.Centre); billText += DrawLine; } if (voucher.Customer.Name != "Cash") { billText += "\n\r" + voucher.Customer.Name; billText += string.Format("\n\r{0}\n\r{1}", voucher.Customer.Phone, voucher.Customer.Address); billText += DrawLine; } billText += "\n\r" + "Cashier : " + voucher.User.Name + " / " + voucher.Waiter.Name; if (!IsTwenty) billText += "\n\r" + " For Home Delivery: 0172-3242030, 3243040 "; else billText += "\n\r" + " For Home Delivery: 0172-3535000, 3536000 "; billText += CutPrinter; return billText; } private static string FormatText(string inputString, int width, Boolean multiline, Align alignment) { if ((multiline) && (inputString.Length > width)) { var newString = ""; var lines = Convert.ToInt32(Math.Floor(Convert.ToDecimal(inputString.Length / width))); for (var i = 0; i < lines; i++) newString += inputString.Substring(i * width, width) + "\n\r"; newString += FormatText(inputString.Substring(lines * width), width, true, alignment); return newString; } if (inputString.Length > width) return inputString.Substring(0, width); if (alignment == Align.None) return inputString; if (alignment == Align.Left) return string.Format("{0,-" + width.ToString() + "}", inputString); if (alignment == Align.Centre) { int left = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((width - inputString.Length) / 2))) + inputString.Length; inputString = string.Format("{0,-" + left.ToString() + "}", inputString); return string.Format("{0," + width.ToString() + "}", inputString); } if (alignment == Align.Right) return string.Format("{0," + width.ToString() + "}", inputString); return inputString; } private static string FormatBillNum(decimal amount, int length) { var temp = String.Format("{0:#,##0.00;(#,##0.00);0}", amount); return string.Format("{0," + length.ToString() + "}", temp); } private static bool PrintRaw(string text, string documentName) { #if (DEBUG) MessageBox.Show(text, documentName); return true; #else var printerName = System.Configuration.ConfigurationManager.AppSettings["Printer"].Trim(); if (!RawPrinterHelper.PrintString(printerName, documentName, text, new char[] { ';' })) { System.Windows.Forms.MessageBox.Show("Error in PrintRAW Function. Please Report immediately"); } return true; #endif } #region Print Cash Total public static Boolean PrintCash(Dictionary amount) { string printText; int total = 0; printText = FormatText(CurrentUser.user.Name, 42, false, Align.Centre); printText += DrawLine; printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy HH:mm:ss}", DateTime.Now), 42, false, Align.Centre); foreach (int key in amount.Keys.OrderByDescending(k => k)) { printText += CashLine(amount, key); total += amount[key] * key; } printText += "\n\r" + DrawEqual; printText += string.Format("\n\r Total = {0,10:#,##0}", total); printText += DrawLine; printText += CutPrinter; return PrintRaw(printText, "Closing/Opening for " + CurrentUser.user.Name); } public static Boolean PrintSale(List det, DateTime startDate, DateTime endDate) { var printText = FormatText(CurrentUser.user.Name, 42, false, Align.Centre); printText += DrawLine; printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy HH:mm:ss}", DateTime.Now), 42, false, Align.Centre); printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy} to {1:dd-MMM-yyyy}", startDate, endDate), 42, false, Align.Centre); printText += DrawLine; foreach (var d in det) { if (d.Name.Length > 30) d.Name = d.Name.Substring(0, 30); if (d.Vat != 0 || d.ServiceTax != 0) { printText += string.Format("\n\r{0,-17} {1,8:#,##0} {2,7:#,##0} {3,7:#,##0}", d.Name, d.Net, d.Vat, d.ServiceTax); } else { printText += string.Format("\n\r{0,-32} {1,9:#,##0}", d.Name, d.Net); } } printText += DrawEqual; printText += CutPrinter; return PrintRaw(printText, "Sale Detail " + CurrentUser.user.Name); } public static Boolean PrintSale(List det, DateTime startDate, DateTime endDate) { var printText = FormatText(CurrentUser.user.Name, 42, false, Align.Centre); printText += DrawLine; printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy HH:mm:ss}", DateTime.Now), 42, false, Align.Centre); printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy} to {1:dd-MMM-yyyy}", startDate, endDate), 42, false, Align.Centre); printText += DrawLine; foreach (var d in det) { printText += string.Format("\n\r{0,-23} {1,9:#,##0.00} {2,8:#,##0}", d.Product, d.Quantity, d.Amount); } printText += DrawEqual; printText += CutPrinter; return PrintRaw(printText, "Sale Detail " + CurrentUser.user.Name); } private static string CashLine(IDictionary amount, int key) { if (amount.ContainsKey(key)) return string.Format("\n\r{0,5:#,##0} x {1,10:#,##0} = {2,10:#,##0}", key, amount[key], key * amount[key]); else return string.Empty; } #endregion public static Boolean PrintAdvance(string advance, string narration) { try { var billText = "\n\r" + FormatText("Bengali Sweet House", 42, false, Align.Centre); billText += "\n\r" + FormatText("+ V2C Multi Cuisine", 42, false, Align.Centre); if (!IsTwenty) billText += "\n\r" + FormatText("SCO 1, Pocket 1, Manimajra, Chandigarh", 42, false, Align.Centre); else billText += "\n\r" + FormatText("SCO 399, Sector 20, Panchkula", 42, false, Align.Centre); billText += "\n\r" + FormatText("A Unit of AA Herbal Biotech Pvt. Ltd.", 42, false, Align.Centre); billText += DrawLine; billText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy HH:mm:ss}", DateTime.Now), 42, false, Align.Centre); billText += "\n\r" + FormatText("Received Rs. " + advance + " as advance.", 42, false, Align.Centre); billText += DrawLine; if (narration != "") billText += "\n\r" + FormatText(narration, 42, false, Align.Centre); billText += DrawLine; billText += "\n\r" + FormatText("Thanking you " + CurrentUser.user.Name, 42, false, Align.Left); billText += "\n\r"; billText += "\n\r"; billText += "\n\r"; billText += "\n\r"; billText += "\n\r"; billText += "Signed" + "\n\r"; billText += "\n\r"; billText += CutPrinter; billText += "\n\r" + billText; return PrintRaw(billText, "Closing/Opening for " + CurrentUser.user.Name); } catch (Exception ex) { throw ex; } } public static Boolean PrintClosing(CheckoutBI details) { var billText = FormatText(string.Format("{0} Checkout By {1}", details.Cashier, details.Manager), 42, false, Align.Centre); billText += string.Format("{0:dd-MMM-yy} To {1:dd-MMM-yy} @ {2:dd-MMM-yyyy HH:mm}", details.StartDate, details.FinishDate, DateTime.Now); billText += DrawLine; billText += string.Format("\n\rOpening : {0,26:#,##0.00}", details.Opening); billText += string.Format("\n\rReceipts : {0,26:#,##0.00}", details.Receipts); billText += string.Format("\n\rAdvance Rcv. : {0,26:#,##0.00}", details.AdvanceReceipts); billText += string.Format("\n\rCC Receipts : {0,26:#,##0.00}", details.CreditCardReceipts); billText += string.Format("\n\rCredit Sale : {0,26:#,##0.00}", details.CreditReceipts); billText += string.Format("\n\rStaff Sale : {0,26:#,##0.00}", details.StaffReceipts); billText += string.Format("\n\rNo Charge : {0,26:#,##0.00}", details.NcBills); billText += string.Format("\n\rAdvance Adj. : {0,26:#,##0.00}", details.AdvanceAdjusted); billText += string.Format("\n\rPayments : {0,26:#,##0.00}", details.CashPayments); billText += string.Format("\n\rAddl. Voids : {0,26:#,##0.00}", details.AdditionalVoids); billText += string.Format("\n\rVoids in Sys. : {0,26:#,##0.00}", details.VoidsInSystem); billText += string.Format("\n\rPending Bills : {0,26:#,##0.00}", details.PendingBills); billText += string.Format("\n\rNet Sales : {0,26:#,##0.00}", details.NetSales); billText += string.Format("\n\rClosing Bal. : {0,26:#,##0.00}", details.ClosingBalance); billText += string.Format("\n\rCash Dep. : {0,26:#,##0.00}", details.CashDeposited); billText += DrawLine; billText += string.Format("\n\rOld Pending : {0,26:#,##0.00}", details.OldPending); billText += string.Format("\n\rOld Receipts : {0,26:#,##0.00}", details.OldReceipts); billText += string.Format("\n\rOld Voided : {0,26:#,##0.00}", details.OldVoided); billText += DrawEqual; billText += "\n\r" + FormatText(details.Status, 42, false, Align.Centre); billText += DrawEqual; billText += string.Format("\n\rActive Cashiers : {0}", details.Cashiers); if (details.PendingString.Length > 0) billText += details.PendingString; if (details.CCString.Length > 0) billText += details.CCString; if (details.CreditString.Length > 0) billText += details.CreditString; if (details.StaffString.Length > 0) billText += details.StaffString; if (details.VoidsString.Length > 0) billText += details.VoidsString; if (details.NcString.Length > 0) billText += details.NcString; if (details.PaymentString.Length > 0) billText += details.PaymentString; if (details.DiscountString.Length > 0) billText += details.DiscountString; billText += CutPrinter; return PrintRaw(billText, "Closing"); } public static string FormatPrintNum(string InputString) { if (InputString == "") return "0.00"; //return FormatText(FormatNumber(InputString.Trim, 2, TriState.UseDefault, TriState.True, TriState.True), 24, False, Align.Right); return FormatText(InputString, 24, false, Align.Right); } public static void PrintBill(Guid voucherID) { var voucher = new VoucherBI().GetVoucher(voucherID); PrintRaw(DesignBill(voucher), "Bill"); } public static void PrintWaiterKot(string p, Guid voucherID, List list) { var voucher = new VoucherBI().GetVoucher(voucherID); if (list.Count(i => (i.Quantity - i.Printed) > 0) > 0) { PrintRaw(DesignWaiterKot(voucher, list), "KOT"); PrintRaw(DesignPickupKot(voucher, list), "KOT"); } } public static void PrintCustomerKot(string p, Guid voucherID, List list) { var voucher = new VoucherBI().GetVoucher(voucherID); if (list.Count(i => (i.Quantity - i.Printed) > 0) > 0) { PrintRaw(DesignCustomerKot(voucher, list), "KOT"); } } } }