Services: Add Utils.RunForeground() to factor Task.Run and try-catch-Blocks out
Test / Run tests (push) Successful in 2m2s

This commit is contained in:
2026-06-30 02:27:57 +02:00
parent 69efca1cc3
commit beacba6bd9
6 changed files with 188 additions and 291 deletions
+17 -3
View File
@@ -31,6 +31,7 @@ using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input;
using System.Windows.Markup; using System.Windows.Markup;
namespace Elwig.Helpers { namespace Elwig.Helpers {
@@ -209,16 +210,29 @@ namespace Elwig.Helpers {
return Regex.Replace(iban.Trim(), ".{4}", "$0 ").Trim(); return Regex.Replace(iban.Trim(), ".{4}", "$0 ").Trim();
} }
public static void RunBackground(string title, Func<Task> a) { public static void RunBackground(string title, Func<Task> function) {
Task.Run(async () => { Task.Run(async () => {
try { try {
await a(); await function();
} catch (Exception exc) { } catch (Exception exc) {
InteractionService.ShowException(title, exc); InteractionService.ShowException(title, exc);
} }
}); });
} }
public static async Task RunForeground(Func<Task> function) {
var isSTA = Thread.CurrentThread.GetApartmentState() == ApartmentState.STA;
if (isSTA) Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
await function();
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
});
if (isSTA) Mouse.OverrideCursor = null;
}
public static void MailTo(string emailAddress) { public static void MailTo(string emailAddress) {
MailTo([emailAddress]); MailTo([emailAddress]);
} }
@@ -579,7 +593,7 @@ namespace Elwig.Helpers {
await doc.Generate(ctx); await doc.Generate(ctx);
} }
doc.SaveTo(filename); doc.SaveTo(filename);
Process.Start("explorer.exe", filename); if (!App.Config.Debug) Process.Start("explorer.exe", filename);
} }
} else { } else {
using (var ctx = new AppDbContext()) { using (var ctx = new AppDbContext()) {
+8 -21
View File
@@ -8,7 +8,6 @@ using Microsoft.EntityFrameworkCore;
using Elwig.Documents; using Elwig.Documents;
using Elwig.Helpers.Export; using Elwig.Helpers.Export;
using Elwig.Models.Dtos; using Elwig.Models.Dtos;
using System.Windows.Input;
using System.Windows; using System.Windows;
using System; using System;
using LinqKit; using LinqKit;
@@ -262,30 +261,18 @@ namespace Elwig.Services {
if (mode == ExportMode.SaveList) { if (mode == ExportMode.SaveList) {
var filename = InteractionService.SaveFile(DeliveryAncmtList.Name, DeliveryAncmtList.Name, "ods"); var filename = InteractionService.SaveFile(DeliveryAncmtList.Name, DeliveryAncmtList.Name, "ods");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await DeliveryAncmtListData.FromQuery(query, filterNames);
try { using var ods = new OdsFile(filename);
var data = await DeliveryAncmtListData.FromQuery(query, filterNames); await ods.AddTable(data);
using var ods = new OdsFile(filename);
await ods.AddTable(data);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} else { } else {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await DeliveryAncmtListData.FromQuery(query, filterNames);
try { using var doc = new DeliveryAncmtList(string.Join(" / ", filterNames), data);
var data = await DeliveryAncmtListData.FromQuery(query, filterNames); await Utils.ExportDocument(doc, mode);
using var doc = new DeliveryAncmtList(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
+58 -124
View File
@@ -6,7 +6,6 @@ using Elwig.Models.Entities;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows; using System.Windows;
using System; using System;
using Elwig.ViewModels; using Elwig.ViewModels;
@@ -726,16 +725,10 @@ namespace Elwig.Services {
} }
public static async Task GenerateDeliveryNote(int year, int did, ExportMode mode) { public static async Task GenerateDeliveryNote(int year, int did, ExportMode mode) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var doc = await DeliveryNote.Initialize(year, did);
try { await Utils.ExportDocument(doc, mode, doc.Delivery.LsNr, (doc.Member, $"{DeliveryNote.Name} Nr. {doc.Delivery.LsNr}", $"Im Anhang finden Sie den {DeliveryNote.Name} Nr. {doc.Delivery.LsNr}"));
using var doc = await DeliveryNote.Initialize(year, did);
await Utils.ExportDocument(doc, mode, doc.Delivery.LsNr, (doc.Member, $"{DeliveryNote.Name} Nr. {doc.Delivery.LsNr}", $"Im Anhang finden Sie den {DeliveryNote.Name} Nr. {doc.Delivery.LsNr}"));
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
public static async Task GenerateDeliveryJournal(this DeliveryAdminViewModel vm, ExportSubject subject, ExportMode mode) { public static async Task GenerateDeliveryJournal(this DeliveryAdminViewModel vm, ExportSubject subject, ExportMode mode) {
@@ -774,62 +767,42 @@ namespace Elwig.Services {
if (mode == ExportMode.SaveList) { if (mode == ExportMode.SaveList) {
var filename = InteractionService.SaveFile(DeliveryJournal.Name, DeliveryJournal.Name, "ods"); var filename = InteractionService.SaveFile(DeliveryJournal.Name, DeliveryJournal.Name, "ods");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await DeliveryJournalData.FromQuery(query, filterNames);
try { using var ods = new OdsFile(filename);
var data = await DeliveryJournalData.FromQuery(query, filterNames); await ods.AddTable(data);
using var ods = new OdsFile(filename);
await ods.AddTable(data);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} else if (mode == ExportMode.Export) { } else if (mode == ExportMode.Export) {
var filename = InteractionService.SaveFile(DeliveryJournal.Name, subject == ExportSubject.Selected ? $"Lieferung_{vm.SelectedDelivery?.LsNr}" : $"Lieferungen_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{App.ZwstId}", "elwig.zip"); var filename = InteractionService.SaveFile(DeliveryJournal.Name, subject == ExportSubject.Selected ? $"Lieferung_{vm.SelectedDelivery?.LsNr}" : $"Lieferungen_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{App.ZwstId}", "elwig.zip");
if (filename != null) { if (filename != null) {
if (!filename.EndsWith(".elwig.zip")) filename += ".elwig.zip"; if (!filename.EndsWith(".elwig.zip")) filename += ".elwig.zip";
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var list = await query
try { .Select(p => p.Delivery)
var list = await query .Distinct()
.Select(p => p.Delivery) .Include(d => d.Parts).ThenInclude(p => p.PartModifiers)
.Distinct() .ToListAsync();
.Include(d => d.Parts).ThenInclude(p => p.PartModifiers) var wbKgs = list
.ToListAsync(); .SelectMany(d => d.Parts)
var wbKgs = list .Where(p => p.Kg != null)
.SelectMany(d => d.Parts) .Select(p => p.Kg!)
.Where(p => p.Kg != null) .DistinctBy(k => k.KgNr)
.Select(p => p.Kg!) .OrderBy(k => k.KgNr)
.DistinctBy(k => k.KgNr) .ToList();
.OrderBy(k => k.KgNr) await ElwigData.Export(filename, list, wbKgs, filterNames);
.ToList();
await ElwigData.Export(filename, list, wbKgs, filterNames);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} else if (mode == ExportMode.Upload && App.Config.SyncUrl != null) { } else if (mode == ExportMode.Upload && App.Config.SyncUrl != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => {
await SyncService.Upload(App.Config.SyncUrl, App.Config.SyncUrl, App.Config.SyncPassword, query, filterNames); await SyncService.Upload(App.Config.SyncUrl, App.Config.SyncUrl, App.Config.SyncPassword, query, filterNames);
}); });
Mouse.OverrideCursor = null;
} else { } else {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await DeliveryJournalData.FromQuery(query, filterNames);
try { using var doc = new DeliveryJournal(string.Join(" / ", filterNames), data);
var data = await DeliveryJournalData.FromQuery(query, filterNames); await Utils.ExportDocument(doc, mode);
using var doc = new DeliveryJournal(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
@@ -850,17 +823,11 @@ namespace Elwig.Services {
throw new ArgumentException("Invalid value for ExportSubject"); throw new ArgumentException("Invalid value for ExportSubject");
} }
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await WineQualityStatisticsData.FromQuery(query, App.Client.OrderingMemberList);
try { using var doc = new WineQualityStatistics(string.Join(" / ", filterNames), data);
var data = await WineQualityStatisticsData.FromQuery(query, App.Client.OrderingMemberList); await Utils.ExportDocument(doc, mode);
using var doc = new WineQualityStatistics(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
public static async Task GenerateLocalityStatistics(this DeliveryAdminViewModel vm, ExportSubject subject) { public static async Task GenerateLocalityStatistics(this DeliveryAdminViewModel vm, ExportSubject subject) {
@@ -878,17 +845,11 @@ namespace Elwig.Services {
var filename = InteractionService.SaveFile("Lieferstatistik pro Ort", $"Lieferstatistik-{vm.FilterSeason ?? Utils.CurrentLastSeason}", "ods"); var filename = InteractionService.SaveFile("Lieferstatistik pro Ort", $"Lieferstatistik-{vm.FilterSeason ?? Utils.CurrentLastSeason}", "ods");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var ods = new OdsFile(filename);
try { var tbl = await WineLocalityStatisticsData.FromQuery(query, filterNames);
using var ods = new OdsFile(filename); await ods.AddTable(tbl);
var tbl = await WineLocalityStatisticsData.FromQuery(query, filterNames);
await ods.AddTable(tbl);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
@@ -921,38 +882,26 @@ namespace Elwig.Services {
if (mode == ExportMode.SaveList) { if (mode == ExportMode.SaveList) {
var filename = InteractionService.SaveFile(DeliveryDepreciationList.Name, $"{DeliveryDepreciationList.Name}-{vm.FilterSeason ?? Utils.CurrentLastSeason}", "ods"); var filename = InteractionService.SaveFile(DeliveryDepreciationList.Name, $"{DeliveryDepreciationList.Name}-{vm.FilterSeason ?? Utils.CurrentLastSeason}", "ods");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var ods = new OdsFile(filename);
try { var tblTotal = await DeliveryJournalData.FromQuery(query, filterNames);
using var ods = new OdsFile(filename); tblTotal.FullName = DeliveryDepreciationList.Name;
var tblTotal = await DeliveryJournalData.FromQuery(query, filterNames); tblTotal.Name = "Gesamt";
tblTotal.FullName = DeliveryDepreciationList.Name; await ods.AddTable(tblTotal);
tblTotal.Name = "Gesamt"; foreach (var branch in await ctx.FetchBranches().ToListAsync()) {
await ods.AddTable(tblTotal); var tbl = await DeliveryJournalData.FromQuery(query.Where(p => p.Delivery.ZwstId == branch.ZwstId), filterNames);
foreach (var branch in await ctx.FetchBranches().ToListAsync()) { tbl.FullName = DeliveryDepreciationList.Name;
var tbl = await DeliveryJournalData.FromQuery(query.Where(p => p.Delivery.ZwstId == branch.ZwstId), filterNames); tbl.Name = branch.Name;
tbl.FullName = DeliveryDepreciationList.Name; await ods.AddTable(tbl);
tbl.Name = branch.Name;
await ods.AddTable(tbl);
}
} catch (Exception exc) {
InteractionService.ShowException(exc);
} }
}); });
Mouse.OverrideCursor = null;
} }
} else { } else {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await DeliveryJournalData.FromQuery(query, filterNames);
try { using var doc = new DeliveryDepreciationList(string.Join(" / ", filterNames), data);
var data = await DeliveryJournalData.FromQuery(query, filterNames); await Utils.ExportDocument(doc, mode);
using var doc = new DeliveryDepreciationList(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
@@ -981,19 +930,13 @@ namespace Elwig.Services {
var filename = InteractionService.SaveFile("Liefermengen", "Liefermengen", "ods"); var filename = InteractionService.SaveFile("Liefermengen", "Liefermengen", "ods");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var ods = new OdsFile(filename);
try { var tblTotal = await MemberDeliveryData.FromQuery(query, filterNames);
using var ods = new OdsFile(filename); var tbl = await MemberDeliveryPerVarietyData.FromQuery(query, filterNames);
var tblTotal = await MemberDeliveryData.FromQuery(query, filterNames); await ods.AddTable(tblTotal);
var tbl = await MemberDeliveryPerVarietyData.FromQuery(query, filterNames); await ods.AddTable(tbl);
await ods.AddTable(tblTotal);
await ods.AddTable(tbl);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
@@ -1196,8 +1139,7 @@ namespace Elwig.Services {
$"Soll wirklich für {dids.Length:N0} Teillieferung(en) das Attribut\n'{attributeName}' gesetz werden?")) $"Soll wirklich für {dids.Length:N0} Teillieferung(en) das Attribut\n'{attributeName}' gesetz werden?"))
return; return;
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => {
using (var cnx = await AppDbContext.ConnectAsync()) { using (var cnx = await AppDbContext.ConnectAsync()) {
await cnx.ExecuteBatch($""" await cnx.ExecuteBatch($"""
UPDATE delivery_part SET attrid = {attrid} UPDATE delivery_part SET attrid = {attrid}
@@ -1208,8 +1150,6 @@ namespace Elwig.Services {
}); });
} catch (Exception exc) { } catch (Exception exc) {
InteractionService.ShowException(exc); InteractionService.ShowException(exc);
} finally {
Mouse.OverrideCursor = null;
} }
} }
@@ -1226,8 +1166,7 @@ namespace Elwig.Services {
$"Soll wirklich für {dids.Length:N0} Teillieferung(en) der Zu-/Abschlag\n'{modifierName}' hinzugefügt werden?")) $"Soll wirklich für {dids.Length:N0} Teillieferung(en) der Zu-/Abschlag\n'{modifierName}' hinzugefügt werden?"))
return; return;
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => {
using (var cnx = await AppDbContext.ConnectAsync()) { using (var cnx = await AppDbContext.ConnectAsync()) {
await cnx.ExecuteBatch($""" await cnx.ExecuteBatch($"""
INSERT INTO delivery_part_modifier (year, did, dpnr, modid) INSERT INTO delivery_part_modifier (year, did, dpnr, modid)
@@ -1239,8 +1178,6 @@ namespace Elwig.Services {
}); });
} catch (Exception exc) { } catch (Exception exc) {
InteractionService.ShowException(exc); InteractionService.ShowException(exc);
} finally {
Mouse.OverrideCursor = null;
} }
} }
@@ -1257,8 +1194,7 @@ namespace Elwig.Services {
$"Soll wirklich für {dids.Length:N0} Teillieferung(en) der Zu-/Abschlag\n'{modifierName}' entfernt werden?")) $"Soll wirklich für {dids.Length:N0} Teillieferung(en) der Zu-/Abschlag\n'{modifierName}' entfernt werden?"))
return; return;
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => {
using (var cnx = await AppDbContext.ConnectAsync()) { using (var cnx = await AppDbContext.ConnectAsync()) {
await cnx.ExecuteBatch($""" await cnx.ExecuteBatch($"""
DELETE FROM delivery_part_modifier DELETE FROM delivery_part_modifier
@@ -1269,8 +1205,6 @@ namespace Elwig.Services {
}); });
} catch (Exception exc) { } catch (Exception exc) {
InteractionService.ShowException(exc); InteractionService.ShowException(exc);
} finally {
Mouse.OverrideCursor = null;
} }
} }
} }
+40 -8
View File
@@ -7,6 +7,13 @@ namespace Elwig.Services {
public static class InteractionService { public static class InteractionService {
public static Func<string, string, string, string?>? Override; public static Func<string, string, string, string?>? Override;
public static Action<string, string>? NextInformation;
public static Action<string, string>? NextWarning;
public static Action<string, string>? NextError;
public static Func<string, string, bool>? NextContinue;
public static Func<string, string, bool>? NextConfirmation;
public static Func<string, string, bool>? NextQuestion;
public static Func<string, string, string?>? NextSave;
public static readonly Dictionary<string, string> ExtensionFilters = new() { public static readonly Dictionary<string, string> ExtensionFilters = new() {
["pdf"] = "PDF-Datei (*.pdf)|*.pdf", ["pdf"] = "PDF-Datei (*.pdf)|*.pdf",
@@ -19,7 +26,10 @@ namespace Elwig.Services {
}; };
public static void ShowInformation(string title, string text) { public static void ShowInformation(string title, string text) {
if (Override != null) { if (NextInformation != null) {
NextInformation(title, text);
NextInformation = null;
} else if (Override != null) {
Override("information", title, text); Override("information", title, text);
} else { } else {
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information); MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Information);
@@ -27,7 +37,11 @@ namespace Elwig.Services {
} }
public static bool AskContinue(string title, string text) { public static bool AskContinue(string title, string text) {
if (Override != null) { if (NextContinue != null) {
var r = NextContinue(title, text);
NextContinue = null;
return r;
} else if (Override != null) {
return Override("continue", title, text) != null; return Override("continue", title, text) != null;
} else { } else {
return MessageBox.Show(text, title, MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) == MessageBoxResult.OK; return MessageBox.Show(text, title, MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel) == MessageBoxResult.OK;
@@ -35,7 +49,11 @@ namespace Elwig.Services {
} }
public static bool AskConfirmation(string title, string text) { public static bool AskConfirmation(string title, string text) {
if (Override != null) { if (NextConfirmation != null) {
var r = NextConfirmation(title, text);
NextConfirmation = null;
return r;
} else if (Override != null) {
return Override("confirm", title, text) != null; return Override("confirm", title, text) != null;
} else { } else {
return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes; return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes;
@@ -43,7 +61,11 @@ namespace Elwig.Services {
} }
public static bool AskQuestion(string title, string text, bool defaultResult) { public static bool AskQuestion(string title, string text, bool defaultResult) {
if (Override != null) { if (NextQuestion != null) {
var r = NextQuestion(title, text);
NextQuestion = null;
return r;
} else if (Override != null) {
return Override("question", title, text) != null; return Override("question", title, text) != null;
} else { } else {
return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Question, defaultResult ? MessageBoxResult.Yes : MessageBoxResult.No) == MessageBoxResult.Yes; return MessageBox.Show(text, title, MessageBoxButton.YesNo, MessageBoxImage.Question, defaultResult ? MessageBoxResult.Yes : MessageBoxResult.No) == MessageBoxResult.Yes;
@@ -51,7 +73,10 @@ namespace Elwig.Services {
} }
public static void ShowWarning(string title, string text) { public static void ShowWarning(string title, string text) {
if (Override != null) { if (NextWarning != null) {
NextWarning(title, text);
NextWarning = null;
} else if (Override != null) {
Override("warning", title, text); Override("warning", title, text);
} else { } else {
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Warning); MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Warning);
@@ -59,7 +84,10 @@ namespace Elwig.Services {
} }
public static void ShowError(string title, string text) { public static void ShowError(string title, string text) {
if (Override != null) { if (NextError != null) {
NextError(title, text);
NextError = null;
} else if (Override != null) {
Override("error", title, text); Override("error", title, text);
} else { } else {
MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show(text, title, MessageBoxButton.OK, MessageBoxImage.Error);
@@ -71,7 +99,7 @@ namespace Elwig.Services {
} }
public static void ShowException(string title, Exception exc, bool showExcType = false, bool isError = true) { public static void ShowException(string title, Exception exc, bool showExcType = false, bool isError = true) {
ShowException(title, exc, showExcType, isError); ShowException(title, null, exc, showExcType, isError);
} }
public static void ShowException(string title, string? text, Exception exc, bool showExcType = false, bool isError = true) { public static void ShowException(string title, string? text, Exception exc, bool showExcType = false, bool isError = true) {
@@ -101,7 +129,11 @@ namespace Elwig.Services {
} }
public static string? SaveFile(string title, string defaultFileName, string extension) { public static string? SaveFile(string title, string defaultFileName, string extension) {
if (Override != null) { if (NextSave != null) {
var r = NextSave(title, $"{defaultFileName}.{extension}");
NextSave = null;
return r;
} else if (Override != null) {
return Override("save", title, $"{defaultFileName}.{extension}"); return Override("save", title, $"{defaultFileName}.{extension}");
} else { } else {
var d = new SaveFileDialog() { var d = new SaveFileDialog() {
+49 -94
View File
@@ -10,7 +10,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input;
namespace Elwig.Services { namespace Elwig.Services {
public static class MemberService { public static class MemberService {
@@ -390,47 +389,29 @@ namespace Elwig.Services {
} }
public static async Task GenerateMemberDataSheet(Member m, ExportMode mode) { public static async Task GenerateMemberDataSheet(Member m, ExportMode mode) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var doc = new MemberDataSheet(m);
try { await Utils.ExportDocument(doc, mode, emailData: (m, MemberDataSheet.Name, "Im Anhang finden Sie das aktuelle Stammdatenblatt"));
using var doc = new MemberDataSheet(m);
await Utils.ExportDocument(doc, mode, emailData: (m, MemberDataSheet.Name, "Im Anhang finden Sie das aktuelle Stammdatenblatt"));
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
public static async Task GenerateDeliveryConfirmation(Member m, int year, ExportMode mode) { public static async Task GenerateDeliveryConfirmation(Member m, int year, ExportMode mode) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var b = await Billing.Create(year);
try { await b.FinishSeason();
var b = await Billing.Create(year); await b.CalculateBuckets();
await b.FinishSeason(); App.HintContextChange();
await b.CalculateBuckets();
App.HintContextChange();
using var doc = new DeliveryConfirmation(year, m, null); using var doc = new DeliveryConfirmation(year, m, null);
await Utils.ExportDocument(doc, mode, emailData: (m, $"{DeliveryConfirmation.Name} {year}", $"Im Anhang finden Sie die Anlieferungsbestätigung {year}")); await Utils.ExportDocument(doc, mode, emailData: (m, $"{DeliveryConfirmation.Name} {year}", $"Im Anhang finden Sie die Anlieferungsbestätigung {year}"));
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
public static async Task GenerateCreditNote(Member m, int year, int avnr, ExportMode mode) { public static async Task GenerateCreditNote(Member m, int year, int avnr, ExportMode mode) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var doc = await CreditNote.Initialize(year, avnr, m.MgNr, null);
try { await Utils.ExportDocument(doc, mode, emailData: (m, $"{CreditNote.Name} {doc.Payment.Variant.Name}", $"Im Anhang finden Sie die Traubengutschrift {doc.Payment.Variant.Name}"));
using var doc = await CreditNote.Initialize(year, avnr, m.MgNr, null);
await Utils.ExportDocument(doc, mode, emailData: (m, $"{CreditNote.Name} {doc.Payment.Variant.Name}", $"Im Anhang finden Sie die Traubengutschrift {doc.Payment.Variant.Name}"));
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
public static async Task GenerateMemberList(this MemberAdminViewModel vm, ExportSubject subject, ExportMode mode) { public static async Task GenerateMemberList(this MemberAdminViewModel vm, ExportSubject subject, ExportMode mode) {
@@ -477,86 +458,60 @@ namespace Elwig.Services {
if (mode == ExportMode.SaveList) { if (mode == ExportMode.SaveList) {
var filename = InteractionService.SaveFile(MemberList.Name, MemberList.Name, "ods"); var filename = InteractionService.SaveFile(MemberList.Name, MemberList.Name, "ods");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await MemberListData.FromQuery(query, filterNames, filterNames.Where(f => f.StartsWith("Flächenbindung")).Select(f => f.Split(' ')[^1]));
try { using var ods = new OdsFile(filename);
var data = await MemberListData.FromQuery(query, filterNames, filterNames.Where(f => f.StartsWith("Flächenbindung")).Select(f => f.Split(' ')[^1])); await ods.AddTable(data);
using var ods = new OdsFile(filename);
await ods.AddTable(data);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} else if (mode == ExportMode.Vcf) { } else if (mode == ExportMode.Vcf) {
var filename = InteractionService.SaveFile("Kontakte", "Mitglieder", "vcf"); var filename = InteractionService.SaveFile("Kontakte", "Mitglieder", "vcf");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var members = await query
try { .OrderBy(m => m.MgNr)
var members = await query .Include(m => m.TelephoneNumbers)
.OrderBy(m => m.MgNr) .Include(m => m.EmailAddresses)
.Include(m => m.TelephoneNumbers) .ToListAsync();
.Include(m => m.EmailAddresses) using var exporter = new VCard(filename);
.ToListAsync(); await exporter.ExportAsync(members);
using var exporter = new VCard(filename);
await exporter.ExportAsync(members);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} else if (mode == ExportMode.Export) { } else if (mode == ExportMode.Export) {
var filename = InteractionService.SaveFile(MemberList.Name, subject == ExportSubject.Selected ? $"Mitglied_{vm.SelectedMember?.MgNr}" : $"Mitglieder_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{App.ZwstId}", "elwig.zip"); var filename = InteractionService.SaveFile(MemberList.Name, subject == ExportSubject.Selected ? $"Mitglied_{vm.SelectedMember?.MgNr}" : $"Mitglieder_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{App.ZwstId}", "elwig.zip");
if (filename != null) { if (filename != null) {
if (!filename.EndsWith(".elwig.zip")) filename += ".elwig.zip"; if (!filename.EndsWith(".elwig.zip")) filename += ".elwig.zip";
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var members = await query
try { .OrderBy(m => m.MgNr)
var members = await query .Include(m => m.TelephoneNumbers)
.OrderBy(m => m.MgNr) .Include(m => m.EmailAddresses)
.Include(m => m.TelephoneNumbers) .ToListAsync();
.Include(m => m.EmailAddresses) var areaComs = await query
.ToListAsync(); .SelectMany(m => m.AreaCommitments)
var areaComs = await query .Select(c => c.Contract).Distinct()
.SelectMany(m => m.AreaCommitments) .Include(c => c.Revisions)
.Select(c => c.Contract).Distinct() .ToListAsync();
.Include(c => c.Revisions) var wbKgs = members
.ToListAsync(); .Where(m => m.DefaultWbKg != null)
var wbKgs = members .Select(m => m.DefaultWbKg!)
.Where(m => m.DefaultWbKg != null) .Union(areaComs.Select(c => c.Kg))
.Select(m => m.DefaultWbKg!) .Distinct()
.Union(areaComs.Select(c => c.Kg)) .OrderBy(k => k.KgNr)
.Distinct() .ToList();
.OrderBy(k => k.KgNr) await ElwigData.Export(filename, members, areaComs, wbKgs, filterNames);
.ToList();
await ElwigData.Export(filename, members, areaComs, wbKgs, filterNames);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} else if (mode == ExportMode.Upload && App.Config.SyncUrl != null) { } else if (mode == ExportMode.Upload && App.Config.SyncUrl != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => {
await SyncService.Upload(App.Config.SyncUrl, App.Config.SyncUrl, App.Config.SyncPassword, query, filterNames); await SyncService.Upload(App.Config.SyncUrl, App.Config.SyncUrl, App.Config.SyncPassword, query, filterNames);
}); });
Mouse.OverrideCursor = null;
} else { } else {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { var data = await MemberListData.FromQuery(query, filterNames, filterNames.Where(f => f.StartsWith("Flächenbindung")).Select(f => f.Split(' ')[^1]));
try { using var doc = new MemberList(string.Join(" / ", filterNames), data);
var data = await MemberListData.FromQuery(query, filterNames, filterNames.Where(f => f.StartsWith("Flächenbindung")).Select(f => f.Split(' ')[^1])); await Utils.ExportDocument(doc, mode);
using var doc = new MemberList(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
+16 -41
View File
@@ -12,7 +12,6 @@ using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input;
namespace Elwig.Services { namespace Elwig.Services {
public static class PaymentVariantService { public static class PaymentVariantService {
@@ -192,29 +191,17 @@ namespace Elwig.Services {
var filename = InteractionService.SaveFile($"Variantendaten {v.Name}", $"Variantendaten-{v.Name.Trim().Replace(' ', '-')}", "ods"); var filename = InteractionService.SaveFile($"Variantendaten {v.Name}", $"Variantendaten-{v.Name.Trim().Replace(' ', '-')}", "ods");
if (filename == null) if (filename == null)
return; return;
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var ctx = new AppDbContext();
try { var data = await PaymentVariantSummaryData.ForPaymentVariant(v, ctx.PaymentVariantSummaryRows);
using var ctx = new AppDbContext(); using var ods = new OdsFile(filename);
var data = await PaymentVariantSummaryData.ForPaymentVariant(v, ctx.PaymentVariantSummaryRows); await ods.AddTable(data);
using var ods = new OdsFile(filename);
await ods.AddTable(data);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} else { } else {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var doc = await PaymentVariantSummary.Initialize(v.Year, v.AvNr);
try { await Utils.ExportDocument(doc, mode);
using var doc = await PaymentVariantSummary.Initialize(v.Year, v.AvNr);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
@@ -237,34 +224,22 @@ namespace Elwig.Services {
var filename = InteractionService.SaveFile("Überweisungsdaten", $"{App.Client.NameToken}-Überweisungsdaten-{v.Year}-{v.Name.Trim().Replace(' ', '-')}", Ebics.FileExtension); var filename = InteractionService.SaveFile("Überweisungsdaten", $"{App.Client.NameToken}-Überweisungsdaten-{v.Year}-{v.Name.Trim().Replace(' ', '-')}", Ebics.FileExtension);
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var e = new Ebics(v, filename, App.Client.ExportEbicsVersion, (Ebics.AddressMode)App.Client.ExportEbicsAddress);
try { await e.ExportAsync(Transaction.FromPaymentVariant(v));
using var e = new Ebics(v, filename, App.Client.ExportEbicsVersion, (Ebics.AddressMode)App.Client.ExportEbicsAddress);
await e.ExportAsync(Transaction.FromPaymentVariant(v));
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }
public static async Task GenerateAccountingList(int year, int avnr, string name) { public static async Task GenerateAccountingList(int year, int avnr, string name) {
var filename = InteractionService.SaveFile("Buchungsliste", $"{App.Client.NameToken}-Buchungsliste-{year}-{name.Trim().Replace(' ', '-')}", "ods"); var filename = InteractionService.SaveFile("Buchungsliste", $"{App.Client.NameToken}-Buchungsliste-{year}-{name.Trim().Replace(' ', '-')}", "ods");
if (filename != null) { if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait; await Utils.RunForeground(async () => {
await Task.Run(async () => { using var ctx = new AppDbContext();
try { var tbl = await CreditNoteData.ForPaymentVariant(ctx, year, avnr);
using var ctx = new AppDbContext(); using var ods = new OdsFile(filename);
var tbl = await CreditNoteData.ForPaymentVariant(ctx, year, avnr); await ods.AddTable(tbl);
using var ods = new OdsFile(filename);
await ods.AddTable(tbl);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
}); });
Mouse.OverrideCursor = null;
} }
} }