household editor: handle case when the person is repositionned in the

same household, and the person is already in a position "without
household"
This commit is contained in:
Julien Fastré 2022-03-30 17:00:07 +02:00
parent c477996acf
commit 83dfe530e9
2 changed files with 48 additions and 0 deletions

View File

@ -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;

View File

@ -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:.
*