add custom importing

multistore
Razmig Sarkissian 1 year ago
parent 5c98c367ae
commit e7d8a8c1f9
  1. 2
      PadelClub/Data/Tournament.swift
  2. 72
      PadelClub/Utils/FileImportManager.swift
  3. 2
      PadelClub/Views/Team/Components/TeamHeaderView.swift
  4. 18
      PadelClub/Views/Tournament/FileImportView.swift
  5. 2
      PadelClub/Views/Tournament/Screen/TournamentCashierView.swift

@ -837,7 +837,7 @@ class Tournament : ModelObject, Storable {
} else if index - bracketCut() < groupStageCut() {
return "Poule"
} else {
return "Liste d'attente"
return "Attente"
}
}

@ -377,67 +377,31 @@ class FileImportManager {
}
private func _getPadelBusinessLeagueTeams(from fileContent: String, tournament: Tournament) async -> [TeamHolder] {
let lines = fileContent.components(separatedBy: "\n")
let lines = fileContent.replacingOccurrences(of: "\"", with: "").components(separatedBy: "\n")
guard let firstLine = lines.first else { return [] }
var separator = ","
if firstLine.contains(";") {
separator = ";"
}
let headerCount = firstLine.components(separatedBy: separator).count
var results: [TeamHolder] = []
if headerCount == 23 {
//todo
let fetchRequest = ImportedPlayer.fetchRequest()
let federalContext = PersistenceController.shared.localContainer.viewContext
lines.dropFirst().forEach { line in
let data = line.components(separatedBy: separator)
if data.count == 23 {
// let team = Team(context: context)
// let brand = Brand(context: context)
// brand.title = data[2].trimmed
// brand.qualifier = data[0].trimmed
// brand.country = data[1].trimmed
// brand.lineOfBusiness = data[3].trimmed
// if brand.lineOfBusiness == "Bâtiment / Immo" { //quick fix
// brand.lineOfBusiness = "Bâtiment / Immo / Transport"
// }
// brand.name = data[4].trimmed
// team.brand = brand
//
// for i in 0...5 {
// let sex = data[i*3+5]
// let lastName = data[i*3+6].trimmed
// let firstName = data[i*3+7].trimmed
// if lastName.isEmpty == false {
// let playerOne = Player(context: context)
// let predicate = NSPredicate(format: "(canonicalLastName matches[cd] %@ OR canonicalLastName matches[cd] %@) AND (canonicalFirstName matches[cd] %@ OR canonicalFirstName matches[cd] %@)", lastName, lastName.removePunctuationAndHyphens, firstName, firstName.removePunctuationAndHyphens)
// fetchRequest.predicate = predicate
// if let playerFound = try? federalContext.fetch(fetchRequest).first {
// playerOne.updateWithImportedPlayer(playerFound)
// } else {
// playerOne.lastName = lastName
// playerOne.firstName = firstName
// playerOne.sex = sex == "H" ? 1 : sex == "F" ? 0 : -1
// playerOne.currentRank = tournament?.lastRankMan ?? 0
// }
// team.addToPlayers(playerOne)
// }
// }
// team.category = TournamentCategory.men.importingRawValue
//
// if let players = team.players, players.count > 0 {
// results.append(team)
// } else {
// context.delete(team)
// }
}
let results: [TeamHolder] = lines.chunked(into: 2).map { team in
var teamName: String? = nil
let players = team.map { player in
let data = player.components(separatedBy: separator)
let firstName : String = data[safe: 2]?.trimmed ?? ""
let lastName : String = data[safe: 3]?.trimmed ?? ""
let sex: PlayerRegistration.PlayerSexType = data[safe: 0] == "f" ? PlayerRegistration.PlayerSexType.female : PlayerRegistration.PlayerSexType.male
if data[safe: 1]?.trimmed != nil {
teamName = data[safe: 1]?.trimmed
}
let phoneNumber : String? = data[safe: 4]?.trimmed
let email : String? = data[safe: 5]?.trimmed
//let level : String? = data[safe: 6]?.trimmed
let player = PlayerRegistration(firstName: firstName, lastName: lastName, sex: sex, phoneNumber: phoneNumber, email: email)
return player
}
return results
return TeamHolder(players: players, tournamentCategory: .men, previousTeam: nil, name: teamName, tournament: tournament)
}
return []
return results
}
}

@ -22,7 +22,7 @@ struct TeamHeaderView: View {
}
if team.unsortedPlayers().isEmpty == false {
if tournament?.hideWeight() == false {
if tournament?.hideWeight() == false, team.weight > 0 {
VStack(alignment: .leading, spacing: 0) {
Text("Poids").font(.caption)
Text(team.weight.formatted())

@ -34,6 +34,16 @@ struct FileImportView: View {
return teams.filter { $0.tournamentCategory == tournament.tournamentCategory }.sorted(by: \.weight)
}
private func _deleteTeams() async {
await MainActor.run {
do {
try dataStore.teamRegistrations.delete(contentOfs: tournament.unsortedTeams())
} catch {
Logger.error(error)
}
}
}
var body: some View {
List {
if teams.isEmpty {
@ -44,6 +54,12 @@ struct FileImportView: View {
}
}
if tournament.unsortedTeams().count > 0 {
RowButtonView("Effacer les équipes déjà inscrites", role: .destructive) {
await _deleteTeams()
}
}
Section {
Picker(selection: $fileProvider) {
ForEach(FileImportManager.FileProvider.allCases) {
@ -222,7 +238,7 @@ struct FileImportView: View {
teams.removeAll()
Task {
do {
if selectedFile.lastPathComponent.hasSuffix("xls") {
if selectedFile.lastPathComponent.hasSuffix("xls") || selectedFile.lastPathComponent.hasSuffix("xlsx") {
convertingFile = true
fileContent = try await CloudConvert.manager.uploadFile(selectedFile)
convertingFile = false

@ -58,7 +58,7 @@ enum CashierDestination: Identifiable, Selectable, Equatable {
case .bracket(let round):
let playerRegistrations: [PlayerRegistration] = round.seeds().flatMap { $0.unsortedPlayers() }
return playerRegistrations.filter({ $0.hasPaid() == false }).count
case .all(let tournament):
case .all(_):
return nil
}
}

Loading…
Cancel
Save