diff --git a/Tasks/PdfBuilder.cs b/Tasks/PdfBuilder.cs index bdfdd29..7dd4083 100644 --- a/Tasks/PdfBuilder.cs +++ b/Tasks/PdfBuilder.cs @@ -1409,6 +1409,7 @@ public class PdfBuilder public List<(int, string, string, string, int)> GroupAddresses(int setID) { int grpcount = Settings._instance.pdfExport.rsNumGrouped; // Amount of addresses per group + int grptol = Settings._instance.pdfExport.rsGrupTol; // Amount of addresses per group int stpoint = 2; // group starting point (first n characters of the plz) KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID); if (list == null) @@ -1427,12 +1428,20 @@ public class PdfBuilder { string start = group.Key; int fraktion = 0; + int addressCount = group.Count(); + int remainder = addressCount % grpcount; + int groupCount = (addressCount - 1) / grpcount + 1; + bool mergeLastGroup = groupCount > 1 && remainder > 0 && remainder <= grptol; + int effectiveGroupCount = groupCount - (mergeLastGroup ? 1 : 0); - for (int count = 0; count < group.Count(); count += grpcount) + for (int groupIndex = 0; groupIndex < effectiveGroupCount; groupIndex++) { fraktion++; - int currentGroupSize = Math.Min(grpcount, group.Count() - count); + int count = groupIndex * grpcount; + int currentGroupSize = mergeLastGroup && groupIndex == effectiveGroupCount - 1 + ? addressCount - count + : Math.Min(grpcount, addressCount - count); string first = group.ElementAt(count).used_plz; string last = group.ElementAt(count + currentGroupSize - 1).used_plz; @@ -1447,6 +1456,7 @@ public class PdfBuilder public List<(int, string, string, string, int)> GroupAddressesInternational(int setID) { int grpcount = Settings._instance.pdfExport.rsNumGrouped; // Amount of addresses per group + int grptol = Settings._instance.pdfExport.rsGrupTol; // Amount of addresses per group KasAddressList list = Settings._instance.addressSets.GetAddressSetByID(setID); if (list == null) throw new Exception("AddressSet nicht gefunden"); @@ -1463,12 +1473,20 @@ public class PdfBuilder { string start = group.Key; int fraktion = 0; + int addressCount = group.Count(); + int remainder = addressCount % grpcount; + int groupCount = (addressCount - 1) / grpcount + 1; + bool mergeLastGroup = groupCount > 1 && remainder > 0 && remainder <= grptol; + int effectiveGroupCount = groupCount - (mergeLastGroup ? 1 : 0); - for (int count = 0; count < group.Count(); count += grpcount) + for (int groupIndex = 0; groupIndex < effectiveGroupCount; groupIndex++) { fraktion++; - int currentGroupSize = Math.Min(grpcount, group.Count() - count); + int count = groupIndex * grpcount; + int currentGroupSize = mergeLastGroup && groupIndex == effectiveGroupCount - 1 + ? addressCount - count + : Math.Min(grpcount, addressCount - count); string first = group.ElementAt(count).used_plz; string last = group.ElementAt(count + currentGroupSize - 1).used_plz;