[#46] Windows: Add PaymentAdjustmentWindow
All checks were successful
Test / Run tests (push) Successful in 2m13s

This commit is contained in:
2024-06-17 01:41:32 +02:00
parent 9d9f929843
commit 5c46a00752
5 changed files with 238 additions and 23 deletions

View File

@ -296,6 +296,10 @@ namespace Elwig {
return FocusWindow<PaymentVariantsWindow>(() => new(year), w => w.Year == year);
}
public static PaymentAdjustmentWindow FocusPaymentAdjustment(int year) {
return FocusWindow<PaymentAdjustmentWindow>(() => new(year), w => w.Year == year);
}
public static ChartWindow FocusChartWindow(int year, int avnr) {
return FocusWindow<ChartWindow>(() => new(year, avnr), w => w.Year == year && w.AvNr == avnr);
}

View File

@ -3,6 +3,7 @@ using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
@ -33,15 +34,44 @@ namespace Elwig.Helpers.Billing {
""");
}
public async Task AutoAdjustBusinessShare() {
public async Task AutoAdjustBusinessShares(DateOnly date, int allowanceKg = 0, double allowanceBs = 0, int allowanceKgPerBs = 0, double allowanceRel = 0, int addMinBs = 1) {
if (addMinBs < 1) addMinBs = 1;
using var cnx = await AppDbContext.ConnectAsync();
await AppDbContext.ExecuteBatch(cnx, $"""
INSERT INTO member_history (mgnr, date, business_shares, type)
SELECT u.mgnr, '{Utils.Today:yyyy-MM-dd}', u.diff / s.max_kg_per_bs AS bs, 'auto'
UPDATE member
SET business_shares = member.business_shares - h.business_shares
FROM member_history h
WHERE h.date = '{Year}-11-30' AND h.type = 'auto' AND h.mgnr = member.mgnr;
INSERT INTO member_history (mgnr, date, type, business_shares)
SELECT u.mgnr,
'{date:yyyy-MM-dd}',
'auto',
CEIL((u.diff - {allowanceKg}.0 - {allowanceKgPerBs}.0 * u.business_shares) / s.max_kg_per_bs
- {allowanceBs.ToString(CultureInfo.InvariantCulture)}
- {allowanceRel.ToString(CultureInfo.InvariantCulture)} * u.business_shares) AS bs
FROM v_total_under_delivery u
JOIN season s ON s.year = u.year
WHERE s.year = {Year} AND bs > 0
ON CONFLICT DO NOTHING
WHERE s.year = {Year} AND bs >= {addMinBs}
ON CONFLICT DO UPDATE
SET business_shares = excluded.business_shares;
UPDATE member
SET business_shares = member.business_shares + h.business_shares
FROM member_history h
WHERE h.date = '{Year}-11-30' AND h.type = 'auto' AND h.mgnr = member.mgnr;
""");
}
public async Task UnAdjustBusinessShares() {
using var cnx = await AppDbContext.ConnectAsync();
await AppDbContext.ExecuteBatch(cnx, $"""
UPDATE member
SET business_shares = member.business_shares - h.business_shares
FROM member_history h
WHERE h.date = '{Year}-11-30' AND h.type = 'auto' AND h.mgnr = member.mgnr;
DELETE FROM member_history WHERE date = '{Year}-11-30' AND type = 'auto';
""");
}

View File

@ -0,0 +1,105 @@
<local:ContextWindow
x:Class="Elwig.Windows.PaymentAdjustmentWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Elwig.Windows"
xmlns:ctrl="clr-namespace:Elwig.Controls"
Title="Auszahlung anpassen - Elwig" Height="500" Width="800" MinHeight="400" MinWidth="700">
<Window.Resources>
<Style TargetType="Label">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Padding" Value="2,4,2,4"/>
<Setter Property="Height" Value="25"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Height" Value="25"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
</Style>
<Style TargetType="ctrl:UnitTextBox">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="Height" Value="25"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
</Style>
<Style TargetType="Button">
<Setter Property="FontSize" Value="14"/>
<Setter Property="Padding" Value="9,3"/>
<Setter Property="Height" Value="27"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="19"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="24"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="2.5*"/>
</Grid.ColumnDefinitions>
<Menu Grid.ColumnSpan="2" BorderThickness="0,0,0,1" BorderBrush="LightGray" Background="White">
</Menu>
<Grid Grid.Row="1">
<ListBox x:Name="MemberList" Margin="10,10,10,10">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding MgNr}" Width="30" TextAlignment="Right" Margin="0,0,10,0"/>
<TextBlock Text="{Binding FamilyName}" Width="100"/>
<TextBlock Text="{Binding GivenName}" Width="60"/>
<TextBlock Text="{Binding BusinessShares}" Width="40" TextAlignment="Right"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<Grid Grid.Column="1" Grid.Row="1">
<GroupBox Header="Automatische Nachzeichnung der Geschäftsanteile" Margin="10,10,10,10" Height="180" Width="360"
VerticalAlignment="Top" HorizontalAlignment="Left">
<Grid>
<Label Content="Absoluter Freibetrag:" Margin="10,10,10,10"/>
<ctrl:UnitTextBox x:Name="AllowanceKgInput" Unit="kg" Margin="140,10,10,10" Width="70"
TextChanged="KgInput_TextChanged"/>
<ctrl:UnitTextBox x:Name="AllowanceBsInput" Unit="GA" Margin="215,10,10,10" Width="60"
TextChanged="PercentInput_TextChanged"/>
<Label Content="Relativer Freibetrag:" Margin="10,40,10,10"/>
<ctrl:UnitTextBox x:Name="AllowanceKgPerBsInput" Unit="kg/GA" Margin="140,40,10,10" Width="87"
TextChanged="KgInput_TextChanged"/>
<ctrl:UnitTextBox x:Name="AllowancePercentInput" Unit="%" Margin="232,40,10,10" Width="60"
TextChanged="PercentInput_TextChanged"/>
<Label Content="Nur mind. nachz.:" Margin="10,70,10,10"/>
<ctrl:UnitTextBox x:Name="MinBsInput" Unit="GA" Margin="140,70,10,10" Width="50"
TextChanged="BsInput_TextChanged"/>
<Button x:Name="SeasonButton" Content="GA-Wert" Margin="10,10,10,40" Width="120"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Click="SeasonButton_Click"/>
<Button x:Name="AutoAdjustBsButton" Content="Nachzeichnen" Margin="10,10,135,10" Width="120"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Click="AutoAdjustBsButton_Click"/>
<Button x:Name="UnAdjustBsButton" Content="Rückgängig" Margin="10,10,10,10" Width="120"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Click="UnAdjustBsButton_Click"/>
</Grid>
</GroupBox>
</Grid>
<StatusBar Grid.Row="2" Grid.ColumnSpan="2" BorderThickness="0,1,0,0" BorderBrush="Gray">
</StatusBar>
</Grid>
</local:ContextWindow>

View File

@ -0,0 +1,92 @@
using Elwig.Helpers;
using Elwig.Helpers.Billing;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
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";
}
protected override async Task OnRenewContext(AppDbContext ctx) {
MemberList.ItemsSource = 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)
.Select(h => new {
h.Key.MgNr,
h.Key.FamilyName,
h.Key.GivenName,
BusinessShares = h.Sum(g => g.BusinessShares),
})
.OrderBy(h => h.FamilyName)
.ThenBy(h => h.GivenName)
.ThenBy(h => h.MgNr)
.ToListAsync();
}
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);
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);
}
}
}

View File

@ -301,26 +301,10 @@ namespace Elwig.Windows {
App.FocusChartWindow(v.Year, v.AvNr);
}
private async void PaymentAdjustmentButton_Click(object sender, RoutedEventArgs evt) {
if (false && App.Client.IsMatzen) {
PaymentAdjustmentButton.IsEnabled = false;
Mouse.OverrideCursor = Cursors.AppStarting;
var b = new Billing(Year);
await b.AutoAdjustBusinessShare();
Mouse.OverrideCursor = null;
PaymentAdjustmentButton.IsEnabled = true;
} else {
MessageBox.Show(
"Es ist kein automatisches Nachzeichnen der Geschäftsanteile\n" +
"für diese Genossenschaft eingestellt!\n" +
"Bitte wenden Sie sich an die Programmierer!", "Fehler",
MessageBoxButton.OK, MessageBoxImage.Information);
}
private void PaymentAdjustmentButton_Click(object sender, RoutedEventArgs evt) {
App.FocusPaymentAdjustment(Year);
}
private async void MailButton_Click(object sender, RoutedEventArgs evt) {
if (PaymentVariantList.SelectedItem is not PaymentVar pv)
return;