Initial commit

Initial commit til Git.
V2 er deployed
This commit is contained in:
2026-06-13 17:31:50 +02:00
parent 9fcd2b145e
commit 41e23b6184
375 changed files with 15956 additions and 0 deletions

View File

@@ -0,0 +1,248 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using EpsonPrinter.Model;
using Microsoft.Extensions.Configuration;
namespace EpsonPrinter.Services
{
public class EpsonPrintService
{
private readonly IConfiguration _config;
private readonly string _devicePath;
public EpsonPrintService(IConfiguration config)
{
_config = config;
_devicePath = _config["PrintSettings:DevicePath"] ?? "/dev/usb/lp0";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
public void PrintReceipt(ReceiptModel model)
{
ICommandEmitter e = new EPSON();
InitAndWrite(e);
List<byte[][]> printBytes = new List<byte[][]>();
PrintHeader printHeader = new PrintHeader(e);
PrintBody printBody = new PrintBody(e, _config);
PrintFooter printFooter = new PrintFooter(e);
var header = (model.Header != null && model.Header.Any()) ? model.Header : BuildDefaultHeader();
var body = model.BodyModel ?? BuildDefaultBody();
if (string.IsNullOrWhiteSpace(body.Staff))
{
body.Staff = _config["PrintSettings:Defaults:Staff"] ?? "Ukendt";
}
if (string.IsNullOrWhiteSpace(body.ReceiptTime))
{
body.ReceiptTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
}
if (body.ReceiptNumber <= 0)
{
body.ReceiptNumber = Math.Abs(Environment.TickCount % 1000000);
}
body.Products ??= new List<BodyProductModel>();
if (!body.Products.Any())
{
body.Products.Add(new BodyProductModel
{
Product = "Standard vare",
NoOfProduct = 1,
Price = 99.00m,
TotalPrice = 99.00m
});
}
if (body.TotalPrice <= 0)
{
body.TotalPrice = body.Products.Sum(x => x.TotalPrice <= 0 ? x.Price * x.NoOfProduct : x.TotalPrice);
}
if (body.TotalVat <= 0)
{
body.TotalVat = Math.Round(body.TotalPrice * 0.25m, 2);
}
var footer = (model.Footer != null && model.Footer.Any()) ? model.Footer : BuildDefaultFooter();
printBytes.AddRange(printHeader.Print(header));
printBytes.AddRange(printBody.Print(body));
printBytes.AddRange(printFooter.Print(footer));
byte[][] array = printBytes.SelectMany(c => c).ToArray();
Write(array);
Write(e.FeedLines(5));
Write(e.FullCut());
}
public void PrintSaleOfDay(SaleOfDayModel model)
{
ICommandEmitter e = new EPSON();
InitAndWrite(e);
PrintSaleOfDay(model, e);
}
private void InitAndWrite(ICommandEmitter e)
{
Write(e.Initialize());
Write(e.Enable());
Write(e.CodePage(CodePage.ISO8859_15_LATIN9));
Write(e.EnableAutomaticStatusBack());
}
private void PrintSaleOfDay(SaleOfDayModel model, ICommandEmitter e)
{
List<byte[][]> data = new List<byte[][]>();
byte[][] dateBytes =
{
e.SetStyles(PrintStyle.DoubleWidth),
e.LeftAlign(),
e.PrintLine($"Dato: {model.Date:dd-MM-yyyy}")
};
data.Add(dateBytes);
byte[][] seperater =
{
e.SetStyles(PrintStyle.None),
e.LeftAlign(),
e.PrintLine("-----------------------------------")
};
data.Add(seperater);
foreach (SaleOfDayDetail saleOfDayDetail in model.SaleOfDayDetail)
{
string pS = $"{saleOfDayDetail.Category} : {saleOfDayDetail.TotalSale.ToString("#.#0", CultureInfo.InvariantCulture)}";
byte[][] totalPrice =
{
e.SetStyles(PrintStyle.DoubleWidth),
e.LeftAlign(),
e.PrintLine(pS)
};
data.Add(totalPrice);
}
data.Add(seperater);
string totalSale = $"Total salg: {model.TotalSale.ToString("#.#0", CultureInfo.InvariantCulture)}";
byte[][] tSBytes =
{
e.SetStyles(PrintStyle.DoubleWidth),
e.LeftAlign(),
e.PrintLine(totalSale)
};
data.Add(tSBytes);
string totalCustomer = $"Antal kunder: {model.TotalCustomers}";
byte[][] tCBytes =
{
e.SetStyles(PrintStyle.DoubleWidth),
e.LeftAlign(),
e.PrintLine(totalCustomer)
};
data.Add(tCBytes);
byte[][] array = data.SelectMany(c => c).ToArray();
Write(array);
Write(e.FeedLines(5));
Write(e.FullCut());
}
private List<HeaderModel> BuildDefaultHeader()
{
return new List<HeaderModel>
{
new HeaderModel
{
Value = _config["PrintSettings:Defaults:StoreName"] ?? "TableTop3D",
TextAlignment = Enums.AlignmentEnum.Center,
FeedingLines = 0,
PrintStyles = new PrintStyleModel { Bold = true, DoubleWidth = true, DoubleHeight = true }
},
new HeaderModel
{
Value = _config["PrintSettings:Defaults:AddressLine1"] ?? string.Empty,
TextAlignment = Enums.AlignmentEnum.Center,
FeedingLines = 0,
PrintStyles = new PrintStyleModel { Bold = true }
},
new HeaderModel
{
Value = _config["PrintSettings:Defaults:AddressLine2"] ?? string.Empty,
TextAlignment = Enums.AlignmentEnum.Center,
FeedingLines = 0,
PrintStyles = new PrintStyleModel()
},
new HeaderModel
{
Value = _config["PrintSettings:Defaults:Cvr"] ?? string.Empty,
TextAlignment = Enums.AlignmentEnum.Center,
FeedingLines = 1,
PrintStyles = new PrintStyleModel()
}
}.Where(x => !string.IsNullOrWhiteSpace(x.Value)).ToList();
}
private FooterModel CreateFooterLine(string value, bool bold = false)
{
return new FooterModel
{
Value = value,
TextAlignment = Enums.AlignmentEnum.Center,
FeedingLines = 0,
PrintStyles = new PrintStyleModel { Bold = bold }
};
}
private List<FooterModel> BuildDefaultFooter()
{
var values = new[]
{
_config["PrintSettings:Defaults:FooterLine1"] ?? "Tak for dit køb!",
_config["PrintSettings:Defaults:FooterLine2"] ?? string.Empty,
_config["PrintSettings:Defaults:Phone"] ?? string.Empty
}.Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
return values.Select((x, i) => CreateFooterLine(x, i == 0)).ToList();
}
private BodyModel BuildDefaultBody()
{
return new BodyModel
{
ReceiptNumber = Math.Abs(Environment.TickCount % 1000000),
ReceiptTime = DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"),
Staff = _config["PrintSettings:Defaults:Staff"] ?? "Ukendt",
Products = new List<BodyProductModel>
{
new BodyProductModel
{
Product = "Standard vare",
NoOfProduct = 1,
Price = 99.00m,
TotalPrice = 99.00m
}
},
TotalPrice = 99.00m,
TotalVat = 24.75m
};
}
private void Write(byte[] data)
{
using var stream = new FileStream(_devicePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
stream.Write(data, 0, data.Length);
stream.Flush();
}
private void Write(byte[][] data)
{
foreach (var segment in data)
{
Write(segment);
}
}
}
}

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using EpsonPrinter.Enums;
using ESCPOS_NET.Emitters;
namespace EpsonPrinter.Services
{
public class PrintAlignment
{
public byte[] GetTextAlignment(ICommandEmitter e, AlignmentEnum alignment)
{
if (alignment == AlignmentEnum.Left)
return e.LeftAlign();
if (alignment == AlignmentEnum.Right)
return e.RightAlign();
return e.CenterAlign();
}
}
}

View File

@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using EpsonPrinter.Model;
using ESCPOS_NET.Emitters;
using Microsoft.Extensions.Configuration;
namespace EpsonPrinter.Services
{
public class PrintBody
{
private ICommandEmitter _e;
private IConfiguration _config;
private int productMaxWidth;
public PrintBody(ICommandEmitter e, IConfiguration config)
{
_e = e;
_config = config;
}
public List<byte[][]> Print(BodyModel model)
{
List<byte[][]> data = new List<byte[][]>();
byte[][] headerModel =
{
_e.SetStyles(PrintStyle.None),
_e.LeftAlign(),
_e.PrintLine($"Dato.........: {model.ReceiptTime}"),
_e.PrintLine($"Ekspedient...: {model.Staff}"),
_e.PrintLine($"Kvittering...: {model.ReceiptNumber}"),
_e.FeedLines(1)
};
data.Add(headerModel);
int printWidth = Convert.ToInt32(_config["PrintSettings:PrintWidth"]);
foreach (BodyProductModel product in model.Products)
{
//Size of product is max half of recipt
string totalProductLine = MakeProductNameString(product.Product, printWidth);
totalProductLine += MakeProductNumber(product.NoOfProduct, product.Price);
totalProductLine = MakeTotalProductPrice(totalProductLine, product.TotalPrice,printWidth);
byte[][] p =
{
_e.PrintLine(totalProductLine)
};
data.Add(p);
}
PrintString printString = new PrintString();
string totalPricePrint = printString.MakePrintString(printWidth, $"Total: ", $"{model.TotalPrice:F} DKK");
byte[][] totalPrice =
{
_e.FeedLines(1),
_e.SetStyles(PrintStyle.Bold | PrintStyle.DoubleHeight),
_e.RightAlign(),
_e.PrintLine(totalPricePrint)
};
data.Add(totalPrice);
byte[][] vat =
{
_e.FeedLines(1),
_e.SetStyles(PrintStyle.None),
_e.LeftAlign(),
_e.PrintLine(MakeVat(printWidth,model.TotalVat))
};
data.Add(vat);
return data;
}
private string MakeProductNameString(string product, int totalWidth)
{
string productString = product;
productMaxWidth = (totalWidth / 2) - 5;
int productLength = product.Length;
if (productLength < productMaxWidth)
{
for (int i = productLength; i < productMaxWidth; i++)
{
productString += ".";
}
}
return productString;
}
private string MakeProductNumber(int noOfProduct, decimal price)
{
return $"{noOfProduct} á {price:F} DKK :";
}
private string MakeTotalProductPrice(string buffer, decimal totalPrice, int printWidth)
{
PrintString printString = new PrintString();
string p = $"{totalPrice:F} DKK";
string makePrintString = printString.MakePrintString(printWidth, buffer, p);
return makePrintString;
}
private string MakeVat(int printWidth, decimal vat)
{
PrintString printString = new PrintString();
string vatDesc = $"Moms udgør 25% :";
string v = $"{vat:F} DKK";
string vatString = printString.MakePrintString(printWidth, vatDesc, v);
return vatString;
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EpsonPrinter.Model;
using ESCPOS_NET;
using ESCPOS_NET.Emitters;
namespace EpsonPrinter.Services
{
public class PrintFooter
{
private ICommandEmitter _e;
public PrintFooter(ICommandEmitter e)
{
_e = e;
}
public List<byte[][]> Print(List<FooterModel> model)
{
List<byte[][]> data = new List<byte[][]>();
PrintStyleCombination printStyleCombination = new PrintStyleCombination();
PrintAlignment printAlignment = new PrintAlignment();
byte[][] feedLines =
{
_e.FeedLines(1)
};
data.Add(feedLines);
foreach (FooterModel footerModel in model)
{
PrintStyle printStyle = printStyleCombination.Combine(footerModel.PrintStyles);
byte[][] bytes = {
_e.SetStyles(PrintStyle.None),
printAlignment.GetTextAlignment(_e,footerModel.TextAlignment),
_e.SetStyles(printStyle),
_e.PrintLine(footerModel.Value),
_e.FeedLines(footerModel.FeedingLines)
};
data.Add(bytes);
}
return data;
}
}
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EpsonPrinter.Model;
using ESCPOS_NET;
using ESCPOS_NET.Emitters;
using ESCPOS_NET.Utilities;
namespace EpsonPrinter.Services
{
public class PrintHeader
{
private ICommandEmitter _e;
public PrintHeader(ICommandEmitter e)
{
_e = e;
}
public List<Byte[][]> Print(List<HeaderModel> model)
{
List<byte[][]> data = new List<byte[][]>();
PrintStyleCombination printStyleCombination = new PrintStyleCombination();
PrintAlignment printAlignment = new PrintAlignment();
foreach (HeaderModel headerModel in model)
{
PrintStyle printStyle = printStyleCombination.Combine(headerModel.PrintStyles);
byte[][] bytes = {
_e.SetStyles(PrintStyle.None),
printAlignment.GetTextAlignment(_e,headerModel.TextAlignment),
_e.SetStyles(printStyle),
_e.PrintLine(headerModel.Value),
_e.FeedLines(headerModel.FeedingLines)
};
data.Add(bytes);
}
byte[][] feedLines =
{
_e.FeedLines(1)
};
data.Add(feedLines);
return data;
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace EpsonPrinter.Services
{
public class PrintString
{
/// <summary>
/// An appropriate interval is converted into the length of
/// the tab about two texts. And make a printing data.
/// </summary>
/// <param name="iLineChars">
/// The width of the territory which it prints on is converted into the number of
/// characters, and that value is specified.
/// </param>
/// <param name="strBuf">
/// It is necessary as an information for deciding the interval of the text.
/// </param>
/// <param name="strPrice">
/// It is necessary as an information for deciding the interval of the text, too.
/// </param>
/// <returns>printing data.
/// </returns>
public String MakePrintString(int iLineChars, String strBuf, String strPrice)
{
int iSpaces = 0;
String tab = "";
try
{
iSpaces = iLineChars - (strBuf.Length + strPrice.Length);
for (int j = 0; j < iSpaces; j++)
{
tab += " ";
}
}
catch (Exception)
{
}
return strBuf + tab + strPrice;
}
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EpsonPrinter.Enums;
using EpsonPrinter.Model;
using ESCPOS_NET.Emitters;
namespace EpsonPrinter.Services
{
public class PrintStyleCombination
{
public PrintStyle Combine(PrintStyleModel printStyleModel)
{
//Bold
if (printStyleModel.Bold && !printStyleModel.DoubleHeight && !printStyleModel.DoubleWidth && !printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.Bold;
if (printStyleModel.Bold && printStyleModel.DoubleHeight && !printStyleModel.DoubleWidth && !printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.Bold | PrintStyle.DoubleHeight;
if(printStyleModel.Bold && printStyleModel.DoubleHeight && printStyleModel.DoubleWidth && !printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.Bold | PrintStyle.DoubleHeight | PrintStyle.DoubleWidth;
if (printStyleModel.Bold && printStyleModel.DoubleHeight && printStyleModel.DoubleWidth && printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.Bold | PrintStyle.DoubleHeight | PrintStyle.DoubleWidth | PrintStyle.FontB;
if (printStyleModel.Bold && printStyleModel.DoubleHeight && printStyleModel.DoubleWidth && printStyleModel.FontB && printStyleModel.Underline)
return PrintStyle.Bold | PrintStyle.DoubleHeight | PrintStyle.DoubleWidth | PrintStyle.FontB | PrintStyle.Underline;
//DoubleHeight
if (printStyleModel.DoubleHeight && !printStyleModel.DoubleWidth && !printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.DoubleHeight;
if (printStyleModel.DoubleHeight && printStyleModel.DoubleWidth && !printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.DoubleHeight | PrintStyle.DoubleWidth;
if (printStyleModel.DoubleHeight && printStyleModel.DoubleWidth && printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.DoubleHeight | PrintStyle.DoubleWidth | PrintStyle.FontB;
if (printStyleModel.DoubleHeight && printStyleModel.DoubleWidth && printStyleModel.FontB && printStyleModel.Underline)
return PrintStyle.DoubleHeight | PrintStyle.DoubleWidth | PrintStyle.FontB | PrintStyle.Underline;
//DoubleWidth
if (printStyleModel.DoubleWidth && !printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.DoubleWidth;
if (printStyleModel.DoubleWidth && printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.DoubleWidth | PrintStyle.FontB;
if (printStyleModel.DoubleWidth && printStyleModel.FontB && printStyleModel.Underline)
return PrintStyle.DoubleWidth | PrintStyle.FontB | PrintStyle.Underline;
//FontB
if (printStyleModel.FontB && !printStyleModel.Underline)
return PrintStyle.FontB;
if(printStyleModel.FontB && printStyleModel.Underline)
return PrintStyle.FontB | PrintStyle.Underline;
//Underline
if (printStyleModel.Underline)
return PrintStyle.Underline;
return PrintStyle.None;
}
}
}