diff --git a/PadelClub.xcodeproj/project.pbxproj b/PadelClub.xcodeproj/project.pbxproj index 44be01c..7d91dfa 100644 --- a/PadelClub.xcodeproj/project.pbxproj +++ b/PadelClub.xcodeproj/project.pbxproj @@ -1882,7 +1882,7 @@ ); MARKETING_VERSION = 0.1; MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++20"; - OTHER_SWIFT_FLAGS = "-Xfrontend -warn-long-function-bodies=5 -Xfrontend -warn-long-expression-type-checking=20 -Xfrontend -warn-long-function-bodies=50"; + OTHER_SWIFT_FLAGS = ""; PRODUCT_BUNDLE_IDENTIFIER = app.padelclub; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index de777c3..2aafb28 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -867,7 +867,15 @@ class Tournament : ModelObject, Storable { func selectedPlayers() -> [PlayerRegistration] { return self.selectedSortedTeams().flatMap { $0.unsortedPlayers() }.sorted(by: \.computedRank) } - + + func paidSelectedPlayers(type: PlayerRegistration.PlayerPaymentType) -> Double? { + if let entryFee { + return Double(self.selectedSortedTeams().flatMap { $0.unsortedPlayers() }.filter { $0.paymentType == type }.count) * entryFee + } else { + return nil + } + } + func players() -> [PlayerRegistration] { return self.unsortedTeams().flatMap { $0.unsortedPlayers() }.sorted(by: \.computedRank) } diff --git a/PadelClub/Views/Cashier/CashierDetailView.swift b/PadelClub/Views/Cashier/CashierDetailView.swift index c262b32..fd5952e 100644 --- a/PadelClub/Views/Cashier/CashierDetailView.swift +++ b/PadelClub/Views/Cashier/CashierDetailView.swift @@ -9,6 +9,10 @@ import SwiftUI struct CashierDetailView: View { var tournaments : [Tournament] + @State private var calculateEarnings: Bool = false + @State private var calculateCompletion: Bool = false + @State private var earnings: Double? = nil + @State private var paidCompletion: Double? = nil init(tournaments: [Tournament]) { self.tournaments = tournaments @@ -20,6 +24,27 @@ struct CashierDetailView: View { var body: some View { List { + if tournaments.count > 1 { + Section { + LabeledContent { + if let earnings { + Text(earnings.formatted(.currency(code: "EUR").precision(.fractionLength(0)))) + } else { + ProgressView() + } + } label: { + Text("Encaissement") + if let paidCompletion { + Text(paidCompletion.formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary) + } + } + _tournamentsCashierDetailView(tournaments) + + } header: { + Text("Bilan") + } + } + ForEach(tournaments) { tournament in Section { LabeledContent { @@ -37,12 +62,29 @@ struct CashierDetailView: View { } } .headerProminence(.increased) + .onAppear { + Task { + _getEarnings() + _getPaidCompletion() + } + } + } + + private func _getEarnings() { + earnings = tournaments.map { $0.earnings() }.reduce(0,+) + } + + private func _getPaidCompletion() { + let selectedPlayers = tournaments.flatMap { $0.selectedPlayers() } + if selectedPlayers.isEmpty { paidCompletion = 0 } + paidCompletion = Double(selectedPlayers.filter { $0.hasPaid() }.count) / Double(selectedPlayers.count) } private func _tournamentCashierDetailView(_ tournament: Tournament) -> some View { DisclosureGroup { + let selectedPlayers = tournament.selectedPlayers() ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in - let count = tournament.selectedPlayers().filter({ $0.paymentType == type }).count + let count = selectedPlayers.filter({ $0.paymentType == type }).count if count > 0 { LabeledContent { if let entryFee = tournament.entryFee { @@ -58,20 +100,22 @@ struct CashierDetailView: View { } label: { Text("Voir le détail") } + } -// -// Section { -// ForEach(tournaments) { tournament in -// } -//// HStack { -//// Text("Total") -//// Spacer() -//// Text(event.earnings.formatted(.currency(code: "EUR").precision(.fractionLength(0)))) -//// Text(event.paidCompletion.formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary) -//// } -// } header: { -// Text("Encaissement") -// } - + private func _tournamentsCashierDetailView(_ tournaments: [Tournament]) -> some View { + DisclosureGroup { + ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in + let value = tournaments.compactMap({ $0.paidSelectedPlayers(type: type) }).reduce(0,+) + if value > 0 { + LabeledContent { + Text(value.formatted(.currency(code: "EUR"))) + } label: { + Text(type.localizedLabel()) + } + } + } + } label: { + Text("Voir le détail") + } } }