Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace AppBundle\IntegrationTests\MembershipFee\Entity\Repository;

use Afup\Tests\Support\IntegrationTestCase;
use AppBundle\Association\MemberType;
use AppBundle\MembershipFee\Entity\Repository\CotisationRepository;
use Doctrine\DBAL\Connection;

final class CotisationRepositoryTest extends IntegrationTestCase
{
public function testGenerateInvoiceNumberReturnsFirstNumberOnEmptyTable(): void
{
$cotisationRepository = self::getContainer()->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,
]);
}
}
Loading