DeliveryConfirmation: Add Statistics table
This commit is contained in:
@ -42,7 +42,7 @@ namespace Elwig.Helpers.Billing {
|
||||
var attrVals = Context.WineAttributes.ToDictionary(a => a.AttrId, a => a.FillLowerBins);
|
||||
var attrForced = attrVals.Where(a => a.Value == 0).Select(a => a.Key).ToArray();
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
var memberOblRig = await GetMemberRightsObligations(cnx, Year);
|
||||
await Context.GetMemberRightsAndObligations(Year, 0, cnx);
|
||||
var inserts = new List<(int, int, int, string, int)>();
|
||||
|
||||
var deliveries = new List<(int, int, int, string, int, double, string, string[], string[], bool?)>();
|
||||
@ -68,19 +68,15 @@ namespace Elwig.Helpers.Billing {
|
||||
}
|
||||
|
||||
int lastMgNr = 0;
|
||||
Dictionary<string, int>? rights = null;
|
||||
Dictionary<string, int>? obligations = null;
|
||||
Dictionary<string, (int, int)>? rightsAndObligations = null;
|
||||
Dictionary<string, int> used = new();
|
||||
foreach (var (mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers, gebunden) in deliveries) {
|
||||
if (lastMgNr != mgnr) {
|
||||
var or = memberOblRig.GetValueOrDefault(mgnr, (new(), new()));
|
||||
rights = or.Item2;
|
||||
obligations = or.Item1;
|
||||
rightsAndObligations = await Context.GetMemberRightsAndObligations(Year, mgnr);
|
||||
used = new();
|
||||
}
|
||||
if ((honorGebunden && gebunden == false) ||
|
||||
obligations == null || rights == null ||
|
||||
obligations.Count == 0 || rights.Count == 0 ||
|
||||
rightsAndObligations == null || rightsAndObligations.Count == 0 ||
|
||||
qualid == "WEI" || qualid == "RSW" || qualid == "LDW")
|
||||
{
|
||||
// Explizit als ungebunden markiert,
|
||||
@ -98,7 +94,7 @@ namespace Elwig.Helpers.Billing {
|
||||
foreach (var p in Utils.Permutate(attributes, attributes.Intersect(attrForced))) {
|
||||
var c = p.Count();
|
||||
var key = sortid + string.Join("", p);
|
||||
if (rights.ContainsKey(key) && obligations.ContainsKey(key)) {
|
||||
if (rightsAndObligations.ContainsKey(key)) {
|
||||
int i = 4;
|
||||
if (c == 1) {
|
||||
i = (p.ElementAt(0) == attributes[0]) ? 2 : 3;
|
||||
@ -106,8 +102,8 @@ namespace Elwig.Helpers.Billing {
|
||||
i = 1;
|
||||
}
|
||||
var u = used.GetValueOrDefault(key, 0);
|
||||
var vr = Math.Max(0, Math.Min(rights[key] - u, w));
|
||||
var vo = Math.Max(0, Math.Min(obligations[key] - u, w));
|
||||
var vr = Math.Max(0, Math.Min(rightsAndObligations[key].Item1 - u, w));
|
||||
var vo = Math.Max(0, Math.Min(rightsAndObligations[key].Item2 - u, w));
|
||||
var v = (c == 0 || p.Select(a => attrVals[a]).Min() == 2) ? vr : vo;
|
||||
used[key] = u + v;
|
||||
inserts.Add((did, dpnr, i, key[2..], v));
|
||||
@ -131,58 +127,5 @@ namespace Elwig.Helpers.Billing {
|
||||
|
||||
// TODO add second round to avoid under deliveries
|
||||
}
|
||||
|
||||
public static async Task<Dictionary<int, (Dictionary<string, int>, Dictionary<string, int>)>> GetMemberRightsObligations(SqliteConnection cnx, int year, int? mgnr = null) {
|
||||
var members = new Dictionary<int, (Dictionary<string, int>, Dictionary<string, int>)>();
|
||||
|
||||
using var cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = $"""
|
||||
SELECT mgnr, t.vtrgid,
|
||||
SUM(COALESCE(area * min_kg_per_ha, 0)) / 10000 AS min_kg,
|
||||
SUM(COALESCE(area * max_kg_per_ha, 0)) / 10000 AS max_kg
|
||||
FROM area_commitment c
|
||||
JOIN area_commitment_type t ON t.vtrgid = c.vtrgid
|
||||
WHERE ({(mgnr == null ? "NULL" : mgnr)} IS NULL OR mgnr = {(mgnr == null ? "NULL" : mgnr)}) AND
|
||||
(year_from IS NULL OR year_from <= {year}) AND
|
||||
(year_to IS NULL OR year_to >= {year})
|
||||
GROUP BY mgnr, t.vtrgid
|
||||
ORDER BY LENGTH(t.vtrgid) DESC, t.vtrgid
|
||||
""";
|
||||
|
||||
var reader = await cmd.ExecuteReaderAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
var m = reader.GetInt32(0);
|
||||
var vtrgid = reader.GetString(1);
|
||||
if (!members.ContainsKey(m)) members[m] = (new(), new());
|
||||
members[m].Item1[vtrgid] = reader.GetInt32(2);
|
||||
members[m].Item2[vtrgid] = reader.GetInt32(3);
|
||||
}
|
||||
|
||||
return members;
|
||||
}
|
||||
|
||||
public static async Task<(Dictionary<string, int>, Dictionary<string, int>)> GetMemberRightsObligations(SqliteConnection cnx, int year, int mgnr) {
|
||||
var members = await GetMemberRightsObligations(cnx, year, (int?)mgnr);
|
||||
return members.GetValueOrDefault(mgnr, (new(), new()));
|
||||
}
|
||||
|
||||
public static async Task<Dictionary<string, int>> GetMemberBinWeights(int mgnr, int year, SqliteConnection cnx) {
|
||||
var bins = new Dictionary<string, int>();
|
||||
|
||||
using var cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = $"""
|
||||
SELECT bin, weight
|
||||
FROM v_delivery_bin
|
||||
WHERE (year, mgnr) = ({year}, {mgnr})
|
||||
""";
|
||||
|
||||
var reader = await cmd.ExecuteReaderAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
var bin = reader.GetString(0);
|
||||
bins[bin] = reader.GetInt32(1);
|
||||
}
|
||||
|
||||
return bins;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user