diff --git a/WGneu/Helpers/Utils.cs b/WGneu/Helpers/Utils.cs index 4d181cf..c83b358 100644 --- a/WGneu/Helpers/Utils.cs +++ b/WGneu/Helpers/Utils.cs @@ -65,5 +65,11 @@ namespace WGneu.Helpers { public static bool MgNrExists(AppDbContext ctx, int mgnr) { return ctx.Members.Find(mgnr) != null; } + + public static int NextMgNr(AppDbContext ctx) { + int c = ctx.Members.Select(m => m.MgNr).Min(); + ctx.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToList().ForEach(a => { if (a <= c + 100) c = a; }); + return c + 1; + } } } diff --git a/WGneu/Helpers/Validator.cs b/WGneu/Helpers/Validator.cs index 926d0d2..8953f4c 100644 --- a/WGneu/Helpers/Validator.cs +++ b/WGneu/Helpers/Validator.cs @@ -255,7 +255,7 @@ namespace WGneu.Helpers { return new(true, "Not implemented yet"); } - public static ValidationResult CheckMgNr(TextBox input, bool optional, AppDbContext ctx, Member m) { + public static ValidationResult CheckMgNr(TextBox input, bool optional, AppDbContext ctx, Member? m) { var res = CheckNumeric(input, optional); if (!res.IsValid) { return res; @@ -264,7 +264,7 @@ namespace WGneu.Helpers { } int nr = int.Parse(input.Text); - if (nr != m.MgNr && Utils.MgNrExists(ctx, nr)) { + if (nr != m?.MgNr && Utils.MgNrExists(ctx, nr)) { return new(false, "Mitgliedsnummer wird bereits verwendet"); } diff --git a/WGneu/Windows/MemberListWindow.xaml.cs b/WGneu/Windows/MemberListWindow.xaml.cs index 3a36f73..3ec33d1 100644 --- a/WGneu/Windows/MemberListWindow.xaml.cs +++ b/WGneu/Windows/MemberListWindow.xaml.cs @@ -93,7 +93,8 @@ namespace WGneu.Windows { Utils.ClearInputState(rb); } - private void RefreshInputs() { + private void RefreshInputs(bool validate = false) { + ClearInputStates(); Member m = (Member)MemberList.SelectedItem; if (m != null) { EditMemberButton.IsEnabled = true; @@ -104,12 +105,16 @@ namespace WGneu.Windows { DeleteMemberButton.IsEnabled = false; ClearInputs(); } - ClearInputStates(); + if (!validate) ClearInputStates(); } private void InitInputs() { ClearInputs(); - MgNrInput.Text = NextMgNr().ToString(); + MgNrInput.Text = Utils.NextMgNr(Context).ToString(); + EntryDateInput.Text = DateTime.Now.ToString("dd.MM.yyyy"); + if (Context.Branches.Count() == 1) + BranchInput.SelectedItem = Context.Branches.First(); + FillOriginalValues(); } private void MemberList_SelectionChanged(object sender, RoutedEventArgs evt) { @@ -123,10 +128,11 @@ namespace WGneu.Windows { private void NewMemberButton_Click(object sender, RoutedEventArgs evt) { IsCreating = true; MemberList.IsEnabled = false; - InitInputs(); + MemberList.SelectedItem = null; HideNewEditDeleteButtons(); ShowSaveResetCancelButtons(); UnlockInputs(); + InitInputs(); LockSearchInputs(); } @@ -201,6 +207,7 @@ namespace WGneu.Windows { m.ZwstId = ((Branch)BranchInput.SelectedItem).ZwstId; m.DefaultKgNr = ((AT_Kg)DefaultKgInput.SelectedItem).KgNr; m.Comment = (CommentInput.Text == "") ? null : CommentInput.Text; + m.DefaultContact = "post"; if (ContactPostInput.IsChecked ?? false) m.DefaultContact = "post"; if (ContactEmailInput.IsChecked ?? false) m.DefaultContact = "email"; @@ -245,6 +252,7 @@ namespace WGneu.Windows { HideSaveResetCancelButtons(); ShowNewEditDeleteButtons(); RefreshInputs(); + ClearInputStates(); LockInputs(); UnlockSearchInputs(); } @@ -270,12 +278,6 @@ namespace WGneu.Windows { } } - private int NextMgNr() { - int c = Context.Members.Select(m => m.MgNr).Min(); - Context.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToList().ForEach(a => { if (a <= c + 100) c = a; }); - return c + 1; - } - private void ShowSaveResetCancelButtons() { SaveButton.IsEnabled = false; ResetButton.IsEnabled = false; @@ -397,6 +399,10 @@ namespace WGneu.Windows { Menu_Member_SendEmail.IsEnabled = m.Email != null; + FillOriginalValues(); + } + + private void FillOriginalValues() { foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) OriginalValues[tb] = tb.Text; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) @@ -410,8 +416,10 @@ namespace WGneu.Windows { private void ClearInputs() { Menu_Member_SendEmail.IsEnabled = false; OriginalValues.Clear(); - foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) + foreach (var tb in Utils.FindVisualChilds(this, ExemptInputs)) { + tb.Text = " "; tb.Text = ""; + } foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) cb.SelectedItem = null; foreach (var cb in Utils.FindVisualChilds(this, ExemptInputs)) @@ -456,7 +464,7 @@ namespace WGneu.Windows { Utils.FindVisualChilds(this, ExemptInputs).Any(InputHasChanged); } - private void UpdatePlz(TextBox plzInput, ComboBox ortInput) { + private void UpdatePlz(TextBox plzInput, ComboBox ortInput, bool optional) { if (plzInput.Text.Length == 4) { int plz = int.Parse(plzInput.Text); ortInput.ItemsSource = Context.Postleitzahlen.Where(p => p.Plz == plz).ToHashSet(); @@ -464,7 +472,7 @@ namespace WGneu.Windows { ortInput.ItemsSource = null; } ortInput.SelectedItem = null; - Valid[plzInput] = (ortInput.ItemsSource != null); + Valid[plzInput] = optional || (ortInput.ItemsSource != null); UpdateButtons(); } @@ -499,7 +507,7 @@ namespace WGneu.Windows { InputLostFocus(input, optional, (tb, optional, ctx, m) => checker(tb, optional, ctx), msg); } - private void InputLostFocus(TextBox input, bool optional, Func checker, string? msg = null) { + private void InputLostFocus(TextBox input, bool optional, Func 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); @@ -572,12 +580,12 @@ namespace WGneu.Windows { private void PlzInput_TextChanged(object sender, RoutedEventArgs evt) { InputTextChanged((TextBox)sender, false, Validator.CheckPlz); - UpdatePlz((TextBox)sender, OrtInput); + UpdatePlz((TextBox)sender, OrtInput, false); } private void PlzInput_LostFocus(object sender, RoutedEventArgs evt) { InputLostFocus((TextBox)sender, true, Validator.CheckPlz); - UpdatePlz((TextBox)sender, OrtInput); + UpdatePlz((TextBox)sender, OrtInput, false); } private void PhoneNrInput_TextChanged(object sender, RoutedEventArgs evt) { @@ -630,12 +638,12 @@ namespace WGneu.Windows { private void BillingPlzInput_TextChanged(object sender, RoutedEventArgs evt) { InputTextChanged((TextBox)sender, true, Validator.CheckPlz); - UpdatePlz((TextBox)sender, BillingOrtInput); + UpdatePlz((TextBox)sender, BillingOrtInput, true); } private void BillingPlzInput_LostFocus(object sender, RoutedEventArgs evt) { InputLostFocus((TextBox)sender, true, Validator.CheckPlz); - UpdatePlz((TextBox)sender, BillingOrtInput); + UpdatePlz((TextBox)sender, BillingOrtInput, true); } private void DateInput_TextChanged(object sender, RoutedEventArgs evt) {