exports: add ChildrenNumber Aggregator

This commit is contained in:
Mathieu Jaumotte 2022-08-10 17:17:29 +02:00
parent bc5d610f80
commit 18c17818f6
3 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,108 @@
<?php
namespace Chill\PersonBundle\Export\Aggregator\HouseholdAggregators;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ChildrenNumberAggregator implements AggregatorInterface
{
private TranslatorInterface $translator;
public function __construct(
TranslatorInterface $translator
) {
$this->translator = $translator;
}
/**
* @inheritDoc
*/
public function getLabels($key, array $values, $data)
{
return function ($value): string {
if ($value === '_header') {
return 'Number of children';
}
return $this->translator->trans(
'household_composition.numberOfChildren children in household', [
'numberOfChildren' => $value
]);
};
}
/**
* @inheritDoc
*/
public function getQueryKeys($data): array
{
return ['childrennumber_aggregator'];
}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('on_date', ChillDateType::class, [
'data' => new \DateTime('now'),
]);
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return 'Group by number of children';
}
/**
* @inheritDoc
*/
public function addRole()
{
return null;
}
/**
* @inheritDoc
*/
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->addSelect('composition.numberOfChildren AS childrennumber_aggregator');
$groupBy = $qb->getDQLPart('groupBy');
if (!empty($groupBy)) {
$qb->addGroupBy('childrennumber_aggregator');
} else {
$qb->groupBy('childrennumber_aggregator');
}
// add date in where clause
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->andX(
$qb->expr()->lte('composition.startDate', ':ondate'),
$qb->expr()->orX(
$qb->expr()->gt('composition.endDate', ':ondate'),
$qb->expr()->isNull('composition.endDate')
)
);
}
/**
* @inheritDoc
*/
public function applyOn(): string
{
return Declarations::HOUSEHOLD_TYPE;
}
}

View File

@ -23,3 +23,10 @@ services:
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: household_composition_aggregator }
chill.person.export.aggregator_household_childrennumber:
class: Chill\PersonBundle\Export\Aggregator\HouseholdAggregators\ChildrenNumberAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: household_childrennumber_aggregator }

View File

@ -535,6 +535,8 @@ Accepted composition: Composition familiale
"Filtered by composition: only %compositions% on %ondate%": "Filtré par composition familiale: uniquement %compositions%, en date du %ondate%"
Group by composition: Grouper par composition familiale
Group by number of children: Grouper par nombre d'enfants
## aggregators
Group people by nationality: Grouper les personnes par nationalités
Group by level: Grouper par niveau