From f18ee2383c200a09eca6e3627d39f8b3d057ba3f Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 26 Sep 2023 16:00:42 +0200 Subject: [PATCH] [export] fix calendar scopeAggregator query + unit test --- .../Export/Aggregator/JobAggregator.php | 12 ++--- .../Export/Aggregator/ScopeAggregator.php | 44 +++++++++++++++---- .../Export/Aggregator/ScopeAggregatorTest.php | 5 ++- .../translations/messages.fr.yml | 6 ++- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php b/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php index fa6be1169..94807d709 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php +++ b/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php @@ -26,7 +26,7 @@ use Symfony\Component\Form\FormBuilderInterface; final readonly class JobAggregator implements AggregatorInterface { - private const PREFIX = 'cal_agg'; + private const PREFIX = 'cal_agg_job'; public function __construct( private RollingDateConverterInterface $rollingDateConverter, @@ -50,12 +50,12 @@ final readonly class JobAggregator implements AggregatorInterface ) ->leftJoin( UserJobHistory::class, - "{$p}_ujh", + "{$p}_history", Expr\Join::WITH, - $qb->expr()->eq("{$p}_ujh.user", "{$p}_user") + $qb->expr()->eq("{$p}_history.user", "{$p}_user") ) - ->andWhere("{$p}_ujh.startDate <= :{$p}_at AND ({$p}_ujh.endDate IS NULL OR {$p}_ujh.endDate > :{$p}_at)") - ->addSelect("IDENTITY({$p}_ujh.job) AS {$p}_select") + ->andWhere("{$p}_history.startDate <= :{$p}_at AND ({$p}_history.endDate IS NULL OR {$p}_history.endDate > :{$p}_at)") + ->addSelect("IDENTITY({$p}_history.job) AS {$p}_select") ->setParameter( "{$p}_at", $this->rollingDateConverter->convert($data['job_at']) @@ -71,7 +71,7 @@ final readonly class JobAggregator implements AggregatorInterface public function buildForm(FormBuilderInterface $builder) { $builder->add('job_at', PickRollingDateType::class, [ - 'label' => 'export.aggregator.calendar.Calc date', + 'label' => 'export.aggregator.calendar.agent_job.Calc date', ]); } diff --git a/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php b/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php index f1daa643c..df68b7c42 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php +++ b/src/Bundle/ChillCalendarBundle/Export/Aggregator/ScopeAggregator.php @@ -12,17 +12,26 @@ declare(strict_types=1); namespace Chill\CalendarBundle\Export\Aggregator; use Chill\CalendarBundle\Export\Declarations; +use Chill\MainBundle\Entity\User\UserScopeHistory; use Chill\MainBundle\Export\AggregatorInterface; +use Chill\MainBundle\Form\Type\PickRollingDateType; use Chill\MainBundle\Repository\ScopeRepository; +use Chill\MainBundle\Service\RollingDate\RollingDate; +use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Closure; +use Doctrine\ORM\Query\Expr; use Doctrine\ORM\QueryBuilder; +use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Exp; use Symfony\Component\Form\FormBuilderInterface; use function in_array; final readonly class ScopeAggregator implements AggregatorInterface { + private const PREFIX = "cal_agg_scope"; + public function __construct( + private RollingDateConverterInterface $rollingDateConverter, private ScopeRepository $scopeRepository, private TranslatableStringHelper $translatableStringHelper ) {} @@ -34,12 +43,25 @@ final readonly class ScopeAggregator implements AggregatorInterface public function alterQuery(QueryBuilder $qb, $data) { - if (!in_array('caluser', $qb->getAllAliases(), true)) { - $qb->join('cal.mainUser', 'caluser'); - } + $p = self::PREFIX; - $qb->addSelect('IDENTITY(caluser.mainScope) as scope_aggregator'); - $qb->addGroupBy('scope_aggregator'); + $qb + ->leftJoin( + "cal.mainUser", + "{$p}_user" + ) + ->leftJoin( + UserScopeHistory::class, + "{$p}_history", + Expr\Join::WITH, + $qb->expr()->eq("{$p}_history.user", "{$p}_user") + ) + ->andWhere("{$p}_history.startDate <= :{$p}_at AND ({$p}_history.endDate IS NULL OR {$p}_history.endDate > :{$p}_at)") + ->addSelect("IDENTITY({$p}_history.scope) AS {$p}_select") + ->setParameter( + "{$p}_at", + $this->rollingDateConverter->convert($data['scope_at'])) + ->addGroupBy("{$p}_select"); } public function applyOn(): string @@ -49,11 +71,13 @@ final readonly class ScopeAggregator implements AggregatorInterface public function buildForm(FormBuilderInterface $builder) { - // no form + $builder->add('scope_at', PickRollingDateType::class, [ + 'label' => 'export.aggregator.calendar.agent_scope.Calc date', + ]); } public function getFormDefaultData(): array { - return []; + return ['scope_at' => new RollingDate(RollingDate::T_TODAY)]; } public function getLabels($key, array $values, $data): Closure @@ -67,7 +91,9 @@ final readonly class ScopeAggregator implements AggregatorInterface return ''; } - $s = $this->scopeRepository->find($value); + if (null === $s = $this->scopeRepository->find($value)) { + return ''; + } return $this->translatableStringHelper->localize( $s->getName() @@ -77,7 +103,7 @@ final readonly class ScopeAggregator implements AggregatorInterface public function getQueryKeys($data): array { - return ['scope_aggregator']; + return [self::PREFIX . '_select']; } public function getTitle(): string diff --git a/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/ScopeAggregatorTest.php b/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/ScopeAggregatorTest.php index c9a2b9e5c..43900d53a 100644 --- a/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/ScopeAggregatorTest.php +++ b/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/ScopeAggregatorTest.php @@ -20,6 +20,7 @@ namespace Chill\CalendarBundle\Tests\Export\Aggregator; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Export\Aggregator\ScopeAggregator; +use Chill\MainBundle\Service\RollingDate\RollingDate; use Chill\MainBundle\Test\Export\AbstractAggregatorTest; use Doctrine\ORM\EntityManagerInterface; @@ -46,7 +47,9 @@ final class ScopeAggregatorTest extends AbstractAggregatorTest public function getFormData(): array { return [ - [], + [ + 'scope_at' => new RollingDate(RollingDate::T_FIXED_DATE, \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01')), + ], ]; } diff --git a/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml b/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml index f608bfc12..20e9604db 100644 --- a/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml @@ -117,7 +117,11 @@ Group calendars by cancel reason: Grouper les rendez-vous par motif d'annulation Group calendars by month and year: Grouper les rendez-vous par mois et année Group calendars by urgency: Grouper les rendez-vous par urgent ou non -export.aggregator.calendar.Calc date: Date de calcul du métier de l'agent +export.aggregator.calendar: + agent_job: + Calc date: Date de calcul du métier de l'agent + agent_scope: + Calc date: Date de calcul du service de l'agent Scope: Service Job: Métier