diff --git a/sources/AppBundle/MembershipFee/Entity/Repository/CotisationRepository.php b/sources/AppBundle/MembershipFee/Entity/Repository/CotisationRepository.php index a54d99ecd..f699d296e 100644 --- a/sources/AppBundle/MembershipFee/Entity/Repository/CotisationRepository.php +++ b/sources/AppBundle/MembershipFee/Entity/Repository/CotisationRepository.php @@ -28,15 +28,15 @@ public function getMembershipStartingDate(MemberType $typePersonne, int $idPerso public function generateInvoiceNumber(): string { - $result = $this->getEntityManager()->getConnection()->executeQuery( - <<<'SQL' -SELECT MAX(CAST(SUBSTRING_INDEX(numero_facture, '-', -1) AS UNSIGNED)) + 1 as number -FROM afup_cotisations -WHERE LEFT(numero_facture, 4) = :date - OR LEFT(numero_facture, 10) = :prefixed_date -SQL, - ['date' => date('Y'), 'prefixed_date' => 'COTIS-' . date('Y')], - )->fetchOne(); + $result = $this->getEntityManager()->getConnection()->createQueryBuilder() + ->select('MAX(CAST(SUBSTRING_INDEX(numero_facture, \'-\', -1) AS UNSIGNED)) + 1 as number') + ->from('afup_cotisations') + ->where('LEFT(numero_facture, 4) = :date') + ->orWhere('LEFT(numero_facture, 10) = :prefixed_date') + ->setParameter('date', date('Y')) + ->setParameter('prefixed_date', 'COTIS-' . date('Y')) + ->executeQuery() + ->fetchOne(); return 'COTIS-' . date('Y') . '-' . (is_numeric($result) ? (int) $result : 1); } diff --git a/tests/integration/AppBundle/MembershipFee/Entity/Repository/CotisationRepositoryTest.php b/tests/integration/AppBundle/MembershipFee/Entity/Repository/CotisationRepositoryTest.php new file mode 100644 index 000000000..380ea6098 --- /dev/null +++ b/tests/integration/AppBundle/MembershipFee/Entity/Repository/CotisationRepositoryTest.php @@ -0,0 +1,65 @@ +get(CotisationRepository::class); + + self::assertSame('COTIS-' . date('Y') . '-1', $cotisationRepository->generateInvoiceNumber()); + } + + public function testGenerateInvoiceNumberIncrementsLastPrefixedInvoiceNumber(): void + { + $cotisationRepository = self::getContainer()->get(CotisationRepository::class); + $connection = self::getContainer()->get(Connection::class); + + $this->insertCotisation($connection, 'COTIS-' . date('Y') . '-42'); + + self::assertSame('COTIS-' . date('Y') . '-43', $cotisationRepository->generateInvoiceNumber()); + } + + public function testGenerateInvoiceNumberTakesOldFormatInvoiceNumbersIntoAccount(): void + { + $cotisationRepository = self::getContainer()->get(CotisationRepository::class); + $connection = self::getContainer()->get(Connection::class); + + // Ancien format : numéro commençant directement par l'année (comme la compta générale) + $this->insertCotisation($connection, date('Y') . '-04-14-7'); + $this->insertCotisation($connection, 'COTIS-' . date('Y') . '-20'); + + self::assertSame('COTIS-' . date('Y') . '-21', $cotisationRepository->generateInvoiceNumber()); + } + + public function testGenerateInvoiceNumberIgnoresInvoiceNumbersFromOtherYears(): void + { + $cotisationRepository = self::getContainer()->get(CotisationRepository::class); + $connection = self::getContainer()->get(Connection::class); + + $this->insertCotisation($connection, 'COTIS-' . (date('Y') - 1) . '-99'); + $this->insertCotisation($connection, (date('Y') - 1) . '-04-14-99'); + + self::assertSame('COTIS-' . date('Y') . '-1', $cotisationRepository->generateInvoiceNumber()); + } + + private function insertCotisation(Connection $connection, string $numeroFacture): void + { + $connection->insert('afup_cotisations', [ + 'type_personne' => MemberType::MemberPhysical->value, + 'id_personne' => 42, + 'montant' => 50.0, + 'date_debut' => strtotime('now'), + 'date_fin' => strtotime('+1 year'), + 'numero_facture' => $numeroFacture, + ]); + } +}