Gastansicht, Verbesserungen bei Planung

This commit is contained in:
concierge committed 2026-11-18 07:57:00 +01:00
1 parent b3afa3ef32
commit c08d43e7dd
38 files changed
+3845 -219

No files matched your search

+75 -8
View File
@@ -6,6 +6,7 @@ public struct ServicePlanner {
public var services: [ServiceOccurrence]
public var existingAssignments: [Assignment]
public var churches: [Church]
public var liturgicalDays: [LiturgicalDay]
public var options: PlannerOptions
public init(
@@ -14,6 +15,7 @@ public struct ServicePlanner {
services: [ServiceOccurrence],
existingAssignments: [Assignment] = [],
churches: [Church] = [],
liturgicalDays: [LiturgicalDay] = [],
options: PlannerOptions = PlannerOptions()
) {
self.people = people
@@ -21,6 +23,7 @@ public struct ServicePlanner {
self.services = services
self.existingAssignments = existingAssignments
self.churches = churches
self.liturgicalDays = liturgicalDays
self.options = options
}
@@ -29,6 +32,7 @@ public struct ServicePlanner {
let peopleByID = Dictionary(uniqueKeysWithValues: activePeople.map { ($0.id, $0) })
let absencesByPerson = Dictionary(grouping: absences, by: \.personID)
let churchNames = Dictionary(uniqueKeysWithValues: churches.map { ($0.id, $0.name) })
let highFeastDates = Set(liturgicalDays.filter(\.isHighFeast).map(\.dateKey))
var working: [Assignment]
if options.replaceExisting {
@@ -93,7 +97,9 @@ public struct ServicePlanner {
dayServicesByPerson: dayServicesByPerson,
churchByPerson: churchByPerson,
lastDateByPerson: lastDateByPerson,
assignedRolesByPerson: assignedRolesByPerson
assignedRolesByPerson: assignedRolesByPerson,
highFeast: highFeastDates.contains(service.dateKey),
enforcePreferences: true
)
switch result {
case .success(let person):
@@ -122,6 +128,58 @@ public struct ServicePlanner {
}
}
// Second pass, only on explicit request: use volunteers who agreed to be
// planned beyond their preferences for the slots that remained open.
if options.allowBeyondPreferences, !unfilled.isEmpty {
let flagged = activePeople.filter(\.allowBeyondPreferences)
if !flagged.isEmpty {
var remaining: [UnfilledSlot] = []
for slot in unfilled {
let service = slot.service
var assignedRolesByPerson: [String: Set<Role>] = [:]
for assignment in working where assignment.serviceID == service.id {
assignedRolesByPerson[assignment.personID, default: []].insert(assignment.role)
}
let result = bestCandidate(
for: service,
role: slot.role,
people: flagged,
peopleByID: peopleByID,
absencesByPerson: absencesByPerson,
totalByPerson: totalByPerson,
monthByPerson: monthByPerson,
dayServicesByPerson: dayServicesByPerson,
churchByPerson: churchByPerson,
lastDateByPerson: lastDateByPerson,
assignedRolesByPerson: assignedRolesByPerson,
highFeast: highFeastDates.contains(service.dateKey),
enforcePreferences: false
)
switch result {
case .success(let person):
let assignment = Assignment(
serviceID: service.id,
personID: person.id,
role: slot.role,
beyondPreferences: true
)
newAssignments.append(assignment)
working.append(assignment)
totalByPerson[person.id, default: 0] += 1
monthByPerson["\(person.id)|\(DayMath.monthKey(service.date))", default: 0] += 1
dayServicesByPerson["\(person.id)|\(service.dateKey)", default: []].insert(service.id)
churchByPerson[person.id, default: [:]][service.churchID, default: 0] += 1
lastDateByPerson[person.id] = service.date
case .failure(let reason):
var updated = slot
updated.reason = reason
remaining.append(updated)
}
}
unfilled = remaining
}
}
let filled = newAssignments.count
let message: String
if filled == 0 && unfilled.isEmpty {
@@ -156,7 +214,9 @@ public struct ServicePlanner {
dayServicesByPerson: [String: Set<String>],
churchByPerson: [String: [String: Int]],
lastDateByPerson: [String: Date],
assignedRolesByPerson: [String: Set<Role>]
assignedRolesByPerson: [String: Set<Role>],
highFeast: Bool,
enforcePreferences: Bool
) -> CandidateResult {
var blockedReasons: [String: Int] = [:]
var eligiblePeople: [Person] = []
@@ -171,15 +231,18 @@ public struct ServicePlanner {
blockedReasons["abwesend", default: 0] += 1
continue
}
if options.respectPreferredWeekdays,
!person.preferredWeekdays.isEmpty,
!person.preferredWeekdays.contains(DayMath.weekday(service.date)) {
blockedReasons["Einsatztag passt nicht", default: 0] += 1
if options.respectPreferredWeekdays, enforcePreferences,
!person.matchesWeekdayPreference(
weekday: DayMath.weekday(service.date),
timeMinutes: service.timeMinutes,
highFeast: highFeast
) {
blockedReasons["Einsatztag oder Uhrzeit passt nicht", default: 0] += 1
continue
}
let month = DayMath.monthKey(service.date)
let usedThisMonth = monthByPerson["\(person.id)|\(month)", default: 0]
if person.maxPerMonth > 0, usedThisMonth >= person.maxPerMonth {
if enforcePreferences, person.maxPerMonth > 0, usedThisMonth >= person.maxPerMonth {
blockedReasons["Monatslimit erreicht", default: 0] += 1
continue
}
@@ -232,7 +295,11 @@ public struct ServicePlanner {
}
}
}
if person.preferredWeekdays.contains(DayMath.weekday(service.date)) {
if person.matchesWeekdayPreference(
weekday: DayMath.weekday(service.date),
timeMinutes: service.timeMinutes,
highFeast: highFeast
), !person.preferredWeekdays.isEmpty {
score += 3
}
score -= Double(total) * options.weightFairness