diff --git a/DataStore/DataStructures.cs b/DataStore/DataStructures.cs index c2b5dad..ad5bef0 100644 --- a/DataStore/DataStructures.cs +++ b/DataStore/DataStructures.cs @@ -178,6 +178,23 @@ public class KasPerson public string abteilung { get; set; } public string funktionad { get; set; } + public string used_plz { get; set; } = ""; + + public static void SetUsedPLZ(int id, string plz) + { + foreach (var set in Settings._instance.addressSets.addresses) + { + foreach (var add in set.KasPersons) + { + if (add.id == id) + { + add.used_plz = plz; + return; + } + } + } + } + public static int GenerateNewID(int base_id) { //var newid = 100000 + base_id; diff --git a/DataStore/Settings.cs b/DataStore/Settings.cs index 7c52e8c..1421cc9 100644 --- a/DataStore/Settings.cs +++ b/DataStore/Settings.cs @@ -67,6 +67,9 @@ public class PdfExportSettings public double fontSize { get; set; } = 9; public double smallFontSize { get; set; } = 6; + public bool exportRunningSheets { get; set; } = true; + public int rsNumGrouped { get; set; } = 25; + public int rsPlzStartpoint { get; set; } = 2; } public class Global @@ -169,6 +172,19 @@ public class Customer public char separator { get; set; } = ','; public int ID { get; } + public static Customer GetCustomerByID(int id) + { + foreach (var customer in Settings._instance.customers.customers) + { + if (id == customer.ID) + { + return customer; + } + } + + return null; + } + // public static int GetIDByCustomerListItem(string item_content) // { diff --git a/MainWindow.axaml b/MainWindow.axaml index 66ad7be..c7c2c9c 100644 --- a/MainWindow.axaml +++ b/MainWindow.axaml @@ -254,6 +254,7 @@ + @@ -273,6 +274,10 @@ + + + + @@ -292,6 +297,10 @@ + + + + diff --git a/MainWindow.axaml.cs b/MainWindow.axaml.cs index 213838a..8b65ab0 100644 --- a/MainWindow.axaml.cs +++ b/MainWindow.axaml.cs @@ -772,8 +772,9 @@ public partial class MainWindow : Window list.Add((KasAddressList)item); try { - StartCombine(list, Convert.ToInt32(LstCustomers.SelectedItem), "difference", GetCombiningTyp()); - }catch (Exception ex) + StartCombine(list, Convert.ToInt32((LstCustomers.SelectedItem as Customer).ID), "difference", GetCombiningTyp()); + } + catch (Exception ex) { Logger.Log($"Error while combining (difference): {ex.Message}", Logger.LogType.Error); } @@ -802,7 +803,7 @@ public partial class MainWindow : Window list.Add((KasAddressList)item); try { - StartCombine(list, Convert.ToInt32(LstCustomers.SelectedItem.ToString().Split(" - ")[0]), "intersection", GetCombiningTyp()); + StartCombine(list, Convert.ToInt32((LstCustomers.SelectedItem as Customer).ID), "intersection", GetCombiningTyp()); } catch (Exception ex) { @@ -829,7 +830,7 @@ public partial class MainWindow : Window foreach (var item in LstCustomerAdressSets.SelectedItems) list.Add((KasAddressList)item); - StartCombine(list, Convert.ToInt32(LstCustomers.SelectedItem.ToString().Split(" - ")[0]), "symdiff", GetCombiningTyp()); + StartCombine(list, Convert.ToInt32((LstCustomers.SelectedItem as Customer).ID), "symdiff", GetCombiningTyp()); } private async void BtnGenerateLabels_OnClick(object? sender, RoutedEventArgs e) diff --git a/Tasks/AddressCreation.cs b/Tasks/AddressCreation.cs index 47e33e3..668433c 100644 --- a/Tasks/AddressCreation.cs +++ b/Tasks/AddressCreation.cs @@ -43,166 +43,162 @@ public static class AddressCreator public static string? CreateFinalMarkdownString(int id) { // Maximum seven lines of information - try + + // find the address + KasPerson? address = null; + var string_address = ""; + var address_line_count = 0; + foreach (var set in Settings._instance.addressSets.addresses) { - // find the address - KasPerson? address = null; - var string_address = ""; - var address_line_count = 0; - foreach (var set in Settings._instance.addressSets.addresses) - { - var temp = set.KasPersons.FirstOrDefault(obj => obj.id == id); - if (temp != null) - { - address = temp; - break; - } - } - - // no address found - if (address == null) return null; - - // let's get started: we start from the bottom - // if the country is not Germany, set it; try to map via Global countries alternatives -> translation - var trimmedLand = (address.land ?? "").Trim(); - var trimmedLowerLand = trimmedLand.ToLower(); - var isGermany = trimmedLowerLand == "germany" || trimmedLowerLand == "ger" || - trimmedLowerLand == "" || trimmedLowerLand == "de" || - trimmedLowerLand == "deutschland"; - - if (!isGermany) - { - var countryToShow = trimmedLand; // default: use raw land value - - if (!string.IsNullOrEmpty(trimmedLand)) - // search for matching country via alternatives using for-loops - for (var ci = 0; ci < Global._instance.countries.Count; ci++) - { - var country = Global._instance.countries[ci]; - for (var ai = 0; ai < country.alternatives.Count; ai++) - try - { - var alt = (country.alternatives[ai] ?? "").Trim(); - if (string.Equals(alt, trimmedLand, StringComparison.OrdinalIgnoreCase)) - { - countryToShow = string.IsNullOrWhiteSpace(country.translation) - ? country.name - : country.translation; - goto CountryFound; - } - } - catch - { - // ignore malformed alternative - } - } - - CountryFound: - string_address = "**" + countryToShow + "**"; // Needs to be bold - address_line_count++; - } - - // Alternative A: pplz valid and city existing - if (!string.IsNullOrEmpty(address.ort) && CheckPLZ(address.pplz, address.land)) - { - string_address = address.pplz + " " + address.ort + "\n" + string_address; - address_line_count++; - if (!string.IsNullOrWhiteSpace(address.postfach)) - { - string_address = "Postfach " + address.postfach.Trim() + "\n" + string_address; - address_line_count++; - } - else if (!string.IsNullOrWhiteSpace(address.strasse.Trim())) - { - string_address = address.strasse.Trim() + "\n" + string_address; - address_line_count++; - } - - var nameline = CreateNameLine(address.anredzus, address.anrede, address.titel, address.vorname, - address.adel, address.name, address.namezus); - - if (!string.IsNullOrWhiteSpace(nameline)) - { - string_address = nameline + "\n" + string_address; - address_line_count++; - } - - // REIHENFOLGE - var nameattribs = new[] - { address.name1, address.name2, address.name3, address.name4, address.name5, address.abteilung }; - - var names = ""; - for (var i = 0; i < nameattribs.Length; i++) - try - { - if (address_line_count >= 7) break; - if (!string.IsNullOrWhiteSpace(nameattribs[i])) - { - names += "\n" + nameattribs[i]; - address_line_count++; - } - } - catch - { - Console.WriteLine("ERROR 15821"); - } - - string_address = names + "\n" + string_address; - } // Alternative B: plz valid and city existing - else if (!string.IsNullOrEmpty(address.ort) && CheckPLZ(address.plz, address.land)) - { - string_address = address.plz + " " + address.ort + "\n" + string_address; - address_line_count++; - if (!string.IsNullOrWhiteSpace(address.strasse)) - { - string_address = address.strasse.Trim() + "\n" + string_address; - address_line_count++; - } - else if (!string.IsNullOrWhiteSpace(address.postfach.Trim())) - { - string_address = "Postfach " + address.postfach.Trim() + "\n" + string_address; - address_line_count++; - } - - var nameline = CreateNameLine(address.anredzus, address.anrede, address.titel, address.vorname, - address.adel, address.name, address.namezus); - - if (!string.IsNullOrWhiteSpace(nameline)) - { - string_address = nameline + "\n" + string_address; - address_line_count++; - } - - var nameattribs = new[] - { address.name1, address.name2, address.name3, address.name4, address.name5, address.abteilung }; - - var names = ""; - for (var i = 0; i < nameattribs.Length; i++) - try - { - if (address_line_count >= 7) break; - if (!string.IsNullOrWhiteSpace(nameattribs[i])) - { - names += "\n" + nameattribs[i]; - address_line_count++; - } - } - catch - { - Console.WriteLine("ERROR 15821"); - } - - string_address = names + "\n" + string_address; - } // Error-Handling? - - if (address_line_count > 1) return string_address; - return null; - } - catch (Exception ex) - { - Logger.Log($"Error while creating markdown string: {ex.Message}", Logger.LogType.Error); + var temp = set.KasPersons.FirstOrDefault(obj => obj.id == id); + if (temp != null) + { + address = temp; + break; + } } + // no address found + if (address == null) return null; + + // let's get started: we start from the bottom + // if the country is not Germany, set it; try to map via Global countries alternatives -> translation + var trimmedLand = (address.land ?? "").Trim(); + var trimmedLowerLand = trimmedLand.ToLower(); + var isGermany = trimmedLowerLand == "germany" || trimmedLowerLand == "ger" || + trimmedLowerLand == "" || trimmedLowerLand == "de" || + trimmedLowerLand == "deutschland"; + + if (!isGermany) + { + var countryToShow = trimmedLand; // default: use raw land value + + if (!string.IsNullOrEmpty(trimmedLand)) + // search for matching country via alternatives using for-loops + for (var ci = 0; ci < Global._instance.countries.Count; ci++) + { + var country = Global._instance.countries[ci]; + for (var ai = 0; ai < country.alternatives.Count; ai++) + try + { + var alt = (country.alternatives[ai] ?? "").Trim(); + if (string.Equals(alt, trimmedLand, StringComparison.OrdinalIgnoreCase)) + { + countryToShow = string.IsNullOrWhiteSpace(country.translation) + ? country.name + : country.translation; + goto CountryFound; + } + } + catch + { + // ignore malformed alternative + } + } + + CountryFound: + string_address = "**" + countryToShow + "**"; // Needs to be bold + address_line_count++; + } + + // Alternative A: pplz valid and city existing + if (!string.IsNullOrEmpty(address.ort) && CheckPLZ(address.pplz, address.land)) + { + string pplz = NormalizeGermanPLZ(address.pplz); + KasPerson.SetUsedPLZ(id, pplz); + string_address = pplz + " " + address.ort + "\n" + string_address; + address_line_count++; + if (!string.IsNullOrWhiteSpace(address.postfach)) + { + string_address = "Postfach " + address.postfach.Trim() + "\n" + string_address; + address_line_count++; + } + else if (!string.IsNullOrWhiteSpace(address.strasse.Trim())) + { + string_address = address.strasse.Trim() + "\n" + string_address; + address_line_count++; + } + + var nameline = CreateNameLine(address.anredzus, address.anrede, address.titel, address.vorname, + address.adel, address.name, address.namezus); + + if (!string.IsNullOrWhiteSpace(nameline)) + { + string_address = nameline + "\n" + string_address; + address_line_count++; + } + + // REIHENFOLGE + var nameattribs = new[] + { address.name1, address.name2, address.name3, address.name4, address.name5, address.abteilung }; + + var names = ""; + for (var i = 0; i < nameattribs.Length; i++) + try + { + if (address_line_count >= 7) break; + if (!string.IsNullOrWhiteSpace(nameattribs[i])) + { + names += "\n" + nameattribs[i]; + address_line_count++; + } + } + catch + { + Console.WriteLine("ERROR 15821"); + } + + string_address = names + "\n" + string_address; + } // Alternative B: plz valid and city existing + else if (!string.IsNullOrEmpty(address.ort) && CheckPLZ(address.plz, address.land)) + { + string plz = NormalizeGermanPLZ(address.plz); + KasPerson.SetUsedPLZ(id, plz); + string_address = plz + " " + address.ort + "\n" + string_address; + address_line_count++; + if (!string.IsNullOrWhiteSpace(address.strasse)) + { + string_address = address.strasse.Trim() + "\n" + string_address; + address_line_count++; + } + else if (!string.IsNullOrWhiteSpace(address.postfach.Trim())) + { + string_address = "Postfach " + address.postfach.Trim() + "\n" + string_address; + address_line_count++; + } + + var nameline = CreateNameLine(address.anredzus, address.anrede, address.titel, address.vorname, + address.adel, address.name, address.namezus); + + if (!string.IsNullOrWhiteSpace(nameline)) + { + string_address = nameline + "\n" + string_address; + address_line_count++; + } + + var nameattribs = new[] + { address.name1, address.name2, address.name3, address.name4, address.name5, address.abteilung }; + + var names = ""; + for (var i = 0; i < nameattribs.Length; i++) + try + { + if (address_line_count >= 7) break; + if (!string.IsNullOrWhiteSpace(nameattribs[i])) + { + names += "\n" + nameattribs[i]; + address_line_count++; + } + } + catch + { + Console.WriteLine("ERROR 15821"); + } + + string_address = names + "\n" + string_address; + } // Error-Handling? + + if (address_line_count > 1) return string_address; return null; } @@ -212,12 +208,12 @@ public static class AddressCreator try { if (!string.IsNullOrWhiteSpace(anredezus)) - return string.Join(" ", - new[] { anredezus, titel, vorname, adel, name } - .Where(s => !string.IsNullOrWhiteSpace(s)) - ) - + (string.IsNullOrWhiteSpace(namezus) ? "" : $" ({namezus.Trim()})"); - + return string.Join(" ", + new[] { anredezus, titel, vorname, adel, name } + .Where(s => !string.IsNullOrWhiteSpace(s)) + ) + + (string.IsNullOrWhiteSpace(namezus) ? "" : $" ({namezus.Trim()})"); + // else return string.Join(" ", new[] { anrede, titel, vorname, adel, name } @@ -231,7 +227,6 @@ public static class AddressCreator } return null; - } /// @@ -277,4 +272,23 @@ public static class AddressCreator return false; } + + public static string NormalizeGermanPLZ(string plz) + { + if(plz.Length == 5) return plz; + if(plz.Length > 5) + { + return plz.Substring(0, 5); + } + + if (plz.Length < 5) + { + int toadd = 5 - plz.Length; + for (int i = 0; i < toadd; i++) + { + plz = "0" + plz; + } + } + return plz; + } } \ No newline at end of file diff --git a/Tasks/PdfBuilder.cs b/Tasks/PdfBuilder.cs index f7d19ae..31017bf 100644 --- a/Tasks/PdfBuilder.cs +++ b/Tasks/PdfBuilder.cs @@ -134,6 +134,17 @@ public class PdfBuilder addresses.Add(addr); } + if (addresses.Count == 0) + { + MessageBox.Show(MainWindow._instance, "Keine validen Adressen konnten generiert werden. Abbruch.", "Fehler"); + return; + } + CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath); + + if (_settings.exportRunningSheets) + { + ExportRunningSheets(addressSetId, outputPath); + } CreateAddressLabelPdfWithPlaceholder(addresses, placeholderText, outputPath); } catch (Exception ex) @@ -480,4 +491,142 @@ public class PdfBuilder if (left < 0 || top < 0 || right < 0 || bottom < 0) throw new ArgumentException("Margins cannot be negative"); } + + public void ExportRunningSheets(int setID, string path) + { + if (path.EndsWith(".pdf")) + { + path = path.Substring(0, path.Length - 4); + path = path + "-Laufzettel.pdf"; + } + else + { + path = path + "-Laufzettel.pdf"; + } + + CreateRunningSheets(setID, path); + + } + + public void CreateRunningSheets(int setID, string path) + { + KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID); + var document = new PdfDocument(); + document.Info.Title = $"Laufzettel für {list.Name}"; + document.Info.Subject = "powered by logofclient"; + document.Info.Author = "logofclient"; + + int margin = 50; + + var grouped_nums = GroupAddresses(setID); + + foreach (var result in grouped_nums) + { + var page = document.AddPage(); + page.Size = PageSize.A4; + + var gfx = XGraphics.FromPdfPage(page); + + var width = page.Width.Point-margin; + var height = page.Height.Point-margin; + gfx.DrawLine(XPens.Black, margin, margin, margin, height); + gfx.DrawLine(XPens.Black, margin, margin, width, margin); + gfx.DrawLine(XPens.Black, width, margin, width, height); + gfx.DrawLine(XPens.Black, margin, height, width, height); + + var boldfont = new XFont("Cantarell", 11, XFontStyleEx.Bold); + var font = new XFont("Cantarell", 11, XFontStyleEx.Regular); + var bigboldfont = new XFont("Cantarell", 35, XFontStyleEx.Bold); + + // Versandinfo + gfx.DrawString($"Versand {list.Name}", boldfont, XBrushes.Black, + new XRect(margin+5, margin, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"Start: ", font, XBrushes.Black, + new XRect(margin+5, margin+25, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"{result.Item3}", font, XBrushes.Black, + new XRect(margin+75, margin+25, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"Ende: ", font, XBrushes.Black, + new XRect(margin+5, margin+40, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"{result.Item4}", font, XBrushes.Black, + new XRect(margin+75, margin+40, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"Kunde: ", font, XBrushes.Black, + new XRect(margin+5, margin+55, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"{Customer.GetCustomerByID(list.owner_id).name}", font, XBrushes.Black, + new XRect(margin+75, margin+55, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"Absender: ", font, XBrushes.Black, + new XRect(margin+5, margin+70, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"{Customer.GetCustomerByID(list.owner_id).sender_address}", font, XBrushes.Black, + new XRect(margin+75, margin+70, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"Anzahl: ", font, XBrushes.Black, + new XRect(margin+5, margin+85, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"{result.Item5}", font, XBrushes.Black, + new XRect(margin+75, margin+85, width-margin, 25), XStringFormats.CenterLeft); + + // logofclient ad + gfx.DrawString($"powered by logofclient", font, XBrushes.Black, + new XRect(margin+5, height-55, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"(c) 2026 MyPapertown", font, XBrushes.Black, + new XRect(margin+5, height-40, width-margin, 25), XStringFormats.CenterLeft); + gfx.DrawString($"mypapercloud.de/logof", font, XBrushes.Black, + new XRect(margin+5, height-25, width-margin, 25), XStringFormats.CenterLeft); + + + int total_frac = 0; + foreach (var item in grouped_nums) + { + if (item.Item2 == result.Item2) total_frac++; + } + + // group number + gfx.DrawString($"{result.Item2}", bigboldfont, XBrushes.Black, + new XRect(margin, margin, width-margin, (height-margin)/2), XStringFormats.Center); + gfx.DrawString($"Fraktion {result.Item1}/{total_frac}", font, XBrushes.Black, + new XRect(margin, margin, width-margin, (height-margin)/2 + 50), XStringFormats.Center); + } + + document.Save(path); + } + + /// + /// Calculates address groups to summarize for the single pages of the running sheets. + /// + /// + /// List of quadruples consisting of a number and the starting of the plz, as well as first, last plz and total amount of addresses + public List<(int, string, string, string, int)> GroupAddresses(int setID) + { + int grpcount = Settings._instance.pdfExport.rsNumGrouped; // Amount of addresses per group + int stpoint = Settings._instance.pdfExport.rsPlzStartpoint; // group starting point (first n characters of the plz) + KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID); + if (list == null) + throw new Exception("AddressSet nicht gefunden"); + + List<(int, string, string, string, int)> output = new(); + + List> sorted_list = list.KasPersons + .Where(x => !string.IsNullOrEmpty(x?.used_plz) && + x.used_plz.Length >= stpoint) + .OrderBy(x => x.used_plz) + .GroupBy(x => x.used_plz.Substring(0, stpoint)) + .ToList(); + + foreach (var group in sorted_list) + { + string start = group.Key; + int fraktion = 0; + + for (int count = 0; count < group.Count(); count += grpcount) + { + fraktion++; + + int currentGroupSize = Math.Min(grpcount, group.Count() - count); + + string first = group.ElementAt(count).used_plz; + string last = group.ElementAt(count + currentGroupSize - 1).used_plz; + + output.Add((fraktion, start, first, last, currentGroupSize)); + } + } + + return output; + } }