| 
							
							
							
						 |  |  | @@ -1,18 +1,18 @@ | 
		
	
		
			
				|  |  |  |  | using Elwig.Documents; | 
		
	
		
			
				|  |  |  |  | using Elwig.Helpers; | 
		
	
		
			
				|  |  |  |  | using Elwig.Helpers.Billing; | 
		
	
		
			
				|  |  |  |  | using Elwig.Helpers.Export; | 
		
	
		
			
				|  |  |  |  | using Elwig.Models.Dtos; | 
		
	
		
			
				|  |  |  |  | using Elwig.Models.Entities; | 
		
	
		
			
				|  |  |  |  | using Microsoft.EntityFrameworkCore; | 
		
	
		
			
				|  |  |  |  | using Microsoft.Win32; | 
		
	
		
			
				|  |  |  |  | using System; | 
		
	
		
			
				|  |  |  |  | using System.Linq; | 
		
	
		
			
				|  |  |  |  | using System.Text.Json; | 
		
	
		
			
				|  |  |  |  | using System.Threading.Tasks; | 
		
	
		
			
				|  |  |  |  | using System.Windows; | 
		
	
		
			
				|  |  |  |  | using System.Windows.Controls; | 
		
	
		
			
				|  |  |  |  | using System.Windows.Input; | 
		
	
		
			
				|  |  |  |  | using Elwig.Helpers.Billing; | 
		
	
		
			
				|  |  |  |  | using Elwig.Helpers.Export; | 
		
	
		
			
				|  |  |  |  | using Microsoft.Win32; | 
		
	
		
			
				|  |  |  |  | using System.Text.Json; | 
		
	
		
			
				|  |  |  |  | using Elwig.Documents; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | namespace Elwig.Windows { | 
		
	
		
			
				|  |  |  |  |     public partial class PaymentVariantsWindow : ContextWindow { | 
		
	
	
		
			
				
					
					|  |  |  | @@ -123,6 +123,15 @@ namespace Elwig.Windows { | 
		
	
		
			
				|  |  |  |  |                 ConsiderAutoInput.IsEnabled = !locked; | 
		
	
		
			
				|  |  |  |  |                 ConsiderCustomInput.IsEnabled = !locked; | 
		
	
		
			
				|  |  |  |  |                 DataInput.IsReadOnly = locked; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |                 ModifierSum.Text = "..."; | 
		
	
		
			
				|  |  |  |  |                 TotalSum.Text = "..."; | 
		
	
		
			
				|  |  |  |  |                 VatSum.Text = "..."; | 
		
	
		
			
				|  |  |  |  |                 DeductionSum.Text = "..."; | 
		
	
		
			
				|  |  |  |  |                 PaymentSum.Text = "..."; | 
		
	
		
			
				|  |  |  |  |                 Utils.RunBackground("Variantendaten laden", async () => { | 
		
	
		
			
				|  |  |  |  |                     await UpdateSums(v); | 
		
	
		
			
				|  |  |  |  |                 }); | 
		
	
		
			
				|  |  |  |  |             } else { | 
		
	
		
			
				|  |  |  |  |                 EditButton.Content = "Bearbeiten"; | 
		
	
		
			
				|  |  |  |  |                 EditButton.IsEnabled = false; | 
		
	
	
		
			
				
					
					|  |  |  | @@ -167,8 +176,14 @@ namespace Elwig.Windows { | 
		
	
		
			
				|  |  |  |  |                 ConsiderCustomInput.IsEnabled = false; | 
		
	
		
			
				|  |  |  |  |                 DataInput.Text = ""; | 
		
	
		
			
				|  |  |  |  |                 DataInput.IsReadOnly = true; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |                 ModifierSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 TotalSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 VatSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 DeductionSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 PaymentSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |             await UpdateSums(); | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |             UpdateSaveButton(); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
	
		
			
				
					
					|  |  |  | @@ -187,37 +202,80 @@ namespace Elwig.Windows { | 
		
	
		
			
				|  |  |  |  |             CommitButton.IsEnabled = CalculateButton.IsEnabled; | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |         private async Task UpdateSums() { | 
		
	
		
			
				|  |  |  |  |             if (PaymentVariantList.SelectedItem is PaymentVar v) { | 
		
	
		
			
				|  |  |  |  |         private async Task UpdateSums(PaymentVar v) { | 
		
	
		
			
				|  |  |  |  |             if (App.MainDispatcher == null) | 
		
	
		
			
				|  |  |  |  |                 return; | 
		
	
		
			
				|  |  |  |  |             var modText = "-"; | 
		
	
		
			
				|  |  |  |  |             var totalText = "-"; | 
		
	
		
			
				|  |  |  |  |             var vatText = "-"; | 
		
	
		
			
				|  |  |  |  |             var deductionText = "-"; | 
		
	
		
			
				|  |  |  |  |             var paymentText = "-"; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |             using var ctx = new AppDbContext(); | 
		
	
		
			
				|  |  |  |  |             var sym = v.Season.Currency.Symbol ?? v.Season.Currency.Code; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |             var modSum = await ctx.PaymentDeliveryParts | 
		
	
		
			
				|  |  |  |  |                 .Where(p => p.Year == v.Year && p.AvNr == v.AvNr) | 
		
	
		
			
				|  |  |  |  |                 .SumAsync(p => p.AmountValue - p.NetAmountValue); | 
		
	
		
			
				|  |  |  |  |                 ModifierSum.Text = $"{v.Season.DecFromDb(modSum):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |             modText = $"{v.Season.DecFromDb(modSum):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |             var totalSum = await ctx.MemberPayments | 
		
	
		
			
				|  |  |  |  |                 .Where(p => p.Year == v.Year && p.AvNr == v.AvNr) | 
		
	
		
			
				|  |  |  |  |                 .SumAsync(p => p.AmountValue); | 
		
	
		
			
				|  |  |  |  |                 TotalSum.Text = $"{v.Season.DecFromDb(totalSum):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |             totalText = $"{v.Season.DecFromDb(totalSum):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |             await App.MainDispatcher.BeginInvoke(() => { | 
		
	
		
			
				|  |  |  |  |                 if (PaymentVariantList.SelectedItem != v) | 
		
	
		
			
				|  |  |  |  |                     return; | 
		
	
		
			
				|  |  |  |  |                 ModifierSum.Text = modText; | 
		
	
		
			
				|  |  |  |  |                 TotalSum.Text = totalText; | 
		
	
		
			
				|  |  |  |  |             }); | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |             var credits = ctx.Credits.Where(c => c.Year == v.Year && c.AvNr == v.AvNr); | 
		
	
		
			
				|  |  |  |  |             if (!credits.Any()) { | 
		
	
		
			
				|  |  |  |  |                     VatSum.Text = $"- {sym}"; | 
		
	
		
			
				|  |  |  |  |                     DeductionSum.Text = $"- {sym}"; | 
		
	
		
			
				|  |  |  |  |                     PaymentSum.Text = $"- {sym}"; | 
		
	
		
			
				|  |  |  |  |                 long lastTotalSum = 0; | 
		
	
		
			
				|  |  |  |  |                 decimal vatSum = 0; | 
		
	
		
			
				|  |  |  |  |                 var currentPayments = await ctx.MemberPayments | 
		
	
		
			
				|  |  |  |  |                         .Where(p => p.Year == v.Year && p.AvNr == v.AvNr) | 
		
	
		
			
				|  |  |  |  |                         .ToDictionaryAsync(p => p.MgNr); | 
		
	
		
			
				|  |  |  |  |                 var lastV = await ctx.PaymentVariants | 
		
	
		
			
				|  |  |  |  |                     .Where(l => l.Year == v.Year && !l.TestVariant) | 
		
	
		
			
				|  |  |  |  |                     .OrderByDescending(l => l.TransferDateString ?? l.DateString) | 
		
	
		
			
				|  |  |  |  |                     .ThenByDescending(l => l.AvNr) | 
		
	
		
			
				|  |  |  |  |                     .FirstOrDefaultAsync(); | 
		
	
		
			
				|  |  |  |  |                 if (lastV != null) { | 
		
	
		
			
				|  |  |  |  |                     var lastPayments = await ctx.MemberPayments | 
		
	
		
			
				|  |  |  |  |                         .Where(p => p.Year == v.Year && p.AvNr == lastV.AvNr) | 
		
	
		
			
				|  |  |  |  |                         .ToDictionaryAsync(p => p.MgNr); | 
		
	
		
			
				|  |  |  |  |                     lastTotalSum = lastPayments.Sum(e => e.Value.AmountValue); | 
		
	
		
			
				|  |  |  |  |                     foreach (int mgnr in currentPayments.Keys) { | 
		
	
		
			
				|  |  |  |  |                         var c = currentPayments[mgnr]; | 
		
	
		
			
				|  |  |  |  |                         var l = lastPayments[mgnr]; | 
		
	
		
			
				|  |  |  |  |                         vatSum += (c.Amount - l.Amount) * (c.Member.IsBuchführend ? 0.1m : 0.13m); | 
		
	
		
			
				|  |  |  |  |                     } | 
		
	
		
			
				|  |  |  |  |                 } else { | 
		
	
		
			
				|  |  |  |  |                     vatSum = currentPayments.Sum(e => e.Value.Amount * (e.Value.Member.IsBuchführend ? 0.1m : 0.13m)); | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 vatText = $"~{vatSum:N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                 deductionText = $"- {sym}"; | 
		
	
		
			
				|  |  |  |  |                 paymentText = $"~{v.Season.DecFromDb(totalSum - lastTotalSum) + vatSum:N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |             } else { | 
		
	
		
			
				|  |  |  |  |                 // all values in the credit table are stored with precision 2! | 
		
	
		
			
				|  |  |  |  |                     TotalSum.Text = $"{Utils.DecFromDb(await credits.SumAsync(c => c.NetAmountValue), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                     VatSum.Text = $"{Utils.DecFromDb(await credits.SumAsync(c => c.VatAmountValue), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                     DeductionSum.Text = $"{-Utils.DecFromDb(await credits.SumAsync(c => c.ModifiersValue ?? 0), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                     PaymentSum.Text = $"{Utils.DecFromDb(await credits.SumAsync(c => c.AmountValue), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |             } else { | 
		
	
		
			
				|  |  |  |  |                 ModifierSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 TotalSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 VatSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 DeductionSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 PaymentSum.Text = "-"; | 
		
	
		
			
				|  |  |  |  |                 totalText = $"{Utils.DecFromDb(await credits.SumAsync(c => c.NetAmountValue), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                 vatText = $"{Utils.DecFromDb(await credits.SumAsync(c => c.VatAmountValue), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                 deductionText = $"{-Utils.DecFromDb(await credits.SumAsync(c => c.ModifiersValue ?? 0), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |                 paymentText = $"{Utils.DecFromDb(await credits.SumAsync(c => c.AmountValue), 2):N2} {sym}"; | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |             await App.MainDispatcher.BeginInvoke(() => { | 
		
	
		
			
				|  |  |  |  |                 if (PaymentVariantList.SelectedItem != v) | 
		
	
		
			
				|  |  |  |  |                     return; | 
		
	
		
			
				|  |  |  |  |                 ModifierSum.Text = modText; | 
		
	
		
			
				|  |  |  |  |                 TotalSum.Text = totalText; | 
		
	
		
			
				|  |  |  |  |                 VatSum.Text = vatText; | 
		
	
		
			
				|  |  |  |  |                 DeductionSum.Text = deductionText; | 
		
	
		
			
				|  |  |  |  |                 PaymentSum.Text = paymentText; | 
		
	
		
			
				|  |  |  |  |             }); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |         private async void PaymentVariantList_SelectionChanged(object sender, SelectionChangedEventArgs evt) { | 
		
	
	
		
			
				
					
					|  |  |  |   |