Fix: more consistency in Resource and Charge repositories

This commit is contained in:
Julien Fastré 2023-03-28 16:59:08 +02:00
parent a64d75ffc9
commit 59147b2217
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
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\Calculator\CalculatorManager;
use Chill\BudgetBundle\Entity\Charge; use Chill\BudgetBundle\Entity\Charge;
use Chill\BudgetBundle\Entity\Resource; use Chill\BudgetBundle\Entity\Resource;
use Chill\BudgetBundle\Repository\ChargeRepository;
use Chill\BudgetBundle\Repository\ResourceRepository;
use Chill\BudgetBundle\Security\Authorization\BudgetElementVoter; use Chill\BudgetBundle\Security\Authorization\BudgetElementVoter;
use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
@ -29,24 +31,32 @@ use function count;
class ElementController extends AbstractController 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( public function __construct(
EntityManagerInterface $em, EntityManagerInterface $em,
TranslatorInterface $translator, TranslatorInterface $translator,
LoggerInterface $chillMainLogger, LoggerInterface $chillMainLogger,
CalculatorManager $calculator CalculatorManager $calculator,
ResourceRepository $resourceRepository,
ChargeRepository $chargeRepository,
) { ) {
$this->em = $em; $this->em = $em;
$this->translator = $translator; $this->translator = $translator;
$this->chillMainLogger = $chillMainLogger; $this->chillMainLogger = $chillMainLogger;
$this->calculator = $calculator; $this->calculator = $calculator;
$this->resourceRepository = $resourceRepository;
$this->chargeRepository = $chargeRepository;
} }
/** /**
@ -59,24 +69,10 @@ class ElementController extends AbstractController
{ {
$this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $person); $this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $person);
$charges = $this->em $charges = $this->chargeRepository->findAllByEntity($person);
->getRepository(Charge::class) $resources = $this->resourceRepository->findAllByEntity($person);
->findByPerson($person);
$ressources = $this->em $elements = array_merge($charges, $resources);
->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);
if (count($elements) > 0) { if (count($elements) > 0) {
$results = $this->calculator->calculateDefault($elements); $results = $this->calculator->calculateDefault($elements);
@ -85,7 +81,7 @@ class ElementController extends AbstractController
return $this->render('ChillBudgetBundle:Person:index.html.twig', [ return $this->render('ChillBudgetBundle:Person:index.html.twig', [
'person' => $person, 'person' => $person,
'charges' => $charges, 'charges' => $charges,
'resources' => $ressources, 'resources' => $resources,
'results' => $results ?? [], 'results' => $results ?? [],
]); ]);
} }
@ -100,60 +96,19 @@ class ElementController extends AbstractController
{ {
$this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $household); $this->denyAccessUnlessGranted(BudgetElementVoter::SEE, $household);
$charges = $this->em $charges = $this->chargeRepository->findAllByEntity($household);
->getRepository(Charge::class) $resources = $this->resourceRepository->findAllByEntity($household);
->findByHousehold($household);
$ressources = $this->em $elements = array_merge($charges, $resources);
->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);
if (count($elements) > 0) { if (count($elements) > 0) {
$results = $this->calculator->calculateDefault($elements); $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', [ return $this->render('ChillBudgetBundle:Household:index.html.twig', [
'household' => $household, 'household' => $household,
'charges' => $charges, 'charges' => $charges,
'resources' => $ressources, 'resources' => $resources,
'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);
}),
'results' => $results ?? [], 'results' => $results ?? [],
]); ]);
} }

View File

@ -11,9 +11,12 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Repository; namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\Charge;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use DateTime; use DateTime;
use Doctrine\ORM\EntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/** /**
* ChargeRepository. * ChargeRepository.
@ -21,24 +24,44 @@ use Doctrine\ORM\EntityRepository;
* This class was generated by the Doctrine ORM. Add your own custom * This class was generated by the Doctrine ORM. Add your own custom
* repository methods below. * repository methods below.
*/ */
class ChargeRepository extends EntityRepository class ChargeRepository extends ServiceEntityRepository
{ {
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Charge::class);
}
/**
* @return Charge[]
*/
public function findAllByEntity(Person|Household $entity): array
{
$qb = $this->createQueryBuilder('c');
$property = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$property} = :entity")
->setParameter('entity', $entity);
return $qb->getQuery()->getResult();
}
public function findByEntityAndDate($entity, DateTime $date, $sort = null) public function findByEntityAndDate($entity, DateTime $date, $sort = null)
{ {
$qb = $this->createQueryBuilder('c'); $qb = $this->createQueryBuilder('c');
$entityStr = $entity instanceof Person ? 'person' : 'household'; $entityStr = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$entityStr} = :{$entityStr}") $qb->where("c.{$entityStr} = :entity")
->andWhere('c.startDate < :date') ->andWhere('c.startDate <= :date')
->andWhere('c.startDate < :date OR c.startDate IS NULL'); ->andWhere('c.endDate > :date OR c.endDate IS NULL');
if (null !== $sort) { if (null !== $sort) {
$qb->orderBy($sort); $qb->orderBy($sort);
} }
$qb->setParameters([ $qb->setParameters([
$entityStr => $entity, 'entity' => $entity,
'date' => $date, 'date' => $date,
]); ]);

View File

@ -11,9 +11,13 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Repository; namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\Resource;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use DateTime; use DateTime;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/** /**
* ResourceRepository. * ResourceRepository.
@ -21,28 +25,45 @@ use Doctrine\ORM\EntityRepository;
* This class was generated by the Doctrine ORM. Add your own custom * This class was generated by the Doctrine ORM. Add your own custom
* repository methods below. * repository methods below.
*/ */
class ResourceRepository extends EntityRepository class ResourceRepository extends ServiceEntityRepository
{ {
public function findByEntityAndDate($entity, DateTime $date, $sort = null) public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Resource::class);
}
/**
* @return Resource[]
*/
public function findAllByEntity(Person|Household $entity): array
{
$qb = $this->createQueryBuilder('r');
$property = $entity instanceof Person ? 'person' : 'household';
$qb->where("r.{$property} = :entity")
->setParameter('entity', $entity);
return $qb->getQuery()->getResult();
}
public function findByEntityAndDate(Person|Household $entity, DateTime $date, $sort = null)
{ {
$qb = $this->createQueryBuilder('c'); $qb = $this->createQueryBuilder('c');
$entityStr = $entity instanceof Person ? 'person' : 'household'; $entityStr = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$entityStr} = :{$entityStr}") $qb->where("c.{$entityStr} = :entity")
// TODO: in controller, the budget and charges asked are also for future and actual ->andWhere('c.startDate <= :date')
//->andWhere('c.startDate < :date') ->andWhere('c.endDate > :date OR c.endDate IS NULL');
// TODO: there is a misconception here, the end date must be lower or null. startDate are never null
//->andWhere('c.startDate < :date OR c.startDate IS NULL');
;
if (null !== $sort) { if (null !== $sort) {
$qb->orderBy($sort); $qb->orderBy($sort);
} }
$qb->setParameters([ $qb->setParameters([
$entityStr => $entity, 'entity' => $entity,
//'date' => $date, 'date' => $date,
]); ]);
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();