Feature: [export] allow to choose between different granularity for DurationAggregator (acp) [wip]

This commit is contained in:
Julien Fastré 2022-10-11 16:48:22 +02:00
parent aba3b33fd0
commit 6f1ec00d3a

View File

@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
@ -21,6 +22,12 @@ final class DurationAggregator implements AggregatorInterface
{
private TranslatorInterface $translator;
private const CHOICES = [
'month',
'week',
'day'
];
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
@ -33,11 +40,17 @@ final class DurationAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->addSelect(
'
(acp.closingDate - acp.openingDate +15) *12/365
AS duration_aggregator'
);
switch ($data['precision']) {
case 'day':
$qb->addSelect('(COALESCE(acp.closingDate, NOW()) - acp.openingDate) AS duration_aggregator');
break;
case 'week':
$qb->addSelect('EXTRACT (YEAR FROM AGE(COALESCE(acp.closingDate, NOW()), acp.openingDate) * 52 +
EXTRACT (WEEK FROM AGE(COALESCE(acp.closingDate, NOW()');
break;
}
// TODO Pour avoir un interval plus précis (nécessaire ?):
// adapter la fonction extract pour pouvoir l'utiliser avec des intervals: extract(month from interval)
@ -55,7 +68,13 @@ final class DurationAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder)
{
// no form
$builder->add('precision', ChoiceType::class, [
'choices' => array_combine(self::CHOICES, self::CHOICES),
'label' => 'export.aggregator.course.duration.Precision',
'choice_label' => fn (string $c) => 'export.aggregator.course.duration.'.$c,
'multiple' => false,
'expanded' => true,
]);
}
public function getLabels($key, array $values, $data)