Merge branch '111_exports' into social_action_exports

This commit is contained in:
Julie Lenaerts 2022-08-30 13:17:37 +02:00
commit 7eb75b709c
8 changed files with 307 additions and 46 deletions

View File

@ -34,31 +34,26 @@ class DateAggregator implements AggregatorInterface
public function getLabels($key, array $values, $data)
{
return function ($value) use ($data): string {
if ($value === '_header') {
return 'by '. $data['frequency'];
}
switch ($data['frequency']) {
case 'month':
if ($value === '_header') {
return 'by month';
}
$month = \DateTime::createFromFormat('!m', $value);
return
sprintf("%02d", $value) .'/'.
$month->format('F') // TODO translation ?!?
;
return sprintf(
"%02d (%s)",
$value,
$month->format('M')
);
case 'week':
if ($value === '_header') {
return 'by week';
}
return $this->translator->trans('for week') .' '. $value ;
//return $this->translator->trans('for week') .' '. $value ;
case 'year':
if ($value === '_header') {
return 'by year';
}
return $this->translator->trans('in year') .' '. $value ;
//return $this->translator->trans('in year') .' '. $value ;
default:
throw new RuntimeException(sprintf('The value %s is not valid', $value));
return $value;
}
};
}
@ -91,34 +86,24 @@ class DateAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data)
{
$order = null;
switch ($data['frequency']) {
case 'month':
$qb
//->addSelect("TO_CHAR(activity.date,'Mon') AS MON")
->addSelect('EXTRACT(month FROM activity.date) AS date_aggregator')
//->orderBy('date_aggregator')
;
break;
$fmt = 'MM'; break;
case 'week':
$qb
->addSelect("TO_CHAR(activity.date, 'IW') AS date_aggregator")
//->orderBy('date_aggregator')
;
break;
$fmt = 'IW'; break;
case 'year':
$qb
//->addSelect("TO_CHAR(activity.date, 'YYYY') AS date_aggregator")
->addSelect('EXTRACT(year FROM activity.date) AS date_aggregator')
//->orderBy('date_aggregator', 'ASC')
;
break;
$fmt = 'YYYY'; $order = 'DESC'; break;
default:
throw new RuntimeException(sprintf("The frequency data '%s' is invalid.", $data['frequency']));
}
$qb->addSelect(sprintf("TO_CHAR(activity.date, '%s') AS date_aggregator", $fmt));
$groupBy = $qb->getDQLPart('groupBy');
if (!empty($groupBy)) {
@ -126,6 +111,14 @@ class DateAggregator implements AggregatorInterface
} else {
$qb->groupBy('date_aggregator');
}
$orderBy = $qb->getDQLPart('orderBy');
if (!empty($orderBy)) {
$qb->addOrderBy('date_aggregator', $order);
} else {
$qb->orderBy('date_aggregator', $order);
}
}
public function applyOn(): string

View File

