narsil/Tanshu.Accounts.Print/Thermal.cs

493 lines
22 KiB
C#
Raw Normal View History

2010-03-02 17:56:21 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Repository;
2010-03-02 17:56:21 +00:00
using Tanshu.Accounts.Helpers;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
2010-03-02 17:56:21 +00:00
namespace Tanshu.Accounts.Print
{
enum Align
{
None = 0,
Left = 1,
Centre = 2,
Right = 4
}
public static class Thermal
{
private const string DrawLine = "\n\r------------------------------------------";
private const string DrawEqual = "\n\r==========================================";
private static string DesignKot(Voucher trans, string kotCode, List<Inventory> billItems, int copyNumber)
2010-03-02 17:56:21 +00:00
{
Waiter waiter = trans.Waiter;
2010-03-02 17:56:21 +00:00
string BillText;
try
{
BillText = "\n\r" + FormatText("KOT / BOT", 42, false, Align.Centre);
BillText += "\n\r" + FormatText(string.Format("Copy No. {0}", copyNumber), 42, false, Align.Centre);
2010-03-02 17:56:21 +00:00
BillText += DrawLine;
BillText += string.Format("\n\rKOT ID : {0,-7}/{1,-7} {2:dd-MMM-yyyy HH:mm}", trans.KotID, kotCode, trans.Date);
2010-03-02 17:56:21 +00:00
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)
2010-03-02 17:56:21 +00:00
{
BillText += string.Format("\n\r{0,6:#,##0.00} x {1,-33}", item.Quantity, Name(item.Product));
foreach (var mod in item.InventoryModifier)
BillText += string.Format("\n\r --- {0,-32}", mod.Modifier.Name);
2010-03-02 17:56:21 +00:00
}
BillText += DrawLine;
if (trans.Narration != "")
{
BillText += "\n\r" + FormatText(trans.Narration, 42, false, Align.Centre);
BillText += DrawLine;
}
return BillText;
}
catch (Exception ex)
{
throw ex;
}
}
private static string DesignBill(Voucher trans)
2010-03-02 17:56:21 +00:00
{
Dictionary<int, Inventory> list = new Dictionary<int, Inventory>();
foreach (var kot in trans.Kots)
{
foreach (var item in kot.Inventories)
{
if (list.ContainsKey(item.Product.ProductID))
list[item.Product.ProductID].Quantity += item.Quantity;
else
list.Add(item.Product.ProductID, item);
}
}
User cashier = trans.User;
Waiter waiter = trans.Waiter;
Customer customer = trans.Customer;
2010-03-02 17:56:21 +00:00
String billNo = trans.BillID.Substring(trans.BillID.IndexOf("-") + 1);
String BillText = "";
BillText = "\n\r" + FormatText("Hops n Grains", 42, false, Align.Centre);
BillText += "\n\r" + FormatText("The Microbrewery", 42, false, Align.Centre);
BillText += "\n\r" + FormatText("SCO 358, Sector 9, Panchkula", 42, false, Align.Centre);
BillText += "\n\r" + FormatText("A Unit of Peitho Foods Pvt. Ltd.", 42, false, Align.Centre);
BillText += "\n\r" + FormatText("TIN: 06592507323", 42, false, Align.Centre);
2010-03-02 17:56:21 +00:00
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 list.Values)
2010-03-02 17:56:21 +00:00
{
if (item.Quantity == 0)
continue;
if (Math.Round((item.Rate * (1 + item.Tax)), 0) == 1)
2010-03-02 17:56:21 +00:00
{
BillText += "\n\r" + " ";
BillText += FormatText(Name(item.Product), 22, false, Align.Left) + " ";
2010-03-02 17:56:21 +00:00
BillText += " ";
BillText += FormatBillNum(item.Rate * item.Quantity, 6);
2010-03-02 17:56:21 +00:00
}
else
{
BillText += "\n\r" + FormatBillNum(item.Quantity, 5) + " ";
BillText += FormatText(Name(item.Product), 22, false, Align.Left) + " ";
BillText += FormatBillNum(item.Rate, 6) + " ";
BillText += FormatBillNum(item.Rate * item.Quantity, 6);
2010-03-02 17:56:21 +00:00
}
}
BillText += "\n\r" + "------------------------------------------";
decimal amount;
amount = Net(trans);
if (amount != 0)
BillText += "\n\r" + FormatText("Net : ", 33, false, Align.Right) + FormatBillNum(amount, 9);
amount = Discount(trans);
if (amount != 0)
BillText += "\n\r" + FormatText("Discount : ", 33, false, Align.Right) + FormatBillNum(amount, 9);
amount = ServiceCharge(trans);
if (amount != 0)
BillText += "\n\r" + FormatText("Service Charge : ", 33, false, Align.Right) + FormatBillNum(amount, 9);
amount = Tax(trans);
if (amount != 0)
BillText += "\n\r" + FormatText("VAT (incl. surcharge) : ", 33, false, Align.Right) + FormatBillNum(amount, 9);
amount = Amount(trans);
if (amount != 0)
BillText += string.Format("\n\r Final Amount : {0,9:#,##0.00;(#,##0.00);0}", Math.Round(amount, 0));
2010-03-02 17:56:21 +00:00
BillText += DrawLine;
if (trans.Narration != "")
{
BillText += "\n\r" + FormatText(trans.Narration, 42, false, Align.Centre);
BillText += DrawLine;
}
if (customer.CustomerID != 1)
2010-03-02 17:56:21 +00:00
{
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;
BillText += "\n\r" + "Call: 0172-4026666, 8054923853, 8054923856";
2010-03-02 17:56:21 +00:00
return BillText;
}
private static decimal Amount(Voucher voucher)
{
decimal amount = 0;
foreach (var kot in voucher.Kots)
{
foreach (var inv in kot.Inventories)
{
amount += inv.Quantity * inv.Rate * (1 - inv.Discount) * (1 + inv.ServiceCharge) * (1 + inv.Tax);
}
}
return amount;
}
private static decimal Tax(Voucher voucher)
{
decimal amount = 0;
foreach (var kot in voucher.Kots)
{
foreach (var inv in kot.Inventories)
{
amount += inv.Quantity * inv.Rate * (1 - inv.Discount) * (1 + inv.ServiceCharge) * inv.Tax;
}
}
return amount;
}
private static decimal ServiceCharge(Voucher voucher)
{
decimal amount = 0;
foreach (var kot in voucher.Kots)
{
foreach (var inv in kot.Inventories)
{
amount += inv.Quantity * inv.Rate * (1 - inv.Discount) * inv.ServiceCharge;
}
}
return amount;
}
private static decimal Discount(Voucher voucher)
{
decimal amount = 0;
foreach (var kot in voucher.Kots)
{
foreach (var inv in kot.Inventories)
{
amount += inv.Quantity * inv.Rate * inv.Discount;
}
}
return amount;
}
private static decimal Net(Voucher voucher)
{
decimal amount = 0;
foreach (var kot in voucher.Kots)
{
foreach (var inv in kot.Inventories)
{
amount += inv.Quantity * inv.Rate;
}
}
return amount;
}
private static string Name(Product product)
{
if (product.Units == null || product.Units == string.Empty)
return product.Name;
else
return string.Format("{0} ({1})", product.Name, product.Units);
}
2010-03-02 17:56:21 +00:00
private static string FormatText(string inputString, int width, Boolean multiline, Align alignment)
{
if ((multiline) && (inputString.Length > width))
{
string newString = "";
int lines = Convert.ToInt32(Math.Floor(Convert.ToDecimal(inputString.Length / width)));
for (int i = 0; i < lines; i++)
newString += inputString.Substring(i * width, width) + "\n\r";
newString += FormatText(inputString.Substring(lines * width), width, multiline, alignment);
return newString;
}
else if (inputString.Length > width)
return inputString.Substring(0, width);
else if (alignment == Align.None)
return inputString;
else if (alignment == Align.Left)
return string.Format("{0,-" + width.ToString() + "}", inputString);
else if (alignment == Align.Centre)
{
int left;
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);
}
else if (alignment == Align.Right)
return string.Format("{0," + width.ToString() + "}", inputString);
else
return inputString;
}
private static string FormatBillNum(decimal Amount, int Length)
{
string Temp = String.Format("{0:#,##0.00;(#,##0.00);0}", Amount);
return string.Format("{0," + Length.ToString() + "}", Temp);
}
private static bool PrintRAW(PrintLocation printer, string Text, string DocumentName)
2010-03-02 17:56:21 +00:00
{
#if (DEBUG)
System.Windows.Forms.MessageBox.Show(Text);
return true;
#else
2010-03-02 17:56:21 +00:00
try
{
Text += printer.CutCode;
if (!RawPrinterHelper.PrintString(printer.Printer, DocumentName, Text, new char[] { ';' }))
2010-03-02 17:56:21 +00:00
{
GC.Collect();
System.Windows.Forms.MessageBox.Show("Error in PrintRAW Function. Please Report immediately");
}
}
catch (Exception ex)
{
throw ex;
}
return true;
#endif
2010-03-02 17:56:21 +00:00
}
#region Print Cash Total
public static Boolean PrintCash(Dictionary<int, int> amount, string user)
2010-03-02 17:56:21 +00:00
{
string printText;
int total = 0;
try
{
2011-01-06 07:17:00 +00:00
printText = FormatText(user, 42, false, Align.Centre);
2010-03-02 17:56:21 +00:00
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;
return PrintRAW(PrintLocationBI.BasePrinter, printText, "Closing/Opening for " + user);
2010-03-02 17:56:21 +00:00
}
catch (Exception ex)
{
throw ex;
}
}
public static Boolean PrintSale(string user, IList<SalesAnalysis> det, DateTime startDate, DateTime endDate)
2010-03-02 17:56:21 +00:00
{
string printText;
try
{
2011-01-06 07:17:00 +00:00
printText = FormatText(user, 42, false, Align.Centre);
2010-03-02 17:56:21 +00:00
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 (SalesAnalysis d in det)
2010-03-02 17:56:21 +00:00
{
printText += string.Format("\n\r {0,-22} {1,9:#,##0}", d.GroupType, d.Amount);
}
printText += DrawEqual;
return PrintRAW(PrintLocationBI.BasePrinter, printText, "Sale Detail " + user);
}
catch (Exception ex)
{
throw ex;
}
}
public static Boolean PrintSale(string user, IList<SalesAnalysisDetail> list, DateTime startDate, DateTime endDate)
{
string printText;
try
{
printText = FormatText(user, 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 item in list)
{
printText += string.Format("\n\r{0,-22} {1,9:#,##0.00} {2,9:#,##0.00}", item.Product, item.Sale, item.NC);
2010-03-02 17:56:21 +00:00
}
printText += DrawEqual;
return PrintRAW(PrintLocationBI.BasePrinter, printText, "Sale Detail " + user);
2010-03-02 17:56:21 +00:00
}
catch (Exception ex)
{
throw ex;
}
}
private static string CashLine(Dictionary<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 user, string Advance, string Narration)
2010-03-02 17:56:21 +00:00
{
String BillText;
try
{
BillText = "\n\r" + FormatText("Hops n Grains", 42, false, Align.Centre);
BillText += "\n\r" + FormatText("The Microbrewery", 42, false, Align.Centre);
BillText += "\n\r" + FormatText("SCO 358, Sector 9, Panchkula", 42, false, Align.Centre);
BillText += "\n\r" + FormatText("A Unit of Peitho Foods Pvt. Ltd.", 42, false, Align.Centre);
2010-03-02 17:56:21 +00:00
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;
2011-01-06 07:17:00 +00:00
BillText += "\n\r" + FormatText("Thanking you " + user, 42, false, Align.Left);
2010-03-02 17:56:21 +00:00
BillText += "\n\r";
BillText += "\n\r";
BillText += "\n\r";
BillText += "\n\r";
BillText += "\n\r";
BillText += "Signed" + "\n\r";
BillText += "\n\r";
PrintRAW(PrintLocationBI.BasePrinter, BillText, "Advance for " + user);
return PrintRAW(PrintLocationBI.BasePrinter, BillText, "Advance for " + user);
2010-03-02 17:56:21 +00:00
}
catch (Exception ex)
{
throw ex;
}
}
public static Boolean PrintClosing(CheckoutBI details)
2010-03-02 17:56:21 +00:00
{
string BillText;
try
{
BillText = FormatText(string.Format("{0} Checkout By {1}", details.Cashier.Name, details.Manager), 42, false, 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);
2010-03-02 17:56:21 +00:00
BillText += DrawLine;
//BillText += string.Format("\n\rOpening : {0,26:#,##0.00}", details.Opening);
//BillText += string.Format("\n\rReceipts : {0,26:#,##0.00}", details.Receipts);
2010-03-02 17:56:21 +00:00
BillText += string.Format("\n\rAdvance Rcv. : {0,26:#,##0.00}", details.AdvanceReceipts);
BillText += string.Format("\n\rCC Receipts : {0,26:#,##0.00}", details.CCReceipts);
BillText += string.Format("\n\rNC Amount : {0,26:#,##0.00}", details.NCReceipts);
BillText += string.Format("\n\rBTC Amount : {0,26:#,##0.00}", details.BTCReceipts);
2010-03-02 17:56:21 +00:00
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);
2010-03-02 17:56:21 +00:00
BillText += string.Format("\n\rVoids in Sys. : {0,26:#,##0.00}", details.VoidsInSystem);
BillText += string.Format("\n\rDiscounts : {0,26:#,##0.00}", details.Discount);
2010-03-02 17:56:21 +00:00
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.NCString.Length > 0)
BillText += details.NCString;
if (details.BTCString.Length > 0)
BillText += details.BTCString;
if (details.StaffString.Length > 0)
BillText += details.StaffString;
2010-03-02 17:56:21 +00:00
if (details.VoidsString.Length > 0)
BillText += details.VoidsString;
if (details.PaymentString.Length > 0)
BillText += details.PaymentString;
if (details.DiscountString.Length > 0)
BillText += details.DiscountString;
return PrintRAW(PrintLocationBI.BasePrinter, BillText, "Closing");
2010-03-02 17:56:21 +00:00
}
catch (Exception ex)
{
throw ex;
}
}
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(int voucherID)
2010-03-02 17:56:21 +00:00
{
var voucher = VoucherBI.GetSaleVoucher(voucherID);
PrintRAW(PrintLocationBI.BasePrinter, DesignBill(voucher), "Bill");
2010-03-02 17:56:21 +00:00
}
public static void PrintKot(int voucherID, int kotID)
2010-03-02 17:56:21 +00:00
{
var voucher = VoucherBI.GetSaleVoucher(voucherID);
Dictionary<PrintLocation, List<Inventory>> dict = new Dictionary<PrintLocation, List<Inventory>>();
//Dictionary<string, int> dictCount = new Dictionary<string, int>();
foreach (var item in voucher.Kots.Where(x => x.KotID == kotID))
2010-03-02 17:56:21 +00:00
{
foreach (var inventory in item.Inventories)
{
int type = ProductBI.GetProduct(inventory.Product.ProductID).ProductGroup.ProductGroupID;
var printer = PrintLocationBI.KotPrinter(type);
if (!dict.ContainsKey(printer))
{
dict.Add(printer, new List<Inventory>());
//dictCount.Add(printer.Printer, 0);
}
dict[printer].Add(inventory);
//dictCount[printer.Printer] = printer.Copies;
}
2010-03-02 17:56:21 +00:00
}
foreach (var item in dict)
2010-03-02 17:56:21 +00:00
{
for (int i = 0; i < item.Key.Copies; i++)
{
PrintRAW(item.Key, DesignKot(voucher, voucher.Kots.Where(x => x.KotID == kotID).Single().Code, item.Value, i), "KOT");
}
2010-03-02 17:56:21 +00:00
}
}
}
}