diff --git a/Elwig/Helpers/Billing/ContractSelection.cs b/Elwig/Helpers/Billing/ContractSelection.cs new file mode 100644 index 0000000..4798f01 --- /dev/null +++ b/Elwig/Helpers/Billing/ContractSelection.cs @@ -0,0 +1,47 @@ +using Elwig.Models.Entities; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Elwig.Helpers.Billing { + public class ContractSelection : IComparable { + + public WineVar? Variety { get; } + public WineAttr? Attribute { get; } + public string Listing => Variety != null || Attribute != null ? $"{Variety?.SortId}{Attribute?.AttrId}" : ""; + + public ContractSelection(WineVar? var, WineAttr? attr) { + Variety = var; + Attribute = attr; + } + + public ContractSelection(WineVar var) { + Variety = var; + } + + public ContractSelection(WineAttr attr) { + Attribute = attr; + } + + public override string ToString() { + return (Variety != null ? $"{Variety.Name}" : "") + (Attribute != null ? $" {Attribute.Name}" : ""); + } + + public static List GetContractsForYear(AppDbContext context, int year) { + return context.DeliveryParts + .Where(d => d.Year == year) + .Select(d => new ContractSelection(d.Variant, d.Attribute)) + .Distinct() + .ToList() + .Union(context.WineVarieties.Select(v => new ContractSelection(v))) + .ToList(); + } + + public int CompareTo(ContractSelection? other) { + //MessageBox.Show($"{Listing} -- {other.Listing} : {Listing.CompareTo(other.Listing)}"); + return other != null ? + Listing.CompareTo(other.Listing) : + throw new ArgumentException(); + } + } +} diff --git a/Elwig/Helpers/Billing/Graph.cs b/Elwig/Helpers/Billing/Graph.cs index 7e213ea..95aa636 100644 --- a/Elwig/Helpers/Billing/Graph.cs +++ b/Elwig/Helpers/Billing/Graph.cs @@ -26,6 +26,22 @@ namespace Elwig.Helpers.Billing { DataY = dataY; } + public double GetOechsleAt(int index) { + return DataX[index]; + } + + public void SetOechsleAt(int index, double oechsle) { + DataX[index] = oechsle; + } + + public void SetPriceAt(int index, double price) { + DataY[index] = price; + } + + public double GetPriceAt(int index) { + return DataY[index]; + } + private void ParseGraphData(Dictionary graphPoints, int minX, int maxX) { if (graphPoints.Keys.Count < 1) { return; @@ -71,19 +87,48 @@ namespace Elwig.Helpers.Billing { } } - public void FlattenGraph(int begin, int end, double value) { + private void FlattenGraph(int begin, int end, double value) { for (int i = begin; i <= end; i++) { DataY[i] = value; } } - public void LinearIncreaseGraph(int begin, int end, double inc) { + public void FlattenGraphLeft(int pointIndex) { + FlattenGraph(0, pointIndex, DataY[pointIndex]); + } + + public void FlattenGraphRight(int pointIndex) { + FlattenGraph(pointIndex, DataY.Length - 1, DataY[pointIndex]); + } + + private void LinearIncreaseGraph(int begin, int end, double inc) { for (int i = begin; i < end; i++) { - DataY[i + 1] = DataY[i] + inc; + DataY[i + 1] = Math.Round(DataY[i] + inc, 4); //TODO richtig runden } } - public JsonObject ToJson(string mode) { + public void LinearIncreaseGraphToEnd(int begin, double inc) { + LinearIncreaseGraph(begin, DataY.Length - 1, inc); + } + + public void InterpolateGraph(int firstPoint, int secondPoint) { + int steps = Math.Abs(firstPoint - secondPoint); + if (firstPoint == -1 || secondPoint == -1 || steps < 2) { + return; + } + var (lowIndex, highIndex) = firstPoint < secondPoint ? (firstPoint, secondPoint) : (secondPoint, firstPoint); + double step = (DataY[highIndex] - DataY[lowIndex]) / steps; + + for (int i = lowIndex; i < highIndex - 1; i++) { + DataY[i + 1] = Math.Round(DataY[i] + step, 4); // TODO richtig runden + } + } + + public JsonNode ToJson(string mode) { + if (DataY.Distinct().Count() == 1) { + return JsonValue.Create(DataY[0]); + } + var data = new JsonObject(); if (DataY[0] != DataY[1]) { diff --git a/Elwig/Helpers/Billing/GraphEntry.cs b/Elwig/Helpers/Billing/GraphEntry.cs index 4dc37b9..05dae82 100644 --- a/Elwig/Helpers/Billing/GraphEntry.cs +++ b/Elwig/Helpers/Billing/GraphEntry.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Text.Json.Nodes; namespace Elwig.Helpers.Billing { @@ -8,8 +9,10 @@ namespace Elwig.Helpers.Billing { public BillingData.CurveMode Mode { get; set; } public Graph DataGraph { get; set; } public Graph? GebundenGraph { get; set; } - public decimal? GebundenFlatPrice { get; set; } - public List Contracts { get; set; } + public decimal? GebundenFlatBonus { get; set; } + public List Contracts { get; set; } + public string ContractsStringSimple => Contracts.Any() ? string.Join(", ", Contracts.Select(c => c.Listing)) : "-"; + public string ContractsString => Contracts.Any() ? string.Join("\n", Contracts.Select(c => c.ToString())) : "-"; private int MinX { get; set; } private int MaxX { get; set; } @@ -22,9 +25,10 @@ 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, Dictionary? gebunden, + int minX, int maxX) : this(id, mode, minX, maxX) { DataGraph = new Graph(data, minX, maxX); + if (gebunden != null) GebundenGraph = new Graph(gebunden, minX, maxX); } public GraphEntry(int id, BillingData.Curve curve, int minX, int maxX) : @@ -35,17 +39,29 @@ namespace Elwig.Helpers.Billing { } 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; MaxX = maxX; DataGraph = dataGraph; GebundenGraph = gebundenGraph; - GebundenFlatPrice = gebundenFlatPrice; + GebundenFlatBonus = gebundenFlatPrice; Contracts = contracts; } + public void AddGebundenGraph() { + GebundenGraph ??= new Graph(MinX, MaxX); + } + + public void RemoveGebundenGraph() { + GebundenGraph = null; + } + + public void SetGebundenFlatBonus(decimal? value) { + GebundenFlatBonus = value; + } + public JsonObject ToJson() { var curve = new JsonObject { ["id"] = Id, @@ -54,8 +70,8 @@ namespace Elwig.Helpers.Billing { curve["data"] = DataGraph.ToJson(Mode.ToString().ToLower()); - if (GebundenFlatPrice != null) { - curve["geb"] = GebundenFlatPrice.ToString(); + if (GebundenFlatBonus != null) { + curve["geb"] = GebundenFlatBonus; } else if (GebundenGraph != null) { curve["geb"] = GebundenGraph.ToJson(Mode.ToString().ToLower()); } @@ -64,7 +80,7 @@ namespace Elwig.Helpers.Billing { } public GraphEntry Copy(int id) { - return new GraphEntry(id, Mode, (Graph)DataGraph.Clone(), (Graph?)GebundenGraph?.Clone(), GebundenFlatPrice, Contracts, MinX, MaxX); + return new GraphEntry(id, Mode, (Graph)DataGraph.Clone(), (Graph?)GebundenGraph?.Clone(), GebundenFlatBonus, [], MinX, MaxX); } } } diff --git a/Elwig/Windows/ChartWindow.xaml b/Elwig/Windows/ChartWindow.xaml index a5eb296..4268437 100644 --- a/Elwig/Windows/ChartWindow.xaml +++ b/Elwig/Windows/ChartWindow.xaml @@ -5,6 +5,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Elwig.Windows" + xmlns:ctrl="clr-namespace:Elwig.Controls" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:ScottPlot="clr-namespace:ScottPlot;assembly=ScottPlot.WPF" mc:Ignorable="d" @@ -59,12 +60,12 @@