[export] fix calendar filters/aggregators + tests: 'at' based on calendar date

This commit is contained in:
2023-10-03 14:19:45 +02:00
parent 84ba626fb5
commit 31fc7fffe9
9 changed files with 74 additions and 105 deletions

View File

@@ -14,13 +14,10 @@ namespace Chill\CalendarBundle\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\User\UserJobHistory;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Repository\UserJobRepository;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
@@ -29,7 +26,6 @@ final readonly class JobAggregator implements AggregatorInterface
private const PREFIX = 'cal_agg_job';
public function __construct(
private RollingDateConverterInterface $rollingDateConverter,
private UserJobRepository $jobRepository,
private TranslatableStringHelper $translatableStringHelper
) {}
@@ -44,22 +40,24 @@ final readonly class JobAggregator implements AggregatorInterface
$p = self::PREFIX;
$qb
->leftJoin(
'cal.mainUser',
"{$p}_user"
)
->leftJoin('cal.mainUser', "{$p}_user")
->leftJoin(
UserJobHistory::class,
"{$p}_history",
Expr\Join::WITH,
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.job) AS {$p}_select")
->setParameter(
"{$p}_at",
$this->rollingDateConverter->convert($data['job_at'])
// job_at based on cal.startDate
->andWhere(
$qb->expr()->andX(
$qb->expr()->lte("{$p}_history.startDate", "cal.startDate"),
$qb->expr()->orX(
$qb->expr()->isNull("{$p}_history.endDate"),
$qb->expr()->gt("{$p}_history.endDate", "cal.startDate")
)
)
)
->addSelect("IDENTITY({$p}_history.job) AS {$p}_select")
->addGroupBy("{$p}_select");
}
@@ -70,15 +68,11 @@ final readonly class JobAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('job_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_job.Calc date',
'required' => true,
]);
}
public function getFormDefaultData(): array
{
return ['job_at' => new RollingDate(RollingDate::T_TODAY)];
return [];
}
public function getLabels($key, array $values, $data): Closure
@@ -109,6 +103,6 @@ final readonly class JobAggregator implements AggregatorInterface
public function getTitle(): string
{
return 'Group calendars by agent job';
return 'export.aggregator.calendar.agent_job.Group calendars by agent job';
}
}

View File

@@ -14,24 +14,18 @@ namespace Chill\CalendarBundle\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Repository\ScopeRepository;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Exp;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final readonly class ScopeAggregator implements AggregatorInterface
{
private const PREFIX = "cal_agg_scope";
public function __construct(
private RollingDateConverterInterface $rollingDateConverter,
private ScopeRepository $scopeRepository,
private TranslatableStringHelper $translatableStringHelper
) {}
@@ -46,22 +40,24 @@ final readonly class ScopeAggregator implements AggregatorInterface
$p = self::PREFIX;
$qb
->leftJoin(
"cal.mainUser",
"{$p}_user"
)
->leftJoin("cal.mainUser", "{$p}_user")
->leftJoin(
UserScopeHistory::class,
"{$p}_history",
Expr\Join::WITH,
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'])
// scope_at based on cal.startDate
->andWhere(
$qb->expr()->andX(
$qb->expr()->lte("{$p}_history.startDate", "cal.startDate"),
$qb->expr()->orX(
$qb->expr()->isNull("{$p}_history.endDate"),
$qb->expr()->gt("{$p}_history.endDate", "cal.startDate")
)
)
)
->addSelect("IDENTITY({$p}_history.scope) AS {$p}_select")
->addGroupBy("{$p}_select");
}
@@ -72,14 +68,11 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('scope_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_scope.Calc date',
'required' => true,
]);
}
public function getFormDefaultData(): array
{
return ['scope_at' => new RollingDate(RollingDate::T_TODAY)];
return [];
}
public function getLabels($key, array $values, $data): Closure
@@ -110,6 +103,6 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function getTitle(): string
{
return 'Group calendars by agent scope';
return 'export.aggregator.calendar.agent_scope.Group calendars by agent scope';
}
}

View File

