diff --git a/Elwig/Helpers/Billing/BillingData.cs b/Elwig/Helpers/Billing/BillingData.cs index 94bc815..d59e4e4 100644 --- a/Elwig/Helpers/Billing/BillingData.cs +++ b/Elwig/Helpers/Billing/BillingData.cs @@ -124,6 +124,7 @@ namespace Elwig.Helpers.Billing { var obj = c?.AsObject() ?? throw new InvalidOperationException(); var id = obj["id"]?.GetValue() ?? throw new InvalidOperationException(); var cMode = (obj["mode"]?.GetValue() == "kmw") ? CurveMode.Kmw : CurveMode.Oe; + double quw = cMode == CurveMode.Oe ? 73 : 15; Dictionary c1; Dictionary? c2 = null; @@ -131,7 +132,7 @@ namespace Elwig.Helpers.Billing { if (norm is JsonObject) { c1 = GetCurveData(norm.AsObject(), cMode); } else if (norm?.AsValue().TryGetValue(out decimal v) == true) { - c1 = new() { { cMode == CurveMode.Oe ? 73 : 15, v } }; + c1 = new() { { quw, v } }; } else { throw new InvalidOperationException(); } @@ -139,7 +140,10 @@ namespace Elwig.Helpers.Billing { if (geb is JsonObject) { c2 = GetCurveData(geb.AsObject(), cMode); } else if (geb?.AsValue().TryGetValue(out decimal v) == true) { - c2 = c1.ToDictionary(e => e.Key, e => e.Value + v); + var splitVal = GetCurveValueAt(c1, quw); + c2 = c1.ToDictionary(e => e.Key, e => e.Value + (e.Key >= quw ? v : 0)); + c2[quw] = splitVal + v; + c2[Math.BitDecrement(quw)] = splitVal; } dict.Add(id, new(cMode, c1, c2)); } @@ -181,5 +185,25 @@ namespace Elwig.Helpers.Billing { return dict; } + + public static decimal GetCurveValueAt(Dictionary curve, double key) { + if (curve.Count == 1) return curve.First().Value; + + var lt = curve.Keys.Where(v => v <= key); + var gt = curve.Keys.Where(v => v >= key); + if (!lt.Any()) { + return curve[gt.Min()]; + } else if (!gt.Any()) { + return curve[lt.Max()]; + } + + var max = lt.Max(); + var min = gt.Min(); + if (max == min) return curve[key]; + + var p1 = ((decimal)key - (decimal)min) / ((decimal)max - (decimal)min); + var p2 = 1 - p1; + return curve[min] * p2 + curve[max] * p1; + } } } diff --git a/Elwig/Helpers/Billing/PaymentBillingData.cs b/Elwig/Helpers/Billing/PaymentBillingData.cs index 0c1535d..75f622a 100644 --- a/Elwig/Helpers/Billing/PaymentBillingData.cs +++ b/Elwig/Helpers/Billing/PaymentBillingData.cs @@ -49,25 +49,7 @@ namespace Elwig.Helpers.Billing { public decimal CalculatePrice(string sortid, string? attrid, string qualid, bool gebunden, double oe, double kmw) { var curve = GetQualityCurve(qualid, sortid, attrid) ?? GetCurve(sortid, attrid); - var d = (gebunden ? curve.Gebunden : null) ?? curve.Normal; - if (d.Count == 1) return d.First().Value; - - var r = curve.Mode == CurveMode.Oe ? oe : kmw; - var lt = d.Keys.Where(v => v <= r); - var gt = d.Keys.Where(v => v >= r); - if (!lt.Any()) { - return d[gt.Min()]; - } else if (!gt.Any()) { - return d[lt.Max()]; - } - - var max = lt.Max(); - var min = gt.Min(); - if (max == min) return d[r]; - - var p1 = ((decimal)r - (decimal)min) / ((decimal)max - (decimal)min); - var p2 = 1 - p1; - return d[min] * p2 + d[max] * p1; + return GetCurveValueAt((gebunden ? curve.Gebunden : null) ?? curve.Normal, curve.Mode == CurveMode.Oe ? oe : kmw); } private Curve LookupCurve(JsonValue val) {