From 5b9aedd1a4d3a1ff029b23a64581670679650c8f Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Tue, 23 May 2023 20:00:24 +0200 Subject: [PATCH] Improve DeliveryAdminWindow --- Elwig/App.xaml | 4 ++ Elwig/App.xaml.cs | 19 ++++++ Elwig/Helpers/AppDbContext.cs | 8 +++ Elwig/Helpers/Config.cs | 7 +++ Elwig/Helpers/NullItem.cs | 5 ++ Elwig/Helpers/Utils.cs | 1 - Elwig/Models/WineQualLevel.cs | 2 +- Elwig/Windows/DeliveryAdminWindow.xaml | 75 +++++++++++++++++++---- Elwig/Windows/DeliveryAdminWindow.xaml.cs | 55 ++++++++++++++++- 9 files changed, 161 insertions(+), 15 deletions(-) create mode 100644 Elwig/Helpers/NullItem.cs diff --git a/Elwig/App.xaml b/Elwig/App.xaml index 4962032..f758573 100644 --- a/Elwig/App.xaml +++ b/Elwig/App.xaml @@ -2,9 +2,12 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Elwig" + xmlns:controls="clr-namespace:Elwig.Controls" StartupUri="Windows\MainWindow.xaml" xmlns:ui="http://schemas.modernwpf.com/2019"> + + @@ -63,6 +66,7 @@ + diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index 163096b..6aaf889 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -15,6 +15,8 @@ namespace Elwig { public static readonly string DataPath = @"C:\ProgramData\Elwig\"; public static readonly string ExePath = @"C:\Program Files\Elwig\"; public static readonly Config Config = new(DataPath + "config.ini"); + + public static string ZwstId { get; private set; } public static IEnumerable Scales { get; private set; } public static ClientParameters Client { get; private set; } @@ -30,10 +32,13 @@ namespace Elwig { } protected override void OnStartup(StartupEventArgs evt) { + IEnumerable branches = Array.Empty(); using (var ctx = new AppDbContext()) { if (!ctx.Database.CanConnect()) { MessageBox.Show($"Invalid Database:\n\n{Config.DatabaseFile}", "Invalid Database", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(); + } else { + branches = ctx.Branches.Select(b => b.ZwstId).ToList(); } } Utils.RunBackground("HTML Initialization", () => Documents.Html.Init(PrintingReadyChanged)); @@ -62,6 +67,20 @@ namespace Elwig { } Scales = list; + if (Config.ZwstId != null) { + if (!branches.Contains(Config.ZwstId)) { + MessageBox.Show("Invalid branch id in config!", "Invalid Branch Config", MessageBoxButton.OK, MessageBoxImage.Error); + Shutdown(); + } else { + ZwstId = Config.ZwstId; + } + } else if (branches.Count() == 1) { + ZwstId = branches.First(); + } else { + MessageBox.Show("Unable to determine local branch!", "Invalid Branch Config", MessageBoxButton.OK, MessageBoxImage.Error); + Shutdown(); + } + base.OnStartup(evt); } diff --git a/Elwig/Helpers/AppDbContext.cs b/Elwig/Helpers/AppDbContext.cs index 832f62f..f41a440 100644 --- a/Elwig/Helpers/AppDbContext.cs +++ b/Elwig/Helpers/AppDbContext.cs @@ -100,5 +100,13 @@ namespace Elwig.Helpers { .ForEach(a => { if (a <= c + 100) c = a; }); return c + 1; } + + public async Task NextLNr(DateOnly date) { + var dateStr = date.ToString("yyyy-MM-dd"); + int c = 0; + (await Deliveries.Where(d => d.DateString == dateStr).Select(d => d.LNr).ToListAsync()) + .ForEach(a => { if (a <= c + 10) c = a; }); + return c + 1; + } } } diff --git a/Elwig/Helpers/Config.cs b/Elwig/Helpers/Config.cs index a95ed67..ebf2024 100644 --- a/Elwig/Helpers/Config.cs +++ b/Elwig/Helpers/Config.cs @@ -11,6 +11,7 @@ namespace Elwig.Helpers { private readonly string FileName; public string DatabaseFile = App.DataPath + "database.sqlite3"; public string? DatabaseLog = null; + public string? ZwstId = null; public IEnumerable Scales; private readonly LinkedList ScaleList = new(); @@ -43,6 +44,12 @@ namespace Elwig.Helpers { DatabaseLog = App.DataPath + log; } + if (ini == null || !ini.TryGetKey("general.branch", out string branch)) { + ZwstId = null; + } else { + ZwstId = branch; + } + ScaleList.Clear(); Scales = ScaleList; if (ini != null) { diff --git a/Elwig/Helpers/NullItem.cs b/Elwig/Helpers/NullItem.cs new file mode 100644 index 0000000..9ac9f61 --- /dev/null +++ b/Elwig/Helpers/NullItem.cs @@ -0,0 +1,5 @@ +namespace Elwig.Helpers { + public class NullItem { + public static string Name => "- Keine Angabe -"; + } +} diff --git a/Elwig/Helpers/Utils.cs b/Elwig/Helpers/Utils.cs index b22a959..18d1d42 100644 --- a/Elwig/Helpers/Utils.cs +++ b/Elwig/Helpers/Utils.cs @@ -9,7 +9,6 @@ using System.Diagnostics; using System.Windows.Controls.Primitives; using System.Text.RegularExpressions; using System.IO.Ports; -using PdfSharp.Charting; using System.Net.Sockets; namespace Elwig.Helpers { diff --git a/Elwig/Models/WineQualLevel.cs b/Elwig/Models/WineQualLevel.cs index ded8697..144051b 100644 --- a/Elwig/Models/WineQualLevel.cs +++ b/Elwig/Models/WineQualLevel.cs @@ -23,6 +23,6 @@ namespace Elwig.Models { [Column("name")] public string Name { get; private set; } - public string MinKmwStr => (MinKmw == null) ? "" : $"(mind. {MinKmw:#.0} °KMW)"; + public string MinKmwStr => (MinKmw == null) ? "" : $"(mind. {MinKmw:#.0}°)"; } } diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml b/Elwig/Windows/DeliveryAdminWindow.xaml index 337ef14..ecc767a 100644 --- a/Elwig/Windows/DeliveryAdminWindow.xaml +++ b/Elwig/Windows/DeliveryAdminWindow.xaml @@ -88,6 +88,30 @@ + + + + + + + + + + @@ -112,27 +136,39 @@ - - + + + + - + + + + + + + - + - + + + + + diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml.cs b/Elwig/Windows/DeliveryAdminWindow.xaml.cs index 835a722..ee34b3d 100644 --- a/Elwig/Windows/DeliveryAdminWindow.xaml.cs +++ b/Elwig/Windows/DeliveryAdminWindow.xaml.cs @@ -1,8 +1,11 @@ using Elwig.Helpers; using Elwig.Models; +using System; +using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Threading; using Xceed.Wpf.Toolkit.Primitives; namespace Elwig.Windows { @@ -10,6 +13,8 @@ namespace Elwig.Windows { private bool IsUpdatingGradation = false; + private DispatcherTimer dispatcherTimer; + public DeliveryAdminWindow() { InitializeComponent(); RequiredInputs = new Control[] { @@ -17,21 +22,35 @@ namespace Elwig.Windows { SortIdInput, WineVarietyInput, GradationOeInput, GradationKmwInput, WineQualityLevelInput, - WineOriginInput, + WineOriginInput, WineKgInput }; ExemptInputs = new Control[] { MgNrInput, MemberInput, MemberAddressField }; + + dispatcherTimer = new DispatcherTimer(); + dispatcherTimer.Tick += new EventHandler(OnSecondPassed); + dispatcherTimer.Interval = new TimeSpan(0, 0, 1); + dispatcherTimer.Start(); } private void Window_Loaded(object sender, RoutedEventArgs evt) { MemberInput.ItemsSource = Context.Members.OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToList(); + BranchInput.ItemsSource = Context.Branches.OrderBy(b => b.Name).ToList(); + BranchInput.SelectedItem = BranchInput.ItemsSource.Cast().Where(b => b.ZwstId == App.ZwstId).First(); WineVarietyInput.ItemsSource = Context.WineVarieties.OrderBy(v => v.Name).ToList(); AttributesInput.ItemsSource = Context.WineAttributes.OrderBy(a => a.Name).ToList(); WineQualityLevelInput.ItemsSource = Context.WineQualityLevels.ToList(); ModifiersInput.ItemsSource = Context.Modifiers.Where(m => m.Season.Year == 2022).OrderBy(m => m.Name).ToList(); WineOriginInput.ItemsSource = Context.WineOrigins.ToList().OrderByDescending(o => o.SortKey).ThenBy(o => o.HkId); + WineKgInput.ItemsSource = Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToList(); + } + + private void OnSecondPassed(object? sender, EventArgs evt) { + var now = DateTime.Now; + TimeInput.Text = now.ToString("HH:mm"); + DateInput.Text = now.ToString("dd.MM.yyyy"); } protected override void UpdateButtons() { @@ -52,6 +71,14 @@ namespace Elwig.Windows { var m = MemberInput.SelectedItem as Member; if (m != null) MgNrInput.Text = m.MgNr.ToString(); MemberAddressField.Text = m?.FullAddress; + WineKgInput.SelectedItem = m?.DefaultKg; + } + + private void DateInput_TextChanged(object sender, TextChangedEventArgs evt) { + var branch = (Branch)BranchInput.SelectedItem; + var date = DateOnly.ParseExact(DateInput.Text, "dd.MM.yyyy"); + var lnr = Context.NextLNr(date).GetAwaiter().GetResult(); + LsNrInput.Text = $"{date.ToString("yyyyMMdd")}{branch.ZwstId}{lnr:000}"; } private void UpdateWineVariety(bool valid) { @@ -135,5 +162,31 @@ namespace Elwig.Windows { private void ModifiersInput_SelectionChanged(object sender, ItemSelectionChangedEventArgs evt) { } + + private void UpdateWineOrigin() { + var qual = (WineQualLevel)WineQualityLevelInput.SelectedItem; + var kg = ((AT_Kg)WineKgInput.SelectedItem)?.WbKg; + if (qual == null || kg == null) return; + var o = kg.Origin; + while (o != null && o.Level > qual.OriginLevel) o = o.Parent; + WineOriginInput.SelectedItem = o; + } + + private void WineQualityLevelInput_SelectionChanged(object sender, SelectionChangedEventArgs evt) { + UpdateWineOrigin(); + } + + private void WineKgInput_SelectionChanged(object sender, SelectionChangedEventArgs evt) { + UpdateWineOrigin(); + var kg = (AT_Kg)WineKgInput.SelectedItem; + if (kg != null) { + var list = Context.WbRde.Where(r => r.KgNr == kg.KgNr).OrderBy(r => r.Name).Cast().ToList(); + list.Insert(0, new NullItem()); + WineRdInput.ItemsSource = list; + WineRdInput.SelectedIndex = 0; + } else { + WineRdInput.ItemsSource = null; + } + } } }