From 6f1ec00d3ad647cc14f5d2b7f1a734bc70dc0d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 11 Oct 2022 16:48:22 +0200 Subject: [PATCH] Feature: [export] allow to choose between different granularity for DurationAggregator (acp) [wip] --- .../DurationAggregator.php | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/DurationAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/DurationAggregator.php index 2f8686da0..8d1960dda 100644 --- a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/DurationAggregator.php +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/DurationAggregator.php @@ -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)