DeliveryAdminWindow is now able to look at deliveries
This commit is contained in:
@ -2,10 +2,12 @@ using Elwig.Helpers;
|
||||
using Elwig.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
using Xceed.Wpf.Toolkit.Primitives;
|
||||
|
||||
@ -13,20 +15,29 @@ namespace Elwig.Windows {
|
||||
public partial class DeliveryAdminWindow : AdministrationWindow {
|
||||
|
||||
private bool IsUpdatingGradation = false;
|
||||
private bool IsRefreshingInputs = false;
|
||||
private readonly bool IsReceipt = false;
|
||||
private readonly DispatcherTimer Timer;
|
||||
private List<string> TextFilter = new();
|
||||
private readonly RoutedCommand CtrlF = new();
|
||||
|
||||
public DeliveryAdminWindow() {
|
||||
InitializeComponent();
|
||||
CtrlF.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control));
|
||||
CommandBindings.Add(new CommandBinding(CtrlF, FocusSearchInput));
|
||||
RequiredInputs = new Control[] {
|
||||
MgNrInput, MemberInput,
|
||||
SortIdInput, WineVarietyInput,
|
||||
GradationOeInput, GradationKmwInput,
|
||||
WineQualityLevelInput,
|
||||
WineOriginInput, WineKgInput
|
||||
WineOriginInput, WineKgInput,
|
||||
WeightInput
|
||||
};
|
||||
ExemptInputs = new Control[] {
|
||||
SearchInput, TodayOnlyInput, SeasonOnlyInput,
|
||||
DeliveryList, DeliveryPartList,
|
||||
MemberAddressField,
|
||||
LsNrInput, DateInput, TimeInput, BranchInput,
|
||||
MemberAddressField
|
||||
};
|
||||
|
||||
Timer = new DispatcherTimer();
|
||||
@ -34,23 +45,82 @@ namespace Elwig.Windows {
|
||||
Timer.Interval = new TimeSpan(0, 0, 1);
|
||||
}
|
||||
|
||||
public DeliveryAdminWindow(bool receipt) : this() {
|
||||
IsReceipt = receipt;
|
||||
}
|
||||
|
||||
public DeliveryAdminWindow(int mgnr) : this() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs evt) {
|
||||
OnSecondPassed(null, null);
|
||||
Timer.Start();
|
||||
LockInputs();
|
||||
}
|
||||
|
||||
private void OnSecondPassed(object? sender, EventArgs? evt) {
|
||||
var now = DateTime.Now;
|
||||
TimeInput.Text = now.ToString("HH:mm");
|
||||
DateInput.Text = now.ToString("dd.MM.yyyy");
|
||||
if (IsReceipt && (IsEditing || IsCreating)) {
|
||||
var now = DateTime.Now;
|
||||
TimeInput.Text = now.ToString("HH:mm");
|
||||
DateInput.Text = now.ToString("dd.MM.yyyy");
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpdateButtons() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private async Task RefreshDeliveryList() {
|
||||
await RefreshDeliveryListQuery();
|
||||
}
|
||||
|
||||
private async Task RefreshDeliveryListQuery(bool updateSort = false) {
|
||||
IQueryable<Delivery> deliveryQuery = Context.Deliveries;
|
||||
if (TodayOnlyInput.IsChecked == true) {
|
||||
deliveryQuery = deliveryQuery
|
||||
.Where(d => (d.DateString == Utils.Today.ToString("yyyy-MM-dd") && d.TimeString.CompareTo("03:00:00") > 0) ||
|
||||
(d.DateString == Utils.Today.AddDays(1).ToString("yyyy-MM-dd") && d.TimeString.CompareTo("03:00:00") <= 0));
|
||||
} else if (SeasonOnlyInput.IsChecked == true) {
|
||||
deliveryQuery = deliveryQuery.Where(d => d.Year == Utils.CurrentLastSeason);
|
||||
}
|
||||
|
||||
List<Delivery> deliveries = await deliveryQuery.OrderByDescending(d => d.DateString).ThenByDescending(d => d.TimeString).ToListAsync();
|
||||
if (TextFilter.Count > 0) {
|
||||
deliveries = deliveries
|
||||
.ToDictionary(d => d, d => d.SearchScore(TextFilter))
|
||||
.OrderByDescending(a => a.Value)
|
||||
.ThenBy(a => a.Key.DateTime)
|
||||
.Where(a => a.Value > 0)
|
||||
.Select(a => a.Key)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
Utils.RenewItemsSource(DeliveryList, deliveries, d => ((d as Delivery)?.Year, (d as Delivery)?.DId), !updateSort);
|
||||
if (deliveries.Count == 1)
|
||||
DeliveryList.SelectedIndex = 0;
|
||||
|
||||
RefreshInputs();
|
||||
}
|
||||
|
||||
private void RefreshDeliveryPartList(bool updateSort = false) {
|
||||
if (DeliveryList.SelectedItem is not Delivery d) {
|
||||
DeliveryPartList.ItemsSource = null;
|
||||
return;
|
||||
}
|
||||
|
||||
Utils.RenewItemsSource(DeliveryPartList, d.Parts.OrderBy(p => p.DPNr), i => ((i as DeliveryPart)?.Year, (i as DeliveryPart)?.DId, (i as DeliveryPart)?.DPNr));
|
||||
if (d.Parts.Count == 1)
|
||||
DeliveryPartList.SelectedIndex = 0;
|
||||
|
||||
RefreshInputs();
|
||||
}
|
||||
|
||||
protected override async Task RenewContext() {
|
||||
await base.RenewContext();
|
||||
await RefreshDeliveryList();
|
||||
RefreshDeliveryPartList();
|
||||
Utils.RenewItemsSource(MemberInput, await Context.Members.OrderBy(m => m.FamilyName).ThenBy(m => m.GivenName).ToListAsync(), i => (i as Member)?.MgNr);
|
||||
Utils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId);
|
||||
BranchInput.SelectedItem = BranchInput.ItemsSource.Cast<Branch>().First(b => b.ZwstId == App.ZwstId);
|
||||
@ -68,6 +138,85 @@ namespace Elwig.Windows {
|
||||
await UpdateLsNr();
|
||||
}
|
||||
|
||||
private void FocusSearchInput(object sender, RoutedEventArgs evt) {
|
||||
if (!IsEditing && !IsCreating) {
|
||||
SearchInput.Focus();
|
||||
SearchInput.SelectAll();
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshInputs() {
|
||||
IsRefreshingInputs = true;
|
||||
ClearInputStates();
|
||||
if (DeliveryList.SelectedItem is Delivery d) {
|
||||
FillInputs(d);
|
||||
} else {
|
||||
ClearOriginalValues();
|
||||
ClearInputs();
|
||||
}
|
||||
IsRefreshingInputs = false;
|
||||
GC.Collect();
|
||||
}
|
||||
|
||||
private void FillInputs(Delivery d) {
|
||||
ClearOriginalValues();
|
||||
|
||||
MgNrInput.Text = d.MgNr.ToString();
|
||||
LsNrInput.Text = d.LsNr;
|
||||
DateInput.Text = d.Date.ToString("dd.MM.yyyy");
|
||||
TimeInput.Text = d.Time.ToString("HH:mm");
|
||||
CommentInput.Text = d.Comment ?? "";
|
||||
Utils.RenewItemsSource(DeliveryPartList, d.Parts, i => ((i as DeliveryPart)?.Year, (i as DeliveryPart)?.DId));
|
||||
if (DeliveryPartList.SelectedItem == null && DeliveryPartList.ItemsSource != null) {
|
||||
DeliveryPartList.SelectedIndex = 0;
|
||||
}
|
||||
|
||||
var p = DeliveryPartList.SelectedItem as DeliveryPart;
|
||||
SortIdInput.Text = p?.SortId ?? "";
|
||||
AttributesInput.SelectedItems.Clear();
|
||||
foreach (var a in p?.Attributes ?? Array.Empty<WineAttr>())
|
||||
AttributesInput.SelectedItems.Add(AttributesInput.ItemsSource.Cast<WineAttr>().Where(i => i.AttrId == a.AttrId).First());
|
||||
GradationKmwInput.Text = p?.Kmw.ToString() ?? "";
|
||||
WineQualityLevelInput.SelectedItem = p?.Quality ?? null;
|
||||
WeightInput.Text = p?.Weight.ToString() ?? "";
|
||||
ManualWeighingInput.IsChecked = p?.ManualWeighing ?? false;
|
||||
GerebeltGewogenInput.IsChecked = p?.IsGerebelt ?? false;
|
||||
ModifiersInput.SelectedItems.Clear();
|
||||
foreach (var m in p?.Modifiers ?? Array.Empty<Modifier>())
|
||||
ModifiersInput.SelectedItems.Add(ModifiersInput.ItemsSource.Cast<Modifier>().Where(i => i.ModId == m.ModId).First());
|
||||
PartCommentInput.Text = p?.Comment ?? "";
|
||||
TemperatureInput.Text = p?.Temperature?.ToString() ?? "";
|
||||
AcidInput.Text = p?.Acid?.ToString() ?? "";
|
||||
LesewagenInput.IsChecked = p?.IsLesewagen ?? false;
|
||||
HandPickedInput.IsChecked = p?.IsHandPicked;
|
||||
|
||||
FillOriginalValues();
|
||||
}
|
||||
|
||||
private async void SearchInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
TextFilter = SearchInput.Text.ToLower().Split(" ").ToList().FindAll(e => e.Length > 0);
|
||||
await RefreshDeliveryListQuery(true);
|
||||
}
|
||||
|
||||
private async void TodayOnlyInput_Changed(object sender, RoutedEventArgs evt) {
|
||||
if (TodayOnlyInput.IsChecked == true && SeasonOnlyInput.IsChecked == true) SeasonOnlyInput.IsChecked = false;
|
||||
await RefreshDeliveryListQuery();
|
||||
}
|
||||
|
||||
private async void SeasonOnlyInput_Changed(object sender, RoutedEventArgs evt) {
|
||||
if (!IsInitialized) return;
|
||||
if (SeasonOnlyInput.IsChecked == true && TodayOnlyInput.IsChecked == true) TodayOnlyInput.IsChecked = false;
|
||||
await RefreshDeliveryListQuery();
|
||||
}
|
||||
|
||||
private void DeliveryList_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
RefreshInputs();
|
||||
}
|
||||
|
||||
private void DeliveryPartList_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
if (!IsRefreshingInputs) RefreshInputs();
|
||||
}
|
||||
|
||||
private void MgNrInput_TextChanged(object sender, TextChangedEventArgs evt) {
|
||||
var valid = InputTextChanged((TextBox)sender, Validator.CheckMgNr);
|
||||
MemberInput.SelectedItem = valid ? Context.Members.Find(int.Parse(MgNrInput.Text)) : null;
|
||||
@ -138,7 +287,7 @@ namespace Elwig.Windows {
|
||||
}
|
||||
var kmw = double.Parse(GradationKmwInput.Text);
|
||||
WineQualityLevelInput.ItemsSource = Context.WineQualityLevels.Where(q => q.MinKmw == null || q.MinKmw <= kmw).ToList();
|
||||
var qual = Context.WineQualityLevels.Where(q => !q.IsPredicate && (q.MinKmw == null || q.MinKmw <= kmw)).OrderBy(q => q.MinKmw).LastOrDefault();
|
||||
var qual = Context.GetWineQualityLevel(kmw).GetAwaiter().GetResult();
|
||||
SetOriginalValue(WineQualityLevelInput, qual);
|
||||
if (WineQualityLevelInput.SelectedItem == null || (WineQualityLevelInput.SelectedItem is WineQualLevel selected && !selected.IsPredicate)) {
|
||||
WineQualityLevelInput.SelectedItem = qual;
|
||||
@ -202,7 +351,7 @@ namespace Elwig.Windows {
|
||||
var qual = WineQualityLevelInput.SelectedItem as WineQualLevel;
|
||||
var kg = (WineKgInput.SelectedItem as AT_Kg)?.WbKg;
|
||||
if (qual == null || kg == null) {
|
||||
WineOriginInput.IsEnabled = true;
|
||||
WineOriginInput.IsEnabled = (IsEditing || IsCreating);
|
||||
return;
|
||||
}
|
||||
WineOriginInput.IsEnabled = false;
|
||||
@ -213,6 +362,7 @@ namespace Elwig.Windows {
|
||||
|
||||
private void WineQualityLevelInput_SelectionChanged(object sender, SelectionChangedEventArgs evt) {
|
||||
UpdateWineOrigin();
|
||||
UpdateAbgewertet();
|
||||
}
|
||||
|
||||
private void UpdateRdInput() {
|
||||
@ -221,7 +371,7 @@ namespace Elwig.Windows {
|
||||
list.Insert(0, new NullItem());
|
||||
Utils.RenewItemsSource(WineRdInput, list, i => ((i as WbRd)?.KgNr, (i as WbRd)?.RdNr));
|
||||
if (WineRdInput.SelectedItem == null) WineRdInput.SelectedIndex = 0;
|
||||
WineRdInput.IsEnabled = list.Count > 1;
|
||||
WineRdInput.IsEnabled = (IsEditing || IsCreating) && list.Count > 1;
|
||||
} else {
|
||||
WineRdInput.ItemsSource = null;
|
||||
WineRdInput.IsEnabled = false;
|
||||
@ -232,5 +382,15 @@ namespace Elwig.Windows {
|
||||
UpdateWineOrigin();
|
||||
UpdateRdInput();
|
||||
}
|
||||
|
||||
private void UpdateAbgewertet() {
|
||||
var qual = WineQualityLevelInput.SelectedItem as WineQualLevel;
|
||||
if (qual == null) {
|
||||
AbgewertetInput.IsChecked = false;
|
||||
return;
|
||||
}
|
||||
var defQual = Context.GetWineQualityLevel(double.Parse(GradationKmwInput.Text)).GetAwaiter().GetResult();
|
||||
AbgewertetInput.IsChecked = !qual.IsPredicate && defQual != qual;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user