[export] fix calendar scopeAggregator query + unit test

This commit is contained in:
Mathieu Jaumotte 2023-09-26 16:00:42 +02:00
parent 28583f4193
commit f18ee2383c
4 changed files with 50 additions and 17 deletions

View File

@ -26,7 +26,7 @@ use Symfony\Component\Form\FormBuilderInterface;
final readonly class JobAggregator implements AggregatorInterface final readonly class JobAggregator implements AggregatorInterface
{ {
private const PREFIX = 'cal_agg'; private const PREFIX = 'cal_agg_job';
public function __construct( public function __construct(
private RollingDateConverterInterface $rollingDateConverter, private RollingDateConverterInterface $rollingDateConverter,
@ -50,12 +50,12 @@ final readonly class JobAggregator implements AggregatorInterface
) )
->leftJoin( ->leftJoin(
UserJobHistory::class, UserJobHistory::class,
"{$p}_ujh", "{$p}_history",
Expr\Join::WITH, 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)") ->andWhere("{$p}_history.startDate <= :{$p}_at AND ({$p}_history.endDate IS NULL OR {$p}_history.endDate > :{$p}_at)")
->addSelect("IDENTITY({$p}_ujh.job) AS {$p}_select") ->addSelect("IDENTITY({$p}_history.job) AS {$p}_select")
->setParameter( ->setParameter(
"{$p}_at", "{$p}_at",
$this->rollingDateConverter->convert($data['job_at']) $this->rollingDateConverter->convert($data['job_at'])
@ -71,7 +71,7 @@ final readonly class JobAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
$builder->add('job_at', PickRollingDateType::class, [ $builder->add('job_at', PickRollingDateType::class, [
'label' => 'export.aggregator.calendar.Calc date', 'label' => 'export.aggregator.calendar.agent_job.Calc date',
]); ]);
} }

View File

@ -12,17 +12,26 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\Export\Aggregator; namespace Chill\CalendarBundle\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations; use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\AggregatorInterface; use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Repository\ScopeRepository; use Chill\MainBundle\Repository\ScopeRepository;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure; use Closure;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Exp;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use function in_array; use function in_array;
final readonly class ScopeAggregator implements AggregatorInterface final readonly class ScopeAggregator implements AggregatorInterface
{ {
private const PREFIX = "cal_agg_scope";
public function __construct( public function __construct(
private RollingDateConverterInterface $rollingDateConverter,
private ScopeRepository $scopeRepository, private ScopeRepository $scopeRepository,
private TranslatableStringHelper $translatableStringHelper private TranslatableStringHelper $translatableStringHelper
) {} ) {}
@ -34,12 +43,25 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
if (!in_array('caluser', $qb->getAllAliases(), true)) { $p = self::PREFIX;
$qb->join('cal.mainUser', 'caluser');
}
$qb->addSelect('IDENTITY(caluser.mainScope) as scope_aggregator'); $qb
$qb->addGroupBy('scope_aggregator'); ->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 public function applyOn(): string
@ -49,11 +71,13 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder) 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 public function getFormDefaultData(): array
{ {
return []; return ['scope_at' => new RollingDate(RollingDate::T_TODAY)];
} }
public function getLabels($key, array $values, $data): Closure public function getLabels($key, array $values, $data): Closure
@ -67,7 +91,9 @@ final readonly class ScopeAggregator implements AggregatorInterface
return ''; return '';
} }
$s = $this->scopeRepository->find($value); if (null === $s = $this->scopeRepository->find($value)) {
return '';
}
return $this->translatableStringHelper->localize( return $this->translatableStringHelper->localize(
$s->getName() $s->getName()
@ -77,7 +103,7 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function getQueryKeys($data): array public function getQueryKeys($data): array
{ {
return ['scope_aggregator']; return [self::PREFIX . '_select'];
} }
public function getTitle(): string public function getTitle(): string

View File

@ -20,6 +20,7 @@ namespace Chill\CalendarBundle\Tests\Export\Aggregator;
use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Export\Aggregator\ScopeAggregator; use Chill\CalendarBundle\Export\Aggregator\ScopeAggregator;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Test\Export\AbstractAggregatorTest; use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -46,7 +47,9 @@ final class ScopeAggregatorTest extends AbstractAggregatorTest
public function getFormData(): array public function getFormData(): array
{ {
return [ return [
[], [
'scope_at' => new RollingDate(RollingDate::T_FIXED_DATE, \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01')),
],
]; ];
} }

View File

@ -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 month and year: Grouper les rendez-vous par mois et année
Group calendars by urgency: Grouper les rendez-vous par urgent ou non 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 Scope: Service
Job: Métier Job: Métier