word wrap for final receipt
discount for final receipt
This commit is contained in:
@@ -10,19 +10,19 @@ using System.Text.Json;
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
// FINAL RECEIPT - HTML PRINTER
|
// FINAL RECEIPT - HTML PRINTER
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
var receipt = TestHelpers.BuildSampleFinalReceipt(true);
|
//var receipt = TestHelpers.BuildSampleFinalReceipt(true);
|
||||||
var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
|
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
|
||||||
|
|
||||||
var converter = new FinalReceiptConverter(lineWidth: 48, bigFontLineWidth: 24);
|
//var converter = new FinalReceiptConverter(lineWidth: 48, bigFontLineWidth: 24);
|
||||||
var printCommands = converter.Convert(serializedReceipt);
|
//var printCommands = converter.Convert(serializedReceipt);
|
||||||
|
|
||||||
TestHelpers.PrintCommandsToConsole(printCommands);
|
//TestHelpers.PrintCommandsToConsole(printCommands);
|
||||||
|
|
||||||
var printer = await TestHelpers.CreateHtmlPrinterAsync();
|
//var printer = await TestHelpers.CreateHtmlPrinterAsync();
|
||||||
await TestHelpers.PrintCommandsToHtmlPrinterAsync(
|
//await TestHelpers.PrintCommandsToHtmlPrinterAsync(
|
||||||
printer,
|
// printer,
|
||||||
printCommands,
|
// printCommands,
|
||||||
logoPath: "ristorante-klinglers.ch-logo_white_bg.png");
|
// logoPath: "ristorante-klinglers.ch-logo_white_bg.png");
|
||||||
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -77,19 +77,19 @@ await TestHelpers.PrintCommandsToHtmlPrinterAsync(
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
// KITCHEN RECEIPT - HTML PRINTER
|
// KITCHEN RECEIPT - HTML PRINTER
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
//var receipt = TestHelpers.BuildSampleKitchenReceipt();
|
var receipt = TestHelpers.BuildSampleKitchenReceipt();
|
||||||
//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
|
var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true });
|
||||||
//var deserializedReceipt = JsonSerializer.Deserialize<KitchenReceipt>(serializedReceipt);
|
var deserializedReceipt = JsonSerializer.Deserialize<KitchenReceipt>(serializedReceipt);
|
||||||
|
|
||||||
//var converter = new KitchenReceiptConverter(bigLineWidth: 20, lineWidth: 33);
|
var converter = new KitchenReceiptConverter(bigLineWidth: 20, lineWidth: 33);
|
||||||
//var printCommands = converter.ConvertToPrintCommands(deserializedReceipt);
|
var printCommands = converter.ConvertToPrintCommands(deserializedReceipt);
|
||||||
|
|
||||||
//TestHelpers.PrintCommandsToConsole(printCommands);
|
TestHelpers.PrintCommandsToConsole(printCommands);
|
||||||
|
|
||||||
//var printer = await TestHelpers.CreateHtmlPrinterAsync(
|
var printer = await TestHelpers.CreateHtmlPrinterAsync(
|
||||||
// path: @".\kitchen_test.html",
|
path: @".\kitchen_test.html",
|
||||||
// paperWidth: TestHelpers.KitchenPaperWidth);
|
paperWidth: TestHelpers.KitchenPaperWidth);
|
||||||
//await TestHelpers.PrintKitchenCommandsToHtmlPrinterAsync(printer, printCommands);
|
await TestHelpers.PrintKitchenCommandsToHtmlPrinterAsync(printer, printCommands);
|
||||||
|
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ public static class TestHelpers
|
|||||||
receipt.Items.Add(new FinalReceiptItem
|
receipt.Items.Add(new FinalReceiptItem
|
||||||
{
|
{
|
||||||
Quantity = 1,
|
Quantity = 1,
|
||||||
Description = "Granini Tomatensaft",
|
Description = "An item with really long description that should wrap",
|
||||||
UnitPrice = 5.50m,
|
UnitPrice = 5.50m,
|
||||||
TotalPrice = 5.50m,
|
TotalPrice = 5.50m,
|
||||||
TaxCategory = "A"
|
TaxCategory = "A"
|
||||||
@@ -323,29 +323,29 @@ public static class TestHelpers
|
|||||||
GuestId = 1
|
GuestId = 1
|
||||||
});
|
});
|
||||||
receipt.Gangs[0].Dishes.Add(new Dish(1, "Baby Spinach Salad with Truffl"){GuestId = 2});
|
receipt.Gangs[0].Dishes.Add(new Dish(1, "Baby Spinach Salad with Truffl"){GuestId = 2});
|
||||||
receipt.Gangs[0].Dishes.Add(new Dish(1, "Beef Tataki")
|
//receipt.Gangs[0].Dishes.Add(new Dish(1, "Beef Tataki")
|
||||||
{
|
//{
|
||||||
Modifications = new DishModifications
|
// Modifications = new DishModifications
|
||||||
{
|
// {
|
||||||
Removed = new List<string> { "Onion" },
|
// Removed = new List<string> { "Onion" },
|
||||||
Added = new List<string> { "Extra Sauce" }
|
// Added = new List<string> { "Extra Sauce" }
|
||||||
},
|
// },
|
||||||
Comment = "Medium rare",
|
// Comment = "Medium rare",
|
||||||
GuestId = 3
|
// GuestId = 3
|
||||||
});
|
//});
|
||||||
receipt.Gangs[0].Dishes.Add(new Dish(1, "Salmon Taco")
|
//receipt.Gangs[0].Dishes.Add(new Dish(1, "Salmon Taco")
|
||||||
{
|
//{
|
||||||
Modifications = new DishModifications
|
// Modifications = new DishModifications
|
||||||
{
|
// {
|
||||||
Added = new List<string> { "Extra Lime" }
|
// Added = new List<string> { "Extra Lime" }
|
||||||
},
|
// },
|
||||||
GuestId = 4
|
// GuestId = 4
|
||||||
});
|
//});
|
||||||
|
|
||||||
receipt.Gangs[1].Dishes.Add(new Dish(1, "Avocado Sashimi") { GuestId = 2 });
|
receipt.Gangs[1].Dishes.Add(new Dish(1, "Avocado Sashimi") { GuestId = 1 });
|
||||||
receipt.Gangs[1].Dishes.Add(new Dish(1, "Baby Spinach Salad with Truffl") { GuestId = 2 });
|
receipt.Gangs[1].Dishes.Add(new Dish(1, "Baby Spinach Salad with Truffl") { GuestId = 2 });
|
||||||
receipt.Gangs[1].Dishes.Add(new Dish(1, "Beef Tataki") { GuestId = 3 });
|
//receipt.Gangs[1].Dishes.Add(new Dish(1, "Beef Tataki") { GuestId = 3 });
|
||||||
receipt.Gangs[1].Dishes.Add(new Dish(1, "Salmon Taco") { GuestId = 4 });
|
//receipt.Gangs[1].Dishes.Add(new Dish(1, "Salmon Taco") { GuestId = 4 });
|
||||||
|
|
||||||
return receipt;
|
return receipt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,24 +40,7 @@ public class ReceiptConverter
|
|||||||
// Items
|
// Items
|
||||||
foreach (var item in finalReceipt.Items)
|
foreach (var item in finalReceipt.Items)
|
||||||
{
|
{
|
||||||
string quantityDesc = $"{item.Quantity}x {item.Description}";
|
PrintItem(item, commands);
|
||||||
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("")); // Reini
|
commands.Add(new PrintCommand("")); // Reini
|
||||||
@@ -185,6 +168,93 @@ public class ReceiptConverter
|
|||||||
return commands;
|
return commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void PrintItem(FinalReceiptItem item, List<PrintCommand> commands)
|
||||||
|
{
|
||||||
|
string quantityPrefix = $"{item.Quantity}x ";
|
||||||
|
string prices = $"{item.UnitPrice:F2}" + $"{item.TotalPrice:F2}".PadLeft(7) + $" {item.TaxCategory}";
|
||||||
|
string fullLine = quantityPrefix + item.Description;
|
||||||
|
|
||||||
|
int spacesNeeded = _lineWidth - fullLine.Length - prices.Length;
|
||||||
|
|
||||||
|
if (spacesNeeded >= 1 || string.IsNullOrEmpty(item.Description))
|
||||||
|
{
|
||||||
|
if (spacesNeeded < 1) spacesNeeded = 1;
|
||||||
|
commands.Add(new PrintCommand(fullLine + new string(' ', spacesNeeded) + prices));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int continuationAvailable = _lineWidth - quantityPrefix.Length;
|
||||||
|
int firstLineAvailable = continuationAvailable - prices.Length - 1;
|
||||||
|
|
||||||
|
if (continuationAvailable <= 0)
|
||||||
|
{
|
||||||
|
// Prefix alone exceeds line width, print without wrapping
|
||||||
|
commands.Add(new PrintCommand(fullLine));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var words = item.Description.Split(' ');
|
||||||
|
var lines = new List<string>();
|
||||||
|
string currentLine = "";
|
||||||
|
bool isFirstLine = true;
|
||||||
|
|
||||||
|
foreach (var word in words)
|
||||||
|
{
|
||||||
|
int available = (isFirstLine && firstLineAvailable > 0) ? firstLineAvailable : continuationAvailable;
|
||||||
|
|
||||||
|
if (currentLine.Length == 0)
|
||||||
|
{
|
||||||
|
currentLine = word;
|
||||||
|
}
|
||||||
|
else if (currentLine.Length + 1 + word.Length <= available)
|
||||||
|
{
|
||||||
|
currentLine += " " + word;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lines.Add(currentLine);
|
||||||
|
currentLine = word;
|
||||||
|
if (isFirstLine) isFirstLine = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentLine.Length > 0)
|
||||||
|
{
|
||||||
|
lines.Add(currentLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
// First line with prefix and right-aligned prices
|
||||||
|
string firstLineText = quantityPrefix + lines[0];
|
||||||
|
int firstSpaces = _lineWidth - firstLineText.Length - prices.Length;
|
||||||
|
if (firstSpaces >= 1)
|
||||||
|
{
|
||||||
|
commands.Add(new PrintCommand(firstLineText + new string(' ', firstSpaces) + prices));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
commands.Add(new PrintCommand(firstLineText));
|
||||||
|
commands.Add(new PrintCommand(prices.PadLeft(_lineWidth)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Continuation lines indented to align with description
|
||||||
|
string continuationIndent = new string(' ', quantityPrefix.Length);
|
||||||
|
for (int i = 1; i < lines.Count; i++)
|
||||||
|
{
|
||||||
|
commands.Add(new PrintCommand(continuationIndent + lines[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SubItems
|
||||||
|
if (item.SubItems != null)
|
||||||
|
{
|
||||||
|
foreach (var subItem in item.SubItems)
|
||||||
|
{
|
||||||
|
commands.Add(new PrintCommand($" - {subItem}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private string Center(string text, bool isBigFont)
|
private string Center(string text, bool isBigFont)
|
||||||
{
|
{
|
||||||
int effectiveLineWidth = isBigFont ? _bigFontLineWidth : _lineWidth;
|
int effectiveLineWidth = isBigFont ? _bigFontLineWidth : _lineWidth;
|
||||||
|
|||||||
Reference in New Issue
Block a user