diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/HouseholdAggregators/CompositionAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/HouseholdAggregators/CompositionAggregator.php index 2a7c89ee3..3e292ceec 100644 --- a/src/Bundle/ChillPersonBundle/Export/Aggregator/HouseholdAggregators/CompositionAggregator.php +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/HouseholdAggregators/CompositionAggregator.php @@ -3,9 +3,12 @@ namespace Chill\PersonBundle\Export\Aggregator\HouseholdAggregators; use Chill\MainBundle\Export\AggregatorInterface; +use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Repository\Household\HouseholdCompositionTypeRepository; +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; @@ -54,7 +57,9 @@ class CompositionAggregator implements AggregatorInterface */ public function buildForm(FormBuilderInterface $builder) { - // TODO: Implement buildForm() method. + $builder->add('on_date', ChillDateType::class, [ + 'data' => new \DateTime('now'), + ]); } /** @@ -87,6 +92,26 @@ class CompositionAggregator implements AggregatorInterface } else { $qb->groupBy('composition_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') + ) + ); + + if ($where instanceof Andx) { + $where->add($clause); + } else { + $where = $qb->expr()->andX($clause); + } + + $qb->add('where', $where); + $qb->setParameter('ondate', $data['on_date'], Types::DATE_MUTABLE); } /** diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/HouseholdFilters/CompositionFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/HouseholdFilters/CompositionFilter.php index ae4427e6d..f8a36e4b9 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/HouseholdFilters/CompositionFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/HouseholdFilters/CompositionFilter.php @@ -3,9 +3,11 @@ namespace Chill\PersonBundle\Export\Filter\HouseholdFilters; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\Household\HouseholdCompositionType; use Chill\PersonBundle\Export\Declarations; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Bridge\Doctrine\Form\Type\EntityType; @@ -26,16 +28,21 @@ class CompositionFilter implements FilterInterface */ public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_composition', EntityType::class, [ - 'class' => HouseholdCompositionType::class, - 'choice_label' => function (HouseholdCompositionType $type) { - return $this->translatableStringHelper->localize( - $type->getLabel() - ); - }, - 'multiple' => true, - 'expanded' => true, - ]); + $builder + ->add('accepted_composition', EntityType::class, [ + 'class' => HouseholdCompositionType::class, + 'choice_label' => function (HouseholdCompositionType $type) { + return $this->translatableStringHelper->localize( + $type->getLabel() + ); + }, + 'multiple' => true, + 'expanded' => true, + ]) + ->add('on_date', ChillDateType::class, [ + 'data' => new \DateTime('now'), + ]) + ; } /** @@ -59,8 +66,9 @@ class CompositionFilter implements FilterInterface ); } - return ['Filtered by composition: only %compositions%', [ - '%compositions%' => implode(", ou ", $compositions) + return ['Filtered by composition: only %compositions% on %ondate%', [ + '%compositions%' => implode(", ou ", $compositions), + '%ondate%' => $data['on_date']->format('d-m-Y') ]]; } @@ -79,7 +87,16 @@ class CompositionFilter implements FilterInterface { $where = $qb->getDQLPart('where'); - $clause = $qb->expr()->in('composition.householdCompositionType', ':compositions'); + $clause = $qb->expr()->andX( + $qb->expr()->in('composition.householdCompositionType', ':compositions'), + $qb->expr()->andX( + $qb->expr()->lte('composition.startDate', ':ondate'), + $qb->expr()->orX( + $qb->expr()->gt('composition.endDate', ':ondate'), + $qb->expr()->isNull('composition.endDate') + ) + ) + ); if ($where instanceof Andx) { $where->add($clause); @@ -89,6 +106,7 @@ class CompositionFilter implements FilterInterface $qb->add('where', $where); $qb->setParameter('compositions', $data['accepted_composition']); + $qb->setParameter('ondate', $data['on_date'], Types::DATE_MUTABLE); } /** diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index a19150477..f46bb1750 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -532,7 +532,7 @@ is not specified: La date d'échéance n'est pas spécifiée Filter by composition: Filtrer par composition familiale Accepted composition: Composition familiale -"Filtered by composition: only %compositions%": "Filtré par composition familiale: uniquement %compositions%" +"Filtered by composition: only %compositions% on %ondate%": "Filtré par composition familiale: uniquement %compositions%, en date du %ondate%" Group by composition: Grouper par composition familiale ## aggregators