[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, [ $builder->add('job_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_job.Calc date', '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, [ $builder->add('scope_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_scope.Calc date', 'label' => 'export.calendar.agent_scope.Calc date',
'required' => true,
]); ]);
} }
public function getFormDefaultData(): array public function getFormDefaultData(): array

View File

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

View File

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

View File

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