BillingVariant: Assume price to be 0 for undefined vaributes
All checks were successful
Test / Run tests (push) Successful in 1m56s

This commit is contained in:
2025-10-28 13:15:24 +01:00
parent 2de8af878b
commit 428cd6ddc2
3 changed files with 28 additions and 7 deletions

View File

@@ -20,13 +20,19 @@ namespace Elwig.Helpers.Billing {
Data = PaymentBillingData.FromJson(PaymentVariant.Data, Utils.GetVaributes(ctx, Year, onlyDelivered: false)); Data = PaymentBillingData.FromJson(PaymentVariant.Data, Utils.GetVaributes(ctx, Year, onlyDelivered: false));
} }
public async Task Calculate(bool? honorGebunden = null, bool? allowAttrsIntoLower = null, bool? avoidUnderDeliveries = null) { public async Task Calculate(bool strictPrices = true, bool? honorGebunden = null, bool? allowAttrsIntoLower = null, bool? avoidUnderDeliveries = null) {
using var cnx = await AppDbContext.ConnectAsync(); using var cnx = await AppDbContext.ConnectAsync();
using var tx = await cnx.BeginTransactionAsync(); using var tx = await cnx.BeginTransactionAsync();
await CalculateBuckets(honorGebunden, allowAttrsIntoLower, avoidUnderDeliveries, cnx); await CalculateBuckets(honorGebunden, allowAttrsIntoLower, avoidUnderDeliveries, cnx);
await DeleteInDb(cnx); await DeleteInDb(cnx);
await SetCalcTime(cnx); await SetCalcTime(cnx);
await CalculatePrices(cnx); KeyNotFoundException? exception = null;
try {
await CalculatePrices(cnx, strictPrices);
} catch (KeyNotFoundException e) {
if (strictPrices) throw;
exception = e;
}
if (Data.ConsiderDelieryModifiers) { if (Data.ConsiderDelieryModifiers) {
await CalculateDeliveryModifiers(cnx); await CalculateDeliveryModifiers(cnx);
} }
@@ -34,6 +40,8 @@ namespace Elwig.Helpers.Billing {
await CalculateMemberModifiers(cnx); await CalculateMemberModifiers(cnx);
} }
await tx.CommitAsync(); await tx.CommitAsync();
if (exception != null)
throw exception;
} }
public async Task Commit() { public async Task Commit() {
@@ -142,7 +150,8 @@ namespace Elwig.Helpers.Billing {
"""); """);
} }
protected async Task CalculatePrices(SqliteConnection cnx) { protected async Task CalculatePrices(SqliteConnection cnx, bool strict = true) {
var invalid = new HashSet<string>();
var parts = new List<(int Year, int DId, int DPNr, int BktNr, string SortId, string? AttrId, string? CultId, string Discr, int Value, double Oe, double Kmw, string QualId, bool AttrAreaCom)>(); var parts = new List<(int Year, int DId, int DPNr, int BktNr, string SortId, string? AttrId, string? CultId, string Discr, int Value, double Oe, double Kmw, string QualId, bool AttrAreaCom)>();
using (var cmd = cnx.CreateCommand()) { using (var cmd = cnx.CreateCommand()) {
cmd.CommandText = $""" cmd.CommandText = $"""
@@ -172,15 +181,25 @@ namespace Elwig.Helpers.Billing {
var payAttrId = (part.Discr is "" or "_") ? null : part.Discr; var payAttrId = (part.Discr is "" or "_") ? null : part.Discr;
var attrId = part.AttrAreaCom ? payAttrId : part.AttrId; var attrId = part.AttrAreaCom ? payAttrId : part.AttrId;
var geb = !ungeb && (payAttrId == attrId || !part.AttrAreaCom); var geb = !ungeb && (payAttrId == attrId || !part.AttrAreaCom);
var price = Data.CalculatePrice(part.SortId, attrId, part.CultId, part.QualId, geb, part.Oe, part.Kmw); decimal price = 0;
try {
price = Data.CalculatePrice(part.SortId, attrId, part.CultId, part.QualId, geb, part.Oe, part.Kmw);
} catch (KeyNotFoundException e) {
invalid.Add(e.Message.Split('\'')[1]);
}
var priceL = PaymentVariant.Season.DecToDb(price); var priceL = PaymentVariant.Season.DecToDb(price);
inserts.Add((part.Year, part.DId, part.DPNr, part.BktNr, priceL, priceL * part.Value)); inserts.Add((part.Year, part.DId, part.DPNr, part.BktNr, priceL, priceL * part.Value));
} }
var msg = invalid.Count == 0 ? null : "Für folgende Sorten wurde noch keine Preiskurve festgelegt: " + string.Join(", ", invalid);
if (msg != null && strict)
throw new KeyNotFoundException(msg);
await AppDbContext.ExecuteBatch(cnx, $""" await AppDbContext.ExecuteBatch(cnx, $"""
INSERT INTO payment_delivery_part_bucket (year, did, dpnr, bktnr, avnr, price, amount) INSERT INTO payment_delivery_part_bucket (year, did, dpnr, bktnr, avnr, price, amount)
VALUES {string.Join(",\n ", inserts.Select(i => $"({i.Year}, {i.DId}, {i.DPNr}, {i.BktNr}, {AvNr}, {i.Price}, {i.Amount})"))}; VALUES {string.Join(",\n ", inserts.Select(i => $"({i.Year}, {i.DId}, {i.DPNr}, {i.BktNr}, {AvNr}, {i.Price}, {i.Amount})"))};
"""); """);
if (msg != null)
throw new KeyNotFoundException(msg);
} }
protected async Task CalculateDeliveryModifiers(SqliteConnection cnx) { protected async Task CalculateDeliveryModifiers(SqliteConnection cnx) {

View File

@@ -664,8 +664,10 @@ namespace Elwig.Windows {
try { try {
await Task.Run(async () => { await Task.Run(async () => {
var b = new BillingVariant(PaymentVar.Year, PaymentVar.AvNr); var b = new BillingVariant(PaymentVar.Year, PaymentVar.AvNr);
await b.Calculate(); await b.Calculate(false);
}); });
} catch (KeyNotFoundException exc) {
MessageBox.Show(exc.Message, "Noch nicht alle Preise festgelegt", MessageBoxButton.OK, MessageBoxImage.Information);
} catch (Exception exc) { } catch (Exception exc) {
MessageBox.Show(exc.Message, "Berechnungsfehler", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show(exc.Message, "Berechnungsfehler", MessageBoxButton.OK, MessageBoxImage.Error);
} }

View File

@@ -188,7 +188,7 @@ namespace Tests.UnitTests.HelperTests {
Assert.That(payment["GV"], Is.EqualTo(10_000)); Assert.That(payment["GV"], Is.EqualTo(10_000));
}); });
await b.Calculate(false, false, false); await b.Calculate(true, false, false, false);
var prices = await GetMemberDeliveryPrices(year, mgnr); var prices = await GetMemberDeliveryPrices(year, mgnr);
Assert.Multiple(() => { Assert.Multiple(() => {
Assert.That(prices, Has.Count.EqualTo(7)); Assert.That(prices, Has.Count.EqualTo(7));
@@ -234,7 +234,7 @@ namespace Tests.UnitTests.HelperTests {
Assert.That(payment["GV"], Is.EqualTo(8_000)); Assert.That(payment["GV"], Is.EqualTo(8_000));
}); });
await b.Calculate(true, false, false); await b.Calculate(true, true, false, false);
var prices = await GetMemberDeliveryPrices(year, mgnr); var prices = await GetMemberDeliveryPrices(year, mgnr);
Assert.Multiple(() => { Assert.Multiple(() => {
Assert.That(prices, Has.Count.EqualTo(6)); Assert.That(prices, Has.Count.EqualTo(6));