migration for history, and create history location on each change

This commit is contained in:
2022-02-14 23:03:40 +01:00
parent 441704dc29
commit b9dbb1916a
6 changed files with 324 additions and 173 deletions

View File

@@ -38,17 +38,19 @@ use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Iterator;
use LogicException;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\GroupSequenceProviderInterface;
use UnexpectedValueException;
use function in_array;
use const SORT_REGULAR;
/**
@@ -114,12 +116,6 @@ class AccompanyingPeriod implements
*/
public const STEP_DRAFT = 'DRAFT';
/**
* @ORM\OneToMany(targetEntity=AccompanyingPeriodLocationHistory::class,
* mappedBy="period")
*/
private Collection $locationHistories;
/**
* @ORM\ManyToOne(
* targetEntity=Address::class
@@ -219,6 +215,12 @@ class AccompanyingPeriod implements
*/
private ?UserJob $job = null;
/**
* @ORM\OneToMany(targetEntity=AccompanyingPeriodLocationHistory::class,
* mappedBy="period", cascade="{"persist", "remove""}, orphanRemoval=true)
*/
private Collection $locationHistories;
/**
* @var DateTime
*
@@ -442,6 +444,39 @@ class AccompanyingPeriod implements
return $this;
}
public function addLocationHistory(AccompanyingPeriodLocationHistory $history): self
{
if ($this->getStep() === self::STEP_DRAFT) {
return $this;
}
if (!$this->locationHistories->contains($history)) {
$this->locationHistories[] = $history;
$history->setPeriod($this);
}
// ensure continuity of histories
$criteria = new Criteria();
$criteria->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]);
/** @var Iterator $locations */
$locations = $this->getLocationHistories()->matching($criteria)->getIterator();
$locations->rewind();
do {
/** @var AccompanyingPeriodLocationHistory $current */
$current = $locations->current();
$locations->next();
if ($locations->valid()) {
$next = $locations->current();
$current->setEndDate($next->getStartDate());
}
} while ($locations->valid());
return $this;
}
public function addPerson(?Person $person = null): self
{
if (null !== $person) {
@@ -688,6 +723,11 @@ class AccompanyingPeriod implements
return $this->getAddressLocation();
}
public function getLocationHistories(): Collection
{
return $this->locationHistories;
}
/**
* Get where the location is.
*
@@ -990,6 +1030,15 @@ class AccompanyingPeriod implements
$this->comments->removeElement($comment);
}
public function removeLocationHistory(AccompanyingPeriodLocationHistory $history): self
{
if ($this->locationHistories->removeElement($history)) {
$history->setPeriod(null);
}
return $this;
}
/**
* Remove Participation.
*/
@@ -1046,6 +1095,15 @@ class AccompanyingPeriod implements
{
if ($this->addressLocation !== $addressLocation) {
$this->addressLocation = $addressLocation;
if (null !== $addressLocation) {
$locationHistory = new AccompanyingPeriodLocationHistory();
$locationHistory
->setStartDate(new DateTimeImmutable('now'))
->setAddressLocation($addressLocation);
$this->addLocationHistory($locationHistory);
}
}
return $this;
@@ -1149,7 +1207,18 @@ class AccompanyingPeriod implements
*/
public function setPersonLocation(?Person $person = null): self
{
$this->personLocation = $person;
if ($this->personLocation !== $person) {
$this->personLocation = $person;
if (null !== $person) {
$locationHistory = new AccompanyingPeriodLocationHistory();
$locationHistory
->setStartDate(new DateTimeImmutable('now'))
->setPersonLocation($person);
$this->addLocationHistory($locationHistory);
}
}
return $this;
}
@@ -1216,8 +1285,14 @@ class AccompanyingPeriod implements
public function setStep(string $step): self
{
$previous = $this->step;
$this->step = $step;
if (self::STEP_DRAFT === $previous && self::STEP_DRAFT !== $step) {
$this->bootPeriod();
}
return $this;
}
@@ -1256,6 +1331,17 @@ class AccompanyingPeriod implements
return $this;
}
private function bootPeriod(): void
{
// first location history
$locationHistory = new AccompanyingPeriodLocationHistory();
$locationHistory
->setStartDate(new DateTimeImmutable('now'))
->setPersonLocation($this->getPersonLocation())
->setAddressLocation($this->getAddressLocation());
$this->addLocationHistory($locationHistory);
}
private function setRequestorPerson(?Person $requestorPerson = null): self
{
$this->requestorPerson = $requestorPerson;
@@ -1269,28 +1355,4 @@ class AccompanyingPeriod implements
return $this;
}
public function getLocationHistories(): Collection
{
return $this->locationHistories;
}
public function addLocationHistory(AccompanyingPeriodLocationHistory $history): self
{
if (!$this->locationHistories->contains($history)) {
$this->locationHistories[] = $history;
$history->setPeriod($this);
}
return $this;
}
public function removeLocationHistory(AccompanyingPeriodLocationHistory $history): self
{
if ($this->locationHistories->removeElement($history)) {
$history->setPeriod(null);
}
return $this;
}
}

View File

@@ -1,18 +1,41 @@
<?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\AccompanyingPeriod;
use Chill\MainBundle\Doctrine\Model\TrackCreationTrait;
use Chill\MainBundle\Entity\Address;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
* @ORM\Entity
* @ORM\Table("chill_person_accompanying_period_location_history")
*/
class AccompanyingPeriodLocationHistory
{
use TrackCreationTrait;
/**
* @ORM\ManyToOne(targetEntity=Address::class)
*/
private ?Address $addressLocation = null;
/**
* @ORM\Column(type="date_immutable")
*/
private ?DateTimeImmutable $endDate = null;
/**
* @ORM\Id
* @ORM\GeneratedValue
@@ -28,117 +51,78 @@ class AccompanyingPeriodLocationHistory
/**
* @ORM\ManyToOne(targetEntity=Person::class)
*/
private ?Person $personLocation;
/**
* @ORM\ManyToOne(targetEntity=Address::class)
*/
private ?Address $addressLocation;
private ?Person $personLocation = null;
/**
* @ORM\Column(type="date_immutable")
*/
private ?\DateTimeImmutable $startDate;
private ?DateTimeImmutable $startDate = null;
/**
* @ORM\Column(type="date_immutable")
*/
private ?\DateTimeImmutable $endDate;
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return Person|null
*/
public function getPersonLocation(): ?Person
{
return $this->personLocation;
}
/**
* @param Person|null $personLocation
* @return AccompanyingPeriodLocationHistory
*/
public function setPersonLocation(?Person $personLocation): AccompanyingPeriodLocationHistory
{
$this->personLocation = $personLocation;
return $this;
}
/**
* @return Address|null
*/
public function getAddressLocation(): ?Address
{
return $this->addressLocation;
}
/**
* @param Address|null $addressLocation
* @return AccompanyingPeriodLocationHistory
*/
public function setAddressLocation(?Address $addressLocation): AccompanyingPeriodLocationHistory
{
$this->addressLocation = $addressLocation;
return $this;
}
/**
* @return \DateTimeImmutable|null
*/
public function getStartDate(): ?\DateTimeImmutable
{
return $this->startDate;
}
/**
* @param \DateTimeImmutable|null $startDate
* @return AccompanyingPeriodLocationHistory
*/
public function setStartDate(?\DateTimeImmutable $startDate): AccompanyingPeriodLocationHistory
{
$this->startDate = $startDate;
return $this;
}
/**
* @return \DateTimeImmutable|null
*/
public function getEndDate(): ?\DateTimeImmutable
public function getEndDate(): ?DateTimeImmutable
{
return $this->endDate;
}
/**
* @param \DateTimeImmutable|null $endDate
* @return AccompanyingPeriodLocationHistory
*/
public function setEndDate(?\DateTimeImmutable $endDate): AccompanyingPeriodLocationHistory
public function getId(): ?int
{
$this->endDate = $endDate;
return $this;
return $this->id;
}
/**
* @return AccompanyingPeriod
*/
public function getPeriod(): AccompanyingPeriod
{
return $this->period;
}
public function getPersonLocation(): ?Person
{
return $this->personLocation;
}
public function getStartDate(): ?DateTimeImmutable
{
return $this->startDate;
}
public function setAddressLocation(?Address $addressLocation): AccompanyingPeriodLocationHistory
{
$this->addressLocation = $addressLocation;
return $this;
}
public function setEndDate(?DateTimeImmutable $endDate): AccompanyingPeriodLocationHistory
{
$this->endDate = $endDate;
return $this;
}
/**
* @internal use AccompanyingPeriod::addLocationHistory
*/
public function setPeriod(AccompanyingPeriod $period): AccompanyingPeriodLocationHistory
{
$this->period = $period;
return $this;
}
public function setPersonLocation(?Person $personLocation): AccompanyingPeriodLocationHistory
{
$this->personLocation = $personLocation;
return $this;
}
public function setStartDate(?DateTimeImmutable $startDate): AccompanyingPeriodLocationHistory
{
$this->startDate = $startDate;
return $this;
}
}