From de55ff920f1ffee00b0d5ea47aa5028a23dff5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 31 Jan 2023 19:41:43 +0100 Subject: [PATCH] Fixed: [budget] remove deprecated config repository and fix summary budget when generating document --- .../Config/ConfigRepository.php | 102 ------------------ .../ChillBudgetExtension.php | 1 - .../ChillBudgetBundle/Form/ChargeType.php | 22 ---- .../ChillBudgetBundle/Form/ResourceType.php | 21 ---- .../Service/Summary/SummaryBudget.php | 94 +++++++++------- .../ChillBudgetBundle/Templating/Twig.php | 65 ----------- .../config/services/config.yaml | 5 - .../vuejs/StoredObjectButton/helpers.ts | 4 + 8 files changed, 61 insertions(+), 253 deletions(-) delete mode 100644 src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php delete mode 100644 src/Bundle/ChillBudgetBundle/Templating/Twig.php delete mode 100644 src/Bundle/ChillBudgetBundle/config/services/config.yaml diff --git a/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php b/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php deleted file mode 100644 index f28a83a4d..000000000 --- a/src/Bundle/ChillBudgetBundle/Config/ConfigRepository.php +++ /dev/null @@ -1,102 +0,0 @@ -resources = $resources; - $this->charges = $charges; - } - - public function getChargesKeys(bool $onlyActive = false): array - { - return array_map(static function ($element) { - return $element['key']; - }, $this->getCharges($onlyActive)); - } - - /** - * @return array where keys are the resource'key and label the ressource label - */ - public function getChargesLabels(bool $onlyActive = false) - { - $charges = []; - - foreach ($this->getCharges($onlyActive) as $definition) { - $charges[$definition['key']] = $this->normalizeLabel($definition['labels']); - } - - return $charges; - } - - public function getResourcesKeys(bool $onlyActive = false): array - { - return array_map(static function ($element) { - return $element['key']; - }, $this->getResources($onlyActive)); - } - - /** - * @return array where keys are the resource'key and label the ressource label - */ - public function getResourcesLabels(bool $onlyActive = false) - { - $resources = []; - - foreach ($this->getResources($onlyActive) as $definition) { - $resources[$definition['key']] = $this->normalizeLabel($definition['labels']); - } - - return $resources; - } - - private function getCharges(bool $onlyActive = false): array - { - return $onlyActive ? - array_filter($this->charges, static function ($el) { - return $el['active']; - }) - : $this->charges; - } - - private function getResources(bool $onlyActive = false): array - { - return $onlyActive ? - array_filter($this->resources, static function ($el) { - return $el['active']; - }) - : $this->resources; - } - - private function normalizeLabel($labels) - { - $normalizedLabels = []; - - foreach ($labels as $labelDefinition) { - $normalizedLabels[$labelDefinition['lang']] = $labelDefinition['label']; - } - - return $normalizedLabels; - } -} diff --git a/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php b/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php index ed7fd1ae9..57976f846 100644 --- a/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php +++ b/src/Bundle/ChillBudgetBundle/DependencyInjection/ChillBudgetExtension.php @@ -35,7 +35,6 @@ class ChillBudgetExtension extends Extension implements PrependExtensionInterfac $config = $this->processConfiguration($configuration, $configs); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config')); - $loader->load('services/config.yaml'); $loader->load('services/form.yaml'); $loader->load('services/repository.yaml'); $loader->load('services/security.yaml'); diff --git a/src/Bundle/ChillBudgetBundle/Form/ChargeType.php b/src/Bundle/ChillBudgetBundle/Form/ChargeType.php index 3dc2e230c..3356057cf 100644 --- a/src/Bundle/ChillBudgetBundle/Form/ChargeType.php +++ b/src/Bundle/ChillBudgetBundle/Form/ChargeType.php @@ -11,7 +11,6 @@ declare(strict_types=1); namespace Chill\BudgetBundle\Form; -use Chill\BudgetBundle\Config\ConfigRepository; use Chill\BudgetBundle\Entity\Charge; use Chill\BudgetBundle\Entity\ChargeKind; use Chill\BudgetBundle\Repository\ChargeKindRepository; @@ -25,13 +24,9 @@ use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; -use function array_flip; -use function asort; class ChargeType extends AbstractType { - protected ConfigRepository $configRepository; - protected TranslatableStringHelperInterface $translatableStringHelper; private ChargeKindRepository $repository; @@ -39,12 +34,10 @@ class ChargeType extends AbstractType private TranslatorInterface $translator; public function __construct( - ConfigRepository $configRepository, TranslatableStringHelperInterface $translatableStringHelper, ChargeKindRepository $repository, TranslatorInterface $translator ) { - $this->configRepository = $configRepository; $this->translatableStringHelper = $translatableStringHelper; $this->repository = $repository; $this->translator = $translator; @@ -116,19 +109,4 @@ class ChargeType extends AbstractType { return 'chill_budgetbundle_charge'; } - - private function getTypes() - { - $charges = $this->configRepository - ->getChargesLabels(true); - - // rewrite labels to filter in language - foreach ($charges as $key => $labels) { - $charges[$key] = $this->translatableStringHelper->localize($labels); - } - - asort($charges); - - return array_flip($charges); - } } diff --git a/src/Bundle/ChillBudgetBundle/Form/ResourceType.php b/src/Bundle/ChillBudgetBundle/Form/ResourceType.php index 106f50ad1..fd859217a 100644 --- a/src/Bundle/ChillBudgetBundle/Form/ResourceType.php +++ b/src/Bundle/ChillBudgetBundle/Form/ResourceType.php @@ -11,7 +11,6 @@ declare(strict_types=1); namespace Chill\BudgetBundle\Form; -use Chill\BudgetBundle\Config\ConfigRepository; use Chill\BudgetBundle\Entity\Resource; use Chill\BudgetBundle\Entity\ResourceKind; use Chill\BudgetBundle\Repository\ResourceKindRepository; @@ -24,12 +23,9 @@ use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; -use function array_flip; class ResourceType extends AbstractType { - protected ConfigRepository $configRepository; - protected TranslatableStringHelperInterface $translatableStringHelper; private ResourceKindRepository $repository; @@ -37,12 +33,10 @@ class ResourceType extends AbstractType private TranslatorInterface $translator; public function __construct( - ConfigRepository $configRepository, TranslatableStringHelperInterface $translatableStringHelper, ResourceKindRepository $repository, TranslatorInterface $translator ) { - $this->configRepository = $configRepository; $this->translatableStringHelper = $translatableStringHelper; $this->repository = $repository; $this->translator = $translator; @@ -98,19 +92,4 @@ class ResourceType extends AbstractType { return 'chill_budgetbundle_resource'; } - - private function getTypes() - { - $resources = $this->configRepository - ->getResourcesLabels(true); - - // rewrite labels to filter in language - foreach ($resources as $key => $labels) { - $resources[$key] = $this->translatableStringHelper->localize($labels); - } - - asort($resources); - - return array_flip($resources); - } } diff --git a/src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php b/src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php index f02d2e64a..096531c52 100644 --- a/src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php +++ b/src/Bundle/ChillBudgetBundle/Service/Summary/SummaryBudget.php @@ -11,13 +11,17 @@ declare(strict_types=1); namespace Chill\BudgetBundle\Service\Summary; -use Chill\BudgetBundle\Config\ConfigRepository; +use Chill\BudgetBundle\Entity\ChargeKind; +use Chill\BudgetBundle\Entity\ResourceKind; +use Chill\BudgetBundle\Repository\ChargeKindRepository; +use Chill\BudgetBundle\Repository\ResourceKindRepository; 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 RuntimeException; use function count; /** @@ -25,31 +29,36 @@ use function count; */ class SummaryBudget implements SummaryBudgetInterface { - private const QUERY_CHARGE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, 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_HOUSEHOLD = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, charge_id AS kind_id FROM chill_budget.charge WHERE (person_id IN (_ids_) OR household_id = ?) AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY charge_id'; - private const QUERY_CHARGE_BY_PERSON = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, type FROM chill_budget.charge WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type'; + private const QUERY_CHARGE_BY_PERSON = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, charge_id AS kind_id FROM chill_budget.charge WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY charge_id'; - private const QUERY_RESOURCE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, 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_HOUSEHOLD = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, resource_id AS kind_id FROM chill_budget.resource WHERE (person_id IN (_ids_) OR household_id = ?) AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY resource_id'; - private const QUERY_RESOURCE_BY_PERSON = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, type FROM chill_budget.resource WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type'; + private const QUERY_RESOURCE_BY_PERSON = 'select SUM(amount) AS sum, string_agg(comment, \'|\') AS comment, resource_id AS kind_id FROM chill_budget.resource WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY resource_id'; + + private ChargeKindRepository $chargeKindRepository; private array $chargeLabels; - private ConfigRepository $configRepository; - private EntityManagerInterface $em; + private ResourceKindRepository $resourceKindRepository; + private array $resourcesLabels; private TranslatableStringHelperInterface $translatableStringHelper; - public function __construct(EntityManagerInterface $em, ConfigRepository $configRepository, TranslatableStringHelperInterface $translatableStringHelper) - { + public function __construct( + EntityManagerInterface $em, + TranslatableStringHelperInterface $translatableStringHelper, + ResourceKindRepository $resourceKindRepository, + ChargeKindRepository $chargeKindRepository + ) { $this->em = $em; - $this->configRepository = $configRepository; - $this->chargeLabels = $configRepository->getChargesLabels(); - $this->resourcesLabels = $configRepository->getResourcesLabels(); $this->translatableStringHelper = $translatableStringHelper; + $this->resourceKindRepository = $resourceKindRepository; + $this->chargeKindRepository = $chargeKindRepository; } public function getSummaryForHousehold(?Household $household): array @@ -112,7 +121,7 @@ class SummaryBudget implements SummaryBudgetInterface $rsm = new ResultSetMapping(); $rsm ->addScalarResult('sum', 'sum') - ->addScalarResult('type', 'type') + ->addScalarResult('kind_id', 'kind_id') ->addScalarResult('comment', 'comment'); return $rsm; @@ -120,51 +129,62 @@ class SummaryBudget implements SummaryBudgetInterface private function getEmptyChargeArray(): array { - $keys = $this->configRepository->getChargesKeys(); - $labels = $this->chargeLabels; + $keys = array_map(static fn (ChargeKind $kind) => $kind->getId(), $this->chargeKindRepository->findAll()); - return array_combine($keys, array_map(function ($i) use ($labels) { - return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i]), 'comment' => '']; + return array_combine($keys, array_map(function ($id) { + return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->chargeKindRepository->find($id)->getName()), 'comment' => '']; }, $keys)); } private function getEmptyResourceArray(): array { - $keys = $this->configRepository->getResourcesKeys(); - $labels = $this->resourcesLabels; + $keys = array_map(static fn (ResourceKind $kind) => $kind->getId(), $this->resourceKindRepository->findAll()); - return array_combine($keys, array_map(function ($i) use ($labels) { - return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i]), 'comment' => '']; + return array_combine($keys, array_map(function ($id) { + return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->resourceKindRepository->find($id)->getName()), 'comment' => '']; }, $keys)); } private function rowToArray(array $rows, string $kind): array { + $result = []; + switch ($kind) { case 'charge': - $label = $this->chargeLabels; + foreach ($rows as $row) { + $chargeKind = $this->chargeKindRepository->find($row['kind_id']); - break; + if (null === $chargeKind) { + throw new RuntimeException('charge kind not found'); + } + $result[$chargeKind->getKind()] = [ + 'sum' => (float) $row['sum'], + 'label' => $this->translatableStringHelper->localize($chargeKind->getName()), + 'comment' => (string) $row['comment'], + ]; + } + + return $result; case 'resource': - $label = $this->resourcesLabels; + foreach ($rows as $row) { + $resourceKind = $this->resourceKindRepository->find($row['kind_id']); - break; + if (null === $resourceKind) { + throw new RuntimeException('charge kind not found'); + } + + $result[$resourceKind->getKind()] = [ + 'sum' => (float) $row['sum'], + 'label' => $this->translatableStringHelper->localize($resourceKind->getName()), + 'comment' => (string) $row['comment'], + ]; + } + + return $result; default: throw new LogicException(); } - - $result = []; - - foreach ($rows as $row) { - $result[$row['type']] = [ - 'sum' => (float) $row['sum'], - 'label' => $this->translatableStringHelper->localize($label[$row['type']]), - 'comment' => (string) $row['comment'], - ]; - } - - return $result; } } diff --git a/src/Bundle/ChillBudgetBundle/Templating/Twig.php b/src/Bundle/ChillBudgetBundle/Templating/Twig.php deleted file mode 100644 index b4395f375..000000000 --- a/src/Bundle/ChillBudgetBundle/Templating/Twig.php +++ /dev/null @@ -1,65 +0,0 @@ -configRepository = $configRepository; - $this->translatableStringHelper = $translatableStringHelper; - } - - public function displayLink($link, $family) - { - switch ($family) { - case 'resource': - return $this->translatableStringHelper->localize( - $this->configRepository->getResourcesLabels()[$link] - ); - - case 'charge': - return $this->translatableStringHelper->localize( - $this->configRepository->getChargesLabels()[$link] - ); - - default: - throw new UnexpectedValueException("This family of element: {$family} is not " - . "supported. Supported families are 'resource', 'charge'"); - } - } - - public function getFilters() - { - return [ - new TwigFilter('budget_element_type_display', [$this, 'displayLink'], ['is_safe' => ['html']]), - ]; - } -} diff --git a/src/Bundle/ChillBudgetBundle/config/services/config.yaml b/src/Bundle/ChillBudgetBundle/config/services/config.yaml deleted file mode 100644 index 21136db2f..000000000 --- a/src/Bundle/ChillBudgetBundle/config/services/config.yaml +++ /dev/null @@ -1,5 +0,0 @@ -services: - Chill\BudgetBundle\Config\ConfigRepository: - arguments: - $resources: '%chill_budget.resources%' - $charges: '%chill_budget.charges%' diff --git a/src/Bundle/ChillDocStoreBundle/Resources/public/vuejs/StoredObjectButton/helpers.ts b/src/Bundle/ChillDocStoreBundle/Resources/public/vuejs/StoredObjectButton/helpers.ts index dc746ace7..58d2ebc71 100644 --- a/src/Bundle/ChillDocStoreBundle/Resources/public/vuejs/StoredObjectButton/helpers.ts +++ b/src/Bundle/ChillDocStoreBundle/Resources/public/vuejs/StoredObjectButton/helpers.ts @@ -137,6 +137,10 @@ async function download_and_decrypt_doc(urlGenerator: string, keyData: JsonWebKe throw new Error("error while downloading raw file " + rawResponse.status + " " + rawResponse.statusText); } + if (iv.length === 0) { + return rawResponse.blob(); + } + const rawBuffer = await rawResponse.arrayBuffer(); try {