From e5c462b43f4d966c4a2459eb11e611ab26e15d7f Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 29 Feb 2024 10:48:48 +0100 Subject: [PATCH] [#15] MailWindow: Add Rundschreiben-Funktion --- Elwig/App.xaml.cs | 4 + Elwig/Documents/Document.cs | 42 ++- Elwig/Helpers/ActionCommand.cs | 22 ++ Elwig/Helpers/ClientParameters.cs | 9 + Elwig/Models/Entities/Member.cs | 5 +- Elwig/Models/Entities/WbKg.cs | 3 + Elwig/Windows/MailWindow.xaml | 259 ++++++++++++++ Elwig/Windows/MailWindow.xaml.cs | 557 ++++++++++++++++++++++++++++++ Elwig/Windows/MainWindow.xaml | 37 +- Elwig/Windows/MainWindow.xaml.cs | 34 +- 10 files changed, 922 insertions(+), 50 deletions(-) create mode 100644 Elwig/Helpers/ActionCommand.cs create mode 100644 Elwig/Windows/MailWindow.xaml create mode 100644 Elwig/Windows/MailWindow.xaml.cs diff --git a/Elwig/App.xaml.cs b/Elwig/App.xaml.cs index 335c40b..a42af11 100644 --- a/Elwig/App.xaml.cs +++ b/Elwig/App.xaml.cs @@ -254,5 +254,9 @@ namespace Elwig { w.FocusMember(mgnr); return w; } + + public static MailWindow FocusMailWindow() { + return FocusWindow(() => new()); + } } } diff --git a/Elwig/Documents/Document.cs b/Elwig/Documents/Document.cs index 6295a9c..12359bb 100644 --- a/Elwig/Documents/Document.cs +++ b/Elwig/Documents/Document.cs @@ -11,9 +11,11 @@ namespace Elwig.Documents { public static string Name => "Dokument"; - private static readonly double GenerationProportion = 0.125; + protected static readonly double GenerationProportion = 0.125; - private TempFile? _pdfFile = null; + protected TempFile? _pdfFile = null; + protected string? _pdfPath; + protected string? PdfPath => _pdfPath ?? _pdfFile?.FilePath; public bool ShowFoldMarks = App.Config.Debug; public bool DoubleSided = false; @@ -59,6 +61,10 @@ namespace Elwig.Documents { return new MergedDocument(docs); } + public static Document FromPdf(string path) { + return new PdfDocument(path); + } + private async Task Render() { string name; if (this is BusinessLetter) { @@ -87,20 +93,28 @@ namespace Elwig.Documents { public async Task Generate(IProgress? progress = null) { progress?.Report(0.0); - if (this is MergedDocument m) { + if (this is PdfDocument) { + // nothing to do + } else if (this is MergedDocument m) { var pdf = new TempFile("pdf"); var tmpHtmls = new List(); + var tmpFiles = new List(); var n = m.Documents.Count(); int i = 0; foreach (var doc in m.Documents) { + if (doc is PdfDocument) { + tmpFiles.Add(doc.PdfPath!); + continue; + } var tmpHtml = new TempFile("html"); await File.WriteAllTextAsync(tmpHtml.FilePath, await doc.Render(), Utils.UTF8); tmpHtmls.Add(tmpHtml); + tmpFiles.Add(tmpHtml.FileName); i++; progress?.Report(GenerationProportion * 100 * i / n); } progress?.Report(GenerationProportion * 100); - await Pdf.Convert(tmpHtmls.Select(f => f.FileName), pdf.FileName, DoubleSided, new Progress(v => progress?.Report(GenerationProportion * 100 + v * (1 - GenerationProportion)))); + await Pdf.Convert(tmpFiles, pdf.FileName, DoubleSided, new Progress(v => progress?.Report(GenerationProportion * 100 + v * (1 - GenerationProportion)))); foreach (var tmp in tmpHtmls) { tmp.Dispose(); } @@ -118,13 +132,13 @@ namespace Elwig.Documents { } public void SaveTo(string pdfPath) { - if (_pdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet"); - File.Copy(_pdfFile.FilePath, pdfPath); + if (PdfPath == null) throw new InvalidOperationException("Pdf file has not been generated yet"); + File.Copy(PdfPath, pdfPath); } public async Task Print(int copies = 1) { - if (_pdfFile == null) throw new InvalidOperationException("Pdf file has not been generated yet"); - await Pdf.Print(_pdfFile.FilePath, copies); + if (PdfPath == null) throw new InvalidOperationException("Pdf file has not been generated yet"); + await Pdf.Print(PdfPath, copies); } public void Show() { @@ -132,10 +146,14 @@ namespace Elwig.Documents { Pdf.Show(_pdfFile.NewReference(), Title + (this is BusinessDocument b ? $" - {b.Member.Name}" : "")); } - private class MergedDocument : Document { - public IEnumerable Documents; - public MergedDocument(IEnumerable docs) : base("Mehrere Dokumente") { - Documents = docs; + private class MergedDocument(IEnumerable docs) : Document("Mehrere Dokumente") { + public IEnumerable Documents = docs; + } + + private class PdfDocument : Document { + public PdfDocument(string pdfPath) : + base(Path.GetFileNameWithoutExtension(pdfPath)) { + _pdfPath = pdfPath; } } } diff --git a/Elwig/Helpers/ActionCommand.cs b/Elwig/Helpers/ActionCommand.cs new file mode 100644 index 0000000..d2058a5 --- /dev/null +++ b/Elwig/Helpers/ActionCommand.cs @@ -0,0 +1,22 @@ +using System; +using System.Windows.Input; + +namespace Elwig.Helpers { + public class ActionCommand : ICommand { + + public event EventHandler CanExecuteChanged; + private readonly Action Action; + + public ActionCommand(Action action) { + Action = action; + } + + public void Execute(object parameter) { + Action(); + } + + public bool CanExecute(object parameter) { + return true; + } + } +} diff --git a/Elwig/Helpers/ClientParameters.cs b/Elwig/Helpers/ClientParameters.cs index 91757fd..b6b3dc0 100644 --- a/Elwig/Helpers/ClientParameters.cs +++ b/Elwig/Helpers/ClientParameters.cs @@ -61,6 +61,8 @@ namespace Elwig.Helpers { public string? TextDeliveryNote; public string? TextDeliveryConfirmation; public string? TextCreditNote; + public string? TextEmailSubject; + public string? TextEmailBody; public ClientParameters(AppDbContext ctx) : this(ctx.ClientParameters.ToDictionary(e => e.Param, e => e.Value)) { } @@ -108,6 +110,10 @@ namespace Elwig.Helpers { if (TextDeliveryConfirmation == "") TextDeliveryConfirmation = null; TextCreditNote = parameters.GetValueOrDefault("TEXT_CREDITNOTE"); if (TextCreditNote == "") TextCreditNote = null; + TextEmailSubject = parameters.GetValueOrDefault("TEXT_EMAIL_SUBJECT"); + if (TextEmailSubject == "") TextEmailSubject = null; + TextEmailBody = parameters.GetValueOrDefault("TEXT_EMAIL_BODY"); + if (TextEmailBody == "") TextEmailBody = null; } catch { throw new KeyNotFoundException(); } @@ -143,6 +149,8 @@ namespace Elwig.Helpers { ("TEXT_DELIVERYNOTE", TextDeliveryNote), ("TEXT_DELIVERYCONFIRMATION", TextDeliveryConfirmation), ("TEXT_CREDITNOTE", TextCreditNote), + ("TEXT_EMAIL_SUBJECT", TextEmailSubject), + ("TEXT_EMAIL_BODY", TextEmailBody) ]; } @@ -163,6 +171,7 @@ namespace Elwig.Helpers { } await cmd.ExecuteNonQueryAsync(); + await App.HintContextChange(); } } } diff --git a/Elwig/Models/Entities/Member.cs b/Elwig/Models/Entities/Member.cs index 6c728f6..d3f6087 100644 --- a/Elwig/Models/Entities/Member.cs +++ b/Elwig/Models/Entities/Member.cs @@ -145,7 +145,10 @@ namespace Elwig.Models.Entities { public virtual PostalDest PostalDest { get; private set; } [ForeignKey("DefaultKgNr")] - public virtual AT_Kg? DefaultKg { get; private set; } + public virtual WbKg? DefaultWbKg { get; private set; } + + [NotMapped] + public AT_Kg? DefaultKg => DefaultWbKg?.AtKg; [ForeignKey("ZwstId")] public virtual Branch? Branch { get; private set; } diff --git a/Elwig/Models/Entities/WbKg.cs b/Elwig/Models/Entities/WbKg.cs index 9cfe6dd..d960b1c 100644 --- a/Elwig/Models/Entities/WbKg.cs +++ b/Elwig/Models/Entities/WbKg.cs @@ -20,6 +20,9 @@ namespace Elwig.Models.Entities { [InverseProperty("Kg")] public virtual ISet Rds { get; private set; } + [InverseProperty("DefaultWbKg")] + public virtual ISet Members { get; private set; } + [NotMapped] public WbGem Gem => AtKg.Gem.WbGem; diff --git a/Elwig/Windows/MailWindow.xaml b/Elwig/Windows/MailWindow.xaml new file mode 100644 index 0000000..8335e27 --- /dev/null +++ b/Elwig/Windows/MailWindow.xaml @@ -0,0 +1,259 @@ + + + + + + + + + + + + + + + + + + + +