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() { } else if index - bracketCut() < groupStageCut() {
return "Poule" return "Poule"
} else { } else {
return "Liste d'attente" return "Attente"
} }
} }

@ -377,67 +377,31 @@ class FileImportManager {
} }
private func _getPadelBusinessLeagueTeams(from fileContent: String, tournament: Tournament) async -> [TeamHolder] { 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 [] } guard let firstLine = lines.first else { return [] }
var separator = "," var separator = ","
if firstLine.contains(";") { if firstLine.contains(";") {
separator = ";" separator = ";"
} }
let headerCount = firstLine.components(separatedBy: separator).count let results: [TeamHolder] = lines.chunked(into: 2).map { team in
var results: [TeamHolder] = [] var teamName: String? = nil
if headerCount == 23 { let players = team.map { player in
//todo let data = player.components(separatedBy: separator)
let fetchRequest = ImportedPlayer.fetchRequest() let firstName : String = data[safe: 2]?.trimmed ?? ""
let federalContext = PersistenceController.shared.localContainer.viewContext let lastName : String = data[safe: 3]?.trimmed ?? ""
let sex: PlayerRegistration.PlayerSexType = data[safe: 0] == "f" ? PlayerRegistration.PlayerSexType.female : PlayerRegistration.PlayerSexType.male
lines.dropFirst().forEach { line in if data[safe: 1]?.trimmed != nil {
let data = line.components(separatedBy: separator) teamName = data[safe: 1]?.trimmed
if data.count == 23 { }
let phoneNumber : String? = data[safe: 4]?.trimmed
// let team = Team(context: context) let email : String? = data[safe: 5]?.trimmed
// let brand = Brand(context: context) //let level : String? = data[safe: 6]?.trimmed
// brand.title = data[2].trimmed let player = PlayerRegistration(firstName: firstName, lastName: lastName, sex: sex, phoneNumber: phoneNumber, email: email)
// brand.qualifier = data[0].trimmed return player
// 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)
// }
}
} }
return TeamHolder(players: players, tournamentCategory: .men, previousTeam: nil, name: teamName, tournament: tournament)
return results
} }
return [] return results
} }
} }

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

@ -34,6 +34,16 @@ struct FileImportView: View {
return teams.filter { $0.tournamentCategory == tournament.tournamentCategory }.sorted(by: \.weight) 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 { var body: some View {
List { List {
if teams.isEmpty { 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 { Section {
Picker(selection: $fileProvider) { Picker(selection: $fileProvider) {
ForEach(FileImportManager.FileProvider.allCases) { ForEach(FileImportManager.FileProvider.allCases) {
@ -222,7 +238,7 @@ struct FileImportView: View {
teams.removeAll() teams.removeAll()
Task { Task {
do { do {
if selectedFile.lastPathComponent.hasSuffix("xls") { if selectedFile.lastPathComponent.hasSuffix("xls") || selectedFile.lastPathComponent.hasSuffix("xlsx") {
convertingFile = true convertingFile = true
fileContent = try await CloudConvert.manager.uploadFile(selectedFile) fileContent = try await CloudConvert.manager.uploadFile(selectedFile)
convertingFile = false convertingFile = false

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

Loading…
Cancel
Save