Elwig: Rename AreaCom to AreaCmt
This commit is contained in:
@@ -0,0 +1,275 @@
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Models.Entities;
|
||||
using Elwig.ViewModels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Elwig.Services {
|
||||
public static class AreaCmtService {
|
||||
|
||||
public static async Task InitInputs(this AreaCmtAdminViewModel vm) {
|
||||
using var ctx = new AppDbContext();
|
||||
vm.FbNr = await ctx.NextFbNr();
|
||||
vm.MgNr = vm.FilterMember.MgNr;
|
||||
vm.YearFrom = Utils.CurrentYear;
|
||||
vm.WineCult = null;
|
||||
}
|
||||
|
||||
public static void ClearInputs(this AreaCmtAdminViewModel vm) {
|
||||
vm.Period = null;
|
||||
}
|
||||
|
||||
public static void FillInputs(this AreaCmtAdminViewModel vm, AreaCmtContract c) {
|
||||
vm.FbNr = c.FbNr;
|
||||
vm.Period = c.YearTo == null ? $"ab {c.YearFrom}" : $"{c.YearFrom}\u2013{c.YearTo}";
|
||||
vm.Comment = c.Comment;
|
||||
vm.Kg = ControlUtils.GetItemFromSourceWithPk(vm.KgSource, c.KgNr) as AT_Kg;
|
||||
vm.Rd = ControlUtils.GetItemFromSourceWithPk(vm.RdSource, c.KgNr, c.RdNr) as WbRd;
|
||||
}
|
||||
|
||||
public static void FillInputs(this AreaCmtAdminViewModel vm, AreaCmt a) {
|
||||
vm.MgNr = a.MgNr;
|
||||
vm.YearFrom = a.YearFrom;
|
||||
vm.YearTo = a.YearTo;
|
||||
vm.AreaCmtType = ControlUtils.GetItemFromSourceWithPk(vm.AreaCmtTypeSource, a.VtrgId) as AreaCmtType;
|
||||
vm.WineCult = ControlUtils.GetItemFromSourceWithPk(vm.WineCultSource, a.CultId) as WineCult;
|
||||
vm.GstNr = a.GstNr;
|
||||
vm.Area = a.Area;
|
||||
}
|
||||
|
||||
public static async Task<(List<string>, IQueryable<AreaCmtContract>, IQueryable<AreaCmt>, List<string>)> GetFilters(this AreaCmtAdminViewModel vm, AppDbContext ctx) {
|
||||
List<string> filterNames = [];
|
||||
IQueryable<AreaCmt> areaCmtQuery = ctx.AreaCommitments.Where(a => a.MgNr == vm.FilterMember.MgNr).OrderBy(a => a.FbNr);
|
||||
filterNames.Add(vm.FilterMember.AdministrativeName);
|
||||
|
||||
if (vm.FilterSeason is int season) {
|
||||
areaCmtQuery = Utils.ActiveAreaCommitments(areaCmtQuery, season);
|
||||
filterNames.Add($"laufend {season}");
|
||||
}
|
||||
|
||||
var filterVar = new List<string>();
|
||||
var filterNotVar = new List<string>();
|
||||
var filterAttr = new List<string>();
|
||||
var filterNotAttr = new List<string>();
|
||||
|
||||
var filter = vm.TextFilter;
|
||||
if (filter.Count > 0) {
|
||||
var var = await ctx.FetchWineVarieties().ToDictionaryAsync(v => v.SortId, v => v);
|
||||
var attrId = await ctx.FetchWineAttributes().ToDictionaryAsync(a => a.AttrId, a => a);
|
||||
var attr = attrId.Values.ToDictionary(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--);
|
||||
filterNames.Add($"ohne {var[e.ToUpper()].Name}");
|
||||
} 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}");
|
||||
} else if (e.Length > 2 && var.ContainsKey(e.ToUpper()[..2]) && attrId.ContainsKey(e[2..].ToUpper())) {
|
||||
filterVar.Add(e[..2].ToUpper());
|
||||
filterAttr.Add(e[2..].ToUpper());
|
||||
filter.RemoveAt(i--);
|
||||
filterNames.Add(var[e[..2].ToUpper()].Name);
|
||||
filterNames.Add($"Attribut {attrId[e[2..].ToUpper()].Name}");
|
||||
} else if (e[0] == '!' && e.Length > 3 && var.ContainsKey(e.ToUpper()[1..3]) && attrId.ContainsKey(e[3..].ToUpper())) {
|
||||
filterNotVar.Add(e[1..3].ToUpper());
|
||||
filterNotAttr.Add(e[3..].ToUpper());
|
||||
filter.RemoveAt(i--);
|
||||
filterNames.Add($"ohne {var[e[1..3].ToUpper()].Name}");
|
||||
filterNames.Add($"ohne Attribut {attrId[e[3..].ToUpper()].Name}");
|
||||
}
|
||||
}
|
||||
|
||||
if (filterVar.Count > 0) areaCmtQuery = areaCmtQuery.Where(a => filterVar.Contains(a.AreaCmtType.WineVar.SortId));
|
||||
if (filterNotVar.Count > 0) areaCmtQuery = areaCmtQuery.Where(a => !filterNotVar.Contains(a.AreaCmtType.WineVar.SortId));
|
||||
if (filterAttr.Count > 0) areaCmtQuery = areaCmtQuery.Where(a => a.AreaCmtType.WineAttr!.AttrId != null && filterAttr.Contains(a.AreaCmtType.WineAttr.AttrId));
|
||||
if (filterNotAttr.Count > 0) areaCmtQuery = areaCmtQuery.Where(a => a.AreaCmtType.WineAttr!.AttrId == null || !filterNotAttr.Contains(a.AreaCmtType.WineAttr.AttrId));
|
||||
}
|
||||
|
||||
IQueryable<AreaCmtContract> contracts = areaCmtQuery
|
||||
.Select(c => c.Contract).Distinct()
|
||||
.OrderBy(c => c.FbNr);
|
||||
|
||||
return (filterNames, contracts, areaCmtQuery, filter);
|
||||
}
|
||||
|
||||
public static async Task<(int FbNr, int RevNr)> UpdateAreaCommitment(this AreaCmtAdminViewModel vm, int? oldFbNr, int? revNr) {
|
||||
int newFbNr = vm.FbNr!.Value;
|
||||
|
||||
return await Task.Run(async () => {
|
||||
using var ctx = new AppDbContext();
|
||||
var c = new AreaCmtContract {
|
||||
FbNr = oldFbNr ?? newFbNr,
|
||||
Comment = string.IsNullOrWhiteSpace(vm.Comment) ? null : vm.Comment,
|
||||
KgNr = vm.Kg!.KgNr,
|
||||
RdNr = vm.Rd?.RdNr,
|
||||
};
|
||||
var a = new AreaCmt {
|
||||
FbNr = oldFbNr ?? newFbNr,
|
||||
RevNr = revNr ?? await ctx.NextRevNr(oldFbNr ?? newFbNr),
|
||||
MgNr = vm.MgNr!.Value,
|
||||
YearFrom = vm.YearFrom,
|
||||
YearTo = vm.YearTo,
|
||||
VtrgId = vm.AreaCmtType!.VtrgId,
|
||||
CultId = vm.WineCult?.CultId,
|
||||
GstNr = vm.GstNr?.Trim() ?? "-",
|
||||
Area = vm.Area!.Value,
|
||||
};
|
||||
|
||||
if (vm.Rd?.RdNr == 0) {
|
||||
vm.Rd.RdNr = await ctx.NextRdNr(c.KgNr);
|
||||
c.RdNr = vm.Rd.RdNr;
|
||||
ctx.Add(vm.Rd);
|
||||
}
|
||||
|
||||
if (oldFbNr != null) {
|
||||
ctx.Update(c);
|
||||
} else {
|
||||
ctx.Add(c);
|
||||
}
|
||||
if (revNr != null) {
|
||||
ctx.Update(a);
|
||||
} else {
|
||||
ctx.Add(a);
|
||||
}
|
||||
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
if (newFbNr != a.FbNr) {
|
||||
await ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment_contract SET fbnr = {newFbNr} WHERE fbnr = {oldFbNr}");
|
||||
}
|
||||
|
||||
return (newFbNr, a.RevNr);
|
||||
});
|
||||
}
|
||||
|
||||
private static void AddToolTipRow(Grid grid, int row, string? h1, string? h2, int area, int? min, int? max) {
|
||||
var bold = h2 == null;
|
||||
if (h1 != null) grid.AddToolTipCell(h1 + ":", row, 0, (h2 == null) ? 2 : 1, bold);
|
||||
if (h2 != null) grid.AddToolTipCell(h2 + ":", row, 1, bold: bold);
|
||||
grid.AddToolTipCell($"{area:N0} m²", row, 2, bold: bold, alignRight: true);
|
||||
grid.AddToolTipCell(min == null ? "" : $"{min:N0} kg", row, 3, bold: bold, alignRight: true);
|
||||
grid.AddToolTipCell(max == null ? "" : $"{max:N0} kg", row, 4, bold: bold, alignRight: true);
|
||||
}
|
||||
|
||||
public static async Task<(string, (string?, string?, int, int?, int?)[])> GenerateToolTipData(IQueryable<AreaCmt> areaCmts, int maxKgPerHa) {
|
||||
var grid = new List<(string?, string?, int, int?, int?)>();
|
||||
var text = "-";
|
||||
|
||||
var area = await areaCmts.SumAsync(p => p.Area);
|
||||
text = $"{area:N0} m²";
|
||||
grid.Add(("Geb. Fläche", null, area, null, null));
|
||||
|
||||
if (await areaCmts.AnyAsync()) {
|
||||
var attrGroups = await areaCmts
|
||||
.Where(c => c.AreaCmtType.WineAttr != null)
|
||||
.GroupBy(c => c.AreaCmtType.WineAttr!.Name)
|
||||
.Select(g => new {
|
||||
Attr = g.Key,
|
||||
Area = g.Sum(c => c.Area),
|
||||
Min = g.Sum(c => c.Area * (c.AreaCmtType.MinKgPerHa ?? 0) / 10_000),
|
||||
Max = g.Sum(c => c.Area * (c.AreaCmtType.WineAttr!.MaxKgPerHa ?? maxKgPerHa) / 10_000),
|
||||
})
|
||||
.OrderByDescending(g => g.Area)
|
||||
.ThenBy(g => g.Attr)
|
||||
.ToListAsync();
|
||||
var groups = await areaCmts
|
||||
.Where(c => c.AreaCmtType.WineAttr != null)
|
||||
.GroupBy(c => new {
|
||||
Attr = c.AreaCmtType.WineAttr!.Name,
|
||||
c.AreaCmtType.SortId,
|
||||
})
|
||||
.Select(g => new {
|
||||
g.Key.Attr,
|
||||
g.Key.SortId,
|
||||
Area = g.Sum(c => c.Area),
|
||||
Min = g.Sum(c => c.Area * (c.AreaCmtType.MinKgPerHa ?? 0) / 10_000),
|
||||
Max = g.Sum(c => c.Area * (c.AreaCmtType.WineAttr!.MaxKgPerHa ?? maxKgPerHa) / 10_000),
|
||||
})
|
||||
.OrderByDescending(g => g.Area)
|
||||
.ThenBy(g => g.Attr)
|
||||
.ThenBy(g => g.SortId)
|
||||
.ToListAsync();
|
||||
|
||||
var noAttr = await areaCmts
|
||||
.Where(c => c.AreaCmtType.WineAttr == null || !c.AreaCmtType.WineAttr.IsStrict)
|
||||
.GroupBy(c => c.AreaCmtType.SortId)
|
||||
.Select(g => new {
|
||||
SortId = g.Key,
|
||||
Area = g.Sum(c => c.Area),
|
||||
Min = g.Sum(c => c.Area * (c.AreaCmtType.MinKgPerHa ?? 0) / 10_000),
|
||||
Max = g.Sum(c => c.Area * (c.AreaCmtType.WineAttr!.MaxKgPerHa ?? maxKgPerHa) / 10_000),
|
||||
})
|
||||
.OrderByDescending(g => g.Area)
|
||||
.ThenBy(g => g.SortId)
|
||||
.ToListAsync();
|
||||
|
||||
if (noAttr.Count > 0) {
|
||||
grid.Add((null, null, noAttr.Sum(g => g.Area), noAttr.Sum(g => g.Min), noAttr.Sum(g => g.Max)));
|
||||
foreach (var g in noAttr) {
|
||||
grid.Add((null, g.SortId, g.Area, g.Min, g.Max));
|
||||
}
|
||||
}
|
||||
foreach (var attrG in attrGroups) {
|
||||
grid.Add((attrG.Attr, null, attrG.Area, attrG.Min, attrG.Max));
|
||||
foreach (var g in groups.Where(g => g.Attr == attrG.Attr).OrderByDescending(g => g.Area).ThenBy(g => g.SortId)) {
|
||||
grid.Add((null, g.SortId, g.Area, g.Min, g.Max));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (text, grid.ToArray());
|
||||
}
|
||||
|
||||
public static Grid GenerateToolTip((string?, string?, int, int?, int?)[] data) {
|
||||
var grid = new Grid();
|
||||
grid.ColumnDefinitions.Add(new() { Width = new(10) });
|
||||
grid.ColumnDefinitions.Add(new() { Width = new(60) });
|
||||
grid.ColumnDefinitions.Add(new() { Width = new(80) });
|
||||
grid.ColumnDefinitions.Add(new() { Width = new(80) });
|
||||
grid.ColumnDefinitions.Add(new() { Width = new(80) });
|
||||
grid.AddToolTipCell("Lieferpflicht", 0, 3, alignCenter: true);
|
||||
grid.AddToolTipCell("Lieferrecht", 0, 4, 1, alignCenter: true);
|
||||
int rowNum = 1;
|
||||
foreach (var row in data) {
|
||||
if (rowNum == 2 || (rowNum != 1 && row.Item1 != null)) rowNum++;
|
||||
AddToolTipRow(grid, rowNum++, row.Item1, row.Item2, row.Item3, row.Item4, row.Item5);
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
public static async Task DeleteAreaCmt(int fbnr) {
|
||||
await Task.Run(async () => {
|
||||
using var ctx = new AppDbContext();
|
||||
var l = (await ctx.AreaCommitmentContracts.FindAsync(fbnr))!;
|
||||
ctx.Remove(l);
|
||||
await ctx.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task DeleteAreaCmtRevision(int fbnr, int revnr) {
|
||||
await Task.Run(async () => {
|
||||
using var ctx = new AppDbContext();
|
||||
var l = (await ctx.AreaCommitments.FindAsync(fbnr, revnr))!;
|
||||
ctx.Remove(l);
|
||||
await ctx.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user