Compare commits
	
		
			15 Commits
		
	
	
		
			f8126c392e
			...
			v0.4.0
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 2f8e4ca812 | |||
| 450f5d8109 | |||
| 62d6707d10 | |||
| 2bcf26cc8d | |||
| daddd069a3 | |||
| 3b3489b492 | |||
| 505ee0ad24 | |||
| 0b79fa192e | |||
| ff2968c989 | |||
| 8c49cc8ef2 | |||
| f0751499ea | |||
| d4dd84394b | |||
| 3b4340b5e8 | |||
| 4db147e582 | |||
| c997acc95d | 
							
								
								
									
										32
									
								
								Elwig/Dialogs/DeliveryConfirmationsDialog.xaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								Elwig/Dialogs/DeliveryConfirmationsDialog.xaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | ||||
| <local:ContextWindow x:Class="Elwig.Dialogs.DeliveryConfirmationsDialog" | ||||
|                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||||
|                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||||
|                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||||
|                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||||
|                      xmlns:local="clr-namespace:Elwig.Windows" | ||||
|                      mc:Ignorable="d" | ||||
|                      ResizeMode="NoResize" | ||||
|                      Title="Anlieferungsbestätingungen - Elwig" Height="400" Width="600"> | ||||
|     <Grid> | ||||
|         <GroupBox Header="Sortieren nach" Margin="10,10,10,10" Width="150" Height="80" VerticalAlignment="Top" HorizontalAlignment="Left"> | ||||
|             <StackPanel Margin="5,5,0,5"> | ||||
|                 <RadioButton GroupName="Order" x:Name="OrderMgNrInput" Content="Mitgliedsnummer" IsChecked="True"/> | ||||
|                 <RadioButton GroupName="Order" x:Name="OrderNameInput" Content="Name"/> | ||||
|                 <RadioButton GroupName="Order" x:Name="OrderPlzInput" Content="PLZ, Ort, Name"/> | ||||
|             </StackPanel> | ||||
|         </GroupBox> | ||||
|  | ||||
|         <TextBox x:Name="TextElement" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" | ||||
|                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="170,10,10,10" Height="Auto"/> | ||||
|  | ||||
|         <Button x:Name="TestButton" Content="Stichprobe" FontSize="14" Width="150" Margin="10,10,10,74" Height="27" | ||||
|                 Click="TestButton_Click" | ||||
|                 VerticalAlignment="Bottom" HorizontalAlignment="Left"/> | ||||
|         <Button x:Name="ShowButton" Content="Vorschau" FontSize="14" Width="150" Margin="10,10,10,42" Height="27" | ||||
|                 Click="ShowButton_Click" | ||||
|                 VerticalAlignment="Bottom" HorizontalAlignment="Left"/> | ||||
|         <Button x:Name="PrintButton" Content="Drucken" FontSize="14" Width="150" Margin="10,10,10,10" Height="27" | ||||
|                 Click="PrintButton_Click" | ||||
|                 VerticalAlignment="Bottom" HorizontalAlignment="Left"/> | ||||
|     </Grid> | ||||
| </local:ContextWindow> | ||||
							
								
								
									
										101
									
								
								Elwig/Dialogs/DeliveryConfirmationsDialog.xaml.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								Elwig/Dialogs/DeliveryConfirmationsDialog.xaml.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,101 @@ | ||||
