From aeb0d5eab851674b9deb2b85d4b7dfd60ebfc740 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 27 Sep 2023 16:13:11 +0200 Subject: [PATCH] [export] fix acpw socialWork agent ScopeFilter query + unit test (partial) --- .../UserJobFilter.php | 2 +- .../UserScopeFilter.php | 2 +- .../Filter/SocialWorkFilters/ScopeFilter.php | 86 ++++++++++++------- .../SocialWorkFilters/ScopeFilterTest.php | 4 +- .../translations/messages.fr.yml | 7 +- 5 files changed, 65 insertions(+), 36 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php index 4f0d368d5..65acb56e5 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserJobFilter.php @@ -120,7 +120,7 @@ class UserJobFilter implements FilterInterface ]; } - public function getTitle():string + public function getTitle(): string { return 'export.filter.course.by_user_job.Filter by user job'; } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserScopeFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserScopeFilter.php index 38450a1a0..d530e0bd2 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserScopeFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserScopeFilter.php @@ -121,7 +121,7 @@ class UserScopeFilter implements FilterInterface ]; } - public function getTitle():string + public function getTitle(): string { return 'export.filter.course.by_user_scope.Filter by user scope'; } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/ScopeFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/ScopeFilter.php index f25897a7e..5f15ccbaa 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/ScopeFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/SocialWorkFilters/ScopeFilter.php @@ -12,19 +12,28 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters; use Chill\MainBundle\Entity\Scope; +use Chill\MainBundle\Entity\User\UserScopeHistory; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Form\Type\PickRollingDateType; +use Chill\MainBundle\Service\RollingDate\RollingDate; +use Chill\MainBundle\Service\RollingDate\RollingDateConverter; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; -use Doctrine\ORM\Query\Expr\Andx; +use Doctrine\ORM\Query\Expr; use Doctrine\ORM\QueryBuilder; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Contracts\Translation\TranslatorInterface; -use function in_array; class ScopeFilter implements FilterInterface { - public function __construct(protected TranslatorInterface $translator, private readonly TranslatableStringHelper $translatableStringHelper) {} + private const PREFIX = 'acp_work_action_filter_user_scope'; + + public function __construct( + private readonly RollingDateConverter $rollingDateConverter, + protected TranslatorInterface $translator, + private readonly TranslatableStringHelper $translatableStringHelper + ) {} public function addRole(): ?string { @@ -33,21 +42,32 @@ class ScopeFilter implements FilterInterface public function alterQuery(QueryBuilder $qb, $data) { - if (!in_array('acpwuser', $qb->getAllAliases(), true)) { - $qb->join('acpw.referrers', 'acpwuser'); - } + $p = self::PREFIX; - $where = $qb->getDQLPart('where'); - $clause = $qb->expr()->in('acpwuser.mainScope', ':scope'); - - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter('scope', $data['scope']); + $qb + ->leftJoin("acpw.referrers", "{$p}_user") + ->leftJoin( + UserScopeHistory::class, + "{$p}_history", + Expr\Join::WITH, + $qb->expr()->eq("{$p}_history.user", "{$p}_user") + ) + ->andWhere( + $qb->expr()->andX( + $qb->expr()->lte("{$p}_history.startDate", ":{$p}_at"), + $qb->expr()->orX( + $qb->expr()->isNull("{$p}_history.endDate"), + $qb->expr()->gt("{$p}_history.endDate", ":{$p}_at") + ) + ) + ) + ->andWhere( + $qb->expr()->in("{$p}_history.scope", ":{$p}_scope") + ) + ->setParameters([ + ["{$p}_scope", $data["scope"]], + ["{$p}_at", $this->rollingDateConverter->convert($data['scope_at'])] + ]); } public function applyOn() @@ -57,21 +77,27 @@ class ScopeFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('scope', EntityType::class, [ - 'class' => Scope::class, - 'choice_label' => fn (Scope $s) => $this->translatableStringHelper->localize( - $s->getName() - ), - 'multiple' => true, - 'expanded' => true, - ]); + $builder + ->add('scope', EntityType::class, [ + 'class' => Scope::class, + 'choice_label' => fn (Scope $s) => $this->translatableStringHelper->localize( + $s->getName() + ), + 'multiple' => true, + 'expanded' => true, + ]) + ->add('scope_at', PickRollingDateType::class, [ + 'label' => 'export.filter.work.by_user_scope.Calc date', + 'required' => true, + ]) + ; } public function getFormDefaultData(): array { - return []; + return ['scope_at' => new RollingDate(RollingDate::T_TODAY)]; } - public function describeAction($data, $format = 'string') + public function describeAction($data, $format = 'string'): array { $scopes = []; @@ -81,13 +107,13 @@ class ScopeFilter implements FilterInterface ); } - return ['Filtered by treating agent scope: only %scopes%', [ + return ['export.filter.work.by_user_scope.Filtered by treating agent scope: only %scopes%', [ '%scopes%' => implode(', ', $scopes), ]]; } - public function getTitle() + public function getTitle(): string { - return 'Filter by treating agent scope'; + return 'export.filter.work.by_user_scope.Filter by treating agent scope'; } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/SocialWorkFilters/ScopeFilterTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/SocialWorkFilters/ScopeFilterTest.php index 1ff6c6d6e..f2ffce2e4 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/SocialWorkFilters/ScopeFilterTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/SocialWorkFilters/ScopeFilterTest.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Tests\Export\Filter\SocialWorkFilters; use Chill\MainBundle\Entity\Scope; +use Chill\MainBundle\Service\RollingDate\RollingDate; use Chill\MainBundle\Test\Export\AbstractFilterTest; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; use Chill\PersonBundle\Export\Filter\SocialWorkFilters\ScopeFilter; @@ -48,7 +49,8 @@ final class ScopeFilterTest extends AbstractFilterTest return [ [ - 'scope' => $scopes + 'scope' => $scopes, + 'scope_at' => new RollingDate(RollingDate::T_FIXED_DATE, \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01')) ] ]; } diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 9f4915f78..bfecd6559 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -589,9 +589,6 @@ Filter by current evaluations: Filtrer les évaluations en cours 'Filtered by geographic unit: computed at %date%, only in %units%': 'Filtré par unité géographique: adresse le %date%, seulement les unités %units%' ## social actions filters/aggr -Filter by treating agent scope: Filtrer les actions par service de l'agent traitant -"Filtered by treating agent scope: only %scopes%": "Filtré par service de l'agent traitant: uniquement %scopes%" - Filter by scope: Filtrer par service Filter by treating agent: Filtrer les actions par agent traitant @@ -1160,6 +1157,10 @@ export: Filter by treating agent job: Filtrer les actions par métier de l'agent traitant "Filtered by treating agent job: only %jobs%": "Filtré par métier de l'agent traitant: uniquement %jobs%" Calc date: Date de calcul du métier de l'agent traitant + by_user_scope: + Filter by treating agent scope: Filtrer les actions par service de l'agent traitant + "Filtered by treating agent scope: only %scopes%": "Filtré par service de l'agent traitant: uniquement %scopes%" + Calc date: Date de calcul du service de l'agent traitant list: person_with_acp: