From b981b5f89533b3b87f1c32bf9967121bed8217fc Mon Sep 17 00:00:00 2001
From: Thomas Hilscher <thomas.hilscher@gmail.com>
Date: Sat, 20 Jan 2024 19:24:26 +0100
Subject: [PATCH] ChartWindow/Billing: Misc improvements

---
 Elwig/Helpers/Billing/EditBillingData.cs | 11 ++---
 Elwig/Helpers/Billing/Graph.cs           | 31 ++++++++++----
 Elwig/Helpers/Billing/GraphEntry.cs      | 39 ++++++++++-------
 Elwig/Windows/ChartWindow.xaml           | 26 ++++++------
 Elwig/Windows/ChartWindow.xaml.cs        | 53 +++++++++++++++++-------
 5 files changed, 104 insertions(+), 56 deletions(-)

diff --git a/Elwig/Helpers/Billing/EditBillingData.cs b/Elwig/Helpers/Billing/EditBillingData.cs
index ec73ad1..a287cd4 100644
--- a/Elwig/Helpers/Billing/EditBillingData.cs
+++ b/Elwig/Helpers/Billing/EditBillingData.cs
@@ -1,4 +1,5 @@
-using System;
+using Elwig.Models.Entities;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text.Json.Nodes;
@@ -17,7 +18,7 @@ namespace Elwig.Helpers.Billing {
             return new(ParseJson(json), attributeVariants);
         }
 
