Billing: Build BillingData-Json in BillingData instead of anywhere else
This commit is contained in:
@ -205,5 +205,75 @@ namespace Elwig.Helpers.Billing {
|
||||
var p2 = 1 - p1;
|
||||
return curve[min] * p2 + curve[max] * p1;
|
||||
}
|
||||
|
||||
protected static JsonNode GraphToJson(Graph graph, string mode) {
|
||||
var x = graph.DataX;
|
||||
var y = graph.DataY;
|
||||
if (y.Distinct().Count() == 1) {
|
||||
return JsonValue.Create(graph.DataY[0]);
|
||||
}
|
||||
|
||||
var data = new JsonObject();
|
||||
|
||||
if (y[0] != y[1]) {
|
||||
data.Add(new KeyValuePair<string, JsonNode?>(x[0] + mode, Math.Round(y[0], graph.Precision)));
|
||||
}
|
||||
for (int i = 1; i < x.Length - 1; i++) {
|
||||
if (Math.Round(y[i] - y[i - 1], 10) != Math.Round(y[i + 1] - y[i], 10)) {
|
||||
data.Add(new KeyValuePair<string, JsonNode?>(x[i] + mode, Math.Round(y[i], graph.Precision)));
|
||||
}
|
||||
}
|
||||
if (y[^1] != y[^2]) {
|
||||
data.Add(new KeyValuePair<string, JsonNode?>(x[^1] + mode, Math.Round(y[^1], graph.Precision)));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
protected static JsonObject GraphEntryToJson(GraphEntry entry) {
|
||||
var curve = new JsonObject {
|
||||
["id"] = entry.Id,
|
||||
["mode"] = entry.Mode.ToString().ToLower(),
|
||||
};
|
||||
|
||||
curve["data"] = GraphToJson(entry.DataGraph, entry.Mode.ToString().ToLower());
|
||||
|
||||
if (entry.GebundenFlatBonus != null) {
|
||||
curve["geb"] = entry.GebundenFlatBonus;
|
||||
} else if (entry.GebundenGraph != null) {
|
||||
curve["geb"] = GraphToJson(entry.GebundenGraph, entry.Mode.ToString().ToLower());
|
||||
}
|
||||
|
||||
return curve;
|
||||
}
|
||||
|
||||
public JsonObject FromGraphEntries(IEnumerable<GraphEntry> graphEntries) {
|
||||
var payment = new JsonObject();
|
||||
var curves = new JsonArray();
|
||||
|
||||
foreach (var entry in graphEntries) {
|
||||
curves.Add(GraphEntryToJson(entry));
|
||||
foreach (var contract in entry.Contracts) {
|
||||
payment[$"{contract.Variety?.SortId}/{contract.Attribute?.AttrId}"] = $"curve:{entry.Id}";
|
||||
}
|
||||
}
|
||||
|
||||
var data = new JsonObject {
|
||||
["mode"] = "elwig",
|
||||
["version"] = 1,
|
||||
};
|
||||
if (ConsiderDelieryModifiers)
|
||||
data["consider_delivery_modifiers"] = true;
|
||||
if (ConsiderContractPenalties)
|
||||
data["consider_contract_penalties"] = true;
|
||||
if (ConsiderTotalPenalty)
|
||||
data["consider_total_penalty"] = true;
|
||||
if (ConsiderAutoBusinessShares)
|
||||
data["consider_auto_business_shares"] = true;
|
||||
|
||||
data["payment"] = payment;
|
||||
data["curves"] = curves;
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user