narsil/Tanshu.Accounts.Print/Thermal.cs

414 lines
20 KiB
C#

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(SaleVoucherBO trans, IEnumerable<SalesBillItemBO> billItems, string title)
{
var waiter = new WaiterBI().GetWaiter(trans.WaiterID);
try
{
var billText = title;
billText += title;
billText += DrawLine;
billText += string.Format("\n\rKOT ID : {0,-13} {1:dd-MMM-yyyy HH:mm}", trans.KotID, trans.Date);
billText += string.Format("\n\rTable No.: {0} / {1}", trans.TableID, 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 (trans.Narration != "")
{
billText += "\n\r" + FormatText(trans.Narration, 42, false, Align.Centre);
billText += DrawLine;
}
billText += CutPrinter;
return billText;
}
catch (Exception ex)
{
throw ex;
}
}
private static string DesignWaiterKot(SaleVoucherBO trans, IEnumerable<SalesBillItemBO> billItems)
{
return DesignKot(trans, billItems, string.Format("\n\rWAITER WAITER WAITER WAITER WAITER\n\rWAITER WAITER WAITER WAITER WAITER"));
}
private static string DesignPickupKot(SaleVoucherBO trans, IEnumerable<SalesBillItemBO> billItems)
{
return DesignKot(trans, 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(SaleVoucherBO trans, IEnumerable<SalesBillItemBO> billItems)
{
return DesignKot(trans, billItems, string.Format("\n\r Counter / Order Slip "));
}
private static string DesignBill(SaleVoucherBO trans, IEnumerable<InventoryDisplayBO> billItems)
{
var cashier = new UserBI().GetUser(trans.UserID);
var waiter = new WaiterBI().GetWaiter(trans.WaiterID);
var customer = new CustomerBI().GetCustomer(trans.CustomerID);
var billNo = trans.BillID.Substring(trans.BillID.IndexOf("-") + 1);
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);
if (!IsTwenty)
billText += "\n\r" + FormatText("TIN: 04400023630", 42, false, Align.Centre);
else
billText += "\n\r" + FormatText("TIN: 06892504733", 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}", trans.Date);
billText += "\n\r" + "Table No.: " + trans.TableID;
billText += "\n\r" + "------------------------------------------";
billText += "\n\r" + "Qty. Particulars Price Amount";
billText += "\n\r" + "------------------------------------------";
foreach (var item in billItems)
{
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 = billItems.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 = billItems.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 = billItems.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 (trans.AdvanceID.HasValue)
{
advance = new AdvanceBI().Get(trans.AdvanceID.Value).Amount;
billText += "\n\r" + FormatText("Less Advance : ", 33, false, Align.Right) + FormatBillNum(advance, 9);
}
amount = billItems.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 (trans.Narration != "")
{
billText += "\n\r" + FormatText(trans.Narration, 42, false, Align.Centre);
billText += DrawLine;
}
if (customer.Name != "Cash")
{
billText += "\n\r" + customer.Name;
billText += string.Format("\n\r{0}\n\r{1}", customer.Phone, customer.Address);
billText += DrawLine;
}
billText += "\n\r" + "Cashier : " + cashier.Name + " / " + 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<int, int> amount)
{
string printText;
int total = 0;
try
{
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);
}
catch (Exception ex)
{
throw;
}
}
public static Boolean PrintSale(List<SalesAnalysisBO> 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<SalesAnalysisDetailBO> 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<int, int> 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\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.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)
{
SaleVoucherBO trans = new SaleVoucherBO();
List<InventoryDisplayBO> iList = new List<InventoryDisplayBO>();
new SaleVoucherBI().GetSaleVoucher(voucherID, ref trans, ref iList);
PrintRaw(DesignBill(trans, iList), "Bill");
}
public static void PrintWaiterKot(string p, Guid voucherID, List<SalesBillItemBO> list)
{
SaleVoucherBO trans = new SaleVoucherBO();
List<InventoryDisplayBO> iList = new List<InventoryDisplayBO>();
new SaleVoucherBI().GetSaleVoucher(voucherID, ref trans, ref iList);
if (list.Count(i => (i.Quantity - i.Printed) > 0) > 0)
{
PrintRaw(DesignWaiterKot(trans, list), "KOT");
PrintRaw(DesignPickupKot(trans, list), "KOT");
}
}
public static void PrintCustomerKot(string p, Guid voucherID, List<SalesBillItemBO> list)
{
SaleVoucherBO trans = new SaleVoucherBO();
List<InventoryDisplayBO> iList = new List<InventoryDisplayBO>();
new SaleVoucherBI().GetSaleVoucher(voucherID, ref trans, ref iList);
if (list.Count(i => (i.Quantity - i.Printed) > 0) > 0)
{
PrintRaw(DesignCustomerKot(trans, list), "KOT");
}
}
}
}