MainWindow: Add WeightBreakdownButton

This commit is contained in:
2024-04-30 23:34:31 +02:00
parent 8bf8362480
commit 2a4e8d69d0
8 changed files with 119 additions and 3 deletions

View File

@ -64,6 +64,7 @@ namespace Elwig.Helpers {
public DbSet<MemberDeliveryPerVariantRowSingle> MemberDeliveryPerVariantRows { get; private set; } public DbSet<MemberDeliveryPerVariantRowSingle> MemberDeliveryPerVariantRows { get; private set; }
public DbSet<CreditNoteDeliveryRowSingle> CreditNoteDeliveryRows { get; private set; } public DbSet<CreditNoteDeliveryRowSingle> CreditNoteDeliveryRows { get; private set; }
public DbSet<CreditNoteRowSingle> CreditNoteRows { get; private set; } public DbSet<CreditNoteRowSingle> CreditNoteRows { get; private set; }
public DbSet<WeightBreakdownRow> WeightBreakDownRows { get; private set; }
private readonly StreamWriter? LogFile = null; private readonly StreamWriter? LogFile = null;
public static DateTime LastWriteTime => File.GetLastWriteTime(App.Config.DatabaseFile); public static DateTime LastWriteTime => File.GetLastWriteTime(App.Config.DatabaseFile);

View File

@ -9,7 +9,7 @@ namespace Elwig.Helpers {
public static class AppDbUpdater { public static class AppDbUpdater {
// Don't forget to update value in Tests/fetch-resources.bat! // Don't forget to update value in Tests/fetch-resources.bat!
public static readonly int RequiredSchemaVersion = 18; public static readonly int RequiredSchemaVersion = 19;
private static int VersionOffset = 0; private static int VersionOffset = 0;

View File

@ -305,6 +305,7 @@ namespace Elwig.Helpers.Export {
case "°KMW": n = 1; data = $"{v:N1}"; break; case "°KMW": n = 1; data = $"{v:N1}"; break;
case "°Oe": n = 0; data = $"{v:N0}"; break; case "°Oe": n = 0; data = $"{v:N0}"; break;
case "m²": n = 0; data = $"{v:N0}"; break; case "m²": n = 0; data = $"{v:N0}"; break;
case "kg": n = 0; data = $"{v:N0}"; break;
} }
if (n >= 0) add = string.Join(' ', add.Split(' ').Select(p => p.StartsWith("table:style-name=") ? $"table:style-name=\"N{n}\"" : p)); if (n >= 0) add = string.Join(' ', add.Split(' ').Select(p => p.StartsWith("table:style-name=") ? $"table:style-name=\"N{n}\"" : p));
} }

View File

@ -0,0 +1,58 @@
using Elwig.Models.Entities;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;
namespace Elwig.Models.Dtos {
public class WeightBreakdownData : DataTable<WeightBreakdownRow> {
private static readonly (string, string, string?, int?)[] FieldNames = [
("Type", "R/W", null, 10),
("SortId", "Sorte", null, 10),
("AttrId", "Attr.", null, 10),
("CultId", "Bewirt.", null, 15),
("QualId", "Qual.", null, 15),
("Geb", "gebunden", null, 20),
("Weight", "Gewicht", "kg", 20),
];
public WeightBreakdownData(IEnumerable<WeightBreakdownRow> rows, int year, string name) :
base(name, $"Sorten-/Qualitätsaufschlüsselung {year}", name, rows, FieldNames) {
}
public static async Task<WeightBreakdownData> ForSeason(DbSet<WeightBreakdownRow> table, int year, Branch? branch = null) {
return new(await FromDbSet(table, year, branch?.ZwstId), year, branch?.Name ?? "Gesamt");
}
private static async Task<IEnumerable<WeightBreakdownRow>> FromDbSet(DbSet<WeightBreakdownRow> table, int year, string? zwstid) {
zwstid = zwstid == null ? "NULL" : $"'{zwstid}'";
return await table.FromSqlRaw($"""
SELECT type, sortid, attrid, cultid, v.qualid, geb, SUM(weight) AS weight
FROM v_stat_total v
LEFT JOIN wine_quality_level q ON q.qualid = v.qualid
WHERE year = {year} AND ({zwstid} IS NULL OR zwstid = {zwstid})
GROUP BY type, sortid, attrid, cultid, v.qualid, geb
ORDER BY type DESC, sortid, attrid, cultid, q.min_kmw, geb
""").ToListAsync();
}
}
[Keyless]
public class WeightBreakdownRow {
[Column("type")]
public required string Type { get; set; }
[Column("sortid")]
public required string SortId { get; set; }
[Column("attrid")]
public string? AttrId { get; set; }
[Column("cultid")]
public string? CultId { get; set; }
[Column("qualid")]
public required string QualId { get; set; }
[Column("geb")]
public required string Geb { get; set; }
[Column("weight")]
public int Weight { get; set; }
}
}

View File

@ -0,0 +1,17 @@
-- schema version 18 to 19
CREATE VIEW v_stat_total AS
SELECT d.year, d.zwstid, v.type, v.sortid,
IIF(b.discr = a.attrid OR NOT a.area_com, a.attrid, NULL) AS attrid,
p.cultid, q.qualid,
IIF(b.discr = '_', 'ungeb', 'geb') AS geb,
SUM(value) AS weight
FROM delivery_part p
LEFT JOIN delivery d ON (d.year, d.did) = (p.year, p.did)
LEFT JOIN wine_variety v ON v.sortid = p.sortid
LEFT JOIN wine_quality_level q ON q.qualid = p.qualid
LEFT JOIN delivery_part_bucket b ON (b.year, b.did, b.dpnr) = (p.year, p.did, p.dpnr)
LEFT JOIN v_wine_attribute a ON a.attrid = p.attrid
GROUP BY d.year, d.zwstid, v.type, v.sortid, IIF(b.discr = a.attrid OR NOT a.area_com, a.attrid, NULL), p.cultid, q.qualid, geb
HAVING SUM(value) > 0
ORDER BY d.year, d.zwstid, v.type DESC, v.sortid, IIF(b.discr = a.attrid OR NOT a.area_com, a.attrid, NULL), p.cultid, q.min_kmw, geb;

View File

@ -90,6 +90,10 @@
<Button x:Name="AutoBusinessSharesButton" Content="Autom. GA nachzeichnen" <Button x:Name="AutoBusinessSharesButton" Content="Autom. GA nachzeichnen"
Click="AutoBusinessSharesButton_Click" IsEnabled="False" Click="AutoBusinessSharesButton_Click" IsEnabled="False"
Margin="200,90,0,10" Width="190"/> Margin="200,90,0,10" Width="190"/>
<Button x:Name="BreakdownButton" Content="Sorten-/Qual.aufteilung"
Click="BreakdownButton_Click"
Margin="0,130,200,10" Width="190"/>
</Grid> </Grid>
</Expander> </Expander>
</Grid> </Grid>

View File

@ -2,6 +2,7 @@ using Elwig.Helpers;
using Elwig.Helpers.Billing; using Elwig.Helpers.Billing;
using Elwig.Helpers.Export; using Elwig.Helpers.Export;
using Elwig.Models.Dtos; using Elwig.Models.Dtos;
using Microsoft.EntityFrameworkCore;
using Microsoft.Win32; using Microsoft.Win32;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -151,7 +152,7 @@ namespace Elwig.Windows {
} }
private void SeasonFinish_Expanded(object sender, RoutedEventArgs evt) { private void SeasonFinish_Expanded(object sender, RoutedEventArgs evt) {
Height = 530; Height = 570;
} }
private void SeasonFinish_Collapsed(object sender, RoutedEventArgs evt) { private void SeasonFinish_Collapsed(object sender, RoutedEventArgs evt) {
@ -166,6 +167,7 @@ namespace Elwig.Windows {
OverUnderDeliveryButton.IsEnabled = valid; OverUnderDeliveryButton.IsEnabled = valid;
AutoBusinessSharesButton.IsEnabled = valid && false; AutoBusinessSharesButton.IsEnabled = valid && false;
PaymentButton.IsEnabled = valid; PaymentButton.IsEnabled = valid;
BreakdownButton.IsEnabled = valid;
} }
private void DeliveryConfirmationButton_Click(object sender, RoutedEventArgs evt) { private void DeliveryConfirmationButton_Click(object sender, RoutedEventArgs evt) {
@ -234,5 +236,38 @@ namespace Elwig.Windows {
return; return;
App.FocusPaymentVariants(year); App.FocusPaymentVariants(year);
} }
private async void BreakdownButton_Click(object sender, RoutedEventArgs evt) {
if (SeasonInput.Value is not int year)
return;
var d = new SaveFileDialog() {
FileName = $"Aufschlüsselung-{year}.ods",
DefaultExt = "ods",
Filter = "OpenDocument Format Spreadsheet (*.ods)|*.ods",
Title = $"Sorten-/Qualitätsaufschlüsselung {year} speichern unter - Elwig"
};
if (d.ShowDialog() == false)
return;
Mouse.OverrideCursor = Cursors.AppStarting;
try {
var b = new Billing(year);
await b.FinishSeason();
await b.CalculateBuckets();
await App.HintContextChange();
using var ctx = new AppDbContext();
using var ods = new OdsFile(d.FileName);
var tblTotal = await WeightBreakdownData.ForSeason(ctx.WeightBreakDownRows, year);
await ods.AddTable(tblTotal);
foreach (var branch in await ctx.Branches.OrderBy(b => b.Name).ToListAsync()) {
var tbl = await WeightBreakdownData.ForSeason(ctx.WeightBreakDownRows, year, branch);
await ods.AddTable(tbl);
}
} catch (Exception exc) {
MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
Mouse.OverrideCursor = null;
}
} }
} }

View File

@ -1 +1 @@
curl -s -L "https://www.necronda.net/elwig/files/create.sql?v=18" -u "elwig:ganzGeheim123!" -o "Resources\Sql\Create.sql" curl -s -L "https://www.necronda.net/elwig/files/create.sql?v=19" -u "elwig:ganzGeheim123!" -o "Resources\Sql\Create.sql"