fix cashier

multistore
Razmig Sarkissian 1 year ago
parent eaafbdc248
commit 79871c3d5a
  1. 112
      PadelClub/Views/Cashier/CashierDetailView.swift

@ -9,8 +9,6 @@ import SwiftUI
struct CashierDetailView: View { struct CashierDetailView: View {
var tournaments : [Tournament] var tournaments : [Tournament]
@State private var calculateEarnings: Bool = false
@State private var calculateCompletion: Bool = false
@State private var earnings: Double? = nil @State private var earnings: Double? = nil
@State private var paidCompletion: Double? = nil @State private var paidCompletion: Double? = nil
@ -46,29 +44,22 @@ struct CashierDetailView: View {
} }
ForEach(tournaments) { tournament in ForEach(tournaments) { tournament in
Section { CashierSectionView(tournament: tournament, showTournamentTitle: tournaments.count > 1)
LabeledContent {
Text(tournament.earnings().formatted(.currency(code: "EUR").precision(.fractionLength(0))))
} label: {
Text("Encaissement")
Text(tournament.paidCompletion().formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary)
}
_tournamentCashierDetailView(tournament)
} header: {
if tournaments.count > 1 {
Text(tournament.tournamentTitle())
}
}
} }
} }
.headerProminence(.increased) .headerProminence(.increased)
.onAppear { .onAppear {
Task { Task {
if earnings == nil {
_getEarnings() _getEarnings()
}
if paidCompletion == nil {
_getPaidCompletion() _getPaidCompletion()
} }
} }
} }
}
private func _getEarnings() { private func _getEarnings() {
earnings = tournaments.map { $0.earnings() }.reduce(0,+) earnings = tournaments.map { $0.earnings() }.reduce(0,+)
@ -80,37 +71,99 @@ struct CashierDetailView: View {
paidCompletion = Double(selectedPlayers.filter { $0.hasPaid() }.count) / Double(selectedPlayers.count) paidCompletion = Double(selectedPlayers.filter { $0.hasPaid() }.count) / Double(selectedPlayers.count)
} }
private func _tournamentCashierDetailView(_ tournament: Tournament) -> some View { private func _tournamentsCashierDetailView(_ tournaments: [Tournament]) -> some View {
DisclosureGroup { DisclosureGroup {
let selectedPlayers = tournament.selectedPlayers()
ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in
let count = selectedPlayers.filter({ $0.paymentType == type }).count PaymentTypeCashierRowView(tournaments: tournaments, type: type)
if count > 0 { }
} label: {
Text("Voir le détail")
}
}
struct CashierSectionView: View {
let tournament: Tournament
let showTournamentTitle: Bool
@State private var earnings: Double? = nil
@State private var paidCompletion: Double? = nil
var body: some View {
Section {
LabeledContent { LabeledContent {
if let entryFee = tournament.entryFee { if let earnings {
let sum = Double(count) * entryFee Text(earnings.formatted(.currency(code: "EUR").precision(.fractionLength(0))))
Text(sum.formatted(.currency(code: "EUR"))) } else {
ProgressView()
} }
} label: { } label: {
Text(type.localizedLabel()) Text("Encaissement")
Text(count.formatted()) if let paidCompletion {
Text(paidCompletion.formatted(.percent.precision(.fractionLength(0)))).foregroundStyle(.secondary)
}
}
CashierDetailDisclosureView(tournament: tournament)
} header: {
if showTournamentTitle {
Text(tournament.tournamentTitle())
}
}
.onAppear {
Task {
if earnings == nil {
earnings = tournament.earnings()
}
if paidCompletion == nil {
paidCompletion = tournament.paidCompletion()
}
}
} }
} }
} }
struct PaymentTypeCashierRowView: View {
let tournaments: [Tournament]
let type: PlayerRegistration.PlayerPaymentType
@State private var value: Double?
var body: some View {
LabeledContent {
if let value {
Text(value.formatted(.currency(code: "EUR")))
} else {
ProgressView()
}
} label: { } label: {
Text("Voir le détail") Text(type.localizedLabel())
}
.onAppear {
Task {
if value == nil {
value = tournaments.compactMap({ $0.paidSelectedPlayers(type: type) }).reduce(0,+)
}
}
}
} }
} }
private func _tournamentsCashierDetailView(_ tournaments: [Tournament]) -> some View { struct CashierDetailDisclosureView: View {
let tournament: Tournament
var body: some View {
DisclosureGroup { DisclosureGroup {
let selectedPlayers = tournament.selectedPlayers()
ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in ForEach(PlayerRegistration.PlayerPaymentType.allCases) { type in
let value = tournaments.compactMap({ $0.paidSelectedPlayers(type: type) }).reduce(0,+) let count = selectedPlayers.filter({ $0.paymentType == type }).count
if value > 0 { if count > 0 {
LabeledContent { LabeledContent {
Text(value.formatted(.currency(code: "EUR"))) if let entryFee = tournament.entryFee {
let sum = Double(count) * entryFee
Text(sum.formatted(.currency(code: "EUR")))
}
} label: { } label: {
Text(type.localizedLabel()) Text(type.localizedLabel())
Text(count.formatted())
} }
} }
} }
@ -119,3 +172,4 @@ struct CashierDetailView: View {
} }
} }
} }
}

Loading…
Cancel
Save