|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AppBundle\Event\Entity\Repository; |
| 6 | + |
| 7 | +use AppBundle\Doctrine\EntityRepository; |
| 8 | +use AppBundle\Association\Entity\Utilisateur; |
| 9 | +use AppBundle\Event\Entity\Inscription; |
| 10 | +use AppBundle\Event\Entity\TicketSpecialPrice; |
| 11 | +use AppBundle\Event\Model\Ticket; |
| 12 | +use DateTimeImmutable; |
| 13 | +use Doctrine\DBAL\ArrayParameterType; |
| 14 | +use Doctrine\Persistence\ManagerRegistry; |
| 15 | + |
| 16 | +/** |
| 17 | + * @extends EntityRepository<TicketSpecialPrice> |
| 18 | + */ |
| 19 | +final class TicketSpecialPriceRepository extends EntityRepository |
| 20 | +{ |
| 21 | + public function __construct(ManagerRegistry $registry) |
| 22 | + { |
| 23 | + parent::__construct($registry, TicketSpecialPrice::class); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Retourne le token de prix spécial s'il est dans sa fenêtre de validité |
| 28 | + * et qu'aucune inscription (hors annulations) ne l'a déjà consommé. |
| 29 | + */ |
| 30 | + public function findUnusedToken(int $eventId, ?string $token): ?TicketSpecialPrice |
| 31 | + { |
| 32 | + if (!is_string($token) || $token === '') { |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + $now = new DateTimeImmutable(); |
| 37 | + |
| 38 | + /** @var TicketSpecialPrice|null $ticketSpecialPrice */ |
| 39 | + $ticketSpecialPrice = $this->createQueryBuilder('specialPrice') |
| 40 | + ->where('specialPrice.token = :token') |
| 41 | + ->andWhere('specialPrice.eventId = :eventId') |
| 42 | + ->andWhere('specialPrice.dateStart <= :now') |
| 43 | + ->andWhere('specialPrice.dateEnd >= :now') |
| 44 | + ->andWhere('NOT EXISTS ( |
| 45 | + SELECT inscription.id |
| 46 | + FROM ' . Inscription::class . ' inscription |
| 47 | + WHERE inscription.specialPriceToken = specialPrice.token |
| 48 | + AND inscription.etat <> :etatAnnule |
| 49 | + )') |
| 50 | + ->setMaxResults(1) |
| 51 | + ->setParameter('token', $token) |
| 52 | + ->setParameter('eventId', $eventId) |
| 53 | + ->setParameter('now', $now) |
| 54 | + ->setParameter('etatAnnule', Ticket::STATUS_CANCELLED) |
| 55 | + ->getQuery() |
| 56 | + ->getOneOrNullResult() |
| 57 | + ; |
| 58 | + |
| 59 | + return $ticketSpecialPrice; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param list<string> $tokens |
| 64 | + * @return list<TicketSpecialPrice> |
| 65 | + */ |
| 66 | + public function findByTokens(array $tokens): array |
| 67 | + { |
| 68 | + $tokens = array_values(array_unique($tokens)); |
| 69 | + if ($tokens === []) { |
| 70 | + return []; |
| 71 | + } |
| 72 | + |
| 73 | + /** @var list<TicketSpecialPrice> $specialPrices */ |
| 74 | + $specialPrices = $this->createQueryBuilder('specialPrice') |
| 75 | + ->where('specialPrice.token IN (:tokens)') |
| 76 | + ->setParameter('tokens', $tokens, ArrayParameterType::STRING) |
| 77 | + ->getQuery() |
| 78 | + ->getResult() |
| 79 | + ; |
| 80 | + |
| 81 | + return $specialPrices; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Liste des tokens d'un évènement avec le nom du créateur et l'indicateur |
| 86 | + * d'utilisation (une inscription non annulée a consommé le token). |
| 87 | + * |
| 88 | + * @return list<array{specialPrice: TicketSpecialPrice, creatorPrenom: ?string, creatorNom: ?string, used: bool}> |
| 89 | + */ |
| 90 | + public function getByEvent(int $eventId): array |
| 91 | + { |
| 92 | + /** @var list<TicketSpecialPrice> $specialPrices */ |
| 93 | + $specialPrices = $this->findBy(['eventId' => $eventId], ['id' => 'DESC']); |
| 94 | + if ($specialPrices === []) { |
| 95 | + return []; |
| 96 | + } |
| 97 | + |
| 98 | + // Comptage des inscriptions (hors annulées) qui ont consommé chaque token |
| 99 | + $tokens = array_map( |
| 100 | + static fn(TicketSpecialPrice $specialPrice): string => $specialPrice->token, |
| 101 | + $specialPrices, |
| 102 | + ); |
| 103 | + |
| 104 | + $queryBuilder = $this->getEntityManager()->createQueryBuilder(); |
| 105 | + $queryBuilder |
| 106 | + ->select('inscription.specialPriceToken AS token', 'COUNT(inscription.id) AS nbInscriptions') |
| 107 | + ->from(Inscription::class, 'inscription') |
| 108 | + ->where('inscription.specialPriceToken IN (:tokens)') |
| 109 | + ->andWhere('inscription.etat <> :etatAnnule') |
| 110 | + ->groupBy('inscription.specialPriceToken') |
| 111 | + ->setParameter('tokens', $tokens, ArrayParameterType::STRING) |
| 112 | + ->setParameter('etatAnnule', Ticket::STATUS_CANCELLED) |
| 113 | + ; |
| 114 | + |
| 115 | + $usedCounts = []; |
| 116 | + foreach ($queryBuilder->getQuery()->getArrayResult() as $row) { |
| 117 | + if (!is_array($row) || !is_string($row['token'] ?? null)) { |
| 118 | + continue; // le groupe par token garantit la présence de la clé |
| 119 | + } |
| 120 | + |
| 121 | + $token = $row['token']; |
| 122 | + $nbInscriptions = $row['nbInscriptions'] ?? null; |
| 123 | + if (is_int($nbInscriptions)) { |
| 124 | + $usedCounts[$token] = $nbInscriptions > 0; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Noms des créateurs, chargés depuis l'entité Utilisateur (table afup_personnes_physiques) |
| 129 | + $creatorIds = array_map( |
| 130 | + static fn(TicketSpecialPrice $specialPrice): int => $specialPrice->creatorId, |
| 131 | + $specialPrices, |
| 132 | + ); |
| 133 | + $creators = $this->getEntityManager() |
| 134 | + ->getRepository(Utilisateur::class) |
| 135 | + ->findBy(['id' => array_values(array_unique($creatorIds))]) |
| 136 | + ; |
| 137 | + $creatorsById = []; |
| 138 | + foreach ($creators as $creator) { |
| 139 | + if ($creator->id !== null) { |
| 140 | + $creatorsById[$creator->id] = $creator; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + $byEvent = []; |
| 145 | + foreach ($specialPrices as $specialPrice) { |
| 146 | + $creator = $creatorsById[$specialPrice->creatorId] ?? null; |
| 147 | + $byEvent[] = [ |
| 148 | + 'specialPrice' => $specialPrice, |
| 149 | + 'creatorPrenom' => $creator?->firstname, |
| 150 | + 'creatorNom' => $creator?->lastname, |
| 151 | + 'used' => $usedCounts[$specialPrice->token] ?? false, |
| 152 | + ]; |
| 153 | + } |
| 154 | + |
| 155 | + return $byEvent; |
| 156 | + } |
| 157 | +} |
0 commit comments