Export: add grouping "group peoples by postal code"

This commit is contained in:
Julien Fastré 2023-11-10 17:17:51 +01:00
parent cbc83c0e63
commit 0d741ab886
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
5 changed files with 172 additions and 1 deletions

View File

@ -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"

View File

@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Export\Aggregator\PersonAggregators;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
final readonly class PostalCodeAggregator implements AggregatorInterface
{
private const PREFIX = 'person_postal_code_agg';
public function __construct(
private RollingDateConverterInterface $rollingDateConverter,
) {}
public function buildForm(FormBuilderInterface $builder)
{
$builder
->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;
}
}

View File

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Tests\Export\Aggregator\PersonAggregators;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Export\Aggregator\PersonAggregators\PostalCodeAggregator;
use Doctrine\ORM\EntityManagerInterface;
/**
* @internal
*
* @coversNothing
*/
class PostalCodeAggregatorTest extends AbstractAggregatorTest
{
private RollingDateConverterInterface $rollingDateConverter;
protected function setUp(): void
{
self::bootKernel();
$this->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'),
];
}
}

View File

@ -173,4 +173,6 @@ services:
tags: tags:
- { name: chill.export_aggregator, alias: person_center_aggregator } - { name: chill.export_aggregator, alias: person_center_aggregator }
Chill\PersonBundle\Export\Aggregator\PersonAggregators\PostalCodeAggregator:
tags:
- { name: chill.export_aggregator, alias: person_postal_code_aggregator }

View File

@ -1010,6 +1010,10 @@ export:
title: Grouper les usagers par centre title: Grouper les usagers par centre
at_date: Date de calcul du centre at_date: Date de calcul du centre
center: Centre de l'usager 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: course:
by_referrer: by_referrer: