Billing: Always call CalculateBuckets() when Calculate() is called to avoid user confusion

This commit is contained in:
2024-01-31 11:14:56 +01:00
parent 50ac757067
commit b9287f8260
7 changed files with 83 additions and 21 deletions

View File

@ -9,7 +9,7 @@ namespace Elwig.Helpers {
public static class AppDbUpdater {
// Don't forget to update value in Tests/fetch-resources.bat!
public static readonly int RequiredSchemaVersion = 14;
public static readonly int RequiredSchemaVersion = 15;
private static int VersionOffset = 0;

View File

@ -1,3 +1,5 @@
using Elwig.Models.Entities;
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Linq;
@ -8,6 +10,7 @@ namespace Elwig.Helpers.Billing {
protected readonly int Year;
protected readonly AppDbContext Context;
protected readonly Season Season;
protected readonly Dictionary<string, string> Attributes;
protected readonly Dictionary<string, (decimal?, decimal?)> Modifiers;
protected readonly Dictionary<string, (string, string?, string?, int?, decimal?)> AreaComTypes;
@ -15,6 +18,7 @@ namespace Elwig.Helpers.Billing {
public Billing(int year) {
Year = year;
Context = new AppDbContext();
Season = Context.Seasons.Find(Year)!;
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.AttrId, v.Discriminator, v.MinKgPerHa, v.PenaltyAmount));
@ -26,8 +30,6 @@ namespace Elwig.Helpers.Billing {
UPDATE season
SET (start_date, end_date) = (SELECT MIN(date), MAX(date) FROM delivery WHERE year = {Year})
WHERE year = {Year};
DELETE FROM delivery_part_bucket WHERE year = {Year};
""");
}
@ -43,10 +45,11 @@ namespace Elwig.Helpers.Billing {
""");
}
public async Task CalculateBuckets(bool allowAttrsIntoLower, bool avoidUnderDeliveries, bool honorGebunden) {
public async Task CalculateBuckets(bool allowAttrsIntoLower, bool avoidUnderDeliveries, bool honorGebunden, SqliteConnection? cnx = null) {
var attrVals = Context.WineAttributes.ToDictionary(a => a.AttrId, a => (a.IsStrict, a.FillLower));
var attrForced = attrVals.Where(a => a.Value.IsStrict && a.Value.FillLower == 0).Select(a => a.Key).ToArray();
using var cnx = await AppDbContext.ConnectAsync();
var ownCnx = cnx == null;
cnx ??= await AppDbContext.ConnectAsync();
await Context.GetMemberAreaCommitmentBuckets(Year, 0, cnx);
var inserts = new List<(int, int, int, string, int)>();
@ -65,7 +68,7 @@ namespace Elwig.Helpers.Billing {
reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetString(3), reader.GetInt32(4),
reader.GetDouble(5), reader.GetString(6),
reader.IsDBNull(7) ? null : reader.GetString(7),
reader.IsDBNull(8) ? Array.Empty<string>() : reader.GetString(8).Split(",").Order().ToArray(),
reader.IsDBNull(8) ? [] : reader.GetString(8).Split(",").Order().ToArray(),
reader.IsDBNull(9) ? null : reader.GetBoolean(9)
));
}
@ -73,11 +76,11 @@ namespace Elwig.Helpers.Billing {
int lastMgNr = 0;
Dictionary<string, AreaComBucket>? rightsAndObligations = null;
Dictionary<string, int> used = new();
Dictionary<string, int> used = [];
foreach (var (mgnr, did, dpnr, sortid, weight, kmw, qualid, attrid, modifiers, gebunden) in deliveries) {
if (lastMgNr != mgnr) {
rightsAndObligations = await Context.GetMemberAreaCommitmentBuckets(Year, mgnr);
used = new();
used = [];
}
if ((honorGebunden && gebunden == false) ||
rightsAndObligations == null || rightsAndObligations.Count == 0 ||
@ -92,16 +95,16 @@ namespace Elwig.Helpers.Billing {
}
int w = weight;
var attributes = attrid == null ? Array.Empty<string>() : new string[] { attrid };
var attributes = attrid == null ? [] : new string[] { attrid };
var isStrict = attrid != null && attrVals[attrid].IsStrict;
foreach (var p in Utils.Permutate(attributes, attributes.Intersect(attrForced))) {
var c = p.Count();
var key = sortid + string.Join("", p);
if (rightsAndObligations.ContainsKey(key)) {
if (rightsAndObligations.TryGetValue(key, out AreaComBucket value)) {
int i = (c == 0) ? 1 : 2;
var u = used.GetValueOrDefault(key, 0);
var vr = Math.Max(0, Math.Min(rightsAndObligations[key].Right - u, w));
var vo = Math.Max(0, Math.Min(rightsAndObligations[key].Obligation - u, w));
var vr = Math.Max(0, Math.Min(value.Right - u, w));
var vo = Math.Max(0, Math.Min(value.Obligation - u, w));
var v = (attributes.Length == c || attributes.Select(a => !attrVals[a].IsStrict ? 2 : attrVals[a].FillLower).Min() == 2) ? vr : vo;
used[key] = u + v;
if (key.Length > 2 && !isStrict) used[key[..2]] = used.GetValueOrDefault(key[..2], 0) + v;
@ -115,14 +118,17 @@ namespace Elwig.Helpers.Billing {
}
await AppDbContext.ExecuteBatch(cnx, $"""
UPDATE delivery_part_bucket SET value = 0 WHERE year = {Year};
INSERT INTO delivery_part_bucket (year, did, dpnr, bktnr, discr, value)
VALUES {string.Join(",\n ", inserts.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, '{i.Item4}', {i.Item5})"))}
ON CONFLICT DO UPDATE
SET discr = excluded.discr, value = value + excluded.value;
""");
if (!avoidUnderDeliveries)
if (!avoidUnderDeliveries) {
if (ownCnx) await cnx.DisposeAsync();
return;
}
// FIXME avoidUnderDelivery-calculations not always right!
@ -200,6 +206,8 @@ namespace Elwig.Helpers.Billing {
ON CONFLICT DO UPDATE
SET value = excluded.value;
""");
if (ownCnx) await cnx.DisposeAsync();
}
}
}

View File

@ -21,6 +21,11 @@ namespace Elwig.Helpers.Billing {
public async Task Calculate() {
using var cnx = await AppDbContext.ConnectAsync();
using var tx = await cnx.BeginTransactionAsync();
await CalculateBuckets(
Season.Billing_AllowAttrsIntoLower,
Season.Billing_AvoidUnderDeliveries,
Season.Billing_HonorGebunden,
cnx);
await DeleteInDb(cnx);
await SetCalcTime(cnx);
await CalculatePrices(cnx);