diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php index 63c0657a0..e78838e22 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php @@ -72,6 +72,15 @@ class HouseholdMemberController extends ApiController } /** + * Route for showing an editor to leave a household. + * + * Possibles arguments are: + * + * * persons[]: an id of the person to add to the form + * * household: the id of the destination household + * * allow_leave_without_household: if present, the editor will allow + * to leave household without joining another + * * @Route( * "/{_locale}/person/household/members/editor", * name="chill_person_household_members_editor" diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php index db7ef2c2b..ce15bbf9f 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php @@ -102,8 +102,7 @@ class Household { $criteria = new Criteria(); $expr = Criteria::expr(); - $date = $now === null ? (new \DateTimeImmutable('now')) : $now; - + $date = $now === null ? (new \DateTimeImmutable('today')) : $now; $criteria ->where($expr->orX( @@ -135,7 +134,7 @@ class Household { $criteria = new Criteria(); $expr = Criteria::expr(); - $date = $now === null ? (new \DateTimeImmutable('now')) : $now; + $date = $now === null ? (new \DateTimeImmutable('today')) : $now; $criteria ->where( diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php index 58a731697..b891bbc15 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php @@ -50,9 +50,9 @@ class HouseholdMember private ?string $comment = NULL; /** - * @ORM\Column(type="boolean") + * @ORM\Column(type="boolean", name="sharedhousehold") */ - private bool $sharedHousehold = false; + private bool $shareHousehold = false; /** * @ORM\Column(type="boolean", options={"default": false}) @@ -98,7 +98,7 @@ class HouseholdMember } $this->position = $position; - $this->sharedHousehold = $position->getShareHousehold(); + $this->shareHousehold = $position->getShareHousehold(); return $this; } @@ -144,7 +144,7 @@ class HouseholdMember */ public function getShareHousehold(): ?bool { - return $this->sharedHousehold; + return $this->shareHousehold; } diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index 0d9bb7cf6..0ec04bf35 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -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); + } } diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig index 3fe5c56db..4fef55ceb 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig @@ -62,6 +62,19 @@