From 84f9fdba2819e8756bfec711fe85c055a8a3eb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 24 Mar 2022 18:22:49 +0100 Subject: [PATCH] add budget to docgen --- .../Config/ConfigRepository.php | 10 ++ .../ChillBudgetExtension.php | 1 + .../Service/Summary/SummaryBudget.php | 159 ++++++++++++++++++ .../config/services/services.yaml | 6 + .../Menu/SectionMenuBuilder.php | 5 +- .../Normalizer/PersonDocGenNormalizer.php | 17 +- .../AccompanyingPeriodContext.php | 1 + .../Service/DocGenerator/PersonContext.php | 5 +- 8 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php create mode 100644 src/Bundle/ChillBudgetBundle/config/services/services.yaml diff --git a/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php b/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php index 7906e5821..73f6fb355 100644 --- a/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php +++ b/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php @@ -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 */ diff --git a/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php b/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php index d21c0e11a..cf7e4fad2 100644 --- a/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php +++ b/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php @@ -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); diff --git a/src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php b/src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php new file mode 100644 index 000000000..08a0fec77 --- /dev/null +++ b/src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php @@ -0,0 +1,159 @@ +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; + } +} diff --git a/src/Bundle/ChillBudgetBundle/config/services/services.yaml b/src/Bundle/ChillBudgetBundle/config/services/services.yaml new file mode 100644 index 000000000..5a49c9ff3 --- /dev/null +++ b/src/Bundle/ChillBudgetBundle/config/services/services.yaml @@ -0,0 +1,6 @@ +services: + Chill\BudgetBundle\Service\: + resource: './../Service' + autowire: true + autoconfigure: true + diff --git a/src/Bundle/ChillPersonBundle/Menu/SectionMenuBuilder.php b/src/Bundle/ChillPersonBundle/Menu/SectionMenuBuilder.php index e458b049d..c95c5caf5 100644 --- a/src/Bundle/ChillPersonBundle/Menu/SectionMenuBuilder.php +++ b/src/Bundle/ChillPersonBundle/Menu/SectionMenuBuilder.php @@ -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. */ diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index 0e146b5c0..28216f368 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -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; } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 45057038f..495989b29 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -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, ]); } } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 85ca1c161..1561f0735 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -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;