219 lines
9.6 KiB
C#
219 lines
9.6 KiB
C#
using Newtonsoft.Json;
|
|
using NJsonSchema;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text.Json.Nodes;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Elwig.Helpers.Billing {
|
|
public class BillingData {
|
|
|
|
public enum CalculationMode { Elwig, WgMaster }
|
|
public enum CurveMode { Oe, Kmw }
|
|
|
|
public record struct Curve(CurveMode Mode, Dictionary<double, decimal> Normal, Dictionary<double, decimal>? Gebunden);
|
|
|
|
public static JsonSchema? Schema { get; private set; }
|
|
|
|
public static async Task Init() {
|
|
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Elwig.Resources.Schemas.PaymentVariantData.json");
|
|
Schema = await JsonSchema.FromJsonAsync(stream ?? throw new ArgumentException("JSON schema not found"));
|
|
}
|
|
|
|
private readonly CalculationMode Mode;
|
|
private readonly JsonObject Data;
|
|
private readonly Dictionary<int, Curve> Curves;
|
|
private readonly Dictionary<string, Curve> PaymentData;
|
|
private readonly Dictionary<string, Curve> QualityData;
|
|
|
|
public BillingData(JsonObject data, IEnumerable<string> attributeVariants) {
|
|
if (attributeVariants.Any(e => e.Any(c => c < 'A' || c > 'Z')))
|
|
throw new ArgumentException("Invalid attributeVariants");
|
|
Data = data;
|
|
var mode = Data["mode"]?.GetValue<string>();
|
|
Mode = (mode == "elwig") ? CalculationMode.Elwig : CalculationMode.WgMaster;
|
|
Curves = GetCurves(Data, Mode);
|
|
PaymentData = GetPaymentData(attributeVariants);
|
|
QualityData = GetQualityData(attributeVariants);
|
|
}
|
|
|
|
public static JsonObject ParseJson(string json) {
|
|
if (Schema == null) throw new InvalidOperationException("Schema has to be initialized first");
|
|
try {
|
|
var errors = Schema.Validate(json);
|
|
if (errors.Count != 0) throw new ArgumentException("Invalid JSON data");
|
|
return JsonNode.Parse(json)?.AsObject() ?? throw new ArgumentException("Invalid JSON data");
|
|
} catch (JsonReaderException) {
|
|
throw new ArgumentException("Invalid JSON data");
|
|
}
|
|
}
|
|
|
|
public static BillingData FromJson(string json, IEnumerable<string> attributeVariants) {
|
|
return new(ParseJson(json), attributeVariants);
|
|
}
|
|
|
|
private static Dictionary<double, decimal> GetCurveData(JsonObject data, CurveMode mode) {
|
|
var dict = new Dictionary<double, decimal>();
|
|
foreach (var (index, price) in data) {
|
|
double idx;
|
|
bool? gtlt = index.StartsWith('>') ? true : index.StartsWith('<') ? false : null;
|
|
if (index.EndsWith("kmw")) {
|
|
idx = double.Parse(index[(gtlt != null ? 1 : 0)..^3], CultureInfo.InvariantCulture);
|
|
if (mode == CurveMode.Oe) {
|
|
idx = Utils.KmwToOe(idx);
|
|
}
|
|
} else if (index.EndsWith("oe")) {
|
|
idx = double.Parse(index[(gtlt != null ? 1 : 0)..^2], CultureInfo.InvariantCulture);
|
|
if (mode == CurveMode.Kmw) {
|
|
idx = Utils.OeToKmw(idx);
|
|
}
|
|
} else {
|
|
throw new InvalidOperationException();
|
|
}
|
|
if (gtlt == true) {
|
|
idx = Math.BitIncrement(idx);
|
|
} else if (gtlt == false) {
|
|
idx = Math.BitDecrement(idx);
|
|
}
|
|
dict[idx] = price?.AsValue().GetValue<decimal>() ?? throw new InvalidOperationException();
|
|
}
|
|
return dict;
|
|
}
|
|
|
|
public static Dictionary<int, Curve> GetCurves(JsonObject data, CalculationMode mode) {
|
|
var dict = new Dictionary<int, Curve>();
|
|
var curves = data[mode == CalculationMode.Elwig ? "curves" : "Kurven"]?.AsArray() ?? throw new InvalidOperationException();
|
|
foreach (var c in curves) {
|
|
var obj = c?.AsObject() ?? throw new InvalidOperationException();
|
|
var id = obj["id"]?.GetValue<int>() ?? throw new InvalidOperationException();
|
|
var cMode = (obj["mode"]?.GetValue<string>() == "kmw") ? CurveMode.Kmw : CurveMode.Oe;
|
|
|
|
Dictionary<double, decimal> c1;
|
|
Dictionary<double, decimal>? c2 = null;
|
|
var norm = obj["data"];
|
|
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 } };
|
|
} else {
|
|
throw new InvalidOperationException();
|
|
}
|
|
var geb = obj["geb"];
|
|
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);
|
|
}
|
|
dict.Add(id, new(cMode, c1, c2));
|
|
}
|
|
return dict;
|
|
}
|
|
|
|
private Dictionary<string, Curve> GetData(JsonObject data, IEnumerable<string> attributeVariants) {
|
|
Dictionary<string, Curve> dict;
|
|
if (data["default"] is JsonValue def) {
|
|
var c = LookupCurve(def);
|
|
dict = attributeVariants.ToDictionary(e => e, _ => c);
|
|
} else {
|
|
dict = [];
|
|
}
|
|
|
|
var variants = data.Where(p => !p.Key.StartsWith('/') && p.Key.Length == 2);
|
|
var attributes = data.Where(p => p.Key.StartsWith('/'));
|
|
var others = data.Where(p => !p.Key.StartsWith('/') && p.Key.Length > 2);
|
|
foreach (var (idx, v) in variants) {
|
|
var curve = LookupCurve(v?.AsValue() ?? throw new InvalidOperationException());
|
|
foreach (var i in attributeVariants.Where(e => e.StartsWith(idx[..^1]))) {
|
|
dict[i] = curve;
|
|
}
|
|
}
|
|
foreach (var (idx, v) in attributes) {
|
|
var curve = LookupCurve(v?.AsValue() ?? throw new InvalidOperationException());
|
|
foreach (var i in attributeVariants.Where(e => e[2..] == idx[1..])) {
|
|
dict[i] = curve;
|
|
}
|
|
}
|
|
foreach (var (idx, v) in others) {
|
|
var curve = LookupCurve(v?.AsValue() ?? throw new InvalidOperationException());
|
|
dict[idx.Replace("/", "")] = curve;
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
|
|
public Dictionary<string, Curve> GetPaymentData(IEnumerable<string> attributeVariants) {
|
|
var p = Data[Mode == CalculationMode.Elwig ? "payment" : "AuszahlungSorten"];
|
|
if (p is JsonValue val) {
|
|
var c = LookupCurve(val);
|
|
return attributeVariants.ToDictionary(e => e, _ => c);
|
|
}
|
|
return GetData(p?.AsObject() ?? throw new InvalidOperationException(), attributeVariants);
|
|
}
|
|
|
|
public Dictionary<string, Curve> GetQualityData(IEnumerable<string> attributeVariants) {
|
|
var q = Data[Mode == CalculationMode.Elwig ? "quality" : "AuszahlungSortenQualitätsstufe"]?.AsObject();
|
|
Dictionary<string, Curve> dict = [];
|
|
if (q == null) return dict;
|
|
|
|
foreach (var (qualid, data) in q) {
|
|
Dictionary<string, Curve> qualDict;
|
|
if (data is JsonValue val) {
|
|
var c = LookupCurve(val);
|
|
qualDict = attributeVariants.ToDictionary(e => e, _ => c);
|
|
} else {
|
|
qualDict = GetData(data?.AsObject() ?? throw new InvalidOperationException(), attributeVariants);
|
|
}
|
|
foreach (var (idx, d) in qualDict) {
|
|
dict[$"{qualid}/{idx}"] = d;
|
|
}
|
|
}
|
|
|
|
return dict;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private Curve LookupCurve(JsonValue val) {
|
|
if (val.TryGetValue(out string? curve)) {
|
|
var curveId = int.Parse(curve.Split(":")[1]);
|
|
return Curves[curveId];
|
|
} else if (val.TryGetValue(out decimal value)) {
|
|
return new(CurveMode.Oe, new() { { 73, value } }, null);
|
|
}
|
|
throw new InvalidOperationException();
|
|
}
|
|
|
|
public Curve GetCurve(string sortid, string? attrid) {
|
|
return PaymentData[$"{sortid}{attrid ?? ""}"];
|
|
}
|
|
|
|
public Curve? GetQualityCurve(string qualid, string sortid, string? attrid) {
|
|
return QualityData.TryGetValue($"{qualid}/{sortid}{attrid ?? ""}", out var curve) ? curve : null;
|
|
}
|
|
}
|
|
}
|