diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index 309c313..ceddd24 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -272,7 +272,7 @@ namespace Elwig { } public static AreaComAdminWindow FocusMemberAreaComs(int mgnr) { - return FocusWindow(() => new(mgnr), w => w.MgNr == mgnr); + return FocusWindow(() => new(mgnr), w => w.ViewModel.Member.MgNr == mgnr); } public static BaseDataWindow FocusBaseData() { diff --git a/Elwig/Services/AreaComService.cs b/Elwig/Services/AreaComService.cs new file mode 100644 index 0000000..0ae17cc --- /dev/null +++ b/Elwig/Services/AreaComService.cs @@ -0,0 +1,127 @@ +using Elwig.Helpers; +using Elwig.Models.Entities; +using Elwig.ViewModels; +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Elwig.Services { + public static class AreaComService { + + public static async Task InitInputs(this AreaComAdminViewModel vm) { + using var ctx = new AppDbContext(); + vm.FbNr = await ctx.NextFbNr(); + vm.MgNr = vm.Member.MgNr; + vm.YearFrom = Utils.CurrentYear; + vm.WineCult = null; + } + + public static void ClearInputs(this AreaComAdminViewModel vm) { + } + + public static void FillInputs(this AreaComAdminViewModel vm, AreaCom a) { + vm.FbNr = a.FbNr; + vm.MgNr = a.MgNr; + vm.YearFrom = a.YearFrom; + vm.YearTo = a.YearTo; + vm.AreaComType = ControlUtils.GetItemFromSourceWithPk(vm.AreaComTypeSource, a.VtrgId) as AreaComType; + vm.WineCult = ControlUtils.GetItemFromSourceWithPk(vm.WineCultSource, a.CultId) as WineCult; + vm.Comment = a.Comment; + vm.Kg = ControlUtils.GetItemFromSourceWithPk(vm.KgSource, a.KgNr) as AT_Kg; + vm.Rd = ControlUtils.GetItemFromSourceWithPk(vm.RdSource, a.KgNr, a.RdNr) as WbRd; + vm.GstNr = a.GstNr; + vm.Area = a.Area; + } + + public static async Task<(List, IQueryable, List)> GetFilters(this AreaComAdminViewModel vm, AppDbContext ctx) { + List filterNames = []; + IQueryable areaComQuery = ctx.AreaCommitments.Where(a => a.MgNr == vm.Member.MgNr).OrderBy(a => a.FbNr); + if (vm.ShowOnlyActiveAreaComs) { + areaComQuery = Utils.ActiveAreaCommitments(areaComQuery); + filterNames.Add("laufend"); + } + + var filterVar = new List(); + var filterNotVar = new List(); + var filterAttr = new List(); + var filterNotAttr = new List(); + + var filter = vm.TextFilter; + if (filter.Count > 0) { + var var = await ctx.WineVarieties.ToDictionaryAsync(v => v.SortId, v => v); + var attr = await ctx.WineAttributes.ToDictionaryAsync(a => a.Name.ToLower().Split(" ")[0], a => a); + + for (int i = 0; i < filter.Count; i++) { + var e = filter[i]; + if (e.Length == 2 && var.ContainsKey(e.ToUpper())) { + filterVar.Add(e.ToUpper()); + filter.RemoveAt(i--); + filterNames.Add(var[e.ToUpper()].Name); + } else if (e.Length == 3 && e[0] == '!' && var.ContainsKey(e[1..].ToUpper())) { + filterNotVar.Add(e[1..].ToUpper()); + filter.RemoveAt(i--); + } else if (attr.ContainsKey(e.ToLower())) { + var a = attr[e.ToLower()]; + filterAttr.Add(a.AttrId); + filter.RemoveAt(i--); + filterNames.Add($"Attribut {a.Name}"); + } else if (e[0] == '!' && attr.ContainsKey(e[1..].ToLower())) { + var a = attr[e[1..].ToLower()]; + filterNotAttr.Add(a.AttrId); + filter.RemoveAt(i--); + filterNames.Add($"ohne Attribut {a.Name}"); + } + } + + if (filterVar.Count > 0) areaComQuery = areaComQuery.Where(a => filterVar.Contains(a.AreaComType.WineVar.SortId)); + if (filterNotVar.Count > 0) areaComQuery = areaComQuery.Where(a => !filterNotVar.Contains(a.AreaComType.WineVar.SortId)); + if (filterAttr.Count > 0) areaComQuery = areaComQuery.Where(a => a.AreaComType.WineAttr != null && a.AreaComType.WineAttr.AttrId != null && filterAttr.Contains(a.AreaComType.WineAttr.AttrId)); + if (filterNotAttr.Count > 0) areaComQuery = areaComQuery.Where(a => a.AreaComType.WineAttr == null || a.AreaComType.WineAttr.AttrId == null || !filterAttr.Contains(a.AreaComType.WineAttr.AttrId)); + } + + return (filterNames, areaComQuery, filter); + } + + public static async Task UpdateAreaCommitment(this AreaComAdminViewModel vm, int? oldFbNr) { + using var ctx = new AppDbContext(); + int newFbNr = (int)vm.FbNr!; + + var a = new AreaCom { + FbNr = oldFbNr ?? newFbNr, + MgNr = (int)vm.MgNr!, + YearFrom = (int)vm.YearFrom!, + YearTo = vm.YearTo, + VtrgId = vm.AreaComType!.VtrgId, + CultId = vm.WineCult?.CultId, + Comment = string.IsNullOrEmpty(vm.Comment) ? null : vm.Comment, + KgNr = vm.Kg!.KgNr, + RdNr = vm.Rd?.RdNr, + GstNr = vm.GstNr!.Trim(), + Area = (int)vm.Area!, + }; + + if (vm.Rd?.RdNr == 0) { + vm.Rd.RdNr = await ctx.NextRdNr(a.KgNr); + a.RdNr = vm.Rd.RdNr; + ctx.Add(vm.Rd); + } + + if (oldFbNr != null) { + ctx.Update(a); + } else { + ctx.Add(a); + } + + await ctx.SaveChangesAsync(); + + if (newFbNr != a.FbNr) { + await ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment SET fbnr = {newFbNr} WHERE fbnr = {oldFbNr}"); + } + + await App.HintContextChange(); + + return newFbNr; + } + } +} diff --git a/Elwig/ViewModels/AreaComAdminViewModel.cs b/Elwig/ViewModels/AreaComAdminViewModel.cs new file mode 100644 index 0000000..f7918e4 --- /dev/null +++ b/Elwig/ViewModels/AreaComAdminViewModel.cs @@ -0,0 +1,89 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using Elwig.Models.Entities; +using System.Collections.Generic; +using System.Linq; + +namespace Elwig.ViewModels { + public partial class AreaComAdminViewModel : ObservableObject { + + [ObservableProperty] + private string? _searchQuery = ""; + public List TextFilter { + get => [.. SearchQuery?.ToLower().Split(' ').ToList().FindAll(e => e.Length > 0)]; + set => SearchQuery = string.Join(' ', value.Where(e => e.Length > 0)); + } + + public required Member Member { get; set; } + + [ObservableProperty] + private bool _showOnlyActiveAreaComs; + + [ObservableProperty] + private string? _fbNrString; + public int? FbNr { + get => int.TryParse(FbNrString, out var mgnr) ? mgnr : null; + set => FbNrString = $"{value}"; + } + [ObservableProperty] + private string? _mgNrString; + public int? MgNr { + get => int.TryParse(MgNrString, out var mgnr) ? mgnr : null; + set => MgNrString = $"{value}"; + } + [ObservableProperty] + private string? _yearFromString; + public int? YearFrom { + get => int.TryParse(YearFromString, out var year) ? year : null; + set => YearFromString = $"{value}"; + } + [ObservableProperty] + private string? _yearToString; + public int? YearTo { + get => int.TryParse(YearToString, out var year) ? year : null; + set => YearToString = $"{value}"; + } + + [ObservableProperty] + private AreaComType? _areaComType; + [ObservableProperty] + private IEnumerable _areaComTypeSource = []; + [ObservableProperty] + private object? _wineCultObj; + public WineCult? WineCult { + get => WineCultObj as WineCult; + set => WineCultObj = value ?? WineCultSource.FirstOrDefault(); + } + [ObservableProperty] + private IEnumerable _wineCultSource = []; + [ObservableProperty] + private string? _comment; + + [ObservableProperty] + private AT_Kg? _kg; + [ObservableProperty] + private IEnumerable _kgSource = []; + [ObservableProperty] + private object? _rdObj; + public WbRd? Rd { + get => RdObj as WbRd; + set => RdObj = value ?? RdSource.FirstOrDefault(); + } + [ObservableProperty] + private IEnumerable _rdSource = []; + [ObservableProperty] + private string? _gstNr; + [ObservableProperty] + private string? _areaString; + public int? Area { + get => int.TryParse(AreaString, out var area) ? area : null; + set => AreaString = $"{value}"; + } + + [ObservableProperty] + private string _statusAreaCommitments = "Flächenbindungen: -"; + [ObservableProperty] + private string _statusArea = "Fläche: -"; + [ObservableProperty] + private string _statusContracts = "Vertragsarten: -"; + } +} diff --git a/Elwig/Windows/AreaComAdminWindow.xaml b/Elwig/Windows/AreaComAdminWindow.xaml index b3df4ab..9dae5d5 100644 --- a/Elwig/Windows/AreaComAdminWindow.xaml +++ b/Elwig/Windows/AreaComAdminWindow.xaml @@ -5,9 +5,12 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Elwig.Windows" xmlns:ctrl="clr-namespace:Elwig.Controls" - mc:Ignorable="d" + xmlns:vm="clr-namespace:Elwig.ViewModels" Title="Flächenbindungen - Elwig" Height="500" MinHeight="440" Width="920" MinWidth="860" Loaded="Window_Loaded"> + + +