| using Elwig.Documents; | ||||
| using Elwig.Models; | ||||
| using Elwig.Windows; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using System.Windows; | ||||
| using System.Windows.Input; | ||||
|  | ||||
| namespace Elwig.Dialogs { | ||||
|     public partial class DeliveryConfirmationsDialog : ContextWindow { | ||||
|  | ||||
|         public readonly int Year; | ||||
|  | ||||
|         public DeliveryConfirmationsDialog(int year) { | ||||
|             InitializeComponent(); | ||||
|             Year = year; | ||||
|             Title = $"Anlieferungsbestätigungen - Lese {Year} - Elwig"; | ||||
|             TextElement.Text = App.Client.TextDeliveryConfirmation; | ||||
|             if (!App.Config.Debug) { | ||||
|                 TestButton.Visibility = Visibility.Hidden; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         protected override async Task OnRenewContext() { } | ||||
|  | ||||
|         private async Task UpdateTextParameter() { | ||||
|             var text = TextElement.Text; | ||||
|             if (text.Length == 0) text = null; | ||||
|             if (text != App.Client.TextDeliveryConfirmation) { | ||||
|                 App.Client.TextDeliveryConfirmation = text; | ||||
|                 await App.Client.UpdateValues(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private async Task Generate(int mode) { | ||||
|  | ||||
|             Mouse.OverrideCursor = Cursors.AppStarting; | ||||
|             await UpdateTextParameter(); | ||||
|  | ||||
|             var members = Context.Members.FromSqlRaw($""" | ||||
|                 SELECT m.* | ||||
|                 FROM member m | ||||
|                     INNER JOIN delivery d ON d.mgnr = m.mgnr | ||||
|                 WHERE d.year = {Year} | ||||
|                 GROUP BY m.mgnr | ||||
|                 """); | ||||
|             if (OrderMgNrInput.IsChecked == true) { | ||||
|                 members = members | ||||
|                     .OrderBy(m => m.MgNr); | ||||
|             } else if (OrderNameInput.IsChecked == true) { | ||||
|                 members = members | ||||
|                     .OrderBy(m => m.FamilyName) | ||||
|                     .ThenBy(m => m.GivenName) | ||||
|                     .ThenBy(m => m.MgNr); | ||||
|             } else if (OrderPlzInput.IsChecked == true) { | ||||
|                 members = members | ||||
|                     .OrderBy(m => m.PostalDest.AtPlz.Plz) | ||||
|                     .ThenBy(m => m.PostalDest.AtPlz.Ort.Name) | ||||
|                     .ThenBy(m => m.FamilyName) | ||||
|                     .ThenBy(m => m.GivenName) | ||||
|                     .ThenBy(m => m.MgNr); | ||||
|             } | ||||
|  | ||||
|             IEnumerable<Member> list = await members.ToListAsync(); | ||||
|             if (mode == 0) { | ||||
|                 var r = new Random().Next(0, 10); | ||||
|                 list = list.Where((_, n) => n % 10 == r); | ||||
|             } | ||||
|  | ||||
|             using var doc = await Document.Merge(list.Select(m => new DeliveryConfirmation(Context, Year, m))); ; | ||||
|             await doc.Generate(); | ||||
|             Mouse.OverrideCursor = null; | ||||
|  | ||||
|             if (mode < 2) { | ||||
|                 doc.Show(); | ||||
|                 return; | ||||
|             } | ||||
|             if (App.Config.Debug) { | ||||
|                 doc.Show(); | ||||
|             } else { | ||||
|                 await doc.Print(); | ||||
|             } | ||||
|             Close(); | ||||
|         } | ||||
|  | ||||
|         private async void TestButton_Click(object sender, RoutedEventArgs evt) { | ||||
|             await Generate(0); | ||||
|         } | ||||
|  | ||||
|         private async void ShowButton_Click(object sender, RoutedEventArgs evt) { | ||||
|             await Generate(1); | ||||
|         } | ||||
|  | ||||
|         private async void PrintButton_Click(object sender, RoutedEventArgs evt) { | ||||
|             await Generate(2); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -70,12 +70,14 @@ | ||||
|                     <td rowspan="@binNum" class="kmw">@($"{part.Kmw:N1}")</td> | ||||
|                     <td rowspan="@binNum" class="abs">@abs</td> | ||||
|                     <td rowspan="@binNum" class="rel">@rel</td> | ||||
|                     @Raw(FormatRow(pmt?.DeliveryPart.Bins?.ElementAtOrDefault(0), pmt?.Prices?.ElementAtOrDefault(0))) | ||||
|                     <!--FIXME price--> | ||||
|                     @Raw(FormatRow(pmt?.DeliveryPart.Bins?.ElementAtOrDefault(0)?.Value, 0)) | ||||
|                     <td rowspan="@binNum" class="amount sum">@($"{pmt?.Amount:N2}")</td> | ||||
|                 </tr> | ||||
|                 @for (int i = 1; i < binNum; i++) { | ||||
|                     <tr class="@(i == binNum - 1 ? "last" : "")"> | ||||
|                         @Raw(FormatRow(pmt?.DeliveryPart.Bins?.ElementAtOrDefault(i), pmt?.Prices?.ElementAtOrDefault(i))) | ||||
|                         <!--FIXME price--> | ||||
|                         @Raw(FormatRow(pmt?.DeliveryPart.Bins?.ElementAtOrDefault(i)?.Value, 0)) | ||||
|                     </tr> | ||||
|                 } | ||||
|                 last = part.SortId; | ||||
|   | ||||
| @@ -27,7 +27,7 @@ namespace Elwig.Documents { | ||||
|             Text = App.Client.TextDeliveryNote; | ||||
|             DocumentId = $"Tr.-Gutschr. {c.TgId}"; | ||||
|             CurrencySymbol = c.Payment.Variant.Season.Currency.Symbol ?? c.Payment.Variant.Season.Currency.Code; | ||||
|             BinNames = c.Payment.Variant.Season.BinNames; | ||||
|             BinNames = new string[0]; // FIXME | ||||
|             Precision = c.Payment.Variant.Season.Precision; | ||||
|             Parts = ctx.DeliveryParts.FromSql($""" | ||||
|                 SELECT p.* | ||||
|   | ||||
| @@ -29,7 +29,7 @@ | ||||
|                 <th colspan="2">Gradation</th> | ||||
|                 <th colspan="2">Flächenbindung</th> | ||||
|                 <th>Gewicht</th> | ||||
|                 <th>Davon<br/>abwerten</th> | ||||
|                 <th>Davon<br/>abzuwerten</th> | ||||
|             </tr> | ||||
|             <tr> | ||||
|                 <th>[°Oe]</th> | ||||
| @@ -44,7 +44,7 @@ | ||||
|                 var lastSortId = ""; | ||||
|             } | ||||
|             @foreach (var p in Model.Deliveries) { | ||||
|                 var bins = p.Bins.Select((b, n) => (b, n + 1)).Where(b => b.Item1 > 0).ToArray(); | ||||
|                 var bins = p.Bins.Where(b => b.Value > 0).OrderByDescending(b => b.BinNr).ToArray(); | ||||
|                 var rowsBins = bins.Length; | ||||
|                 var mods = p.Modifiers.Select(m => m.Name).ToArray(); | ||||
|                 var rowsMod = mods.Length + 1; | ||||
| @@ -67,22 +67,18 @@ | ||||
|                             <td colspan="3"></td> | ||||
|                         } | ||||
|                         @if (i < bins.Length) { | ||||
|                             var (b, n) = bins[i]; | ||||
|                             string name = ""; | ||||
|                             switch (n) { | ||||
|                                 case 1: name = $"geb. {p.SortId}{string.Join("", p.Attributes.Order().Select(a => a.AttrId).Take(2))}"; break; | ||||
|                                 case 2: name = $"geb. {p.SortId}{p.Attributes.Select(a => a.AttrId).Order().FirstOrDefault()}"; break; | ||||
|                                 case 3: name = $"geb. {p.SortId}{p.Attributes.Select(a => a.AttrId).Order().Skip(1).FirstOrDefault()}"; break; | ||||
|                                 case 4: name = $"geb. {p.SortId}"; break; | ||||
|                                 case 5: name = "ungeb."; break; | ||||
|                             } | ||||
|                             <td class="geb">@name:</td> | ||||
|                             <td class="weight">@($"{b:N0}")</td> | ||||
|                             var bin = bins[i]; | ||||
|                             <td class="geb">@(bin.Discr == "_" ? "ungeb." : $"geb. {p.SortId}{bin.Discr}"):</td> | ||||
|                             <td class="weight">@($"{bin.Value:N0}")</td> | ||||
|                         } else { | ||||
|                             <td colspan="2"></td> | ||||
|                         } | ||||
|                         @if (i == bins.Length - 1) { | ||||
|                             <td class="weight">@($"{p.Weight:N0}")</td> | ||||
|                         } else { | ||||
|                             <td></td> | ||||
|                         } | ||||
|                         @if (first) { | ||||
|                             <td rowspan="@rows" class="weight">@($"{p.Weight:N0}")</td> | ||||
|                             <td rowspan="@rows" class="weight"></td> | ||||
|                             first = false; | ||||
|                         } | ||||
| @@ -92,9 +88,68 @@ | ||||
|             } | ||||
|             <tr class="sum"> | ||||
|                 <td colspan="8">Gesamt:</td> | ||||
|                 <td colspan="2" style="text-align: right;">@($"{Model.Deliveries.Sum(p => p.Weight):N0}")</td> | ||||
|                 <td colspan="2" class="weight">@($"{Model.Deliveries.Sum(p => p.Weight):N0}")</td> | ||||
|                 <td></td> | ||||
|             </tr> | ||||
|         </tbody> | ||||
|     </table> | ||||
|     <table class="delivery-confirmation-stats"> | ||||
|         <colgroup> | ||||
|             <col style="width: 45mm;"/> | ||||
|             <col style="width: 17mm;"/> | ||||
|             <col style="width: 17mm;"/> | ||||
|             <col style="width: 17mm;"/> | ||||
|             <col style="width: 19mm;"/> | ||||
|             <col style="width: 16mm;"/> | ||||
|             <col style="width: 17mm;"/> | ||||
|             <col style="width: 17mm;"/> | ||||
|         </colgroup> | ||||
|         <thead> | ||||
|             <tr> | ||||
|                 <th><b>Lese @Model.Year</b> per @($"{Model.Date:dd.MM.yyyy}") [kg]</th> | ||||
|                 <th>Lieferpflicht</th> | ||||
|                 <th>Lieferrecht</th> | ||||
|                 <th>Unterliefert<br/>(bzgl. Zuget.)</th> | ||||
|                 <th>Noch zu liefern<br/>(bzgl. Gelft.)</th> | ||||
|                 <th>Überliefert<br/>(bzgl. Gelft.)</th> | ||||
|                 <th>Zugeteilt</th> | ||||
|                 <th>Geliefert</th> | ||||
|             </tr> | ||||
|         </thead> | ||||
|         <tbody> | ||||
|             @{ | ||||
|                 string FormatRow(int obligation, int right, int sum, int? payment = null) { | ||||
|                     var isGa = payment == null; | ||||
|                     payment ??= sum; | ||||
|                     return $"<td>{obligation:N0}</td>" + | ||||
|                         $"<td>{right:N0}</td>" + | ||||
|                         $"<td>{(payment < obligation ? $"<b>{obligation - payment:N0}\x3c/b>" : "-")}</td>" + | ||||
|                         $"<td>{(sum >= obligation && sum <= right ? $"{right - sum:N0}" : "-")}</td>" + | ||||
|                         $"<td>{(obligation == 0 && right == 0 ? "-" : (sum > right ? ((isGa ? "<b>" : "") + $"{sum - right:N0}" + (isGa ? "</b>" : "")) : "-"))}</td>" + | ||||
|                         $"<td>{(obligation == 0 && right == 0 ? "-" : $"{payment:N0}")}</td>" + | ||||
|                         $"<td>{sum:N0}</td>"; | ||||
|                 } | ||||
|             } | ||||
|             <tr> | ||||
|                 <th>Gesamtlieferung lt. gez. GA</th> | ||||
|                 @Raw(FormatRow(Model.Member.DeliveryObligation, Model.Member.DeliveryRight, Model.Member.Deliveries.Where(d => d.Year == Model.Year).Sum(d => d.Weight))) | ||||
|             </tr> | ||||
|             <tr class="subheading"> | ||||
|                 <th>Flächenbindungen:</th> | ||||
|             </tr> | ||||
|             @foreach (var (id, (name, right, obligation, sum, payment)) in Model.MemberBins.OrderBy(b => b.Key)) { | ||||
|                 if (right > 0 || obligation > 0 || sum > 0) { | ||||
|                     <tr> | ||||
|                         <th>@name</th> | ||||
|                         @Raw(FormatRow(obligation, right, sum, payment)) | ||||
|                     </tr> | ||||
|                 } | ||||
|             } | ||||
|         </tbody> | ||||
|     </table> | ||||
|     <div class="text" style="margin-top: 2em;"> | ||||
|         @if (Model.Text != null) { | ||||
|             <p class="comment" style="white-space: pre-wrap; break-inside: avoid;">@Model.Text</p> | ||||
|         } | ||||
|     </div> | ||||
| </main> | ||||
|   | ||||
| @@ -9,12 +9,15 @@ namespace Elwig.Documents { | ||||
|  | ||||
|         public int Year; | ||||
|         public IEnumerable<DeliveryPart> Deliveries; | ||||
|         public string? Text = App.Client.TextDeliveryConfirmation; | ||||
|         public Dictionary<string, (string, int, int, int, int)> MemberBins; | ||||
|  | ||||
|         public DeliveryConfirmation(AppDbContext ctx, int year, Member m) : | ||||
|             base($"Anlieferungsbestätigung {year} – {((IAddress?)m.BillingAddress ?? m).Name}", m) { | ||||
|             Year = year; | ||||
|             ShowDateAndLocation = true; | ||||
|             UseBillingAddress = true; | ||||
|             IncludeSender = true; | ||||
|             // FIXME footer in merged documents | ||||
|             //DocumentId = $"Anl.-Best. {Year}/{m.MgNr}"; | ||||
|             Deliveries = ctx.DeliveryParts.FromSqlRaw($""" | ||||
| @@ -27,6 +30,7 @@ namespace Elwig.Documents { | ||||
|                          v.kmw DESC, v.lsnr, v.dpnr | ||||
|                 """) | ||||
|                 .ToList(); | ||||
|             MemberBins = ctx.GetMemberBins(Year, m.MgNr).GetAwaiter().GetResult(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -76,65 +76,63 @@ | ||||
|     <p class="comment">Amerkung zur Lieferung: @Model.Delivery.Comment</p> | ||||
| } | ||||
| @if (Model.DisplayStats > 0) { | ||||
|     <div id="delivery-stats"> | ||||
|         <table class="delivery-stats @(Model.DisplayStats > 2 ? "expanded" : "")"> | ||||
|             <colgroup> | ||||
|                 <col style="width: 45mm;"/> | ||||
|                 <col style="width: 20mm;"/> | ||||
|                 <col style="width: 20mm;"/> | ||||
|                 <col style="width: 20mm;"/> | ||||
|                 <col style="width: 20mm;"/> | ||||
|                 <col style="width: 20mm;"/> | ||||
|                 <col style="width: 20mm;"/> | ||||
|             </colgroup> | ||||
|             <thead> | ||||
|                 <tr> | ||||
|                     <th><b>Lese @Model.Delivery.Year</b> per @($"{Model.Date:dd.MM.yyyy}") [kg]</th> | ||||
|                     <th>Lieferpflicht</th> | ||||
|                     <th>Lieferrecht</th> | ||||
|                     <th>Unterliefert</th> | ||||
|                     <th>Noch zu liefern</th> | ||||
|                     <th>Überliefert</th> | ||||
|                     <th>Geliefert</th> | ||||
|                 </tr> | ||||
|             </thead> | ||||
|             <tbody> | ||||
|                 @{ | ||||
|                     string FormatRow(int obligation, int right, int sum) { | ||||
|                         return $"<td>{obligation:N0}</td>" + | ||||
|                             $"<td>{right:N0}</td>" + | ||||
|                             $"<td>{(sum < obligation ? $"{obligation - sum:N0}" : "-")}</td>" + | ||||
|                             $"<td>{(sum >= obligation && sum <= right ? $"{right - sum:N0}" : "-")}</td>" + | ||||
|                             $"<td>{(sum > right ? $"{sum - right:N0}" : "-")}</td>" + | ||||
|                             $"<td>{sum:N0}</td>"; | ||||
|                     } | ||||
|                     var sortids = Model.Delivery.Parts.Select(p => p.SortId).ToList(); | ||||
|                     var bins = Model.MemberBins.GroupBy(b => b.Item1[..2]).ToDictionary(g => g.Key, g => g.Count()); | ||||
|     <table class="delivery-note-stats @(Model.DisplayStats > 2 ? "expanded" : "")"> | ||||
|         <colgroup> | ||||
|             <col style="width: 45mm;"/> | ||||
|             <col style="width: 20mm;"/> | ||||
|             <col style="width: 20mm;"/> | ||||
|             <col style="width: 20mm;"/> | ||||
|             <col style="width: 20mm;"/> | ||||
|             <col style="width: 20mm;"/> | ||||
|             <col style="width: 20mm;"/> | ||||
|         </colgroup> | ||||
|         <thead> | ||||
|             <tr> | ||||
|                 <th><b>Lese @Model.Delivery.Year</b> per @($"{Model.Date:dd.MM.yyyy}") [kg]</th> | ||||
|                 <th>Lieferpflicht</th> | ||||
|                 <th>Lieferrecht</th> | ||||
|                 <th>Unterliefert</th> | ||||
|                 <th>Noch zu liefern</th> | ||||
|                 <th>Überliefert</th> | ||||
|                 <th>Geliefert</th> | ||||
|             </tr> | ||||
|         </thead> | ||||
|         <tbody> | ||||
|             @{ | ||||
|                 string FormatRow(int obligation, int right, int sum) { | ||||
|                     return $"<td>{obligation:N0}</td>" + | ||||
|                         $"<td>{right:N0}</td>" + | ||||
|                         $"<td>{(sum < obligation ? $"{obligation - sum:N0}" : "-")}</td>" + | ||||
|                         $"<td>{(sum >= obligation && sum <= right ? $"{right - sum:N0}" : "-")}</td>" + | ||||
|                         $"<td>{(sum > right ? $"{sum - right:N0}" : "-")}</td>" + | ||||
|                         $"<td>{sum:N0}</td>"; | ||||
|                 } | ||||
|                 <tr> | ||||
|                     <th>Gesamtlieferung lt. gez. GA</th> | ||||
|                     @Raw(FormatRow(Model.Member.DeliveryObligation, Model.Member.DeliveryRight, Model.Member.Deliveries.Where(d => d.Year == Model.Delivery.Year).Sum(d => d.Weight))) | ||||
|                 var sortids = Model.Delivery.Parts.Select(p => p.SortId).ToList(); | ||||
|                 var bins = Model.MemberBins.GroupBy(b => b.Key[..2]).ToDictionary(g => g.Key, g => g.Count()); | ||||
|             } | ||||
|             <tr> | ||||
|                 <th>Gesamtlieferung lt. gez. GA</th> | ||||
|                 @Raw(FormatRow(Model.Member.DeliveryObligation, Model.Member.DeliveryRight, Model.Member.Deliveries.Where(d => d.Year == Model.Delivery.Year).Sum(d => d.Weight))) | ||||
|             </tr> | ||||
|             @if (Model.DisplayStats > 1) { | ||||
|                 <tr class="subheading"> | ||||
|                     <th>Flächenbindungen:</th> | ||||
|                 </tr> | ||||
|                 @if (Model.DisplayStats > 1) { | ||||
|                     <tr class="subheading"> | ||||
|                         <th>Flächenbindungen:</th> | ||||
|                     </tr> | ||||
|                     @foreach (var (id, name, right, obligation, sum) in Model.MemberBins.OrderBy(b => b.Item1)) { | ||||
|                         if (right > 0 || obligation > 0 || (sum > 0 && bins[id[..2]] > 1 && !id.EndsWith('_'))) { | ||||
|                             <tr class="@(sortids.Contains(id[..2]) ? "" : "optional")"> | ||||
|                                 <th>@name</th> | ||||
|                                 @Raw(FormatRow(obligation, right, sum)) | ||||
|                             </tr> | ||||
|                         } | ||||
|                 @foreach (var (id, (name, right, obligation, sum, _)) in Model.MemberBins.OrderBy(b => b.Key)) { | ||||
|                     if (right > 0 || obligation > 0 || (sum > 0 && bins[id[..2]] > 1 && !id.EndsWith('_'))) { | ||||
|                         <tr class="@(sortids.Contains(id[..2]) ? "" : "optional")"> | ||||
|                             <th>@name</th> | ||||
|                             @Raw(FormatRow(obligation, right, sum)) | ||||
|                         </tr> | ||||
|                     } | ||||
|                 } | ||||
|             </tbody> | ||||
|         </table> | ||||
|     </div> | ||||
|             } | ||||
|         </tbody> | ||||
|     </table> | ||||
| } | ||||
| </main> | ||||
| @for (int i = 0; i < 2; i++) { | ||||
|     <div class="@(i == 0 ? "hidden" : "bottom")"> | ||||
|     <div class="text @(i == 0 ? "hidden" : "bottom")"> | ||||
|         @if (Model.Text != null) { | ||||
|             <p class="comment">@Model.Text</p> | ||||
|         } | ||||
|   | ||||
| @@ -7,7 +7,7 @@ namespace Elwig.Documents { | ||||
|  | ||||
|         public Delivery Delivery; | ||||
|         public string? Text; | ||||
|         public IEnumerable<(string, string, int, int, int)> MemberBins; | ||||
|         public Dictionary<string, (string, int, int, int, int)> MemberBins; | ||||
|  | ||||
|         // 0 - none | ||||
|         // 1 - GA only | ||||
| @@ -27,7 +27,7 @@ namespace Elwig.Documents { | ||||
|                 $"</tbody></table>"; | ||||
|             Text = App.Client.TextDeliveryNote; | ||||
|             DocumentId = d.LsNr; | ||||
|             MemberBins = ctx.GetMemberBins(d.Member, d.Year).GetAwaiter().GetResult(); | ||||
|             MemberBins = ctx.GetMemberBins(d.Year, d.Member.MgNr).GetAwaiter().GetResult(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
|  | ||||
| table.delivery-confirmation { | ||||
|     font-size: 10pt; | ||||
|     margin-bottom: 5mm; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation thead { | ||||
| @@ -68,3 +69,40 @@ table.delivery-confirmation tr.sum { | ||||
| table.delivery-confirmation tr.sum td { | ||||
|     padding-top: 1mm; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation-stats { | ||||
|     font-size: 10pt; | ||||
|     break-inside: avoid; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation-stats th, | ||||
| table.delivery-confirmation-stats td { | ||||
|     padding: 0.125mm 0; | ||||
|     overflow: hidden; | ||||
|     white-space: nowrap; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation-stats tr.subheading th { | ||||
|     text-align: left; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation-stats thead th { | ||||
|     font-weight: normal; | ||||
|     font-style: italic; | ||||
|     text-align: right; | ||||
|     font-size: 8pt; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation-stats thead th:first-child { | ||||
|     text-align: left; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation-stats td { | ||||
|     text-align: right; | ||||
| } | ||||
|  | ||||
| table.delivery-confirmation-stats tbody th { | ||||
|     font-weight: normal; | ||||
|     font-style: italic; | ||||
|     text-align: left; | ||||
| } | ||||
|   | ||||
| @@ -52,45 +52,45 @@ table.delivery tr.sum td { | ||||
|     padding-top: 1mm; | ||||
| } | ||||
|  | ||||
| table.delivery-stats { | ||||
| table.delivery-note-stats { | ||||
|     font-size: 8pt; | ||||
|     break-inside: avoid; | ||||
|     break-after: avoid; | ||||
| } | ||||
|  | ||||
| table.delivery-stats th, | ||||
| table.delivery-stats td { | ||||
| table.delivery-note-stats th, | ||||
| table.delivery-note-stats td { | ||||
|     padding: 0.125mm 0; | ||||
| } | ||||
|  | ||||
| table.delivery-stats:not(.expanded) tr.optional { | ||||
| table.delivery-note-stats:not(.expanded) tr.optional { | ||||
|     display: none; | ||||
| } | ||||
|  | ||||
| table.delivery-stats tr.subheading th { | ||||
| table.delivery-note-stats tr.subheading th { | ||||
|     text-align: left; | ||||
| } | ||||
|  | ||||
| table.delivery-stats.expanded tr.subheading:not(:has(~ tr)), | ||||
| table.delivery-stats tr.subheading:not(:has(~ tr:not(.optional))) { | ||||
| table.delivery-note-stats.expanded tr.subheading:not(:has(~ tr)), | ||||
| table.delivery-note-stats tr.subheading:not(:has(~ tr:not(.optional))) { | ||||
|     display: none; | ||||
| } | ||||
|  | ||||
| table.delivery-stats thead th { | ||||
| table.delivery-note-stats thead th { | ||||
|     font-weight: normal; | ||||
|     font-style: italic; | ||||
|     text-align: right; | ||||
| } | ||||
|  | ||||
| table.delivery-stats thead th:first-child { | ||||
| table.delivery-note-stats thead th:first-child { | ||||
|     text-align: left; | ||||
| } | ||||
|  | ||||
| table.delivery-stats td { | ||||
| table.delivery-note-stats td { | ||||
|     text-align: right; | ||||
| } | ||||
|  | ||||
| table.delivery-stats tbody th { | ||||
| table.delivery-note-stats tbody th { | ||||
|     font-weight: normal; | ||||
|     font-style: italic; | ||||
|     text-align: left; | ||||
|   | ||||
| @@ -7,7 +7,7 @@ | ||||
|     <UseWPF>true</UseWPF> | ||||
|     <PreserveCompilationContext>true</PreserveCompilationContext> | ||||
|     <ApplicationIcon>elwig.ico</ApplicationIcon> | ||||
|     <Version>0.3.7</Version> | ||||
|     <Version>0.4.0</Version> | ||||
|     <SatelliteResourceLanguages>de-AT</SatelliteResourceLanguages> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   | ||||
| @@ -53,6 +53,10 @@ namespace Elwig.Helpers { | ||||
|  | ||||
|         public static string ConnectionString => $"Data Source=\"{App.Config.DatabaseFile}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default"; | ||||
|  | ||||
|         private readonly Dictionary<int, Dictionary<int, Dictionary<string, (int, int)>>> _memberRightsAndObligations = new(); | ||||
|         private readonly Dictionary<int, Dictionary<int, Dictionary<string, int>>> _memberDeliveryBins = new(); | ||||
|         private readonly Dictionary<int, Dictionary<int, Dictionary<string, int>>> _memberPaymentBins = new(); | ||||
|  | ||||
|         public AppDbContext() { | ||||
|             if (App.Config.DatabaseLog != null) { | ||||
|                 try { | ||||
| @@ -206,26 +210,111 @@ namespace Elwig.Helpers { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<IEnumerable<(string, string, int, int, int)>> GetMemberBins(Member m, int year) { | ||||
|             using var cnx = await ConnectAsync(); | ||||
|             var (rights, obligations) = await Billing.Billing.GetMemberRightsObligations(cnx, year, m.MgNr); | ||||
|             var bins = await Billing.Billing.GetMemberBinWeights(m.MgNr, year, cnx); | ||||
|  | ||||
|             var list = new List<(string, string, int, int, int)>(); | ||||
|             foreach (var id in rights.Keys.Union(obligations.Keys).Union(bins.Keys)) { | ||||
|                 var s = await WineVarieties.FindAsync(id[..2]); | ||||
|                 var attrIds = id[2..]; | ||||
|                 var a = await WineAttributes.Where(a => attrIds.Contains(a.AttrId)).ToListAsync(); | ||||
|                 var name = (s?.Name ?? "") + (a.Count > 0 ? $" ({string.Join(" / ", a.Select(a => a.Name))})" : ""); | ||||
|                 list.Add(( | ||||
|                     id, name, | ||||
|                     rights.TryGetValue(id, out var v1) ? v1 : 0, | ||||
|                     obligations.TryGetValue(id, out var v2) ? v2 : 0, | ||||
|                     bins.TryGetValue(id, out var v3) ? v3 : 0 | ||||
|                 )); | ||||
|         private async Task FetchMemberRightsAndObligations(int year, SqliteConnection? cnx = null) { | ||||
|             var ownCnx = cnx == null; | ||||
|             cnx ??= await ConnectAsync(); | ||||
|             var bins = new Dictionary<int, Dictionary<string, (int, int)>>(); | ||||
|             using (var cmd = cnx.CreateCommand()) { | ||||
|                 cmd.CommandText = $""" | ||||
|                     SELECT mgnr, t.vtrgid, | ||||
|                            ROUND(SUM(COALESCE(area * min_kg_per_ha, 0)) / 10000.0) AS min_kg, | ||||
|                            ROUND(SUM(COALESCE(area * max_kg_per_ha, 0)) / 10000.0) AS max_kg | ||||
|                     FROM area_commitment c | ||||
|                     JOIN area_commitment_type t ON t.vtrgid = c.vtrgid | ||||
|                     WHERE (year_from IS NULL OR year_from <= {year}) AND | ||||
|                           (year_to IS NULL OR year_to >= {year}) | ||||
|                     GROUP BY mgnr, t.vtrgid | ||||
|                     ORDER BY LENGTH(t.vtrgid) DESC, t.vtrgid | ||||
|                     """; | ||||
|                 using var reader = await cmd.ExecuteReaderAsync(); | ||||
|                 while (await reader.ReadAsync()) { | ||||
|                     var mgnr = reader.GetInt32(0); | ||||
|                     var vtrgid = reader.GetString(1); | ||||
|                     if (!bins.ContainsKey(mgnr)) bins[mgnr] = new(); | ||||
|                     bins[mgnr][vtrgid] = (reader.GetInt32(3), reader.GetInt32(2)); | ||||
|                 } | ||||
|             } | ||||
|             if (ownCnx) await cnx.DisposeAsync(); | ||||
|             _memberRightsAndObligations[year] = bins; | ||||
|         } | ||||
|  | ||||
|             return list; | ||||
|         private async Task FetchMemberDeliveryBins(int year, SqliteConnection? cnx = null) { | ||||
|             var ownCnx = cnx == null; | ||||
|             cnx ??= await ConnectAsync(); | ||||
|             var bins = new Dictionary<int, Dictionary<string, int>>(); | ||||
|             using (var cmd = cnx.CreateCommand()) { | ||||
|                 cmd.CommandText = $"SELECT mgnr, bin, weight FROM v_delivery_bin WHERE year = {year}"; | ||||
|                 using var reader = await cmd.ExecuteReaderAsync(); | ||||
|                 while (await reader.ReadAsync()) { | ||||
|                     var mgnr = reader.GetInt32(0); | ||||
|                     var bin = reader.GetString(1); | ||||
|                     if (!bins.ContainsKey(mgnr)) bins[mgnr] = new(); | ||||
|                     bins[mgnr][bin] = reader.GetInt32(2); | ||||
|                 } | ||||
|             } | ||||
|             if (ownCnx) await cnx.DisposeAsync(); | ||||
|             _memberDeliveryBins[year] = bins; | ||||
|         } | ||||
|  | ||||
|         private async Task FetchMemberPaymentBins(int year, SqliteConnection? cnx = null) { | ||||
|             var ownCnx = cnx == null; | ||||
|             cnx ??= await ConnectAsync(); | ||||
|             var bins = new Dictionary<int, Dictionary<string, int>>(); | ||||
|             using (var cmd = cnx.CreateCommand()) { | ||||
|                 cmd.CommandText = $"SELECT mgnr, bin, weight FROM v_payment_bin WHERE year = {year}"; | ||||
|                 using var reader = await cmd.ExecuteReaderAsync(); | ||||
|                 while (await reader.ReadAsync()) { | ||||
|                     var mgnr = reader.GetInt32(0); | ||||
|                     var bin = reader.GetString(1); | ||||
|                     if (!bins.ContainsKey(mgnr)) bins[mgnr] = new(); | ||||
|                     bins[mgnr][bin] = reader.GetInt32(2); | ||||
|                 } | ||||
|             } | ||||
|             if (ownCnx) await cnx.DisposeAsync(); | ||||
|             _memberPaymentBins[year] = bins; | ||||
|         } | ||||
|  | ||||
|         public async Task<Dictionary<string, (int, int)>> GetMemberRightsAndObligations(int year, int mgnr, SqliteConnection? cnx = null) { | ||||
|             if (!_memberRightsAndObligations.ContainsKey(year)) | ||||
|                 await FetchMemberRightsAndObligations(year, cnx); | ||||
|             return _memberRightsAndObligations[year].GetValueOrDefault(mgnr, new()); | ||||
|         } | ||||
|  | ||||
|         public async Task<Dictionary<string, int>> GetMemberDeliveryBins(int year, int mgnr, SqliteConnection? cnx = null) { | ||||
|             if (!_memberDeliveryBins.ContainsKey(year)) | ||||
|                 await FetchMemberDeliveryBins(year, cnx); | ||||
|             return _memberDeliveryBins[year].GetValueOrDefault(mgnr, new()); | ||||
|         } | ||||
|  | ||||
|         public async Task<Dictionary<string, int>> GetMemberPaymentBins(int year, int mgnr, SqliteConnection? cnx = null) { | ||||
|             if (!_memberPaymentBins.ContainsKey(year)) | ||||
|                 await FetchMemberPaymentBins(year, cnx); | ||||
|             return _memberPaymentBins[year].GetValueOrDefault(mgnr, new()); | ||||
|         } | ||||
|  | ||||
|         public async Task<Dictionary<string, (string, int, int, int, int)>> GetMemberBins(int year, int mgnr, SqliteConnection? cnx = null) { | ||||
|             var ownCnx = cnx == null; | ||||
|             cnx ??= await ConnectAsync(); | ||||
|             var rightsAndObligations = await GetMemberRightsAndObligations(year, mgnr, cnx); | ||||
|             var deliveryBins = await GetMemberDeliveryBins(year, mgnr, cnx); | ||||
|             var paymentBins = await GetMemberPaymentBins(year, mgnr, cnx); | ||||
|             if (ownCnx) await cnx.DisposeAsync(); | ||||
|  | ||||
|             var bins = new Dictionary<string, (string, int, int, int, int)>(); | ||||
|             foreach (var id in rightsAndObligations.Keys.Union(deliveryBins.Keys).Union(paymentBins.Keys)) { | ||||
|                 var variety = await WineVarieties.FindAsync(id[..2]); | ||||
|                 var attrIds = id[2..]; | ||||
|                 var attrs = await WineAttributes.Where(a => attrIds.Contains(a.AttrId)).ToListAsync(); | ||||
|                 var name = (variety?.Name ?? "") + (attrs.Count > 0 ? $" ({string.Join(" / ", attrs.Select(a => a.Name))})" : ""); | ||||
|                 bins[id] = ( | ||||
|                     name, | ||||
|                     rightsAndObligations.GetValueOrDefault(id).Item1, | ||||
|                     rightsAndObligations.GetValueOrDefault(id).Item2, | ||||
|                     deliveryBins.GetValueOrDefault(id), | ||||
|                     paymentBins.GetValueOrDefault(id) | ||||
|                 ); | ||||
|             } | ||||
|             return bins; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -81,60 +81,37 @@ namespace Elwig.Helpers { | ||||
|         } | ||||
|  | ||||
|         private static void UpdateDbSchema_2_To_3(SqliteConnection cnx) { | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_1_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_2_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_3_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_4_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_5_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_6_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_7_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_8_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE season ADD COLUMN bin_9_name TEXT DEFAULT NULL"); | ||||
|             ExecuteNonQuery(cnx, """ | ||||
|                 UPDATE season | ||||
|                 SET bin_1_name = n.bucket_1_name, | ||||
|                     bin_2_name = n.bucket_2_name, | ||||
|                     bin_3_name = n.bucket_3_name, | ||||
|                     bin_4_name = n.bucket_4_name, | ||||
|                     bin_5_name = n.bucket_5_name, | ||||
|                     bin_6_name = n.bucket_6_name, | ||||
|                     bin_7_name = n.bucket_7_name, | ||||
|                     bin_8_name = n.bucket_8_name, | ||||
|                     bin_9_name = n.bucket_9_name | ||||
|                 FROM (SELECT year, bucket_1_name, bucket_2_name, bucket_3_name, bucket_4_name, bucket_5_name, bucket_6_name, bucket_7_name, bucket_8_name, bucket_9_name | ||||
|                       FROM payment_variant GROUP BY year HAVING avnr = MAX(avnr)) AS n | ||||
|                 WHERE season.year = n.year | ||||
|                 """); | ||||
|             ExecuteNonQuery(cnx, """ | ||||
|                 CREATE TABLE delivery_part_bin ( | ||||
|                     year     INTEGER NOT NULL, | ||||
|                     did      INTEGER NOT NULL, | ||||
|                     dpnr     INTEGER NOT NULL, | ||||
|                     binnr    INTEGER NOT NULL, | ||||
|  | ||||
|                     bin_1 INTEGER DEFAULT NULL, | ||||
|                     bin_2 INTEGER DEFAULT NULL, | ||||
|                     bin_3 INTEGER DEFAULT NULL, | ||||
|                     bin_4 INTEGER DEFAULT NULL, | ||||
|                     bin_5 INTEGER DEFAULT NULL, | ||||
|                     bin_6 INTEGER DEFAULT NULL, | ||||
|                     bin_7 INTEGER DEFAULT NULL, | ||||
|                     bin_8 INTEGER DEFAULT NULL, | ||||
|                     bin_9 INTEGER DEFAULT NULL, | ||||
|                     discr    TEXT    NOT NULL, | ||||
|                     value    INTEGER NOT NULL, | ||||
|  | ||||
|                     CONSTRAINT pk_delivery_part_bin PRIMARY KEY (year, did, dpnr), | ||||
|                     CONSTRAINT pk_delivery_part_bin PRIMARY KEY (year, did, dpnr, binnr), | ||||
|                     CONSTRAINT fk_delivery_part_bin_delivery_part FOREIGN KEY (year, did, dpnr) REFERENCES delivery_part (year, did, dpnr) | ||||
|                         ON UPDATE CASCADE | ||||
|                         ON DELETE CASCADE | ||||
|                 ) STRICT; | ||||
|                 """); | ||||
|             ExecuteNonQuery(cnx, """ | ||||
|                 INSERT INTO delivery_part_bin | ||||
|                       (year, did, dpnr, bin_1, bin_2, bin_3, bin_4, bin_5, bin_6, bin_7, bin_8, bin_9) | ||||
|                 SELECT year, did, dpnr, bucket_1, bucket_2, bucket_3, bucket_4, bucket_5, bucket_6, bucket_7, bucket_8, bucket_9 | ||||
|                 INSERT INTO delivery_part_bin (year, did, dpnr, binnr, discr, value) | ||||
|                 SELECT year, did, dpnr, 0, '_', bucket_2 + bucket_3 | ||||
|                 FROM payment_delivery_part | ||||
|                 WHERE COALESCE(bucket_1, bucket_2, bucket_3, bucket_4, bucket_5, bucket_6, bucket_7, bucket_8, bucket_9) IS NOT NULL | ||||
|                 ON CONFLICT DO NOTHING; | ||||
|                 """); | ||||
|             ExecuteNonQuery(cnx, """ | ||||
|                 INSERT INTO delivery_part_bin (year, did, dpnr, binnr, discr, value) | ||||
|                 SELECT d.year, d.did, d.dpnr, 1, COALESCE(attributes, ''), bucket_1 | ||||
|                 FROM payment_delivery_part p | ||||
|                     JOIN v_delivery d ON (d.year, d.did, d.dpnr) = (p.year, p.did, p.dpnr) | ||||
|                 WHERE COALESCE(bucket_1, bucket_2, bucket_3, bucket_4, bucket_5, bucket_6, bucket_7, bucket_8, bucket_9) IS NOT NULL | ||||
|                 ON CONFLICT DO NOTHING; | ||||
|                 """); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN bucket_1"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN bucket_2"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE payment_delivery_part DROP COLUMN bucket_3"); | ||||
| @@ -175,10 +152,11 @@ namespace Elwig.Helpers { | ||||
|                         JOIN member m ON m.mgnr = d.mgnr | ||||
|                         LEFT JOIN delivery_part_attribute pa ON (pa.year, pa.did, pa.dpnr) = (p.year, p.did, p.dpnr) | ||||
|                         LEFT JOIN wine_attribute a ON a.attrid = pa.attrid | ||||
|                     GROUP BY p.year, p.did, p.dpnr) s | ||||
|                     GROUP BY p.year, p.did, p.dpnr | ||||
|                     ORDER BY p.year, p.did, p.dpnr, a.attrid) s | ||||
|                 LEFT JOIN delivery_part_modifier o ON (o.year, o.did, o.dpnr) = (s.year, s.did, s.dpnr) | ||||
|                 GROUP BY s.year, s.lsnr, s.dpnr | ||||
|                 ORDER BY s.year, s.lsnr, s.dpnr; | ||||
|                 ORDER BY s.year, s.lsnr, s.dpnr, o.modid; | ||||
|                 """); | ||||
|  | ||||
|             ExecuteNonQuery(cnx, "DROP VIEW v_bucket"); | ||||
| @@ -192,9 +170,19 @@ namespace Elwig.Helpers { | ||||
|                 ORDER BY year, mgnr, LENGTH(bin) DESC, bin; | ||||
|                 """); | ||||
|  | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE wine_attribute ADD COLUMN fill_lower_bins INTEGER NOT NULL CHECK (fill_lower_bins IN (0, 1, 2)) DEFAULT 0"); | ||||
|             ExecuteNonQuery(cnx, """ | ||||
|                 CREATE VIEW v_payment_bin AS | ||||
|                 SELECT d.year, d.mgnr, | ||||
|                        sortid || discr AS bin, | ||||
|                        SUM(value) AS weight | ||||
|                 FROM v_delivery d | ||||
|                     JOIN delivery_part_bin b ON (b.year, b.did, b.dpnr) = (d.year, d.did, d.dpnr) | ||||
|                 GROUP BY d.year, d.mgnr, bin | ||||
|                 HAVING SUM(value) > 0 | ||||
|                 ORDER BY d.year, d.mgnr, bin; | ||||
|                 """); | ||||
|  | ||||
|             ExecuteNonQuery(cnx, "UPDATE delivery_part_bin SET bin_2 = bin_2 + bin_3, bin_3 = NULL WHERE bin_4 IS NULL"); | ||||
|             ExecuteNonQuery(cnx, "ALTER TABLE wine_attribute ADD COLUMN fill_lower_bins INTEGER NOT NULL CHECK (fill_lower_bins IN (0, 1, 2)) DEFAULT 0"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -26,36 +26,32 @@ namespace Elwig.Helpers.Billing { | ||||
|             using (var cmd = cnx.CreateCommand()) { | ||||
|                 cmd.CommandText = $""" | ||||
|                     UPDATE season | ||||
|                     SET (start_date, end_date) = (SELECT MIN(date), MAX(date) FROM delivery WHERE year = {Year}), | ||||
|                         bin_1_name = 'gebunden mit zwei Attributen', | ||||
|                         bin_2_name = 'gebunden mit (erstem) Attribut', | ||||
|                         bin_3_name = 'gebunden mit zweitem Attribut', | ||||
|                         bin_4_name = 'gebunden ohne Attribut', | ||||
|                         bin_5_name = 'ungebunden', | ||||
|                         bin_6_name = NULL, | ||||
|                         bin_7_name = NULL, | ||||
|                         bin_8_name = NULL, | ||||
|                         bin_9_name = NULL | ||||
|                     SET (start_date, end_date) = (SELECT MIN(date), MAX(date) FROM delivery WHERE year = {Year}) | ||||
|                     WHERE year = {Year} | ||||
|                     """; | ||||
|                 await cmd.ExecuteNonQueryAsync(); | ||||
|             } | ||||
|  | ||||
|             using (var cmd = cnx.CreateCommand()) { | ||||
|                 cmd.CommandText = $"DELETE FROM delivery_part_bin WHERE year = {Year}"; | ||||
|                 await cmd.ExecuteNonQueryAsync(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task CalculateBins() { | ||||
|         public async Task CalculateBins(bool allowAttrsIntoLowerBins, bool avoidUnderDeliveries, bool honorGebunden) { | ||||
|             var attrVals = Context.WineAttributes.ToDictionary(a => a.AttrId, a => a.FillLowerBins); | ||||
|             var attrForced = attrVals.Where(a => a.Value == 0).Select(a => a.Key).ToArray(); | ||||
|             using var cnx = await AppDbContext.ConnectAsync(); | ||||
|             var memberOblRig = await GetMemberRightsObligations(cnx, Year); | ||||
|             var inserts = new List<(int, int, int, int, int, int, int)>(); | ||||
|             await Context.GetMemberRightsAndObligations(Year, 0, cnx); | ||||
|             var inserts = new List<(int, int, int, string, int)>(); | ||||
|  | ||||
|             var deliveries = new List<(int, int, int, string, int, double, string, string[], string[])>(); | ||||
|             var deliveries = new List<(int, int, int, string, int, double, string, string[], string[], bool?)>(); | ||||
|             using (var cmd = cnx.CreateCommand()) { | ||||
|                 cmd.CommandText = $""" | ||||
|                     SELECT mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers | ||||
|                     SELECT mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers, gebunden | ||||
|                     FROM v_delivery | ||||
|                     WHERE year = {Year} | ||||
|                     ORDER BY mgnr, sortid, abgewertet ASC, | ||||
|                     ORDER BY mgnr, sortid, abgewertet ASC, {(honorGebunden ? "gebunden IS NOT NULL DESC, gebunden DESC," : "")} | ||||
|                              COALESCE(LENGTH(attributes), 0) ASC, attribute_prio DESC, COALESCE(attributes, '~'), | ||||
|                              kmw DESC, lsnr, dpnr | ||||
|                     """; | ||||
| @@ -65,28 +61,29 @@ namespace Elwig.Helpers.Billing { | ||||
|                         reader.GetInt32(0), reader.GetInt32(1), reader.GetInt32(2), reader.GetString(3), reader.GetInt32(4), | ||||
|                         reader.GetDouble(5), reader.GetString(6), | ||||
|                         reader.IsDBNull(7) ? Array.Empty<string>() : reader.GetString(7).Split(",").Order().ToArray(), | ||||
|                         reader.IsDBNull(8) ? Array.Empty<string>() : reader.GetString(8).Split(",").Order().ToArray() | ||||
|                         reader.IsDBNull(8) ? Array.Empty<string>() : reader.GetString(8).Split(",").Order().ToArray(), | ||||
|                         reader.IsDBNull(9) ? null : reader.GetBoolean(9) | ||||
|                     )); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             int lastMgNr = 0; | ||||
|             Dictionary<string, int>? rights = null; | ||||
|             Dictionary<string, int>? obligations = null; | ||||
|             Dictionary<string, (int, int)>? rightsAndObligations = null; | ||||
|             Dictionary<string, int> used = new(); | ||||
|             foreach (var (mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers) in deliveries) { | ||||
|             foreach (var (mgnr, did, dpnr, sortid, weight, kmw, qualid, attributes, modifiers, gebunden) in deliveries) { | ||||
|                 if (lastMgNr != mgnr) { | ||||
|                     var or = memberOblRig.GetValueOrDefault(mgnr, (new(), new())); | ||||
|                     rights = or.Item2; | ||||
|                     obligations = or.Item1; | ||||
|                     rightsAndObligations = await Context.GetMemberRightsAndObligations(Year, mgnr); | ||||
|                     used = new(); | ||||
|                 } | ||||
|                 if (obligations == null || rights == null || obligations.Count == 0 || rights.Count == 0 || | ||||
|                 if ((honorGebunden && gebunden == false) || | ||||
|                     rightsAndObligations == null || rightsAndObligations.Count == 0 || | ||||
|                     qualid == "WEI" || qualid == "RSW" || qualid == "LDW") | ||||
|                 { | ||||
|                     // Explizit als ungebunden markiert, | ||||
|                     // Mitglied hat keine Flächenbindungen, oder | ||||
|                     // Nicht mindestens Qualitätswein (QUW) -> ungebunden | ||||
|                     inserts.Add((did, dpnr, 0, 0, 0, 0, weight)); | ||||
|                     // Nicht mindestens Qualitätswein (QUW) | ||||
|                     // -> ungebunden | ||||
|                     inserts.Add((did, dpnr, 0, "_", weight)); | ||||
|                     continue; | ||||
|                 } | ||||
|  | ||||
| @@ -94,100 +91,41 @@ namespace Elwig.Helpers.Billing { | ||||
|                     throw new NotSupportedException(); | ||||
|  | ||||
|                 int w = weight; | ||||
|                 int[] b = new int[4]; | ||||
|                 foreach (var p in Utils.Permutate(attributes, attributes.Intersect(attrForced))) { | ||||
|                     var c = p.Count(); | ||||
|                     var key = sortid + string.Join("", p); | ||||
|                     if (rights.ContainsKey(key) && obligations.ContainsKey(key)) { | ||||
|                         int i = 0; | ||||
|                     if (rightsAndObligations.ContainsKey(key)) { | ||||
|                         int i = 4; | ||||
|                         if (c == 1) { | ||||
|                             i = (p.ElementAt(0) == attributes[0]) ? 1 : 2; | ||||
|                             i = (p.ElementAt(0) == attributes[0]) ? 2 : 3; | ||||
|                         } else if (c == 0) { | ||||
|                             i = b.Length - 1; | ||||
|                             i = 1; | ||||
|                         } | ||||
|                         var vr = Math.Max(0, Math.Min(rights[key] - used.GetValueOrDefault(key, 0), w)); | ||||
|                         var vo = Math.Max(0, Math.Min(obligations[key] - used.GetValueOrDefault(key, 0), w)); | ||||
|                         var v = (c == 0 || p.Select(a => attrVals[a]).Min() == 2) ? vr : vo; | ||||
|                         b[i] += v; | ||||
|                         used[key] = used.GetValueOrDefault(key, 0) + v; | ||||
|                         var u = used.GetValueOrDefault(key, 0); | ||||
|                         var vr = Math.Max(0, Math.Min(rightsAndObligations[key].Item1 - u, w)); | ||||
|                         var vo = Math.Max(0, Math.Min(rightsAndObligations[key].Item2 - u, w)); | ||||
|                         var v = (attributes.Length == 0 || attributes.Select(a => attrVals[a]).Min() == 2) ? vr : vo; | ||||
|                         used[key] = u + v; | ||||
|                         inserts.Add((did, dpnr, i, key[2..], v)); | ||||
|                         w -= v; | ||||
|                     } | ||||
|                     if (w == 0) break; | ||||
|                     if (w == 0 || !allowAttrsIntoLowerBins) break; | ||||
|                 } | ||||
|                 inserts.Add((did, dpnr, b[0], b[1], b[2], b[3], weight - b[0] - b[1] - b[2] - b[3])); | ||||
|                 inserts.Add((did, dpnr, 0, "_", w)); | ||||
|                 lastMgNr = mgnr; | ||||
|             } | ||||
|  | ||||
|             using (var cmd = cnx.CreateCommand()) { | ||||
|                 cmd.CommandText = $""" | ||||
|                     INSERT INTO delivery_part_bin (year, did, dpnr, bin_1, bin_2, bin_3, bin_4, bin_5) | ||||
|                     VALUES {string.Join(",\n       ", inserts.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, {i.Item4}, {i.Item5}, {i.Item6}, {i.Item7})"))} | ||||
|                     INSERT INTO delivery_part_bin (year, did, dpnr, binnr, discr, value) | ||||
|                     VALUES {string.Join(",\n       ", inserts.Select(i => $"({Year}, {i.Item1}, {i.Item2}, {i.Item3}, '{i.Item4}', {i.Item5})"))} | ||||
|                     ON CONFLICT DO UPDATE | ||||
|                     SET bin_1 = excluded.bin_1, | ||||
|                         bin_2 = excluded.bin_2, | ||||
|                         bin_3 = excluded.bin_3, | ||||
|                         bin_4 = excluded.bin_4, | ||||
|                         bin_5 = excluded.bin_5, | ||||
|                         bin_6 = NULL, | ||||
|                         bin_7 = NULL, | ||||
|                         bin_8 = NULL, | ||||
|                         bin_9 = NULL; | ||||
|                     SET discr = excluded.discr, value = value + excluded.value | ||||
|                     """; | ||||
|                 await cmd.ExecuteNonQueryAsync(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public static async Task<Dictionary<int, (Dictionary<string, int>, Dictionary<string, int>)>> GetMemberRightsObligations(SqliteConnection cnx, int year, int? mgnr = null) { | ||||
|             var members = new Dictionary<int, (Dictionary<string, int>, Dictionary<string, int>)>(); | ||||
|  | ||||
|             using var cmd = cnx.CreateCommand(); | ||||
|             cmd.CommandText = $""" | ||||
|                 SELECT mgnr, t.vtrgid, | ||||
|                        SUM(COALESCE(area * min_kg_per_ha, 0)) / 10000 AS min_kg, | ||||
|                        SUM(COALESCE(area * max_kg_per_ha, 0)) / 10000 AS max_kg | ||||
|                 FROM area_commitment c | ||||
|                 JOIN area_commitment_type t ON t.vtrgid = c.vtrgid | ||||
|                 WHERE ({(mgnr == null ? "NULL" : mgnr)} IS NULL OR mgnr = {(mgnr == null ? "NULL" : mgnr)}) AND | ||||
|                       (year_from IS NULL OR year_from <= {year}) AND | ||||
|                       (year_to IS NULL OR year_to >= {year}) | ||||
|                 GROUP BY mgnr, t.vtrgid | ||||
|                 ORDER BY LENGTH(t.vtrgid) DESC, t.vtrgid | ||||
|                 """; | ||||
|  | ||||
|             var reader = await cmd.ExecuteReaderAsync(); | ||||
|             while (await reader.ReadAsync()) { | ||||
|                 var m = reader.GetInt32(0); | ||||
|                 var vtrgid = reader.GetString(1); | ||||
|                 if (!members.ContainsKey(m)) members[m] = (new(), new()); | ||||
|                 members[m].Item1[vtrgid] = reader.GetInt32(2); | ||||
|                 members[m].Item2[vtrgid] = reader.GetInt32(3); | ||||
|             } | ||||
|  | ||||
|             return members; | ||||
|         } | ||||
|  | ||||
|         public static async Task<(Dictionary<string, int>, Dictionary<string, int>)> GetMemberRightsObligations(SqliteConnection cnx, int year, int mgnr) { | ||||
|             var members = await GetMemberRightsObligations(cnx, year, (int?)mgnr); | ||||
|             return members.GetValueOrDefault(mgnr, (new(), new())); | ||||
|         } | ||||
|  | ||||
|         public static async Task<Dictionary<string, int>> GetMemberBinWeights(int mgnr, int year, SqliteConnection cnx) { | ||||
|             var bins = new Dictionary<string, int>(); | ||||
|  | ||||
|             using var cmd = cnx.CreateCommand(); | ||||
|             cmd.CommandText = $""" | ||||
|                 SELECT bin, weight | ||||
|                 FROM v_delivery_bin | ||||
|                 WHERE (year, mgnr) = ({year}, {mgnr}) | ||||
|                 """; | ||||
|  | ||||
|             var reader = await cmd.ExecuteReaderAsync(); | ||||
|             while (await reader.ReadAsync()) { | ||||
|                 var bin = reader.GetString(0); | ||||
|                 bins[bin] = reader.GetInt32(1); | ||||
|             } | ||||
|  | ||||
|             return bins; | ||||
|             // TODO add second round to avoid under deliveries | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -58,6 +58,7 @@ namespace Elwig.Helpers { | ||||
|         public int ModeDeliveryNoteStats; | ||||
|  | ||||
|         public string? TextDeliveryNote; | ||||
|         public string? TextDeliveryConfirmation; | ||||
|  | ||||
|         public ClientParameters(AppDbContext ctx) : this(ctx.ClientParameters.ToDictionary(e => e.Param, e => e.Value)) { } | ||||
|  | ||||
| @@ -100,6 +101,9 @@ namespace Elwig.Helpers { | ||||
|  | ||||
|                 Sender2 = parameters.GetValueOrDefault("DOCUMENT_SENDER") ?? ""; | ||||
|                 TextDeliveryNote = parameters.GetValueOrDefault("TEXT_DELIVERYNOTE"); | ||||
|                 if (TextDeliveryNote == "") TextDeliveryNote = null; | ||||
|                 TextDeliveryConfirmation = parameters.GetValueOrDefault("TEXT_DELIVERYCONFIRMATION"); | ||||
|                 if (TextDeliveryConfirmation == "") TextDeliveryConfirmation = null; | ||||
|             } catch { | ||||
|                 throw new KeyNotFoundException(); | ||||
|             } | ||||
| @@ -138,6 +142,7 @@ namespace Elwig.Helpers { | ||||
|                 ("MODE_DELIVERYNOTE_STATS", deliveryNoteStats), | ||||
|                 ("DOCUMENT_SENDER", Sender2), | ||||
|                 ("TEXT_DELIVERYNOTE", TextDeliveryNote), | ||||
|                 ("TEXT_DELIVERYCONFIRMATION", TextDeliveryConfirmation), | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -118,10 +118,6 @@ namespace Elwig.Models { | ||||
|         public string OriginString => Origin.OriginString + "\n" + (Kg?.Gl != null ? $" / {Kg.Gl.Name}" : "") + (Kg != null ? $" / {Kg.AtKg.Gem.Name} / KG {Kg.AtKg.Name}" : "") + (Rd != null ? $" / Ried {Rd.Name}" : ""); | ||||
|  | ||||
|         [InverseProperty("Part")] | ||||
|         public virtual DeliveryPartBin? Bin { get; private set; } | ||||
|  | ||||
|         [NotMapped] | ||||
|         public int[] Bins => (new int?[] { Bin?.Bin1, Bin?.Bin2, Bin?.Bin3, Bin?.Bin4, Bin?.Bin5, Bin?.Bin6, Bin?.Bin7, Bin?.Bin8, Bin?.Bin9 }) | ||||
|             .Where(b => b != null).Select(b => b.Value).ToArray(); | ||||
|         public virtual ISet<DeliveryPartBin> Bins { get; private set; } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
|  | ||||
| namespace Elwig.Models { | ||||
|     [Table("delivery_part_bin"), PrimaryKey("Year", "DId", "DPNr")] | ||||
|     [Table("delivery_part_bin"), PrimaryKey("Year", "DId", "DPNr", "BinNr")] | ||||
|     public class DeliveryPartBin { | ||||
|         [Column("year")] | ||||
|         public int Year { get; set; } | ||||
| @@ -13,34 +13,16 @@ namespace Elwig.Models { | ||||
|         [Column("dpnr")] | ||||
|         public int DPNr { get; set; } | ||||
|  | ||||
|         [Column("binnr")] | ||||
|         public int BinNr { get; set; } | ||||
|  | ||||
|         [Column("discr")] | ||||
|         public string Discr { get; set; } | ||||
|  | ||||
|         [Column("value")] | ||||
|         public int Value { get; set; } | ||||
|  | ||||
|         [ForeignKey("Year, DId, DPNr")] | ||||
|         public virtual DeliveryPart Part { get; private set; } | ||||
|  | ||||
|         [Column("bin_1")] | ||||
|         public int? Bin1 { get; set; } | ||||
|  | ||||
|         [Column("bin_2")] | ||||
|         public int? Bin2 { get; set; } | ||||
|  | ||||
|         [Column("bin_3")] | ||||
|         public int? Bin3 { get; set; } | ||||
|  | ||||
|         [Column("bin_4")] | ||||
|         public int? Bin4 { get; set; } | ||||
|  | ||||
|         [Column("bin_5")] | ||||
|         public int? Bin5 { get; set; } | ||||
|  | ||||
|         [Column("bin_6")] | ||||
|         public int? Bin6 { get; set; } | ||||
|  | ||||
|         [Column("bin_7")] | ||||
|         public int? Bin7 { get; set; } | ||||
|  | ||||
|         [Column("bin_8")] | ||||
|         public int? Bin8 { get; set; } | ||||
|  | ||||
|         [Column("bin_9")] | ||||
|         public int? Bin9 { get; set; } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,5 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System.Collections.Generic; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
| using System.Linq; | ||||
|  | ||||
| namespace Elwig.Models { | ||||
|     [Table("payment_delivery_part"), PrimaryKey("Year", "DId", "DPNr", "AvNr")] | ||||
| @@ -34,78 +32,6 @@ namespace Elwig.Models { | ||||
|             set => ModRelValue = (double)value; | ||||
|         } | ||||
|  | ||||
|         [Column("price_1")] | ||||
|         public long? Price1Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price1 { | ||||
|             get => Price1Value != null ? Variant.Season.DecFromDb(Price1Value.Value) : null; | ||||
|             set => Price1Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
|         } | ||||
|  | ||||
|         [Column("price_2")] | ||||
|         public long? Price2Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price2 { | ||||
| 			get => Price2Value != null ? Variant.Season.DecFromDb(Price2Value.Value) : null; | ||||
| 			set => Price2Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("price_3")] | ||||
|         public long? Price3Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price3 { | ||||
| 			get => Price3Value != null ? Variant.Season.DecFromDb(Price3Value.Value) : null; | ||||
| 			set => Price3Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("price_4")] | ||||
|         public long? Price4Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price4 { | ||||
| 			get => Price4Value != null ? Variant.Season.DecFromDb(Price4Value.Value) : null; | ||||
| 			set => Price4Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("price_5")] | ||||
|         public long? Price5Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price5 { | ||||
| 			get => Price5Value != null ? Variant.Season.DecFromDb(Price5Value.Value) : null; | ||||
| 			set => Price5Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("price_6")] | ||||
|         public long? Price6Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price6 { | ||||
| 			get => Price6Value != null ? Variant.Season.DecFromDb(Price6Value.Value) : null; | ||||
| 			set => Price6Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("price_7")] | ||||
|         public long? Price7Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price7 { | ||||
| 			get => Price7Value != null ? Variant.Season.DecFromDb(Price7Value.Value) : null; | ||||
| 			set => Price7Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("price_8")] | ||||
|         public long? Price8Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price8 { | ||||
| 			get => Price8Value != null ? Variant.Season.DecFromDb(Price8Value.Value) : null; | ||||
| 			set => Price8Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("price_9")] | ||||
|         public long? Price9Value { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal? Price9 { | ||||
| 			get => Price9Value != null ? Variant.Season.DecFromDb(Price9Value.Value) : null; | ||||
| 			set => Price9Value = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
| 		} | ||||
|  | ||||
|         [Column("amount")] | ||||
|         public long? AmountValue { get; set; } | ||||
|         [NotMapped] | ||||
| @@ -114,10 +40,6 @@ namespace Elwig.Models { | ||||
|             set => AmountValue = value != null ? Variant.Season.DecToDb(value.Value) : null; | ||||
|         } | ||||
|  | ||||
|         [NotMapped] | ||||
|         public decimal[] Prices => (new decimal?[] { Price1, Price2, Price3, Price4, Price5, Price6, Price7, Price8, Price9 }) | ||||
|             .Where(p => p != null).Select(p => p.Value).ToArray(); | ||||
|  | ||||
|         [ForeignKey("Year, AvNr")] | ||||
|         public virtual PaymentVar Variant { get; private set; } | ||||
|  | ||||
|   | ||||
							
								
								
									
										44
									
								
								Elwig/Models/PaymentDeliveryPartBin.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								Elwig/Models/PaymentDeliveryPartBin.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
|  | ||||
| namespace Elwig.Models { | ||||
|     [Table("payment_delivery_part_bin"), PrimaryKey("Year", "DId", "DPNr", "BinNr", "AvNr")] | ||||
|     public class PaymentDeliveryPartBin { | ||||
|         [Column("year")] | ||||
|         public int Year { get; set; } | ||||
|  | ||||
|         [Column("did")] | ||||
|         public int DId { get; set; } | ||||
|  | ||||
|         [Column("dpnr")] | ||||
|         public int DPNr { get; set; } | ||||
|  | ||||
|         [Column("binnr")] | ||||
|         public int BinNr { get; set; } | ||||
|  | ||||
|         [Column("avnr")] | ||||
|         public int AvNr { get; set; } | ||||
|  | ||||
|         [Column("price")] | ||||
|         public long PriceValue { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal Price { | ||||
|             get => Variant.Season.DecFromDb(PriceValue); | ||||
|             set => PriceValue = Variant.Season.DecToDb(value); | ||||
|         } | ||||
|  | ||||
|         [Column("amount")] | ||||
|         public long AmountValue { get; set; } | ||||
|         [NotMapped] | ||||
|         public decimal Amount { | ||||
|             get => Variant.Season.DecFromDb(AmountValue); | ||||
|             set => AmountValue = Variant.Season.DecToDb(value); | ||||
|         } | ||||
|  | ||||
|         [ForeignKey("Year, AvNr")] | ||||
|         public virtual PaymentVar Variant { get; private set; } | ||||
|  | ||||
|         [ForeignKey("Year, DId, DPNr")] | ||||
|         public virtual DeliveryPart DeliveryPart { get; private set; } | ||||
|     } | ||||
| } | ||||
| @@ -3,7 +3,6 @@ using Microsoft.EntityFrameworkCore; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
| using System.Linq; | ||||
|  | ||||
| namespace Elwig.Models { | ||||
|     [Table("season"), PrimaryKey("Year")] | ||||
| @@ -43,37 +42,6 @@ namespace Elwig.Models { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         [Column("bin_1_name")] | ||||
|         public string? Bin1Name { get; set; } | ||||
|  | ||||
|         [Column("bin_2_name")] | ||||
|         public string? Bin2Name { get; set; } | ||||
|  | ||||
|         [Column("bin_3_name")] | ||||
|         public string? Bin3Name { get; set; } | ||||
|  | ||||
|         [Column("bin_4_name")] | ||||
|         public string? Bin4Name { get; set; } | ||||
|  | ||||
|         [Column("bin_5_name")] | ||||
|         public string? Bin5Name { get; set; } | ||||
|  | ||||
|         [Column("bin_6_name")] | ||||
|         public string? Bin6Name { get; set; } | ||||
|  | ||||
|         [Column("bin_7_name")] | ||||
|         public string? Bin7Name { get; set; } | ||||
|  | ||||
|         [Column("bin_8_name")] | ||||
|         public string? Bin8Name { get; set; } | ||||
|  | ||||
|         [Column("bin_9_name")] | ||||
|         public string? Bin9Name { get; set; } | ||||
|  | ||||
|         [NotMapped] | ||||
|         public string[] BinNames => (new string?[] { Bin1Name, Bin2Name, Bin3Name, Bin4Name, Bin5Name, Bin6Name, Bin7Name, Bin8Name, Bin9Name }) | ||||
|             .Where(n => n != null).Select(n => n ?? "").ToArray(); | ||||
|  | ||||
|         [ForeignKey("CurrencyCode")] | ||||
|         public virtual Currency Currency { get; private set; } | ||||
|  | ||||
|   | ||||
| @@ -241,6 +241,17 @@ | ||||
|                                 </GroupBox> | ||||
|                             </Grid> | ||||
|                         </GroupBox> | ||||
|                         <GroupBox Header="Anlieferungsbestätigung" Margin="10,10,10,10" Height="250"> | ||||
|                             <Grid> | ||||
|                                 <Grid.ColumnDefinitions> | ||||
|                                     <ColumnDefinition Width="*"/> | ||||
|                                 </Grid.ColumnDefinitions> | ||||
|  | ||||
|                                 <TextBox x:Name="TextElementDeliveryConfirmation" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" | ||||
|                                          HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,10,10" Height="Auto" | ||||
|                                          TextChanged="TextBox_TextChanged"/> | ||||
|                             </Grid> | ||||
|                         </GroupBox> | ||||
|                     </StackPanel> | ||||
|                 </ScrollViewer> | ||||
|             </TabItem> | ||||
|   | ||||
| @@ -208,6 +208,7 @@ namespace Elwig.Windows { | ||||
|                 case 2: ModeDeliveryNoteShort.IsChecked = true; break; | ||||
|                 case 3: ModeDeliveryNoteFull.IsChecked = true; break; | ||||
|             } | ||||
|             TextElementDeliveryConfirmation.Text = p.TextDeliveryConfirmation; | ||||
|  | ||||
|             FinishInputFilling(); | ||||
|         } | ||||
| @@ -232,6 +233,7 @@ namespace Elwig.Windows { | ||||
|  | ||||
|             p.TextDeliveryNote = TextElementDeliveryNote.Text.Length > 0 ? TextElementDeliveryNote.Text : null; | ||||
|             p.ModeDeliveryNoteStats = (ModeDeliveryNoteNone.IsChecked == true) ? 0 : (ModeDeliveryNoteGaOnly.IsChecked == true) ? 1 : (ModeDeliveryNoteShort.IsChecked == true) ? 2 : (ModeDeliveryNoteFull.IsChecked == true) ? 3 : 2; | ||||
|             p.TextDeliveryConfirmation = TextElementDeliveryConfirmation.Text.Length > 0 ? TextElementDeliveryConfirmation.Text : null; | ||||
|  | ||||
|             await p.UpdateValues(); | ||||
|         } | ||||
|   | ||||
| @@ -32,12 +32,16 @@ | ||||
|             </Grid.ColumnDefinitions> | ||||
|             <Image Source="/elwig.png" RenderOptions.BitmapScalingMode="HighQuality" Grid.Column="0" | ||||
|                    HorizontalAlignment="Left" Margin="5,5,5,5" VerticalAlignment="Top"/> | ||||
|             <Label Grid.Column="1" Content="Elwig" FontSize="32" | ||||
|                    HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top"/> | ||||
|             <Label Grid.Column="1" Content="Elektonische Winzer-" | ||||
|                    HorizontalAlignment="Left" Margin="0,55,0,0" VerticalAlignment="Top"/> | ||||
|             <Label Grid.Column="1" Content="genossenschaftsverwaltung" | ||||
|                    HorizontalAlignment="Left" Margin="0,70,0,0" VerticalAlignment="Top"/> | ||||
|             <TextBlock Grid.Column="1" FontSize="32" HorizontalAlignment="Left" Margin="0,5,0,0" VerticalAlignment="Top"> | ||||
|                 Elwig | ||||
|             </TextBlock> | ||||
|             <TextBlock Grid.Column="1" HorizontalAlignment="Left" Margin="0,50,0,0" VerticalAlignment="Top" LineHeight="14" LineStackingStrategy="BlockLineHeight"> | ||||
|                 Elektonische Winzer-<LineBreak/> | ||||
|                 genossenschaftsverwaltung | ||||
|             </TextBlock> | ||||
|             <TextBlock x:Name="VersionField" Grid.Column="1" FontSize="10" HorizontalAlignment="Left" Margin="0,80,0,0" VerticalAlignment="Top"> | ||||
|                 Version: ? | ||||
|             </TextBlock> | ||||
|         </Grid> | ||||
|  | ||||
|         <Button x:Name="MemberAdminButton" Content="Mitglieder" Click="MemberAdminButton_Click" | ||||
|   | ||||
| @@ -1,3 +1,4 @@ | ||||
| using System.Reflection; | ||||
| using System.Windows; | ||||
|  | ||||
| namespace Elwig.Windows { | ||||
| @@ -5,6 +6,8 @@ namespace Elwig.Windows { | ||||
|  | ||||
|         public MainWindow() { | ||||
|             InitializeComponent(); | ||||
|             var v = Assembly.GetExecutingAssembly().GetName().Version; | ||||
|             VersionField.Text = "Version: " + (v == null ? "?" : $"{v.Major}.{v.Minor}.{v.Build}"); | ||||
|             if (!App.Config.Debug) { | ||||
|                 TestWindowButton.Visibility = Visibility.Hidden; | ||||
|                 //QueryWindowButton.Visibility = Visibility.Hidden; | ||||
|   | ||||
| @@ -59,6 +59,8 @@ | ||||
|                               Click="Menu_Print_Letterheads_MgNr_Click"/> | ||||
|                     <MenuItem x:Name="Menu_Print_Letterheads_Name" Header="nach Name sortiert" IsEnabled="False" Tag="Print" | ||||
|                               Click="Menu_Print_Letterheads_Name_Click"/> | ||||
|                     <MenuItem x:Name="Menu_Print_Letterheads_Plz" Header="nach PLZ, Ort, Name sortiert" IsEnabled="False" Tag="Print" | ||||
|                               Click="Menu_Print_Letterheads_Plz_Click"/> | ||||
|                 </MenuItem> | ||||
|             </MenuItem> | ||||
|             <MenuItem Header="Rundschreiben"> | ||||
|   | ||||
| @@ -291,7 +291,7 @@ namespace Elwig.Windows { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private async void Menu_Print_Letterheads_MgNr_Click(object sender, RoutedEventArgs evt) { | ||||
|         private async Task PrintLetterheads(int ordering) { | ||||
|             var n = await Context.Members.CountAsync(m => m.IsActive); | ||||
|             var res = MessageBox.Show( | ||||
|                 $"Sollen wirklich {n} Seiten gedruckt werden?", "Ausdruck Bestätigen", | ||||
| @@ -299,10 +299,25 @@ namespace Elwig.Windows { | ||||
|             if (res != MessageBoxResult.Yes) | ||||
|                 return; | ||||
|             Mouse.OverrideCursor = Cursors.AppStarting; | ||||
|             using var doc = await Document.Merge(Context.Members | ||||
|                 .Where(m => m.IsActive && m.ContactViaPost) | ||||
|                 .OrderBy(m => m.MgNr) | ||||
|                 .Select(m => new Letterhead(m))); | ||||
|             var members = Context.Members.Where(m => m.IsActive && m.ContactViaPost); | ||||
|             switch (ordering) { | ||||
|                 case 0: members = members | ||||
|                         .OrderBy(m => m.MgNr); | ||||
|                     break; | ||||
|                 case 1: members = members | ||||
|                         .OrderBy(m => m.FamilyName) | ||||
|                         .ThenBy(m => m.GivenName) | ||||
|                         .ThenBy(m => m.MgNr); | ||||
|                     break; | ||||
|                 case 2: members = members | ||||
|                         .OrderBy(m => m.PostalDest.AtPlz.Plz) | ||||
|                         .ThenBy(m => m.PostalDest.AtPlz.Ort.Name) | ||||
|                         .ThenBy(m => m.FamilyName) | ||||
|                         .ThenBy(m => m.GivenName) | ||||
|                         .ThenBy(m => m.MgNr); | ||||
|                     break; | ||||
|             } | ||||
|             using var doc = await Document.Merge((await members.ToListAsync()).Select(m => new Letterhead(m))); | ||||
|             await doc.Generate(); | ||||
|             Mouse.OverrideCursor = null; | ||||
|             if (App.Config.Debug) { | ||||
| @@ -312,26 +327,16 @@ namespace Elwig.Windows { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private async void Menu_Print_Letterheads_MgNr_Click(object sender, RoutedEventArgs evt) { | ||||
|             await PrintLetterheads(0); | ||||
|         } | ||||
|  | ||||
|         private async void Menu_Print_Letterheads_Name_Click(object sender, RoutedEventArgs evt) { | ||||
|             var n = await Context.Members.CountAsync(m => m.IsActive); | ||||
|             var res = MessageBox.Show( | ||||
|                 $"Sollen wirklich {n} Seiten gedruckt werden?", "Ausdruck Bestätigen", | ||||
|                 MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); | ||||
|             if (res != MessageBoxResult.Yes) | ||||
|                 return; | ||||
|             Mouse.OverrideCursor = Cursors.AppStarting; | ||||
|             using var doc = await Document.Merge(Context.Members | ||||
|                 .Where(m => m.IsActive && m.ContactViaPost) | ||||
|                 .OrderBy(m => m.FamilyName) | ||||
|                 .ThenBy(m => m.GivenName) | ||||
|                 .Select(m => new Letterhead(m))); | ||||
|             await doc.Generate(); | ||||
|             Mouse.OverrideCursor = null; | ||||
|             if (App.Config.Debug) { | ||||
|                 doc.Show(); | ||||
|             } else { | ||||
|                 await doc.Print(); | ||||
|             } | ||||
|             await PrintLetterheads(1); | ||||
|         } | ||||
|  | ||||
|         private async void Menu_Print_Letterheads_Plz_Click(object sender, RoutedEventArgs evt) { | ||||
|             await PrintLetterheads(2); | ||||
|         } | ||||
|  | ||||
|         private void FocusSearchInput(object sender, RoutedEventArgs evt) { | ||||
|   | ||||
| @@ -24,22 +24,27 @@ | ||||
|                             Margin="110,40,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" | ||||
|                             ValueChanged="SeasonInput_ValueChanged"/> | ||||
|  | ||||
|         <Button x:Name="CalculateBinsButton" | ||||
|         <Button x:Name="CalculateBinsButton" Content="Aufteilung Berechnen" | ||||
|                 Click="CalculateBinsButton_Click" | ||||
|                 Margin="50,80,0,0" FontSize="12" Height="40"> | ||||
|             <TextBlock TextAlignment="Center">Lieferungen auf Flächen-<LineBreak/>bindungen aufteilen</TextBlock> | ||||
|         </Button> | ||||
|                 Margin="50,80,0,0"/> | ||||
|         <CheckBox x:Name="AllowAttrIntoLowerBinsInput" Content="Erlauben Lieferungen auch auf (konfigurierte) "schlechtere" Flächenbindungen aufzuteilen" IsChecked="True" | ||||
|                   VerticalAlignment="Top" HorizontalAlignment="Left" Margin="255,68,0,0"/> | ||||
|         <CheckBox x:Name="AvoidUnderDeliveriesInput" Content="Unterlieferungen durch Abzug bei "besseren" Flächenbindungen vermeiden" IsEnabled="False" | ||||
|                   VerticalAlignment="Top" HorizontalAlignment="Left" Margin="255,88,0,0"/> | ||||
|         <CheckBox x:Name="HonorGebundenInput" Margin="255,108,0,0" VerticalAlignment="Top"> | ||||
|             <TextBlock>Bei Lieferungen das Feld <Italic>Gebunden</Italic> berücksichtigen</TextBlock> | ||||
|         </CheckBox> | ||||
|  | ||||
|         <Button x:Name="DeliveryConfirmationButton" Content="Anlieferungsbestätigungen" | ||||
|                 Click="DeliveryConfirmationButton_Click" | ||||
|                 Margin="50,130,0,0"/> | ||||
|                 Margin="50,122,0,0"/> | ||||
|  | ||||
|         <Button x:Name="OverUnderDeliveryButton" Content="Über-/Unterlieferungen" | ||||
|                 Click="OverUnderDeliveryButton_Click" | ||||
|                 Margin="50,172,0,0"/> | ||||
|                 Margin="50,164,0,0"/> | ||||
|  | ||||
|         <Button x:Name="PaymentButton" Content="Auszahlung" | ||||
|                 Click="PaymentButton_Click" | ||||
|                 Margin="50,214,0,0"/> | ||||
|                 Margin="50,206,0,0"/> | ||||
|     </Grid> | ||||
| </local:ContextWindow> | ||||
|   | ||||
| @@ -1,8 +1,6 @@ | ||||
| using Elwig.Documents; | ||||
| using Elwig.Dialogs; | ||||
| using Elwig.Helpers; | ||||
| using Elwig.Helpers.Billing; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using System.Windows; | ||||
| using System.Windows.Input; | ||||
| @@ -18,13 +16,15 @@ namespace Elwig.Windows { | ||||
|         } | ||||
|  | ||||
|         protected override async Task OnRenewContext() { | ||||
|  | ||||
|             SeasonInput_ValueChanged(null, null); | ||||
|         } | ||||
|  | ||||
|         private async void SeasonInput_ValueChanged(object sender, RoutedEventArgs evt) { | ||||
|             var s = await Context.Seasons.FindAsync(SeasonInput.Value); | ||||
|             var valid = (s != null); | ||||
|             CalculateBinsButton.IsEnabled = valid; | ||||
|         private async void SeasonInput_ValueChanged(object? sender, RoutedEventArgs? evt) { | ||||
|             var s0 = await Context.Seasons.FindAsync(SeasonInput.Value); | ||||
|             var s1 = await Context.Seasons.FindAsync(SeasonInput.Value + 1); | ||||
|             var valid = (s0 != null); | ||||
|             var last = (s1 == null); | ||||
|             CalculateBinsButton.IsEnabled = valid && last; | ||||
|             DeliveryConfirmationButton.IsEnabled = valid; | ||||
|             OverUnderDeliveryButton.IsEnabled = valid; | ||||
|         } | ||||
| @@ -32,39 +32,23 @@ namespace Elwig.Windows { | ||||
|         private async void CalculateBinsButton_Click(object sender, RoutedEventArgs evt) { | ||||
|             if (SeasonInput.Value is not int year) | ||||
|                 return; | ||||
|             CalculateBinsButton.IsEnabled = false; | ||||
|             Mouse.OverrideCursor = Cursors.AppStarting; | ||||
|             var b = new Billing(year); | ||||
|             await b.FinishSeason(); | ||||
|             await b.CalculateBins(); | ||||
|             await b.CalculateBins( | ||||
|                 AllowAttrIntoLowerBinsInput.IsChecked ?? false, | ||||
|                 AvoidUnderDeliveriesInput.IsChecked ?? false, | ||||
|                 HonorGebundenInput.IsChecked ?? false); | ||||
|             Mouse.OverrideCursor = null; | ||||
|             CalculateBinsButton.IsEnabled = true; | ||||
|         } | ||||
|  | ||||
|         private async void DeliveryConfirmationButton_Click(object sender, RoutedEventArgs evt) { | ||||
|         private void DeliveryConfirmationButton_Click(object sender, RoutedEventArgs evt) { | ||||
|             if (SeasonInput.Value is not int year) | ||||
|                 return; | ||||
|             var res = MessageBox.Show( | ||||
|                 $"Sollen wirklich alle Bestätigungen gedruckt werden?", "Ausdruck Bestätigen", | ||||
|                 MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); | ||||
|             if (res != MessageBoxResult.Yes) | ||||
|                 return; | ||||
|             Mouse.OverrideCursor = Cursors.AppStarting; | ||||
|             using var doc = await Document.Merge(Context.Members.FromSqlRaw($""" | ||||
|                 SELECT m.* | ||||
|                 FROM member m | ||||
|                     JOIN delivery d ON d.mgnr = m.mgnr | ||||
|                 WHERE m.active AND d.year = {year} | ||||
|                 GROUP BY m.mgnr | ||||
|                 ORDER BY m.mgnr | ||||
|                 """) | ||||
|                 .ToList() | ||||
|                 .Select(m => new DeliveryConfirmation(Context, year, m))); | ||||
|             await doc.Generate(); | ||||
|             Mouse.OverrideCursor = null; | ||||
|             if (App.Config.Debug) { | ||||
|                 doc.Show(); | ||||
|             } else { | ||||
|                 await doc.Print(); | ||||
|             } | ||||
|             var d = new DeliveryConfirmationsDialog(year); | ||||
|             d.Show(); | ||||
|         } | ||||
|  | ||||
|         private void OverUnderDeliveryButton_Click(object sender, RoutedEventArgs evt) { | ||||
|   | ||||
| @@ -23,8 +23,5 @@ | ||||
|                 Margin="260,190,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/> | ||||
|         <Button x:Name="PdfCreditButton" Content="Gutschrift Erzeugen" Click="PdfCreditButton_Click" Tag="Print" IsEnabled="False" | ||||
|                 Margin="260,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/> | ||||
|  | ||||
|         <Button x:Name="CalcBinsButton" Content="Berechnen" Click="CalcBinsButton_Click" | ||||
|                 Margin="20,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/> | ||||
|     </Grid> | ||||
| </Window> | ||||
|   | ||||
| @@ -68,13 +68,5 @@ namespace Elwig.Windows { | ||||
|             doc.Show(); | ||||
|             Mouse.OverrideCursor = null; | ||||
|         } | ||||
|  | ||||
|         private async void CalcBinsButton_Click(object sender, RoutedEventArgs evt) { | ||||
|             Mouse.OverrideCursor = Cursors.AppStarting; | ||||
|             var b = new Billing(2022); | ||||
|             await b.FinishSeason(); | ||||
|             await b.CalculateBins(); | ||||
|             Mouse.OverrideCursor = null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user