Documents: Replace Razor templates with iText
All checks were successful
Test / Run tests (push) Successful in 2m24s
All checks were successful
Test / Run tests (push) Successful in 2m24s
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Models.Entities;
|
||||
using iText.Kernel.Pdf;
|
||||
using iText.Layout.Borders;
|
||||
using iText.Layout.Element;
|
||||
using iText.Layout.Layout;
|
||||
using iText.Layout.Properties;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Elwig.Documents {
|
||||
public class DeliveryNote : BusinessDocument {
|
||||
@@ -17,19 +25,183 @@ namespace Elwig.Documents {
|
||||
// 3 - full
|
||||
public int DisplayStats = App.Client.ModeDeliveryNoteStats;
|
||||
|
||||
public DeliveryNote(Delivery d, AppDbContext? ctx = null) : base($"{Name} Nr. {d.LsNr}", d.Member) {
|
||||
public DeliveryNote(Delivery d, AppDbContext? ctx = null) :
|
||||
base($"{Name} Nr. {d.LsNr}", d.Member) {
|
||||
UseBillingAddress = true;
|
||||
ShowDateAndLocation = true;
|
||||
Delivery = d;
|
||||
Aside = Aside.Replace("</table>", "") +
|
||||
$"<thead><tr><th colspan='2'>Lieferung</th></tr></thead><tbody>" +
|
||||
$"<tr><th>LS-Nr.:</th><td>{d.LsNr}</td></tr>" +
|
||||
$"<tr><th>Datum/Zeit:</th><td>{d.Date:dd.MM.yyyy} / {d.Time:HH:mm}</td></tr>" +
|
||||
$"<tr><th>Zweigstelle:</th><td>{d.Branch.Name}</td></tr>" +
|
||||
$"</tbody></table>";
|
||||
Text = App.Client.TextDeliveryNote;
|
||||
DocumentId = d.LsNr;
|
||||
IsDoublePaged = true;
|
||||
MemberBuckets = ctx?.GetMemberBuckets(d.Year, d.Member.MgNr).GetAwaiter().GetResult() ?? [];
|
||||
}
|
||||
|
||||
protected override void RenderHeader(iText.Layout.Document doc, PdfDocument pdf) {
|
||||
base.RenderHeader(doc, pdf);
|
||||
Aside?.AddCell(NewAsideCell("Lieferung", 2))
|
||||
.AddCell(NewAsideCell("LS-Nr.:", isName: true)).AddCell(NewAsideCell(Delivery.LsNr))
|
||||
.AddCell(NewAsideCell("Datum/Zeit:", isName: true)).AddCell(NewAsideCell($"{Delivery.Date:dd.MM.yyyy} / {Delivery.Time:HH:mm}"))
|
||||
.AddCell(NewAsideCell("Zweigstelle:", isName: true)).AddCell(NewAsideCell(Delivery.Branch.Name));
|
||||
}
|
||||
|
||||
protected override void RenderBody(iText.Layout.Document doc, PdfDocument pdf) {
|
||||
base.RenderBody(doc, pdf);
|
||||
|
||||
doc.Add(NewDeliveryTable());
|
||||
if (Delivery.Comment != null) {
|
||||
doc.Add(new KernedParagraph($"Anmerkung zur Lieferung: {Delivery.Comment}", 10).SetMarginsMM(5, 0, 0, 0));
|
||||
}
|
||||
if (DisplayStats > 0) {
|
||||
doc.Add(NewBucketTable(Delivery.Season, MemberBuckets, isTiny: true,
|
||||
filter: DisplayStats > 2 ? null : DisplayStats == 1 ? [] : Delivery.Parts.Select(p => p.SortId).Distinct().ToList())
|
||||
.SetKeepTogether(true)
|
||||
.SetMarginsMM(5, 0, 0, 0));
|
||||
}
|
||||
|
||||
var sig = new Div().SetWidth(165 * PtInMM);
|
||||
if (Text != null)
|
||||
sig.Add(new KernedParagraph(Regex.Replace(Text, @"\s+", " "), 10).SetTextAlignment(TextAlignment.JUSTIFIED).SetSpacingRatio(1));
|
||||
sig.Add(new Table(ColsMM(15, 50, 35, 50, 15)).SetMarginsMM(18, 0, 2, 0)
|
||||
.AddCell(NewTd())
|
||||
.AddCell(NewTd("Genossenschaft", center: true, borderTop: true).SetPaddingTopMM(1))
|
||||
.AddCell(NewTd())
|
||||
.AddCell(NewTd("Mitglied", center: true, borderTop: true).SetPaddingTopMM(1))
|
||||
.AddCell(NewTd()));
|
||||
|
||||
var renderer = sig.CreateRendererSubTree();
|
||||
renderer.SetParent(doc.GetRenderer());
|
||||
var layoutResult = renderer.Layout(new LayoutContext(new LayoutArea(1, pdf.GetDefaultPageSize())));
|
||||
float sigHeight = layoutResult.GetOccupiedArea().GetBBox().GetHeight();
|
||||
doc.Add(new Div().SetWidth(165 * PtInMM).SetHeight(sigHeight));
|
||||
|
||||
var size = pdf.GetDefaultPageSize();
|
||||
doc.Add(sig.SetFixedPosition(
|
||||
size.GetLeft() + doc.GetLeftMargin(),
|
||||
size.GetBottom() + doc.GetBottomMargin(),
|
||||
size.GetWidth() - doc.GetLeftMargin() - doc.GetRightMargin())); ;
|
||||
}
|
||||
|
||||
protected Cell NewDeliveryMainTd(string? text, int rowspan = 1, int colspan = 1, bool center = false, bool right = false) {
|
||||
return NewTd(text, 12, rowspan: rowspan, colspan: colspan, bold: true, center: center, right: right)
|
||||
.SetPaddingTopMM(2);
|
||||
}
|
||||
|
||||
protected Table NewDeliveryTable() {
|
||||
var tbl = new Table(ColsMM(10, 21, 25, 19.5, 19.5, 30, 12.5, 12.5, 15), true)
|
||||
.SetWidth(UnitValue.CreatePercentValue(100)).SetFixedLayout()
|
||||
.SetBorder(Border.NO_BORDER).SetBorderCollapse(BorderCollapsePropertyValue.COLLAPSE);
|
||||
|
||||
tbl.AddHeaderCell(NewTh("Pos.", rowspan: 2, left: true))
|
||||
.AddHeaderCell(NewTh("Sorte", colspan: 2, rowspan: 2, left: true))
|
||||
.AddHeaderCell(NewTh("Attribut", colspan: 2, rowspan: 2, left: true))
|
||||
.AddHeaderCell(NewTh("Qualitätsstufe", rowspan: 2, left: true))
|
||||
.AddHeaderCell(NewTh("Gradation", colspan: 2))
|
||||
.AddHeaderCell(NewTh("Menge"))
|
||||
.AddHeaderCell(NewTh("[°Oe]", 8))
|
||||
.AddHeaderCell(NewTh("[°KMW]", 8))
|
||||
.AddHeaderCell(NewTh("[kg]", 8));
|
||||
|
||||
foreach (var part in Delivery.Parts.OrderBy(p => p.DPNr)) {
|
||||
var sub = new Table(ColsMM(10, 21, 25, 19.5, 19.5, 30, 12.5, 12.5, 15), true)
|
||||
.SetWidth(UnitValue.CreatePercentValue(100)).SetFixedLayout()
|
||||
.SetBorderCollapse(BorderCollapsePropertyValue.COLLAPSE);
|
||||
|
||||
int rowspan = 1 + (part.Cultivation != null ? 1 : 0) + 1 + part.Modifiers.Count() + 1 + (part.Comment != null ? 1 : 0) + (part.Temperature != null || part.Acid != null ? 1 : 0);
|
||||
sub.AddCell(NewDeliveryMainTd($"{part.DPNr:N0}", center: true))
|
||||
.AddCell(NewDeliveryMainTd(part.Variety.Name, colspan: 2))
|
||||
.AddCell(NewDeliveryMainTd(part.Attribute?.Name, colspan: 2))
|
||||
.AddCell(NewDeliveryMainTd(part.Quality.Name))
|
||||
.AddCell(NewDeliveryMainTd($"{part.Oe:N0}", center: true))
|
||||
.AddCell(NewDeliveryMainTd($"{part.Kmw:N1}", center: true))
|
||||
.AddCell(NewDeliveryMainTd($"{part.Weight:N0}", center: true));
|
||||
|
||||
if (part.Cultivation != null) {
|
||||
var cult = new KernedParagraph(8);
|
||||
cult.Add(Italic("Bewirtschaftung:")).Add(Normal(" " + part.Cultivation.Name + (part.Cultivation.Description != null ? $" ({part.Cultivation.Description})" : "")));
|
||||
sub.AddCell(NewTd())
|
||||
.AddCell(NewTd(cult, colspan: 5))
|
||||
.AddCell(NewTd(colspan: 3));
|
||||
}
|
||||
sub.AddCell(NewTd())
|
||||
.AddCell(NewTd(new KernedParagraph(8).Add(Italic("Herkunft:")).Add(Normal(" " + part.OriginString.Replace("\n ", "\n\u00a0"))), colspan: 5))
|
||||
.AddCell(NewTd(colspan: 3));
|
||||
if (part.Modifiers.Any()) {
|
||||
sub.AddCell(NewTd())
|
||||
.AddCell(NewTd("Zu-/Abschläge:", 8, italic: true));
|
||||
int i = 0, last = part.Modifiers.Count() - 1;
|
||||
foreach (var mod in part.Modifiers) {
|
||||
if (i > 0) sub.AddCell(NewCell(colspan: 2));
|
||||
sub.AddCell(NewTd(mod.Name, 8, bold: true, colspan: 3).SetPaddingsMM(i == 0 ? 0.5f : 0, 1, i == last ? 0.5f : 0, 1))
|
||||
.AddCell(NewTd(mod.PublicValueStr, 8).SetPaddingsMM(i == 0 ? 0.5f : 0, 1, i == last ? 0.5f : 0, 1))
|
||||
.AddCell(NewTd(colspan: 3));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
var weighing = new KernedParagraph(8);
|
||||
if (part.IsManualWeighing) {
|
||||
weighing.Add(Italic("Handwiegung " + (part.IsNetWeight ? "(gerebelt gewogen)" : "(nicht gerebelt gewogen)")));
|
||||
if (part.WeighingReason != null) {
|
||||
weighing.Add(Normal(", "))
|
||||
.Add(Italic("Begründung:"))
|
||||
.Add(Normal(" " + part.WeighingReason));
|
||||
}
|
||||
} else {
|
||||
var info = part.WeighingInfo;
|
||||
weighing.Add(Italic("Waage:"))
|
||||
.Add(Normal(" " + (part.ScaleId ?? "?") + ", "))
|
||||
.Add(Italic("ID:"))
|
||||
.Add(Normal(" " + (info.Id ?? "?")));
|
||||
if (info.Date != null || info.Time != null)
|
||||
weighing.Add(Normal(" \u2013 "));
|
||||
if (info.Time != null)
|
||||
weighing.Add(Normal($"{info.Time:HH:mm}"));
|
||||
if (info.Date != null)
|
||||
weighing.Add(Normal($", {info.Date:dd.MM.yyyy}"));
|
||||
if (info.Gross != null && info.Tare != null && info.Net != null) {
|
||||
weighing.Add("\n")
|
||||
.Add(Italic("Brutto:"))
|
||||
.Add(Normal($" {info.Gross:N0} kg \u2013 "))
|
||||
.Add(Italic("Tara:"))
|
||||
.Add(Normal($" {info.Tare:N0} kg \u2013 "))
|
||||
.Add(Italic("Netto:"))
|
||||
.Add(Normal($" {info.Net:N0} kg \u2013 "))
|
||||
.Add(Italic(part.IsNetWeight ? "gerebelt gewogen" : "nicht gerebelt gewogen"));
|
||||
} else {
|
||||
weighing.Add(" ")
|
||||
.Add(Italic(part.IsNetWeight ? "(gerebelt gewogen)" : "(nicht gerebelt gewogen)"));
|
||||
}
|
||||
}
|
||||
sub.AddCell(NewCell())
|
||||
.AddCell(NewCell(weighing, colspan: 5))
|
||||
.AddCell(NewCell(colspan: 3));
|
||||
if (part.Comment != null) {
|
||||
sub.AddCell(NewCell())
|
||||
.AddCell(NewCell(new KernedParagraph(8).Add(Italic("Anmerkung")).Add(Normal(" " + part.Comment)), colspan: 5))
|
||||
.AddCell(NewCell(colspan: 3));
|
||||
}
|
||||
if (part.Temperature != null || part.Acid != null) {
|
||||
var p = new KernedParagraph(8);
|
||||
if (part.Temperature != null)
|
||||
p.Add(Italic("Temperatur:")).Add(Normal($" {part.Temperature:N1} °C"));
|
||||
if (part.Temperature != null && part.Acid != null)
|
||||
p.Add(Normal(", "));
|
||||
if (part.Acid != null)
|
||||
p.Add(Italic("Säure")).Add(Normal($" {part.Acid:N1} g/l"));
|
||||
sub.AddCell(NewCell())
|
||||
.AddCell(NewCell(p, colspan: 5))
|
||||
.AddCell(NewCell(colspan: 3));
|
||||
}
|
||||
tbl.AddCell(new Cell(1, 9).SetPadding(0).SetBorder(Border.NO_BORDER).Add(sub));
|
||||
}
|
||||
|
||||
if (Delivery.Parts.Count > 1) {
|
||||
tbl.AddCell(NewTd("Gesamt:", 12, bold: true, borderTop: true, colspan: 6).SetPaddingsMM(1, 1, 1, 1))
|
||||
.AddCell(NewTd($"{Delivery.Oe:N0}", 12, bold: true, center: true, borderTop: true).SetPaddingsMM(1, 1, 1, 1))
|
||||
.AddCell(NewTd($"{Delivery.Kmw:N1}", 12, bold: true, center: true, borderTop: true).SetPaddingsMM(1, 1, 1, 1))
|
||||
.AddCell(NewTd($"{Delivery.Weight:N0}", 12, bold: true, right: true, borderTop: true).SetPaddingsMM(1, 1, 1, 1));
|
||||
}
|
||||
|
||||
return tbl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user