Billing: Add EditBillingData class
This commit is contained in:
@ -62,7 +62,7 @@ namespace Elwig.Helpers.Billing {
|
|||||||
Mode = (mode == "elwig") ? CalculationMode.Elwig : CalculationMode.WgMaster;
|
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");
|
if (Schema == null) throw new InvalidOperationException("Schema has to be initialized first");
|
||||||
try {
|
try {
|
||||||
var errors = Schema.Validate(json);
|
var errors = Schema.Validate(json);
|
||||||
@ -117,7 +117,7 @@ namespace Elwig.Helpers.Billing {
|
|||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<int, Curve> GetCurves() {
|
protected Dictionary<int, Curve> GetCurves() {
|
||||||
var dict = new Dictionary<int, Curve>();
|
var dict = new Dictionary<int, Curve>();
|
||||||
var curves = GetCurvesEntry();
|
var curves = GetCurvesEntry();
|
||||||
foreach (var c in curves) {
|
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.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json.Nodes;
|
using System.Text.Json.Nodes;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Elwig.Helpers.Billing {
|
namespace Elwig.Helpers.Billing {
|
||||||
public class GraphEntry {
|
public class GraphEntry {
|
||||||
@ -26,12 +22,20 @@ namespace Elwig.Helpers.Billing {
|
|||||||
Contracts = [];
|
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);
|
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,
|
private GraphEntry(int id, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph,
|
||||||
decimal? gebundenFlatPrice, List<string> contracts, int minX, int maxX) {
|
decimal? gebundenFlatPrice, List<string> contracts, int minX, int maxX) {
|
||||||
Id = id;
|
Id = id;
|
||||||
Mode = mode;
|
Mode = mode;
|
||||||
MinX = minX;
|
MinX = minX;
|
||||||
|
@ -59,24 +59,7 @@ namespace Elwig.Windows {
|
|||||||
await RefreshGraphListQuery();
|
await RefreshGraphListQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JsonObject? ParseData(PaymentVar variant) {
|
private async Task RefreshGraphListQuery() {
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
var attrVariants = Context.DeliveryParts
|
var attrVariants = Context.DeliveryParts
|
||||||
.Where(d => d.Year == Year)
|
.Where(d => d.Year == Year)
|
||||||
.Select(d => $"{d.SortId}{d.AttrId}")
|
.Select(d => $"{d.SortId}{d.AttrId}")
|
||||||
@ -85,12 +68,12 @@ namespace Elwig.Windows {
|
|||||||
.Union(Context.WineVarieties.Select(v => v.SortId))
|
.Union(Context.WineVarieties.Select(v => v.SortId))
|
||||||
.Order()
|
.Order()
|
||||||
.ToList();
|
.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);
|
ControlUtils.RenewItemsSource(AppliedInput, attrVariants, g => g);
|
||||||
if (GraphEntries.Count == 1) {
|
ControlUtils.RenewItemsSource(GraphList, GraphEntries, g => (g as GraphEntry)?.Id, null, ControlUtils.RenewSourceDefault.IfOnly);
|
||||||
GraphList.SelectedIndex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
RefreshInputs();
|
RefreshInputs();
|
||||||
}
|
}
|
||||||
@ -98,7 +81,6 @@ namespace Elwig.Windows {
|
|||||||
private string ParseContracts(JsonObject auszahlungsSorten, int num) {
|
private string ParseContracts(JsonObject auszahlungsSorten, int num) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void RefreshInputs(bool validate = false) {
|
private void RefreshInputs(bool validate = false) {
|
||||||
ResetPlot();
|
ResetPlot();
|
||||||
|
@ -396,13 +396,13 @@ namespace Elwig.Windows {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
var json = BillingData.ParseJson(DataInput.Text);
|
var data = BillingData.FromJson(DataInput.Text);
|
||||||
var origJson = v.Data;
|
var origJson = v.Data;
|
||||||
try {
|
try {
|
||||||
origJson = JsonSerializer.Serialize(BillingData.ParseJson(v.Data));
|
origJson = JsonSerializer.Serialize(BillingData.FromJson(v.Data).Data);
|
||||||
} catch { }
|
} catch { }
|
||||||
DataValid = true;
|
DataValid = true;
|
||||||
if (JsonSerializer.Serialize(json) != origJson) {
|
if (JsonSerializer.Serialize(data.Data) != origJson) {
|
||||||
ControlUtils.SetInputChanged(DataInput);
|
ControlUtils.SetInputChanged(DataInput);
|
||||||
DataChanged = true;
|
DataChanged = true;
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user