diff --git a/.changes/unreleased/Feature-20231110-171720.yaml b/.changes/unreleased/Feature-20231110-171720.yaml new file mode 100644 index 000000000..69135e62c --- /dev/null +++ b/.changes/unreleased/Feature-20231110-171720.yaml @@ -0,0 +1,5 @@ +kind: Feature +body: 'Export: add a new aggregator "group peoples by postal code"' +time: 2023-11-10T17:17:20.236876015+01:00 +custom: + Issue: "199" diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/PersonAggregators/PostalCodeAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/PersonAggregators/PostalCodeAggregator.php new file mode 100644 index 000000000..6f8978ad4 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/PersonAggregators/PostalCodeAggregator.php @@ -0,0 +1,99 @@ +add('calc_date', PickRollingDateType::class, [ + 'label' => 'export.aggregator.person.by_postal_code.at_date', + ]); + } + + public function getFormDefaultData(): array + { + return ['calc_date' => new RollingDate(RollingDate::T_TODAY)]; + } + + public function getLabels($key, array $values, mixed $data) + { + return function (null|int|string $value): string { + if ('_header' === $value) { + return 'export.aggregator.person.by_postal_code.header'; + } + + if (null === $value) { + return ''; + } + + return $value; + }; + } + + public function getQueryKeys($data) + { + return [self::PREFIX.'_postal_code_code', self::PREFIX.'_postal_code_label']; + } + + public function getTitle() + { + return 'export.aggregator.person.by_postal_code.title'; + } + + public function addRole(): ?string + { + return null; + } + + public function alterQuery(QueryBuilder $qb, $data) + { + $p = self::PREFIX; + + $qb + ->leftJoin( + 'person.householdAddresses', + "{$p}_household_addresses", + Join::WITH, + "{$p}_household_addresses.validFrom <= :{$p}_calc_date AND ({$p}_household_addresses.validTo IS NULL OR {$p}_household_addresses.validTo > :{$p}_calc_date)" + ) + ->setParameter("{$p}_calc_date", $this->rollingDateConverter->convert($data['calc_date'])) + ->leftJoin("{$p}_household_addresses.address", "{$p}_address") + ->leftJoin("{$p}_address.postcode", "{$p}_postal_code") + ->addSelect("{$p}_postal_code.code AS {$p}_postal_code_code") + ->addSelect("{$p}_postal_code.name AS {$p}_postal_code_label") + ->addGroupBy("{$p}_postal_code_code") + ->addGroupBy("{$p}_postal_code_label") + ; + } + + public function applyOn() + { + return Declarations::PERSON_TYPE; + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/PersonAggregators/PostalCodeAggregatorTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/PersonAggregators/PostalCodeAggregatorTest.php new file mode 100644 index 000000000..445f257b8 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/PersonAggregators/PostalCodeAggregatorTest.php @@ -0,0 +1,61 @@ +rollingDateConverter = self::$container->get(RollingDateConverterInterface::class); + } + + public function getAggregator() + { + return new PostalCodeAggregator($this->rollingDateConverter); + } + + public function getFormData() + { + return [ + ['calc_date' => new RollingDate(RollingDate::T_TODAY)], + ]; + } + + public function getQueryBuilders() + { + self::bootKernel(); + + $em = self::$container + ->get(EntityManagerInterface::class); + + return [ + $em->createQueryBuilder() + ->select('count(person.id)') + ->from(Person::class, 'person'), + ]; + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/exports_person.yaml b/src/Bundle/ChillPersonBundle/config/services/exports_person.yaml index 43f5556cf..d25a3b9e6 100644 --- a/src/Bundle/ChillPersonBundle/config/services/exports_person.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/exports_person.yaml @@ -173,4 +173,6 @@ services: tags: - { name: chill.export_aggregator, alias: person_center_aggregator } - + Chill\PersonBundle\Export\Aggregator\PersonAggregators\PostalCodeAggregator: + tags: + - { name: chill.export_aggregator, alias: person_postal_code_aggregator } diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index f2a90c31f..87c0e3703 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -1010,6 +1010,10 @@ export: title: Grouper les usagers par centre at_date: Date de calcul du centre center: Centre de l'usager + by_postal_code: + title: Grouper les usagers par code postal de l'adresse + at_date: Date de calcul de l'adresse + header: Code postal course: by_referrer: