add budget to docgen

This commit is contained in:
Julien Fastré 2022-03-24 18:22:49 +01:00
parent 531f940b65
commit 84f9fdba28
8 changed files with 199 additions and 5 deletions

View File

@ -29,6 +29,11 @@ class ConfigRepository
$this->charges = $charges;
}
public function getChargesKeys(): array
{
return array_map(static function ($element) { return $element['key']; }, $this->charges);
}
/**
* @return array where keys are the resource'key and label the ressource label
*/
@ -43,6 +48,11 @@ class ConfigRepository
return $charges;
}
public function getResourcesKeys(): array
{
return array_map(static function ($element) { return $element['key']; }, $this->resources);
}
/**
* @return array where keys are the resource'key and label the ressource label
*/

View File

@ -38,6 +38,7 @@ class ChillBudgetExtension extends Extension implements PrependExtensionInterfac
$loader->load('services/templating.yaml');
$loader->load('services/menu.yaml');
$loader->load('services/calculator.yaml');
$loader->load('services/services.yaml');
$this->storeConfig('resources', $config, $container);
$this->storeConfig('charges', $config, $container);

View File

@ -0,0 +1,159 @@
<?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\BudgetBundle\Service\Summary;
use Chill\BudgetBundle\Config\ConfigRepository;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use LogicException;
use function count;
/**
* Helps to find a summary of the budget: the sum of resources and charges.
*/
class SummaryBudget
{
private const QUERY_CHARGE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, type FROM chill_budget.charge WHERE (person_id IN (_ids_) OR household_id = ?) AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type';
private const QUERY_CHARGE_BY_PERSON = 'select SUM(amount) AS sum, type FROM chill_budget.charge WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type';
private const QUERY_RESOURCE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, type FROM chill_budget.resource WHERE (person_id IN (_ids_) OR household_id = ?) AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type';
private const QUERY_RESOURCE_BY_PERSON = 'select SUM(amount) AS sum, type FROM chill_budget.resource WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type';
private array $chargeLabels;
private ConfigRepository $configRepository;
private EntityManagerInterface $em;
private array $resourcesLabels;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct(EntityManagerInterface $em, ConfigRepository $configRepository, TranslatableStringHelperInterface $translatableStringHelper)
{
$this->em = $em;
$this->configRepository = $configRepository;
$this->chargeLabels = $configRepository->getChargesLabels();
$this->resourcesLabels = $configRepository->getResourcesLabels();
$this->translatableStringHelper = $translatableStringHelper;
}
public function getEmptyChargeArray(): array
{
$keys = $this->configRepository->getChargesKeys();
$labels = $this->chargeLabels;
return array_combine($keys, array_map(function ($i) use ($labels) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])];
}, $keys));
}
public function getEmptyResourceArray(): array
{
$keys = $this->configRepository->getResourcesKeys();
$labels = $this->resourcesLabels;
return array_combine($keys, array_map(function ($i) use ($labels) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])];
}, $keys));
}
public function getSummaryForHousehold(?Household $household): array
{
if (null === $household) {
return [
'resources' => $this->getEmptyResourceArray(),
'charges' => $this->getEmptyChargeArray(),
];
}
$personIds = $household->getCurrentPersons()->map(static function (Person $p) { return $p->getId(); });
$ids = implode(', ', array_fill(0, count($personIds), '?'));
$parameters = [...$personIds, $household->getId()];
$rsm = $this->buildRsm();
$resources = $this->em->createNativeQuery(strtr(self::QUERY_RESOURCE_BY_HOUSEHOLD, ['_ids_' => $ids]), $rsm)
->setParameters($parameters)
->getResult();
$charges = $this->em->createNativeQuery(strtr(self::QUERY_CHARGE_BY_HOUSEHOLD, ['_ids_' => $ids]), $rsm)
->setParameters($parameters)
->getResult();
return [
'resources' => array_merge($this->getEmptyResourceArray(), $this->rowToArray($resources, 'resource')),
'charges' => array_merge($this->getEmptyChargeArray(), $this->rowToArray($charges, 'charge')),
];
}
public function getSummaryForPerson(Person $person): array
{
$rsm = $this->buildRsm();
$resources = $this->em->createNativeQuery(self::QUERY_RESOURCE_BY_PERSON, $rsm)
->setParameters([$person->getId()])
->getResult();
$charges = $this->em->createNativeQuery(self::QUERY_CHARGE_BY_PERSON, $rsm)
->setParameters([$person->getId()])
->getResult();
return [
'resources' => array_merge($this->getEmptyResourceArray(), $this->rowToArray($resources, 'resource')),
'charges' => array_merge($this->getEmptyChargeArray(), $this->rowToArray($charges, 'charge')),
];
}
private function buildRsm(): ResultSetMapping
{
$rsm = new ResultSetMapping();
$rsm
->addScalarResult('sum', 'sum')
->addScalarResult('type', 'type');
return $rsm;
}
private function rowToArray(array $rows, string $kind): array
{
switch ($kind) {
case 'charge':
$label = $this->chargeLabels;
break;
case 'resource':
$label = $this->resourcesLabels;
break;
default:
throw new LogicException();
}
$result = [];
foreach ($rows as $row) {
$result[$row['type']] = [
'sum' => (float) $row['sum'],
'label' => $this->translatableStringHelper->localize($label[$row['type']]),
];
}
return $result;
}
}

