From 86be95ac43e0a9396ef1aa6a4fcbb68fbd22cf9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 7 Nov 2022 18:20:22 +0100 Subject: [PATCH] Feature: [export] Add a rolling date on Acp "by step" filter --- .../ChillMainBundle/config/services.yaml | 5 ++ .../AccompanyingCourseFilters/StepFilter.php | 59 +++++++++++++------ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/Bundle/ChillMainBundle/config/services.yaml b/src/Bundle/ChillMainBundle/config/services.yaml index 040db1a9f..1c56c1ad5 100644 --- a/src/Bundle/ChillMainBundle/config/services.yaml +++ b/src/Bundle/ChillMainBundle/config/services.yaml @@ -105,3 +105,8 @@ services: resource: '../Service/Import/' autowire: true autoconfigure: true + + Chill\MainBundle\Service\RollingDate\: + resource: '../Service/RollingDate/' + autowire: true + autoconfigure: true diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilter.php index 2eb1b90b3..0c9b28e8c 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/StepFilter.php @@ -12,31 +12,40 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Form\Type\PickRollingDateType; +use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Export\Declarations; -use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Contracts\Translation\TranslatorInterface; +use function in_array; class StepFilter implements FilterInterface { + private const A = 'acp_filter_bystep_stephistories'; + private const DEFAULT_CHOICE = AccompanyingPeriod::STEP_CONFIRMED; + private const P = 'acp_step_filter_date'; + private const STEPS = [ 'Draft' => AccompanyingPeriod::STEP_DRAFT, 'Confirmed' => AccompanyingPeriod::STEP_CONFIRMED, 'Closed' => AccompanyingPeriod::STEP_CLOSED, ]; + private RollingDateConverterInterface $rollingDateConverter; + /** * @var TranslatorInterface */ - protected $translator; + private $translator; - public function __construct(TranslatorInterface $translator) + public function __construct(RollingDateConverterInterface $rollingDateConverter, TranslatorInterface $translator) { + $this->rollingDateConverter = $rollingDateConverter; $this->translator = $translator; } @@ -47,17 +56,25 @@ class StepFilter implements FilterInterface public function alterQuery(QueryBuilder $qb, $data) { - $where = $qb->getDQLPart('where'); - $clause = $qb->expr()->eq('acp.step', ':step'); - - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); + if (!in_array(self::A, $qb->getAllAliases(), true)) { + $qb->leftJoin('acp.stepHistories', self::A); } - $qb->add('where', $where); - $qb->setParameter('step', $data['accepted_steps']); + $qb + ->andWhere( + $qb->expr()->andX( + $qb->expr()->lte(self::A . '.startDate', ':' . self::P), + $qb->expr()->orX( + $qb->expr()->isNull(self::A . '.endDate'), + $qb->expr()->lt(self::A . '.endDate', ':' . self::P) + ) + ) + ) + ->andWhere( + $qb->expr()->in(self::A . '.step', ':acp_filter_by_step_steps') + ) + ->setParameter(self::P, $this->rollingDateConverter->convert($data['calc_date'])) + ->setParameter('acp_filter_by_step_steps', $data['accepted_steps']); } public function applyOn() @@ -67,13 +84,17 @@ class StepFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_steps', ChoiceType::class, [ - 'choices' => self::STEPS, - 'multiple' => false, - 'expanded' => true, - 'empty_data' => self::DEFAULT_CHOICE, - 'data' => self::DEFAULT_CHOICE, - ]); + $builder + ->add('accepted_steps', ChoiceType::class, [ + 'choices' => self::STEPS, + 'multiple' => false, + 'expanded' => true, + 'empty_data' => self::DEFAULT_CHOICE, + 'data' => self::DEFAULT_CHOICE, + ]) + ->add('calc_date', PickRollingDateType::class, [ + 'label' => 'export.acp.filter.by_step.date_calc', + ]); } public function describeAction($data, $format = 'string')