Billing: honor gebunden field

This commit is contained in:
2023-10-15 15:13:59 +02:00
parent c997acc95d
commit 4db147e582
2 changed files with 17 additions and 10 deletions

View File

@ -42,20 +42,20 @@ namespace Elwig.Helpers.Billing {
} }
} }
public async Task CalculateBins() { public async Task CalculateBins(bool allowAttrsIntoLowerBins, bool avoidUnderDeliveries, bool honorGebunden) {
var attrVals = Context.WineAttributes.ToDictionary(a => a.AttrId, a => a.FillLowerBins); var attrVals = Context.WineAttributes.ToDictionary(a => a.AttrId, a => a.FillLowerBins);
var attrForced = attrVals.Where(a => a.Value == 0).Select(a => a.Key).ToArray(); var attrForced = attrVals.Where(a => a.Value == 0).Select(a => a.Key).ToArray();
using var cnx = await AppDbContext.ConnectAsync(); using var cnx = await AppDbContext.ConnectAsync();
var memberOblRig = await GetMemberRightsObligations(cnx, Year); var memberOblRig = await GetMemberRightsObligations(cnx, Year);
var inserts = new List<(int, int, int, int, int, int, int)>(); var inserts = new List<(int, int, int, int, int, int, int)>();
var deliveries = new List<(int, int, int, string, int, double, string, string[], string[])>(); var deliveries = new List<(int, int, int, string, int, double, string, string[], string[], bool?)>();
using (var cmd = cnx.CreateCommand()) { using (var cmd = cnx.CreateCommand()) {
cmd.CommandText = $""" cmd.CommandText = $"""
SELECT mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers SELECT mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers, gebunden
FROM v_delivery FROM v_delivery
WHERE year = {Year} WHERE year = {Year}
ORDER BY mgnr, sortid, abgewertet ASC, ORDER BY mgnr, sortid, abgewertet ASC, {(honorGebunden ? "gebunden IS NOT NULL DESC, gebunden DESC," : "")}
COALESCE(LENGTH(attributes), 0) ASC, attribute_prio DESC, COALESCE(attributes, '~'), COALESCE(LENGTH(attributes), 0) ASC, attribute_prio DESC, COALESCE(attributes, '~'),
kmw DESC, lsnr, dpnr kmw DESC, lsnr, dpnr
"""; """;
@ -65,7 +65,8 @@ namespace Elwig.Helpers.Billing {
reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetString(3), reader.GetInt32(4), reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetString(3), reader.GetInt32(4),
reader.GetDouble(5), reader.GetString(6), reader.GetDouble(5), reader.GetString(6),
reader.IsDBNull(7) ? Array.Empty<string>() : reader.GetString(7).Split(",").Order().ToArray(), reader.IsDBNull(7) ? Array.Empty<string>() : reader.GetString(7).Split(",").Order().ToArray(),
reader.IsDBNull(8) ? Array.Empty<string>() : reader.GetString(8).Split(",").Order().ToArray() reader.IsDBNull(8) ? Array.Empty<string>() : reader.GetString(8).Split(",").Order().ToArray(),
reader.IsDBNull(9) ? null : reader.GetBoolean(9)
)); ));
} }
} }
@ -74,18 +75,22 @@ namespace Elwig.Helpers.Billing {
Dictionary<string, int>? rights = null; Dictionary<string, int>? rights = null;
Dictionary<string, int>? obligations = null; Dictionary<string, int>? obligations = null;
Dictionary<string, int> used = new(); Dictionary<string, int> used = new();
foreach (var (mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers) in deliveries) { foreach (var (mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers, gebunden) in deliveries) {
if (lastMgNr != mgnr) { if (lastMgNr != mgnr) {
var or = memberOblRig.GetValueOrDefault(mgnr, (new(), new())); var or = memberOblRig.GetValueOrDefault(mgnr, (new(), new()));
rights = or.Item2; rights = or.Item2;
obligations = or.Item1; obligations = or.Item1;
used = new(); used = new();
} }
if (obligations == null || rights == null || obligations.Count == 0 || rights.Count == 0 || if ((honorGebunden && gebunden == false) ||
obligations == null || rights == null ||
obligations.Count == 0 || rights.Count == 0 ||
qualid == "WEI" || qualid == "RSW" || qualid == "LDW") qualid == "WEI" || qualid == "RSW" || qualid == "LDW")
{ {
// Explizit als ungebunden markiert,
// Mitglied hat keine Flächenbindungen, oder // Mitglied hat keine Flächenbindungen, oder
// Nicht mindestens Qualitätswein (QUW) -> ungebunden // Nicht mindestens Qualitätswein (QUW)
// -> ungebunden
inserts.Add((did, dpnr, 0, 0, 0, 0, weight)); inserts.Add((did, dpnr, 0, 0, 0, 0, weight));
continue; continue;
} }
@ -112,7 +117,7 @@ namespace Elwig.Helpers.Billing {
used[key] = used.GetValueOrDefault(key, 0) + v; used[key] = used.GetValueOrDefault(key, 0) + v;
w -= v; w -= v;
} }
if (w == 0) break; if (w == 0 || !allowAttrsIntoLowerBins) break;
} }
inserts.Add((did, dpnr, b[0], b[1], b[2], b[3], weight - b[0] - b[1] - b[2] - b[3])); inserts.Add((did, dpnr, b[0], b[1], b[2], b[3], weight - b[0] - b[1] - b[2] - b[3]));
lastMgNr = mgnr; lastMgNr = mgnr;
@ -135,6 +140,8 @@ namespace Elwig.Helpers.Billing {
"""; """;
await cmd.ExecuteNonQueryAsync(); await cmd.ExecuteNonQueryAsync();
} }
// 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) { public static async Task<Dictionary<int, (Dictionary<string, int>, Dictionary<string, int>)>> GetMemberRightsObligations(SqliteConnection cnx, int year, int? mgnr = null) {

View File

@ -35,7 +35,7 @@ namespace Elwig.Windows {
Mouse.OverrideCursor = Cursors.AppStarting; Mouse.OverrideCursor = Cursors.AppStarting;
var b = new Billing(year); var b = new Billing(year);
await b.FinishSeason(); await b.FinishSeason();
await b.CalculateBins(); await b.CalculateBins(true, false, false);
Mouse.OverrideCursor = null; Mouse.OverrideCursor = null;
} }