mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
167 lines
5.9 KiB
PHP
167 lines
5.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\BudgetBundle\Service\Summary;
|
|
|
|
use Chill\BudgetBundle\Config\ConfigRepository;
|
|
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 function count;
|
|
|
|
/**
|
|
* Helps to find a summary of the budget: the sum of resources and charges.
|
|
*/
|
|
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';
|
|
|
|
private const QUERY_CHARGE_BY_PERSON = 'select SUM(amount) AS sum, type FROM chill_budget.charge WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type';
|
|
|
|
private const QUERY_RESOURCE_BY_HOUSEHOLD = 'select SUM(amount) AS sum, 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_PERSON = 'select SUM(amount) AS sum, type FROM chill_budget.resource WHERE person_id = ? AND NOW() BETWEEN startdate AND COALESCE(enddate, \'infinity\'::timestamp) GROUP BY type';
|
|
|
|
private array $chargeLabels;
|
|
|
|
private ConfigRepository $configRepository;
|
|
|
|
private EntityManagerInterface $em;
|
|
|
|
private array $resourcesLabels;
|
|
|
|
private TranslatableStringHelperInterface $translatableStringHelper;
|
|
|
|
public function __construct(EntityManagerInterface $em, ConfigRepository $configRepository, TranslatableStringHelperInterface $translatableStringHelper)
|
|
{
|
|
$this->em = $em;
|
|
$this->configRepository = $configRepository;
|
|
$this->chargeLabels = $configRepository->getChargesLabels();
|
|
$this->resourcesLabels = $configRepository->getResourcesLabels();
|
|
$this->translatableStringHelper = $translatableStringHelper;
|
|
}
|
|
|
|
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
|
|
{
|
|
if (null === $person) {
|
|
return [
|
|
'resources' => $this->getEmptyResourceArray(),
|
|
'charges' => $this->getEmptyChargeArray(),
|
|
];
|
|
}
|
|
|
|
$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 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) {
|
|
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;
|
|
}
|
|
}
|