From 6cdb3033dbba36887b2524677d354602cb80352c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 23 Sep 2022 21:51:26 +0200 Subject: [PATCH] [export][person] Fixed: filter per person's age: use calculatio non year interval --- .../Export/Filter/PersonFilters/AgeFilter.php | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/AgeFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/AgeFilter.php index bada59a8d..eb83f3ff0 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/AgeFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/PersonFilters/AgeFilter.php @@ -34,17 +34,20 @@ class AgeFilter implements ExportElementValidatedInterface, FilterInterface $where = $qb->getDQLPart('where'); $min = null !== $data['min_age'] ? $data['min_age'] : 0; - $max = null !== $data['max_age'] ? $data['max_age'] : 200; + $max = null !== $data['max_age'] ? $data['max_age'] : 3000; $calc = $data['date_calc']; + $minDate = $calc->sub(new \DateInterval('P' . $max . 'Y')); + $maxDate = $calc->sub(new \DateInterval('P' . $min . 'Y')); + $clause = $qb->expr()->andX( $qb->expr()->gte( - 'DATE_DIFF(:calc_date, person.birthdate)/365', - ':min_age' + 'person.birthdate', + ':min_date' ), $qb->expr()->lte( - 'DATE_DIFF(:calc_date, person.birthdate)/365', - ':max_age' + 'person.birthdate', + ':max_date' ) ); @@ -55,9 +58,8 @@ class AgeFilter implements ExportElementValidatedInterface, FilterInterface } $qb->add('where', $where); - $qb->setParameter('min_age', $min); - $qb->setParameter('max_age', $max); - $qb->setParameter('calc_date', $calc); + $qb->setParameter('min_date', $minDate); + $qb->setParameter('max_date', $maxDate); } public function applyOn() @@ -77,7 +79,8 @@ class AgeFilter implements ExportElementValidatedInterface, FilterInterface $builder->add('date_calc', ChillDateType::class, [ 'label' => 'Calculate age in relation to this date', - 'data' => new DateTime('now'), + 'data' => new \DateTimeImmutable('now'), + 'input' => 'datetime_immutable' ]); }