mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +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)) {
|
||||
|
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Entity\Household;
|
||||
|
||||
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
|
||||
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
|
||||
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
|
||||
use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait;
|
||||
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(
|
||||
* name="chill_person_household_composition"
|
||||
* )
|
||||
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
|
||||
* "household_composition_type": HouseholdCompositionType::class
|
||||
* })
|
||||
*/
|
||||
class HouseholdComposition implements TrackCreationInterface, TrackUpdateInterface
|
||||
{
|
||||
use TrackCreationTrait;
|
||||
use TrackUpdateTrait;
|
||||
|
||||
/**
|
||||
* @ORM\Embedded(class=CommentEmbeddable::class, columnPrefix="comment_")
|
||||
*/
|
||||
private CommentEmbeddable $comment;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="date_immutable", nullable=true, options={"default": null})
|
||||
* @Assert\GreaterThanOrEqual(propertyPath="startDate", groups={"Default", "household_composition"})
|
||||
*/
|
||||
private ?DateTimeImmutable $endDate = null;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity=Household::class, inversedBy="compositions")
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
*/
|
||||
private ?Household $household = null;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity=HouseholdCompositionType::class)
|
||||
* @ORM\JoinColumn(nullable=false)
|
||||
*/
|
||||
private ?HouseholdCompositionType $householdCompositionType = null;
|
||||
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\GeneratedValue
|
||||
* @ORM\Column(type="integer")
|
||||
* @Serializer\Groups({"read", "docgen:read"})
|
||||
*/
|
||||
private ?int $id = null;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="integer", nullable=true, options={"default": null})
|
||||
* @Assert\NotNull
|
||||
* @Assert\GreaterThanOrEqual(0, groups={"Default", "household_composition"})
|
||||
*/
|
||||
private ?int $numberOfChildren = null;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="date_immutable", nullable=false)
|
||||
* @Assert\NotNull(groups={"Default", "household_composition"})
|
||||
*/
|
||||
private ?DateTimeImmutable $startDate = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->comment = new CommentEmbeddable();
|
||||
}
|
||||
|
||||
public function getComment(): CommentEmbeddable
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
public function getEndDate(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->endDate;
|
||||
}
|
||||
|
||||
public function getHousehold(): ?Household
|
||||
{
|
||||
return $this->household;
|
||||
}
|
||||
|
||||
public function getHouseholdCompositionType(): ?HouseholdCompositionType
|
||||
{
|
||||
return $this->householdCompositionType;
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getNumberOfChildren(): ?int
|
||||
{
|
||||
return $this->numberOfChildren;
|
||||
}
|
||||
|
||||
public function getStartDate(): ?DateTimeImmutable
|
||||
{
|
||||
return $this->startDate;
|
||||
}
|
||||
|
||||
public function setComment(CommentEmbeddable $comment): HouseholdComposition
|
||||
{
|
||||
$this->comment = $comment;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setEndDate(?DateTimeImmutable $endDate): HouseholdComposition
|
||||
{
|
||||
$this->endDate = $endDate;
|
||||
|
||||
if (null !== $this->household) {
|
||||
$this->household->householdCompositionConsistency();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setHousehold(?Household $household): HouseholdComposition
|
||||
{
|
||||
$this->household = $household;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setHouseholdCompositionType(?HouseholdCompositionType $householdCompositionType): HouseholdComposition
|
||||
{
|
||||
$this->householdCompositionType = $householdCompositionType;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setNumberOfChildren(?int $numberOfChildren): HouseholdComposition
|
||||
{
|
||||
$this->numberOfChildren = $numberOfChildren;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setStartDate(?DateTimeImmutable $startDate): HouseholdComposition
|
||||
{
|
||||
$this->startDate = $startDate;
|
||||
|
||||
if (null !== $this->household) {
|
||||
$this->household->householdCompositionConsistency();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Entity\Household;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation as Serializer;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(
|
||||
* name="chill_person_household_composition_type"
|
||||
* )
|
||||
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
|
||||
* "household_composition_type": HouseholdCompositionType::class
|
||||
* })
|
||||
*/
|
||||
class HouseholdCompositionType
|
||||
{
|
||||
/**
|
||||
* @ORM\Column(type="boolean")
|
||||
*/
|
||||
private bool $active = true;
|
||||
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\GeneratedValue
|
||||
* @ORM\Column(type="integer")
|
||||
* @Serializer\Groups({"read", "docgen:read"})
|
||||
*/
|
||||
private ?int $id = null;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="json")
|
||||
* @Serializer\Groups({"read", "docgen:read"})
|
||||
* @Serializer\Context({"is-translatable": true}, groups={"docgen:read"})
|
||||
*/
|
||||
private array $label = [];
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getLabel(): array
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
public function setActive(bool $active): HouseholdCompositionType
|
||||
{
|
||||
$this->active = $active;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setLabel(array $label): HouseholdCompositionType
|
||||
{
|
||||
$this->label = $label;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user