Fixed: [budget] remove deprecated config repository and fix summary budget when generating document

This commit is contained in:
Julien Fastré 2023-01-31 19:41:43 +01:00
parent 885256ac0d
commit de55ff920f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
8 changed files with 61 additions and 253 deletions

View File

@ -1,102 +0,0 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\BudgetBundle\Config;
class ConfigRepository
{
/**
* @var array
*/
protected $charges;
/**
* @var array
*/
protected $resources;
public function __construct($resources, $charges)
{
$this->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;
}
}

View File

@ -35,7 +35,6 @@ class ChillBudgetExtension extends Extension implements PrependExtensionInterfac
$config = $this->processConfiguration($configuration, $configs); $config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config')); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config'));
$loader->load('services/config.yaml');
$loader->load('services/form.yaml'); $loader->load('services/form.yaml');
$loader->load('services/repository.yaml'); $loader->load('services/repository.yaml');
$loader->load('services/security.yaml'); $loader->load('services/security.yaml');

View File

@ -11,7 +11,6 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Form; namespace Chill\BudgetBundle\Form;
use Chill\BudgetBundle\Config\ConfigRepository;
use Chill\BudgetBundle\Entity\Charge; use Chill\BudgetBundle\Entity\Charge;
use Chill\BudgetBundle\Entity\ChargeKind; use Chill\BudgetBundle\Entity\ChargeKind;
use Chill\BudgetBundle\Repository\ChargeKindRepository; 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\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use function array_flip;
use function asort;
class ChargeType extends AbstractType class ChargeType extends AbstractType
{ {
protected ConfigRepository $configRepository;
protected TranslatableStringHelperInterface $translatableStringHelper; protected TranslatableStringHelperInterface $translatableStringHelper;
private ChargeKindRepository $repository; private ChargeKindRepository $repository;
@ -39,12 +34,10 @@ class ChargeType extends AbstractType
private TranslatorInterface $translator; private TranslatorInterface $translator;
public function __construct( public function __construct(
ConfigRepository $configRepository,
TranslatableStringHelperInterface $translatableStringHelper, TranslatableStringHelperInterface $translatableStringHelper,
ChargeKindRepository $repository, ChargeKindRepository $repository,
TranslatorInterface $translator TranslatorInterface $translator
) { ) {
$this->configRepository = $configRepository;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->repository = $repository; $this->repository = $repository;
$this->translator = $translator; $this->translator = $translator;
@ -116,19 +109,4 @@ class ChargeType extends AbstractType
{ {
return 'chill_budgetbundle_charge'; 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);
}
} }

View File

@ -11,7 +11,6 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Form; namespace Chill\BudgetBundle\Form;
use Chill\BudgetBundle\Config\ConfigRepository;
use Chill\BudgetBundle\Entity\Resource; use Chill\BudgetBundle\Entity\Resource;
use Chill\BudgetBundle\Entity\ResourceKind; use Chill\BudgetBundle\Entity\ResourceKind;
use Chill\BudgetBundle\Repository\ResourceKindRepository; 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\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use function array_flip;
class ResourceType extends AbstractType class ResourceType extends AbstractType
{ {
protected ConfigRepository $configRepository;
protected TranslatableStringHelperInterface $translatableStringHelper; protected TranslatableStringHelperInterface $translatableStringHelper;
private ResourceKindRepository $repository; private ResourceKindRepository $repository;
@ -37,12 +33,10 @@ class ResourceType extends AbstractType
private TranslatorInterface $translator; private TranslatorInterface $translator;
public function __construct( public function __construct(
ConfigRepository $configRepository,
TranslatableStringHelperInterface $translatableStringHelper, TranslatableStringHelperInterface $translatableStringHelper,
ResourceKindRepository $repository, ResourceKindRepository $repository,
TranslatorInterface $translator TranslatorInterface $translator
) { ) {
$this->configRepository = $configRepository;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->repository = $repository; $this->repository = $repository;
$this->translator = $translator; $this->translator = $translator;
@ -98,19 +92,4 @@ class ResourceType extends AbstractType
{ {
return 'chill_budgetbundle_resource'; 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);
}
} }

View File

@ -11,13 +11,17 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Service\Summary; 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\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\ORM\Query\ResultSetMapping;
use LogicException; use LogicException;
use RuntimeException;
use function count; use function count;
/** /**
@ -25,31 +29,36 @@ use function count;
*/ */
class SummaryBudget implements SummaryBudgetInterface 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 array $chargeLabels;
private ConfigRepository $configRepository;
private EntityManagerInterface $em; private EntityManagerInterface $em;
private ResourceKindRepository $resourceKindRepository;
private array $resourcesLabels; private array $resourcesLabels;
private TranslatableStringHelperInterface $translatableStringHelper; 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->em = $em;
$this->configRepository = $configRepository;
$this->chargeLabels = $configRepository->getChargesLabels();
$this->resourcesLabels = $configRepository->getResourcesLabels();
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->resourceKindRepository = $resourceKindRepository;
$this->chargeKindRepository = $chargeKindRepository;
} }
public function getSummaryForHousehold(?Household $household): array public function getSummaryForHousehold(?Household $household): array
@ -112,7 +121,7 @@ class SummaryBudget implements SummaryBudgetInterface
$rsm = new ResultSetMapping(); $rsm = new ResultSetMapping();
$rsm $rsm
->addScalarResult('sum', 'sum') ->addScalarResult('sum', 'sum')
->addScalarResult('type', 'type') ->addScalarResult('kind_id', 'kind_id')
->addScalarResult('comment', 'comment'); ->addScalarResult('comment', 'comment');
return $rsm; return $rsm;
@ -120,51 +129,62 @@ class SummaryBudget implements SummaryBudgetInterface
private function getEmptyChargeArray(): array private function getEmptyChargeArray(): array
{ {
$keys = $this->configRepository->getChargesKeys(); $keys = array_map(static fn (ChargeKind $kind) => $kind->getId(), $this->chargeKindRepository->findAll());
$labels = $this->chargeLabels;
return array_combine($keys, array_map(function ($i) use ($labels) { return array_combine($keys, array_map(function ($id) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i]), 'comment' => '']; return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->chargeKindRepository->find($id)->getName()), 'comment' => ''];
}, $keys)); }, $keys));
} }
private function getEmptyResourceArray(): array private function getEmptyResourceArray(): array
{ {
$keys = $this->configRepository->getResourcesKeys(); $keys = array_map(static fn (ResourceKind $kind) => $kind->getId(), $this->resourceKindRepository->findAll());
$labels = $this->resourcesLabels;
return array_combine($keys, array_map(function ($i) use ($labels) { return array_combine($keys, array_map(function ($id) {
return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i]), 'comment' => '']; return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($this->resourceKindRepository->find($id)->getName()), 'comment' => ''];
}, $keys)); }, $keys));
} }
private function rowToArray(array $rows, string $kind): array private function rowToArray(array $rows, string $kind): array
{ {
$result = [];
switch ($kind) { switch ($kind) {
case 'charge': 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': 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: default:
throw new LogicException(); 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;
} }
} }

View File

@ -1,65 +0,0 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\BudgetBundle\Templating;
use Chill\BudgetBundle\Config\ConfigRepository;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use UnexpectedValueException;
class Twig extends AbstractExtension
{
/**
* @var ConfigRepository
*/
protected $configRepository;
/**
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
public function __construct(
ConfigRepository $configRepository,
TranslatableStringHelper $translatableStringHelper
) {
$this->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']]),
];
}
}

View File

@ -1,5 +0,0 @@
services:
Chill\BudgetBundle\Config\ConfigRepository:
arguments:
$resources: '%chill_budget.resources%'
$charges: '%chill_budget.charges%'

View File

@ -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); throw new Error("error while downloading raw file " + rawResponse.status + " " + rawResponse.statusText);
} }
if (iv.length === 0) {
return rawResponse.blob();
}
const rawBuffer = await rawResponse.arrayBuffer(); const rawBuffer = await rawResponse.arrayBuffer();
try { try {