From 83dfe530e912a23628516ae3816287ce9953d812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 30 Mar 2022 17:00:07 +0200 Subject: [PATCH] household editor: handle case when the person is repositionned in the same household, and the person is already in a position "without household" --- .../Household/MembersEditor.php | 14 ++++++++ .../Tests/Household/MembersEditorTest.php | 34 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php index 812307a14..1c7cbb538 100644 --- a/src/Bundle/ChillPersonBundle/Household/MembersEditor.php +++ b/src/Bundle/ChillPersonBundle/Household/MembersEditor.php @@ -119,6 +119,20 @@ class MembersEditor if ($person->getCurrentHousehold($date) === $this->household) { $this->leaveMovement($date, $person); } + + // if there are multiple belongings not sharing household, close the others + foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) { + if ($participation === $membership) { + continue; + } + + if ($participation->getHousehold() === $this->household + && ($participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate()) + && $participation->getStartDate() <= $membership->getStartDate() + ) { + $participation->setEndDate($membership->getStartDate()); + } + } } $this->membershipsAffected[] = $membership; diff --git a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php index 69603031a..77eeed387 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Household/MembersEditorTest.php @@ -40,6 +40,40 @@ final class MembersEditorTest extends TestCase $this->factory = $this->buildMembersEditorFactory(); } + public function testAddingParticipationNotSharingHouseholdCloseTheOldOnes() + { + $person = new Person(); + $position = (new Position())->setShareHousehold(false); + $household = new Household(); + + // set a first time the person in position + $factory = $this->buildMembersEditorFactory(); + $editor = $factory->createEditor($household); + + $editor->addMovement($aMonthAgo = new DateTimeImmutable('1 month ago'), $person, $position); + + // set a second time the person in position + $factory = $this->buildMembersEditorFactory(); + $editor = $factory->createEditor($household); + + $editor->addMovement($yesterday = new DateTimeImmutable('yesterday'), $person, $position); + + $this->assertCount(2, $person->getHouseholdParticipationsNotShareHousehold()); + + $startDates = []; + $endDates = []; + + foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) { + $startDates[] = $participation->getStartDate(); + $endDates[] = $participation->getEndDate(); + } + + $this->assertContains($aMonthAgo, $startDates); + $this->assertContains($yesterday, $startDates); + $this->assertContains($yesterday, $endDates); + $this->assertContains(null, $endDates); + } + /** * We test here a move for a person:. *