diff --git a/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php b/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php index 139de7b17..fa6be1169 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php +++ b/src/Bundle/ChillCalendarBundle/Export/Aggregator/JobAggregator.php @@ -12,17 +12,27 @@ declare(strict_types=1); namespace Chill\CalendarBundle\Export\Aggregator; use Chill\CalendarBundle\Export\Declarations; +use Chill\MainBundle\Entity\User\UserJobHistory; use Chill\MainBundle\Export\AggregatorInterface; +use Chill\MainBundle\Form\Type\PickRollingDateType; use Chill\MainBundle\Repository\UserJobRepository; +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 Symfony\Component\Form\FormBuilderInterface; -use function in_array; final readonly class JobAggregator implements AggregatorInterface { - public function __construct(private UserJobRepository $jobRepository, private TranslatableStringHelper $translatableStringHelper) {} + private const PREFIX = 'cal_agg'; + + public function __construct( + private RollingDateConverterInterface $rollingDateConverter, + private UserJobRepository $jobRepository, + private TranslatableStringHelper $translatableStringHelper + ) {} public function addRole(): ?string { @@ -31,12 +41,26 @@ final readonly class JobAggregator 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.userJob) as job_aggregator'); - $qb->addGroupBy('job_aggregator'); + $qb + ->leftJoin( + 'cal.mainUser', + "{$p}_user" + ) + ->leftJoin( + UserJobHistory::class, + "{$p}_ujh", + Expr\Join::WITH, + $qb->expr()->eq("{$p}_ujh.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") + ->setParameter( + "{$p}_at", + $this->rollingDateConverter->convert($data['job_at']) + ) + ->addGroupBy("{$p}_select"); } public function applyOn(): string @@ -46,11 +70,14 @@ final readonly class JobAggregator implements AggregatorInterface public function buildForm(FormBuilderInterface $builder) { - // no form + $builder->add('job_at', PickRollingDateType::class, [ + 'label' => 'export.aggregator.calendar.Calc date', + ]); } + public function getFormDefaultData(): array { - return []; + return ['job_at' => new RollingDate(RollingDate::T_TODAY)]; } public function getLabels($key, array $values, $data): Closure @@ -64,7 +91,9 @@ final readonly class JobAggregator implements AggregatorInterface return ''; } - $j = $this->jobRepository->find($value); + if (null === $j = $this->jobRepository->find($value)) { + return ''; + } return $this->translatableStringHelper->localize( $j->getLabel() @@ -74,7 +103,7 @@ final readonly class JobAggregator implements AggregatorInterface public function getQueryKeys($data): array { - return ['job_aggregator']; + return [self::PREFIX . '_select']; } public function getTitle(): string diff --git a/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/JobAggregatorTest.php b/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/JobAggregatorTest.php index 8dda425f7..838f8b988 100644 --- a/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/JobAggregatorTest.php +++ b/src/Bundle/ChillCalendarBundle/Tests/Export/Aggregator/JobAggregatorTest.php @@ -20,6 +20,7 @@ namespace Chill\CalendarBundle\Tests\Export\Aggregator; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Export\Aggregator\JobAggregator; +use Chill\MainBundle\Service\RollingDate\RollingDate; use Chill\MainBundle\Test\Export\AbstractAggregatorTest; use Doctrine\ORM\EntityManagerInterface; @@ -46,7 +47,9 @@ final class JobAggregatorTest extends AbstractAggregatorTest public function getFormData(): array { return [ - [], + [ + 'job_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 c56d7835f..f608bfc12 100644 --- a/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillCalendarBundle/translations/messages.fr.yml @@ -117,6 +117,8 @@ 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 + Scope: Service Job: Métier Location type: Type de localisation