Billing: Add EditBillingData class
This commit is contained in:
@ -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<int, Curve> GetCurves() {
|
||||
protected Dictionary<int, Curve> GetCurves() {
|
||||
var dict = new Dictionary<int, Curve>();
|
||||
var curves = GetCurvesEntry();
|
||||
foreach (var c in curves) {
|
||||
|
90
Elwig/Helpers/Billing/EditBillingData.cs
Normal file
90
Elwig/Helpers/Billing/EditBillingData.cs
Normal file
@ -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<string> AttributeVariants;
|
||||
|
||||
public EditBillingData(JsonObject data, IEnumerable<string> attributeVariants) :
|
||||
base(data) {
|
||||
AttributeVariants = attributeVariants;
|
||||
}
|
||||
|
||||
public static EditBillingData FromJson(string json, IEnumerable<string> attributeVariants) {
|
||||
return new(ParseJson(json), attributeVariants);
|
||||
}
|
||||
|
||||
public IEnumerable<GraphEntry> GetPaymentGraphEntries() {
|
||||
Dictionary<int, List<string>> dict1 = [];
|
||||
Dictionary<decimal, List<string>> 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<decimal>(out var price)) {
|
||||
if (!dict2.ContainsKey(price)) dict2[price] = [];
|
||||
dict2[price].Add(selector);
|
||||
} else if (val.TryGetValue<string>(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<decimal>();
|
||||
if (!dict2.ContainsKey(idx)) dict2[idx] = [];
|
||||
dict2[idx].Add("default");
|
||||
}
|
||||
|
||||
Dictionary<int, Curve> 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<int, List<string>> dict3 = [];
|
||||
|
||||
|
||||
|
||||
return dict3.Select(e => new GraphEntry(e.Key, curves[e.Key], 50, 120)).ToList();
|
||||
}
|
||||
|
||||
public IEnumerable<GraphEntry> GetQualityGraphEntries() {
|
||||
Dictionary<int, List<string>> dict1 = [];
|
||||
Dictionary<decimal, List<string>> 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<decimal>(out var price)) {
|
||||
if (!dict2.ContainsKey(price)) dict2[price] = [];
|
||||
dict2[price].Add(selector);
|
||||
} else if (val.TryGetValue<string>(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<decimal>();
|
||||
if (!dict2.ContainsKey(idx)) dict2[idx] = [];
|
||||
dict2[idx].Add($"{qualid}/");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
|
||||
List<GraphEntry> list = [];
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
@ -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,10 +22,18 @@ namespace Elwig.Helpers.Billing {
|
||||
Contracts = [];
|
||||
}
|
||||
|
||||
public GraphEntry(int id, BillingData.CurveMode mode, Dictionary<double, decimal> data, int minX, int maxX) : this(id, mode, minX, maxX) {
|
||||
public GraphEntry(int id, BillingData.CurveMode mode, Dictionary<double, decimal> 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<string> contracts, int minX, int maxX) {
|
||||
Id = id;
|
||||
|
@ -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();
|
||||
}
|
||||
@ -99,7 +82,6 @@ namespace Elwig.Windows {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
private void RefreshInputs(bool validate = false) {
|
||||
ResetPlot();
|
||||
if (!PaymentVar.TestVariant) {
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user