Feature: [export] Add a rolling date on Acp "by step" filter

This commit is contained in:
Julien Fastré 2022-11-07 18:20:22 +01:00
parent 8159f91e25
commit 86be95ac43
2 changed files with 45 additions and 19 deletions

View File

@ -105,3 +105,8 @@ services:
resource: '../Service/Import/' resource: '../Service/Import/'
autowire: true autowire: true
autoconfigure: true autoconfigure: true
Chill\MainBundle\Service\RollingDate\:
resource: '../Service/RollingDate/'
autowire: true
autoconfigure: true

View File

@ -12,31 +12,40 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use function in_array;
class StepFilter implements FilterInterface class StepFilter implements FilterInterface
{ {
private const A = 'acp_filter_bystep_stephistories';
private const DEFAULT_CHOICE = AccompanyingPeriod::STEP_CONFIRMED; private const DEFAULT_CHOICE = AccompanyingPeriod::STEP_CONFIRMED;
private const P = 'acp_step_filter_date';
private const STEPS = [ private const STEPS = [
'Draft' => AccompanyingPeriod::STEP_DRAFT, 'Draft' => AccompanyingPeriod::STEP_DRAFT,
'Confirmed' => AccompanyingPeriod::STEP_CONFIRMED, 'Confirmed' => AccompanyingPeriod::STEP_CONFIRMED,
'Closed' => AccompanyingPeriod::STEP_CLOSED, 'Closed' => AccompanyingPeriod::STEP_CLOSED,
]; ];
private RollingDateConverterInterface $rollingDateConverter;
/** /**
* @var TranslatorInterface * @var TranslatorInterface
*/ */
protected $translator; private $translator;
public function __construct(TranslatorInterface $translator) public function __construct(RollingDateConverterInterface $rollingDateConverter, TranslatorInterface $translator)
{ {
$this->rollingDateConverter = $rollingDateConverter;
$this->translator = $translator; $this->translator = $translator;
} }
@ -47,17 +56,25 @@ class StepFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
$where = $qb->getDQLPart('where'); if (!in_array(self::A, $qb->getAllAliases(), true)) {
$clause = $qb->expr()->eq('acp.step', ':step'); $qb->leftJoin('acp.stepHistories', self::A);
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
} }
$qb->add('where', $where); $qb
$qb->setParameter('step', $data['accepted_steps']); ->andWhere(
$qb->expr()->andX(
$qb->expr()->lte(self::A . '.startDate', ':' . self::P),
$qb->expr()->orX(
$qb->expr()->isNull(self::A . '.endDate'),
$qb->expr()->lt(self::A . '.endDate', ':' . self::P)
)
)
)
->andWhere(
$qb->expr()->in(self::A . '.step', ':acp_filter_by_step_steps')
)
->setParameter(self::P, $this->rollingDateConverter->convert($data['calc_date']))
->setParameter('acp_filter_by_step_steps', $data['accepted_steps']);
} }
public function applyOn() public function applyOn()
@ -67,12 +84,16 @@ class StepFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
$builder->add('accepted_steps', ChoiceType::class, [ $builder
->add('accepted_steps', ChoiceType::class, [
'choices' => self::STEPS, 'choices' => self::STEPS,
'multiple' => false, 'multiple' => false,
'expanded' => true, 'expanded' => true,
'empty_data' => self::DEFAULT_CHOICE, 'empty_data' => self::DEFAULT_CHOICE,
'data' => self::DEFAULT_CHOICE, 'data' => self::DEFAULT_CHOICE,
])
->add('calc_date', PickRollingDateType::class, [
'label' => 'export.acp.filter.by_step.date_calc',
]); ]);
} }