From b341c451ec9eb5ef2a79a7a478aeda489775695f Mon Sep 17 00:00:00 2001 From: Baptiste Leduc Date: Mon, 21 Sep 2026 19:14:08 +0000 Subject: [PATCH] =?UTF-8?q?G=C3=A9n=C3=A9ration=20du=20num=C3=A9ro=20de=20?= =?UTF-8?q?facture=20des=20cotisations=20avec=20le=20QueryBuilder=20DBAL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remplace le SQL brut de generateInvoiceNumber() par le QueryBuilder Doctrine DBAL (pattern établi lors des migrations Ting vers Doctrine) et ajoute un test d'intégration pour le calcul des numéros de facture (formats COTIS-*, ancien format commençant par l'année, fallback sur table vide, années antérieures ignorées). --- .../Repository/CotisationRepository.php | 18 ++--- .../Repository/CotisationRepositoryTest.php | 65 +++++++++++++++++++ 2 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 tests/integration/AppBundle/MembershipFee/Entity/Repository/CotisationRepositoryTest.php 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, + ]); + } +}