diff --git a/api-platform/api/src/ApiResource/AverageTaxStats.php b/api-platform/api/src/ApiResource/AverageTaxStats.php new file mode 100644 index 0000000000000000000000000000000000000000..859d973923fe0084ae7940d95ca02bda77c9c1fe --- /dev/null +++ b/api-platform/api/src/ApiResource/AverageTaxStats.php @@ -0,0 +1,59 @@ + 'integer'] + ), + new Parameter( + name: 'endYear', + in: 'query', + description: 'Année de fin', + required: true, + schema: ['type' => 'integer'] + ), + new Parameter( + name: 'typeCode', + in: 'query', + description: 'Code du type de taxe (ex: TFPNB, TH...)', + required: true, + schema: ['type' => 'string'] + ), + ] + ), + provider: AverageTaxStatsProvider::class + ) + ], + paginationEnabled: false +)] +class AverageTaxStats +{ + #[ApiProperty(identifier: true)] + public string $id; + + public function __construct( + public string $region, + public int $annee, + public float $tauxMoyen + ) { + $this->id = $region . '-' . $annee; + } +} diff --git a/api-platform/api/src/ApiResource/RegionVolumeStats.php b/api-platform/api/src/ApiResource/RegionVolumeStats.php new file mode 100644 index 0000000000000000000000000000000000000000..0c00746a944ff82a66655510ffb8eef164803f64 --- /dev/null +++ b/api-platform/api/src/ApiResource/RegionVolumeStats.php @@ -0,0 +1,56 @@ + 'integer'] + ), + new Parameter( + name: 'typeCode', + in: 'query', + description: 'Code du type de taxe (ex: TFPNB, TH...)', + required: true, + schema: ['type' => 'string'] + ), + ] + ), + provider: RegionVolumeStatsProvider::class + ) + ], + paginationEnabled: false +)] +class RegionVolumeStats +{ + #[ApiProperty(identifier: true)] + public string $id; + + public string $region; + + public float $totalVolume; + + public function __construct(string $region, float $totalVolume) + { + $this->region = $region; + $this->totalVolume = $totalVolume; + $this->id = sprintf('%s-%s', $region, uniqid()); + } +} + diff --git a/api-platform/api/src/Entity/Commune.php b/api-platform/api/src/Entity/Commune.php index 666bc3f530932c939b1abe52e83f30aca06bfd0f..384a105193d209727e6fa02966ebf80b9a523164 100644 --- a/api-platform/api/src/Entity/Commune.php +++ b/api-platform/api/src/Entity/Commune.php @@ -3,13 +3,20 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; use App\Repository\CommuneRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: CommuneRepository::class)] -#[ApiResource] +#[ApiResource( + operations: [ + new Get(), + new GetCollection() + ] +)] class Commune { #[ORM\Id] diff --git a/api-platform/api/src/Entity/Departement.php b/api-platform/api/src/Entity/Departement.php index dd61c5f63b01c5d417c4e92b100e00af148490e1..3e836362a9a88cf4a0d90a7d5f2ab28159da6a87 100644 --- a/api-platform/api/src/Entity/Departement.php +++ b/api-platform/api/src/Entity/Departement.php @@ -3,13 +3,20 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; use App\Repository\DepartementRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: DepartementRepository::class)] -#[ApiResource] +#[ApiResource( + operations: [ + new Get(), + new GetCollection() + ] +)] class Departement { #[ORM\Id] diff --git a/api-platform/api/src/Entity/Region.php b/api-platform/api/src/Entity/Region.php index 617cb3a4fd88f3fc2d9b33b3f439a70b0de1414b..95ba0d7b1d75ae1640b4000379179a626a0bb9bf 100644 --- a/api-platform/api/src/Entity/Region.php +++ b/api-platform/api/src/Entity/Region.php @@ -3,13 +3,20 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; use App\Repository\RegionRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: RegionRepository::class)] -#[ApiResource] +#[ApiResource( + operations: [ + new Get(), + new GetCollection() + ] +)] class Region { #[ORM\Id] diff --git a/api-platform/api/src/Entity/Taxe.php b/api-platform/api/src/Entity/Taxe.php index bba17e487609d1cd26d57dce2d0efab52b4e2194..d3fd0481d6d6565047c97f0aa345de1f15214ae1 100644 --- a/api-platform/api/src/Entity/Taxe.php +++ b/api-platform/api/src/Entity/Taxe.php @@ -3,11 +3,29 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\ApiFilter; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; +use ApiPlatform\Doctrine\Orm\Filter\RangeFilter; use App\Repository\TaxeRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: TaxeRepository::class)] -#[ApiResource] +#[ApiResource( + operations: [ + new Get(), + new GetCollection() + ], + paginationClientEnabled: true +)] +#[ApiFilter(SearchFilter::class, properties: [ + 'type' => 'exact', + 'type.code' => 'exact', + 'annee' => 'exact', + 'commune.departement' => 'exact' +])] +#[ApiFilter(RangeFilter::class, properties: ['annee'])] class Taxe { #[ORM\Id] diff --git a/api-platform/api/src/Entity/TypeTaxe.php b/api-platform/api/src/Entity/TypeTaxe.php index 03f76a3e9432d7e234be2792082d9067c368aa54..91ff8fe6cc60946472627b191d07e14280263a6c 100644 --- a/api-platform/api/src/Entity/TypeTaxe.php +++ b/api-platform/api/src/Entity/TypeTaxe.php @@ -3,13 +3,20 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; use App\Repository\TypeTaxeRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: TypeTaxeRepository::class)] -#[ApiResource] +#[ApiResource( + operations: [ + new Get(), + new GetCollection() + ] +)] class TypeTaxe { public const TFPNB_CODE = 'TFPNB'; diff --git a/api-platform/api/src/Repository/TaxeRepository.php b/api-platform/api/src/Repository/TaxeRepository.php index 9218441a371b9be63e014fe7ecd4f19e0832a7cf..d47c890826908b71e88f7e1f201afbb2d1a57b55 100644 --- a/api-platform/api/src/Repository/TaxeRepository.php +++ b/api-platform/api/src/Repository/TaxeRepository.php @@ -16,28 +16,57 @@ class TaxeRepository extends ServiceEntityRepository parent::__construct($registry, Taxe::class); } -// /** -// * @return Taxe[] Returns an array of Taxe objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('t') -// ->andWhere('t.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('t.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } + /** + * Récupère les taux moyens par région pour une plage d'années et un type de taxe. + * + * @param int $startYear + * @param int $endYear + * @param string $typeCode + * @return array + */ + public function findAverageTauxByRegion(int $startYear, int $endYear, string $typeCode): array + { + return $this->createQueryBuilder('t') + ->select('r.nom as region', 't.annee', 'AVG(t.taux) as tauxMoyen') + ->join('t.commune', 'c') + ->join('c.departement', 'd') + ->join('d.region', 'r') + ->join('t.type', 'typeTaxe') + ->where('t.annee >= :start') + ->andWhere('t.annee <= :end') + ->andWhere('typeTaxe.code = :typeCode') + ->groupBy('r.nom', 't.annee') + ->orderBy('t.annee', 'ASC') + ->addOrderBy('r.nom', 'ASC') + ->setParameter('start', $startYear) + ->setParameter('end', $endYear) + ->setParameter('typeCode', $typeCode) + ->getQuery() + ->getResult(); + } -// public function findOneBySomeField($value): ?Taxe -// { -// return $this->createQueryBuilder('t') -// ->andWhere('t.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + /** + * Récupère la somme des volumes par région pour une année et un type de taxe. + * + * @param int $annee + * @param string $typeCode + * @return array + */ + public function findVolumesByRegion(int $annee, string $typeCode): array + { + return $this->createQueryBuilder('t') + ->select('r.nom as region', 'SUM(t.volume) as totalVolume') + ->join('t.commune', 'c') + ->join('c.departement', 'd') + ->join('d.region', 'r') + ->join('t.type', 'typeTaxe') + ->where('t.annee = :annee') + ->andWhere('typeTaxe.code = :typeCode') + ->groupBy('r.nom') + ->orderBy('totalVolume', 'DESC') + ->setParameter('annee', $annee) + ->setParameter('typeCode', $typeCode) + ->getQuery() + ->getResult(); + } } diff --git a/api-platform/api/src/State/AverageTaxStatsProvider.php b/api-platform/api/src/State/AverageTaxStatsProvider.php new file mode 100644 index 0000000000000000000000000000000000000000..78c8b9429031f7823ee18115e678a1653faa5f5b --- /dev/null +++ b/api-platform/api/src/State/AverageTaxStatsProvider.php @@ -0,0 +1,38 @@ +taxeRepository->findAverageTauxByRegion($startYear, $endYear, $typeCode); + + $dtos = []; + foreach ($results as $result) { + $dtos[] = new AverageTaxStats( + $result['region'], + $result['annee'], + (float)$result['tauxMoyen'] + ); + } + + return $dtos; + } +} + diff --git a/api-platform/api/src/State/RegionVolumeStatsProvider.php b/api-platform/api/src/State/RegionVolumeStatsProvider.php new file mode 100644 index 0000000000000000000000000000000000000000..5fcadf1a552ebb6803a56591d3028186a50ea420 --- /dev/null +++ b/api-platform/api/src/State/RegionVolumeStatsProvider.php @@ -0,0 +1,36 @@ +taxeRepository->findVolumesByRegion($year, $typeCode); + + $dtos = []; + foreach ($results as $result) { + $dtos[] = new RegionVolumeStats( + $result['region'], + (float)$result['totalVolume'] + ); + } + + return $dtos; + } +} +