using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Tanshu.Accounts.Contracts; using Tanshu.Accounts.Entities; using Tanshu.Accounts.Repository; using System.Diagnostics; using Tanshu.Common.Helpers; namespace Tanshu.Accounts.Print { public static partial class Thermal { private const string DrawLine = "\n\r------------------------------------------"; private const string DrawEqual = "\n\r=========================================="; public static void PrintKot(Guid voucherID, Guid kotID) { # if (DEBUG) var stopwatch = new Stopwatch(); stopwatch.Start(); #endif Voucher voucher = VoucherBI.Get(voucherID); var dict = new Dictionary>(); var kot = voucher.Kots.SingleOrDefault(x => x.KotID == kotID); if (kot == null) return; foreach (var inventory in kot.Inventories) { var type = inventory.Product.ProductGroup.ProductGroupID; var printer = Cache.KotPrinter(type); if (!dict.ContainsKey(printer)) { dict.Add(printer, new List()); } dict[printer].Add(inventory); } #if (DEBUG) stopwatch.Stop(); Trace.TraceWarning("kot and printers built in {0} ms", stopwatch.ElapsedMilliseconds); stopwatch.Reset(); #endif foreach (var item in dict) { for (var i = 0; i < item.Key.Copies; i++) { #if (DEBUG) stopwatch.Start(); #endif PrintRaw(item.Key, DesignKot(voucher, kot, item.Value, i), "KOT"); #if (DEBUG) stopwatch.Stop(); Trace.TraceWarning("kot designed and printed in {0} ms", stopwatch.ElapsedMilliseconds); stopwatch.Reset(); #endif } } } private static string DesignKot(Voucher voucher, Kot kot, IEnumerable billItems, int copyNumber) { var billText = "\n\r" + "KOT / BOT".Center42(); billText += "\n\r" + string.Format("Copy No. {0}", copyNumber).Center42(); billText += DrawLine; billText += string.Format("\n\rKOT ID : K-{0,-5}/S-{1,-5} {2:dd-MMM-yyyy HH:mm}", voucher.KotID, kot.Code, kot.Date); billText += string.Format("\n\rTable No.: {0}", voucher.Table.Name); billText += DrawLine; billText += "\n\r Qty. x Name "; billText += DrawLine; foreach (var item in billItems) { billText += string.Format("\n\r{0,6:#,##0.00} x {1,-33}", item.Quantity, item.EffectiveName); foreach (var mod in item.InventoryModifier) billText += string.Format("\n\r --- {0,-32}", mod.Modifier.Name); } billText += DrawLine; if (voucher.Narration != "") { billText += "\n\r" + FormatText(voucher.Narration, 42, Align.Centre); billText += DrawLine; } return billText; } public static void PrintBill(Guid voucherID) { PrintRaw(Cache.BasePrinter, DesignBill(voucherID), "Bill"); } private static string DesignBill(Guid voucherID) { Voucher voucher = VoucherBI.Get(voucherID); var list = new Dictionary(); foreach (var item in voucher.Kots.SelectMany(kot => kot.Inventories)) { int hash = item.Product.ProductID.GetHashCode(); hash = hash ^ item.IsHappyHour.GetHashCode(); foreach (var mod in item.InventoryModifier.Where(x => x.Modifier.ShowInBill)) { hash = hash ^ mod.Modifier.ModifierID.GetHashCode(); } if (list.ContainsKey(hash)) list[hash].Quantity += item.Quantity; else list.Add(hash, item); } var billText = ""; billText += Header(voucher); billText += Products(voucher, list.Values.ToList()); decimal amount; amount = list.Values.Sum(item => item.Quantity * item.Price); if (amount != 0) billText += "\n\r" + FormatText("Total : ", 33, Align.Right) + FormatBillNum(amount, 9); amount = list.Values.Where(x => x.IsHappyHour == true).Sum(item => item.Quantity * item.Price); if (amount != 0) billText += "\n\r" + FormatText("Happy Hour Discount : ", 33, Align.Right) + FormatBillNum(amount, 9); amount = list.Values.Sum(item => item.Quantity * item.EffectivePrice * item.Discount); if (amount != 0) billText += "\n\r" + FormatText("Discount : ", 33, Align.Right) + FormatBillNum(amount, 9); amount = list.Values.Sum(item => item.Quantity * item.EffectivePrice * (1 - item.Discount)); if (amount != 0) { billText += "\n\r" + FormatText(" : ", 33, Align.Right) + "---------"; billText += "\n\r" + FormatText("Net : ", 33, Align.Right) + FormatBillNum(amount, 9); } amount = list.Values.Sum(item => item.Quantity * item.EffectivePrice * (1 - item.Discount) * item.ServiceCharge); if (amount != 0) billText += "\n\r" + FormatText("Service Charge : ", 33, Align.Right) + FormatBillNum(amount, 9); var vatList = from i in list.Values group i by i.Vat.Name into vat select new { Tax = vat.Key, Amount = vat.Sum(x => x.VatAmount) }; foreach (var item in vatList) if (item.Amount != 0) billText += "\n\r" + FormatText(item.Tax, 33, Align.Right) + FormatBillNum(item.Amount, 9); var stList = from i in list.Values group i by i.ServiceTax.Name into serviceTax select new { Tax = serviceTax.Key, Amount = serviceTax.Sum(x => x.ServiceTaxAmount) }; foreach (var item in stList) if (item.Amount != 0) billText += "\n\r" + FormatText(item.Tax, 33, Align.Right) + FormatBillNum(item.Amount, 9); amount = list.Values.Sum(item => item.Amount); if (amount != 0) billText += string.Format("\n\r{0,33}{1,9:#,##0.00;(#,##0.00);0}", "Final Amount : ", Math.Round(amount, 0)); billText += DrawLine; if (voucher.VoucherType == VoucherType.NoCharge || voucher.VoucherType == VoucherType.Staff) { billText += "\n\r" + "THIS IS NOT A BILL - DON'T PAY".Center42(); billText += DrawLine; } if (voucher.Narration != "") { billText += "\n\r" + FormatText(voucher.Narration, 42, Align.Centre); billText += DrawLine; } if (voucher.Customer.CustomerID != Constants.CASH_CUSTOMER) { 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; billText += "\n\r" + (string)SettingBI.Get("Footer").Details["Text"]; return billText; } private static string Products(Voucher voucher, IList list) { string billNo; switch (voucher.VoucherType) { case VoucherType.NoCharge: billNo = "NC-" + voucher.BillID.Value.ToString(); break; case VoucherType.Staff: billNo = "ST-" + voucher.BillID.Value.ToString(); break; case VoucherType.TakeAway: case VoucherType.Regular: default: billNo = (voucher.BillID.Value % 10000).ToString(); break; } var billText = ""; billText += "\n\r" + string.Format("Bill No: {0,-12} {1:dd-MMM-yyyy HH:mm:ss}", billNo, voucher.Date); billText += "\n\r" + "Table No.: " + voucher.Table.Name; billText += "\n\r" + "------------------------------------------"; billText += "\n\r" + "Qty. Particulars Price Amount"; billText += "\n\r" + "------------------------------------------"; foreach (var item in list.Where(item => item.Quantity != 0)) { billText += "\n\r" + FormatBillNum(item.Quantity, 5) + " "; billText += FormatText(item.EffectiveName, 22, Align.Left) + " "; billText += FormatBillNum(item.Price, 6) + " "; billText += FormatBillNum(item.Price * item.Quantity, 6); foreach (var mod in item.InventoryModifier.Where(x => x.Modifier.ShowInBill)) { billText += "\n\r -- " + FormatText(mod.Modifier.Name, 38, Align.Left); } } billText += "\n\r" + "------------------------------------------"; return billText; } private static string Header(Voucher voucher) { var billText = ""; billText += "\n\r" + (string)SettingBI.Get("Header").Details["Text"]; switch (voucher.VoucherType) { case VoucherType.Regular: billText += "\n\r" + "Retail Invoice".Center42(); billText += "\n\r"; break; case VoucherType.NoCharge: billText += "\n\r" + "NO CHARGE - THIS IS NOT A BILL - DON'T PAY".Center42(); billText += "\n\r"; break; case VoucherType.TakeAway: billText += "\n\r" + "Retail Invoice (Delivery)".Center42(); billText += "\n\r"; break; case VoucherType.Staff: billText += "\n\r" + "STAFF CONSUMPTION -- THIS IS NOT A BILL".Center42(); billText += "\n\r"; break; } return billText; } private static string FormatText(string inputString, int width, Align alignment) { if (inputString.Length > width) return inputString.Substring(0, width); if (alignment == Align.Left) return string.Format("{0,-" + width + "}", inputString); if (alignment == Align.Centre) { int left = Convert.ToInt32(Math.Ceiling(Convert.ToDouble((width - inputString.Length) / 2))) + inputString.Length; inputString = string.Format("{0,-" + left + "}", inputString); return string.Format("{0," + width + "}", inputString); } if (alignment == Align.Right) return string.Format("{0," + width + "}", 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 + "}", temp); } private static PlatformPrinter GetPrinter(string location) { if (location.StartsWith("smb:")) return new PrinterWindows(location); else if (location.StartsWith("cups:")) return new PrinterLinux(location); else if (location.StartsWith("pdl:")) return new PrinterSocket(location); throw new NotImplementedException(); } private static bool PrintRaw(PrintLocation location, string text, string documentName) { #if (DEBUG) MessageBox.Show(text); return true; #else text += location.CutCode; var printer = GetPrinter(location.Printer); if (!printer.Print(documentName, text)) MessageBox.Show("Error in PrintRAW Function. Please Report immediately"); return true; #endif } public static Boolean PrintClosing(CheckoutBI details) { var billText = FormatText(string.Format("{0} Checkout By {1}", details.Cashier.Name, details.Manager), 42, Align.Centre); billText += string.Format("\n\r{0:dd-MMM-yy} To {1:dd-MMM-yy} @ {2:dd-MMM-yyyy HH:mm}", details.StartDate, details.FinishDate, DateTime.Now); billText += DrawLine; foreach (var item in details.amounts) { billText += string.Format("\n\r{0} : {1,26:#,##0.00}", item.Key.Display(), item.Value); } billText += DrawEqual; billText += string.Format("\n\rActive Cashiers : {0}", details.ActiveCashiers); foreach (var item in details.info.Where(x => x.Key.Print())) { billText += item.Value; } return PrintRaw(Cache.BasePrinter, billText, "Closing"); } public static string FormatPrintNum(string inputString) { return inputString == "" ? "0.00" : FormatText(inputString, 24, Align.Right); //return FormatText(FormatNumber(InputString.Trim, 2, TriState.UseDefault, TriState.True, TriState.True), 24, False, Align.Right); } #region Print Cash Total public static Boolean PrintCash(Dictionary amount, string user) { string printText; int total = 0; printText = FormatText(user, 42, Align.Centre); printText += DrawLine; printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy HH:mm:ss}", DateTime.Now), 42, 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; return PrintRaw(Cache.BasePrinter, printText, "Closing/Opening for " + user); } public static Boolean PrintSale(string user, IList det, DateTime startDate, DateTime endDate) { var printText = FormatText(user, 42, Align.Centre); printText += DrawLine; printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy HH:mm:ss}", DateTime.Now), 42, Align.Centre); printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy} to {1:dd-MMM-yyyy}", startDate, endDate), 42, Align.Centre); printText += DrawLine; foreach (SalesAnalysis d in det) { printText += string.Format("\n\r {0,-22} {1,9:#,##0}", d.GroupType, d.Amount); } printText += DrawEqual; return PrintRaw(Cache.BasePrinter, printText, "Sale Detail " + user); } public static Boolean PrintSale(string user, IList list, DateTime startDate, DateTime endDate) { var printText = FormatText(user, 42, Align.Centre); printText += DrawLine; printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy HH:mm:ss}", DateTime.Now), 42, Align.Centre); printText += "\n\r" + FormatText(string.Format("{0:dd-MMM-yyyy} to {1:dd-MMM-yyyy}", startDate, endDate), 42, Align.Centre); printText += DrawLine; foreach (var item in list) { printText += string.Format("\n\r{0,-22} {1,9:#,##0.00} {2,9:#,##0.00}", item.Name, item.Sale, item.NC); } printText += DrawEqual; return PrintRaw(Cache.BasePrinter, printText, "Sale Detail " + user); } private static string CashLine(IDictionary amount, int key) { return amount.ContainsKey(key) ? string.Format("\n\r{0,5:#,##0} x {1,10:#,##0} = {2,10:#,##0}", key, amount[key], key * amount[key]) : string.Empty; } private static bool RunningOnLinux() { var platform = (int)Environment.OSVersion.Platform; return (platform == 4) || (platform == 6) || (platform == 128); } #endregion } }