[export] fix acp ReferrerScopeAggregator query + unit test

TO CHECK: logic in alterQuery. calc_date is same for 2 histories ?
This commit is contained in:
Mathieu Jaumotte 2023-09-27 11:53:12 +02:00
parent 3f4a42adb2
commit 9db0011b2e
6 changed files with 49 additions and 29 deletions

View File

@ -72,6 +72,7 @@ final readonly class JobAggregator implements AggregatorInterface
{
$builder->add('job_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_job.Calc date',
'required' => true,
]);
}

View File

@ -74,6 +74,7 @@ final readonly class ScopeAggregator implements AggregatorInterface
{
$builder->add('scope_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_scope.Calc date',
'required' => true,
]);
}
public function getFormDefaultData(): array

View File

@ -85,6 +85,7 @@ class JobFilter implements FilterInterface
])
->add('job_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_job.Calc date',
'required' => true,
]);
}
public function getFormDefaultData(): array

View File

@ -80,6 +80,7 @@ class ScopeFilter implements FilterInterface
])
->add('scope_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_scope.Calc date',
'required' => true,
]);
}
public function getFormDefaultData(): array

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators;
use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Repository\ScopeRepositoryInterface;
@ -18,15 +19,20 @@ use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use LogicException;
use Symfony\Component\Form\FormBuilderInterface;
class ReferrerScopeAggregator implements AggregatorInterface
readonly class ReferrerScopeAggregator implements AggregatorInterface
{
private const SCOPE_KEY = 'acp_agg_refscope_user_history_ref_scope_name';
private const PREFIX = 'acp_agg_referrer_scope';
public function __construct(private readonly ScopeRepositoryInterface $scopeRepository, private readonly TranslatableStringHelperInterface $translatableStringHelper, private readonly RollingDateConverterInterface $rollingDateConverter) {}
public function __construct(
private ScopeRepositoryInterface $scopeRepository,
private TranslatableStringHelperInterface $translatableStringHelper,
private RollingDateConverterInterface $rollingDateConverter
) {}
public function addRole(): ?string
{
@ -35,48 +41,59 @@ class ReferrerScopeAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data)
{
$userHistory = 'acp_agg_refscope_user_history';
$ref = 'acp_agg_refscope_user_history_ref';
$scopeName = self::SCOPE_KEY;
$dateCalc = 'acp_agg_refscope_user_history_date_calc';
$p = self::PREFIX;
$qb
->leftJoin('acp.userHistories', $userHistory)
->leftJoin($userHistory . '.user', $ref)
->leftJoin("acp.userHistories", "{$p}_userHistory")
->leftJoin("{$p}_userHistory.user", "{$p}_user")
->leftJoin(
UserScopeHistory::class,
"{$p}_scopeHistory",
Expr\Join::WITH,
$qb->expr()->eq("{$p}_scopeHistory.user", "{$p}_user")
)
->andWhere(
$qb->expr()->orX(
$qb->expr()->isNull($userHistory),
$qb->expr()->isNull("{$p}_userHistory"),
$qb->expr()->andX(
$qb->expr()->lte($userHistory . '.startDate', ':' . $dateCalc),
$qb->expr()->lte("{$p}_userHistory.startDate", ":{$p}_date_calc"),
$qb->expr()->orX(
$qb->expr()->isNull($userHistory . '.endDate'),
$qb->expr()->gt($userHistory . '.endDate', ':' . $dateCalc)
$qb->expr()->isNull("{$p}_userHistory.endDate"),
$qb->expr()->gt("{$p}_userHistory.endDate", ":{$p}_date_calc")
)
)
)
)
->andWhere(
$qb->expr()->andX(
$qb->expr()->lte("{$p}_scopeHistory.startDate", ":{$p}_date_calc"),
$qb->expr()->orX(
$qb->expr()->isNull("{$p}_scopeHistory.endDate"),
$qb->expr()->gt("{$p}_scopeHistory.endDate", ":{$p}_date_calc")
)
)
)
->setParameter(
$dateCalc,
"{$p}_date_calc",
$this->rollingDateConverter->convert($data['date_calc'])
);
// add groups
$qb
->addSelect('IDENTITY(' . $ref . '.mainScope) AS ' . $scopeName)
->addGroupBy($scopeName);
)
->addSelect("IDENTITY({$p}_scopeHistory.scope) AS {$p}_scope_name")
->addGroupBy("{$p}_scope_name");
}
public function applyOn()
public function applyOn(): string
{
return Declarations::ACP_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('date_calc', PickRollingDateType::class, [
$builder
->add('date_calc', PickRollingDateType::class, [
'label' => 'export.aggregator.course.by_user_scope.Computation date for referrer',
'required' => true,
]);
])
;
}
public function getFormDefaultData(): array
{
@ -104,12 +121,12 @@ class ReferrerScopeAggregator implements AggregatorInterface
};
}
public function getQueryKeys($data)
public function getQueryKeys($data): array
{
return [self::SCOPE_KEY];
return [self::PREFIX . '_scope_name'];
}
public function getTitle()
public function getTitle(): string
{
return 'export.aggregator.course.by_user_scope.Group course by referrer\'s scope';
}

View File

@ -71,7 +71,6 @@ final class ReferrerScopeAggregatorTest extends AbstractAggregatorTest
$em->createQueryBuilder()
->select('count(acp.id)')
->from(AccompanyingPeriod::class, 'acp')
->join('acp.scopes', 'acpscope'),
];
}
}