diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerMainCenterAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerMainCenterAggregator.php new file mode 100644 index 000000000..6079b0a1b --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerMainCenterAggregator.php @@ -0,0 +1,143 @@ +leftJoin('acp.userHistories', "{$p}_uh", Join::WITH, $qb->expr()->andX( + $qb->expr()->eq("{$p}_uh.accompanyingPeriod", 'acp.id'), + "OVERLAPSI (acp.openingDate, acp.closingDate), ({$p}_uh.startDate, {$p}_uh.endDate) = TRUE", + "OVERLAPSI (:{$p}_startDate, :{$p}_endDate), ({$p}_uh.startDate, {$p}_uh.endDate) = TRUE" + )) + ->leftJoin("{$p}_uh.user", "{$p}_user") + ->addSelect("IDENTITY({$p}_user.mainCenter) AS {$p}_select") + ->addGroupBy("{$p}_select") + ->setParameter("{$p}_startDate", $this->rollingDateConverter->convert($data['start_date'])) + ->setParameter("{$p}_endDate", $this->rollingDateConverter->convert($data['end_date'])); + } + + public function applyOn(): string + { + return Declarations::ACP_TYPE; + } + + public function buildForm(FormBuilderInterface $builder): void + { + $builder + ->add('start_date', PickRollingDateType::class, [ + 'label' => 'common.after', + 'required' => true, + ]) + ->add('end_date', PickRollingDateType::class, [ + 'label' => 'common.until', + 'required' => true, + ]); + } + + public function getNormalizationVersion(): int + { + return 1; + } + + public function normalizeFormData(array $formData): array + { + return [ + 'start_date' => $formData['start_date']->normalize(), + 'end_date' => $formData['end_date']->normalize(), + ]; + } + + public function denormalizeFormData(array $formData, int $fromVersion): array + { + $default = $this->getFormDefaultData(); + + return [ + 'start_date' => array_key_exists('start_date', $formData) ? RollingDate::fromNormalized($formData['start_date']) : $default['start_date'], + 'end_date' => array_key_exists('end_date', $formData) ? RollingDate::fromNormalized($formData['end_date']) : $default['end_date'], + ]; + } + + public function getFormDefaultData(): array + { + 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(); + + if (null === $before) { + return $default; + } + + return [ + 'start_date' => $before['start_date'] ?? $before['date_calc'] ?? $default['start_date'], + 'end_date' => $before['end_date'] ?? $before['date_calc'] ?? $default['end_date'], + ]; + } + + public function getLabels($key, array $values, $data): callable + { + return function ($value): string { + if ('_header' === $value) { + return 'person.export.period.aggregator.by_referrer_main_center.column_header'; + } + + if (null === $value || '' === $value) { + return ''; + } + + return (string) $this->centerRepository->find((int) $value)?->getName(); + }; + } + + public function getQueryKeys($data): array + { + return [self::P.'_select']; + } + + public function getTitle(): string + { + return 'person.export.period.aggregator.by_referrer_main_center.title'; + } +} diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/ReferrerMainCenterFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/ReferrerMainCenterFilter.php new file mode 100644 index 000000000..3f5fdfdf1 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/ReferrerMainCenterFilter.php @@ -0,0 +1,134 @@ +join('acp.userHistories', self::UH) + ->join(self::UH.'.user', self::UH.'_user') + ->andWhere( + $qb->expr()->andX( + $qb->expr()->lte(self::UH.'.startDate', ':'.self::DATE_PARAM), + $qb->expr()->orX( + $qb->expr()->isNull(self::UH.'.endDate'), + $qb->expr()->gt(self::UH.'.endDate', ':'.self::DATE_PARAM) + ) + ) + ) + ->andWhere('IDENTITY('.self::UH.'_user.mainCenter) IN (:acp_referrer_main_center_filter_centers)') + ->setParameter(self::DATE_PARAM, $this->rollingDateConverter->convert($data['date_calc'])) + ->setParameter('acp_referrer_main_center_filter_centers', array_map( + static fn (Center $c): int => $c->getId(), + $data['centers'] + )); + } + + public function applyOn(): string + { + return Declarations::ACP_TYPE; + } + + public function buildForm(FormBuilderInterface $builder): void + { + $builder + ->add('centers', EntityType::class, [ + 'class' => Center::class, + 'multiple' => true, + 'expanded' => false, + 'choice_label' => static fn (Center $c) => $c->getName(), + 'required' => true, + 'label' => 'common.centers', + ]) + ->add('date_calc', PickRollingDateType::class, [ + 'label' => 'person.export.period.filter.by_referrer_main_center.referrer_since', + 'required' => true, + ]); + } + + public function getNormalizationVersion(): int + { + return 1; + } + + public function normalizeFormData(array $formData): array + { + return [ + 'centers' => array_values(array_map(static fn (Center $c) => $c->getId(), $formData['centers'])), + 'date_calc' => $formData['date_calc']->normalize(), + ]; + } + + public function denormalizeFormData(array $formData, int $fromVersion): array + { + return [ + 'centers' => array_values(array_filter(array_map( + fn (int $id) => $this->centerRepository->find($id), + $formData['centers'] ?? [] + ))), + 'date_calc' => RollingDate::fromNormalized($formData['date_calc']), + ]; + } + + public function getFormDefaultData(): array + { + return [ + 'centers' => [], + 'date_calc' => new RollingDate(RollingDate::T_TODAY), + ]; + } + + public function describeAction($data, ExportGenerationContext $context): array|string + { + $names = array_map(static fn (Center $c) => $c->getName(), $data['centers']); + + return [ + 'person.export.period.filter.by_referrer_main_center.action_%centers%', + ['%centers%' => implode(', ', $names)], + ]; + } + + public function getTitle(): string + { + return 'person.export.period.filter.by_referrer_main_center.title'; + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml b/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml index d488df8d6..b7ab71d40 100644 --- a/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml @@ -104,6 +104,10 @@ services: tags: - { name: chill.export_filter, alias: accompanyingcourse_referrer_filter_between_dates } + Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\ReferrerMainCenterFilter: + tags: + - { name: chill.export_filter, alias: accompanyingcourse_referrer_main_center_filter } + chill.person.export.filter_openbetweendates: class: Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\OpenBetweenDatesFilter tags: @@ -270,3 +274,7 @@ services: Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators\PersonParticipatingAggregator: tags: - { name: chill.export_aggregator, alias: accompanyingcourse_person_part_aggregator } + + Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators\ReferrerMainCenterAggregator: + tags: + - { name: chill.export_aggregator, alias: accompanyingcourse_referrer_main_center_aggregator }