mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
PersonAddressMove: fix tests, address history on household, and household::getMembersOnRange
This commit is contained in:
@@ -116,66 +116,6 @@ class Household
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAddressesOrdered(): array
|
||||
{
|
||||
$addresses = $this->getAddresses()->toArray();
|
||||
usort($addresses, function (Address $a, Address $b) {
|
||||
$validFromA = $a->getValidFrom()->format('Y-m-d');
|
||||
$validFromB = $b->getValidFrom()->format('Y-m-d');
|
||||
|
||||
if ($a === $b) {
|
||||
if (null === $a->getId()) {
|
||||
return 1;
|
||||
}
|
||||
if (null === $b->getId()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return $a->getId() <=> $b->getId();
|
||||
}
|
||||
|
||||
return $validFromA <=> $validFromB;
|
||||
});
|
||||
|
||||
return $addresses;
|
||||
}
|
||||
|
||||
public function getPreviousAddressOf(Address $address): ?Address
|
||||
{
|
||||
$iterator = new ArrayIterator($this->getAddressesOrdered());
|
||||
$iterator->rewind();
|
||||
|
||||
while ($iterator->valid()) {
|
||||
$current = $iterator->current();
|
||||
$iterator->next();
|
||||
|
||||
if ($iterator->valid()) {
|
||||
if ($address === $iterator->current()) {
|
||||
return $current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function makeAddressConsistent(): void
|
||||
{
|
||||
$iterator = new ArrayIterator($this->getAddressesOrdered());
|
||||
|
||||
$iterator->rewind();
|
||||
|
||||
while ($iterator->valid()) {
|
||||
$current = $iterator->current();
|
||||
|
||||
$iterator->next();
|
||||
|
||||
if ($iterator->valid()) {
|
||||
$current->setValidTo($iterator->current()->getValidFrom());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addComposition(HouseholdComposition $composition): self
|
||||
{
|
||||
if (!$this->compositions->contains($composition)) {
|
||||
@@ -211,6 +151,31 @@ class Household
|
||||
return $this->addresses;
|
||||
}
|
||||
|
||||
public function getAddressesOrdered(): array
|
||||
{
|
||||
$addresses = $this->getAddresses()->toArray();
|
||||
usort($addresses, static function (Address $a, Address $b) {
|
||||
$validFromA = $a->getValidFrom()->format('Y-m-d');
|
||||
$validFromB = $b->getValidFrom()->format('Y-m-d');
|
||||
|
||||
if ($a === $b) {
|
||||
if (null === $a->getId()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (null === $b->getId()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return $a->getId() <=> $b->getId();
|
||||
}
|
||||
|
||||
return $validFromA <=> $validFromB;
|
||||
});
|
||||
|
||||
return $addresses;
|
||||
}
|
||||
|
||||
public function getCommentMembers(): CommentEmbeddable
|
||||
{
|
||||
return $this->commentMembers;
|
||||
@@ -412,24 +377,25 @@ class Household
|
||||
|
||||
public function getMembersOnRange(DateTimeImmutable $from, ?DateTimeImmutable $to): Collection
|
||||
{
|
||||
$criteria = new Criteria();
|
||||
$expr = Criteria::expr();
|
||||
return $this->getMembers()->filter(static function (HouseholdMember $m) use ($from, $to) {
|
||||
if (null === $m->getEndDate() && null !== $to) {
|
||||
return $m->getStartDate() <= $to;
|
||||
}
|
||||
|
||||
$criteria->where(
|
||||
$expr->gte('startDate', $from)
|
||||
);
|
||||
if (null === $to) {
|
||||
return $m->getStartDate() >= $from || null === $m->getEndDate();
|
||||
}
|
||||
|
||||
if (null !== $to) {
|
||||
$criteria->andWhere(
|
||||
$expr->orX(
|
||||
$expr->lte('endDate', $to),
|
||||
$expr->eq('endDate', null)
|
||||
),
|
||||
);
|
||||
}
|
||||
if (null !== $m->getEndDate() && $m->getEndDate() < $from) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getMembers()
|
||||
->matching($criteria);
|
||||
if ($m->getStartDate() <= $to) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public function getNonCurrentMembers(?DateTimeImmutable $now = null): Collection
|
||||
@@ -472,6 +438,25 @@ class Household
|
||||
return $this->getNonCurrentMembers($now)->matching($criteria);
|
||||
}
|
||||
|
||||
public function getPreviousAddressOf(Address $address): ?Address
|
||||
{
|
||||
$iterator = new ArrayIterator($this->getAddressesOrdered());
|
||||
$iterator->rewind();
|
||||
|
||||
while ($iterator->valid()) {
|
||||
$current = $iterator->current();
|
||||
$iterator->next();
|
||||
|
||||
if ($iterator->valid()) {
|
||||
if ($iterator->current() === $address) {
|
||||
return $current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getWaitingForBirth(): bool
|
||||
{
|
||||
return $this->waitingForBirth;
|
||||
@@ -516,6 +501,23 @@ class Household
|
||||
} while ($iterator->valid());
|
||||
}
|
||||
|
||||
public function makeAddressConsistent(): void
|
||||
{
|
||||
$iterator = new ArrayIterator($this->getAddressesOrdered());
|
||||
|
||||
$iterator->rewind();
|
||||
|
||||
while ($iterator->valid()) {
|
||||
$current = $iterator->current();
|
||||
|
||||
$iterator->next();
|
||||
|
||||
if ($iterator->valid()) {
|
||||
$current->setValidTo($iterator->current()->getValidFrom());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function removeAddress(Address $address)
|
||||
{
|
||||
$this->addresses->removeElement($address);
|
||||
|
Reference in New Issue
Block a user