PaymentVariantsWindow: Add possibility to switch options on/off

This commit is contained in:
2024-01-17 14:57:45 +01:00
parent 38ad433b4e
commit 9eb013ce11
10 changed files with 242 additions and 34 deletions

View File

@ -21,6 +21,7 @@ namespace Elwig.Windows {
public readonly int Year;
public readonly bool SeasonLocked;
private bool DataValid, DataChanged, NameChanged, CommentChanged, TransferDateValid, TransferDateChanged;
private BillingData? BillingData;
private static readonly JsonSerializerOptions JsonOpt = new() { WriteIndented = true };
@ -56,16 +57,37 @@ namespace Elwig.Windows {
ExportButton.IsEnabled = locked;
NameInput.Text = v.Name;
NameInput.IsReadOnly = false;
CommentInput.Text = v.Comment;
CommentInput.IsReadOnly = false;
DateInput.Text = $"{v.Date:dd.MM.yyyy}";
DateInput.IsReadOnly = false;
TransferDateInput.Text = $"{v.TransferDate:dd.MM.yyyy}";
if (App.Config.Debug) {
try {
var json = BillingData.ParseJson(v.Data);
DataInput.Text = JsonSerializer.Serialize(json, JsonOpt);
} catch {
DataInput.Text = v.Data;
}
TransferDateInput.IsReadOnly = false;
try {
BillingData = BillingData.FromJson(v.Data);
ConsiderModifiersInput.IsChecked = BillingData.ConsiderDelieryModifiers;
ConsiderModifiersInput.IsEnabled = !locked;
ConsiderPenaltiesInput.IsChecked = BillingData.ConsiderContractPenalties;
ConsiderPenaltiesInput.IsEnabled = !locked;
ConsiderPenaltyInput.IsChecked = BillingData.ConsiderTotalPenalty;
ConsiderPenaltyInput.IsEnabled = !locked;
ConsiderAutoInput.IsChecked = BillingData.ConsiderAutoBusinessShares;
ConsiderAutoInput.IsEnabled = !locked;
DataInput.Text = JsonSerializer.Serialize(BillingData.Data, JsonOpt);
DataInput.IsReadOnly = locked;
} catch {
BillingData = null;
ConsiderModifiersInput.IsChecked = false;
ConsiderModifiersInput.IsEnabled = false;
ConsiderPenaltiesInput.IsChecked = false;
ConsiderPenaltiesInput.IsEnabled = false;
ConsiderPenaltyInput.IsChecked = false;
ConsiderPenaltyInput.IsEnabled = false;
ConsiderAutoInput.IsChecked = false;
ConsiderAutoInput.IsEnabled = false;
DataInput.Text = v.Data;
DataInput.IsEnabled = false;
}
} else {
EditButton.Content = "Bearbeiten";
@ -81,11 +103,25 @@ namespace Elwig.Windows {
PrintButton.IsEnabled = false;
ExportButton.IsEnabled = false;
BillingData = null;
NameInput.Text = "";
NameInput.IsReadOnly = true;
CommentInput.Text = "";
CommentInput.IsReadOnly = true;
DateInput.Text = "";
DateInput.IsReadOnly = true;
TransferDateInput.Text = "";
TransferDateInput.IsReadOnly = true;
ConsiderModifiersInput.IsChecked = false;
ConsiderModifiersInput.IsEnabled = false;
ConsiderPenaltiesInput.IsChecked = false;
ConsiderPenaltiesInput.IsEnabled = false;
ConsiderPenaltyInput.IsChecked = false;
ConsiderPenaltyInput.IsEnabled = false;
ConsiderAutoInput.IsChecked = false;
ConsiderAutoInput.IsEnabled = false;
DataInput.Text = "";
DataInput.IsReadOnly = true;
}
UpdateSums();
UpdateSaveButton();
@ -94,7 +130,11 @@ namespace Elwig.Windows {
private void UpdateSaveButton() {
SaveButton.IsEnabled = PaymentVariantList.SelectedItem != null &&
((DataChanged && DataValid) || NameChanged || CommentChanged ||
(TransferDateChanged && TransferDateValid));
(TransferDateChanged && TransferDateValid)) ||
(ConsiderModifiersInput.IsChecked != BillingData?.ConsiderDelieryModifiers) ||
(ConsiderPenaltiesInput.IsChecked != BillingData?.ConsiderContractPenalties) ||
(ConsiderPenaltyInput.IsChecked != BillingData?.ConsiderTotalPenalty) ||
(ConsiderAutoInput.IsChecked != BillingData?.ConsiderAutoBusinessShares);
}
private void UpdateSums() {
@ -268,15 +308,25 @@ namespace Elwig.Windows {
}
private async void SaveButton_Click(object sender, RoutedEventArgs evt) {
if (PaymentVariantList.SelectedItem is not PaymentVar v) return;
if (PaymentVariantList.SelectedItem is not PaymentVar v || BillingData == null) return;
try {
v.Name = NameInput.Text;
v.Comment = (CommentInput.Text != "") ? CommentInput.Text : null;
v.TransferDateString = (TransferDateInput.Text != "") ? string.Join("-", TransferDateInput.Text.Split(".").Reverse()) : null;
if (App.Config.Debug) v.Data = JsonSerializer.Serialize(BillingData.ParseJson(DataInput.Text));
var d = App.Config.Debug ? BillingData.FromJson(DataInput.Text) : BillingData;
d.ConsiderDelieryModifiers = ConsiderModifiersInput.IsChecked ?? false;
d.ConsiderContractPenalties = ConsiderPenaltiesInput.IsChecked ?? false;
d.ConsiderTotalPenalty = ConsiderPenaltyInput.IsChecked ?? false;
d.ConsiderAutoBusinessShares = ConsiderAutoInput.IsChecked ?? false;
v.Data = JsonSerializer.Serialize(d.Data);
Context.Update(v);
await Context.SaveChangesAsync();
await App.HintContextChange();
CommentInput_TextChanged(null, null);
ConsiderModifiersInput_Changed(null, null);
ConsiderPenaltiesInput_Changed(null, null);
ConsiderPenaltyInput_Changed(null, null);
ConsiderAutoInput_Changed(null, null);
} catch (Exception exc) {
await HintContextChange();
var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message;
@ -304,7 +354,7 @@ namespace Elwig.Windows {
UpdateSaveButton();
}
private void CommentInput_TextChanged(object sender, TextChangedEventArgs evt) {
private void CommentInput_TextChanged(object? sender, TextChangedEventArgs? evt) {
if (PaymentVariantList.SelectedItem is not PaymentVar v) {
ControlUtils.ClearInputState(CommentInput);
return;
@ -366,6 +416,58 @@ namespace Elwig.Windows {
UpdateSaveButton();
}
private void ConsiderModifiersInput_Changed(object? sender, RoutedEventArgs? evt) {
if (BillingData == null) {
ControlUtils.ClearInputState(ConsiderModifiersInput);
return;
}
if (BillingData.ConsiderDelieryModifiers != ConsiderModifiersInput.IsChecked) {
ControlUtils.SetInputChanged(ConsiderModifiersInput);
} else {
ControlUtils.ClearInputState(ConsiderModifiersInput);
}
UpdateSaveButton();
}
private void ConsiderPenaltiesInput_Changed(object? sender, RoutedEventArgs? evt) {
if (BillingData == null) {
ControlUtils.ClearInputState(ConsiderPenaltiesInput);
return;
}
if (BillingData.ConsiderContractPenalties != ConsiderPenaltiesInput.IsChecked) {
ControlUtils.SetInputChanged(ConsiderPenaltiesInput);
} else {
ControlUtils.ClearInputState(ConsiderPenaltiesInput);
}
UpdateSaveButton();
}
private void ConsiderPenaltyInput_Changed(object? sender, RoutedEventArgs? evt) {
if (BillingData == null) {
ControlUtils.ClearInputState(ConsiderPenaltyInput);
return;
}
if (BillingData.ConsiderTotalPenalty != ConsiderPenaltyInput.IsChecked) {
ControlUtils.SetInputChanged(ConsiderPenaltyInput);
} else {
ControlUtils.ClearInputState(ConsiderPenaltyInput);
}
UpdateSaveButton();
}
private void ConsiderAutoInput_Changed(object? sender, RoutedEventArgs? evt) {
if (BillingData == null) {
ControlUtils.ClearInputState(ConsiderAutoInput);
return;
}
if (BillingData.ConsiderAutoBusinessShares != ConsiderAutoInput.IsChecked) {
ControlUtils.SetInputChanged(ConsiderAutoInput);
} else {
ControlUtils.ClearInputState(ConsiderAutoInput);
}
UpdateSaveButton();
}
private async Task Generate(int mode) {
if (PaymentVariantList.SelectedItem is not PaymentVar v)
return;