mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 19:13:49 +00:00
Feature: [acp] record the step history of each accompanying period
Each time a step is changed on an history, a record is stored in a dedicated table. When the acp's opening date is moved, the first row is adapted to match the new opening's date. This mechanisme does not work if the opening date is move beyon the first end date (if any), nor on the closing date.
This commit is contained in:
@@ -22,6 +22,7 @@ use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserJob;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodLocationHistory;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodStepHistory;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment;
|
||||
@@ -336,6 +337,12 @@ class AccompanyingPeriod implements
|
||||
*/
|
||||
private string $step = self::STEP_DRAFT;
|
||||
|
||||
/**
|
||||
* @ORM\OneToMany(targetEntity=AccompanyingPeriodStepHistory::class,
|
||||
* mappedBy="period", cascade={"persist", "remove"}, orphanRemoval=true)
|
||||
*/
|
||||
private Collection $stepHistories;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="datetime", nullable=true, options={"default": NULL})
|
||||
*/
|
||||
@@ -390,7 +397,6 @@ class AccompanyingPeriod implements
|
||||
*/
|
||||
public function __construct(?DateTime $dateOpening = null)
|
||||
{
|
||||
$this->setOpeningDate($dateOpening ?? new DateTime('now'));
|
||||
$this->participations = new ArrayCollection();
|
||||
$this->scopes = new ArrayCollection();
|
||||
$this->socialIssues = new ArrayCollection();
|
||||
@@ -399,6 +405,8 @@ class AccompanyingPeriod implements
|
||||
$this->resources = new ArrayCollection();
|
||||
$this->userHistories = new ArrayCollection();
|
||||
$this->locationHistories = new ArrayCollection();
|
||||
$this->stepHistories = new ArrayCollection();
|
||||
$this->setOpeningDate($dateOpening ?? new DateTime('now'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -966,6 +974,11 @@ class AccompanyingPeriod implements
|
||||
return $this->step;
|
||||
}
|
||||
|
||||
public function getStepHistories(): Collection
|
||||
{
|
||||
return $this->stepHistories;
|
||||
}
|
||||
|
||||
public function getUser(): ?User
|
||||
{
|
||||
return $this->user;
|
||||
@@ -1234,7 +1247,11 @@ class AccompanyingPeriod implements
|
||||
*/
|
||||
public function setOpeningDate($openingDate)
|
||||
{
|
||||
$this->openingDate = $openingDate;
|
||||
if ($this->openingDate !== $openingDate) {
|
||||
$this->openingDate = $openingDate;
|
||||
|
||||
$this->ensureStepContinuity();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -1333,6 +1350,14 @@ class AccompanyingPeriod implements
|
||||
$this->bootPeriod();
|
||||
}
|
||||
|
||||
if (self::STEP_DRAFT !== $this->step && $previous !== $step) {
|
||||
// we create a new history
|
||||
$history = new AccompanyingPeriodStepHistory();
|
||||
$history->setStep($this->step)->setStartDate(new DateTimeImmutable('now'));
|
||||
|
||||
$this->addStepHistory($history);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -1373,6 +1398,17 @@ class AccompanyingPeriod implements
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function addStepHistory(AccompanyingPeriodStepHistory $stepHistory): self
|
||||
{
|
||||
if (!$this->stepHistories->contains($stepHistory)) {
|
||||
$this->stepHistories[] = $stepHistory;
|
||||
$stepHistory->setPeriod($this);
|
||||
$this->ensureStepContinuity();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function bootPeriod(): void
|
||||
{
|
||||
// first location history
|
||||
@@ -1384,6 +1420,43 @@ class AccompanyingPeriod implements
|
||||
$this->addLocationHistory($locationHistory);
|
||||
}
|
||||
|
||||
private function ensureStepContinuity(): void
|
||||
{
|
||||
// ensure continuity of histories
|
||||
$criteria = new Criteria();
|
||||
$criteria->orderBy(['startDate' => Criteria::ASC, 'id' => Criteria::ASC]);
|
||||
|
||||
/** @var Iterator $steps */
|
||||
$steps = $this->getStepHistories()->matching($criteria)->getIterator();
|
||||
$steps->rewind();
|
||||
|
||||
// we set the start date of the first step as the opening date, only if it is
|
||||
// not greater than the end date
|
||||
/** @var AccompanyingPeriodStepHistory $current */
|
||||
$current = $steps->current();
|
||||
|
||||
if (null === $current) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->getOpeningDate()->format('Y-m-d') !== $current->getStartDate()->format('Y-m-d')
|
||||
&& ($this->getOpeningDate() <= $current->getEndDate() || null === $current->getEndDate())) {
|
||||
$current->setStartDate(DateTimeImmutable::createFromMutable($this->getOpeningDate()));
|
||||
}
|
||||
|
||||
// then we set all the end date to the start date of the next one
|
||||
do {
|
||||
/** @var AccompanyingPeriodStepHistory $current */
|
||||
$current = $steps->current();
|
||||
$steps->next();
|
||||
|
||||
if ($steps->valid()) {
|
||||
$next = $steps->current();
|
||||
$current->setEndDate($next->getStartDate());
|
||||
}
|
||||
} while ($steps->valid());
|
||||
}
|
||||
|
||||
private function setRequestorPerson(?Person $requestorPerson = null): self
|
||||
{
|
||||
$this->requestorPerson = $requestorPerson;
|
||||
|
Reference in New Issue
Block a user