@using RazorLight
@using Elwig.Helpers
@inherits TemplatePage<Elwig.Documents.PaymentVariantSummary>
@model Elwig.Documents.PaymentVariantSummary
@{ Layout = "Document"; }
<link rel="stylesheet" href="file:///@Raw(Model.DocumentsPath)\PaymentVariantSummary.css" />
<main>
    <h1>Auszahlungsvariante Lese @Model.Variant.Year</h1>
    <h2>@Model.Variant.Name</h2>
    <table class="payment-variant border">
        <colgroup>
            <col style="width: 20.0mm;"/>
            <col style="width: 30.0mm;"/>
            <col style="width:  4.5mm;"/>
            <col style="width: 28.0mm;"/>
            <col style="width: 47.5mm;"/>
            <col style="width: 15.0mm;"/>
            <col style="width: 20.0mm;"/>
        </colgroup>
        @{
            //var sum1 = Model.Variant.DeliveryPartPayments.Sum(p => p.NetAmount);
            //var sum2 = Model.Variant.Credits.Sum(p => p.);  //Model.Variant.MemberPayments.Sum(p => p.Amount);
            var deliveryModifiers = Model.Variant.DeliveryPartPayments.Sum(p => p.Amount - p.NetAmount);
            var memberModifiers = Model.Variant.Credits.Sum(c => c.Payment.Amount - c.Payment.NetAmount);
            var sum2 = Model.Variant.Credits.Sum(p => p.NetAmount);
            var sum1 = sum2 - deliveryModifiers - memberModifiers;
            var payed = -Model.Variant.Credits.Sum(p => p.PrevNetAmount ?? 0m);
            var netSum = Model.Variant.Credits.Sum(p => p.NetAmount) - Model.Variant.Credits.Sum(p => p.PrevNetAmount ?? 0m);
            var vat = Model.Variant.Credits.Sum(p => p.VatAmount);
            var grossSum = Model.Variant.Credits.Sum(p => p.GrossAmount);
            var totalMods = Model.Variant.Credits.Sum(p => p.Modifiers ?? 0m);
            var considered = -Model.Variant.Credits.Sum(p => p.PrevModifiers ?? 0m);
            var totalSum = Model.Variant.Credits.Sum(p => p.Amount);
        }
        <tbody>
            <tr class="sectionheading">
                <th colspan="4">Allgemein</th>
                <th colspan="3" class="lborder">Berücksichtigt</th>
            </tr>
            <tr>
                <th>Name:</th>
                <td colspan="3">@Model.Variant.Name</td>
                <th colspan="2" class="lborder">Zu-/Abschläge bei Lieferungen:</th>
                <td class="center">@(Model.BillingData.ConsiderDelieryModifiers ? "Ja" : "Nein")</td>
            </tr>
            <tr>
                <th>Beschr.:</th>
                <td colspan="3">@Model.Variant.Comment</td>
                <th colspan="2" class="lborder">Pönalen bei Unterlieferungen (FB):</th>
                <td class="center">@(Model.BillingData.ConsiderContractPenalties ? "Ja" : "Nein")</td>
            </tr>
            <tr>
                <th style="overflow: visible;">Rebel-Zuschl.:</th>
                <td colspan="3" class="center">
                    @($"{Utils.GetSign(Model.BillingData.NetWeightModifier)}{Math.Abs(Model.BillingData.NetWeightModifier) * 100:N2}") % /
                    @($"{Utils.GetSign(Model.BillingData.GrossWeightModifier)}{Math.Abs(Model.BillingData.GrossWeightModifier) * 100:N2}") %
                </td>
                <th colspan="2" class="lborder">Strafen bei Unterlieferungen (GA):</th>
                <td class="center">@(Model.BillingData.ConsiderTotalPenalty ? "Ja" : "Nein")</td>
            </tr>
            <tr>
                <th style="overflow: visible;">Datum/Überw.:</th>
                <td colspan="3" class="center">
                    @($"{Model.Variant.Date:dd.MM.yyyy}") /
                    @($"{Model.Variant.TransferDate:dd.MM.yyyy}")
                </td>
                <th colspan="2" class="lborder">Automatische Nachzeichnung der GA:</th>
                <td class="center">@(Model.BillingData.ConsiderAutoBusinessShares ? "Ja" : "Nein")</td>
            </tr>
            <tr>
                <th>Berechnung:</th>
                <td colspan="3" class="center">@($"{Model.Variant.CalcTime:dd.MM.yyyy, HH:mm:ss}")</td>
                <th colspan="2" class="lborder">Benutzerdef. Zu-/Abschläge pro Mitglied:</th>
                <td class="center">@(Model.BillingData.ConsiderCustomModifiers ? "Ja" : "Nein")</td>
            </tr>
            <tr class="sectionheading">
                <th colspan="4">Beträge</th>
                <th colspan="3" class="lborder">Statistik</th>
            </tr>
            <tr>
                <th colspan="2">Zwischensumme:</th>
                <td></td>
                <td class="number"><span class="fleft">@Model.CurrencySymbol</span>@($"{sum1:N2}")</td>
                <th class="lborder">Lieferanten:</th>
                <td colspan="2" class="number">@($"{Model.MemberNum:N0}")</td>
            </tr>
            <tr>
                <th colspan="2">Zu-/Abschläge (Mitglieder):</th>
                <td class="number">@Utils.GetSign(memberModifiers)</td>
                <td class="number"><span class="fleft">@Model.CurrencySymbol</span>@($"{Math.Abs(memberModifiers):N2}")</td>
                <th class="lborder">Lieferungen:</th>
                <td colspan="2" class="number">@($"{Model.DeliveryNum:N0}")</td>
            </tr>
            <tr>
                <th colspan="2">Zu-/Abschläge (Lieferungen):</th>
                <td class="number">@Utils.GetSign(deliveryModifiers)</td>
                <td class="number"><span class="fleft">@Model.CurrencySymbol</span>@($"{Math.Abs(deliveryModifiers):N2}")</td>
                <th class="lborder">Teillieferungen:</th>
                <td colspan="2" class="number">@($"{Model.DeliveryPartNum:N0}")</td>
            </tr>
            <tr>
                <th colspan="2">Gesamtsumme:</th>
                <td class="number tborder"></td>
                <td class="number tborder"><span class="fleft">@Model.CurrencySymbol</span>@($"{sum2:N2}")</td>
                <th class="lborder"></th>
                <td colspan="2"></td>
            </tr>
            <tr>
                <th colspan="2">Bisher ausgezahlt:</th>
                <td class="number">@Utils.GetSign(payed)</td>
                <td class="number"><span class="fleft">@Model.CurrencySymbol</span>@($"{Math.Abs(payed):N2}")</td>
                @{
                    var weiRows = Model.Data.Rows.Where(r => r.QualityLevel == "Wein");
                    var minWei = weiRows.Min(r => r.Ungeb.MinPrice);
                    var maxWei = weiRows.Max(r => r.Ungeb.MaxPrice);
                }
                <th class="lborder tborder">Preis (abgewertet):</th>
                <td colspan="2" class="center tborder">@(minWei != maxWei ? $"{minWei:N4}–{maxWei:N4}" : $"{minWei:N4}") @Model.CurrencySymbol/kg</td>
            </tr>
            <tr>
                <th colspan="2">Nettosumme:</th>
                <td class="number tborder"></td>
                <td class="number tborder"><span class="fleft">@Model.CurrencySymbol</span>@($"{netSum:N2}")</td>
                @{
                    var quwRows = Model.Data.Rows.Where(r => r.QualityLevel != "Wein");
                    var minPrice = quwRows.Min(r => r.Ungeb.MinPrice);
                    var maxPrice = quwRows.Max(r => r.Ungeb.MaxPrice);
                }
                <th class="lborder">Preis (ungeb., nicht abgew.):</th>
                <td colspan="2" class="center">@(minPrice != maxPrice ? $"{minPrice:N4}–{maxPrice:N4}" : $"{minPrice:N4}") @Model.CurrencySymbol/kg</td>
            </tr>
            <tr>
                <th colspan="2">Mehrwertsteuer:</th>
                <td class="number">@Utils.GetSign(vat)</td>
                <td class="number"><span class="fleft">@Model.CurrencySymbol</span>@($"{Math.Abs(vat):N2}")</td>
                @{
                    var gebRows = Model.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();
                }
                <th class="lborder">Gebunden-Zuschlag:</th>
                <td colspan="2" class="center">
                    @(minGeb != maxGeb ? $"{minGeb:N4}–{maxGeb:N4} {Model.CurrencySymbol}/kg" : minGeb == 0 ? "-" : $"{minGeb:N4} {Model.CurrencySymbol}/kg")
                </td>
            </tr>
            <tr>
                <th colspan="2">Bruttosumme:</th>
                <td class="number tborder"></td>
                <td class="number tborder"><span class="fleft">@Model.CurrencySymbol</span>@($"{grossSum:N2}")</td>
                <th class="lborder"></th>
                <td colspan="2"></td>
            </tr>
            <tr>
                <th colspan="2">Abzüge (Strafen/Pönalen, GA, ...):</th>
                <td class="number">@Utils.GetSign(totalMods)</td>
                <td class="number"><span class="fleft">@Model.CurrencySymbol</span>@($"{Math.Abs(totalMods):N2}")</td>
                <th class="lborder tborder">Menge (ungebunden):</th>
                <td colspan="2" class="number tborder">@($"{Model.Data.Rows.Sum(r => r.Ungeb.Weight):N0}") kg</td>
            </tr>
            <tr>
                <th colspan="2">Bereits berücksichtigte Abzüge:</th>
                <td class="number">@Utils.GetSign(considered)</td>
                <td class="number"><span class="fleft">@Model.CurrencySymbol</span>@($"{Math.Abs(considered):N2}")</td>
                <th class="lborder">Menge (gebunden):</th>
                <td colspan="2" class="number">@($"{Model.Data.Rows.Sum(r => r.Geb.Weight):N0}") kg</td>
            </tr>
            <tr>
                <th colspan="2">Auszahlungsbetrag:</th>
                <td class="number tborder"></td>
                <td class="number tborder"><span class="fleft">@Model.CurrencySymbol</span>@($"{totalSum:N2}")</td>
                <th class="lborder">Gesamtmenge:</th>
                <td colspan="2" class="number tborder">@($"{Model.Data.Rows.Sum(r => r.Ungeb.Weight + r.Geb.Weight):N0}") kg</td>
            </tr>
        </tbody>
    </table>
    <table class="payment-variant border">
        <colgroup>
            <col style="width: 35mm;"/>
            <col style="width: 30mm;"/>
            <col style="width: 25mm;"/>
            <col style="width: 25mm;"/>
            <col style="width: 25mm;"/>
            <col style="width: 25mm;"/>
        </colgroup>
        <thead>
            <tr class="sectionheading">
                <th colspan="6">Statistik Zu-/Abschläge</th>
            </tr>
            <tr>
                <th rowspan="2">Name</th>
                <th rowspan="2">Zu-/Abschlag</th>
                <th>Lieferungen</th>
                <th>Minimum</th>
                <th>Maximum</th>
                <th>Betrag</th>
            </tr>
            <tr>
                <th>[#]</th>
                <th>[@Model.CurrencySymbol]</th>
                <th>[@Model.CurrencySymbol]</th>
                <th>[@Model.CurrencySymbol]</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var m in Model.ModifierStat) {
                var mod = Model.Modifiers[m.ModId];
                <tr>
                    <th>@mod.Name</th>
                    <td class="number">@mod.ValueStr</td>
                    <td class="number">@($"{m.Count:N0}")</td>
                    <td class="number">@($"{m.Min:N2}")</td>
                    <td class="number">@($"{m.Max:N2}")</td>
                    <td class="number">@($"{m.Sum:N2}")</td>
                </tr>
            }
        </tbody>
    </table>
    <table class="payment-variant-data">
        <colgroup>
            <col style="width: 25mm;"/>
            <col style="width: 19mm;"/>
            <col style="width: 18mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 18mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 18mm;"/>
            <col style="width: 15mm;"/>
            <col style="width: 22mm;"/>
        </colgroup>
        <thead>
            <tr>
                <th rowspan="2" style="text-align: left;">Qualitätsstufe</th>
                <th>Gradation</th>
                <th colspan="2">ungebunden</th>
                <th colspan="2">attributlos gebunden</th>
                <th colspan="2">gebunden</th>
                <th>Gesamt</th>
            </tr>
            <tr>
                <th>[@(true ? "°Oe" : "°KMW")]</th>
                <th>[kg]</th>
                <th>[@(Model.CurrencySymbol)/kg]</th>
                <th>[kg]</th>
                <th>[@(Model.CurrencySymbol)/kg]</th>
                <th>[kg]</th>
                <th>[@(Model.CurrencySymbol)/kg]</th>
                <th>[@(Model.CurrencySymbol)]</th>
            </tr>
        </thead>
        <tbody>
            @{
                string? lastHdr = null;
            }
            @foreach (var row in Model.Data.Rows) {
                var hdr = $"{row.Variety}{(row.Attribute != null ? " / " : "")}{row.Attribute}{(row.Cultivation != null ? " / " : "")}{row.Cultivation}";
                if (lastHdr != hdr) {
                    var rows = Model.Data.Rows
                        .Where(r => r.Variety == row.Variety && r.Attribute == row.Attribute && r.Cultivation == row.Cultivation)
                        .ToList();
                    <tr class="subheading @(lastHdr != null ? "new" : "")">
                        <th colspan="2">@hdr</th>
                        <td class="number">@($"{rows.Sum(r => r.Ungeb.Weight):N0}")</td>
                        <td></td>
                        <td class="number">@($"{rows.Sum(r => r.LowGeb.Weight):N0}")</td>
                        <td></td>
                        <td class="number">@($"{rows.Sum(r => r.Geb.Weight):N0}")</td>
                        <td></td>
                        <td class="number">@($"{rows.Sum(r => r.Amount):N2}")</td>
                    </tr>
                }
                <tr>
                    <td>@(row.QualityLevel)</td>
                    <td class="center">@($"{row.Oe:N0}")</td>
                    <td class="number">@(row.Ungeb.Weight != 0 ? $"{row.Ungeb.Weight:N0}" : "-")</td>
                    <td class="number">@(row.Ungeb.MaxPrice != null ? $"{row.Ungeb.MaxPrice:N4}" : "-")</td>
                    <td class="number">@(row.LowGeb.Weight != 0 ? $"{row.LowGeb.Weight:N0}" : "-")</td>
                    <td class="number">@(row.LowGeb.MaxPrice != null ? $"{row.LowGeb.MaxPrice:N4}" : "-")</td>
                    <td class="number">@(row.Geb.Weight != 0 ? $"{row.Geb.Weight:N0}" : "-")</td>
                    <td class="number">@(row.Geb.MaxPrice != null ? $"{row.Geb.MaxPrice:N4}" : "-")</td>
                    <td class="number">@($"{row.Amount:N2}")</td>
                </tr>
                lastHdr = hdr;
            }
        </tbody>
    </table>
</main>
