mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
196 lines
4.9 KiB
PHP
196 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace Chill\PersonBundle\Entity\Household;
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\Common\Collections\Criteria;
|
|
use Symfony\Component\Serializer\Annotation as Serializer;
|
|
use Chill\MainBundle\Entity\Address;
|
|
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
|
|
|
/**
|
|
* @ORM\Entity
|
|
* @ORM\Table(
|
|
* name="chill_person_household"
|
|
* )
|
|
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
|
|
* "household"=Household::class
|
|
* })
|
|
*/
|
|
class Household
|
|
{
|
|
/**
|
|
* @ORM\Id
|
|
* @ORM\GeneratedValue
|
|
* @ORM\Column(type="integer")
|
|
* @Serializer\Groups({"read"})
|
|
*/
|
|
private ?int $id = null;
|
|
|
|
/**
|
|
* Addresses
|
|
*
|
|
* @ORM\ManyToMany(
|
|
* targetEntity="Chill\MainBundle\Entity\Address",
|
|
* cascade={"persist", "remove", "merge", "detach"})
|
|
* @ORM\JoinTable(name="chill_person_household_to_addresses")
|
|
* @ORM\OrderBy({"validFrom" = "DESC"})
|
|
*/
|
|
private Collection $addresses;
|
|
|
|
/**
|
|
* @ORM\OneToMany(
|
|
* targetEntity=HouseholdMember::class,
|
|
* mappedBy="household"
|
|
* )
|
|
* @Serializer\Groups({"read"})
|
|
*/
|
|
private Collection $members;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->addresses = new ArrayCollection();
|
|
$this->members = new ArrayCollection();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
/**
|
|
* @param Address $address
|
|
* @return $this
|
|
*/
|
|
public function addAddress(Address $address)
|
|
{
|
|
$this->addresses[] = $address;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @param Address $address
|
|
*/
|
|
public function removeAddress(Address $address)
|
|
{
|
|
$this->addresses->removeElement($address);
|
|
}
|
|
|
|
/**
|
|
* By default, the addresses are ordered by date, descending (the most
|
|
* recent first)
|
|
*
|
|
* @return \Chill\MainBundle\Entity\Address[]
|
|
*/
|
|
public function getAddresses()
|
|
{
|
|
return $this->addresses;
|
|
}
|
|
|
|
/**
|
|
* @return Collection|HouseholdMember[]
|
|
*/
|
|
public function getMembers(): Collection
|
|
{
|
|
return $this->members;
|
|
}
|
|
|
|
public function getCurrentMembers(?\DateTimeImmutable $now = null): Collection
|
|
{
|
|
$criteria = new Criteria();
|
|
$expr = Criteria::expr();
|
|
$date = $now === null ? (new \DateTimeImmutable('today')) : $now;
|
|
|
|
$criteria
|
|
->where($expr->orX(
|
|
$expr->isNull('startDate'),
|
|
$expr->lte('startDate', $date)
|
|
))
|
|
->andWhere($expr->orX(
|
|
$expr->isNull('endDate'),
|
|
$expr->gte('endDate', $date)
|
|
));
|
|
|
|
return $this->getMembers()->matching($criteria);
|
|
}
|
|
|
|
/**
|
|
* Get the persons currently associated to the household.
|
|
*
|
|
* Return a list of Person, instead of a list of HouseholdMembers
|
|
*
|
|
* @return Person[]
|
|
*/
|
|
public function getCurrentPersons(?\DateTimeImmutable $now = null): Collection
|
|
{
|
|
return $this->getCurrentMembers($now)
|
|
->map(function(HouseholdMember $m) { return $m->getPerson(); });
|
|
}
|
|
|
|
public function getNonCurrentMembers(\DateTimeImmutable $now = null): Collection
|
|
{
|
|
$criteria = new Criteria();
|
|
$expr = Criteria::expr();
|
|
$date = $now === null ? (new \DateTimeImmutable('today')) : $now;
|
|
|
|
$criteria
|
|
->where(
|
|
$expr->gt('startDate', $date)
|
|
)
|
|
->orWhere(
|
|
$expr->andX(
|
|
$expr->lt('endDate', $date),
|
|
$expr->neq('endDate', null)
|
|
)
|
|
);
|
|
|
|
return $this->getMembers()->matching($criteria);
|
|
}
|
|
|
|
public function getCurrentMembersByPosition(Position $position, \DateTimeInterface $now = null)
|
|
{
|
|
$criteria = new Criteria();
|
|
$expr = Criteria::expr();
|
|
|
|
$criteria->where($expr->eq('position', $position));
|
|
|
|
return $this->getCurrentMembers($now)->matching($criteria);
|
|
}
|
|
|
|
public function getNonCurrentMembersByPosition(Position $position, \DateTimeInterface $now = null)
|
|
{
|
|
$criteria = new Criteria();
|
|
$expr = Criteria::expr();
|
|
|
|
$criteria->where($expr->eq('position', $position));
|
|
|
|
return $this->getNonCurrentMembers($now)->matching($criteria);
|
|
}
|
|
|
|
public function addMember(HouseholdMember $member): self
|
|
{
|
|
if (!$this->members->contains($member)) {
|
|
$this->members[] = $member;
|
|
$member->setHousehold($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeMember(HouseholdMember $member): self
|
|
{
|
|
if ($this->members->removeElement($member)) {
|
|
// set the owning side to null (unless already changed)
|
|
if ($member->getHousehold() === $this) {
|
|
$member->setHousehold(null);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
}
|