diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index dcfca54c1..7efbb3eca 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -1264,15 +1264,24 @@ class Person implements HasCenterInterface } public function getCurrentHousehold(?\DateTimeImmutable $at = null): ?Household + { + $participation = $this->getCurrentHouseholdParticipationShareHousehold($at); + + return $participation instanceof HouseholdMember ? + $participation->getHousehold() + : null; + } + + public function getCurrentHouseholdParticipationShareHousehold(?\DateTimeImmutable $at = null): ?HouseholdMember { $criteria = new Criteria(); $expr = Criteria::expr(); - $date = NULL === $at ? new \DateTimeImmutable('now') : $at; + $date = NULL === $at ? new \DateTimeImmutable('today') : $at; $datef = $date->format('Y-m-d'); if ( - NULL !== ($this->currentHouseholdAt[$datef] ?? NULL)) { - return $this->currentHouseholdAt[$datef]; + NULL !== ($this->currentHouseholdParticipationAt[$datef] ?? NULL)) { + return $this->currentHouseholdParticipationAt[$datef]; } $criteria @@ -1281,7 +1290,7 @@ class Person implements HasCenterInterface $expr->lte('startDate', $date), $expr->orX( $expr->isNull('endDate'), - $expr->gte('endDate', $date) + $expr->gt('endDate', $date) ), $expr->eq('shareHousehold', true) ) @@ -1292,8 +1301,7 @@ class Person implements HasCenterInterface ; return $participations->count() > 0 ? - $this->currentHouseholdAt[$datef] = $participations->first() - ->getHousehold() + $this->currentHouseholdParticipationAt[$datef] = $participations->first() : null; } diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdMemberControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdMemberControllerTest.php index fcaa4ae34..e2af41f89 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdMemberControllerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/HouseholdMemberControllerTest.php @@ -61,6 +61,19 @@ class HouseholdMemberControllerTest extends WebTestCase ], true) ); + if ($client->getResponse()->getStatusCode() === 422) { + var_dump(\json_decode($client->getResponse()->getContent(), true)); + var_dump($date); + + $household = self::$container->get(EntityManagerInterface::class) + ->getRepository(Household::class) + ->find($householdId); + + $ms = $household->getMembers()->filter(fn ($m) => $m->getPerson()->getId() === $personId); + foreach ($ms as $m) { + var_dump("id: {$m->getId()}, startDate: {$m->getStartDate()} endDate: {$m->getEndDate()}"); + } + } $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode() @@ -111,6 +124,20 @@ class HouseholdMemberControllerTest extends WebTestCase true) ); + if ($client->getResponse()->getStatusCode() === 422) { + var_dump(\json_decode($client->getResponse()->getContent(), true)); + var_dump($date); + + $household = self::$container->get(EntityManagerInterface::class) + ->getRepository(Household::class) + ->find($householdId); + + $ms = $household->getMembers()->filter(fn ($m) => $m->getPerson()->getId() === $personId); + foreach ($ms as $m) { + var_dump("id: {$m->getId()}, startDate: {$m->getStartDate()} endDate: {$m->getEndDate()}"); + } + } + $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode() ); @@ -168,6 +195,19 @@ class HouseholdMemberControllerTest extends WebTestCase true) ); + if ($client->getResponse()->getStatusCode() === 422) { + var_dump(\json_decode($client->getResponse()->getContent(), true)); + var_dump($date); + + $household = self::$container->get(EntityManagerInterface::class) + ->getRepository(Household::class) + ->find($householdId); + + $ms = $household->getMembers()->filter(fn ($m) => $m->getPerson()->getId() === $personId); + foreach ($ms as $m) { + var_dump("id: {$m->getId()}, startDate: {$m->getStartDate()} endDate: {$m->getEndDate()}"); + } + } $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode() ); @@ -205,19 +245,18 @@ class HouseholdMemberControllerTest extends WebTestCase { self::bootKernel(); $em = self::$container->get(EntityManagerInterface::class); + $yesterday = new \DateTimeImmutable('yesterday'); $personIds = $em->createQuery("SELECT p.id FROM ".Person::class." p ". "JOIN p.center c ". - "JOIN p.householdParticipations hp ". "WHERE ". - "c.name = :center ". - "AND hp.startDate < CURRENT_DATE() ". - "AND hp.endDate IS NULL " + "c.name = :center " ) ->setParameter('center', "Center A") ->setMaxResults(100) ->getScalarResult() ; + \shuffle($personIds); $household = new Household(); @@ -229,12 +268,29 @@ class HouseholdMemberControllerTest extends WebTestCase ->getResult() ; - yield [ - \array_pop($personIds)['id'], - $household->getId(), - $positions[\random_int(0, count($positions) - 1)]['id'], - new \DateTimeImmutable('today') - ]; + $i = 0; + do { + $id = \array_pop($personIds)['id']; + $person = self::$container->get(EntityManagerInterface::class) + ->getRepository(Person::Class) + ->find($id); + + $participation = $person->getCurrentHouseholdParticipationShareHousehold(); + + if (NULL !== $participation && NULL === $participation->getEndDate() + && $participation->getStartDate() > $yesterday) { + continue; + } + + $i++; + + yield [ + $id, + $household->getId(), + $positions[\random_int(0, count($positions) - 1)]['id'], + new \DateTimeImmutable('tomorrow') + ]; + } while ($i <= 1); } public function provideValidDataEditMember(): \Iterator