mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Fix: more consistency in Resource and Charge repositories
This commit is contained in:
parent
a64d75ffc9
commit
59147b2217
@ -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 ?? [],
|
||||
]);
|
||||
}
|
||||
|
@ -11,9 +11,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\BudgetBundle\Repository;
|
||||
|
||||
use Chill\BudgetBundle\Entity\Charge;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* ChargeRepository.
|
||||
@ -21,24 +24,44 @@ use Doctrine\ORM\EntityRepository;
|
||||
* This class was generated by the Doctrine ORM. Add your own custom
|
||||
* 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)
|
||||
{
|
||||
$qb = $this->createQueryBuilder('c');
|
||||
|
||||
$entityStr = $entity instanceof Person ? 'person' : 'household';
|
||||
|
||||
$qb->where("c.{$entityStr} = :{$entityStr}")
|
||||
->andWhere('c.startDate < :date')
|
||||
->andWhere('c.startDate < :date OR c.startDate IS NULL');
|
||||
$qb->where("c.{$entityStr} = :entity")
|
||||
->andWhere('c.startDate <= :date')
|
||||
->andWhere('c.endDate > :date OR c.endDate IS NULL');
|
||||
|
||||
if (null !== $sort) {
|
||||
$qb->orderBy($sort);
|
||||
}
|
||||
|
||||
$qb->setParameters([
|
||||
$entityStr => $entity,
|
||||
'entity' => $entity,
|
||||
'date' => $date,
|
||||
]);
|
||||
|
||||
|
@ -11,9 +11,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\BudgetBundle\Repository;
|
||||
|
||||
use Chill\BudgetBundle\Entity\Resource;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use DateTime;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* ResourceRepository.
|
||||
@ -21,28 +25,45 @@ use Doctrine\ORM\EntityRepository;
|
||||
* This class was generated by the Doctrine ORM. Add your own custom
|
||||
* 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');
|
||||
|
||||
$entityStr = $entity instanceof Person ? 'person' : 'household';
|
||||
|
||||
$qb->where("c.{$entityStr} = :{$entityStr}")
|
||||
// TODO: in controller, the budget and charges asked are also for future and actual
|
||||
//->andWhere('c.startDate < :date')
|
||||
// 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');
|
||||
;
|
||||
$qb->where("c.{$entityStr} = :entity")
|
||||
->andWhere('c.startDate <= :date')
|
||||
->andWhere('c.endDate > :date OR c.endDate IS NULL');
|
||||
|
||||
if (null !== $sort) {
|
||||
$qb->orderBy($sort);
|
||||
}
|
||||
|
||||
$qb->setParameters([
|
||||
$entityStr => $entity,
|
||||
//'date' => $date,
|
||||
'entity' => $entity,
|
||||
'date' => $date,
|
||||
]);
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
|
Loading…
x
Reference in New Issue
Block a user