View File

@ -0,0 +1,6 @@
services:
Chill\BudgetBundle\Service\:
resource: './../Service'
autowire: true
autoconfigure: true

View File

@ -13,7 +13,6 @@ namespace Chill\PersonBundle\Menu;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Doctrine\ORM\Query\Parameter;
use Knp\Menu\MenuItem;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
@ -26,10 +25,10 @@ class SectionMenuBuilder implements LocalMenuBuilderInterface
{
protected AuthorizationCheckerInterface $authorizationChecker;
protected TranslatorInterface $translator;
protected ParameterBagInterface $parameterBag;
protected TranslatorInterface $translator;
/**
* SectionMenuBuilder constructor.
*/

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Serializer\Normalizer;
use Chill\BudgetBundle\Service\Summary\SummaryBudget;
use Chill\DocGeneratorBundle\Serializer\Helper\NormalizeNullValueHelper;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\Civility;
@ -44,6 +45,8 @@ class PersonDocGenNormalizer implements
private RelationshipRepository $relationshipRepository;
private SummaryBudget $summaryBudget;
private TranslatableStringHelper $translatableStringHelper;
private TranslatorInterface $translator;
@ -52,12 +55,14 @@ class PersonDocGenNormalizer implements
PersonRenderInterface $personRender,
RelationshipRepository $relationshipRepository,
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper
TranslatableStringHelper $translatableStringHelper,
SummaryBudget $summaryBudget
) {
$this->personRender = $personRender;
$this->relationshipRepository = $relationshipRepository;
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
$this->summaryBudget = $summaryBudget;
}
public function normalize($person, $format = null, array $context = [])
@ -127,6 +132,7 @@ class PersonDocGenNormalizer implements
'docgen:expects' => Household::class,
'docgen:person:with-household' => false,
'docgen:person:with-relations' => false,
'docgen:person:with-budget' => false,
])
);
}
@ -139,10 +145,19 @@ class PersonDocGenNormalizer implements
'docgen:person:with-household' => false,
'docgen:person:with-relation' => false,
'docgen:relationship:counterpart' => $person,
'docgen:person:with-budget' => false,
])
);
}
if ($context['docgen:person:with-budget'] ?? false) {
$data['budget']['person'] = $this->summaryBudget->getSummaryForPerson($person);
if ($context['docgen:person:with-household'] ?? false) {
$data['budget']['household'] = $this->summaryBudget->getSummaryForHousehold($person->getCurrentHousehold());
}
}
return $data;
}

View File

@ -191,6 +191,7 @@ class AccompanyingPeriodContext implements
'groups' => 'docgen:read',
'docgen:person:with-household' => true,
'docgen:person:with-relations' => true,
'docgen:person:with-budget' => true,
]);
}
}

View File

@ -114,7 +114,10 @@ class PersonContext implements DocGeneratorContextWithAdminFormInterface
$data = array_merge($data, $this->baseContextData->getData());
$data['person'] = $this->normalizer->normalize($entity, 'docgen', [
'docgen:expects' => Person::class,
'groups' => ['docgen:read', 'docgen:person:with-household', 'docgen:person:with-relations'],
'groups' => ['docgen:read'],
'docgen:person:with-household' => true,
'docgen:person:with-relations' => true,
'docgen:person:with-budget' => true,
]);
return $data;