Check MgNr and PredecessorMgNr

This commit is contained in:
2023-03-11 16:18:37 +01:00
parent 86560e6e57
commit b887e0ffe3
4 changed files with 104 additions and 24 deletions

View File

@ -61,5 +61,9 @@ namespace WGneu.Helpers {
UseShellExecute = true,
});
}
public static bool MgNrExists(AppDbContext ctx, int mgnr) {
return ctx.Members.Find(mgnr) != null;
}
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using WGneu.Models;
namespace WGneu.Helpers {
static class Validator {
@ -29,11 +30,11 @@ namespace WGneu.Helpers {
{ "423", Array.Empty<string[]>() },
};
public static ValidationResult CheckNumericInput(TextBox input, bool optional) {
return CheckNumericInput(input, optional, -1);
public static ValidationResult CheckNumeric(TextBox input, bool optional) {
return CheckNumeric(input, optional, -1);
}
private static ValidationResult CheckNumericInput(TextBox input, bool optional, int maxLen) {
private static ValidationResult CheckNumeric(TextBox input, bool optional, int maxLen) {
string text = "";
int pos = input.CaretIndex;
for (int i = 0; i < input.Text.Length; i++) {
@ -60,7 +61,7 @@ namespace WGneu.Helpers {
}
public static ValidationResult CheckPlz(TextBox input, bool optional) {
CheckNumericInput(input, true, 4);
CheckNumeric(input, true, 4);
if (optional && input.Text.Length == 0) {
return new(true, null);
} else if (input.Text.Length != 4) {
@ -235,21 +236,51 @@ namespace WGneu.Helpers {
}
public static ValidationResult CheckLfbisNr(TextBox input, bool optional) {
var res = CheckNumericInput(input, true, 7);
if (!res.IsValid)
var res = CheckNumeric(input, true, 7);
if (!res.IsValid) {
return res;
if (optional && input.Text.Length == 0)
} else if (optional && input.Text.Length == 0) {
return new(true, null);
if (input.Text.Length != 7)
} else if (input.Text.Length != 7) {
return new(false, "Betriebsnummer zu kurz");
}
// TODO
return new(true, "Not implemented yet");
}
public static ValidationResult CheckUstIdInput(TextBox input, bool optional) {
public static ValidationResult CheckUstId(TextBox input, bool optional) {
return new(false, "Not implemented yet");
}
public static ValidationResult CheckMgNr(TextBox input, bool optional, AppDbContext ctx, Member m) {
var res = CheckNumeric(input, optional);
if (!res.IsValid) {
return res;
} else if (optional && input.Text.Length == 0) {
return new(true, null);
}
int nr = int.Parse(input.Text);
if (nr != m.MgNr && Utils.MgNrExists(ctx, nr)) {
return new(false, "Mitgliedsnummer wird bereits verwendet");
}
return new(true, null);
}
public static ValidationResult CheckPredecessorMgNr(TextBox input, bool optional, AppDbContext ctx) {
var res = CheckNumeric(input, optional);
if (!res.IsValid) {
return res;
} else if (optional && input.Text.Length == 0) {
return new(true, null);
} else if (!Utils.MgNrExists(ctx, int.Parse(input.Text))) {
return new(false, "Ein Mitglied mit dieser Mitgliedsnummer existiert nicht");
}
return new(true, null);
}
}
}

View File

@ -115,10 +115,12 @@
</Grid.ColumnDefinitions>
<Label Content="MgNr.:" Margin="10,10,0,0" Grid.Column="0"/>
<TextBox x:Name="MgNrInput" Margin="0,10,0,0" Width="48" Grid.Column="1" TextAlignment="Right" HorizontalAlignment="Left"/>
<TextBox x:Name="MgNrInput" Margin="0,10,0,0" Width="48" Grid.Column="1" TextAlignment="Right" HorizontalAlignment="Left"
TextChanged="MgNrInput_TextChanged" LostFocus="MgNrInput_LostFocus"/>
<Label Content="Vorg.:" Margin="10,10,0,0" Grid.Column="2"/>
<TextBox x:Name="PredecessorMgNrInput" Margin="0,10,10,0" Width="48" Grid.Column="3" TextAlignment="Right" HorizontalAlignment="Left"/>
<TextBox x:Name="PredecessorMgNrInput" Margin="0,10,10,0" Width="48" Grid.Column="3" TextAlignment="Right" HorizontalAlignment="Left"
TextChanged="PredecessorMgNrInput_TextChanged" LostFocus="PredecessorMgNrInput_LostFocus"/>
<Label Content="Vorname:" Margin="10,40,0,0" Grid.Column="0"/>
<TextBox x:Name="GivenNameInput" Margin="0,40,0,0" Grid.Column="1"
@ -200,7 +202,8 @@
</Grid.ColumnDefinitions>
<Label Content="UID:" Margin="10,10,0,0" Grid.Column="0"/>
<TextBox x:Name="UstIdInput" Margin="0,10,10,0" Grid.Column="1" Width="120" HorizontalAlignment="Left"/>
<TextBox x:Name="UstIdInput" Margin="0,10,10,0" Grid.Column="1" Width="120" HorizontalAlignment="Left"
TextChanged="UstIdInput_TextChanged" LostFocus="UstIdInput_LostFocus"/>
<Label Content="BetriebsNr.:" Margin="10,40,0,0" Grid.Column="0"/>
<TextBox x:Name="LfbisNrInput" Margin="0,40,10,0" Grid.Column="1" Width="64" HorizontalAlignment="Left" TextAlignment="Right"

View File

@ -112,7 +112,7 @@ namespace WGneu.Windows {
MgNrInput.Text = NextMgNr().ToString();
}
private void MemberList_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
private void MemberList_SelectionChanged(object sender, RoutedEventArgs evt) {
RefreshInputs();
}
@ -165,6 +165,8 @@ namespace WGneu.Windows {
m = new();
int newMgNr = int.Parse(MgNrInput.Text);
m.PredecessorMgNr = (PredecessorMgNrInput.Text == "") ? null : int.Parse(PredecessorMgNrInput.Text);
m.Predecessor = Context.Members.Find(m.PredecessorMgNr);
m.Prefix = (PrefixInput.Text == "") ? null : PrefixInput.Text;
m.GivenName = GivenNameInput.Text;
m.FamilyName = FamilyNameInput.Text;
@ -257,7 +259,7 @@ namespace WGneu.Windows {
RefreshMemberList();
}
private void Menu_Member_SendEmail_Click(object sender, EventArgs evt) {
private void Menu_Member_SendEmail_Click(object sender, RoutedEventArgs evt) {
Utils.MailTo(((Member)MemberList.SelectedItem).Email);
}
@ -467,7 +469,15 @@ namespace WGneu.Windows {
}
private void InputTextChanged(TextBox input, bool optional, Func<TextBox, bool, ValidationResult> checker) {
var res = checker(input, optional);
InputTextChanged(input, optional, (tb, opt, ctx) => checker(tb, opt));
}
private void InputTextChanged(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
InputTextChanged(input, optional, (tb, opt, ctx, m) => checker(tb, opt, ctx));
}
private void InputTextChanged(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, Member, ValidationResult> checker) {
var res = checker(input, optional, Context, (Member)MemberList.SelectedItem);
Valid[input] = res.IsValid;
if (res.IsValid) {
if (InputHasChanged(input)) {
@ -481,8 +491,16 @@ namespace WGneu.Windows {
UpdateButtons();
}
private void InputLostFocus(TextBox input, bool optional, Func<TextBox, bool, ValidationResult> checker, string? msg) {
var res = checker(input, optional);
private void InputLostFocus(TextBox input, bool optional, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, optional, (tb, optional, ctx) => checker(tb, optional), msg);
}
private void InputLostFocus(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, optional, (tb, optional, ctx, m) => checker(tb, optional, ctx), msg);
}
private void InputLostFocus(TextBox input, bool optional, Func<TextBox, bool, AppDbContext, Member, ValidationResult> checker, string? msg = null) {
var res = checker(input, optional, Context, (Member)MemberList.SelectedItem);
if (!res.IsValid)
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
}
@ -528,13 +546,29 @@ namespace WGneu.Windows {
UpdateButtons();
}
private void MgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, false, Validator.CheckMgNr);
}
private void MgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, false, Validator.CheckMgNr);
}
private void PredecessorMgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckPredecessorMgNr);
}
private void PredecessorMgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPredecessorMgNr);
}
private void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, false, Validator.CheckPlz);
UpdatePlz((TextBox)sender, OrtInput);
}
private void PlzInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPlz, null);
InputLostFocus((TextBox)sender, true, Validator.CheckPlz);
UpdatePlz((TextBox)sender, OrtInput);
}
@ -543,7 +577,7 @@ namespace WGneu.Windows {
}
private void PhoneNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPhoneNumber, null);
InputLostFocus((TextBox)sender, true, Validator.CheckPhoneNumber);
}
private void EmailInput_TextChanged(object sender, RoutedEventArgs evt) {
@ -551,7 +585,7 @@ namespace WGneu.Windows {
}
private void EmailInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckEmailAddress, null);
InputLostFocus((TextBox)sender, true, Validator.CheckEmailAddress);
}
private void IbanInput_TextChanged(object sender, RoutedEventArgs evt) {
@ -559,7 +593,7 @@ namespace WGneu.Windows {
}
private void IbanInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckIban, null);
InputLostFocus((TextBox)sender, true, Validator.CheckIban);
}
private void BicInput_TextChanged(object sender, RoutedEventArgs evt) {
@ -567,7 +601,15 @@ namespace WGneu.Windows {
}
private void BicInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckBic, null);
InputLostFocus((TextBox)sender, true, Validator.CheckBic);
}
private void UstIdInput_TextChanged(object sender, RoutedEventArgs evt) {
InputTextChanged((TextBox)sender, true, Validator.CheckUstId);
}
private void UstIdInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckUstId);
}
private void LfbisNrInput_TextChanged(object sender, RoutedEventArgs evt) {
@ -575,7 +617,7 @@ namespace WGneu.Windows {
}
private void LfbisNrInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckLfbisNr, "Betriebsnummer ungültig");
InputLostFocus((TextBox)sender, true, Validator.CheckLfbisNr);
}
private void BillingPlzInput_TextChanged(object sender, RoutedEventArgs evt) {
@ -584,7 +626,7 @@ namespace WGneu.Windows {
}
private void BillingPlzInput_LostFocus(object sender, RoutedEventArgs evt) {
InputLostFocus((TextBox)sender, true, Validator.CheckPlz, null);
InputLostFocus((TextBox)sender, true, Validator.CheckPlz);
UpdatePlz((TextBox)sender, BillingOrtInput);
}
}