[#77] Entities: Add AreaComContract to group area commitments together
This commit is contained in:
@@ -20,35 +20,39 @@ namespace Elwig.Services {
|
||||
}
|
||||
|
||||
public static void ClearInputs(this AreaComAdminViewModel vm) {
|
||||
vm.Period = null;
|
||||
}
|
||||
|
||||
public static void FillInputs(this AreaComAdminViewModel vm, AreaComContract 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 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<string>, IQueryable<AreaCom>, List<string>)> GetFilters(this AreaComAdminViewModel vm, AppDbContext ctx) {
|
||||
public static async Task<(List<string>, IQueryable<AreaComContract>, IQueryable<AreaCom>, List<string>)> GetFilters(this AreaComAdminViewModel vm, AppDbContext ctx) {
|
||||
List<string> filterNames = [];
|
||||
IQueryable<AreaCom> areaComQuery = ctx.AreaCommitments.Where(a => a.MgNr == vm.FilterMember.MgNr).OrderBy(a => a.FbNr);
|
||||
if (vm.ShowOnlyActiveAreaComs) {
|
||||
areaComQuery = Utils.ActiveAreaCommitments(areaComQuery, Utils.CurrentLastSeason);
|
||||
filterNames.Add($"laufend {Utils.CurrentLastSeason}");
|
||||
if (vm.FilterSeason is int season) {
|
||||
areaComQuery = Utils.ActiveAreaCommitments(areaComQuery, 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 filterSeasons = new List<int>();
|
||||
|
||||
var filter = vm.TextFilter;
|
||||
if (filter.Count > 0) {
|
||||
@@ -88,10 +92,6 @@ namespace Elwig.Services {
|
||||
filter.RemoveAt(i--);
|
||||
filterNames.Add($"ohne {var[e[1..3].ToUpper()].Name}");
|
||||
filterNames.Add($"ohne Attribut {attrId[e[3..].ToUpper()].Name}");
|
||||
} else if (e.Length == 4 && int.TryParse(e, out var year)) {
|
||||
filterSeasons.Add(year);
|
||||
filter.RemoveAt(i--);
|
||||
filterNames.Add($"laufend {e}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,38 +99,50 @@ namespace Elwig.Services {
|
||||
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!.AttrId != null && filterAttr.Contains(a.AreaComType.WineAttr.AttrId));
|
||||
if (filterNotAttr.Count > 0) areaComQuery = areaComQuery.Where(a => a.AreaComType.WineAttr!.AttrId == null || !filterNotAttr.Contains(a.AreaComType.WineAttr.AttrId));
|
||||
foreach (var year in filterSeasons) areaComQuery = Utils.ActiveAreaCommitments(areaComQuery, year);
|
||||
}
|
||||
|
||||
return (filterNames, areaComQuery, filter);
|
||||
IQueryable<AreaComContract> contracts = areaComQuery
|
||||
.Select(c => c.Contract).Distinct()
|
||||
.OrderBy(c => c.FbNr);
|
||||
|
||||
return (filterNames, contracts, areaComQuery, filter);
|
||||
}
|
||||
|
||||
public static async Task<int> UpdateAreaCommitment(this AreaComAdminViewModel vm, int? oldFbNr) {
|
||||
public static async Task<(int FbNr, int RevNr)> UpdateAreaCommitment(this AreaComAdminViewModel vm, int? oldFbNr, int? revNr) {
|
||||
int newFbNr = vm.FbNr!.Value;
|
||||
|
||||
return await Task.Run(async () => {
|
||||
using var ctx = new AppDbContext();
|
||||
var c = new AreaComContract {
|
||||
FbNr = oldFbNr ?? newFbNr,
|
||||
Comment = string.IsNullOrEmpty(vm.Comment) ? null : vm.Comment,
|
||||
KgNr = vm.Kg!.KgNr,
|
||||
RdNr = vm.Rd?.RdNr,
|
||||
};
|
||||
var a = new AreaCom {
|
||||
FbNr = oldFbNr ?? newFbNr,
|
||||
RevNr = revNr ?? await ctx.NextRevNr(oldFbNr ?? newFbNr),
|
||||
MgNr = vm.MgNr!.Value,
|
||||
YearFrom = 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 = vm.Area!.Value,
|
||||
};
|
||||
|
||||
if (vm.Rd?.RdNr == 0) {
|
||||
vm.Rd.RdNr = await ctx.NextRdNr(a.KgNr);
|
||||
a.RdNr = vm.Rd.RdNr;
|
||||
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);
|
||||
@@ -139,10 +151,10 @@ namespace Elwig.Services {
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
if (newFbNr != a.FbNr) {
|
||||
await ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment SET fbnr = {newFbNr} WHERE fbnr = {oldFbNr}");
|
||||
await ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment_contract SET fbnr = {newFbNr} WHERE fbnr = {oldFbNr}");
|
||||
}
|
||||
|
||||
return newFbNr;
|
||||
return (newFbNr, a.RevNr);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -256,7 +268,16 @@ namespace Elwig.Services {
|
||||
public static async Task DeleteAreaCom(int fbnr) {
|
||||
await Task.Run(async () => {
|
||||
using var ctx = new AppDbContext();
|
||||
var l = (await ctx.AreaCommitments.FindAsync(fbnr))!;
|
||||
var l = (await ctx.AreaCommitmentContracts.FindAsync(fbnr))!;
|
||||
ctx.Remove(l);
|
||||
await ctx.SaveChangesAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task DeleteAreaComRevision(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