Files
Print_server/Inspectron.Epson/PrintServer/Printers/Utils/InvoiceReceipt/ReceiptConverter.cs
2026-01-26 14:29:37 +01:00

89 lines
3.3 KiB
C#

using Inspectron.Epson.PrintServer.Printers.Utils.FinalRecipe;
namespace Inspectron.Epson.PrintServer.Printers.Utils.InvoiceReceipt;
public class ReceiptConverter
{
private readonly int _lineWidth;
private readonly int _bigFontLineWidth;
public ReceiptConverter(int lineWidth = 42, int bigFontLineWidth = 21)
{
_lineWidth = lineWidth;
_bigFontLineWidth = bigFontLineWidth;
}
public List<PrintCommand> ConvertToPrintCommands(InvoiceReceipt invoiceReceipt)
{
var commands = new List<PrintCommand>();
// Header - Company info
commands.Add(new PrintCommand(Center(invoiceReceipt.CompanyName, false)));
commands.Add(new PrintCommand(Center(invoiceReceipt.Address1, false)));
commands.Add(new PrintCommand(Center(invoiceReceipt.Address2, false)));
commands.Add(new PrintCommand(Center(invoiceReceipt.Phone, false)));
commands.Add(new PrintCommand(""));
commands.Add(new PrintCommand(""));
// InvoiceReceipt info
string receiptLine = "";
if (invoiceReceipt.ReceiptNumber != null)
receiptLine += $"Rechnung Nr. {invoiceReceipt.ReceiptNumber}".PadRight(_lineWidth / 2);
else
receiptLine += $"".PadRight(_lineWidth / 2);
receiptLine += $"{invoiceReceipt.DateTime:HH:mm dd.MM.yyyy}".PadLeft(_lineWidth / 2);
commands.Add(new PrintCommand(receiptLine,isBold:true));
commands.Add(new PrintCommand($"Guests: {invoiceReceipt.Guests}"));
commands.Add(new PrintCommand(""));
// Items
foreach (var item in invoiceReceipt.Items)
{
string quantityDesc = $"{item.Quantity}x {item.Description}";
string prices = $"{item.UnitPrice:F2}"+$"{item.TotalPrice:F2}".PadLeft(7)+$" {item.TaxCategory}";
// Calculate spacing to align prices to the right
int spacesNeeded = _lineWidth - quantityDesc.Length - prices.Length;
if (spacesNeeded < 1) spacesNeeded = 1;
string itemLine = quantityDesc + new string(' ', spacesNeeded) + prices;
commands.Add(new PrintCommand(itemLine));
// SubItems
if (item.SubItems != null)
{
foreach (var subItem in item.SubItems)
{
commands.Add(new PrintCommand($" - {subItem}"));
}
}
}
commands.Add(new PrintCommand(""));
commands.Add(new PrintCommand("---------".PadLeft(_lineWidth)));
commands.Add(new PrintCommand(""));
// Total
string totalLine = $"Summe: {invoiceReceipt.Total:F2} {invoiceReceipt.Currency}";
commands.Add(new PrintCommand(Center(totalLine, true), true, true));
commands.Add(new PrintCommand(Center($"Tisch: {invoiceReceipt.TableNumber}",false), false));
commands.Add(new PrintCommand(Center(invoiceReceipt.ThankYouMessage, false)));
return commands;
}
private string Center(string text, bool isBigFont)
{
int effectiveLineWidth = isBigFont ? _bigFontLineWidth : _lineWidth;
if (string.IsNullOrEmpty(text) || text.Length >= effectiveLineWidth)
return text;
int totalPadding = effectiveLineWidth - text.Length;
int leftPadding = totalPadding / 2;
return new string(' ', leftPadding) + text;
}
}