From 9db0011b2ee87526e57e5963048f6390f2d54daf Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 27 Sep 2023 11:53:12 +0200 Subject: [PATCH] [export] fix acp ReferrerScopeAggregator query + unit test TO CHECK: logic in alterQuery. calc_date is same for 2 histories ? --- .../Export/Aggregator/JobAggregator.php | 1 + .../Export/Aggregator/ScopeAggregator.php | 1 + .../Export/Filter/JobFilter.php | 1 + .../Export/Filter/ScopeFilter.php | 1 + .../ReferrerScopeAggregator.php | 73 ++++++++++++------- .../ReferrerScopeAggregatorTest.php | 1 - 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php b/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php index 4de1f863b..6a7d4cf5d 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php +++ b/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php @@ -72,6 +72,7 @@ final readonly class JobAggregator implements AggregatorInterface { $builder->add('job_at', PickRollingDateType::class, [ 'label' => 'export.calendar.agent_job.Calc date', + 'required' => true, ]); } diff --git a/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php b/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php index 9e395de80..f7c71951e 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php +++ b/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php @@ -74,6 +74,7 @@ final readonly class ScopeAggregator implements AggregatorInterface { $builder->add('scope_at', PickRollingDateType::class, [ 'label' => 'export.calendar.agent_scope.Calc date', + 'required' => true, ]); } public function getFormDefaultData(): array diff --git a/src/Bundle/ChillCalendarBundle/Export/Filter/JobFilter.php b/src/Bundle/ChillCalendarBundle/Export/Filter/JobFilter.php index ae36fe2f0..da0cb5225 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Filter/JobFilter.php +++ b/src/Bundle/ChillCalendarBundle/Export/Filter/JobFilter.php @@ -85,6 +85,7 @@ class JobFilter implements FilterInterface ]) ->add('job_at', PickRollingDateType::class, [ 'label' => 'export.calendar.agent_job.Calc date', + 'required' => true, ]); } public function getFormDefaultData(): array diff --git a/src/Bundle/ChillCalendarBundle/Export/Filter/ScopeFilter.php b/src/Bundle/ChillCalendarBundle/Export/Filter/ScopeFilter.php index 30f732f9b..e89f00b52 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Filter/ScopeFilter.php +++ b/src/Bundle/ChillCalendarBundle/Export/Filter/ScopeFilter.php @@ -80,6 +80,7 @@ class ScopeFilter implements FilterInterface ]) ->add('scope_at', PickRollingDateType::class, [ 'label' => 'export.calendar.agent_scope.Calc date', + 'required' => true, ]); } public function getFormDefaultData(): array diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php index 7810df3f8..801e5d799 100644 --- a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators; +use Chill\MainBundle\Entity\User\UserScopeHistory; use Chill\MainBundle\Export\AggregatorInterface; use Chill\MainBundle\Form\Type\PickRollingDateType; use Chill\MainBundle\Repository\ScopeRepositoryInterface; @@ -18,15 +19,20 @@ use Chill\MainBundle\Service\RollingDate\RollingDate; use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Export\Declarations; +use Doctrine\ORM\Query\Expr; use Doctrine\ORM\QueryBuilder; use LogicException; use Symfony\Component\Form\FormBuilderInterface; -class ReferrerScopeAggregator implements AggregatorInterface +readonly class ReferrerScopeAggregator implements AggregatorInterface { - private const SCOPE_KEY = 'acp_agg_refscope_user_history_ref_scope_name'; + private const PREFIX = 'acp_agg_referrer_scope'; - public function __construct(private readonly ScopeRepositoryInterface $scopeRepository, private readonly TranslatableStringHelperInterface $translatableStringHelper, private readonly RollingDateConverterInterface $rollingDateConverter) {} + public function __construct( + private ScopeRepositoryInterface $scopeRepository, + private TranslatableStringHelperInterface $translatableStringHelper, + private RollingDateConverterInterface $rollingDateConverter + ) {} public function addRole(): ?string { @@ -35,48 +41,59 @@ class ReferrerScopeAggregator implements AggregatorInterface public function alterQuery(QueryBuilder $qb, $data) { - $userHistory = 'acp_agg_refscope_user_history'; - $ref = 'acp_agg_refscope_user_history_ref'; - $scopeName = self::SCOPE_KEY; - $dateCalc = 'acp_agg_refscope_user_history_date_calc'; + $p = self::PREFIX; $qb - ->leftJoin('acp.userHistories', $userHistory) - ->leftJoin($userHistory . '.user', $ref) + ->leftJoin("acp.userHistories", "{$p}_userHistory") + ->leftJoin("{$p}_userHistory.user", "{$p}_user") + ->leftJoin( + UserScopeHistory::class, + "{$p}_scopeHistory", + Expr\Join::WITH, + $qb->expr()->eq("{$p}_scopeHistory.user", "{$p}_user") + ) ->andWhere( $qb->expr()->orX( - $qb->expr()->isNull($userHistory), + $qb->expr()->isNull("{$p}_userHistory"), $qb->expr()->andX( - $qb->expr()->lte($userHistory . '.startDate', ':' . $dateCalc), + $qb->expr()->lte("{$p}_userHistory.startDate", ":{$p}_date_calc"), $qb->expr()->orX( - $qb->expr()->isNull($userHistory . '.endDate'), - $qb->expr()->gt($userHistory . '.endDate', ':' . $dateCalc) + $qb->expr()->isNull("{$p}_userHistory.endDate"), + $qb->expr()->gt("{$p}_userHistory.endDate", ":{$p}_date_calc") ) ) ) ) + ->andWhere( + $qb->expr()->andX( + $qb->expr()->lte("{$p}_scopeHistory.startDate", ":{$p}_date_calc"), + $qb->expr()->orX( + $qb->expr()->isNull("{$p}_scopeHistory.endDate"), + $qb->expr()->gt("{$p}_scopeHistory.endDate", ":{$p}_date_calc") + ) + ) + ) ->setParameter( - $dateCalc, + "{$p}_date_calc", $this->rollingDateConverter->convert($data['date_calc']) - ); - - // add groups - $qb - ->addSelect('IDENTITY(' . $ref . '.mainScope) AS ' . $scopeName) - ->addGroupBy($scopeName); + ) + ->addSelect("IDENTITY({$p}_scopeHistory.scope) AS {$p}_scope_name") + ->addGroupBy("{$p}_scope_name"); } - public function applyOn() + public function applyOn(): string { return Declarations::ACP_TYPE; } public function buildForm(FormBuilderInterface $builder) { - $builder->add('date_calc', PickRollingDateType::class, [ - 'label' => 'export.aggregator.course.by_user_scope.Computation date for referrer', - 'required' => true, - ]); + $builder + ->add('date_calc', PickRollingDateType::class, [ + 'label' => 'export.aggregator.course.by_user_scope.Computation date for referrer', + 'required' => true, + ]) + ; } public function getFormDefaultData(): array { @@ -104,12 +121,12 @@ class ReferrerScopeAggregator implements AggregatorInterface }; } - public function getQueryKeys($data) + public function getQueryKeys($data): array { - return [self::SCOPE_KEY]; + return [self::PREFIX . '_scope_name']; } - public function getTitle() + public function getTitle(): string { return 'export.aggregator.course.by_user_scope.Group course by referrer\'s scope'; } diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php index 6e17b2059..c69e775be 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php @@ -71,7 +71,6 @@ final class ReferrerScopeAggregatorTest extends AbstractAggregatorTest $em->createQueryBuilder() ->select('count(acp.id)') ->from(AccompanyingPeriod::class, 'acp') - ->join('acp.scopes', 'acpscope'), ]; } }