Features: [export] Apply person filters and aggreators on Count Household

This commit is contained in:
Julien Fastré 2022-11-14 11:21:16 +01:00
parent 400770123a
commit a68422e0e8
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 61 additions and 31 deletions

View File

@ -14,31 +14,42 @@ namespace Chill\PersonBundle\Export\Export;
use Chill\MainBundle\Export\ExportInterface; use Chill\MainBundle\Export\ExportInterface;
use Chill\MainBundle\Export\FormatterInterface; use Chill\MainBundle\Export\FormatterInterface;
use Chill\MainBundle\Export\GroupedExportInterface; use Chill\MainBundle\Export\GroupedExportInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\PersonBundle\Entity\Household\HouseholdMember; use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person\PersonCenterHistory; use Chill\PersonBundle\Entity\Person\PersonCenterHistory;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Security\Authorization\HouseholdVoter; use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use LogicException; use LogicException;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
class CountHousehold implements ExportInterface, GroupedExportInterface class CountHousehold implements ExportInterface, GroupedExportInterface
{ {
private EntityRepository $repository; private const TR_PREFIX = 'export.export.nb_household_with_course.';
private EntityManagerInterface $entityManager;
private RollingDateConverterInterface $rollingDateConverter;
public function __construct( public function __construct(
EntityManagerInterface $em EntityManagerInterface $em,
RollingDateConverterInterface $rollingDateConverter
) { ) {
$this->repository = $em->getRepository(AccompanyingPeriod::class); $this->entityManager = $em;
$this->rollingDateConverter = $rollingDateConverter;
} }
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
// TODO: Implement buildForm() method. $builder
->add('calc_date', PickRollingDateType::class, [
'data' => new RollingDate(RollingDate::T_TODAY),
'label' => self::TR_PREFIX . 'Date of calculation of household members',
'required' => false,
]);
} }
public function getAllowedFormattersTypes(): array public function getAllowedFormattersTypes(): array
@ -48,7 +59,7 @@ class CountHousehold implements ExportInterface, GroupedExportInterface
public function getDescription(): string public function getDescription(): string
{ {
return 'Count household by various parameters.'; return self::TR_PREFIX . 'Count household with accompanying course by various parameters.';
} }
public function getGroup(): string public function getGroup(): string
@ -58,21 +69,31 @@ class CountHousehold implements ExportInterface, GroupedExportInterface
public function getLabels($key, array $values, $data) public function getLabels($key, array $values, $data)
{ {
if ('export_result' !== $key) { return static function ($value) use ($key) {
throw new LogicException("the key {$key} is not used by this export"); if ('_header' === $value) {
} switch ($key) {
case 'household_export_result':
return self::TR_PREFIX . 'Count households';
$labels = array_combine($values, $values); case 'acp_export_result':
$labels['_header'] = $this->getTitle(); return self::TR_PREFIX . 'Count accompanying periods';
return static function ($value) use ($labels) { default:
return $labels[$value]; throw new LogicException('Key not supported: ' . $key);
}
}
if (null === $value) {
return '';
}
return $value;
}; };
} }
public function getQueryKeys($data): array public function getQueryKeys($data): array
{ {
return ['export_result']; return ['household_export_result', 'acp_export_result'];
} }
public function getResult($query, $data) public function getResult($query, $data)
@ -82,7 +103,7 @@ class CountHousehold implements ExportInterface, GroupedExportInterface
public function getTitle(): string public function getTitle(): string
{ {
return 'Count households'; return self::TR_PREFIX . 'Count households with accompanying course';
} }
public function getType(): string public function getType(): string
@ -96,25 +117,29 @@ class CountHousehold implements ExportInterface, GroupedExportInterface
return $el['center']; return $el['center'];
}, $acl); }, $acl);
$qb = $this->repository $qb = $this->entityManager->createQueryBuilder();
->createQueryBuilder('acp')
->join('acp.participations', 'acppart')
// little optimization: we remove joins and make a direct join between participations and household members
->join(HouseholdMember::class, 'member', Query\Expr\Join::WITH, 'IDENTITY(acppart.person) = IDENTITY(member.person)')
->join('member.household', 'household');
$qb $qb
->from(Household::class, 'household')
->join('household.members', 'hmember')
->join('hmember.person', 'person')
->join('person.accompanyingPeriodParticipations', 'acppart')
->join('acppart.accompanyingPeriod', 'acp')
->andWhere('acppart.startDate != acppart.endDate OR acppart.endDate IS NULL')
->andWhere( ->andWhere(
$qb->expr()->exists( $qb->expr()->exists(
'SELECT 1 FROM ' . AccompanyingPeriodParticipation::class . ' acl_count_part 'SELECT 1 FROM ' . PersonCenterHistory::class . ' acl_count_person_history WHERE acl_count_person_history.person = person
JOIN ' . PersonCenterHistory::class . ' acl_count_person_history WITH IDENTITY(acl_count_person_history.person) = IDENTITY(acl_count_part.person) AND acl_count_person_history.center IN (:authorized_centers)
WHERE acl_count_part.accompanyingPeriod = acp.id AND acl_count_person_history.center IN (:authorized_centers)
' '
) )
) )
->setParameter('authorized_centers', $centers); ->andWhere('hmember.startDate <= :count_household_at_date AND (hmember.endDate IS NULL OR hmember.endDate > :count_household_at_date)')
->setParameter('authorized_centers', $centers)
->setParameter('count_household_at_date', $this->rollingDateConverter->convert($data['calc_date']));
$qb->select('COUNT(DISTINCT household.id) AS export_result'); $qb
->select('COUNT(DISTINCT household.id) AS household_export_result')
->addSelect('COUNT(DISTINCT acp.id) AS acp_export_result');
return $qb; return $qb;
} }
@ -129,6 +154,7 @@ class CountHousehold implements ExportInterface, GroupedExportInterface
return [ return [
Declarations::HOUSEHOLD_TYPE, Declarations::HOUSEHOLD_TYPE,
Declarations::ACP_TYPE, Declarations::ACP_TYPE,
Declarations::PERSON_TYPE,
]; ];
} }
} }

View File

@ -373,8 +373,6 @@ Count evaluations: Nombre d'évaluations
Count evaluation by various parameters.: Compte le nombre d'évaluations selon différents filtres. Count evaluation by various parameters.: Compte le nombre d'évaluations selon différents filtres.
Exports of households: Exports des ménages Exports of households: Exports des ménages
Count households: Nombre de ménages
Count household by various parameters.: Compte le nombre de ménages impliqués dans un parcours selon différents filtres.
## persons filters ## persons filters
Filter by person gender: Filtrer les personnes par genre Filter by person gender: Filtrer les personnes par genre
@ -995,6 +993,12 @@ export:
count_participations: Nombre de participations distinctes count_participations: Nombre de participations distinctes
count_persons: Nombre d'usagers concernés distincts count_persons: Nombre d'usagers concernés distincts
count_acps: Nombre de parcours distincts count_acps: Nombre de parcours distincts
nb_household_with_course:
Count households with accompanying course: Nombre de ménages impliqués dans un parcours
Count households: Nombre de ménages
Count accompanying periods: Nombre de parcours
Count household with accompanying course by various parameters.: Compte le nombre de ménages impliqués dans un parcours selon différents filtres.
Date of calculation of household members: Date à laquelle les membres du ménages sont comptabilisés
aggregator: aggregator:
person: person:
by_household_composition: by_household_composition: