diff --git a/.changes/unreleased/Feature-20240617-171414.yaml b/.changes/unreleased/Feature-20240617-171414.yaml new file mode 100644 index 000000000..e6559f61f --- /dev/null +++ b/.changes/unreleased/Feature-20240617-171414.yaml @@ -0,0 +1,5 @@ +kind: Feature +body: '[export] add date range on "group course by jobs''s referrer"' +time: 2024-06-17T17:14:14.737439251+02:00 +custom: + Issue: "282" diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregator.php index 0bf536929..a46fdc5b4 100644 --- a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregator.php +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregator.php @@ -13,20 +13,25 @@ namespace Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators; use Chill\MainBundle\Entity\User\UserJobHistory; use Chill\MainBundle\Export\AggregatorInterface; +use Chill\MainBundle\Export\DataTransformerInterface; +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 Chill\PersonBundle\Export\Declarations; use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; -final readonly class UserJobAggregator implements AggregatorInterface +final readonly class UserJobAggregator implements AggregatorInterface, DataTransformerInterface { private const PREFIX = 'acp_agg_user_job'; public function __construct( private UserJobRepository $jobRepository, - private TranslatableStringHelper $translatableStringHelper + private TranslatableStringHelper $translatableStringHelper, + private RollingDateConverterInterface $rollingDateConverter, ) { } @@ -52,6 +57,10 @@ final readonly class UserJobAggregator implements AggregatorInterface $qb->expr()->isNull("{$p}_userHistory.endDate"), $qb->expr()->lt('COALESCE(acp.closingDate, CURRENT_TIMESTAMP())', "{$p}_userHistory.endDate") ) + ), + $qb->expr()->andX( + "{$p}_userHistory.startDate <= :{$p}_endDate", + "COALESCE({$p}_userHistory.endDate, CURRENT_TIMESTAMP()) > :{$p}_startDate" ) ) ) @@ -67,9 +76,15 @@ final readonly class UserJobAggregator implements AggregatorInterface $qb->expr()->isNull("{$p}_jobHistory.endDate"), $qb->expr()->gt("{$p}_jobHistory.endDate", "{$p}_userHistory.startDate") ) + ), + $qb->expr()->andX( + "{$p}_jobHistory.startDate <= :{$p}_endDate", + "COALESCE({$p}_jobHistory.endDate, CURRENT_TIMESTAMP()) > :{$p}_startDate" ) ) ) + ->setParameter("{$p}_startDate", $this->rollingDateConverter->convert($data['start_date'])) + ->setParameter("{$p}_endDate", $this->rollingDateConverter->convert($data['end_date'])) ->addSelect("IDENTITY({$p}_jobHistory.job) AS {$p}_select") ->addGroupBy("{$p}_select"); } @@ -81,11 +96,34 @@ final readonly class UserJobAggregator implements AggregatorInterface public function buildForm(FormBuilderInterface $builder) { + $builder + ->add('start_date', PickRollingDateType::class, [ + 'label' => 'export.aggregator.course.by_referrer_job.Referrer and job after', + 'required' => true, + ]) + ->add('end_date', PickRollingDateType::class, [ + 'label' => 'export.aggregator.course.by_referrer_job.Until', + 'required' => true, + ]); } public function getFormDefaultData(): array { - return []; + return [ + 'start_date' => new RollingDate(RollingDate::T_YEAR_CURRENT_START), + 'end_date' => new RollingDate(RollingDate::T_TODAY), + ]; + } + + public function transformData(?array $before): array + { + $default = $this->getFormDefaultData(); + $data = []; + + $data['start_date'] = $before['start_date'] ?? new RollingDate(RollingDate::T_FIXED_DATE, new \DateTimeImmutable('1970-01-01')); + $data['end_date'] = $before['end_date'] ?? $default['end_date']; + + return $data; } public function getLabels($key, array $values, $data) diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregatorTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregatorTest.php index a62886152..d6bd6cc48 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregatorTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/UserJobAggregatorTest.php @@ -33,7 +33,39 @@ final class UserJobAggregatorTest extends AbstractAggregatorTest $this->aggregator = self::$container->get('chill.person.export.aggregator_referrer_job'); } - public function getAggregator() + /** + * @dataProvider provideBeforeData + */ + public function testDataTransformer(?array $before, array $expected): void + { + $actual = $this->getAggregator()->transformData($before); + + self::assertEqualsCanonicalizing(array_keys($expected), array_keys($actual)); + foreach (['start_date', 'end_date'] as $key) { + self::assertInstanceOf(RollingDate::class, $actual[$key]); + self::assertEquals($expected[$key]->getRoll(), $actual[$key]->getRoll(), "Check that the roll is the same for {$key}"); + } + } + + public function provideBeforeData(): iterable + { + yield [ + null, + ['start_date' => new RollingDate(RollingDate::T_FIXED_DATE, new \DateTimeImmutable('1970-01-01')), 'end_date' => new RollingDate(RollingDate::T_TODAY)], + ]; + + yield [ + [], + ['start_date' => new RollingDate(RollingDate::T_FIXED_DATE, new \DateTimeImmutable('1970-01-01')), 'end_date' => new RollingDate(RollingDate::T_TODAY)], + ]; + + yield [ + ['start_date' => new RollingDate(RollingDate::T_WEEK_CURRENT_START), 'end_date' => new RollingDate(RollingDate::T_TODAY)], + ['start_date' => new RollingDate(RollingDate::T_WEEK_CURRENT_START), 'end_date' => new RollingDate(RollingDate::T_TODAY)], + ]; + } + + public function getAggregator(): UserJobAggregator { return $this->aggregator; } @@ -41,9 +73,7 @@ final class UserJobAggregatorTest extends AbstractAggregatorTest public function getFormData(): array { return [ - [ - 'job_at' => new RollingDate(RollingDate::T_FIXED_DATE, \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01')), - ], + ['start_date' => new RollingDate(RollingDate::T_WEEK_CURRENT_START), 'end_date' => new RollingDate(RollingDate::T_TODAY)], ]; } diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index d3922de26..60f29acdf 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -1064,6 +1064,9 @@ export: by_referrer_scope: Referrer and scope after: Référent et service après le Until: Jusqu'au + by_referrer_job: + Referrer and job after: Référent et métier après le + Until: Jusqu'au by_user_scope: Group course by referrer's scope: Grouper les parcours par service du référent Referrer's scope: Service du référent de parcours