Gastansicht, Verbesserungen bei Planung
This commit is contained in:
1 parent
b3afa3ef32
commit
c08d43e7dd
38 files changed
+3845
-219
No files matched your search
@@ -47,6 +47,7 @@ public final class AppDatabase {
|
||||
|
||||
CREATE TABLE IF NOT EXISTS people (
|
||||
id TEXT PRIMARY KEY,
|
||||
short_code TEXT NOT NULL DEFAULT '',
|
||||
first_name TEXT NOT NULL,
|
||||
last_name TEXT NOT NULL,
|
||||
roles TEXT NOT NULL,
|
||||
@@ -54,8 +55,10 @@ public final class AppDatabase {
|
||||
phone TEXT NOT NULL DEFAULT '',
|
||||
preferred_church_ids TEXT NOT NULL DEFAULT '[]',
|
||||
preferred_weekdays TEXT NOT NULL DEFAULT '[]',
|
||||
preferred_weekday_periods TEXT NOT NULL DEFAULT '[]',
|
||||
preferred_service_titles TEXT NOT NULL DEFAULT '[]',
|
||||
max_per_month INTEGER NOT NULL DEFAULT 6,
|
||||
allow_beyond_preferences INTEGER NOT NULL DEFAULT 0,
|
||||
active INTEGER NOT NULL DEFAULT 1,
|
||||
notes TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
@@ -118,6 +121,7 @@ public final class AppDatabase {
|
||||
person_id TEXT NOT NULL REFERENCES people(id) ON DELETE CASCADE,
|
||||
role TEXT NOT NULL,
|
||||
locked INTEGER NOT NULL DEFAULT 0,
|
||||
beyond_preferences INTEGER NOT NULL DEFAULT 0,
|
||||
note TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL,
|
||||
UNIQUE(service_id, person_id, role)
|
||||
@@ -178,7 +182,14 @@ public final class AppDatabase {
|
||||
column: "location",
|
||||
definition: "TEXT NOT NULL DEFAULT ''"
|
||||
)
|
||||
try database.run("INSERT OR REPLACE INTO settings(key, value) VALUES('schemaVersion', '3')")
|
||||
// People gained a Kürzel, weekday time periods and permission to be
|
||||
// planned beyond their preferences.
|
||||
try addColumnIfMissing(table: "people", column: "short_code", definition: "TEXT NOT NULL DEFAULT ''")
|
||||
try addColumnIfMissing(table: "people", column: "preferred_weekday_periods", definition: "TEXT NOT NULL DEFAULT '[]'")
|
||||
try addColumnIfMissing(table: "people", column: "allow_beyond_preferences", definition: "INTEGER NOT NULL DEFAULT 0")
|
||||
// Assignments record when a volunteer was planned beyond preferences.
|
||||
try addColumnIfMissing(table: "assignments", column: "beyond_preferences", definition: "INTEGER NOT NULL DEFAULT 0")
|
||||
try database.run("INSERT OR REPLACE INTO settings(key, value) VALUES('schemaVersion', '4')")
|
||||
}
|
||||
|
||||
private func addColumnIfMissing(table: String, column: String, definition: String) throws {
|
||||
@@ -246,22 +257,30 @@ public final class AppDatabase {
|
||||
let roles = encode(person.roles.map(\.rawValue).sorted())
|
||||
let churches = encode(person.preferredChurchIDs.sorted())
|
||||
let weekdays = encode(person.preferredWeekdays.sorted())
|
||||
let periods = encode(person.preferredWeekdayPeriods.sorted { $0.weekday < $1.weekday })
|
||||
let titles = encode(person.preferredServiceTitles)
|
||||
try database.run("""
|
||||
INSERT INTO people(id, first_name, last_name, roles, email, phone, preferred_church_ids,
|
||||
preferred_weekdays, preferred_service_titles, max_per_month, active, notes)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO people(id, short_code, first_name, last_name, roles, email, phone, preferred_church_ids,
|
||||
preferred_weekdays, preferred_weekday_periods, preferred_service_titles,
|
||||
max_per_month, allow_beyond_preferences, active, notes)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
short_code=excluded.short_code,
|
||||
first_name=excluded.first_name, last_name=excluded.last_name, roles=excluded.roles,
|
||||
email=excluded.email, phone=excluded.phone,
|
||||
preferred_church_ids=excluded.preferred_church_ids,
|
||||
preferred_weekdays=excluded.preferred_weekdays,
|
||||
preferred_weekday_periods=excluded.preferred_weekday_periods,
|
||||
preferred_service_titles=excluded.preferred_service_titles,
|
||||
max_per_month=excluded.max_per_month, active=excluded.active, notes=excluded.notes
|
||||
max_per_month=excluded.max_per_month,
|
||||
allow_beyond_preferences=excluded.allow_beyond_preferences,
|
||||
active=excluded.active, notes=excluded.notes
|
||||
""", [
|
||||
.text(person.id), .text(person.firstName), .text(person.lastName), .text(roles),
|
||||
.text(person.id), .text(person.shortCode.trimmingCharacters(in: .whitespacesAndNewlines)),
|
||||
.text(person.firstName), .text(person.lastName), .text(roles),
|
||||
.text(person.email), .text(person.phone), .text(churches), .text(weekdays),
|
||||
.text(titles), .int(person.maxPerMonth), .bool(person.active), .text(person.notes)
|
||||
.text(periods), .text(titles), .int(person.maxPerMonth),
|
||||
.bool(person.allowBeyondPreferences), .bool(person.active), .text(person.notes)
|
||||
])
|
||||
}
|
||||
|
||||
@@ -277,6 +296,7 @@ public final class AppDatabase {
|
||||
let roleStrings: [String] = decode(row.string("roles", default: "[]"))
|
||||
return Person(
|
||||
id: id,
|
||||
shortCode: row.string("short_code", default: ""),
|
||||
firstName: first,
|
||||
lastName: last,
|
||||
roles: Set(roleStrings.compactMap(Role.init(rawValue:))),
|
||||
@@ -284,8 +304,10 @@ public final class AppDatabase {
|
||||
phone: row.string("phone", default: ""),
|
||||
preferredChurchIDs: Set(decode(row.string("preferred_church_ids", default: "[]")) as [String]),
|
||||
preferredWeekdays: Set(decode(row.string("preferred_weekdays", default: "[]")) as [Int]),
|
||||
preferredWeekdayPeriods: decode(row.string("preferred_weekday_periods", default: "[]")),
|
||||
preferredServiceTitles: decode(row.string("preferred_service_titles", default: "[]")),
|
||||
maxPerMonth: row.int("max_per_month", default: 6),
|
||||
allowBeyondPreferences: row.bool("allow_beyond_preferences", default: false),
|
||||
active: row.bool("active", default: true),
|
||||
notes: row.string("notes", default: "")
|
||||
)
|
||||
@@ -545,29 +567,49 @@ public final class AppDatabase {
|
||||
|
||||
public func upsert(_ assignment: Assignment) throws {
|
||||
try database.run("""
|
||||
INSERT INTO assignments(id, service_id, person_id, role, locked, note, created_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO assignments(id, service_id, person_id, role, locked, beyond_preferences, note, created_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET service_id=excluded.service_id, person_id=excluded.person_id,
|
||||
role=excluded.role, locked=excluded.locked, note=excluded.note
|
||||
role=excluded.role, locked=excluded.locked,
|
||||
beyond_preferences=excluded.beyond_preferences, note=excluded.note
|
||||
""", [
|
||||
.text(assignment.id), .text(assignment.serviceID), .text(assignment.personID),
|
||||
.text(assignment.role.rawValue), .bool(assignment.locked), .text(assignment.note),
|
||||
.text(assignment.role.rawValue), .bool(assignment.locked),
|
||||
.bool(assignment.beyondPreferences), .text(assignment.note),
|
||||
.text(ISO8601DateFormatter().string(from: assignment.createdAt))
|
||||
])
|
||||
}
|
||||
|
||||
/// Replaces the assignment for one role of a service. Passing nil clears the slot.
|
||||
public func setAssignment(serviceID: String, role: Role, personID: String?, locked: Bool = false) throws {
|
||||
public func setAssignment(
|
||||
serviceID: String,
|
||||
role: Role,
|
||||
personID: String?,
|
||||
locked: Bool = false,
|
||||
beyondPreferences: Bool = false
|
||||
) throws {
|
||||
try database.transaction {
|
||||
try database.run("DELETE FROM assignments WHERE service_id=? AND role=?", [.text(serviceID), .text(role.rawValue)])
|
||||
guard let personID else { return }
|
||||
let assignment = Assignment(serviceID: serviceID, personID: personID, role: role, locked: locked)
|
||||
let assignment = Assignment(
|
||||
serviceID: serviceID, personID: personID, role: role,
|
||||
locked: locked, beyondPreferences: beyondPreferences
|
||||
)
|
||||
try upsert(assignment)
|
||||
}
|
||||
}
|
||||
|
||||
public func setAssignment(serviceID: String, personID: String?, role: Role, locked: Bool = false) throws {
|
||||
try setAssignment(serviceID: serviceID, role: role, personID: personID, locked: locked)
|
||||
public func setAssignment(
|
||||
serviceID: String,
|
||||
personID: String?,
|
||||
role: Role,
|
||||
locked: Bool = false,
|
||||
beyondPreferences: Bool = false
|
||||
) throws {
|
||||
try setAssignment(
|
||||
serviceID: serviceID, role: role, personID: personID,
|
||||
locked: locked, beyondPreferences: beyondPreferences
|
||||
)
|
||||
}
|
||||
|
||||
public func deleteAssignment(id: String) throws {
|
||||
@@ -605,6 +647,7 @@ public final class AppDatabase {
|
||||
personID: personID,
|
||||
role: role,
|
||||
locked: row.bool("locked", default: false),
|
||||
beyondPreferences: row.bool("beyond_preferences", default: false),
|
||||
note: row.string("note", default: ""),
|
||||
createdAt: ISO8601DateFormatter().date(from: row.string("created_at", default: "")) ?? Date()
|
||||
)
|
||||
|
||||
@@ -107,8 +107,58 @@ public struct Church: Identifiable, Codable, Hashable, Sendable {
|
||||
|
||||
// MARK: - People
|
||||
|
||||
public enum WeekdayPeriod: String, Codable, CaseIterable, Identifiable, Sendable, Hashable {
|
||||
case all
|
||||
case morning
|
||||
case afternoon
|
||||
case evening
|
||||
|
||||
public var id: String { rawValue }
|
||||
|
||||
public var label: String {
|
||||
switch self {
|
||||
case .morning: "vormittags (0–12 Uhr)"
|
||||
case .afternoon: "nachmittags (12–17 Uhr)"
|
||||
case .evening: "abends (ab 17 Uhr)"
|
||||
case .all: "ganztägig"
|
||||
}
|
||||
}
|
||||
|
||||
public var shortLabel: String {
|
||||
switch self {
|
||||
case .morning: "vormittags"
|
||||
case .afternoon: "nachmittags"
|
||||
case .evening: "abends"
|
||||
case .all: "ganztägig"
|
||||
}
|
||||
}
|
||||
|
||||
/// Morning is 0–12, afternoon 12–17, evening after 17.
|
||||
public func matches(timeMinutes: Int) -> Bool {
|
||||
switch self {
|
||||
case .morning: timeMinutes < 12 * 60
|
||||
case .afternoon: timeMinutes >= 12 * 60 && timeMinutes < 17 * 60
|
||||
case .evening: timeMinutes >= 17 * 60
|
||||
case .all: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct WeekdayPeriodPreference: Codable, Hashable, Sendable, Identifiable {
|
||||
public var weekday: Int
|
||||
public var period: WeekdayPeriod
|
||||
|
||||
public var id: Int { weekday }
|
||||
|
||||
public init(weekday: Int, period: WeekdayPeriod = .all) {
|
||||
self.weekday = weekday
|
||||
self.period = period
|
||||
}
|
||||
}
|
||||
|
||||
public struct Person: Identifiable, Codable, Hashable, Sendable {
|
||||
public var id: String
|
||||
public var shortCode: String
|
||||
public var firstName: String
|
||||
public var lastName: String
|
||||
public var roles: Set<Role>
|
||||
@@ -116,13 +166,16 @@ public struct Person: Identifiable, Codable, Hashable, Sendable {
|
||||
public var phone: String
|
||||
public var preferredChurchIDs: Set<String>
|
||||
public var preferredWeekdays: Set<Int> // 1 = Sunday … 7 = Saturday; empty = no preference
|
||||
public var preferredWeekdayPeriods: [WeekdayPeriodPreference]
|
||||
public var preferredServiceTitles: [String]
|
||||
public var maxPerMonth: Int
|
||||
public var allowBeyondPreferences: Bool
|
||||
public var active: Bool
|
||||
public var notes: String
|
||||
|
||||
public init(
|
||||
id: String = UUID().uuidString,
|
||||
shortCode: String = "",
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
roles: Set<Role> = [.lector],
|
||||
@@ -130,12 +183,15 @@ public struct Person: Identifiable, Codable, Hashable, Sendable {
|
||||
phone: String = "",
|
||||
preferredChurchIDs: Set<String> = [],
|
||||
preferredWeekdays: Set<Int> = [],
|
||||
preferredWeekdayPeriods: [WeekdayPeriodPreference] = [],
|
||||
preferredServiceTitles: [String] = [],
|
||||
maxPerMonth: Int = 6,
|
||||
allowBeyondPreferences: Bool = false,
|
||||
active: Bool = true,
|
||||
notes: String = ""
|
||||
) {
|
||||
self.id = id
|
||||
self.shortCode = shortCode
|
||||
self.firstName = firstName
|
||||
self.lastName = lastName
|
||||
self.roles = roles
|
||||
@@ -143,12 +199,42 @@ public struct Person: Identifiable, Codable, Hashable, Sendable {
|
||||
self.phone = phone
|
||||
self.preferredChurchIDs = preferredChurchIDs
|
||||
self.preferredWeekdays = preferredWeekdays
|
||||
self.preferredWeekdayPeriods = preferredWeekdayPeriods
|
||||
self.preferredServiceTitles = preferredServiceTitles
|
||||
self.maxPerMonth = maxPerMonth
|
||||
self.allowBeyondPreferences = allowBeyondPreferences
|
||||
self.active = active
|
||||
self.notes = notes
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case id, shortCode, firstName, lastName, roles, email, phone
|
||||
case preferredChurchIDs, preferredWeekdays, preferredWeekdayPeriods
|
||||
case preferredServiceTitles, maxPerMonth, allowBeyondPreferences
|
||||
case active, notes
|
||||
}
|
||||
|
||||
/// Older exchange files and backups predate the Kürzel, weekday periods and
|
||||
/// the beyond-preferences flag, so every new key falls back to a default.
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
||||
shortCode = try container.decodeIfPresent(String.self, forKey: .shortCode) ?? ""
|
||||
firstName = try container.decode(String.self, forKey: .firstName)
|
||||
lastName = try container.decode(String.self, forKey: .lastName)
|
||||
roles = try container.decodeIfPresent(Set<Role>.self, forKey: .roles) ?? [.lector]
|
||||
email = try container.decodeIfPresent(String.self, forKey: .email) ?? ""
|
||||
phone = try container.decodeIfPresent(String.self, forKey: .phone) ?? ""
|
||||
preferredChurchIDs = try container.decodeIfPresent(Set<String>.self, forKey: .preferredChurchIDs) ?? []
|
||||
preferredWeekdays = try container.decodeIfPresent(Set<Int>.self, forKey: .preferredWeekdays) ?? []
|
||||
preferredWeekdayPeriods = try container.decodeIfPresent([WeekdayPeriodPreference].self, forKey: .preferredWeekdayPeriods) ?? []
|
||||
preferredServiceTitles = try container.decodeIfPresent([String].self, forKey: .preferredServiceTitles) ?? []
|
||||
maxPerMonth = try container.decodeIfPresent(Int.self, forKey: .maxPerMonth) ?? 6
|
||||
allowBeyondPreferences = try container.decodeIfPresent(Bool.self, forKey: .allowBeyondPreferences) ?? false
|
||||
active = try container.decodeIfPresent(Bool.self, forKey: .active) ?? true
|
||||
notes = try container.decodeIfPresent(String.self, forKey: .notes) ?? ""
|
||||
}
|
||||
|
||||
public var fullName: String {
|
||||
"\(firstName) \(lastName)".trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
@@ -176,6 +262,34 @@ public struct Person: Identifiable, Codable, Hashable, Sendable {
|
||||
}
|
||||
|
||||
public func canFill(_ role: Role) -> Bool { roles.contains(role) }
|
||||
|
||||
/// Guest-facing label: an explicitly maintained Kürzel wins, otherwise the
|
||||
/// initials from first and last name.
|
||||
public var displayShort: String {
|
||||
let code = shortCode.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return code.isEmpty ? initials : code
|
||||
}
|
||||
|
||||
public func weekdayPeriod(for weekday: Int) -> WeekdayPeriod {
|
||||
preferredWeekdayPeriods.first { $0.weekday == weekday }?.period ?? .all
|
||||
}
|
||||
|
||||
/// High feasts count like Sundays, so a Sunday preference also covers them.
|
||||
/// An empty weekday list means no restriction.
|
||||
public func matchesWeekdayPreference(weekday: Int, timeMinutes: Int, highFeast: Bool) -> Bool {
|
||||
guard !preferredWeekdays.isEmpty else { return true }
|
||||
var candidates = [weekday]
|
||||
if highFeast && weekday != 1 { candidates.append(1) }
|
||||
for day in candidates where preferredWeekdays.contains(day) {
|
||||
if weekdayPeriod(for: day).matches(timeMinutes: timeMinutes) { return true }
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/// Free-text planning note shown next to assignments and candidates.
|
||||
public var preferenceNote: String {
|
||||
notes.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
}
|
||||
|
||||
public struct Absence: Identifiable, Codable, Hashable, Sendable {
|
||||
@@ -380,6 +494,7 @@ public struct Assignment: Identifiable, Codable, Hashable, Sendable {
|
||||
public var personID: String
|
||||
public var role: Role
|
||||
public var locked: Bool
|
||||
public var beyondPreferences: Bool
|
||||
public var note: String
|
||||
public var createdAt: Date
|
||||
|
||||
@@ -389,6 +504,7 @@ public struct Assignment: Identifiable, Codable, Hashable, Sendable {
|
||||
personID: String,
|
||||
role: Role,
|
||||
locked: Bool = false,
|
||||
beyondPreferences: Bool = false,
|
||||
note: String = "",
|
||||
createdAt: Date = Date()
|
||||
) {
|
||||
@@ -397,9 +513,28 @@ public struct Assignment: Identifiable, Codable, Hashable, Sendable {
|
||||
self.personID = personID
|
||||
self.role = role
|
||||
self.locked = locked
|
||||
self.beyondPreferences = beyondPreferences
|
||||
self.note = note
|
||||
self.createdAt = createdAt
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case id, serviceID, personID, role, locked, beyondPreferences, note, createdAt
|
||||
}
|
||||
|
||||
/// Exchange files created before the beyond-preferences marker decode to
|
||||
/// the ordinary case.
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString
|
||||
serviceID = try container.decode(String.self, forKey: .serviceID)
|
||||
personID = try container.decode(String.self, forKey: .personID)
|
||||
role = try container.decode(Role.self, forKey: .role)
|
||||
locked = try container.decodeIfPresent(Bool.self, forKey: .locked) ?? false
|
||||
beyondPreferences = try container.decodeIfPresent(Bool.self, forKey: .beyondPreferences) ?? false
|
||||
note = try container.decodeIfPresent(String.self, forKey: .note) ?? ""
|
||||
createdAt = try container.decodeIfPresent(Date.self, forKey: .createdAt) ?? Date()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Liturgy
|
||||
@@ -710,6 +845,13 @@ public struct DayPlanRow: Identifiable, Hashable, Sendable {
|
||||
public var openSlots: Int { services.reduce(0) { $0 + $1.openSlots } }
|
||||
public var assignmentCount: Int { services.reduce(0) { $0 + $1.assignments.count } }
|
||||
public var hasServices: Bool { !services.isEmpty }
|
||||
|
||||
/// Marks days with at least one assignment beyond the person's preferences.
|
||||
public var hasBeyondPreferences: Bool {
|
||||
services.contains { service in
|
||||
service.assignments.contains(where: \.beyondPreferences)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct UnfilledSlot: Identifiable, Hashable, Sendable {
|
||||
@@ -898,6 +1040,7 @@ public struct ExchangeSummary: Hashable, Sendable {
|
||||
public struct PlannerOptions: Codable, Hashable, Sendable {
|
||||
public var replaceExisting: Bool
|
||||
public var respectPreferredWeekdays: Bool
|
||||
public var allowBeyondPreferences: Bool
|
||||
public var allowDoubleRolesOnSameDay: Bool
|
||||
public var weightChurchPreference: Double
|
||||
public var weightServicePreference: Double
|
||||
@@ -908,6 +1051,7 @@ public struct PlannerOptions: Codable, Hashable, Sendable {
|
||||
public init(
|
||||
replaceExisting: Bool = false,
|
||||
respectPreferredWeekdays: Bool = true,
|
||||
allowBeyondPreferences: Bool = false,
|
||||
allowDoubleRolesOnSameDay: Bool = false,
|
||||
weightChurchPreference: Double = 8,
|
||||
weightServicePreference: Double = 4,
|
||||
@@ -917,6 +1061,7 @@ public struct PlannerOptions: Codable, Hashable, Sendable {
|
||||
) {
|
||||
self.replaceExisting = replaceExisting
|
||||
self.respectPreferredWeekdays = respectPreferredWeekdays
|
||||
self.allowBeyondPreferences = allowBeyondPreferences
|
||||
self.allowDoubleRolesOnSameDay = allowDoubleRolesOnSameDay
|
||||
self.weightChurchPreference = weightChurchPreference
|
||||
self.weightServicePreference = weightServicePreference
|
||||
@@ -924,6 +1069,25 @@ public struct PlannerOptions: Codable, Hashable, Sendable {
|
||||
self.weightDualRolePreference = weightDualRolePreference
|
||||
self.maxAssignmentsPerDay = maxAssignmentsPerDay
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case replaceExisting, respectPreferredWeekdays, allowBeyondPreferences
|
||||
case allowDoubleRolesOnSameDay, weightChurchPreference, weightServicePreference
|
||||
case weightFairness, weightDualRolePreference, maxAssignmentsPerDay
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
replaceExisting = try container.decodeIfPresent(Bool.self, forKey: .replaceExisting) ?? false
|
||||
respectPreferredWeekdays = try container.decodeIfPresent(Bool.self, forKey: .respectPreferredWeekdays) ?? true
|
||||
allowBeyondPreferences = try container.decodeIfPresent(Bool.self, forKey: .allowBeyondPreferences) ?? false
|
||||
allowDoubleRolesOnSameDay = try container.decodeIfPresent(Bool.self, forKey: .allowDoubleRolesOnSameDay) ?? false
|
||||
weightChurchPreference = try container.decodeIfPresent(Double.self, forKey: .weightChurchPreference) ?? 8
|
||||
weightServicePreference = try container.decodeIfPresent(Double.self, forKey: .weightServicePreference) ?? 4
|
||||
weightFairness = try container.decodeIfPresent(Double.self, forKey: .weightFairness) ?? 2.2
|
||||
weightDualRolePreference = try container.decodeIfPresent(Double.self, forKey: .weightDualRolePreference) ?? 14
|
||||
maxAssignmentsPerDay = try container.decodeIfPresent(Int.self, forKey: .maxAssignmentsPerDay) ?? 1
|
||||
}
|
||||
}
|
||||
|
||||
public struct PlanResult: Sendable {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user