narsil/Tanshu.Accounts.Print/ThermalBill.cs

179 lines
8.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Tanshu.Accounts.Entities;
using Tanshu.Common.Helpers;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Repository;
namespace Tanshu.Accounts.Print
{
public static partial class Thermal
{
private static string DesignBill(Guid voucherID)
{
using (var bi = new VoucherBI())
{
Voucher voucher = bi.Get(x => x.VoucherID == voucherID);
var list = new Dictionary<int, Inventory>();
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 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.Name, 33, Align.Right) + FormatBillNum(item.Amount, 9);
var stList = from i in list.Values
group i by i.ServiceTax 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.Name, 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;
using (var bis = new SettingBI())
billText += "\n\r" + bis.Get(x => x.Name == "Footer").Details;
return billText;
}
}
private static string Products(Voucher voucher, IList<Inventory> 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 = "";
using (var bi = new SettingBI())
billText += "\n\r" + bi.Get(x => x.Name == "Header").Details;
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;
}
}
}