mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 02:23:51 +00:00
handle event PersonMoveEvent on Period
This commit is contained in:
@@ -15,21 +15,23 @@ use Chill\MainBundle\Entity\Address;
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use Symfony\Contracts\EventDispatcher\Event;
|
||||
|
||||
class PersonAddressMoveEvent extends Event
|
||||
{
|
||||
public const PERSON_MOVE_POST = 'chill_person.person_move_post';
|
||||
|
||||
private ?Address $nextAddress;
|
||||
private ?Address $nextAddress = null;
|
||||
|
||||
private ?HouseholdMember $nextMembership;
|
||||
private ?HouseholdMember $nextMembership = null;
|
||||
|
||||
private Person $person;
|
||||
|
||||
private ?Address $previousAddress;
|
||||
private ?Address $previousAddress = null;
|
||||
|
||||
private ?HouseholdMember $previousMembership;
|
||||
private ?HouseholdMember $previousMembership = null;
|
||||
|
||||
public function __construct(
|
||||
Person $person
|
||||
@@ -37,8 +39,39 @@ class PersonAddressMoveEvent extends Event
|
||||
$this->person = $person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the date of the move.
|
||||
*
|
||||
* It might be either:
|
||||
*
|
||||
* * the date of the new membership;
|
||||
* * or the date of the move
|
||||
* * or the date when the household leaving take place (the end date of the previous membership)
|
||||
*/
|
||||
public function getMoveDate(): DateTimeImmutable
|
||||
{
|
||||
if ($this->personLeaveWithoutHousehold()) {
|
||||
return $this->getPreviousMembership()->getEndDate();
|
||||
}
|
||||
|
||||
if ($this->personChangeHousehold()) {
|
||||
return $this->getNextMembership()->getStartDate();
|
||||
}
|
||||
|
||||
// person is changing address without household
|
||||
return DateTimeImmutable::createFromMutable($this->getNextAddress()->getValidFrom());
|
||||
}
|
||||
|
||||
public function getNextAddress(): ?Address
|
||||
{
|
||||
if (null !== $this->getNextMembership()) {
|
||||
return $this->getNextMembership()->getHousehold()
|
||||
->getCurrentAddress(
|
||||
$this->getMoveDate() === null ? null :
|
||||
DateTime::createFromImmutable($this->getMoveDate())
|
||||
);
|
||||
}
|
||||
|
||||
return $this->nextAddress;
|
||||
}
|
||||
|
||||
@@ -63,6 +96,14 @@ class PersonAddressMoveEvent extends Event
|
||||
|
||||
public function getPreviousAddress(): ?Address
|
||||
{
|
||||
if (null !== $this->getPreviousMembership()) {
|
||||
return $this->getPreviousMembership()->getHousehold()
|
||||
->getCurrentAddress(
|
||||
null === $this->getMoveDate() ? null :
|
||||
DateTime::createFromImmutable($this->getMoveDate())
|
||||
);
|
||||
}
|
||||
|
||||
return $this->previousAddress;
|
||||
}
|
||||
|
||||
@@ -85,11 +126,21 @@ class PersonAddressMoveEvent extends Event
|
||||
return $this->getPreviousAddress() !== $this->getNextAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the user change household (this include the fact that a person
|
||||
* leave household without a new one).
|
||||
*/
|
||||
public function personChangeHousehold(): bool
|
||||
{
|
||||
return $this->getPreviousHousehold() !== $this->getNextHousehold();
|
||||
}
|
||||
|
||||
public function personLeaveWithoutHousehold(): bool
|
||||
{
|
||||
return null === $this->getNextMembership()
|
||||
&& null === $this->getNextAddress();
|
||||
}
|
||||
|
||||
public function setNextAddress(?Address $nextAddress): PersonAddressMoveEvent
|
||||
{
|
||||
$this->nextAddress = $nextAddress;
|
||||
|
Reference in New Issue
Block a user