add link to household or household editor

This commit is contained in:
2021-06-10 15:17:15 +02:00
parent 3e39dd9a1e
commit 38c06977ca
8 changed files with 128 additions and 9 deletions

View File

@@ -25,6 +25,7 @@ namespace Chill\PersonBundle\Entity;
use ArrayIterator;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Country;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\MaritalStatus;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\MainBundle\Entity\HasCenterInterface;
@@ -281,6 +282,11 @@ class Person implements HasCenterInterface
*/
private Collection $householdParticipations;
/**
* Cache the computation of household
*/
private array $currentHouseholdAt = [];
/**
* Person constructor.
*
@@ -1202,4 +1208,43 @@ class Person implements HasCenterInterface
{
return $this->householdParticipations;
}
public function getCurrentHousehold(?\DateTimeImmutable $at = null): ?Household
{
$criteria = new Criteria();
$expr = Criteria::expr();
$date = NULL === $at ? new \DateTimeImmutable('now') : $at;
$datef = $date->format('Y-m-d');
if (
NULL !== ($this->currentHouseholdAt[$datef] ?? NULL)) {
return $this->currentHouseholdAt[$datef];
}
$criteria
->where(
$expr->andX(
$expr->lte('startDate', $date),
$expr->orX(
$expr->isNull('endDate'),
$expr->gte('endDate', $date)
),
$expr->eq('shareHousehold', true)
)
);
$participations = $this->getHouseholdParticipations()
->matching($criteria)
;
return $participations->count() > 0 ?
$this->currentHouseholdAt[$datef] = $participations->first()
->getHousehold()
: null;
}
public function isSharingHousehold(?\DateTimeImmutable $at = null): bool
{
return NULL !== $this->getCurrentHousehold($at);
}
}