This commit is contained in:
Julien Fastré 2022-03-03 10:56:36 +01:00
parent 076728bf5f
commit ba24abf788
5 changed files with 28 additions and 28 deletions

View File

@ -19,7 +19,6 @@ use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use DateTime; use DateTime;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Monolog\DateTimeImmutable;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -129,28 +128,31 @@ class ElementController extends AbstractController
// TODO replace this by calculators // TODO replace this by calculators
$wholeCharges = $actualCharges; $wholeCharges = $actualCharges;
$wholeResources = $actualResources; $wholeResources = $actualResources;
foreach ($household->getCurrentPersons() as $person) { foreach ($household->getCurrentPersons() as $person) {
$wholeCharges = array_merge( $wholeCharges = array_merge(
$wholeCharges, $wholeCharges,
$this->em $this->em
->getRepository(Charge::class) ->getRepository(Charge::class)
->findByEntityAndDate($person, $now)); ->findByEntityAndDate($person, $now)
);
$wholeResources = array_merge( $wholeResources = array_merge(
$wholeResources, $wholeResources,
$this->em $this->em
->getRepository(Resource::class) ->getRepository(Resource::class)
->findByEntityAndDate($person, $now)); ->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' => $ressources,
'wholeResources' => array_filter($wholeResources, function (Resource $r) use ($now) { 'wholeResources' => array_filter($wholeResources, static function (Resource $r) use ($now) {
return $r->getStartDate() <= $now and ($r->getEndDate() === null or $r->getEndDate() >= $now); return $r->getStartDate() <= $now && ($r->getEndDate() === null || $r->getEndDate() >= $now);
}), }),
'wholeCharges' => array_filter($wholeCharges, function (Charge $c) use ($now) { 'wholeCharges' => array_filter($wholeCharges, static function (Charge $c) use ($now) {
return $c->getStartDate() <= $now and ($c->getEndDate() === null or $c->getEndDate() >= $now); return $c->getStartDate() <= $now && ($c->getEndDate() === null || $c->getEndDate() >= $now);
}), }),
'results' => $results ?? [], 'results' => $results ?? [],
]); ]);

View File

@ -34,7 +34,7 @@ class ResourceRepository extends EntityRepository
//->andWhere('c.startDate < :date') //->andWhere('c.startDate < :date')
// TODO: there is a misconception here, the end date must be lower or null. startDate are never 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'); //->andWhere('c.startDate < :date OR c.startDate IS NULL');
; ;
if (null !== $sort) { if (null !== $sort) {
$qb->orderBy($sort); $qb->orderBy($sort);

View File

@ -18,8 +18,8 @@ use Chill\PersonBundle\Repository\ResidentialAddressRepository;
use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Context; use Symfony\Component\Serializer\Annotation\Context;
use Symfony\Component\Serializer\Annotation\Groups;
/** /**
* @ORM\Entity(repositoryClass=ResidentialAddressRepository::class) * @ORM\Entity(repositoryClass=ResidentialAddressRepository::class)
@ -49,7 +49,7 @@ class ResidentialAddress
* @ORM\ManyToOne(targetEntity=Person::class) * @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=true) * @ORM\JoinColumn(nullable=true)
* @Groups({"read"}) * @Groups({"read"})
* @Context(normalizationContext={"groups"={"minimal"}}) * @Context(normalizationContext={"groups": {"minimal"}})
*/ */
private ?Person $hostPerson = null; private ?Person $hostPerson = null;

View File

@ -32,26 +32,11 @@ class ResidentialAddressRepository extends ServiceEntityRepository
parent::__construct($registry, ResidentialAddress::class); parent::__construct($registry, ResidentialAddress::class);
} }
/**
* @param Person $person
* @param DateTimeImmutable|null $at
* @return array|ResidentialAddress[]|null
*/
public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): array
{
return $this->buildQueryFindCurrentResidentialAddresses($person, $at)
->select('ra')
->getQuery()
->getResult();
}
public function buildQueryFindCurrentResidentialAddresses(Person $person, ?DateTimeImmutable $at = null): QueryBuilder public function buildQueryFindCurrentResidentialAddresses(Person $person, ?DateTimeImmutable $at = null): QueryBuilder
{ {
$date = null === $at ? new DateTimeImmutable('today') : $at; $date = null === $at ? new DateTimeImmutable('today') : $at;
$qb = $this->createQueryBuilder('ra'); $qb = $this->createQueryBuilder('ra');
$dateFilter = $qb->expr()->andX( $dateFilter = $qb->expr()->andX(
$qb->expr()->lte('ra.startDate', ':dateIn'), $qb->expr()->lte('ra.startDate', ':dateIn'),
$qb->expr()->orX( $qb->expr()->orX(
@ -69,6 +54,17 @@ class ResidentialAddressRepository extends ServiceEntityRepository
return $qb; return $qb;
} }
/**
* @return array|ResidentialAddress[]|null
*/
public function findCurrentResidentialAddressByPerson(Person $person, ?DateTimeImmutable $at = null): array
{
return $this->buildQueryFindCurrentResidentialAddresses($person, $at)
->select('ra')
->getQuery()
->getResult();
}
// /** // /**
// * @return ResidentialAddress[] Returns an array of ResidentialAddress objects // * @return ResidentialAddress[] Returns an array of ResidentialAddress objects
// */ // */

View File

@ -31,6 +31,8 @@ use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait; use Symfony\Component\Serializer\Normalizer\ObjectToPopulateTrait;
use function array_key_exists; use function array_key_exists;
use function count;
use function in_array;
/** /**
* Serialize a Person entity. * Serialize a Person entity.
@ -207,8 +209,8 @@ class PersonJsonNormalizer implements DenormalizerAwareInterface, NormalizerAwar
'gender' => $person->getGender(), 'gender' => $person->getGender(),
]; ];
if (in_array("minimal", $groups) && 1 === count($groups)) { if (in_array('minimal', $groups, true) && 1 === count($groups)) {
return $data; return $data;
} }
return array_merge($data, [ return array_merge($data, [