297 lines
17 KiB
C#
297 lines
17 KiB
C#
using Elwig.Helpers;
|
||
using Elwig.Helpers.Billing;
|
||
using Elwig.Models.Dtos;
|
||
using Elwig.Models.Entities;
|
||
using iText.Kernel.Colors;
|
||
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 PaymentVariantSummary : Document {
|
||
|
||
public new static string Name => "Auszahlungsvariante";
|
||
|
||
public PaymentVariantSummaryData Data;
|
||
public PaymentVar Variant;
|
||
public BillingData BillingData;
|
||
public string CurrencySymbol;
|
||
public int MemberNum;
|
||
public int DeliveryNum;
|
||
public int DeliveryPartNum;
|
||
public List<ModifierStat> ModifierStat;
|
||
public Dictionary<string, Modifier> Modifiers;
|
||
|
||
public PaymentVariantSummary(PaymentVar v, PaymentVariantSummaryData data) :
|
||
base($"{Name} {v.Year} - {v.Name}") {
|
||
Variant = v;
|
||
BillingData = BillingData.FromJson(v.Data);
|
||
Data = data;
|
||
CurrencySymbol = v.Season.Currency.Symbol ?? v.Season.Currency.Code;
|
||
MemberNum = v.Credits.Count;
|
||
IsPreview = MemberNum == 0;
|
||
DeliveryNum = v.DeliveryPartPayments.DistinctBy(p => p.DeliveryPart.Delivery).Count();
|
||
DeliveryPartNum = v.DeliveryPartPayments.Count;
|
||
ModifierStat = AppDbContext.GetModifierStats(v.Year, v.AvNr).GetAwaiter().GetResult();
|
||
Modifiers = v.Season.Modifiers.ToDictionary(m => m.ModId);
|
||
}
|
||
|
||
protected override void RenderBody(iText.Layout.Document doc, PdfDocument pdf) {
|
||
base.RenderBody(doc, pdf);
|
||
doc.Add(new KernedParagraph($"{Name} Lese {Variant.Year}", 24)
|
||
.SetTextAlignment(TextAlignment.CENTER).SetFont(BF)
|
||
.SetMarginsMM(0, 0, 2, 0));
|
||
doc.Add(new KernedParagraph(Variant.Name, 14)
|
||
.SetTextAlignment(TextAlignment.CENTER).SetFont(BF)
|
||
.SetMarginsMM(0, 0, 10, 0));
|
||
doc.Add(NewVariantStatTable().SetMarginBottomMM(10));
|
||
doc.Add(NewModifierStatTable());
|
||
doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
|
||
doc.Add(NewPriceTable());
|
||
}
|
||
|
||
protected Cell NewSectionHdr(string text, int colspan = 1, bool borderLeft = false) {
|
||
return NewTd(text, 10, colspan: colspan, bold: true, italic: true, center: true, borderTop: true)
|
||
.SetBackgroundColor(new DeviceRgb(0xe0, 0xe0, 0xe0))
|
||
.SetPaddingsMM(0.5f, 1, 0.5f, 1)
|
||
.SetBorderLeft(borderLeft ? new SolidBorder(BorderThickness) : Border.NO_BORDER);
|
||
}
|
||
|
||
protected Cell NewSectionTh(string? text = null, float fontSize = 10, int colspan = 1, bool borderTop = false, bool borderLeft = false, bool overflow = false) {
|
||
return NewTd(text, fontSize, colspan: colspan, italic: true, borderTop: borderTop, overflow: overflow)
|
||
.SetPaddingRightMM(0)
|
||
.SetBorderLeft(borderLeft ? new SolidBorder(BorderThickness) : Border.NO_BORDER);
|
||
}
|
||
|
||
protected Table NewVariantStatTable() {
|
||
var tbl = new Table(ColsMM(20, 30, 4.5, 4.5, 23.5, 47.5, 15, 20))
|
||
.SetWidth(UnitValue.CreatePercentValue(100)).SetFixedLayout()
|
||
.SetBorderCollapse(BorderCollapsePropertyValue.COLLAPSE)
|
||
.SetBorder(new SolidBorder(BorderThickness));
|
||
|
||
//var sum1 = Variant.DeliveryPartPayments.Sum(p => p.NetAmount);
|
||
//var sum2 = Variant.Credits.Sum(p => p.); //Variant.MemberPayments.Sum(p => p.Amount);
|
||
var deliveryModifiers = Variant.DeliveryPartPayments.Sum(p => p.Amount - p.NetAmount);
|
||
var memberModifiers = Variant.Credits.Sum(c => c.Payment.Amount - c.Payment.NetAmount);
|
||
var sum2 = Variant.Credits.Sum(p => p.NetAmount);
|
||
var sum1 = sum2 - deliveryModifiers - memberModifiers;
|
||
var payed = -Variant.Credits.Sum(p => p.PrevNetAmount ?? 0m);
|
||
var netSum = Variant.Credits.Sum(p => p.NetAmount) - Variant.Credits.Sum(p => p.PrevNetAmount ?? 0m);
|
||
var vat = Variant.Credits.Sum(p => p.VatAmount);
|
||
var grossSum = Variant.Credits.Sum(p => p.GrossAmount);
|
||
var totalMods = Variant.Credits.Sum(p => p.Modifiers ?? 0m);
|
||
var considered = -Variant.Credits.Sum(p => p.PrevModifiers ?? 0m);
|
||
var totalSum = Variant.Credits.Sum(p => p.Amount);
|
||
|
||
var weiRows = Data.Rows.Where(r => r.QualityLevel == "Wein");
|
||
var minWei = weiRows.Min(r => r.Ungeb.MinPrice);
|
||
var maxWei = weiRows.Max(r => r.Ungeb.MaxPrice);
|
||
var quwRows = Data.Rows.Where(r => r.QualityLevel != "Wein");
|
||
var minPrice = quwRows.Min(r => r.Ungeb.MinPrice);
|
||
var maxPrice = quwRows.Max(r => r.Ungeb.MaxPrice);
|
||
var gebRows = Data.Rows
|
||
.Where(r => r.Geb.MaxPrice != null && r.Ungeb.MinPrice != null)
|
||
.Select(r => r.Geb.MaxPrice - r.Ungeb.MinPrice);
|
||
var minGeb = gebRows.Min();
|
||
var maxGeb = gebRows.Max();
|
||
|
||
tbl.AddCell(NewSectionHdr("Allgemein", colspan: 5))
|
||
.AddCell(NewSectionHdr("Berücksichtigt", colspan: 3, borderLeft: true))
|
||
|
||
.AddCell(NewSectionTh("Name:"))
|
||
.AddCell(NewTd(Variant.Name, colspan: 4))
|
||
.AddCell(NewSectionTh("Zu-/Abschläge bei Lieferungen:", colspan: 2, borderLeft: true))
|
||
.AddCell(NewTd(BillingData.ConsiderDelieryModifiers ? "Ja" : "Nein", center: true))
|
||
|
||
.AddCell(NewSectionTh("Beschr.:"))
|
||
.AddCell(NewTd(Variant.Comment, colspan: 4))
|
||
.AddCell(NewSectionTh("Pönalen bei Unterlieferungen (FB):", colspan: 2, borderLeft: true))
|
||
.AddCell(NewTd(BillingData.ConsiderContractPenalties ? "Ja" : "Nein", center: true))
|
||
|
||
.AddCell(NewSectionTh("Rebel-Zuschl.:", overflow: true))
|
||
.AddCell(NewTd($"{Utils.GetSign(BillingData.NetWeightModifier)}{Math.Abs(BillingData.NetWeightModifier) * 100:N2} % / {Utils.GetSign(BillingData.GrossWeightModifier)}{Math.Abs(BillingData.GrossWeightModifier) * 100:N2} %", colspan: 4, center: true))
|
||
.AddCell(NewSectionTh("Strafen bei Unterlieferungen (GA):", colspan: 2, borderLeft: true))
|
||
.AddCell(NewTd(BillingData.ConsiderTotalPenalty ? "Ja" : "Nein", center: true))
|
||
|
||
.AddCell(NewSectionTh("Datum/Überw.:", overflow: true))
|
||
.AddCell(NewTd($"{Variant.Date:dd.MM.yyyy} / {Variant.TransferDate:dd.MM.yyyy}", colspan: 4, center: true))
|
||
.AddCell(NewSectionTh("Automatische Nachzeichnung der GA:", colspan: 2, borderLeft: true))
|
||
.AddCell(NewTd(BillingData.ConsiderAutoBusinessShares ? "Ja" : "Nein", center: true))
|
||
|
||
.AddCell(NewSectionTh("Berechnung:"))
|
||
.AddCell(NewTd($"{Variant.CalcTime:dd.MM.yyyy, HH:mm:ss}", colspan: 4, center: true))
|
||
.AddCell(NewSectionTh("Benutzerdef. Zu-/Abschläge pro Mitglied:", colspan: 2, borderLeft: true))
|
||
.AddCell(NewTd(BillingData.ConsiderCustomModifiers ? "Ja" : "Nein", center: true))
|
||
|
||
.AddCell(NewSectionHdr("Beträge", colspan: 5))
|
||
.AddCell(NewSectionHdr("Statistik", colspan: 3, borderLeft: true))
|
||
|
||
.AddCell(NewSectionTh("Zwischensumme:", colspan: 2))
|
||
.AddCell(NewTd())
|
||
.AddCell(NewTd(CurrencySymbol))
|
||
.AddCell(NewTd($"{sum1:N2}", right: true))
|
||
.AddCell(NewSectionTh("Lieferanten:", borderLeft: true))
|
||
.AddCell(NewTd($"{MemberNum:N0}", colspan: 2, right: true))
|
||
|
||
.AddCell(NewSectionTh("Zu-/Abschläge (Mitglieder):", colspan: 2))
|
||
.AddCell(NewTd(Utils.GetSign(memberModifiers), right: true))
|
||
.AddCell(NewTd(CurrencySymbol))
|
||
.AddCell(NewTd($"{Math.Abs(memberModifiers):N2}", right: true))
|
||
.AddCell(NewSectionTh("Lieferungen:", borderLeft: true))
|
||
.AddCell(NewTd($"{DeliveryNum:N0}", colspan: 2, right: true))
|
||
|
||
.AddCell(NewSectionTh("Zu-/Abschläge (Lieferungen):", colspan: 2))
|
||
.AddCell(NewTd(Utils.GetSign(deliveryModifiers), right: true))
|
||
.AddCell(NewTd(CurrencySymbol))
|
||
.AddCell(NewTd($"{Math.Abs(deliveryModifiers):N2}", right: true))
|
||
.AddCell(NewSectionTh("Teillieferungen:", borderLeft: true))
|
||
.AddCell(NewTd($"{DeliveryPartNum:N0}", colspan: 2, right: true))
|
||
|
||
.AddCell(NewSectionTh("Gesamtsumme:", colspan: 2))
|
||
.AddCell(NewTd(borderTop: true))
|
||
.AddCell(NewTd(CurrencySymbol, borderTop: true))
|
||
.AddCell(NewTd($"{sum2:N2}", right: true, borderTop: true))
|
||
.AddCell(NewSectionTh(borderLeft: true))
|
||
.AddCell(NewTd(colspan: 2))
|
||
|
||
.AddCell(NewSectionTh("Bisher ausgezahlt:", colspan: 2))
|
||
.AddCell(NewTd(Utils.GetSign(payed), right: true))
|
||
.AddCell(NewTd(CurrencySymbol))
|
||
.AddCell(NewTd($"{Math.Abs(payed):N2}", right: true))
|
||
.AddCell(NewSectionTh("Preis (abgewertet):", borderTop: true, borderLeft: true))
|
||
.AddCell(NewTd((minWei != maxWei ? $"{minWei:N4}\u2013{maxWei:N4}" : $"{minWei:N4}") + $" {CurrencySymbol}/kg", colspan: 2, center: true, borderTop: true))
|
||
|
||
.AddCell(NewSectionTh("Nettosumme:", colspan: 2))
|
||
.AddCell(NewTd(borderTop: true))
|
||
.AddCell(NewTd(CurrencySymbol, borderTop: true))
|
||
.AddCell(NewTd($"{netSum:N2}", right: true, borderTop: true))
|
||
.AddCell(NewSectionTh("Preis (ungeb., nicht abgew.):", borderLeft: true))
|
||
.AddCell(NewTd((minPrice != maxPrice ? $"{minPrice:N4}–{maxPrice:N4}" : $"{minPrice:N4}") + $" {CurrencySymbol}/kg", colspan: 2, center: true))
|
||
|
||
.AddCell(NewSectionTh("Mehrwertsteuer:", colspan: 2))
|
||
.AddCell(NewTd(Utils.GetSign(vat), right: true))
|
||
.AddCell(NewTd(CurrencySymbol))
|
||
.AddCell(NewTd($"{Math.Abs(vat):N2}", right: true))
|
||
.AddCell(NewSectionTh("Gebunden-Zuschlag:", borderLeft: true))
|
||
.AddCell(NewTd(minGeb != maxGeb ? $"{minGeb:N4}\u2013{maxGeb:N4} {CurrencySymbol}/kg" : minGeb == 0 ? "-" : $"{minGeb:N4} {CurrencySymbol}/kg", colspan: 2, center: true))
|
||
|
||
.AddCell(NewSectionTh("Bruttosumme:", colspan: 2))
|
||
.AddCell(NewTd(borderTop: true))
|
||
.AddCell(NewTd(CurrencySymbol, borderTop: true))
|
||
.AddCell(NewTd($"{grossSum:N2}", right: true, borderTop: true))
|
||
.AddCell(NewSectionTh(borderLeft: true))
|
||
.AddCell(NewTd(colspan: 2))
|
||
|
||
.AddCell(NewSectionTh("Abzüge (Strafen/Pönalen, GA, \u2026):", colspan: 2))
|
||
.AddCell(NewTd(Utils.GetSign(totalMods)))
|
||
.AddCell(NewTd(CurrencySymbol))
|
||
.AddCell(NewTd($"{Math.Abs(totalMods):N2}", right: true))
|
||
.AddCell(NewSectionTh("Menge (ungebunden):", borderLeft: true, borderTop: true))
|
||
.AddCell(NewTd($"{Data.Rows.Sum(r => r.Ungeb.Weight):N0} kg", colspan: 2, right: true, borderTop: true))
|
||
|
||
.AddCell(NewSectionTh("Bereits berücksichtigte Abzüge:", colspan: 2))
|
||
.AddCell(NewTd(Utils.GetSign(considered)))
|
||
.AddCell(NewTd(CurrencySymbol))
|
||
.AddCell(NewTd($"{Math.Abs(considered):N2}", right: true))
|
||
.AddCell(NewSectionTh("Menge (gebunden):", borderLeft: true))
|
||
.AddCell(NewTd($"{Data.Rows.Sum(r => r.Geb.Weight + r.LowGeb.Weight):N0} kg", colspan: 2, right: true))
|
||
|
||
.AddCell(NewSectionTh("Auszahlungsbetrag:", colspan: 2))
|
||
.AddCell(NewTd(borderTop: true))
|
||
.AddCell(NewTd(CurrencySymbol, borderTop: true))
|
||
.AddCell(NewTd($"{totalSum:N2}", right: true, borderTop: true))
|
||
.AddCell(NewSectionTh("Gesamtmenge:", borderLeft: true))
|
||
.AddCell(NewTd($"{Data.Rows.Sum(r => r.Ungeb.Weight + r.LowGeb.Weight + r.Geb.Weight):N0} kg", colspan: 2, right: true, borderTop: true));
|
||
|
||
return tbl;
|
||
}
|
||
|
||
protected Table NewModifierStatTable() {
|
||
var tbl = new Table(ColsMM(35, 30, 25, 25, 25, 25))
|
||
.SetWidth(UnitValue.CreatePercentValue(100)).SetFixedLayout()
|
||
.SetBorderCollapse(BorderCollapsePropertyValue.COLLAPSE)
|
||
.SetBorder(new SolidBorder(BorderThickness));
|
||
|
||
tbl.AddCell(NewSectionHdr("Statistik Zu-/Abschläge", colspan: 6))
|
||
.AddCell(NewTh("Name", rowspan: 2))
|
||
.AddCell(NewTh("Zu-/Abschlag", rowspan: 2))
|
||
.AddCell(NewTh("Lieferungen"))
|
||
.AddCell(NewTh("Minimum"))
|
||
.AddCell(NewTh("Maximum"))
|
||
.AddCell(NewTh("Betrag"))
|
||
.AddCell(NewTh("[#]"))
|
||
.AddCell(NewTh($"[{CurrencySymbol}]"))
|
||
.AddCell(NewTh($"[{CurrencySymbol}]"))
|
||
.AddCell(NewTh($"[{CurrencySymbol}]"));
|
||
|
||
foreach (var m in ModifierStat) {
|
||
var mod = Modifiers[m.ModId];
|
||
tbl.AddCell(NewTd(mod.Name, italic: true))
|
||
.AddCell(NewTd(mod.ValueStr, right: true))
|
||
.AddCell(NewTd($"{m.Count:N0}", right: true))
|
||
.AddCell(NewTd($"{m.Min:N2}", right: true))
|
||
.AddCell(NewTd($"{m.Max:N2}", right: true))
|
||
.AddCell(NewTd($"{m.Sum:N2}", right: true));
|
||
}
|
||
|
||
return tbl;
|
||
}
|
||
|
||
protected Table NewPriceTable() {
|
||
var tbl = new Table(ColsMM(25, 19, 18, 15, 18, 15, 18, 15, 22))
|
||
.SetWidth(UnitValue.CreatePercentValue(100)).SetFixedLayout()
|
||
.SetBorderCollapse(BorderCollapsePropertyValue.COLLAPSE);
|
||
|
||
tbl.AddHeaderCell(NewTh("Sorte/Attr./Bewirt.\nQualitätsstufe", rowspan: 2, left: true))
|
||
.AddHeaderCell(NewTh("Gradation"))
|
||
.AddHeaderCell(NewTh("ungebunden", colspan: 2))
|
||
.AddHeaderCell(NewTh("attributlos gebunden", colspan: 2))
|
||
.AddHeaderCell(NewTh("gebunden", colspan: 2))
|
||
.AddHeaderCell(NewTh("Gesamt"))
|
||
.AddHeaderCell(NewTh(true ? "[°Oe]" : "[°KMW]"))
|
||
.AddHeaderCell(NewTh("[kg]"))
|
||
.AddHeaderCell(NewTh($"[{CurrencySymbol}/kg]"))
|
||
.AddHeaderCell(NewTh("[kg]"))
|
||
.AddHeaderCell(NewTh($"[{CurrencySymbol}/kg]"))
|
||
.AddHeaderCell(NewTh("[kg]"))
|
||
.AddHeaderCell(NewTh($"[{CurrencySymbol}/kg]"))
|
||
.AddHeaderCell(NewTh($"[{CurrencySymbol}]"));
|
||
|
||
string? lastHdr = null;
|
||
foreach (var row in Data.Rows) {
|
||
var hdr = $"{row.Variety}{(row.Attribute != null ? " / " : "")}{row.Attribute}{(row.Cultivation != null ? " / " : "")}{row.Cultivation}";
|
||
if (lastHdr != hdr) {
|
||
var rows = Data.Rows
|
||
.Where(r => r.Variety == row.Variety && r.Attribute == row.Attribute && r.Cultivation == row.Cultivation)
|
||
.ToList();
|
||
var border = lastHdr != null;
|
||
tbl.AddCell(NewTd(hdr, colspan: 2, bold: true, italic: true, borderTop: border))
|
||
.AddCell(NewTd($"{rows.Sum(r => r.Ungeb.Weight):N0}", right: true, bold: true, borderTop: border))
|
||
.AddCell(NewTd(borderTop: border))
|
||
.AddCell(NewTd($"{rows.Sum(r => r.LowGeb.Weight):N0}", right: true, bold: true, borderTop: border))
|
||
.AddCell(NewTd(borderTop: border))
|
||
.AddCell(NewTd($"{rows.Sum(r => r.Geb.Weight):N0}", right: true, bold: true, borderTop: border))
|
||
.AddCell(NewTd(borderTop: border))
|
||
.AddCell(NewTd($"{rows.Sum(r => r.Amount):N2}", right: true, bold: true, borderTop: border));
|
||
}
|
||
tbl.AddCell(NewTd(row.QualityLevel))
|
||
.AddCell(NewTd($"{row.Oe:N0}", center: true))
|
||
.AddCell(NewTd(row.Ungeb.Weight != 0 ? $"{row.Ungeb.Weight:N0}" : "-", right: true))
|
||
.AddCell(NewTd(row.Ungeb.MaxPrice != null ? $"{row.Ungeb.MaxPrice:N4}" : "-", right: true))
|
||
.AddCell(NewTd(row.LowGeb.Weight != 0 ? $"{row.LowGeb.Weight:N0}" : "-", right: true))
|
||
.AddCell(NewTd(row.LowGeb.MaxPrice != null ? $"{row.LowGeb.MaxPrice:N4}" : "-", right: true))
|
||
.AddCell(NewTd(row.Geb.Weight != 0 ? $"{row.Geb.Weight:N0}" : "-", right: true))
|
||
.AddCell(NewTd(row.Geb.MaxPrice != null ? $"{row.Geb.MaxPrice:N4}" : "-", right: true))
|
||
.AddCell(NewTd($"{row.Amount:N2}", right: true));
|
||
lastHdr = hdr;
|
||
}
|
||
|
||
return tbl;
|
||
}
|
||
}
|
||
}
|