-        public IEnumerable<GraphEntry> GetPaymentGraphEntries(AppDbContext context) {
+        public IEnumerable<GraphEntry> GetPaymentGraphEntries(AppDbContext context, Season season) {
             Dictionary<int, List<string>> dict1 = [];
             Dictionary<decimal, List<string>> dict2 = [];
             var p = GetPaymentEntry();
@@ -66,13 +67,13 @@ namespace Elwig.Helpers.Billing {
             var attrs = context.WineAttributes.ToDictionary(a => a.AttrId, a => a);
 
             return dict3
-                .Select(e => new GraphEntry(e.Key, curves[e.Key], e.Value
+                .Select(e => new GraphEntry(e.Key, season, curves[e.Key], e.Value
                     .Select(s => new ContractSelection(vars[s[..2]], s.Length > 2 ? attrs[s[2..]] : null))
-                    .ToList(), 50, 140))
+                    .ToList(), 50, 73, 140))
                 .ToList();
         }
 
-        public IEnumerable<GraphEntry> GetQualityGraphEntries(AppDbContext context) {
+        public IEnumerable<GraphEntry> GetQualityGraphEntries(AppDbContext context, Season season) {
             Dictionary<int, List<string>> dict1 = [];
             Dictionary<decimal, List<string>> dict2 = [];
             foreach (var (qualid, q) in GetQualityEntry() ?? []) {
diff --git a/Elwig/Helpers/Billing/Graph.cs b/Elwig/Helpers/Billing/Graph.cs
index cc861c9..d199aed 100644
--- a/Elwig/Helpers/Billing/Graph.cs
+++ b/Elwig/Helpers/Billing/Graph.cs
@@ -1,3 +1,4 @@
+using Elwig.Models.Entities;
 using ScottPlot;
 using System;
 using System.Collections.Generic;
@@ -7,20 +8,32 @@ using System.Text.Json.Nodes;
 namespace Elwig.Helpers.Billing {
     public class Graph : ICloneable {
 
+        private readonly Season Season;
         public double[] DataX { get; set; }
         public double[] DataY { get; set; }
+        public int MinX { get; set; }
+        public int MaxX { get; set; }
 
-        public Graph(int minX, int maxX) {
+        public Graph(Season season, int minX, int maxX) {
+            Season = season;
+            MinX = minX;
+            MaxX = maxX;
             DataX = DataGen.Range(minX, maxX + 1);
             DataY = DataGen.Zeros(maxX - minX + 1);
         }
 
-        public Graph(Dictionary<double, decimal> data, int minX, int maxX) {
+        public Graph(Dictionary<double, decimal> data, Season season, int minX, int maxX) {
+            Season = season;
+            MinX = minX;
+            MaxX = maxX;
             DataX = DataGen.Range(minX, maxX + 1);
             DataY = DataX.Select(i => (double)BillingData.GetCurveValueAt(data, i)).ToArray();
         }
 
-        public Graph(double[] dataX, double[] dataY) {
+        private Graph(double[] dataX, double[] dataY, Season season, int minX, int maxX) {
+            Season = season;
+            MinX = minX;
+            MaxX = maxX;
             DataX = dataX;
             DataY = dataY;
         }
@@ -57,7 +70,7 @@ namespace Elwig.Helpers.Billing {
 
         private void LinearIncreaseGraph(int begin, int end, double inc) {
             for (int i = begin; i < end; i++) {
-                DataY[i + 1] = Math.Round(DataY[i] + inc, 4); //TODO richtig runden
+                DataY[i + 1] = Math.Round(DataY[i] + inc, Season.Precision);
             }
         }
 
@@ -74,7 +87,7 @@ namespace Elwig.Helpers.Billing {
             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
+                DataY[i + 1] = Math.Round(DataY[i] + step, Season.Precision);
             }
         }
 
@@ -86,21 +99,21 @@ namespace Elwig.Helpers.Billing {
             var data = new JsonObject();
 
             if (DataY[0] != DataY[1]) {
-                data.Add(new KeyValuePair<string, JsonNode?>(DataX[0] + mode, Math.Round(DataY[0], 4)));
+                data.Add(new KeyValuePair<string, JsonNode?>(DataX[0] + mode, Math.Round(DataY[0], Season.Precision)));
             }
             for (int i = 1; i < DataX.Length - 1; i++) {
                 if (Math.Round(DataY[i] - DataY[i - 1], 10) != Math.Round(DataY[i + 1] - DataY[i], 10)) {
-                    data.Add(new KeyValuePair<string, JsonNode?>(DataX[i] + mode, Math.Round(DataY[i], 4)));
+                    data.Add(new KeyValuePair<string, JsonNode?>(DataX[i] + mode, Math.Round(DataY[i], Season.Precision)));
                 }
             }
             if (DataY[^1] != DataY[^2]) {
-                data.Add(new KeyValuePair<string, JsonNode?>(DataX[^1] + mode, Math.Round(DataY[^1], 4)));
+                data.Add(new KeyValuePair<string, JsonNode?>(DataX[^1] + mode, Math.Round(DataY[^1], Season.Precision)));
             }
             return data;
         }
 
         public object Clone() {
-            return new Graph((double[])DataX.Clone(), (double[])DataY.Clone());
+            return new Graph((double[])DataX.Clone(), (double[])DataY.Clone(), Season, MinX, MaxX);
         }
     }
 }
diff --git a/Elwig/Helpers/Billing/GraphEntry.cs b/Elwig/Helpers/Billing/GraphEntry.cs
index 533f9ed..3bcb47a 100644
--- a/Elwig/Helpers/Billing/GraphEntry.cs
+++ b/Elwig/Helpers/Billing/GraphEntry.cs
@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using Elwig.Models.Entities;
+using System.Collections.Generic;
 using System.Linq;
 using System.Text.Json.Nodes;
 
@@ -6,7 +7,9 @@ namespace Elwig.Helpers.Billing {
     public class GraphEntry {
 
         public int Id { get; set; }
+        private readonly Season Season;
         public BillingData.CurveMode Mode { get; set; }
+        public bool Abgewertet { get; set; }
         public Graph DataGraph { get; set; }
         public Graph? GebundenGraph { get; set; }
         public decimal? GebundenFlatBonus { get; set; }
@@ -14,36 +17,42 @@ namespace Elwig.Helpers.Billing {
         public string ContractsStringSimple => Contracts.Any() ? string.Join(", ", Contracts.Select(c => c.Listing)) : "-";
         public string ContractsString => Contracts.Any() ? string.Join("\n", Contracts.Select(c => c.FullName)) : "-";
         private int MinX { get; set; }
+        private int MinXGebunden { get; set; }
         private int MaxX { get; set; }
 
-        public GraphEntry(int id, BillingData.CurveMode mode, int minX, int maxX) {
+        public GraphEntry(int id, Season season, BillingData.CurveMode mode, int minX, int minXGebunden, int maxX) {
             Id = id;
+            Season = season;
             Mode = mode;
+            Abgewertet = false;
             MinX = minX;
+            MinXGebunden = minXGebunden;
             MaxX = maxX;
-            DataGraph = new Graph(minX, maxX);
+            DataGraph = new Graph(season, minX, maxX);
             Contracts = [];
         }
 
-        public GraphEntry(int id, BillingData.CurveMode mode, Dictionary<double, decimal> data, Dictionary<double, decimal>? 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, Season season, BillingData.CurveMode mode, Dictionary<double, decimal> data, Dictionary<double, decimal>? gebunden,
+            int minX, int minXGebunden, int maxX) : this(id, season, mode, minX, minXGebunden, maxX) {
+            DataGraph = new Graph(data, season, minX, maxX);
+            if (gebunden != null) GebundenGraph = new Graph(gebunden, season, minXGebunden, maxX);
         }
 
-        public GraphEntry(int id, BillingData.Curve curve, List<ContractSelection> contracts, int minX, int maxX) :
-            this(id, curve.Mode, minX, maxX) {
-            DataGraph = new Graph(curve.Normal, minX, maxX);
+        public GraphEntry(int id, Season season, BillingData.Curve curve, List<ContractSelection> contracts, int minX, int minXGebunden, int maxX) :
+            this(id, season, curve.Mode, minX, minXGebunden, maxX) {
+            DataGraph = new Graph(curve.Normal, season, minX, maxX);
             if (curve.Gebunden != null)
-                GebundenGraph = new Graph(curve.Gebunden, 73, maxX);
+                GebundenGraph = new Graph(curve.Gebunden, season, minXGebunden, maxX);
             Contracts = contracts;
         }
 
-        private GraphEntry(int id, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph,
-            decimal? gebundenFlatPrice, List<ContractSelection> contracts, int minX, int maxX) {
+        private GraphEntry(int id, Season season, BillingData.CurveMode mode, Graph dataGraph, Graph? gebundenGraph,
+            decimal? gebundenFlatPrice, List<ContractSelection> contracts, int minX, int minXGebunden, int maxX) {
             Id = id;
+            Season = season;
             Mode = mode;
             MinX = minX;
+            MinXGebunden = minXGebunden;
             MaxX = maxX;
             DataGraph = dataGraph;
             GebundenGraph = gebundenGraph;
@@ -52,7 +61,7 @@ namespace Elwig.Helpers.Billing {
         }
 
         public void AddGebundenGraph() {
-            GebundenGraph ??= new Graph(MinX, MaxX);
+            GebundenGraph ??= new Graph(Season, MinXGebunden, MaxX);
         }
 
         public void RemoveGebundenGraph() {
@@ -81,7 +90,7 @@ namespace Elwig.Helpers.Billing {
         }
 
         public GraphEntry Copy(int id) {
-            return new GraphEntry(id, Mode, (Graph)DataGraph.Clone(), (Graph?)GebundenGraph?.Clone(), GebundenFlatBonus, [], MinX, MaxX);
+            return new GraphEntry(id, Season, Mode, (Graph)DataGraph.Clone(), (Graph?)GebundenGraph?.Clone(), GebundenFlatBonus, [], MinX, MinXGebunden, MaxX);
         }
     }
 }
diff --git a/Elwig/Windows/ChartWindow.xaml b/Elwig/Windows/ChartWindow.xaml
index 7c93ab8..749bb92 100644
--- a/Elwig/Windows/ChartWindow.xaml
+++ b/Elwig/Windows/ChartWindow.xaml
@@ -53,19 +53,16 @@
             <ColumnDefinition Width="200"/>
         </Grid.ColumnDefinitions>
 
-        <Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
+        <Grid Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3">
             <Grid.ColumnDefinitions>
-                <ColumnDefinition Width="150"/>
-                <ColumnDefinition Width="500"/>
+                <ColumnDefinition Width="560"/>
+                <ColumnDefinition Width="100"/>
             </Grid.ColumnDefinitions>
 
-            <Label Content="Graph:" Margin="10,0,0,0" FontSize="14" Grid.Column="0" VerticalAlignment="Center"/>
-            <TextBlock x:Name="GraphNum" Margin="55,0,0,0" FontSize="14" Width="50" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left"/>
-
-            <Label Content="Für:" Margin="10,0,0,0" FontSize="14" Grid.Column="1" VerticalAlignment="Center"/>
-            <xctk:CheckComboBox x:Name="ContractInput" Margin="0,10,-42,5" Grid.Column="1"
+            <Label Content="Für:" Margin="10,0,0,0" FontSize="14" Grid.Column="0" VerticalAlignment="Center"/>
+            <xctk:CheckComboBox x:Name="ContractInput" Margin="50,0,0,0" Grid.Column="0"
                                 Delimiter=", " AllItemsSelectedContent="Alle" IsEnabled="False" ItemSelectionChanged="ContractInput_Changed"
-                                Width="500" Height="25" HorizontalAlignment="Right">
+                                Width="500" Height="25" HorizontalAlignment="Left">
                 <xctk:CheckComboBox.ItemTemplate>
                     <DataTemplate>
                         <StackPanel Orientation="Horizontal">
@@ -75,9 +72,12 @@
                     </DataTemplate>
                 </xctk:CheckComboBox.ItemTemplate>
             </xctk:CheckComboBox>
+
+            <CheckBox x:Name="AbgewertetInput" Content="Abgewertet" IsEnabled="False" Checked="AbgewertetInput_Changed"
+                              VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,0,0,0" Grid.Column="1"/>
         </Grid>
 
-        <ListBox x:Name="GraphList" Margin="10,10,35,50" Grid.Column="0" Grid.Row="1" SelectionChanged="GraphList_SelectionChanged">
+        <ListBox x:Name="GraphList" Margin="10,10,35,50" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" SelectionChanged="GraphList_SelectionChanged">
             <ListBox.ItemTemplate>
                 <DataTemplate>
                     <StackPanel Orientation="Horizontal">
@@ -93,13 +93,13 @@
             Click="SaveButton_Click"/>
 
         <Button x:Name="AddButton" Content="&#xF8AA;" FontFamily="Segoe MDL2 Assets" FontSize="11" Padding="0,1.5,0,0" ToolTip="Neue Auszahlungsvariante hinzufügen"
-            VerticalAlignment="Center" HorizontalAlignment="Right" Width="25" Height="25" Margin="5,0,5,60" Grid.Column="0" Grid.Row="1"
+            VerticalAlignment="Center" HorizontalAlignment="Right" Width="25" Height="25" Margin="5,0,5,60" Grid.Column="0" Grid.RowSpan="2" Grid.Row="0"
             Click="AddButton_Click"/>
         <Button x:Name="CopyButton" Content="&#xE8C8;" FontFamily="Segoe MDL2 Assets" FontSize="12" Padding="0,0,0,0" IsEnabled="False" ToolTip="Ausgewählte Auszahlungsvariante duplizieren"
-            VerticalAlignment="Center" HorizontalAlignment="Right" Width="25" Height="25" Margin="0,0,5,0" Grid.Column="0" Grid.Row="1"
+            VerticalAlignment="Center" HorizontalAlignment="Right" Width="25" Height="25" Margin="0,0,5,0" Grid.Column="0" Grid.RowSpan="2" Grid.Row="0"
             Click="CopyButton_Click"/>
         <Button x:Name="DeleteButton" Content="&#xF8AB;" FontFamily="Segoe MDL2 Assets" FontSize="11" Padding="0,1.5,0,0" IsEnabled="False" ToolTip="Ausgewählte Auszahlungsvariante löschen"
-                VerticalAlignment="Center" HorizontalAlignment="Right" Width="25" Height="25" Margin="5,60,5,0" Grid.Column="0" Grid.Row="1"
+                VerticalAlignment="Center" HorizontalAlignment="Right" Width="25" Height="25" Margin="5,60,5,0" Grid.Column="0" Grid.RowSpan="2" Grid.Row="0"
                 Click="DeleteButton_Click"/>
 
         <Grid Grid.Row="1" Grid.Column="1">
diff --git a/Elwig/Windows/ChartWindow.xaml.cs b/Elwig/Windows/ChartWindow.xaml.cs
index 4259b4b..59672d2 100644
--- a/Elwig/Windows/ChartWindow.xaml.cs
+++ b/Elwig/Windows/ChartWindow.xaml.cs
@@ -15,6 +15,7 @@ using Microsoft.EntityFrameworkCore;
 using Microsoft.EntityFrameworkCore.ChangeTracking;
 using ScottPlot;
 using ScottPlot.Plottable;
+using Xceed.Wpf.Toolkit.Primitives;
 
 namespace Elwig.Windows {
     public partial class ChartWindow : ContextWindow {
@@ -24,6 +25,7 @@ namespace Elwig.Windows {
 
         public readonly int Year;
         public readonly int AvNr;
+        public readonly Season Season;
         private readonly PaymentVar PaymentVar;
 
         private ScatterPlot DataPlot;
@@ -43,6 +45,7 @@ namespace Elwig.Windows {
         private bool FillingInputs = false;
 
         private const int MinOechsle = 50;
+        private const int MinOechsleGebunden = 73;
         private const int MaxOechsle = 140;
 
         private List<GraphEntry> GraphEntries = [];
@@ -52,6 +55,7 @@ namespace Elwig.Windows {
             InitializeComponent();
             Year = year;
             AvNr = avnr;
+            Season = Context.Seasons.Find(year) ?? throw new ArgumentException("Season not found");
             PaymentVar = Context.PaymentVariants.Find(year, avnr) ?? throw new ArgumentException("PaymentVar not found");
             Title = $"{PaymentVar?.Name} - Lese {year} - Elwig";
         }
@@ -72,7 +76,7 @@ namespace Elwig.Windows {
                 .Order()
                 .ToList();
             var data = EditBillingData.FromJson(PaymentVar.Data, attrVariants);
-            GraphEntries = [ ..data.GetPaymentGraphEntries(Context), ..data.GetQualityGraphEntries(Context)];
+            GraphEntries = [ ..data.GetPaymentGraphEntries(Context, Season), ..data.GetQualityGraphEntries(Context, Season)];
 
             var contracts = ContractSelection.GetContractsForYear(Context, Year);
             ControlUtils.RenewItemsSource(ContractInput, contracts, g => (g as ContractSelection)?.Listing);
@@ -91,6 +95,7 @@ namespace Elwig.Windows {
                 GebundenTypeGraph.IsEnabled = true;
                 GebundenTypeNone.IsEnabled = true;
                 ContractInput.IsEnabled = true;
+                AbgewertetInput.IsEnabled = true;
                 EnableOptionButtons();
                 FillInputs();
             } else {
@@ -109,13 +114,13 @@ namespace Elwig.Windows {
                 GebundenTypeGraph.IsEnabled = false;
                 GebundenTypeNone.IsEnabled = false;
                 ContractInput.IsEnabled = false;
+                AbgewertetInput.IsEnabled = false;
             }
             GC.Collect();
         }
 
         private void FillInputs() {
             FillingInputs = true;
-            GraphNum.Text = SelectedGraphEntry?.Id.ToString();
 
             if (SelectedGraphEntry?.GebundenFlatBonus != null) {
                 GebundenTypeFixed.IsChecked = true;
@@ -138,13 +143,13 @@ namespace Elwig.Windows {
 
         private void InitPlot() {
             if (SelectedGraphEntry?.GebundenGraph != null) {
-                GebundenPlot = OechslePricePlot.Plot.AddScatter(SelectedGraphEntry.GebundenGraph.DataX, SelectedGraphEntry.GebundenGraph.DataY);
+                GebundenPlot = OechslePricePlot.Plot.AddScatter(SelectedGraphEntry.GebundenGraph.DataX, SelectedGraphEntry.GebundenGraph.DataY, label: "Gebunden");
                 GebundenPlot.LineColor = ColorGebunden;
                 GebundenPlot.MarkerColor = ColorGebunden;
                 GebundenPlot.MarkerSize = 9;
             }
 
-            DataPlot = OechslePricePlot.Plot.AddScatter(SelectedGraphEntry!.DataGraph.DataX, SelectedGraphEntry!.DataGraph.DataY);
+            DataPlot = OechslePricePlot.Plot.AddScatter(SelectedGraphEntry!.DataGraph.DataX, SelectedGraphEntry!.DataGraph.DataY, label: "Ungebunden");
             DataPlot.LineColor = ColorUngebunden;
             DataPlot.MarkerColor = ColorUngebunden;
             DataPlot.MarkerSize = 9;
@@ -157,7 +162,7 @@ namespace Elwig.Windows {
             OechslePricePlot.Configuration.DoubleClickBenchmark = false;
             //OechslePricePlot.Plot.XAxis.ManualTickSpacing(1);
             OechslePricePlot.Plot.YAxis.ManualTickSpacing(0.1);
-            OechslePricePlot.Plot.SetAxisLimits(MinOechsle - 1, MaxOechsle + 1, -0.1, 2);
+            OechslePricePlot.Plot.SetAxisLimits(Math.Min(MinOechsle, MinOechsleGebunden) - 1, MaxOechsle + 1, -0.1, 2);
             
             OechslePricePlot.Plot.Layout(padding: 0);
             OechslePricePlot.Plot.XAxis2.Layout(padding: 0);
@@ -291,15 +296,15 @@ namespace Elwig.Windows {
         }
 
         private void ShowLegend() {
-            OechslePricePlot.Plot.Legend(true, Alignment.UpperRight);
+            OechslePricePlot.Plot.Legend(true, Alignment.UpperLeft);
         }
 
         private void HideLegend() {
-            OechslePricePlot.Plot.Legend(false, Alignment.UpperRight);
+            OechslePricePlot.Plot.Legend(false, Alignment.UpperLeft);
         }
 
         private void OechsleInput_TextChanged(object sender, TextChangedEventArgs evt) {
-            if (ActiveGraph == null) {
+            if (ActiveGraph == null || SelectedGraphEntry == null) {
                 return;
             }
 
@@ -308,9 +313,9 @@ namespace Elwig.Windows {
             SecondaryMarkedPoint = -1;
             ChangeMarker(SecondaryMarkedPointPlot, false);
 
-            if (success) {
-                if (oechsle >= MinOechsle && oechsle <= MaxOechsle) {
-                    PrimaryMarkedPoint = oechsle - MinOechsle;
+            if (success) { 
+                if (oechsle >= ActiveGraph.MinX && oechsle <= ActiveGraph.MaxX) {
+                    PrimaryMarkedPoint = oechsle - ActiveGraph.MinX;
                     ChangeMarker(PrimaryMarkedPointPlot, true, ActiveGraph.GetOechsleAt(PrimaryMarkedPoint), ActiveGraph.GetPriceAt(PrimaryMarkedPoint));
 
                     PriceInput.Text = ActiveGraph.GetPriceAt(PrimaryMarkedPoint).ToString();
@@ -479,7 +484,7 @@ namespace Elwig.Windows {
             if (LastHighlighted != Highlighted || HoverChanged) {
                 OechslePricePlot.Plot.Remove(TooltipPlot);
                 if (TooltipInput.IsChecked == true) {
-                    TooltipPlot = OechslePricePlot.Plot.AddTooltip($"Oechsle: {pointX:N2}, Preis: {Math.Round(pointY, 4)}€/kg)", pointX, pointY);
+                    TooltipPlot = OechslePricePlot.Plot.AddTooltip($"Oechsle: {pointX:N2}, Preis: {Math.Round(pointY, Season.Precision)}€/kg)", pointX, pointY);
                 }
                 LastHighlighted = (g, pointIndex);
                 HoverChanged = false;
@@ -492,7 +497,7 @@ namespace Elwig.Windows {
         }
 
         private void AddButton_Click(object sender, RoutedEventArgs e) {
-            GraphEntry newGraphEntry = new(GetMaxGraphId() + 1, BillingData.CurveMode.Oe, MinOechsle, MaxOechsle);
+            GraphEntry newGraphEntry = new(GetMaxGraphId() + 1, Season, BillingData.CurveMode.Oe, MinOechsle, MinOechsleGebunden, MaxOechsle);
             GraphEntries.Add(newGraphEntry);
             GraphList.Items.Refresh();
             GraphList.SelectedItem = newGraphEntry;
@@ -545,6 +550,7 @@ namespace Elwig.Windows {
 
             MessageBox.Show(data.ToJsonString());
 
+            /*
             EntityEntry<PaymentVar>? tr = null;
             try {
                 PaymentVar.Data = data.ToJsonString();
@@ -552,12 +558,14 @@ namespace Elwig.Windows {
 
                 await Context.SaveChangesAsync();
                 await App.HintContextChange();
+                await RefreshGraphList();
             } catch (Exception exc) {
                 if (tr != null) await tr.ReloadAsync();
                 var str = "Der Eintrag konnte nicht in der Datenbank gespeichert werden!\n\n" + exc.Message;
                 if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
                 MessageBox.Show(str, "Graph speichern", MessageBoxButton.OK, MessageBoxImage.Error);
             }
+            */
         }
 
         private void EnableUnitTextBox(UnitTextBox u) {
@@ -618,13 +626,30 @@ namespace Elwig.Windows {
             }
         }
 
-        private void ContractInput_Changed(object sender, RoutedEventArgs e) {
+        private void ContractInput_Changed(object sender, ItemSelectionChangedEventArgs e) {
             if (FillingInputs) return;
+            if (e.IsSelected == true) {
+                RemoveContractFromOtherGraphEntries(e.Item.ToString());
+            }
             var r = ContractInput.SelectedItems.Cast<ContractSelection>();
             SelectedGraphEntry!.Contracts = r.ToList();
             GraphList.Items.Refresh();
         }
 
+        private void RemoveContractFromOtherGraphEntries(string? contract) {
+            if (contract == null) return;
+            foreach (var ge in GraphEntries) {
+                if (ge != SelectedGraphEntry) {
+                    ge.Contracts.RemoveAll(c => c.Listing.Equals(contract));
+                }
+            }
+        }
+
+        private void AbgewertetInput_Changed(object sender, RoutedEventArgs e) {
+            if (SelectedGraphEntry == null) return;
+            SelectedGraphEntry.Abgewertet = AbgewertetInput.IsChecked == true;
+        }
+
         private void GebundenType_Checked(object sender, RoutedEventArgs e) {
             if (FillingInputs) return;
             if (SelectedGraphEntry == null) {