docgen normalization budget: fix budget when person is null

This commit is contained in:
2022-03-30 21:35:02 +02:00
parent bc43d8bae5
commit eb2bad0f47
5 changed files with 120 additions and 42 deletions

View File

@@ -23,7 +23,7 @@ use function count;
/**
* Helps to find a summary of the budget: the sum of resources and charges.
*/
class SummaryBudget
class SummaryBudget implements SummaryBudgetInterface
{
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';
@@ -52,26 +52,6 @@ class SummaryBudget
$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) {
@@ -101,8 +81,15 @@ class SummaryBudget
];
}
public function getSummaryForPerson(Person $person): array
public function getSummaryForPerson(?Person $person): array
{
if (null === $person) {
return [
'resources' => $this->getEmptyResourceArray(),
'charges' => $this->getEmptyChargeArray(),
];
}
$rsm = $this->buildRsm();
$resources = $this->em->createNativeQuery(self::QUERY_RESOURCE_BY_PERSON, $rsm)
@@ -128,6 +115,26 @@ class SummaryBudget
return $rsm;
}
private 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));
}
private 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));
}
private function rowToArray(array $rows, string $kind): array
{
switch ($kind) {