create view for current address and apply on Person/Household normalizer

This commit is contained in:
2021-06-17 13:21:55 +02:00
parent 38bff2e42f
commit ef55d2cf7f
8 changed files with 240 additions and 3 deletions

View File

@@ -100,6 +100,27 @@ class Household
return $this->addresses;
}
/**
* @Serializer\Groups({ "read" })
* @Serializer\SerializedName("current_address")
*/
public function getCurrentAddress(\DateTime $at = null): ?Address
{
$at = $at === null ? new \DateTime('today') : $at;
$addrs = $this->getAddresses()->filter(function (Address $a) use ($at) {
return $a->getValidFrom() < $at && (
NULL === $a->getValidTo() || $at < $a->getValidTo()
);
});
if ($addrs->count() > 0) {
return $addrs->first();
} else {
return null;
}
}
/**
* @return Collection|HouseholdMember[]
*/
@@ -108,6 +129,10 @@ class Household
return $this->members;
}
/**
* @Serializer\Groups({ "read" })
* @Serializer\SerializedName("current_members")
*/
public function getCurrentMembers(?\DateTimeImmutable $now = null): Collection
{
$criteria = new Criteria();

View File

@@ -0,0 +1,72 @@
<?php
namespace Chill\PersonBundle\Entity\Household;
use Chill\MainBundle\Entity\Address;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(readOnly=true)
* @ORM\Table(name="view_chill_person_household_address")
*/
class PersonHouseholdAddress
{
/**
* @ORM\Column(type="date_immutable")
*/
private $validFrom;
/**
* @ORM\Column(type="date_immutable", nullable=true)
*/
private $validTo;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=false)
*/
private $person;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=Household::class)
* @ORM\JoinColumn(nullable=false)
*/
private $household;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity=Address::class)
* @ORM\JoinColumn(nullable=false)
*/
private $address;
public function getValidFrom(): ?\DateTimeInterface
{
return $this->validFrom;
}
public function getValidTo(): ?\DateTimeImmutable
{
return $this->validTo;
}
public function getPerson(): ?Person
{
return $this->person;
}
public function getHousehold(): ?Household
{
return $this->relation;
}
public function getAddress(): ?Address
{
return $this->address;
}
}

View File

@@ -37,6 +37,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Chill\PersonBundle\Entity\Household\PersonHouseholdAddress;
/**
* Person Class
@@ -287,6 +288,14 @@ class Person implements HasCenterInterface
*/
private array $currentHouseholdAt = [];
/**
* @ORM\OneToMany(
* targetEntity=PersonHouseholdAddress::class,
* mappedBy="person"
* )
*/
private Collection $householdAddresses;
/**
* Person constructor.
*
@@ -300,6 +309,7 @@ class Person implements HasCenterInterface
$this->altNames = new ArrayCollection();
$this->otherPhoneNumbers = new ArrayCollection();
$this->householdParticipations = new ArrayCollection();
$this->householdAddresses = new ArrayCollection();
if ($opening === null) {
$opening = new \DateTime();
@@ -1247,4 +1257,36 @@ class Person implements HasCenterInterface
{
return NULL !== $this->getCurrentHousehold($at);
}
public function getHouseholdAddresses(): Collection
{
return $this->householdAddresses;
}
public function getCurrentHouseholdAddress(?\DateTimeImmutable $at = null): ?Address
{
$at = $at === null ? new \DateTimeImmutable('today') : $at;
$criteria = new Criteria();
$expr = Criteria::expr();
$criteria->where(
$expr->lte('validFrom', $at)
)
->andWhere(
$expr->orX(
$expr->isNull('validTo'),
$expr->gte('validTo', $at)
)
);
$addrs = $this->getHouseholdAddresses()
->matching($criteria)
;
if ($addrs->count() > 0) {
return $addrs->first()->getAddress();
} else {
return null;
}
}
}