From fd26ee4f52156408a594d75ec1c9ecaa041bd368 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 13 Jul 2023 21:10:48 +0200 Subject: [PATCH] DeliveryAdminWindow is now able to look at deliveries --- Elwig/Helpers/AppDbContext.cs | 7 + Elwig/Helpers/Utils.cs | 34 ++++- Elwig/Models/Delivery.cs | 22 +++ Elwig/Models/Member.cs | 32 +--- Elwig/Windows/AdministrationWindow.cs | 6 + Elwig/Windows/DeliveryAdminWindow.xaml | 168 ++++++++++++++++++--- Elwig/Windows/DeliveryAdminWindow.xaml.cs | 176 +++++++++++++++++++++- 7 files changed, 390 insertions(+), 55 deletions(-) diff --git a/Elwig/Helpers/AppDbContext.cs b/Elwig/Helpers/AppDbContext.cs index c514056..d83edd9 100644 --- a/Elwig/Helpers/AppDbContext.cs +++ b/Elwig/Helpers/AppDbContext.cs @@ -119,5 +119,12 @@ namespace Elwig.Helpers { .ForEach(a => { if (a <= c + 10) c = a; }); return c + 1; } + + public async Task GetWineQualityLevel(double kmw) { + return await WineQualityLevels + .Where(q => !q.IsPredicate && (q.MinKmw == null || q.MinKmw <= kmw)) + .OrderBy(q => q.MinKmw) + .LastOrDefaultAsync(); + } } } diff --git a/Elwig/Helpers/Utils.cs b/Elwig/Helpers/Utils.cs index 9316bd1..b73c085 100644 --- a/Elwig/Helpers/Utils.cs +++ b/Elwig/Helpers/Utils.cs @@ -15,7 +15,9 @@ using System.Collections; namespace Elwig.Helpers { public static partial class Utils { - public static int CurrentSeason => DateTime.Now.Year - (DateTime.Now.Month <= 3 ? 1 : 0); + public static int CurrentNextSeason => DateTime.Now.Year - (DateTime.Now.Month <= 3 ? 1 : 0); + public static int CurrentLastSeason => DateTime.Now.Year - (DateTime.Now.Month <= 7 ? 1 : 0); + public static DateTime Today => (DateTime.Now.Hour >= 3) ? DateTime.Today : DateTime.Today.AddDays(-1); public static readonly Regex SerialRegex = GeneratedSerialRegex(); public static readonly Regex TcpRegex = GeneratedTcpRegex(); @@ -164,6 +166,13 @@ namespace Elwig.Helpers { dataGrid.CurrentCell = new(dataGrid.SelectedItem, column); } + public static void RenewItemsSource(ListBox listBox, IEnumerable? source, Func getId) { + var selectedId = getId(listBox.SelectedItem); + listBox.ItemsSource = source; + if (selectedId != null && source != null) + listBox.SelectedItem = source.Cast().FirstOrDefault(i => selectedId.Equals(getId(i))); + } + public static int Modulo(string a, int b) { if (a.Length == 0 || !a.All(char.IsAsciiDigit)) { throw new ArgumentException("First argument has to be a decimal string"); @@ -214,5 +223,28 @@ namespace Elwig.Helpers { var b = (birthday.Year * 100 + birthday.Month) * 100 + birthday.Day; return (a - b) / 10000; } + + public static int GetSearchScore(IEnumerable words, IEnumerable searchKeywords) { + searchKeywords = searchKeywords.Where(s => s.Length >= 2 || (s.Length > 0 && s.All(c => char.IsAsciiDigit(c)))); + if (!searchKeywords.Any()) + return 0; + + words = words.Select(w => w?.ToLower()); + int i = 0; + foreach (string? c in words) { + if (c == null) continue; + var parts = c.Split(" "); + if (searchKeywords.Any(f => c == f)) { + i += 100; + } else if (searchKeywords.Any(f => parts.Any(a => a == f))) { + i += 90; + } else if (searchKeywords.Any(f => parts.Any(a => a.StartsWith(f)))) { + i += 50; + } else if (searchKeywords.Any(f => f != null && c.Contains(f))) { + i += 1; + } + } + return i; + } } } diff --git a/Elwig/Models/Delivery.cs b/Elwig/Models/Delivery.cs index 9ee5f06..c3d249b 100644 --- a/Elwig/Models/Delivery.cs +++ b/Elwig/Models/Delivery.cs @@ -1,7 +1,9 @@ +using Elwig.Helpers; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; namespace Elwig.Models { [Table("delivery"), PrimaryKey("Year", "DId"), Index("DateString", "ZwstId", "LNr", IsUnique = true), Index("LsNr", IsUnique = true)] @@ -72,5 +74,25 @@ namespace Elwig.Models { [InverseProperty("Delivery")] public virtual ISet Parts { get; private set; } + + public int Weight => Parts.Select(p => p.Weight).Sum(); + + public IEnumerable SortIds => Parts + .GroupBy(p => p.SortId) + .OrderByDescending(g => g.Select(p => p.Weight).Sum()) + .Select(g => g.Select(p => p.SortId).First()); + + public string SortIdString => string.Join(", ", SortIds); + + public int SearchScore(IEnumerable keywords) { + var list = new string?[] { + LsNr, Year.ToString(), Date.ToString("dd.MM.yyyy"), Time.ToString("HH:ss"), + MgNr.ToString(), Member.FamilyName, Member.MiddleName, Member.GivenName, Member.BillingAddress?.Name, + Comment + }.ToList(); + list.AddRange(Parts.Select(p => p.SortId).Distinct()); + list.AddRange(Parts.Select(p => p.Comment).Distinct()); + return Utils.GetSearchScore(list, keywords); + } } } diff --git a/Elwig/Models/Member.cs b/Elwig/Models/Member.cs index 64a55ba..cd89124 100644 --- a/Elwig/Models/Member.cs +++ b/Elwig/Models/Member.cs @@ -160,7 +160,7 @@ namespace Elwig.Models { [NotMapped] public IEnumerable ActiveAreaCommitments => AreaCommitments - .Where(c => c.YearFrom <= Utils.CurrentSeason && (c.YearTo ?? int.MaxValue) >= Utils.CurrentSeason); + .Where(c => c.YearFrom <= Utils.CurrentNextSeason && (c.YearTo ?? int.MaxValue) >= Utils.CurrentNextSeason); [InverseProperty("Member")] public virtual BillingAddr? BillingAddress { get; private set; } @@ -174,32 +174,12 @@ namespace Elwig.Models { public string FullAddress => $"{Address}, {PostalDest.AtPlz.Plz} {PostalDest.AtPlz.Ort.Name}"; public int SearchScore(IEnumerable keywords) { - keywords = keywords.Where(s => s.Length >= 2 || (s.Length > 0 && s.All(c => char.IsAsciiDigit(c)))); - if (!keywords.Any()) - return 0; - - string?[] check = new string?[] { + return Utils.GetSearchScore(new string?[] { MgNr.ToString(), - FamilyName.ToLower(), MiddleName?.ToLower(), GivenName.ToLower(), - BillingAddress?.Name.ToLower(), - Comment?.ToLower(), - }; - - int i = 0; - foreach (string? c in check) { - if (c == null) continue; - var parts = c.Split(" "); - if (keywords.Any(f => c == f)) { - i += 100; - } else if (keywords.Any(f => parts.Any(a => a == f))) { - i += 90; - } else if (keywords.Any(f => parts.Any(a => a.StartsWith(f)))) { - i += 50; - } else if (keywords.Any(f => f != null && c.Contains(f))) { - i += 1; - } - } - return i; + FamilyName, MiddleName, GivenName, + BillingAddress?.Name, + Comment, + }, keywords); } } } diff --git a/Elwig/Windows/AdministrationWindow.cs b/Elwig/Windows/AdministrationWindow.cs index 9fe47cd..f65d803 100644 --- a/Elwig/Windows/AdministrationWindow.cs +++ b/Elwig/Windows/AdministrationWindow.cs @@ -106,6 +106,7 @@ namespace Elwig.Windows { Utils.ClearInputState(tb); foreach (var cb in ComboBoxInputs) Utils.ClearInputState(cb); + // TODO ComboCheckBox foreach (var cb in CheckBoxInputs) Utils.ClearInputState(cb); foreach (var rb in RadioButtonInputs) @@ -128,6 +129,8 @@ namespace Elwig.Windows { tb.IsReadOnly = true; foreach (var cb in ComboBoxInputs) cb.IsEnabled = false; + foreach (var ccb in CheckComboBoxInputs) + ccb.IsEnabled = false; foreach (var cb in CheckBoxInputs) cb.IsEnabled = false; foreach (var rb in RadioButtonInputs) @@ -139,6 +142,8 @@ namespace Elwig.Windows { tb.IsReadOnly = false; foreach (var cb in ComboBoxInputs) cb.IsEnabled = true; + foreach (var ccb in CheckComboBoxInputs) + ccb.IsEnabled = true; foreach (var cb in CheckBoxInputs) cb.IsEnabled = true; foreach (var rb in RadioButtonInputs) @@ -154,6 +159,7 @@ namespace Elwig.Windows { OriginalValues[tb] = tb.Text; foreach (var cb in ComboBoxInputs) OriginalValues[cb] = cb.SelectedItem; + // TODO ComboCheckBox foreach (var cb in CheckBoxInputs) OriginalValues[cb] = (cb.IsChecked ?? false) ? bool.TrueString : null; foreach (var rb in RadioButtonInputs) diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml b/Elwig/Windows/DeliveryAdminWindow.xaml index 376a55f..f6d3497 100644 --- a/Elwig/Windows/DeliveryAdminWindow.xaml +++ b/Elwig/Windows/DeliveryAdminWindow.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Elwig.Windows" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" - Title="Lieferungsverwaltung - Elwig" Height="600" Width="1000" MinHeight="400" MinWidth="800" + Title="Lieferungsverwaltung - Elwig" Height="700" Width="1100" MinHeight="700" MinWidth="1000" Loaded="Window_Loaded"> + + + + + +