Refactor filters and aggregators of "acpwusers" for using the acpw referrer history instead

This commit is contained in:
2023-10-16 13:06:25 +02:00
parent 51a4ffca2e
commit b65f76262a
10 changed files with 101 additions and 117 deletions

View File

@@ -12,7 +12,10 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Aggregator\SocialWorkAggregators;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Templating\Entity\UserRender;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\QueryBuilder;
@@ -21,7 +24,13 @@ use function in_array;
final readonly class ReferrerAggregator implements AggregatorInterface
{
public function __construct(private UserRepository $userRepository, private UserRender $userRender) {}
private const PREFIX = 'acpw_referrer_aggregator';
public function __construct(
private UserRepository $userRepository,
private UserRender $userRender,
private RollingDateConverterInterface $rollingDateConverter
) {}
public function addRole(): ?string
{
@@ -30,11 +39,16 @@ final readonly class ReferrerAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('acpwuser', $qb->getAllAliases(), true)) {
$qb->leftJoin('acpw.referrers', 'acpwuser');
}
$p = self::PREFIX;
$qb->addSelect('acpwuser.id AS referrer_aggregator');
$qb
->leftJoin('acpw.referrersHistory', $p . "_acpwusers_history")
->andWhere("{$p}_acpwusers_history.startDate <= :{$p}_calc_date AND ({$p}_acpwusers_history.endDate IS NULL or {$p}_acpwusers_history.endDate > :{$p}_calc_date)");
$qb->setParameter("{$p}_calc_date", $this->rollingDateConverter->convert(
$data['referrer_at'] ?? new RollingDate(RollingDate::T_TODAY)
));
$qb->addSelect("IDENTITY({$p}_acpwusers_history.user) AS referrer_aggregator");
$qb->addGroupBy('referrer_aggregator');
}
@@ -45,11 +59,16 @@ final readonly class ReferrerAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder)
{
// no form
$builder->add('referrer_at', PickRollingDateType::class, [
'label' => 'export.aggregator.course_work.by_treating_agent.Calc date'
]);
}
public function getFormDefaultData(): array
{
return [];
return [
'referrer_at' => new RollingDate(RollingDate::T_TODAY),
];
}
public function getLabels($key, array $values, $data)
@@ -76,6 +95,6 @@ final readonly class ReferrerAggregator implements AggregatorInterface
public function getTitle(): string
{
return 'Group by treating agent';
return 'export.aggregator.course_work.by_treating_agent.Group by treating agent';
}
}

View File

@@ -12,7 +12,10 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Templating\Entity\UserRender;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Andx;
@@ -20,8 +23,12 @@ use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
class ReferrerFilter implements FilterInterface
final readonly class ReferrerFilter implements FilterInterface
{
private const PREFIX = 'acpw_referrer_filter';
public function __construct(private RollingDateConverterInterface $rollingDateConverter) {}
public function addRole(): ?string
{
return null;
@@ -29,21 +36,19 @@ class ReferrerFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('acpwuser', $qb->getAllAliases(), true)) {
$qb->join('acpw.referrers', 'acpwuser');
}
$p = self::PREFIX;
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('acpwuser', ':agents');
$qb
->leftJoin('acpw.referrersHistory', $p . "_acpwusers_history")
->andWhere("{$p}_acpwusers_history.startDate <= :{$p}_calc_date AND ({$p}_acpwusers_history.endDate IS NULL or {$p}_acpwusers_history.endDate > :{$p}_calc_date)")
->andWhere("{$p}_acpwusers_history.user IN (:{$p}_agents)");
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('agents', $data['accepted_agents']);
$qb
->setParameter("{$p}_agents", $data['accepted_agents'])
->setParameter("{$p}_calc_date", $this->rollingDateConverter->convert(
$data['agent_at'] ?? new RollingDate(RollingDate::T_TODAY)
))
;
}
public function applyOn(): string
@@ -53,13 +58,23 @@ class ReferrerFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('accepted_agents', PickUserDynamicType::class, [
'multiple' => true,
]);
$builder
->add('accepted_agents', PickUserDynamicType::class, [
'multiple' => true,
'label' => 'export.filter.work.by_treating_agent.Accepted agents'
])
->add('agent_at', PickRollingDateType::class, [
'label' => 'export.filter.work.by_treating_agent.Calc date',
'help' => 'export.filter.work.by_treating_agent.calc_date_help',
])
;
}
public function getFormDefaultData(): array
{
return [];
return [
'accepted_agents' => [],
'agent_at' => new RollingDate(RollingDate::T_TODAY),
];
}
public function describeAction($data, $format = 'string'): array
@@ -71,13 +86,14 @@ class ReferrerFilter implements FilterInterface
}
return [
'Filtered by treating agent: only %agents%', [
'%agents' => implode(', ', $users),
'exports.filter.work.by_treating_agent.Filtered by treating agent at date', [
'agents' => implode(', ', $users),
'agent_at' => $this->rollingDateConverter->convert($data['agent_at'] ?? new RollingDate(RollingDate::T_TODAY)),
], ];
}
public function getTitle(): string
{
return 'Filter by treating agent';
return 'export.filter.work.by_treating_agent.Filter by treating agent';
}
}