Implement option to update billing address

This commit is contained in:
2023-04-27 20:20:56 +02:00
parent 8ffd478ca9
commit 15f999869a
5 changed files with 65 additions and 51 deletions

View File

@ -151,7 +151,7 @@ namespace Elwig.Models {
.ToHashSet(); .ToHashSet();
[InverseProperty("Member")] [InverseProperty("Member")]
public virtual BillingAddr BillingAddress { get; private set; } public virtual BillingAddr? BillingAddress { get; private set; }
public int SearchScore(IEnumerable<string> keywords) { public int SearchScore(IEnumerable<string> keywords) {
keywords = keywords.Where(s => s.Length >= 2 || (s.Length > 0 && s.All(c => char.IsAsciiDigit(c)))); keywords = keywords.Where(s => s.Length >= 2 || (s.Length > 0 && s.All(c => char.IsAsciiDigit(c))));

View File

@ -36,6 +36,7 @@ namespace Elwig.Windows {
RadioButtonInputs = Utils.FindAllChildren<RadioButton>(this, ExemptInputs).ToArray(); RadioButtonInputs = Utils.FindAllChildren<RadioButton>(this, ExemptInputs).ToArray();
foreach (var tb in TextBoxInputs) foreach (var tb in TextBoxInputs)
Valid[tb] = true; Valid[tb] = true;
ValidateRequiredInputs();
} }
abstract protected void UpdateButtons(); abstract protected void UpdateButtons();
@ -119,9 +120,7 @@ namespace Elwig.Windows {
ValidateRequiredInputs(); ValidateRequiredInputs();
} }
protected bool IsValid() { protected bool IsValid => Valid.All(kv => kv.Value);
return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
}
protected bool InputHasChanged(Control input) { protected bool InputHasChanged(Control input) {
if (!OriginalValues.ContainsKey(input)) { if (!OriginalValues.ContainsKey(input)) {
@ -139,39 +138,30 @@ namespace Elwig.Windows {
} }
} }
protected bool HasChanged() { protected bool HasChanged =>
return !IsValid() || !IsValid ||
TextBoxInputs.Any(InputHasChanged) || TextBoxInputs.Any(InputHasChanged) ||
ComboBoxInputs.Any(InputHasChanged) || ComboBoxInputs.Any(InputHasChanged) ||
CheckBoxInputs.Any(InputHasChanged) || CheckBoxInputs.Any(InputHasChanged) ||
RadioButtonInputs.Any(InputHasChanged); RadioButtonInputs.Any(InputHasChanged);
protected void UpdatePlz(TextBox plzInput, bool plzInputValid, ComboBox ortInput) {
var item = ortInput.SelectedItem;
var list = plzInputValid && plzInput.Text.Length == 4 ? Context.Postleitzahlen.Find(int.Parse(plzInput.Text))?.Orte.ToList() : null;
ortInput.ItemsSource = list;
ortInput.SelectedItem = (list != null && item != null && list.Contains(item)) ? item : null;
ComboBox_SelectionChanged(ortInput, new());
} }
protected void UpdatePlz(TextBox plzInput, ComboBox ortInput) { protected bool InputTextChanged(TextBox input, Func<TextBox, bool, ValidationResult> checker) {
if (plzInput.Text.Length == 4) { return InputTextChanged(input, (tb, required, ctx) => checker(tb, required));
ortInput.ItemsSource = Context.Postleitzahlen.Find(int.Parse(plzInput.Text))?.Orte.ToList();
} else {
ortInput.ItemsSource = null;
}
ortInput.SelectedItem = null;
if (ortInput.ItemsSource != null) {
Utils.SetInputInvalid(ortInput);
} else {
Utils.ClearInputState(ortInput);
}
ValidateInput(plzInput, SenderIsRequired(plzInput) || (ortInput.ItemsSource != null));
UpdateButtons();
} }
protected void InputTextChanged(TextBox input, Func<TextBox, bool, ValidationResult> checker) { protected bool InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
InputTextChanged(input, (tb, required, ctx) => checker(tb, required)); return InputTextChanged(input, checker(input, SenderIsRequired(input), Context));
} }
protected void InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker) { protected bool InputTextChanged(TextBox input, ValidationResult res) {
InputTextChanged(input, checker(input, SenderIsRequired(input), Context));
}
protected void InputTextChanged(TextBox input, ValidationResult res) {
ValidateInput(input, res.IsValid); ValidateInput(input, res.IsValid);
if (res.IsValid) { if (res.IsValid) {
if (InputHasChanged(input)) { if (InputHasChanged(input)) {
@ -183,19 +173,21 @@ namespace Elwig.Windows {
Utils.SetInputInvalid(input); Utils.SetInputInvalid(input);
} }
UpdateButtons(); UpdateButtons();
return res.IsValid;
} }
protected void InputLostFocus(TextBox input, Func<TextBox, bool, ValidationResult> checker, string? msg = null) { protected bool InputLostFocus(TextBox input, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg); return InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg);
} }
protected void InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) { protected bool InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, checker(input, SenderIsRequired(input), Context), msg); return InputLostFocus(input, checker(input, SenderIsRequired(input), Context), msg);
} }
protected void InputLostFocus(TextBox input, ValidationResult res, string? msg = null) { protected bool InputLostFocus(TextBox input, ValidationResult res, string? msg = null) {
if (!res.IsValid) if (!res.IsValid)
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning); MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
return res.IsValid;
} }
protected void CheckBox_Changed(object sender, RoutedEventArgs evt) { protected void CheckBox_Changed(object sender, RoutedEventArgs evt) {
@ -240,13 +232,17 @@ namespace Elwig.Windows {
protected void ComboBox_SelectionChanged(object sender, RoutedEventArgs evt) { protected void ComboBox_SelectionChanged(object sender, RoutedEventArgs evt) {
var input = (ComboBox)sender; var input = (ComboBox)sender;
if (input.ItemsSource != null && input.SelectedItem == null) { if (input.ItemsSource != null && input.SelectedItem == null && RequiredInputs.Contains(input)) {
ValidateInput(input, false);
Utils.SetInputInvalid(input); Utils.SetInputInvalid(input);
} else if (InputHasChanged(input)) { } else {
ValidateInput(input, true);
if (InputHasChanged(input)) {
Utils.SetInputChanged(input); Utils.SetInputChanged(input);
} else { } else {
Utils.ClearInputState(input); Utils.ClearInputState(input);
} }
}
UpdateButtons(); UpdateButtons();
} }
@ -272,14 +268,14 @@ namespace Elwig.Windows {
protected void PlzInput_TextChanged(object sender, RoutedEventArgs evt) { protected void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
var plz = (TextBox)sender; var plz = (TextBox)sender;
InputTextChanged(plz, Validator.CheckPlz); var valid = InputTextChanged(plz, Validator.CheckPlz);
UpdatePlz(plz, Utils.FindNextSibling<ComboBox>(plz)); UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
} }
protected void PlzInput_LostFocus(object sender, RoutedEventArgs evt) { protected void PlzInput_LostFocus(object sender, RoutedEventArgs evt) {
var plz = (TextBox)sender; var plz = (TextBox)sender;
InputLostFocus(plz, Validator.CheckPlz); var valid = InputLostFocus(plz, Validator.CheckPlz);
UpdatePlz(plz, Utils.FindNextSibling<ComboBox>(plz)); UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
} }
protected void EmailInput_TextChanged(object sender, RoutedEventArgs evt) { protected void EmailInput_TextChanged(object sender, RoutedEventArgs evt) {

View File

@ -329,7 +329,7 @@ namespace Elwig.Windows {
override protected void UpdateButtons() { override protected void UpdateButtons() {
if (!IsEditing && !IsCreating) return; if (!IsEditing && !IsCreating) return;
bool ch = HasChanged(), v = IsValid(); bool ch = HasChanged, v = IsValid;
ContractSaveButton.IsEnabled = (v && ch); ContractSaveButton.IsEnabled = (v && ch);
AreaCommitmentResetButton.IsEnabled = (ch); AreaCommitmentResetButton.IsEnabled = (ch);
AreaCommitmentSaveButton.IsEnabled = (v && ch); AreaCommitmentSaveButton.IsEnabled = (v && ch);

View File

@ -223,7 +223,7 @@
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Label Content="Name:" Margin="10,10,0,0" Grid.Column="0"/> <Label Content="Name:" Margin="10,10,0,0" Grid.Column="0"/>
<TextBox x:Name="BillingName" Margin="0,10,10,10" Grid.Column="1" Grid.ColumnSpan="2" <TextBox x:Name="BillingNameInput" Margin="0,10,10,10" Grid.Column="1" Grid.ColumnSpan="2"
TextChanged="TextBox_TextChanged"/> TextChanged="TextBox_TextChanged"/>
<Label Content="Adresse:" Margin="10,40,0,0" Grid.Column="0"/> <Label Content="Adresse:" Margin="10,40,0,0" Grid.Column="0"/>

View File

@ -279,8 +279,6 @@ namespace Elwig.Windows {
m.LfbisNr = (LfbisNrInput.Text == "") ? null : LfbisNrInput.Text; m.LfbisNr = (LfbisNrInput.Text == "") ? null : LfbisNrInput.Text;
m.IsBuchführend = BuchführendInput.IsChecked ?? false; m.IsBuchführend = BuchführendInput.IsChecked ?? false;
// TODO Rechnungsadresse
m.EntryDateString = (EntryDateInput.Text == "") ? null : string.Join("-", EntryDateInput.Text.Split(".").Reverse()); m.EntryDateString = (EntryDateInput.Text == "") ? null : string.Join("-", EntryDateInput.Text.Split(".").Reverse());
m.ExitDateString = (ExitDateInput.Text == "") ? null : string.Join("-", ExitDateInput.Text.Split(".").Reverse()); m.ExitDateString = (ExitDateInput.Text == "") ? null : string.Join("-", ExitDateInput.Text.Split(".").Reverse());
m.BusinessShares = (BusinessSharesInput.Text == "") ? 0 : int.Parse(BusinessSharesInput.Text); m.BusinessShares = (BusinessSharesInput.Text == "") ? 0 : int.Parse(BusinessSharesInput.Text);
@ -303,6 +301,26 @@ namespace Elwig.Windows {
m.MgNr = newMgNr; m.MgNr = newMgNr;
tr = (await Context.AddAsync(m)); tr = (await Context.AddAsync(m));
} }
if (BillingOrtInput.SelectedItem == null) {
if (m.BillingAddress != null) {
Context.Remove(m.BillingAddress);
}
} else {
BillingAddr b = m.BillingAddress ?? Context.CreateProxy<BillingAddr>();
b.Name = BillingNameInput.Text;
b.Address = BillingAddressInput.Text;
var p = (AT_PlzDest)BillingOrtInput.SelectedItem;
b.CountryCode = p.CountryCode;
b.PostalDestId = p.Id;
if (m.BillingAddress == null) {
b.MgNr = newMgNr;
await Context.AddAsync(b);
} else {
Context.Update(b);
}
}
await Context.SaveChangesAsync(); await Context.SaveChangesAsync();
if (newMgNr != m.MgNr) { if (newMgNr != m.MgNr) {
@ -356,7 +374,7 @@ namespace Elwig.Windows {
var billingAddr = m.BillingAddress; var billingAddr = m.BillingAddress;
if (billingAddr != null) { if (billingAddr != null) {
BillingName.Text = billingAddr.Name; BillingNameInput.Text = billingAddr.Name;
BillingAddressInput.Text = billingAddr.Address; BillingAddressInput.Text = billingAddr.Address;
AT_PlzDest? b = billingAddr.PostalDest.AtPlz; AT_PlzDest? b = billingAddr.PostalDest.AtPlz;
if (b != null) { if (b != null) {
@ -365,7 +383,7 @@ namespace Elwig.Windows {
BillingOrtInput.SelectedItem = b; BillingOrtInput.SelectedItem = b;
} }
} else { } else {
BillingName.Text = ""; BillingNameInput.Text = "";
BillingAddressInput.Text = ""; BillingAddressInput.Text = "";
BillingPlzInput.Text = ""; BillingPlzInput.Text = "";
BillingOrtInput.ItemsSource = null; BillingOrtInput.ItemsSource = null;
@ -402,7 +420,7 @@ namespace Elwig.Windows {
override protected void UpdateButtons() { override protected void UpdateButtons() {
if (!IsEditing && !IsCreating) return; if (!IsEditing && !IsCreating) return;
bool ch = HasChanged(), v = IsValid(); bool ch = HasChanged, v = IsValid;
ResetButton.IsEnabled = (ch); ResetButton.IsEnabled = (ch);
SaveButton.IsEnabled = (v && ch); SaveButton.IsEnabled = (v && ch);
} }