You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
197 lines
8.0 KiB
197 lines
8.0 KiB
//
|
|
// CashierSettingsView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Razmig Sarkissian on 17/04/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
import PadelClubData
|
|
|
|
struct CashierSettingsView: View {
|
|
|
|
@EnvironmentObject var dataStore: DataStore
|
|
@State private var entryFee: Double? = nil
|
|
@State private var clubMemberFeeDeduction: Double? = nil
|
|
@Bindable var tournament: Tournament
|
|
@FocusState private var focusedField: Tournament.CodingKeys?
|
|
let priceTags: [Double] = [15.0, 20.0, 25.0]
|
|
let deductionTags: [Double] = [5.0, 10.0]
|
|
|
|
init(tournament: Tournament) {
|
|
self.tournament = tournament
|
|
_entryFee = State(wrappedValue: tournament.entryFee)
|
|
_clubMemberFeeDeduction = State(wrappedValue: tournament.clubMemberFeeDeduction)
|
|
}
|
|
|
|
var body: some View {
|
|
List {
|
|
Section {
|
|
LabeledContent {
|
|
TextField(tournament.isFree() ? "Gratuite" : "Inscription", value: $entryFee, format: .currency(code: tournament.defaultCurrency()))
|
|
.keyboardType(.decimalPad)
|
|
.multilineTextAlignment(.trailing)
|
|
.frame(maxWidth: .infinity)
|
|
.focused($focusedField, equals: ._entryFee)
|
|
} label: {
|
|
Text("Frais d'inscription")
|
|
}
|
|
LabeledContent {
|
|
TextField("Réduction", value: $clubMemberFeeDeduction, format: .currency(code: tournament.defaultCurrency()))
|
|
.keyboardType(.decimalPad)
|
|
.multilineTextAlignment(.trailing)
|
|
.frame(maxWidth: .infinity)
|
|
.focused($focusedField, equals: ._clubMemberFeeDeduction)
|
|
.onChange(of: focusedField) {
|
|
if focusedField == ._clubMemberFeeDeduction {
|
|
DispatchQueue.main.async {
|
|
UIApplication.shared.sendAction(#selector(UIResponder.selectAll(_:)), to: nil, from: nil, for: nil)
|
|
}
|
|
}
|
|
}
|
|
} label: {
|
|
Text("Réduction membre")
|
|
}
|
|
.disabled(tournament.isFree())
|
|
|
|
} header: {
|
|
Text("Frais d'inscription")
|
|
} footer: {
|
|
Text("Si vous souhaitez que Padel Club vous aide à suivre les encaissements, indiquer un prix d'inscription. Sinon Padel Club vous aidera à suivre simplement l'arrivée et la présence des joueurs.")
|
|
}
|
|
|
|
let players = tournament.selectedPlayers()
|
|
|
|
if players.anySatisfy({ $0.hasArrived == false }) {
|
|
Section {
|
|
RowButtonView("Tout le monde est arrivé", role: .destructive) {
|
|
players.forEach { player in
|
|
player.hasArrived = true
|
|
}
|
|
_save(players: players)
|
|
}
|
|
} footer: {
|
|
Text("Indique tous les joueurs sont là")
|
|
}
|
|
}
|
|
|
|
if players.anySatisfy({ $0.hasArrived == true }) {
|
|
Section {
|
|
RowButtonView("Personne n'est là", role: .destructive) {
|
|
players.forEach { player in
|
|
player.hasArrived = false
|
|
}
|
|
_save(players: players)
|
|
}
|
|
} footer: {
|
|
Text("Indique qu'aucun joueur n'est arrivé")
|
|
}
|
|
}
|
|
|
|
if players.anySatisfy({ $0.hasPaid() == false }) {
|
|
Section {
|
|
RowButtonView("Tout le monde a réglé", role: .destructive) {
|
|
players.forEach { player in
|
|
if player.hasPaid() == false {
|
|
player.paymentType = .gift
|
|
}
|
|
}
|
|
_save(players: players)
|
|
}
|
|
} footer: {
|
|
Text("Passe tous les joueurs qui n'ont pas réglé en offert")
|
|
}
|
|
}
|
|
|
|
if players.anySatisfy({ $0.hasPaid() == true }) {
|
|
Section {
|
|
RowButtonView("Personne n'a réglé", role: .destructive) {
|
|
players.forEach { player in
|
|
player.paymentType = nil
|
|
}
|
|
_save(players: players)
|
|
}
|
|
} footer: {
|
|
Text("Remet à zéro le type d'encaissement de tous les joueurs")
|
|
}
|
|
}
|
|
}
|
|
.navigationBarBackButtonHidden(focusedField != nil)
|
|
.toolbarBackground(.visible, for: .navigationBar)
|
|
.toolbar {
|
|
if focusedField != nil {
|
|
ToolbarItem(placement: .topBarLeading) {
|
|
Button("Annuler", role: .cancel) {
|
|
focusedField = nil
|
|
}
|
|
}
|
|
|
|
ToolbarItemGroup(placement: .keyboard) {
|
|
if focusedField == ._entryFee {
|
|
if tournament.isFree() {
|
|
ForEach(priceTags, id: \.self) { priceTag in
|
|
Button(priceTag.formatted(.currency(code: tournament.defaultCurrency()))) {
|
|
entryFee = priceTag
|
|
tournament.entryFee = priceTag
|
|
focusedField = nil
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
}
|
|
} else {
|
|
Button("Gratuit") {
|
|
entryFee = nil
|
|
tournament.entryFee = nil
|
|
focusedField = nil
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
|
|
}
|
|
} else if focusedField == ._clubMemberFeeDeduction {
|
|
ForEach(deductionTags, id: \.self) { deductionTag in
|
|
Button(deductionTag.formatted(.currency(code: tournament.defaultCurrency()).precision(.fractionLength(0)))) {
|
|
clubMemberFeeDeduction = deductionTag
|
|
tournament.clubMemberFeeDeduction = deductionTag
|
|
focusedField = nil
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
}
|
|
Button("Gratuit") {
|
|
clubMemberFeeDeduction = entryFee
|
|
tournament.clubMemberFeeDeduction = clubMemberFeeDeduction
|
|
focusedField = nil
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
}
|
|
Spacer()
|
|
Button("Valider") {
|
|
focusedField = nil
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
}
|
|
}
|
|
}
|
|
.onChange(of: focusedField) { old, new in
|
|
if old == ._entryFee {
|
|
tournament.entryFee = entryFee
|
|
} else if old == ._clubMemberFeeDeduction {
|
|
tournament.clubMemberFeeDeduction = clubMemberFeeDeduction
|
|
}
|
|
}
|
|
.onChange(of: [tournament.entryFee, tournament.clubMemberFeeDeduction]) {
|
|
_save()
|
|
}
|
|
}
|
|
|
|
private func _save(players: [PlayerRegistration]) {
|
|
tournament.tournamentStore?.playerRegistrations.addOrUpdate(contentOfs: players)
|
|
}
|
|
|
|
private func _save() {
|
|
dataStore.tournaments.addOrUpdate(instance: tournament)
|
|
}
|
|
}
|
|
|
|
//#Preview {
|
|
// CashierSettingsView(tournaments: [])
|
|
//}
|
|
|