using Elwig.Helpers; using Elwig.Models.Dtos; using Elwig.Models.Entities; using iText.Kernel.Pdf; using iText.Layout.Borders; using iText.Layout.Element; using iText.Layout.Properties; using System; using System.Collections.Generic; using System.Linq; namespace Elwig.Documents { public class DeliveryConfirmation : BusinessDocument { public new static string Name => "Anlieferungsbestätigung"; public Season Season; public DeliveryConfirmationDeliveryData Data; public string? Text = App.Client.TextDeliveryConfirmation; public Dictionary MemberBuckets; public List MemberStats; public DeliveryConfirmation(AppDbContext ctx, int year, Member m, DeliveryConfirmationDeliveryData data) : base($"{Name} {year}", m) { Season = ctx.Seasons.Find(year) ?? throw new ArgumentException("invalid season"); ShowDateAndLocation = true; UseBillingAddress = true; DocumentId = $"Anl.-Best. {Season.Year}/{m.MgNr}"; Data = data; MemberBuckets = ctx.GetMemberBuckets(Season.Year, m.MgNr).GetAwaiter().GetResult(); MemberStats = AppDbContext.GetMemberStats(Season.Year, m.MgNr).GetAwaiter().GetResult(); } protected override void RenderHeader(iText.Layout.Document doc, PdfDocument pdf) { base.RenderHeader(doc, pdf); var firstDay = Data.Rows.MinBy(r => r.Date)?.Date; var lastDay = Data.Rows.MaxBy(r => r.Date)?.Date; Aside?.AddCell(NewAsideCell("Saison", 2)) .AddCell(NewAsideCell("Lieferungen:", isName: true)).AddCell(NewAsideCell($"{Data.Rows.DistinctBy(r => r.LsNr).Count():N0} (Teil-Lfrg.: {Data.RowNum:N0})")) .AddCell(NewAsideCell("Zeitraum:", isName: true)).AddCell(NewAsideCell(firstDay == null || lastDay == null ? "-" : firstDay == lastDay ? $"{firstDay:dd.MM.} (1 Tag)" : $"{firstDay:dd.MM.}\u2013{lastDay:dd.MM.} ({lastDay?.DayNumber - firstDay?.DayNumber + 1:N0} Tage)")) .AddCell(NewAsideCell("Menge:", isName: true)).AddCell(NewAsideCell($"{MemberStats.Sum(s => s.Weight):N0} kg")); } protected override void RenderBody(iText.Layout.Document doc, PdfDocument pdf) { base.RenderBody(doc, pdf); doc.Add(NewDeliveryListTable(Data)); doc.Add(NewWeightsTable(MemberStats) .SetMarginTopMM(10).SetKeepTogether(true)); doc.Add(NewBucketTable(Season, MemberBuckets, includePayment: true) .SetMarginTopMM(10).SetKeepTogether(true)); if (Text != null) { doc.Add(new KernedParagraph(Text, 10) .SetMarginTop(20).SetKeepTogether(true)); } } protected Table NewDeliveryListTable(DeliveryConfirmationDeliveryData data) { var tbl = new Table(ColsMM(25, 7, 39, 14, 11, 11, 15, 12, 14, 3, 14), true) .SetWidth(UnitValue.CreatePercentValue(100)).SetFixedLayout() .SetBorderCollapse(BorderCollapsePropertyValue.COLLAPSE); tbl.AddHeaderCell(NewTh("Lieferschein-Nr.", rowspan: 2, left: true)) .AddHeaderCell(NewTh("Pos.", rowspan: 2, left: true)) .AddHeaderCell(NewTh("Sorte/Attribut/Bewirtschaftung\nZu-/Abschlag", rowspan: 2, left: true)) .AddHeaderCell(NewTh("Qual.", rowspan: 2)) .AddHeaderCell(NewTh("Gradation", colspan: 2)) .AddHeaderCell(NewTh("Flächenbindung", colspan: 2)) .AddHeaderCell(NewTh("Menge")) .AddHeaderCell(NewTh("gerebelt", rowspan: 3, rotated: true)) .AddHeaderCell(NewTh("Davon\nabzuwerten")) .AddHeaderCell(NewTh("[°Oe]")) .AddHeaderCell(NewTh("[°KMW]")) .AddHeaderCell(NewTh("[kg]", colspan: 2)) .AddHeaderCell(NewTh("[kg]")) .AddHeaderCell(NewTh("[kg]")); var lastVariety = ""; foreach (var p in data.Rows) { var sub = new Table(ColsMM(25, 7, 39, 14, 11, 11, 15, 12, 14, 3, 14)) .SetWidth(UnitValue.CreatePercentValue(100)).SetFixedLayout() .SetBorderCollapse(BorderCollapsePropertyValue.COLLAPSE) .SetKeepTogether(true); if (lastVariety != "" && lastVariety != p.Variety) { sub.SetBorderTop(new SolidBorder(BorderThickness)); } var attr = p.Attribute != null || p.Cultivation != null; var rows = Math.Max(p.Buckets.Length, 1 + (attr ? 1 : 0) + p.Modifiers.Length); for (int i = 0; i < rows; i++) { if (i == 0) { sub.AddCell(NewTd(p.LsNr)) .AddCell(NewTd($"{p.DPNr:N0}", center: true)) .AddCell(NewTd(p.Variety)) .AddCell(NewTd(p.QualId, center: true)) .AddCell(NewTd($"{p.Gradation.Oe:N0}", center: true)) .AddCell(NewTd($"{p.Gradation.Kmw:N1}", center: true)); } else if (i == 1 && attr) { sub.AddCell(NewCell(colspan: 2)) .AddCell(NewTd($"{p.Attribute}{(p.Attribute != null && p.Cultivation != null ? " / " : "")}{p.Cultivation}", 8, colspan: 2) .SetPaddingsMM(0.125f, 1, 0.125f, 1)) .AddCell(NewCell(colspan: 2)); } else { sub.AddCell(NewCell(colspan: 2)); if (i - (rows - p.Modifiers.Length) < p.Modifiers.Length) { sub.AddCell(NewTd(p.Modifiers[i - (rows - p.Modifiers.Length)], 8, colspan: 2) .SetPaddingsMM(0.125f, 0, 0.125f, 5)); } else { sub.AddCell(NewCell(colspan: 2)); } sub.AddCell(NewCell(colspan: 2)); } if (i < p.Buckets.Length) { var bucket = p.Buckets[i]; sub.AddCell(NewTd($"{bucket.Name}:", 8).SetHeight(10).SetPaddingsMM(0.125f, 0, 0.125f, 0)); sub.AddCell(NewTd($"{bucket.Value:N0}", right: true)); } else { sub.AddCell(NewCell(colspan: 2)); } if (i == p.Buckets.Length - 1) { sub.AddCell(NewTd($"{p.Weight:N0}", right: true)); sub.AddCell(NewTd(p.IsNetWeight ? "\u2611" : "\u2610", 7, right: true).SetFont(SF).SetPadding(0)); } else { sub.AddCell(NewCell(colspan: 2)); } if (i == 0) { sub.AddCell(NewCell(rowspan: rows)); } lastVariety = p.Variety; } tbl.AddCell(new Cell(1, 11).SetPadding(0).SetBorder(Border.NO_BORDER).Add(sub)); } tbl.AddCell(NewTd("Gesamt:", 12, colspan: 7, bold: true, borderTop: true) .SetPaddingsMM(1, 1, 0, 1)) .AddCell(NewTd($"{data.Rows.Sum(p => p.Weight):N0}", 12, colspan: 2, right: true, bold: true, borderTop: true) .SetPaddingsMM(1, 1, 0, 1)) .AddCell(NewTd(colspan: 2, borderTop: true) .SetPaddingsMM(1, 1, 0, 1)) .AddCell(NewTd("Davon abgewertet:", 10, colspan: 7) .SetPaddingsMM(0, 1, 0.5f, 1)) .AddCell(NewTd($"{data.Rows.Where(p => p.IsDepreciated).Sum(p => p.Weight):N0}", 10, colspan: 2, right: true) .SetPaddingsMM(0, 1, 0.5f, 1)) .AddCell(NewCell(colspan: 2) .SetPaddingsMM(0, 1, 0.5f, 1)); return tbl; } } }