person: create a person with address (and household without position (remove required position for household member)

This commit is contained in:
nobohan
2022-04-19 18:01:25 +02:00
parent c214c2f4a4
commit bad5506b98
8 changed files with 80 additions and 70 deletions

View File

@@ -55,7 +55,7 @@ class MembersEditor
$this->eventDispatcher = $eventDispatcher;
}
public function addMovement(DateTimeImmutable $date, Person $person, Position $position, ?bool $holder = false, ?string $comment = null): self
public function addMovement(DateTimeImmutable $date, Person $person, ?Position $position, ?bool $holder = false, ?string $comment = null): self
{
if (null === $this->household) {
throw new LogicException('You must define a household first');
@@ -69,72 +69,75 @@ class MembersEditor
->setComment($comment);
$this->household->addMember($membership);
if ($position->getShareHousehold()) {
// launch event only if moving to a "share household" position,
// and if the destination household is different than the previous one
$event = new PersonAddressMoveEvent($person);
$event->setNextMembership($membership);
if (null !== $position) {
if ($position->getShareHousehold()) {
// launch event only if moving to a "share household" position,
// and if the destination household is different than the previous one
$event = new PersonAddressMoveEvent($person);
$event->setNextMembership($membership);
$counter = 0;
$counter = 0;
foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) {
if ($participation === $membership) {
continue;
foreach ($person->getHouseholdParticipationsShareHousehold() as $participation) {
if ($participation === $membership) {
continue;
}
if ($participation->getStartDate() > $membership->getStartDate()) {
continue;
}
++$counter;
if ($participation->getEndDate() === null || $participation->getEndDate() > $date) {
$participation->setEndDate($date);
$this->membershipsAffected[] = $participation;
$this->oldMembershipsHashes[] = spl_object_hash($participation);
if ($participation->getHousehold() !== $this->household) {
$event->setPreviousMembership($participation);
$this->events[] = $event;
}
}
}
if ($participation->getStartDate() > $membership->getStartDate()) {
continue;
// send also the event if there was no participation before
if (0 === $counter) {
$this->events[] = $event;
}
++$counter;
foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) {
if ($participation->getHousehold() === $this->household
&& $participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate()
&& $participation->getStartDate() <= $membership->getStartDate()
) {
$participation->setEndDate($membership->getStartDate());
}
}
} else {
// if a members is moved to the same household than the one he belongs to,
// we should make it leave the household
if ($person->getCurrentHousehold($date) === $this->household) {
$this->leaveMovement($date, $person);
}
if ($participation->getEndDate() === null || $participation->getEndDate() > $date) {
$participation->setEndDate($date);
$this->membershipsAffected[] = $participation;
$this->oldMembershipsHashes[] = spl_object_hash($participation);
// 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) {
$event->setPreviousMembership($participation);
$this->events[] = $event;
if ($participation->getHousehold() === $this->household
&& ($participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate())
&& $participation->getStartDate() <= $membership->getStartDate()
) {
$participation->setEndDate($membership->getStartDate());
}
}
}
// send also the event if there was no participation before
if (0 === $counter) {
$this->events[] = $event;
}
foreach ($person->getHouseholdParticipationsNotShareHousehold() as $participation) {
if ($participation->getHousehold() === $this->household
&& $participation->getEndDate() === null || $participation->getEndDate() > $membership->getStartDate()
&& $participation->getStartDate() <= $membership->getStartDate()
) {
$participation->setEndDate($membership->getStartDate());
}
}
} else {
// if a members is moved to the same household than the one he belongs to,
// we should make it leave the household
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;
$this->persistables[] = $membership;