mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Household/composition add + fixes household composition editor
This commit is contained in:
@@ -11,6 +11,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Entity\Household;
|
||||
|
||||
use ArrayIterator;
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
|
||||
use Chill\PersonBundle\Validator\Constraints\Household\MaxHolder;
|
||||
@@ -23,8 +24,8 @@ use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use function count;
|
||||
|
||||
/**
|
||||
@@ -56,6 +57,18 @@ class Household
|
||||
*/
|
||||
private CommentEmbeddable $commentMembers;
|
||||
|
||||
/**
|
||||
* @ORM\OneToMany(
|
||||
* targetEntity=HouseholdComposition::class,
|
||||
* mappedBy="household",
|
||||
* orphanRemoval=true,
|
||||
* cascade={"persist"}
|
||||
* )
|
||||
* @ORM\OrderBy({"startDate": "DESC"})
|
||||
* @Assert\Valid(traverse=true, groups={"household_composition"})
|
||||
*/
|
||||
private Collection $compositions;
|
||||
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\GeneratedValue
|
||||
@@ -90,6 +103,7 @@ class Household
|
||||
$this->addresses = new ArrayCollection();
|
||||
$this->members = new ArrayCollection();
|
||||
$this->commentMembers = new CommentEmbeddable();
|
||||
$this->compositions = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,6 +122,18 @@ class Household
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addComposition(HouseholdComposition $composition): self
|
||||
{
|
||||
if (!$this->compositions->contains($composition)) {
|
||||
$composition->setHousehold($this);
|
||||
$this->compositions[] = $composition;
|
||||
}
|
||||
|
||||
$this->householdCompositionConsistency();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addMember(HouseholdMember $member): self
|
||||
{
|
||||
if (!$this->members->contains($member)) {
|
||||
@@ -136,6 +162,14 @@ class Household
|
||||
return $this->commentMembers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ArrayCollection|Collection|HouseholdComposition[]
|
||||
*/
|
||||
public function getCompositions(): Collection
|
||||
{
|
||||
return $this->compositions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Serializer\Groups({"read", "docgen:read"})
|
||||
* @Serializer\SerializedName("current_address")
|
||||
@@ -157,6 +191,31 @@ class Household
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getCurrentComposition(?DateTimeImmutable $at = null): ?HouseholdComposition
|
||||
{
|
||||
$at ??= new DateTimeImmutable('today');
|
||||
$criteria = new Criteria();
|
||||
$expr = Criteria::expr();
|
||||
|
||||
$criteria->where(
|
||||
$expr->andX(
|
||||
$expr->orX(
|
||||
$expr->isNull('endDate'),
|
||||
$expr->gt('endDate', $at)
|
||||
),
|
||||
$expr->lte('startDate', $at)
|
||||
)
|
||||
);
|
||||
|
||||
$compositions = $this->compositions->matching($criteria);
|
||||
|
||||
if ($compositions->count() > 0) {
|
||||
return $compositions->first();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Serializer\Groups({"docgen:read"})
|
||||
*/
|
||||
@@ -369,11 +428,54 @@ class Household
|
||||
return $this->waitingForBirthDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function householdCompositionConsistency(): void
|
||||
{
|
||||
$compositionOrdered = $this->compositions->toArray();
|
||||
|
||||
usort(
|
||||
$compositionOrdered,
|
||||
static function (HouseholdComposition $a, HouseholdComposition $b) {
|
||||
return $a->getStartDate() <=> $b->getStartDate();
|
||||
}
|
||||
);
|
||||
|
||||
$iterator = new ArrayIterator($compositionOrdered);
|
||||
$iterator->rewind();
|
||||
|
||||
/** @var ?HouseholdComposition $previous */
|
||||
$previous = null;
|
||||
|
||||
do {
|
||||
/** @var ?HouseholdComposition $current */
|
||||
$current = $iterator->current();
|
||||
|
||||
if (null !== $previous) {
|
||||
if (null === $previous->getEndDate() || $previous->getEndDate() > $current->getStartDate()) {
|
||||
$previous->setEndDate($current->getStartDate());
|
||||
}
|
||||
}
|
||||
$previous = $current;
|
||||
$iterator->next();
|
||||
} while ($iterator->valid());
|
||||
}
|
||||
|
||||
public function removeAddress(Address $address)
|
||||
{
|
||||
$this->addresses->removeElement($address);
|
||||
}
|
||||
|
||||
public function removeComposition(HouseholdComposition $composition): self
|
||||
{
|
||||
if ($this->compositions->removeElement($composition)) {
|
||||
$composition->setHousehold(null);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function removeMember(HouseholdMember $member): self
|
||||
{
|
||||
if ($this->members->removeElement($member)) {
|
||||
|
Reference in New Issue
Block a user