@ -2,7 +2,9 @@
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\DateDiffFunction;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
@ -12,13 +14,17 @@ use Doctrine\ORM\Query\SqlWalker;
* Extract postgresql function
* https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
*
* Usage : EXTRACT(field FROM value)
* Usage : EXTRACT(field FROM timestamp)
* TODO allow interval usage -> EXTRACT(field FROM interval)
*/
class Extract extends FunctionNode
{
private string $field;
private PathExpression $value;
private $value;
//private PathExpression $value;
//private FunctionNode $value;
//private DateDiffFunction $value;
public function getSql(SqlWalker $sqlWalker)
{
@ -39,7 +45,8 @@ class Extract extends FunctionNode
$parser->match(Lexer::T_FROM);
$this->value = $parser->ScalarExpression();
//$this->value = $parser->ScalarExpression();
$this->value = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

View File

@ -0,0 +1,72 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="chill_main_geographical_unit")
* @ORM\Entity
*/
class GeographicalUnit
{
/**
* @ORM\Column(type="text", nullable=true)
*/
private $geom;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private ?int $id = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $layerName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $unitName;
public function getId(): ?int
{
return $this->id;
}
public function getLayerName(): ?string
{
return $this->layerName;
}
public function getUnitName(): ?string
{
return $this->unitName;
}
public function setLayerName(?string $layerName): self
{
$this->layerName = $layerName;
return $this;
}
public function setUnitName(?string $unitName): self
{
$this->unitName = $unitName;
return $this;
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Add new entity GeographicalUnit
*/
final class Version20220829132409 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add new entity GeographicalUnit';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE chill_main_geographical_unit_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE chill_main_geographical_unit (id INT NOT NULL, geom TEXT DEFAULT NULL, layerName VARCHAR(255) DEFAULT NULL, unitName VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
}
public function down(Schema $schema): void
{
$this->addSql('DROP SEQUENCE chill_main_geographical_unit_id_seq CASCADE');
$this->addSql('DROP TABLE chill_main_geographical_unit');
}
}

View File

@ -0,0 +1,148 @@
<?php
namespace Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Les regroupements seront un nombre de mois, arrondi à l'unité la plus proche (donc
* - au dela de 15 jours => 1 mois,
* - jusqu'à 45 jours => 1 mois,
* 15 | 45 | 75
* --+----o----+----o----+----
* | 30 | 60 |
* etc.)
*/
class DurationAggregator implements AggregatorInterface
{
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @inheritDoc
*/
public function getLabels($key, array $values, $data)
{
return function ($value) use ($data): string {
if ($value === '_header') {
return $this->translator->trans('Rounded month duration');
}
if ($value === null) {
return $this->translator->trans('current duration'); // when closingDate is null
}
if ($value === 0) {
return $this->translator->trans("duration 0 month");
}
return ''. $value . $this->translator->trans(' months');
};
}
/**
* @inheritDoc
*/
public function getQueryKeys($data): array
{
return ['duration_aggregator'];
}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder)
{
// no form
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return 'Group by duration';
}
/**
* @inheritDoc
*/
public function addRole()
{
return null;
}
/**
* @inheritDoc
*/
public function alterQuery(QueryBuilder $qb, $data)
{
$qb
// OUI
->addSelect('
(acp.closingDate - acp.openingDate +15) *12/365
AS duration_aggregator'
)
//->addSelect('DATE_DIFF(acp.closingDate, acp.openingDate) AS duration_aggregator')
//->addSelect('EXTRACT(month FROM acp.openingDate) AS duration_aggregator')
//->addSelect("DATE_SUB(acp.openingDate, 6, 'day') AS duration_aggregator")
// TODO adapter la fonction extract pour l'utiliser avec des intervals: extract(month from interval)
// et ajouter une fonction custom qui calcule les intervals, comme doctrineum/date-interval
// https://packagist.org/packages/doctrineum/date-interval#3.1.0
// (composer fait un conflit de dépendance)
//->addSelect("
// EXTRACT(
// month FROM
// DATE_INTERVAL(acp.closingDate, acp.openingDate)
// )
// AS duration_aggregator")
// NON
//->addSelect("BETWEEN acp.openingDate AND acp.closingDate AS duration_aggregator")
//->addSelect("EXTRACT(month FROM DATE_SUB(acp.openingDate, 6, 'day')) AS duration_aggregator")
//->addSelect('EXTRACT(month FROM DATE_DIFF(acp.closingDate, acp.openingDate)) AS duration_aggregator')
/*
->addSelect('
( CASE
WHEN EXTRACT(day FROM DATE_DIFF(acp.closingDate, acp.openingDate)) > 15
THEN EXTRACT(month FROM DATE_DIFF(acp.closingDate, acp.openingDate)) +1
ELSE EXTRACT(month FROM DATE_DIFF(acp.closingDate, acp.openingDate))
END ) AS duration_aggregator
')
*/
;
$groupBy = $qb->getDQLPart('groupBy');
if (!empty($groupBy)) {
$qb->addGroupBy('duration_aggregator');
} else {
$qb->groupBy('duration_aggregator');
}
$qb->orderBy('duration_aggregator');
}
/**
* @inheritDoc
*/
public function applyOn(): string
{
return Declarations::ACP_TYPE;
}
}

View File

@ -2,13 +2,14 @@
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\GeographicalUnit;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\ChillDateType;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
/**
@ -23,12 +24,6 @@ use Symfony\Component\Form\FormBuilderInterface;
*/
class GeographicalUnitStatFilter implements FilterInterface
{
private const LOCTYPE = [
'center' => 'center',
// TODO not yet implemented: https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/626
];
/**
* @inheritDoc
*/
@ -38,9 +33,12 @@ class GeographicalUnitStatFilter implements FilterInterface
->add('date', ChillDateType::class, [
'data' => new \DateTime(),
])
->add('accepted_loctype', ChoiceType::class, [
'choices' => self::LOCTYPE,
'multiple' => false,
->add('accepted_loctype', EntityType::class, [
'class' => GeographicalUnit::class,
'choice_label' => function (GeographicalUnit $u) {
return $u->getUnitName();
},
'multiple' => true,
'expanded' => true,
])
;

View File

@ -241,3 +241,10 @@ services:
tags:
- { name: chill.export_aggregator, alias: accompanyingcourse_referrer_aggregator }
chill.person.export.aggregator_duration:
class: Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators\DurationAggregator
autowire: true
autoconfigure: true
tags:
- { name: chill.export_aggregator, alias: accompanyingcourse_duration_aggregator }

View File

@ -569,6 +569,11 @@ Group by composition: Grouper les ménages par composition familiale
Group by number of children: Grouper les ménages par nombre d'enfants
## persons aggregators
Group by duration: Grouper par durée du parcours
Rounded month duration: Durée en mois (arrondie)
current duration: en cours
duration 0 month: 0 mois (<15 jours)
' months': ' mois'
Group people by nationality: Grouper les personnes par nationalités
Group by level: Grouper par niveau
Group by continents: Grouper par continent