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

This commit is contained in:
2023-01-31 19:41:43 +01:00
parent 885256ac0d
commit de55ff920f
8 changed files with 61 additions and 253 deletions

View File

@@ -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;
}
}