DeliveryConfirmation: enhance calculation
This commit is contained in:
@ -43,7 +43,8 @@ namespace Elwig.Helpers.Billing {
|
||||
}
|
||||
|
||||
public async Task CalculateBins() {
|
||||
var forcedAttr = Context.WineAttributes.Where(a => !a.FillLowerBins).Select(a => a.AttrId).ToArray();
|
||||
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);
|
||||
var inserts = new List<(int, int, int, int, int, int, int)>();
|
||||
@ -54,7 +55,9 @@ namespace Elwig.Helpers.Billing {
|
||||
SELECT mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers
|
||||
FROM v_delivery
|
||||
WHERE year = {Year}
|
||||
ORDER BY mgnr, sortid, abgewertet ASC, LENGTH(attributes) DESC, COALESCE(attributes, '~'), kmw DESC, lsnr, dpnr
|
||||
ORDER BY mgnr, sortid, abgewertet ASC,
|
||||
COALESCE(LENGTH(attributes), 0) ASC, attribute_prio DESC, COALESCE(attributes, '~'),
|
||||
kmw DESC, lsnr, dpnr
|
||||
""";
|
||||
var reader = await cmd.ExecuteReaderAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
@ -69,11 +72,18 @@ namespace Elwig.Helpers.Billing {
|
||||
|
||||
int lastMgNr = 0;
|
||||
Dictionary<string, int>? rights = null;
|
||||
Dictionary<string, int>? obligations = null;
|
||||
Dictionary<string, int> used = new();
|
||||
foreach (var (mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers) in deliveries) {
|
||||
if (lastMgNr != mgnr) {
|
||||
rights = memberOblRig.GetValueOrDefault(mgnr, (new(), new())).Item2;
|
||||
var or = memberOblRig.GetValueOrDefault(mgnr, (new(), new()));
|
||||
rights = or.Item2;
|
||||
obligations = or.Item1;
|
||||
used = new();
|
||||
}
|
||||
if (rights == null || rights.Count == 0 || qualid == "WEI" || qualid == "RSW" || qualid == "LDW") {
|
||||
if (obligations == null || rights == null || obligations.Count == 0 || rights.Count == 0 ||
|
||||
qualid == "WEI" || qualid == "RSW" || qualid == "LDW")
|
||||
{
|
||||
// Mitglied hat keine Flächenbindungen, oder
|
||||
// Nicht mindestens Qualitätswein (QUW) -> ungebunden
|
||||
inserts.Add((did, dpnr, 0, 0, 0, 0, weight));
|
||||
@ -85,21 +95,24 @@ namespace Elwig.Helpers.Billing {
|
||||
|
||||
int w = weight;
|
||||
int[] b = new int[4];
|
||||
foreach (var p in Utils.Permutate(attributes, attributes.Intersect(forcedAttr))) {
|
||||
foreach (var p in Utils.Permutate(attributes, attributes.Intersect(attrForced))) {
|
||||
var c = p.Count();
|
||||
var key = sortid + string.Join("", p);
|
||||
if (rights.ContainsKey(key)) {
|
||||
if (rights.ContainsKey(key) && obligations.ContainsKey(key)) {
|
||||
int i = 0;
|
||||
if (c == 1) {
|
||||
i = (p.ElementAt(0) == attributes[0]) ? 1 : 2;
|
||||
} else if (c == 0) {
|
||||
i = b.Length - 1;
|
||||
}
|
||||
var v = Math.Max(0, Math.Min(rights[key], w));
|
||||
var vr = Math.Max(0, Math.Min(rights[key] - used.GetValueOrDefault(key, 0), w));
|
||||
var vo = Math.Max(0, Math.Min(obligations[key] - used.GetValueOrDefault(key, 0), w));
|
||||
var v = (c == 0 || p.Select(a => attrVals[a]).Min() == 2) ? vr : vo;
|
||||
b[i] += v;
|
||||
rights[key] -= v;
|
||||
used[key] = used.GetValueOrDefault(key, 0) + v;
|
||||
w -= v;
|
||||
}
|
||||
if (w == 0) break;
|
||||
}
|
||||
inserts.Add((did, dpnr, b[0], b[1], b[2], b[3], weight - b[0] - b[1] - b[2] - b[3]));
|
||||
lastMgNr = mgnr;
|
||||
|
Reference in New Issue
Block a user