Fix: more consistency in Resource and Charge repositories

This commit is contained in:
2023-03-28 16:59:08 +02:00
parent a64d75ffc9
commit 59147b2217
3 changed files with 83 additions and 84 deletions

View File

@@ -14,6 +14,8 @@ namespace Chill\BudgetBundle\Controller;
use Chill\BudgetBundle\Calculator\CalculatorManager;
use Chill\BudgetBundle\Entity\Charge;
use Chill\BudgetBundle\Entity\Resource;
use Chill\BudgetBundle\Repository\ChargeRepository;
use Chill\BudgetBundle\Repository\ResourceRepository;
use Chill\BudgetBundle\Security\Authorization\BudgetElementVoter;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
@@ -29,24 +31,32 @@ use function count;
class ElementController extends AbstractController
{
protected CalculatorManager $calculator;
private CalculatorManager $calculator;
protected LoggerInterface $chillMainLogger;
private LoggerInterface $chillMainLogger;
protected EntityManagerInterface $em;
private EntityManagerInterface $em;
protected TranslatorInterface $translator;
private ResourceRepository $resourceRepository;
private ChargeRepository $chargeRepository;
private TranslatorInterface $translator;
public function __construct(
EntityManagerInterface $em,
TranslatorInterface $translator,
LoggerInterface $chillMainLogger,
CalculatorManager $calculator
CalculatorManager $calculator,
ResourceRepository $resourceRepository,
ChargeRepository $chargeRepository,
) {
$this->em = $em;
$this->translator = $translator;
$this->chillMainLogger = $chillMainLogger;
$this->calculator = $calculator;
$this->resourceRepository = $resourceRepository;
$this->chargeRepository = $chargeRepository;
}
/**
@@ -59,24 +69,10 @@ class ElementController extends AbstractController
{
$this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $person);
$charges = $this->em
->getRepository(Charge::class)
->findByPerson($person);
$charges = $this->chargeRepository->findAllByEntity($person);
$resources = $this->resourceRepository->findAllByEntity($person);
$ressources = $this->em
->getRepository(Resource::class)
->findByPerson($person);
$now = new DateTime('now');
$actualCharges = $this->em
->getRepository(Charge::class)
->findByEntityAndDate($person, $now);
$actualResources = $this->em
->getRepository(Resource::class)
->findByEntityAndDate($person, $now);
$elements = array_merge($actualCharges, $actualResources);
$elements = array_merge($charges, $resources);
if (count($elements) > 0) {
$results = $this->calculator->calculateDefault($elements);
@@ -85,7 +81,7 @@ class ElementController extends AbstractController
return $this->render('ChillBudgetBundle:Person:index.html.twig', [
'person' => $person,
'charges' => $charges,
'resources' => $ressources,
'resources' => $resources,
'results' => $results ?? [],
]);
}
@@ -100,60 +96,19 @@ class ElementController extends AbstractController
{
$this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $household);
$charges = $this->em
->getRepository(Charge::class)
->findByHousehold($household);
$charges = $this->chargeRepository->findAllByEntity($household);
$resources = $this->resourceRepository->findAllByEntity($household);
$ressources = $this->em
->getRepository(Resource::class)
->findByHousehold($household);
$now = new DateTime('now');
$actualCharges = $this->em
->getRepository(Charge::class)
->findByEntityAndDate($household, $now);
$actualResources = $this->em
->getRepository(Resource::class)
->findByEntityAndDate($household, $now);
$elements = array_merge($actualCharges, $actualResources);
$elements = array_merge($charges, $resources);
if (count($elements) > 0) {
$results = $this->calculator->calculateDefault($elements);
}
// quick solution to calculate the sum, difference and amount from
// controller. This should be done from the calculators
// TODO replace this by calculators
$wholeCharges = $actualCharges;
$wholeResources = $actualResources;
foreach ($household->getCurrentPersons() as $person) {
$wholeCharges = array_merge(
$wholeCharges,
$this->em
->getRepository(Charge::class)
->findByEntityAndDate($person, $now)
);
$wholeResources = array_merge(
$wholeResources,
$this->em
->getRepository(Resource::class)
->findByEntityAndDate($person, $now)
);
}
return $this->render('ChillBudgetBundle:Household:index.html.twig', [
'household' => $household,
'charges' => $charges,
'resources' => $ressources,
'wholeResources' => array_filter($wholeResources, static function (Resource $r) use ($now) {
return $r->getStartDate() <= $now && ($r->getEndDate() === null || $r->getEndDate() >= $now);
}),
'wholeCharges' => array_filter($wholeCharges, static function (Charge $c) use ($now) {
return $c->getStartDate() <= $now && ($c->getEndDate() === null || $c->getEndDate() >= $now);
}),
'resources' => $resources,
'results' => $results ?? [],
]);
}