From cb541cb6e68e7180e5009a5bed7c59a809f2a40e Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 18 Jan 2024 21:30:42 +0100 Subject: [PATCH] Billing: Add EditBillingData class --- Elwig/Helpers/Billing/BillingData.cs | 4 +- Elwig/Helpers/Billing/EditBillingData.cs | 90 +++++++++++++++++++++ Elwig/Helpers/Billing/GraphEntry.cs | 18 +++-- Elwig/Windows/ChartWindow.xaml.cs | 30 ++----- Elwig/Windows/PaymentVariantsWindow.xaml.cs | 6 +- 5 files changed, 112 insertions(+), 36 deletions(-) create mode 100644 Elwig/Helpers/Billing/EditBillingData.cs diff --git a/Elwig/Helpers/Billing/BillingData.cs b/Elwig/Helpers/Billing/BillingData.cs index f32f371..f5ea89b 100644 --- a/Elwig/Helpers/Billing/BillingData.cs +++ b/Elwig/Helpers/Billing/BillingData.cs @@ -62,7 +62,7 @@ namespace Elwig.Helpers.Billing { Mode = (mode == "elwig") ? CalculationMode.Elwig : CalculationMode.WgMaster; } - public static JsonObject ParseJson(string json) { + protected static JsonObject ParseJson(string json) { if (Schema == null) throw new InvalidOperationException("Schema has to be initialized first"); try { var errors = Schema.Validate(json); @@ -117,7 +117,7 @@ namespace Elwig.Helpers.Billing { return dict; } - public Dictionary GetCurves() { + protected Dictionary GetCurves() { var dict = new Dictionary(); var curves = GetCurvesEntry(); foreach (var c in curves) { diff --git a/Elwig/Helpers/Billing/EditBillingData.cs b/Elwig/Helpers/Billing/EditBillingData.cs new file mode 100644 index 0000000..c3b8f1e --- /dev/null +++ b/Elwig/Helpers/Billing/EditBillingData.cs @@ -0,0 +1,90 @@ +using System.Collections.Generic; +using System.Linq; +using System.Text.Json.Nodes; + +namespace Elwig.Helpers.Billing { + public class EditBillingData : BillingData { + + protected readonly IEnumerable AttributeVariants; + + public EditBillingData(JsonObject data, IEnumerable attributeVariants) : + base(data) { + AttributeVariants = attributeVariants; + } + + public static EditBillingData FromJson(string json, IEnumerable attributeVariants) { + return new(ParseJson(json), attributeVariants); + } + + public IEnumerable GetPaymentGraphEntries() { + Dictionary> dict1 = []; + Dictionary> dict2 = []; + var p = GetPaymentEntry(); + if (p is JsonObject paymentObj) { + foreach (var (selector, node) in paymentObj) { + var val = node?.AsValue(); + if (val == null) { + continue; + } else if (val.TryGetValue(out var price)) { + if (!dict2.ContainsKey(price)) dict2[price] = []; + dict2[price].Add(selector); + } else if (val.TryGetValue(out var curve)) { + var idx = int.Parse(curve.Split(":")[1] ?? "0"); + if (!dict1.ContainsKey(idx)) dict1[idx] = []; + dict1[idx].Add(selector); + } + } + } else if (p is JsonValue paymentVal) { + var idx = paymentVal.GetValue(); + if (!dict2.ContainsKey(idx)) dict2[idx] = []; + dict2[idx].Add("default"); + } + + Dictionary curves = GetCurves(); + decimal[] virtCurves = [.. dict2.Keys.Order()]; + for (int i = 0; i < virtCurves.Length; i++) { + var idx = virtCurves[i]; + dict1[1000 + i] = dict2[idx]; + curves[1000 + i] = new Curve(CurveMode.Oe, new() { { 73, idx } }, null); + } + + Dictionary> dict3 = []; + + + + return dict3.Select(e => new GraphEntry(e.Key, curves[e.Key], 50, 120)).ToList(); + } + + public IEnumerable GetQualityGraphEntries() { + Dictionary> dict1 = []; + Dictionary> dict2 = []; + foreach (var (qualid, q) in GetQualityEntry() ?? []) { + if (q is JsonObject qualityObj) { + foreach (var (selector, node) in qualityObj) { + var val = node?.AsValue(); + if (val == null) { + continue; + } else if (val.TryGetValue(out var price)) { + if (!dict2.ContainsKey(price)) dict2[price] = []; + dict2[price].Add(selector); + } else if (val.TryGetValue(out var curve)) { + var idx = int.Parse(curve.Split(":")[1] ?? "0"); + if (!dict1.ContainsKey(idx)) dict1[idx] = []; + dict1[idx].Add(selector); + } + } + } else if (q is JsonValue qualityVal) { + var idx = qualityVal.GetValue(); + if (!dict2.ContainsKey(idx)) dict2[idx] = []; + dict2[idx].Add($"{qualid}/"); + } + } + + // TODO + + List list = []; + + return list; + } + } +} diff --git a/Elwig/Helpers/Billing/GraphEntry.cs b/Elwig/Helpers/Billing/GraphEntry.cs index d9ff337..4dc37b9 100644 --- a/Elwig/Helpers/Billing/GraphEntry.cs +++ b/Elwig/Helpers/Billing/GraphEntry.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; using System.Text.Json.Nodes; -using System.Threading.Tasks; namespace Elwig.Helpers.Billing { public class GraphEntry { @@ -26,12 +22,20 @@ namespace Elwig.Helpers.Billing { Contracts = []; } - public GraphEntry(int id, BillingData.CurveMode mode, Dictionary data, int minX, int maxX) : this(id, mode, minX, maxX) { + public GraphEntry(int id, BillingData.CurveMode mode, Dictionary data, int minX, int maxX) : + this(id, mode, minX, maxX) { DataGraph = new Graph(data, minX, maxX); } + public GraphEntry(int id, BillingData.Curve curve, int minX, int maxX) : + this(id, curve.Mode, minX, maxX) { + DataGraph = new Graph(curve.Normal, minX, maxX); + if (curve.Gebunden != null) + GebundenGraph = new Graph(curve.Gebunden, minX, maxX); + } + private GraphEntry(int id, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph, - decimal? gebundenFlatPrice, List contracts, int minX, int maxX) { + decimal? gebundenFlatPrice, List contracts, int minX, int maxX) { Id = id; Mode = mode; MinX = minX; diff --git a/Elwig/Windows/ChartWindow.xaml.cs b/Elwig/Windows/ChartWindow.xaml.cs index c97bd34..32c8ef0 100644 --- a/Elwig/Windows/ChartWindow.xaml.cs +++ b/Elwig/Windows/ChartWindow.xaml.cs @@ -59,24 +59,7 @@ namespace Elwig.Windows { await RefreshGraphListQuery(); } - private static JsonObject? ParseData(PaymentVar variant) { - try { - return BillingData.ParseJson(variant.Data); - } catch (ArgumentException) { - return null; - } - } - - private async Task RefreshGraphListQuery(bool updateSort = false) { - var data = ParseData(PaymentVar); - if (data == null) return; - - var curves = PaymentBillingData.GetCurves(data, BillingData.CalculationMode.Elwig); - - foreach (var (id, curve) in curves) { - GraphEntries.Add(new GraphEntry(id, curve.Mode, curve.Normal, MinOechsle, MaxOechsle)); - } - + private async Task RefreshGraphListQuery() { var attrVariants = Context.DeliveryParts .Where(d => d.Year == Year) .Select(d => $"{d.SortId}{d.AttrId}") @@ -85,12 +68,12 @@ namespace Elwig.Windows { .Union(Context.WineVarieties.Select(v => v.SortId)) .Order() .ToList(); - ControlUtils.RenewItemsSource(AppliedInput, attrVariants, g => (g as GraphEntry)?.Id); + var data = EditBillingData.FromJson(PaymentVar.Data, attrVariants); + GraphEntries.AddRange(data.GetPaymentGraphEntries()); + GraphEntries.AddRange(data.GetQualityGraphEntries()); - ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.Id); - if (GraphEntries.Count == 1) { - GraphList.SelectedIndex = 0; - } + ControlUtils.RenewItemsSource(AppliedInput, attrVariants, g => g); + ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.Id, null, ControlUtils.RenewSourceDefault.IfOnly); RefreshInputs(); } @@ -98,7 +81,6 @@ namespace Elwig.Windows { private string ParseContracts(JsonObject auszahlungsSorten, int num) { return ""; } - private void RefreshInputs(bool validate = false) { ResetPlot(); diff --git a/Elwig/Windows/PaymentVariantsWindow.xaml.cs b/Elwig/Windows/PaymentVariantsWindow.xaml.cs index 0594ff7..28355e8 100644 --- a/Elwig/Windows/PaymentVariantsWindow.xaml.cs +++ b/Elwig/Windows/PaymentVariantsWindow.xaml.cs @@ -396,13 +396,13 @@ namespace Elwig.Windows { return; } try { - var json = BillingData.ParseJson(DataInput.Text); + var data = BillingData.FromJson(DataInput.Text); var origJson = v.Data; try { - origJson = JsonSerializer.Serialize(BillingData.ParseJson(v.Data)); + origJson = JsonSerializer.Serialize(BillingData.FromJson(v.Data).Data); } catch { } DataValid = true; - if (JsonSerializer.Serialize(json) != origJson) { + if (JsonSerializer.Serialize(data.Data) != origJson) { ControlUtils.SetInputChanged(DataInput); DataChanged = true; } else {