[#44] BaseDataWindow: Add button to add/delete seasons
All checks were successful
Test / Run tests (push) Successful in 2m1s

This commit is contained in:
2024-06-10 20:43:38 +02:00
parent a06921d4ec
commit 3460b9378c
6 changed files with 197 additions and 4 deletions

View File

@ -0,0 +1,68 @@
<Window x:Class="Elwig.Dialogs.NewSeasonDialog"
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.Dialogs"
ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True"
WindowStartupLocation="CenterOwner"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="Saison anlegen" Height="180" Width="400">
<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="ComboBox">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Height" Value="25"/>
</Style>
<Style TargetType="CheckBox">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
</Style>
<Style TargetType="Button">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="25"/>
</Style>
</Window.Resources>
<Grid>
<Label Content="Währung:" Margin="10,10,10,10"/>
<ComboBox x:Name="CurrencyInput" Width="150" Margin="130,10,10,10"
ItemsSource="{Binding Currencies}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Code}" Width="30"/>
<TextBlock Text="- "/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label Content="Nachkommastellen:" Margin="10,40,10,10"/>
<ComboBox x:Name="PrecisionInput" Width="50" Margin="130,40,10,10">
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem IsSelected="True">4</ComboBoxItem>
<ComboBoxItem>5</ComboBoxItem>
<ComboBoxItem>6</ComboBoxItem>
<ComboBoxItem>7</ComboBoxItem>
<ComboBoxItem>8</ComboBoxItem>
</ComboBox>
<CheckBox x:Name="CopyModifiersInput" Content="Zu-/Abschläge der letzten Saison übernehmen"
Margin="15,75,10,10" IsChecked="{Binding CopyModifiers}"/>
<Button x:Name="ConfirmButton" Content="Bestätigen" Margin="10,10,115,10" Grid.Column="1" IsDefault="True"
Click="ConfirmButton_Click"/>
<Button x:Name="CancelButton" Content="Abbrechen" Margin="10,10,10,10" Grid.Column="1" IsCancel="True"/>
</Grid>
</Window>

View File

@ -0,0 +1,30 @@
using Elwig.Helpers;
using Elwig.Models.Entities;
using System.Collections.Generic;
using System.Windows;
namespace Elwig.Dialogs {
public partial class NewSeasonDialog : Window {
public IEnumerable<Currency> Currencies { get; set; }
public int Year { get; set; }
public string CurrencyCode => (CurrencyInput.SelectedItem as Currency)!.Code;
public byte Precision => (byte)(PrecisionInput.SelectedIndex + 2);
public bool CopyModifiers { get; set; }
public NewSeasonDialog(Season? s, IEnumerable<Currency> currencies) {
Currencies = currencies;
CopyModifiers = s != null;
InitializeComponent();
CopyModifiersInput.IsEnabled = s != null;
ControlUtils.SelectItemWithPk(CurrencyInput, s?.CurrencyCode ?? "EUR");
PrecisionInput.SelectedIndex = (s?.Precision ?? 4) - 2;
}
private void ConfirmButton_Click(object sender, RoutedEventArgs evt) {
DialogResult = true;
Close();
}
}
}

View File

@ -399,7 +399,7 @@
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListBox x:Name="SeasonList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,10,10" Grid.RowSpan="2"
<ListBox x:Name="SeasonList" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,10,40" Grid.RowSpan="2"
SelectionChanged="SeasonList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
@ -411,6 +411,12 @@
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="SeasonAddButton" Content="Neu anlegen..." FontSize="12" Height="25" Grid.Row="2"
VerticalAlignment="Bottom" Margin="10,10,40,10" Padding="0,0,0,0"
Click="SeasonAddButton_Click"/>
<Button x:Name="SeasonRemoveButton" Content="&#xE74D;" FontFamily="Segoe MDL2 Assets" FontSize="16" Grid.Row="2" Height="25" Width="25"
VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="10,10,10,10" Padding="0.5,0,0,0"
Click="SeasonRemoveButton_Click"/>
<Grid Grid.Column="1" Margin="0,10,0,0">
<Grid.ColumnDefinitions>

View File

@ -1,10 +1,14 @@
using Elwig.Helpers;
using Elwig.Dialogs;
using Elwig.Helpers;
using Elwig.Models.Entities;
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;
namespace Elwig.Windows {
public partial class BaseDataWindow {
@ -13,17 +17,23 @@ namespace Elwig.Windows {
private bool _seasonUpdate = false;
private async Task SeasonsInitEditing(AppDbContext ctx) {
SeasonAddButton.IsEnabled = false;
SeasonRemoveButton.IsEnabled = false;
ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons
.OrderByDescending(s => s.Year)
.Include(s => s.Modifiers)
.Include(s => s.Currency)
.ToListAsync());
SeasonList_SelectionChanged(null, null);
}
private async Task SeasonsFinishEditing(AppDbContext ctx) {
SeasonAddButton.IsEnabled = true;
SeasonRemoveButton.IsEnabled = true;
ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons
.OrderByDescending(s => s.Year)
.Include(s => s.Modifiers)
.Include(s => s.Currency)
.ToListAsync());
_seasonChanged = false;
}
@ -133,5 +143,84 @@ namespace Elwig.Windows {
InputTextChanged((TextBox)sender, Validator.CheckDecimal((TextBox)sender, false, 4, s.Precision));
Season_Changed(sender, evt);
}
private async void SeasonAddButton_Click(object sender, RoutedEventArgs evt) {
var s = SeasonList.ItemsSource.Cast<Season>().FirstOrDefault();
var year = Utils.CurrentYear;
if (year == s?.Year) year++;
List<Currency> currencies;
using (var ctx = new AppDbContext()) {
currencies = await ctx.Currencies
.OrderBy(c => c.Code)
.ToListAsync();
}
var d = new NewSeasonDialog(s, currencies);
if (d.ShowDialog() == true) {
Mouse.OverrideCursor = Cursors.AppStarting;
try {
using var ctx = new AppDbContext();
ctx.Add(new Season {
Year = year,
CurrencyCode = d.CurrencyCode,
Precision = d.Precision,
MaxKgPerHa = s?.MaxKgPerHa ?? 10000,
VatNormal = s?.VatNormal ?? 0.10,
VatFlatrate = s?.VatFlatrate ?? 0.13,
MinKgPerBusinessShare = s?.MinKgPerBusinessShare ?? 500,
MaxKgPerBusinessShare = s?.MaxKgPerBusinessShare ?? 1000,
PenaltyPerKgValue = s?.PenaltyPerKgValue,
PenaltyAmoutValue = s?.PenaltyAmoutValue,
PenaltyNoneValue = s?.PenaltyNoneValue,
PenaltyPerBsAmountValue = s?.PenaltyPerBsAmountValue,
PenaltyPerBsNoneValue = s?.PenaltyPerBsNoneValue,
BusinessShareValueValue = s?.BusinessShareValueValue,
CalcMode = s?.CalcMode ?? 0,
});
if (s != null && d.CopyModifiers) {
ctx.AddRange(s.Modifiers.Select(m => new Modifier {
Year = year,
ModId = m.ModId,
Ordering = m.Ordering,
Name = m.Name,
AbsValue = m.AbsValue,
RelValue = m.RelValue,
IsStandard = m.IsStandard,
IsQuickSelect = m.IsQuickSelect,
}));
}
await ctx.SaveChangesAsync();
} 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, "Saison anlegen", MessageBoxButton.OK, MessageBoxImage.Error);
}
await App.HintContextChange();
Mouse.OverrideCursor = null;
SeasonList.SelectedIndex = 0;
}
}
private async void SeasonRemoveButton_Click(object sender, RoutedEventArgs evt) {
if (SeasonList.SelectedItem is not Season s)
return;
var r = MessageBox.Show(
$"Soll die Saison {s.Year} wirklich unwiderruflich gelöscht werden?",
"Saison löschen", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel);
if (r == MessageBoxResult.OK) {
Mouse.OverrideCursor = Cursors.AppStarting;
try {
using var ctx = new AppDbContext();
ctx.Remove(s);
await ctx.SaveChangesAsync();
} 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, "Saison löschen", MessageBoxButton.OK, MessageBoxImage.Error);
}
await App.HintContextChange();
Mouse.OverrideCursor = null;
}
}
}
}

View File

@ -331,7 +331,7 @@ namespace Elwig.Windows {
LockInputs();
}
await HintContextChange();
await App.HintContextChange();
}
private void FillInputs(ClientParameters p, Season? s) {

View File

@ -54,7 +54,7 @@
<Button x:Name="DocumentAddButton" Content="&#xF0AF;" FontFamily="Segoe MDL2 Assets" FontSize="14"
Grid.Column="1" Margin="0,0,0,30" VerticalAlignment="Center" Height="25" IsEnabled="False"
Click="DocumentAddButton_Click"/>
<Button x:Name="DocumentRemoveButton" Content="&#xE74D;" FontFamily="Segoe MDL2 Assets" FontSize="14" Padding="1.5,0,0,0"
<Button x:Name="DocumentRemoveButton" Content="&#xE74D;" FontFamily="Segoe MDL2 Assets" FontSize="16" Padding="1.5,0,0,0"
Grid.Column="1" Margin="0,30,0,0" VerticalAlignment="Center" Height="25" IsEnabled="False"
Click="DocumentRemoveButton_Click"/>