@@ -15,11 +15,8 @@ use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\User\UserJobHistory;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverter;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
@@ -30,7 +27,6 @@ class JobFilter implements FilterInterface
private const PREFIX = 'cal_filter_job';
public function __construct(
private readonly RollingDateConverter $rollingDateConverter,
protected TranslatorInterface $translator,
private readonly TranslatableStringHelper $translatableStringHelper
) {}
@@ -45,25 +41,27 @@ class JobFilter implements FilterInterface
$p = self::PREFIX;
$qb
->leftJoin(
"cal.mainUser",
"{$p}_user"
)
->leftJoin("cal.mainUser", "{$p}_user")
->leftJoin(
UserJobHistory::class,
"{$p}_history",
Expr\Join::WITH,
Join::WITH,
$qb->expr()->eq("{$p}_history.user", "{$p}_user")
)
->andWhere($qb->expr()->in("{$p}_history.job", ":{$p}_job"))
// job_at based on cal.startDate
->andWhere(
"{$p}_history.startDate <= :{$p}_at AND ({$p}_history.endDate IS NULL OR {$p}_history.endDate > :{$p}_at)"
$qb->expr()->andX(
$qb->expr()->lte("{$p}_history.startDate", "cal.startDate"),
$qb->expr()->orX(
$qb->expr()->isNull("{$p}_history.endDate"),
$qb->expr()->gt("{$p}_history.endDate", "cal.startDate")
)
)
)
->andWhere($qb->expr()->in("{$p}_history.job", ":{$p}_job"))
->setParameters([
"{$p}_job" => $data["job"],
"{$p}_at" => $this->rollingDateConverter->convert($data["job_at"])
])
;
]);
}
@@ -82,10 +80,6 @@ class JobFilter implements FilterInterface
),
'multiple' => true,
'expanded' => true,
])
->add('job_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_job.Calc date',
'required' => true,
]);
}
@@ -99,7 +93,7 @@ class JobFilter implements FilterInterface
);
}
return ['Filtered by agent job: only %jobs%', [
return ['export.filter.calendar.agent_job.Filtered by agent job: only %jobs%', [
'%jobs%' => implode(', ', $userJobs),
]];
}
@@ -108,12 +102,11 @@ class JobFilter implements FilterInterface
{
return [
'job' => [],
'job_at' => new RollingDate(RollingDate::T_TODAY),
];
}
public function getTitle(): string
{
return 'Filter calendars by agent job';
return 'export.filter.calendar.agent_job.Filter calendars by agent job';
}
}

View File

@@ -15,11 +15,8 @@ use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverter;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
@@ -30,7 +27,6 @@ class ScopeFilter implements FilterInterface
private const PREFIX = 'cal_filter_scope';
public function __construct(
private readonly RollingDateConverter $rollingDateConverter,
protected TranslatorInterface $translator,
private readonly TranslatableStringHelper $translatableStringHelper
) {}
@@ -49,16 +45,22 @@ class ScopeFilter implements FilterInterface
->leftJoin(
UserScopeHistory::class,
"{$p}_history",
Expr\Join::WITH,
Join::WITH,
$qb->expr()->eq("{$p}_history.user", "{$p}_user")
)
->andWhere($qb->expr()->in("{$p}_history.scope", ":{$p}_scope"))
// scope_at based on cal.startDate
->andWhere(
"{$p}_history.startDate <= :{$p}_at AND ({$p}_history.endDate IS NULL OR {$p}_history.endDate > :{$p}_at)"
$qb->expr()->andX(
$qb->expr()->lte("{$p}_history.startDate", "cal.startDate"),
$qb->expr()->orX(
$qb->expr()->isNull("{$p}_history.endDate"),
$qb->expr()->gt("{$p}_history.endDate", "cal.startDate")
)
)
)
->andWhere($qb->expr()->in("{$p}_history.scope", ":{$p}_scope"))
->setParameters([
"{$p}_scope" => $data["scope"],
"{$p}_at" => $this->rollingDateConverter->convert($data['scope_at'])
]);
}
@@ -77,10 +79,6 @@ class ScopeFilter implements FilterInterface
),
'multiple' => true,
'expanded' => true,
])
->add('scope_at', PickRollingDateType::class, [
'label' => 'export.calendar.agent_scope.Calc date',
'required' => true,
]);
}
@@ -94,7 +92,7 @@ class ScopeFilter implements FilterInterface
);
}
return ['Filtered by agent scope: only %scopes%', [
return ['export.filter.calendar.agent_scope.Filtered by agent scope: only %scopes%', [
'%scopes%' => implode(', ', $scopes),
]];
}
@@ -103,12 +101,11 @@ class ScopeFilter implements FilterInterface
{
return [
'scope' => [],
'scope_at' => new RollingDate(RollingDate::T_TODAY)
];
}
public function getTitle(): string
{
return 'Filter calendars by agent scope';
return 'export.filter.calendar.agent_scope.Filter calendars by agent scope';
}
}