Billing: Add BillingData and JSON schema validation
This commit is contained in:
@ -16,6 +16,7 @@ using Elwig.Helpers.Printing;
|
||||
using Elwig.Windows;
|
||||
using Elwig.Dialogs;
|
||||
using System.Threading.Tasks;
|
||||
using Elwig.Helpers.Billing;
|
||||
|
||||
namespace Elwig {
|
||||
public partial class App : Application {
|
||||
@ -104,6 +105,7 @@ namespace Elwig {
|
||||
|
||||
Utils.RunBackground("HTML Initialization", () => Html.Init(PrintingReadyChanged));
|
||||
Utils.RunBackground("PDF Initialization", () => Pdf.Init(PrintingReadyChanged));
|
||||
Utils.RunBackground("JSON Schema Initialization", BillingData.Init);
|
||||
|
||||
var list = new List<IScale>();
|
||||
foreach (var s in Config.Scales) {
|
||||
|
@ -14,6 +14,7 @@
|
||||
<ItemGroup>
|
||||
<Resource Include="Resources\Images\Elwig.png" />
|
||||
<Content Include="Resources\Images\Elwig.ico" />
|
||||
<EmbeddedResource Include="Schemas\payment_variant.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
|
||||
@ -28,6 +29,7 @@
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2151.40" />
|
||||
<PackageReference Include="NJsonSchema" Version="11.0.0" />
|
||||
<PackageReference Include="RazorLight" Version="2.3.1" />
|
||||
<PackageReference Include="ScottPlot.WPF" Version="4.1.68" />
|
||||
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
|
||||
|
44
Elwig/Helpers/Billing/BillingData.cs
Normal file
44
Elwig/Helpers/Billing/BillingData.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using Newtonsoft.Json;
|
||||
using NJsonSchema;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Elwig.Helpers.Billing {
|
||||
public class BillingData {
|
||||
|
||||
public static JsonSchema? Schema { get; private set; }
|
||||
|
||||
public static async Task Init() {
|
||||
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Elwig.Schemas.payment_variant.json");
|
||||
Schema = await JsonSchema.FromJsonAsync(stream ?? throw new ArgumentException("JSON schema not found"));
|
||||
}
|
||||
|
||||
private JsonObject Data;
|
||||
|
||||
public BillingData(JsonObject data) {
|
||||
Data = data;
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
return new(ParseJson(json));
|
||||
}
|
||||
|
||||
public decimal CalculatePrice(string sortId, string? attrid, string qualId, bool gebunden, double oe, double kmw, bool minQuw) {
|
||||
return 0m;
|
||||
}
|
||||
}
|
||||
}
|
135
Elwig/Schemas/payment_variant.json
Normal file
135
Elwig/Schemas/payment_variant.json
Normal file
@ -0,0 +1,135 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"required": ["mode"],
|
||||
"anyOf": [{
|
||||
"required": ["version", "payment", "curves"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"mode": {"enum": ["elwig"]},
|
||||
"version": {"enum": [1]},
|
||||
"payment": {
|
||||
"type": ["number", "string", "object"],
|
||||
"pattern": "^curve:[0-9]+$",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"default": {
|
||||
"type": ["number", "string"],
|
||||
"pattern": "^curve:[0-9]+$"
|
||||
}
|
||||
},
|
||||
"patternProperties": {
|
||||
"^[A-Z]+$": {
|
||||
"type": ["number", "string"],
|
||||
"pattern": "^curve:[0-9]+$"
|
||||
}
|
||||
}
|
||||
},
|
||||
"quality": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"patternProperties": {
|
||||
"^[A-Z]{3}$": {
|
||||
"type": ["number", "string", "object"],
|
||||
"pattern": "^curve:[0-9]+$",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"default": {
|
||||
"type": ["number", "string"],
|
||||
"pattern": "^curve:[0-9]+$"
|
||||
}
|
||||
},
|
||||
"patternProperties": {
|
||||
"^[A-Z]+$": {
|
||||
"type": ["number", "string"],
|
||||
"pattern": "^curve:[0-9]+$"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"curves": {
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/definitions/curve"}
|
||||
}
|
||||
}
|
||||
}, {
|
||||
"properties": {
|
||||
"mode": {"enum": ["wgmaster"]},
|
||||
"AuszahlungSorten": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["Kurven"],
|
||||
"properties": {
|
||||
"Kurven": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"minProperties": 1,
|
||||
"patternProperties": {
|
||||
"^[0-9]+(\\.[0-9]+)?oe$": {"type": "number"}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"patternProperties": {
|
||||
"^[A-Z]{2}$": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"patternProperties": {
|
||||
"^[A-Z]*$": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"Gebunden": {
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
},
|
||||
"NichtGebunden": {
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}],
|
||||
"definitions": {
|
||||
"curve": {
|
||||
"type": "object",
|
||||
"required": ["id", "mode", "data"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
},
|
||||
"mode": {"enum": ["oe", "kmw"]},
|
||||
"data": {
|
||||
"anyOf": [
|
||||
{"type": "number" },
|
||||
{"$ref": "#/definitions/curve_data"}
|
||||
]
|
||||
},
|
||||
"geb": {
|
||||
"anyOf": [
|
||||
{"type": "number"},
|
||||
{"$ref": "#/definitions/curve_data"}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"curve_data": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"minProperties": 1,
|
||||
"patternProperties": {
|
||||
"^([0-9]+(\\.[0-9]+)?)(oe|kmw)$": {"type": "number"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -63,8 +63,8 @@ namespace Elwig.Windows {
|
||||
|
||||
private static JsonObject? ParseData(PaymentVar variant) {
|
||||
try {
|
||||
return JsonNode.Parse(variant.Data)?.AsObject();
|
||||
} catch (JsonException) {
|
||||
return BillingData.ParseJson(variant.Data);
|
||||
} catch (ArgumentException) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user