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();
[InverseProperty("Member")]
public virtual BillingAddr BillingAddress { get; private set; }
public virtual BillingAddr? BillingAddress { get; private set; }
public int SearchScore(IEnumerable<string> keywords) {
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();
foreach (var tb in TextBoxInputs)
Valid[tb] = true;
ValidateRequiredInputs();
}
abstract protected void UpdateButtons();
@ -119,9 +120,7 @@ namespace Elwig.Windows {
ValidateRequiredInputs();
}
protected bool IsValid() {
return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
}
protected bool IsValid => Valid.All(kv => kv.Value);
protected bool InputHasChanged(Control input) {
if (!OriginalValues.ContainsKey(input)) {
@ -139,39 +138,30 @@ namespace Elwig.Windows {
}
}
protected bool HasChanged() {
return !IsValid() ||
TextBoxInputs.Any(InputHasChanged) ||
ComboBoxInputs.Any(InputHasChanged) ||
CheckBoxInputs.Any(InputHasChanged) ||
RadioButtonInputs.Any(InputHasChanged);
protected bool HasChanged =>
!IsValid ||
TextBoxInputs.Any(InputHasChanged) ||
ComboBoxInputs.Any(InputHasChanged) ||
CheckBoxInputs.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) {
if (plzInput.Text.Length == 4) {
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 bool InputTextChanged(TextBox input, Func<TextBox, bool, ValidationResult> checker) {
return InputTextChanged(input, (tb, required, ctx) => checker(tb, required));
}
protected void InputTextChanged(TextBox input, Func<TextBox, bool, ValidationResult> checker) {
InputTextChanged(input, (tb, required, ctx) => checker(tb, required));
protected bool InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
return InputTextChanged(input, checker(input, SenderIsRequired(input), Context));
}
protected void InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
InputTextChanged(input, checker(input, SenderIsRequired(input), Context));
}
protected void InputTextChanged(TextBox input, ValidationResult res) {
protected bool InputTextChanged(TextBox input, ValidationResult res) {
ValidateInput(input, res.IsValid);
if (res.IsValid) {
if (InputHasChanged(input)) {
@ -183,19 +173,21 @@ namespace Elwig.Windows {
Utils.SetInputInvalid(input);
}
UpdateButtons();
return res.IsValid;
}
protected void InputLostFocus(TextBox input, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg);
protected bool InputLostFocus(TextBox input, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
return InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg);
}
protected void InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
InputLostFocus(input, checker(input, SenderIsRequired(input), Context), msg);
protected bool InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
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)
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
return res.IsValid;
}
protected void CheckBox_Changed(object sender, RoutedEventArgs evt) {
@ -240,12 +232,16 @@ namespace Elwig.Windows {
protected void ComboBox_SelectionChanged(object sender, RoutedEventArgs evt) {
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);
} else if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
ValidateInput(input, true);
if (InputHasChanged(input)) {
Utils.SetInputChanged(input);
} else {
Utils.ClearInputState(input);
}
}
UpdateButtons();
}
@ -272,14 +268,14 @@ namespace Elwig.Windows {
protected void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
var plz = (TextBox)sender;
InputTextChanged(plz, Validator.CheckPlz);
UpdatePlz(plz, Utils.FindNextSibling<ComboBox>(plz));
var valid = InputTextChanged(plz, Validator.CheckPlz);
UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
}
protected void PlzInput_LostFocus(object sender, RoutedEventArgs evt) {
var plz = (TextBox)sender;
InputLostFocus(plz, Validator.CheckPlz);
UpdatePlz(plz, Utils.FindNextSibling<ComboBox>(plz));
var valid = InputLostFocus(plz, Validator.CheckPlz);
UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
}
protected void EmailInput_TextChanged(object sender, RoutedEventArgs evt) {

View File

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

View File

@ -223,7 +223,7 @@
</Grid.ColumnDefinitions>
<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"/>
<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.IsBuchführend = BuchführendInput.IsChecked ?? false;
// TODO Rechnungsadresse
m.EntryDateString = (EntryDateInput.Text == "") ? null : string.Join("-", EntryDateInput.Text.Split(".").Reverse());
m.ExitDateString = (ExitDateInput.Text == "") ? null : string.Join("-", ExitDateInput.Text.Split(".").Reverse());
m.BusinessShares = (BusinessSharesInput.Text == "") ? 0 : int.Parse(BusinessSharesInput.Text);
@ -303,6 +301,26 @@ namespace Elwig.Windows {
m.MgNr = newMgNr;
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();
if (newMgNr != m.MgNr) {
@ -356,7 +374,7 @@ namespace Elwig.Windows {
var billingAddr = m.BillingAddress;
if (billingAddr != null) {
BillingName.Text = billingAddr.Name;
BillingNameInput.Text = billingAddr.Name;
BillingAddressInput.Text = billingAddr.Address;
AT_PlzDest? b = billingAddr.PostalDest.AtPlz;
if (b != null) {
@ -365,7 +383,7 @@ namespace Elwig.Windows {
BillingOrtInput.SelectedItem = b;
}
} else {
BillingName.Text = "";
BillingNameInput.Text = "";
BillingAddressInput.Text = "";
BillingPlzInput.Text = "";
BillingOrtInput.ItemsSource = null;
@ -402,7 +420,7 @@ namespace Elwig.Windows {
override protected void UpdateButtons() {
if (!IsEditing && !IsCreating) return;
bool ch = HasChanged(), v = IsValid();
bool ch = HasChanged, v = IsValid;
ResetButton.IsEnabled = (ch);
SaveButton.IsEnabled = (v && ch);
}