Billing: Add feature to calculate member/delivery bins
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@ -6,41 +7,59 @@ using System.Threading.Tasks;
|
||||
namespace Elwig.Helpers.Billing {
|
||||
public class Billing {
|
||||
|
||||
private readonly int Year;
|
||||
private readonly int AvNr;
|
||||
private readonly AppDbContext Context;
|
||||
private readonly Dictionary<string, string> Attributes;
|
||||
private readonly Dictionary<string, (decimal?, decimal?)> Modifiers;
|
||||
private readonly Dictionary<string, (string, string?, string?, string?, int?, int?, decimal?)> AreaComTypes;
|
||||
protected readonly int Year;
|
||||
protected readonly AppDbContext Context;
|
||||
protected readonly Dictionary<string, string> Attributes;
|
||||
protected readonly Dictionary<string, (decimal?, decimal?)> Modifiers;
|
||||
protected readonly Dictionary<string, (string, string?, string?, string?, int?, int?, decimal?)> AreaComTypes;
|
||||
|
||||
public Billing(int year, int avnr) {
|
||||
public Billing(int year) {
|
||||
Year = year;
|
||||
AvNr = avnr;
|
||||
Context = new AppDbContext();
|
||||
Attributes = Context.WineAttributes.ToDictionary(a => a.AttrId, a => a.Name);
|
||||
Modifiers = Context.Modifiers.Where(m => m.Year == Year).ToDictionary(m => m.ModId, m => (m.Abs, m.Rel));
|
||||
AreaComTypes = Context.AreaCommitmentTypes.ToDictionary(v => v.VtrgId, v => (v.SortId, v.AttrId1, v.AttrId2, v.Discriminator, v.MinKgPerHa, v.MaxKgPerHa, v.PenaltyAmount));
|
||||
}
|
||||
|
||||
protected async Task DeleteInDb() {
|
||||
public async Task FinishSeason() {
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
using (var cmd = cnx.CreateCommand()) {
|
||||
cmd.CommandText = $"DELETE FROM payment_delivery_part WHERE (year, avnr) = ({Year}, {AvNr})";
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
using (var cmd = cnx.CreateCommand()) {
|
||||
cmd.CommandText = $"DELETE FROM payment_member WHERE (year, avnr) = ({Year}, {AvNr})";
|
||||
cmd.CommandText = $"""
|
||||
UPDATE season
|
||||
SET (start_date, end_date) = (SELECT MIN(date), MAX(date) FROM delivery WHERE year = {Year}),
|
||||
bin_1_name = 'gebunden mit zwei Attributen',
|
||||
bin_2_name = 'gebunden mit (erstem) Attribut',
|
||||
bin_3_name = 'gebunden mit zweitem Attribut',
|
||||
bin_4_name = 'gebunden ohne Attribut',
|
||||
bin_5_name = 'ungebunden'
|
||||
WHERE year = {Year}
|
||||
""";
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Calculate() {
|
||||
await DeleteInDb();
|
||||
var tasks = new List<Task>();
|
||||
foreach (var mgnr in Context.Members.Select(m => m.MgNr)) {
|
||||
tasks.Add(Task.Run(() => CalculateMember(mgnr)));
|
||||
public async Task CalculateBins() {
|
||||
var inserts = new List<(int, int, int, int, int, int, int)>();
|
||||
foreach (var mgnr in Context.Members.Where(m => m.IsActive).OrderBy(m => m.MgNr).Select(m => m.MgNr)) {
|
||||
inserts.AddRange(await CalculateMemberBins(mgnr));
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
using var cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = $"""
|
||||
INSERT INTO delivery_part_bin (year, did, dpnr, bin_1, bin_2, bin_3, bin_4, bin_5)
|
||||
VALUES {string.Join(",\n ", inserts.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, {i.Item4}, {i.Item5}, {i.Item6}, {i.Item7})"))}
|
||||
ON CONFLICT DO UPDATE
|
||||
SET bin_1 = excluded.bin_1,
|
||||
bin_2 = excluded.bin_2,
|
||||
bin_3 = excluded.bin_3,
|
||||
bin_4 = excluded.bin_4,
|
||||
bin_5 = excluded.bin_5,
|
||||
bin_6 = NULL,
|
||||
bin_7 = NULL,
|
||||
bin_8 = NULL,
|
||||
bin_9 = NULL;
|
||||
""";
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public static async Task<(Dictionary<string, int>, Dictionary<string, int>)> GetMemberRightsObligations(int mgnr, int year, SqliteConnection cnx) {
|
||||
@ -69,26 +88,26 @@ namespace Elwig.Helpers.Billing {
|
||||
return (rights, obligations);
|
||||
}
|
||||
|
||||
public static async Task<Dictionary<string, int>> GetMemberBucketWeights(int mgnr, int year, SqliteConnection cnx) {
|
||||
var buckets = new Dictionary<string, int>();
|
||||
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 bucket, weight
|
||||
FROM v_bucket
|
||||
SELECT bin, weight
|
||||
FROM v_bin
|
||||
WHERE (year, mgnr) = ({year}, {mgnr})
|
||||
""";
|
||||
|
||||
var reader = await cmd.ExecuteReaderAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
var bucket = reader.GetString(0);
|
||||
buckets[bucket] = reader.GetInt32(1);
|
||||
var bin = reader.GetString(0);
|
||||
bins[bin] = reader.GetInt32(1);
|
||||
}
|
||||
|
||||
return buckets;
|
||||
return bins;
|
||||
}
|
||||
|
||||
protected async Task CalculateMember(int mgnr) {
|
||||
protected async Task<List<(int, int, int, int, int, int, int)>> CalculateMemberBins(int mgnr) {
|
||||
using var cnx = await AppDbContext.ConnectAsync();
|
||||
var (rights, obligations) = await GetMemberRightsObligations(mgnr, Year, cnx);
|
||||
|
||||
@ -98,33 +117,51 @@ namespace Elwig.Helpers.Billing {
|
||||
SELECT did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers
|
||||
FROM v_delivery
|
||||
WHERE (year, mgnr) = ({Year}, {mgnr})
|
||||
ORDER BY kmw DESC, weight DESC, did, dpnr
|
||||
ORDER BY sortid, abgewertet ASC, LENGTH(attributes) DESC, COALESCE(attributes, '~'), kmw DESC, lsnr, dpnr
|
||||
""";
|
||||
var reader = await cmd.ExecuteReaderAsync();
|
||||
while (await reader.ReadAsync()) {
|
||||
deliveries.Add((
|
||||
reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetInt32(3),
|
||||
reader.GetDouble(4), reader.GetString(5), reader.GetString(6).Split(","), reader.GetString(7).Split(",")
|
||||
reader.GetDouble(4), reader.GetString(5),
|
||||
reader.IsDBNull(6) ? Array.Empty<string>() : reader.GetString(6).Split(",").Order().ToArray(),
|
||||
reader.IsDBNull(7) ? Array.Empty<string>() : reader.GetString(7).Split(",").Order().ToArray()
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
List<(int, int, int, int, int, int, int)> inserts = new();
|
||||
foreach (var (did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers) in deliveries) {
|
||||
if (qualid == "WEI" || qualid == "RSW" || qualid == "LDW") {
|
||||
// Nicht mindestens Qualitätswein (QUW)
|
||||
using var cmd = cnx.CreateCommand();
|
||||
// TODO
|
||||
cmd.CommandText = $"""
|
||||
INSERT INTO payment_delivery_part (year, did, dpnr, avnr, mod_abs, mod_rel, )
|
||||
VALUES ({Year}, {did}, {dpnr}, {AvNr}, )
|
||||
""";
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
// Nicht mindestens Qualitätswein (QUW) -> ungebunden
|
||||
inserts.Add((did, dpnr, 0, 0, 0, 0, weight));
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO
|
||||
}
|
||||
if (attributes.Length > 2)
|
||||
throw new NotSupportedException();
|
||||
|
||||
int w = weight;
|
||||
int[] b = new int[4];
|
||||
foreach (var p in Utils.Permutate(attributes)) {
|
||||
var c = p.Count();
|
||||
var key = sortid + string.Join("", p);
|
||||
if (rights.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));
|
||||
b[i] += v;
|
||||
rights[key] -= v;
|
||||
w -= v;
|
||||
}
|
||||
}
|
||||
inserts.Add((did, dpnr, b[0], b[1], b[2], b[3], weight - b[0] - b[1] - b[2] - b[3]));
|
||||
}
|
||||
return inserts;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user