From 4ba22184e5fb3aeb5f8cc8718a0af8297b94afc2 Mon Sep 17 00:00:00 2001 From: paaton Date: Tue, 16 Apr 2024 19:11:29 +0200 Subject: [PATCH] SRSImport now checks for alreadyu existing users, and if those users are not in state paid||approved, then it deletes their Participant and creates new Ist Participant on them. --- src/Import/ImportSrs.php | 58 ++++++++++---- src/Participant/Admin/AdminService.php | 2 +- src/Participant/Ist/IstRepository.php | 7 ++ src/Participant/Ist/IstService.php | 105 +++++++++++++++++++++++++ src/User/UserService.php | 76 +----------------- 5 files changed, 158 insertions(+), 90 deletions(-) create mode 100644 src/Participant/Ist/IstService.php diff --git a/src/Import/ImportSrs.php b/src/Import/ImportSrs.php index 5b0bb65a..f2ffa09e 100644 --- a/src/Import/ImportSrs.php +++ b/src/Import/ImportSrs.php @@ -12,22 +12,34 @@ use kissj\FlashMessages\FlashMessagesInterface; use kissj\Participant\Ist\Ist; use kissj\Participant\Ist\IstRepository; +use kissj\Participant\Ist\IstService; +use kissj\Participant\Participant; +use kissj\Participant\ParticipantRepository; use kissj\Participant\ParticipantRole; use kissj\Payment\PaymentService; use kissj\Payment\PaymentStatus; use kissj\User\User; +use kissj\User\UserRepository; +use kissj\User\UserRole; use kissj\User\UserService; use kissj\User\UserStatus; use League\Csv\Exception as LeagueCsvException; +use LeanMapper\Exception\InvalidStateException; +use setasign\Fpdi\PdfParser\Type\PdfNull; use Slim\Psr7\UploadedFile; use Symfony\Contracts\Translation\TranslatorInterface; readonly class ImportSrs { + + public function __construct( + private IstService $istService, private IstRepository $istRepository, private UserService $userService, + private UserRepository $userRepository, private PaymentService $paymentService, + private ParticipantRepository $participantRepository, private CsvParser $csvParser, private FlashMessagesInterface $flashMessages, private TranslatorInterface $translator, @@ -62,8 +74,16 @@ public function importIst(UploadedFile $istsDataFile, Event $event): void $existingCount++; continue; } - - $newIst = $this->mapDataIntoNewIst($istData, $event); + $dbUser = $this->userRepository->findUserFromEmail($istData['E-mail'], $event); + if ($dbUser instanceof User) { + if ($dbUser->status === UserStatus::Approved || $dbUser->status === UserStatus::Paid) { + $errorCount++; + continue; + } + $newIst = $this->mapDataIntoNewIst($istData, $event, $dbUser); + } else { + $newIst = $this->mapDataIntoNewIst($istData, $event); + } if ($newIst === null) { $errorCount++; } else { @@ -80,11 +100,11 @@ public function importIst(UploadedFile $istsDataFile, Event $event): void ], )); } - /** * @param array $data + * @throws InvalidStateException */ - private function mapDataIntoNewIst(array $data, Event $event): ?User + private function mapDataIntoNewIst(array $data, Event $event, ?User $existingUser = null): ?Participant { $notes = []; $notes['id'] = $data['id']; @@ -110,9 +130,9 @@ private function mapDataIntoNewIst(array $data, Event $event): ?User $userStatus = UserStatus::Paid; } - $continget = EventTypeObrok::CONTINGENT_VOLUNTEER; + $contingent = EventTypeObrok::CONTINGENT_VOLUNTEER; if ($data['role'] === 'Organizační tým - registruj se, pokud jsi v týmu Obroku 24') { - $continget = EventTypeObrok::CONTINGENT_ORG; + $contingent = EventTypeObrok::CONTINGENT_ORG; } @@ -135,19 +155,29 @@ private function mapDataIntoNewIst(array $data, Event $event): ?User 'Ano' => true, default => false, }; - - $user = $this->userService->createSkautisUserParticipantPayment( + if (!$existingUser) { + $existingUser = $this->userService->createSkautisUser( + $event, + (int)$skautisUserId, + $email, + $userStatus, + ); + } else { + $participant = $this->participantRepository->findParticipantFromUser($existingUser); + if ($participant !== null) { + $this->participantRepository->delete($participant); + } + } + return $this->istService->createIstPayment( + $existingUser, $event, - (int)$skautisUserId, - $email, - $userStatus, - ParticipantRole::Ist, - $continget, + $contingent, $data['first_name'], $data['last_name'], $data['nick_name'], $address, $data['phone'] === 'NULL' ? '' : $data['phone'], + $email, $data['unit'] === 'NULL' ? '' : $data['unit'], DateTimeUtils::getDateTime($data['birthdate']), $data['Alergie (konkrétní jídlo, hmyz, pyly apod.)'], @@ -170,8 +200,6 @@ private function mapDataIntoNewIst(array $data, Event $event): ?User $event->swift, DateTimeUtils::getDateTime('now + 14 days'), ); - - return $user; } private function getArrivalDate(string $arrivaaDate): DateTimeImmutable diff --git a/src/Participant/Admin/AdminService.php b/src/Participant/Admin/AdminService.php index 6bb7b6d0..29787db6 100755 --- a/src/Participant/Admin/AdminService.php +++ b/src/Participant/Admin/AdminService.php @@ -177,7 +177,7 @@ private function handlePayments( } $correctPayment->participant = $participantTo; - $this->paymentRepository->persist($correctPayment); + $this->paymentRepository-> persist($correctPayment); return $correctPayment; } diff --git a/src/Participant/Ist/IstRepository.php b/src/Participant/Ist/IstRepository.php index 698b36e8..8343467e 100755 --- a/src/Participant/Ist/IstRepository.php +++ b/src/Participant/Ist/IstRepository.php @@ -4,10 +4,15 @@ namespace kissj\Participant\Ist; +use DateTimeImmutable; use Dibi\Row; use kissj\Event\Event; use kissj\Orm\Order; use kissj\Orm\Repository; +use kissj\Participant\Participant; +use kissj\Participant\ParticipantRole; +use kissj\Payment\Payment; +use kissj\Payment\PaymentStatus; /** * @table participant @@ -24,9 +29,11 @@ public function findIst(string $email, Event $event): ?Ist $qb = $this->createFluent(); $qb->where('participant.email = %s', $email); + $qb->where('participant.role = ist'); $qb->join('user')->as('u')->on('u.id = participant.user_id'); $qb->where('u.event_id = %i', $event->id); + /** @var Row $row */ $row = $qb->fetch(); /** @var ?Ist $ist */ diff --git a/src/Participant/Ist/IstService.php b/src/Participant/Ist/IstService.php new file mode 100644 index 00000000..520c7d35 --- /dev/null +++ b/src/Participant/Ist/IstService.php @@ -0,0 +1,105 @@ + $preferredPosition + */ + public function createIstPayment ( + User $user, + Event $event, + string $contingent, + string $firstName, + string $lastName, + string $nickname, + string $permanentResidence, + string $telephoneNumber, + string $email, + string $scoutUnit, + DateTimeImmutable $birthDate, + string $healthProblems, + string $medicaments, + string $psychicalHealthProblems, + string $foodPreferences, + DateTimeImmutable $arrivalDate, + string $skills, + array $preferredPosition, + bool $printedHandbook, + string $notes, + DateTimeImmutable $registrationCloseDate, + DateTimeImmutable $registrationApproveDate, + ?DateTimeImmutable $registrationPayDate, + string $variableSymbol, + int $price, + PaymentStatus $paymentStatus, + string $accountNumber, + string $iban, + string $swift, + DateTimeImmutable $due, + ): Participant { + $participant = new Participant(); + $participant->user = $user; + $participant->role = ParticipantRole::Ist; + $participant->contingent = $contingent; + $participant->firstName = $firstName; + $participant->lastName = $lastName; + $participant->nickname = $nickname; + $participant->permanentResidence = $permanentResidence; + $participant->telephoneNumber = $telephoneNumber; + $participant->email = $email; + $participant->scoutUnit = $scoutUnit; + $participant->birthDate = $birthDate; + $participant->healthProblems = $healthProblems; + $participant->medicaments = $medicaments; + $participant->psychicalHealthProblems = $psychicalHealthProblems; + $participant->foodPreferences = $foodPreferences; + $participant->arrivalDate = $arrivalDate; + $participant->skills = $skills; + $participant->preferredPosition = $preferredPosition; + $participant->printedHandbook = $printedHandbook; + $participant->notes = $notes; + $participant->registrationCloseDate = $registrationCloseDate; + $participant->registrationApproveDate = $registrationApproveDate; + $participant->registrationPayDate = $registrationPayDate; + + $this->participantRepository->persist($participant); + + $payment = new Payment(); + $payment->variableSymbol = $variableSymbol; + $payment->price = (string)$price; + $payment->currency = 'Kč'; + $payment->status = $paymentStatus; + $payment->purpose = 'fee'; + $payment->accountNumber = $accountNumber; + $payment->iban = $iban; + $payment->swift = $swift; + $payment->due = $due; + $payment->note = $event->slug . ' ' . $participant->getFullName(); + $payment->participant = $participant; + + $this->paymentRepository->persist($payment); + + return $participant; + } +} \ No newline at end of file diff --git a/src/User/UserService.php b/src/User/UserService.php index a4e50d53..26b4e38d 100755 --- a/src/User/UserService.php +++ b/src/User/UserService.php @@ -23,7 +23,6 @@ public function __construct( private LoginTokenRepository $loginTokenRepository, private ParticipantRepository $participantRepository, private UserRepository $userRepository, - private PaymentRepository $paymentRepository, private Mailer $mailer, ) { } @@ -134,42 +133,12 @@ public function createParticipantSetRole(User $user, string $role): Participant return $participant; } - /** - * @param array $preferredPosition - */ - public function createSkautisUserParticipantPayment( + + public function createSkautisUser( Event $event, int $skautisId, string $email, UserStatus $userStatus, - ParticipantRole $participantRole, - string $contingent, - string $firstName, - string $lastName, - string $nickname, - string $permanentResidence, - string $telephoneNumber, - string $scoutUnit, - DateTimeImmutable $birthDate, - string $healthProblems, - string $medicaments, - string $psychicalHealthProblems, - string $foodPreferences, - DateTimeImmutable $arrivalDate, - string $skills, - array $preferredPosition, - bool $printedHandbook, - string $notes, - DateTimeImmutable $registrationCloseDate, - DateTimeImmutable $registrationApproveDate, - ?DateTimeImmutable $registrationPayDate, - string $variableSymbol, - int $price, - PaymentStatus $paymentStatus, - string $accountNumber, - string $iban, - string $swift, - DateTimeImmutable $due, ): User { $user = new User(); $user->email = $email; @@ -182,47 +151,6 @@ public function createSkautisUserParticipantPayment( $this->userRepository->persist($user); - $participant = new Participant(); - $participant->user = $user; - $participant->role = $participantRole; - $participant->contingent = $contingent; - $participant->firstName = $firstName; - $participant->lastName = $lastName; - $participant->nickname = $nickname; - $participant->permanentResidence = $permanentResidence; - $participant->telephoneNumber = $telephoneNumber; - $participant->email = $email; - $participant->scoutUnit = $scoutUnit; - $participant->birthDate = $birthDate; - $participant->healthProblems = $healthProblems; - $participant->medicaments = $medicaments; - $participant->psychicalHealthProblems = $psychicalHealthProblems; - $participant->foodPreferences = $foodPreferences; - $participant->arrivalDate = $arrivalDate; - $participant->skills = $skills; - $participant->preferredPosition = $preferredPosition; - $participant->printedHandbook = $printedHandbook; - $participant->notes = $notes; - $participant->registrationCloseDate = $registrationCloseDate; - $participant->registrationApproveDate = $registrationApproveDate; - $participant->registrationPayDate = $registrationPayDate; - - $this->participantRepository->persist($participant); - - $payment = new Payment(); - $payment->variableSymbol = $variableSymbol; - $payment->price = (string)$price; - $payment->currency = 'Kč'; - $payment->status = $paymentStatus; - $payment->purpose = 'fee'; - $payment->accountNumber = $accountNumber; - $payment->iban = $iban; - $payment->swift = $swift; - $payment->due = $due; - $payment->note = $event->slug . ' ' . $participant->getFullName(); - $payment->participant = $participant; - - $this->paymentRepository->persist($payment); return $user; }