diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php index a2ced7fee..6acca44e3 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseController.php @@ -62,7 +62,7 @@ class AccompanyingCourseController extends \Symfony\Bundle\FrameworkBundle\Contr $workflow = $this->registry->get($accompanyingCourse); if ($workflow->can($accompanyingCourse, 'close')) { - $workflow->apply($accompanyingCourse, 'close'); + $workflow->apply($accompanyingCourse, 'close', ['closing_motive' => $form['closingMotive']->getData()]); $em->flush(); diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 9c88a4a69..4f9c480d0 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -1449,7 +1449,7 @@ class AccompanyingPeriod implements return $this; } - public function setStep(string $step): self + public function setStep(string $step, array $context = []): self { $previous = $this->step; @@ -1464,7 +1464,7 @@ class AccompanyingPeriod implements $history = new AccompanyingPeriodStepHistory(); $history->setStep($this->step)->setStartDate(new \DateTimeImmutable('now')); - $this->addStepHistory($history); + $this->addStepHistory($history, $context); } return $this; @@ -1507,11 +1507,14 @@ class AccompanyingPeriod implements return $this; } - private function addStepHistory(AccompanyingPeriodStepHistory $stepHistory): self + private function addStepHistory(AccompanyingPeriodStepHistory $stepHistory, array $context = []): self { if (!$this->stepHistories->contains($stepHistory)) { $this->stepHistories[] = $stepHistory; $stepHistory->setPeriod($this); + if (($context['closing_motive'] ?? null) instanceof ClosingMotive) { + $stepHistory->setClosingMotive($context['closing_motive']); + } $this->ensureStepContinuity(); } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodStepHistory.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodStepHistory.php index 15b2f0d2e..fd4d573ce 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodStepHistory.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodStepHistory.php @@ -59,6 +59,13 @@ class AccompanyingPeriodStepHistory implements TrackCreationInterface, TrackUpda */ private string $step; + /** + * @ORM\ManyToOne(targetEntity=ClosingMotive::class) + * + * @ORM\JoinColumn(nullable=true) + */ + private ?AccompanyingPeriod\ClosingMotive $closingMotive = null; + public function getEndDate(): ?\DateTimeImmutable { return $this->endDate; @@ -114,4 +121,16 @@ class AccompanyingPeriodStepHistory implements TrackCreationInterface, TrackUpda return $this; } + + public function getClosingMotive(): ?AccompanyingPeriod\ClosingMotive + { + return $this->closingMotive; + } + + public function setClosingMotive(?AccompanyingPeriod\ClosingMotive $closingMotive): self + { + $this->closingMotive = $closingMotive; + + return $this; + } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php index 5fdb51c15..9e383b789 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php @@ -312,4 +312,34 @@ final class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase $this->assertNull($period->getRequestorPerson()); $this->assertNull($period->getRequestor()); } + + public function testSetStep(): void + { + $period = new AccompanyingPeriod(); + + $period->setStep(AccompanyingPeriod::STEP_CONFIRMED); + + self::assertEquals(AccompanyingPeriod::STEP_CONFIRMED, $period->getStep()); + self::assertCount(1, $period->getStepHistories()); + + $period->setStep(AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_SHORT); + + self::assertEquals(AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_SHORT, $period->getStep()); + self::assertCount(2, $period->getStepHistories()); + + $periodInactiveSteps = $period->getStepHistories()->filter(fn (AccompanyingPeriod\AccompanyingPeriodStepHistory $h) => AccompanyingPeriod::STEP_CONFIRMED_INACTIVE_SHORT === $h->getStep()); + self::assertCount(1, $periodInactiveSteps); + + $period->setStep(AccompanyingPeriod::STEP_CLOSED, ['closing_motive' => $closingMotive = new AccompanyingPeriod\ClosingMotive()]); + + self::assertEquals(AccompanyingPeriod::STEP_CLOSED, $period->getStep()); + self::assertCount(3, $period->getStepHistories()); + + $periodClosedSteps = $period->getStepHistories()->filter(fn (AccompanyingPeriod\AccompanyingPeriodStepHistory $h) => AccompanyingPeriod::STEP_CLOSED === $h->getStep()); + self::assertCount(1, $periodClosedSteps); + + $periodClosedStep = $periodClosedSteps->first(); + + self::assertSame($closingMotive, $periodClosedStep->getClosingMotive()); + } } diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20240123161457.php b/src/Bundle/ChillPersonBundle/migrations/Version20240123161457.php new file mode 100644 index 000000000..4c19e6c8f --- /dev/null +++ b/src/Bundle/ChillPersonBundle/migrations/Version20240123161457.php @@ -0,0 +1,52 @@ +addSql('ALTER TABLE chill_person_accompanying_period_step_history ADD closingMotive_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_person_accompanying_period_step_history ADD CONSTRAINT FK_84D514AC504CB38D FOREIGN KEY (closingMotive_id) REFERENCES chill_person_accompanying_period_closingmotive (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_person_accompanying_period_step_history ADD CONSTRAINT FK_84D514AC65FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql(<<<'EOF' + WITH last_step AS ( + SELECT * FROM ( + SELECT *, rank() OVER (partition by period_id ORDER BY startdate DESC, id DESC) AS r FROM chill_person_accompanying_period_step_history cpapsh + ) as sq + WHERE r = 1 + ) + UPDATE chill_person_accompanying_period_step_history + SET closingMotive_id = chill_person_accompanying_period.closingmotive_id + FROM last_step, chill_person_accompanying_period + WHERE last_step.period_id = chill_person_accompanying_period_step_history.period_id AND chill_person_accompanying_period.id = chill_person_accompanying_period_step_history.period_id + AND last_step.step = 'CLOSED'; + EOF); + $this->addSql('CREATE INDEX IDX_84D514AC504CB38D ON chill_person_accompanying_period_step_history (closingMotive_id)'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_person_accompanying_period_step_history DROP CONSTRAINT FK_84D514AC504CB38D'); + $this->addSql('ALTER TABLE chill_person_accompanying_period_step_history DROP CONSTRAINT FK_84D514AC65FF1AEC'); + $this->addSql('DROP INDEX IDX_84D514AC504CB38D'); + $this->addSql('ALTER TABLE chill_person_accompanying_period_step_history DROP closingMotive_id'); + } +}