From a2eed6d4eb5805f9790687f70019ec1abbf65272 Mon Sep 17 00:00:00 2001 From: Raz Date: Tue, 29 Apr 2025 20:02:30 +0200 Subject: [PATCH] add remaining amount in event / tournament --- PadelClub/Data/Tournament.swift | 8 +++++ .../Views/Cashier/CashierDetailView.swift | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index 9c32551..a1afae1 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -1425,6 +1425,14 @@ defer { return 0.0 } } + + func remainingAmount() -> Double { + if let entryFee { + return Double(selectedPlayers().filter { $0.hasPaid() == false }.count) * entryFee + } else { + return 0.0 + } + } func paidCompletion() -> Double { let selectedPlayers = selectedPlayers() diff --git a/PadelClub/Views/Cashier/CashierDetailView.swift b/PadelClub/Views/Cashier/CashierDetailView.swift index 0f1c8a1..100a39e 100644 --- a/PadelClub/Views/Cashier/CashierDetailView.swift +++ b/PadelClub/Views/Cashier/CashierDetailView.swift @@ -10,6 +10,7 @@ import SwiftUI struct CashierDetailView: View { var tournaments : [Tournament] @State private var earnings: Double? = nil + @State private var remainingAmount: Double? = nil @State private var paidCompletion: Double? = nil init(tournaments: [Tournament]) { @@ -24,6 +25,16 @@ struct CashierDetailView: View { List { if tournaments.count > 1 { Section { + LabeledContent { + if let remainingAmount { + Text(remainingAmount.formatted(.currency(code: Locale.defaultCurrency()).precision(.fractionLength(0)))) + } else { + ProgressView() + } + } label: { + Text("Reste à encaisser") + } + LabeledContent { if let earnings { Text(earnings.formatted(.currency(code: Locale.defaultCurrency()).precision(.fractionLength(0)))) @@ -57,10 +68,18 @@ struct CashierDetailView: View { if paidCompletion == nil { _getPaidCompletion() } + + if remainingAmount == nil { + _getRemainingAmount() + } } } } + private func _getRemainingAmount() { + remainingAmount = tournaments.map { $0.remainingAmount() }.reduce(0,+) + } + private func _getEarnings() { earnings = tournaments.map { $0.earnings() }.reduce(0,+) } @@ -88,11 +107,22 @@ struct CashierDetailView: View { let tournament: Tournament let showTournamentTitle: Bool @State private var earnings: Double? = nil + @State private var remainingAmount: Double? = nil @State private var paidCompletion: Double? = nil @State private var presence: Double? = nil var body: some View { Section { + LabeledContent { + if let remainingAmount { + Text(remainingAmount.formatted(.currency(code: Locale.defaultCurrency()).precision(.fractionLength(0)))) + } else { + ProgressView() + } + } label: { + Text("Reste à encaisser") + } + LabeledContent { if let earnings { Text(earnings.formatted(.currency(code: Locale.defaultCurrency()).precision(.fractionLength(0)))) @@ -130,6 +160,10 @@ struct CashierDetailView: View { if presence == nil { presence = tournament.presenceStatus() } + + if remainingAmount == nil { + remainingAmount = tournament.remainingAmount() + } } } }