using Elwig.Helpers; using Elwig.Helpers.Billing; using Elwig.Models.Dtos; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace Elwig.Windows { public partial class PaymentAdjustmentWindow : ContextWindow { public readonly int Year; public readonly bool SeasonLocked; public PaymentAdjustmentWindow(int year) { InitializeComponent(); Year = year; using (var ctx = new AppDbContext()) { SeasonLocked = ctx.Seasons.Find(Year + 1) != null; } Title = $"Auszahlung anpassen - Lese {Year} - Elwig"; AutoAdjustBsButton.IsEnabled = !SeasonLocked; UnAdjustBsButton.IsEnabled = !SeasonLocked; AllowanceKgInput.Text = $"{App.Client.AutoAdjustBs.AllowanceKg}"; AllowanceBsInput.Text = $"{App.Client.AutoAdjustBs.AllowanceBs}"; AllowanceKgPerBsInput.Text = $"{App.Client.AutoAdjustBs.AllowanceKgPerBs}"; AllowancePercentInput.Text = $"{App.Client.AutoAdjustBs.AllowancePercent}"; MinBsInput.Text = $"{App.Client.AutoAdjustBs.MinBs}"; } protected override async Task OnRenewContext(AppDbContext ctx) { var members = await ctx.Members .Select(m => new { m.MgNr, m.FamilyName, m.GivenName, m.BusinessShares, m.IsActive, }) .OrderBy(m => m.FamilyName) .ThenBy(m => m.GivenName) .ThenBy(m => m.MgNr) .ToListAsync(); var season = (await ctx.Seasons.FindAsync(Year))!; var contracts = await ctx.AreaCommitmentTypes.ToDictionaryAsync(t => t.VtrgId, t => t); var tbl1 = await OverUnderDeliveryData.ForSeason(ctx.OverUnderDeliveryRows, Year); var tbl2 = await AreaComUnderDeliveryData.ForSeason(ctx.AreaComUnderDeliveryRows, Year); var weight = tbl1.Rows.ToDictionary(r => r.MgNr, r => r.Weight); var areaComs = tbl2.Rows.ToDictionary(r => r.MgNr, r => r.VtrgIds.Zip(r.UnderDeliveries).ToDictionary(r => r.First, r => r.Second)); var history = await ctx.MemberHistory .Where(h => h.DateString.CompareTo($"{Year}-01-01") >= 0 && h.DateString.CompareTo($"{Year}-12-31") <= 0 && h.Type == "auto" && h.BusinessShares > 0) .GroupBy(h => h.Member) .ToDictionaryAsync(h => h.Key.MgNr, h => h.Sum(g => g.BusinessShares)); var list = members .Select(m => new { m.MgNr, m.FamilyName, m.GivenName, m.IsActive, BusinessShares = m.BusinessShares - history.GetValueOrDefault(m.MgNr, 0), DeliveryObligation = (m.BusinessShares - history.GetValueOrDefault(m.MgNr, 0)) * season.MinKgPerBusinessShare, DeliveryRight = (m.BusinessShares - history.GetValueOrDefault(m.MgNr, 0)) * season.MaxKgPerBusinessShare, Adjust = history.TryGetValue(m.MgNr, out int v2) ? (int?)v2 : null, }) .Select(m => new { m.MgNr, m.FamilyName, m.GivenName, m.BusinessShares, Weight = weight.GetValueOrDefault(m.MgNr, 0), OverUnder = weight.TryGetValue(m.MgNr, out int v1) ? (v1 < m.DeliveryObligation ? (int?)v1 - m.DeliveryObligation : v1 > m.DeliveryRight ? (int?)v1 - m.DeliveryRight : null) : (m.IsActive ? -m.DeliveryObligation : null), m.Adjust, AdjustAmount = m.Adjust * -season.BusinessShareValue, }) .Select(m => new { m.MgNr, m.FamilyName, m.GivenName, m.BusinessShares, m.Weight, m.OverUnder, PenaltyBs = m.OverUnder != null && m.OverUnder < 0 ? (season.PenaltyPerKg * m.OverUnder ?? 0) + (-season.PenaltyAmount ?? 0) + (season.PenaltyPerBsAmount * Math.Floor(m.OverUnder / season.MinKgPerBusinessShare ?? 0m) ?? 0) + (m.Weight == 0 ? (-season.PenaltyNone ?? 0) + (-season.PenaltyPerBsNone * m.BusinessShares ?? 0) : 0) : (decimal?)null, PenaltyAc = areaComs.TryGetValue(m.MgNr, out var c) ? c.Select(r => { var con = contracts[r.Key]; return (r.Value.Kg * con.PenaltyPerKg ?? 0) + (r.Value.Kg < 0 ? con.PenaltyAmount ?? 0 : 0) + (r.Value.Percent == -100 ? con.PenaltyNone ?? 0 : 0); }).Sum() : (decimal?)null, m.Adjust, m.AdjustAmount, }) .Select(m => new { m.MgNr, m.FamilyName, m.GivenName, m.BusinessShares, m.Weight, m.OverUnder, PenaltyBs = m.PenaltyBs == null || m.PenaltyBs == 0 ? (decimal?)null : Math.Round((decimal)m.PenaltyBs, 2), PenaltyAc = m.PenaltyAc == null ? (decimal?)null : Math.Round((decimal)m.PenaltyAc, 2), m.Adjust, AdjustAmount = m.AdjustAmount == null ? (decimal?)null : Math.Round((decimal)m.AdjustAmount, 2), }) .Select(m => new { m.MgNr, m.FamilyName, m.GivenName, m.BusinessShares, m.Weight, m.OverUnder, m.PenaltyBs, m.PenaltyAc, m.Adjust, m.AdjustAmount, Total = (m.PenaltyBs ?? 0) + (m.PenaltyAc ?? 0) + (m.AdjustAmount ?? 0), }) .Select(m => new { m.MgNr, m.FamilyName, m.GivenName, m.BusinessShares, m.Weight, m.OverUnder, m.PenaltyBs, m.PenaltyAc, m.Adjust, m.AdjustAmount, m.Total, Background = m.Weight == 0 ? Brushes.Orange : m.Weight / 2 < -m.Total ? Brushes.Red : Brushes.White, Foreground = m.Total == 0 ? Brushes.Gray : Brushes.Black, }) .Where(m => m.OverUnder != null || m.Adjust != null || m.PenaltyBs != null || m.PenaltyAc != null) .OrderByDescending(m => m.OverUnder ?? 0) .ThenBy(m => m.FamilyName) .ThenBy(m => m.GivenName) .ThenBy(m => m.MgNr) .ToList(); MemberList.ItemsSource = list; var sym = season.Currency.Symbol ?? season.Currency.Code; PenaltyBusinessShares.Text = $"{list.Count(r => r.PenaltyBs != null && r.PenaltyBs != 0)} Mg. / {list.Sum(r => r.PenaltyBs):N2} {sym}"; PenaltyAreaCommitments.Text = $"{list.Count(r => r.PenaltyAc != null && r.PenaltyAc != 0)} Mg. / {list.Sum(r => r.PenaltyAc):N2} {sym}"; AutoBusinessShareAdjustment.Text = $"{list.Count(r => r.Adjust > 0)} Mg. / {list.Sum(r => r.Adjust)} GA / {list.Sum(r => r.AdjustAmount):N2} {sym}"; TotalModifiers.Text = $"{list.Count(r => r.Total != 0)} Mg. / {list.Sum(r => r.Total):N2} {sym}"; NonDeliveries.Text = $"{list.Count(r => r.Weight == 0):N0}"; } private async void AutoAdjustBsButton_Click(object sender, RoutedEventArgs evt) { Mouse.OverrideCursor = Cursors.AppStarting; try { int? kg = AllowanceKgInput.Text == "" ? null : int.Parse(AllowanceKgInput.Text); double? bs = AllowanceBsInput.Text == "" ? null : double.Parse(AllowanceBsInput.Text); int? kgPerBs = AllowanceKgPerBsInput.Text == "" ? null : int.Parse(AllowanceKgPerBsInput.Text); double? percent = AllowancePercentInput.Text == "" ? null : double.Parse(AllowancePercentInput.Text); int? minBs = MinBsInput.Text == "" ? null : int.Parse(MinBsInput.Text); App.Client.AutoAdjustBs.AllowanceKg = kg; App.Client.AutoAdjustBs.AllowanceBs = bs; App.Client.AutoAdjustBs.AllowanceKgPerBs = kgPerBs; App.Client.AutoAdjustBs.AllowancePercent = percent; App.Client.AutoAdjustBs.MinBs = minBs; await App.Client.UpdateValues(); var b = new Billing(Year); await b.AutoAdjustBusinessShares(new DateOnly(Year, 11, 30), kg ?? default, bs ?? default, kgPerBs ?? default, percent / 100.0 ?? default, minBs ?? default); await App.HintContextChange(); } catch (Exception exc) { var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message; MessageBox.Show(str, "GA Nachzeichnen", MessageBoxButton.OK, MessageBoxImage.Error); } Mouse.OverrideCursor = null; } private async void UnAdjustBsButton_Click(object sender, RoutedEventArgs evt) { Mouse.OverrideCursor = Cursors.AppStarting; try { var b = new Billing(Year); await b.UnAdjustBusinessShares(); await App.HintContextChange(); } catch (Exception exc) { var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message; MessageBox.Show(str, "GA Nachzeichnen", MessageBoxButton.OK, MessageBoxImage.Error); } Mouse.OverrideCursor = null; } private void SeasonButton_Click(object sender, RoutedEventArgs evt) { App.FocusBaseDataSeason(Year); } private void KgInput_TextChanged(object sender, TextChangedEventArgs evt) { Validator.CheckInteger((TextBox)sender, false, 6); } private void BsInput_TextChanged(object sender, TextChangedEventArgs evt) { Validator.CheckInteger((TextBox)sender, false, 3); } private void PercentInput_TextChanged(object sender, TextChangedEventArgs evt) { Validator.CheckDecimal((TextBox)sender, false, 3, 2); } } }