diff --git a/Elwig/Helpers/AppDbContext.cs b/Elwig/Helpers/AppDbContext.cs index 2d590b0..f2a2e8e 100644 --- a/Elwig/Helpers/AppDbContext.cs +++ b/Elwig/Helpers/AppDbContext.cs @@ -64,6 +64,7 @@ namespace Elwig.Helpers { public DbSet MemberDeliveryPerVariantRows { get; private set; } public DbSet CreditNoteDeliveryRows { get; private set; } public DbSet CreditNoteRows { get; private set; } + public DbSet WeightBreakDownRows { get; private set; } private readonly StreamWriter? LogFile = null; public static DateTime LastWriteTime => File.GetLastWriteTime(App.Config.DatabaseFile); diff --git a/Elwig/Helpers/AppDbUpdater.cs b/Elwig/Helpers/AppDbUpdater.cs index f1a3b1f..e7fa099 100644 --- a/Elwig/Helpers/AppDbUpdater.cs +++ b/Elwig/Helpers/AppDbUpdater.cs @@ -9,7 +9,7 @@ namespace Elwig.Helpers { public static class AppDbUpdater { // 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; diff --git a/Elwig/Helpers/Export/Ods.cs b/Elwig/Helpers/Export/Ods.cs index eb39416..f1d83f7 100644 --- a/Elwig/Helpers/Export/Ods.cs +++ b/Elwig/Helpers/Export/Ods.cs @@ -305,6 +305,7 @@ namespace Elwig.Helpers.Export { case "°KMW": n = 1; data = $"{v:N1}"; break; case "°Oe": 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)); } diff --git a/Elwig/Models/Dtos/WeightBreakdownData.cs b/Elwig/Models/Dtos/WeightBreakdownData.cs new file mode 100644 index 0000000..04772ee --- /dev/null +++ b/Elwig/Models/Dtos/WeightBreakdownData.cs @@ -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 { + + 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 rows, int year, string name) : + base(name, $"Sorten-/Qualitätsaufschlüsselung {year}", name, rows, FieldNames) { + } + + public static async Task ForSeason(DbSet table, int year, Branch? branch = null) { + return new(await FromDbSet(table, year, branch?.ZwstId), year, branch?.Name ?? "Gesamt"); + } + + private static async Task> FromDbSet(DbSet 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; } + } +} diff --git a/Elwig/Resources/Sql/18-19.sql b/Elwig/Resources/Sql/18-19.sql new file mode 100644 index 0000000..9a5cd89 --- /dev/null +++ b/Elwig/Resources/Sql/18-19.sql @@ -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; diff --git a/Elwig/Windows/MainWindow.xaml b/Elwig/Windows/MainWindow.xaml index 5a61d74..4d0af8e 100644 --- a/Elwig/Windows/MainWindow.xaml +++ b/Elwig/Windows/MainWindow.xaml @@ -90,6 +90,10 @@