From 4e50f23558011774ce7de753aa60f36df3a465f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Thu, 12 Nov 2026 11:13:55 +0100 Subject: [PATCH] fix: draw only active placements on the timeline and keep trips overlapping its dates --- src/Model/TimelineItem.php | 24 ++++ src/Repository/AssignmentRepository.php | 9 +- src/Service/Assignment/TimelineService.php | 31 ++-- .../assignment/timeline.html.twig | 9 +- tests/Repository/AssignmentRepositoryTest.php | 42 +++++- .../Assignment/TimelineServiceTest.php | 133 ++++++++++++++++++ 6 files changed, 216 insertions(+), 32 deletions(-) create mode 100644 tests/Service/Assignment/TimelineServiceTest.php diff --git a/src/Model/TimelineItem.php b/src/Model/TimelineItem.php index 9e24d73..f09c659 100644 --- a/src/Model/TimelineItem.php +++ b/src/Model/TimelineItem.php @@ -10,11 +10,17 @@ class TimelineItem public const TYPE_ASSIGNMENT = 'assignment'; public const TYPE_DISPOSITION = 'disposition'; + // what the bar shows: a vacancy nobody applied for, one in progress, or a placed teamer + public const STATE_OPEN = 'open'; + public const STATE_PENDING = 'pending'; + public const STATE_STAFFED = 'staffed'; + private ?string $assignmentUuid = null; private ?string $dispositionUuid = null; private ?\DateTimeImmutable $dateFrom = null; private ?\DateTimeImmutable $dateTo = null; private ?string $status = null; + private ?string $state = null; private ?string $dateRange = null; private ?string $jobProfile = null; private ?string $teamerName = null; @@ -36,6 +42,8 @@ class TimelineItem ->setJobProfile($assignment->getJobProfile()->getName()) ->setDateRange(sprintf('%s - %s', $dateFrom->format('d.m.Y'), $dateTo->format('d.m.Y'))) ->setStatus($assignment->getStaffingStatus()) + // applications are removed when a teamer is placed, so the valid ones are unplaced + ->setState(0 < $assignment->getValidApplications()->count() ? static::STATE_PENDING : static::STATE_OPEN) ; } @@ -54,6 +62,10 @@ class TimelineItem ->setJobProfile($assignment->getJobProfile()->getName()) ->setDateRange(sprintf('%s - %s', $dateFrom->format('d.m.Y'), $dateTo->format('d.m.Y'))) ->setStatus($disposition->getStatus()) + ->setState(match ($disposition->getStatus()) { + Disposition::STATUS_NEW, Disposition::STATUS_CHECKING_CONTRACT => static::STATE_PENDING, + default => static::STATE_STAFFED, + }) ->setTeamerName($disposition->getTeamer()->getFullName()) ->setTeamerUuid($disposition->getTeamer()->getUuid()) ; @@ -148,6 +160,18 @@ class TimelineItem return $this; } + public function getState(): ?string + { + return $this->state; + } + + public function setState(?string $state): static + { + $this->state = $state; + + return $this; + } + public function getTeamerName(): ?string { return $this->teamerName; diff --git a/src/Repository/AssignmentRepository.php b/src/Repository/AssignmentRepository.php index b2b8fc1..4fdceb0 100644 --- a/src/Repository/AssignmentRepository.php +++ b/src/Repository/AssignmentRepository.php @@ -9,6 +9,7 @@ use App\Entity\Disposition; use App\Entity\JobProfile; use App\Entity\Teamer; use App\Model\AssignmentFilterDto; +use App\Model\TimelineFilterDto; use App\Repository\Traits\QueryHelperTrait; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\Query; @@ -224,16 +225,20 @@ class AssignmentRepository extends ServiceEntityRepository ; } + // The timeline draws every trip touching its window, so a team on site is not dropped by a + // window starting today; the lists keep only trips lying inside the dates. + $overlap = $filterDto instanceof TimelineFilterDto; + if (null !== $dateFrom = $filterDto->getDateFrom()) { $qb - ->andWhere($qb->expr()->gte('destination.dateFrom', ':dateFrom')) + ->andWhere($qb->expr()->gte($overlap ? 'destination.dateTo' : 'destination.dateFrom', ':dateFrom')) ->setParameter('dateFrom', $dateFrom) ; } if (null !== $dateTo = $filterDto->getDateTo()) { $qb - ->andWhere($qb->expr()->lte('destination.dateTo', ':dateTo')) + ->andWhere($qb->expr()->lte($overlap ? 'destination.dateFrom' : 'destination.dateTo', ':dateTo')) ->setParameter('dateTo', $dateTo) ; } diff --git a/src/Service/Assignment/TimelineService.php b/src/Service/Assignment/TimelineService.php index d24f951..320c8df 100644 --- a/src/Service/Assignment/TimelineService.php +++ b/src/Service/Assignment/TimelineService.php @@ -40,28 +40,17 @@ class TimelineService $rows[$hotelCode] = [[]]; } - $dispositionsCount = $assignment->getDispositions()->count(); - $dispositionsAvailable = $assignment->getAvailableDispositions(); + // called-off dispositions free their slot again + $dispositions = $assignment->getActiveDispositions(); - // Handle assignments with no dispositions - create items for all available slots - if (0 === $dispositionsCount) { - $item = TimelineItem::fromAssignment($assignment); - for ($i = 1; $i <= $assignment->getAvailableDispositions(); ++$i) { - $this->addItem($rows[$hotelCode], $item); - } - } else { - // Create items for remaining vacant slots if any - if ($dispositionsCount < $dispositionsAvailable) { - $item = TimelineItem::fromAssignment($assignment); - for ($i = 1; $i <= $dispositionsAvailable - $dispositionsCount; ++$i) { - $this->addItem($rows[$hotelCode], $item); - } - } - // Create items for actual dispositions - foreach ($assignment->getDispositions() as $disposition) { - $item = TimelineItem::fromDisposition($disposition); - $this->addItem($rows[$hotelCode], $item); - } + // Create items for the vacant slots, if any + $item = TimelineItem::fromAssignment($assignment); + for ($i = $dispositions->count(); $i < $assignment->getAvailableDispositions(); ++$i) { + $this->addItem($rows[$hotelCode], $item); + } + // Create items for actual dispositions + foreach ($dispositions as $disposition) { + $this->addItem($rows[$hotelCode], TimelineItem::fromDisposition($disposition)); } } diff --git a/templates/administrative/assignment/timeline.html.twig b/templates/administrative/assignment/timeline.html.twig index cffd05d..ff4e192 100644 --- a/templates/administrative/assignment/timeline.html.twig +++ b/templates/administrative/assignment/timeline.html.twig @@ -4,10 +4,9 @@ {% macro cell(item, colspan) %} {% if item.type == 'disposition' %}
@@ -26,7 +25,7 @@ hx-get="{{ path('app_administrative_teamer_info', { 'uuid': item.teamerUuid, 'r': return_url() }) }}" hx-target="body" hx-swap="beforeend"> - {{ item.teamerName | raw }} + {{ item.teamerName }}
{% else %} diff --git a/tests/Repository/AssignmentRepositoryTest.php b/tests/Repository/AssignmentRepositoryTest.php index f10c93c..2fbc015 100644 --- a/tests/Repository/AssignmentRepositoryTest.php +++ b/tests/Repository/AssignmentRepositoryTest.php @@ -6,7 +6,10 @@ namespace App\Tests\Repository; use App\Entity\Assignment; use App\Entity\Disposition; +use App\Model\AssignmentFilterDto; +use App\Model\TimelineFilterDto; use App\Repository\AssignmentRepository; +use Carbon\CarbonPeriodImmutable; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Query; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; @@ -47,14 +50,45 @@ class AssignmentRepositoryTest extends KernelTestCase ); } + /** + * The timeline keeps every trip touching its window, so a team on site stays visible when + * the window starts today. + */ + public function testTimelineDateFilterKeepsOverlappingTrips(): void + { + $filterDto = (new TimelineFilterDto()) + ->setPeriod(CarbonPeriodImmutable::create('2026-11-12', '2026-12-31')) + ; + + $dql = $this->getRepository()->getListQuery($filterDto)->getDQL(); + + $this->assertStringContainsString('destination.dateTo >= :dateFrom', $dql); + $this->assertStringContainsString('destination.dateFrom <= :dateTo', $dql); + } + + public function testListDateFilterKeepsTripsInsideTheDates(): void + { + $filterDto = (new AssignmentFilterDto()) + ->setDateFrom(new \DateTimeImmutable('2026-11-12')) + ->setDateTo(new \DateTimeImmutable('2026-12-31')) + ; + + $dql = $this->getRepository()->getListQuery($filterDto)->getDQL(); + + $this->assertStringContainsString('destination.dateFrom >= :dateFrom', $dql); + $this->assertStringContainsString('destination.dateTo <= :dateTo', $dql); + } + private function createAvailableAssignmentsIdsQuery(): Query + { + return $this->getRepository()->getAvailableAssignmentsIdsQuery(); + } + + private function getRepository(): AssignmentRepository { /** @var EntityManagerInterface $entityManager */ $entityManager = static::getContainer()->get(EntityManagerInterface::class); - /** @var AssignmentRepository $repository */ - $repository = $entityManager->getRepository(Assignment::class); - - return $repository->getAvailableAssignmentsIdsQuery(); + return $entityManager->getRepository(Assignment::class); } } diff --git a/tests/Service/Assignment/TimelineServiceTest.php b/tests/Service/Assignment/TimelineServiceTest.php new file mode 100644 index 0000000..b37d786 --- /dev/null +++ b/tests/Service/Assignment/TimelineServiceTest.php @@ -0,0 +1,133 @@ +assignment(2); + $this->dispose($assignment, 'Anna', Disposition::STATUS_CONFIRMED); + $this->dispose($assignment, 'Bert', Disposition::STATUS_CALLED_OFF); + + $items = $this->sortByType($this->items($assignment)); + + $this->assertCount(2, $items); + $this->assertSame('Anna Probe', $items[0]->getTeamerName()); + $this->assertSame(TimelineItem::TYPE_ASSIGNMENT, $items[1]->getType()); + } + + public function testVacancyIsOpenWithoutApplicationsEvenWhenPartlyStaffed(): void + { + $assignment = $this->assignment(2); + $this->dispose($assignment, 'Anna', Disposition::STATUS_CONFIRMED); + $assignment->setStaffingStatus(Assignment::STATUS_PARTLY_STAFFED); + + $vacancy = $this->sortByType($this->items($assignment))[1]; + + $this->assertSame(TimelineItem::TYPE_ASSIGNMENT, $vacancy->getType()); + $this->assertSame(TimelineItem::STATE_OPEN, $vacancy->getState()); + } + + public function testVacancyIsPendingWithAValidApplication(): void + { + $assignment = $this->assignment(1); + $assignment->addApplication(new Application($assignment, $this->teamer('Carla'))); + + $this->assertSame(TimelineItem::STATE_PENDING, $this->items($assignment)[0]->getState()); + } + + public function testVacancyIgnoresRejectedApplications(): void + { + $assignment = $this->assignment(1); + $application = new Application($assignment, $this->teamer('Carla')); + $application->setStatus(Application::STATUS_REJECTED); + $assignment->addApplication($application); + + $this->assertSame(TimelineItem::STATE_OPEN, $this->items($assignment)[0]->getState()); + } + + /** + * Paid had no colour in the template and rendered like a vacancy. + */ + public function testDispositionStates(): void + { + $expected = [ + Disposition::STATUS_NEW => TimelineItem::STATE_PENDING, + Disposition::STATUS_CHECKING_CONTRACT => TimelineItem::STATE_PENDING, + Disposition::STATUS_CONFIRMED => TimelineItem::STATE_STAFFED, + Disposition::STATUS_ENDED => TimelineItem::STATE_STAFFED, + Disposition::STATUS_CHECKING_INVOICE => TimelineItem::STATE_STAFFED, + Disposition::STATUS_PAID => TimelineItem::STATE_STAFFED, + Disposition::STATUS_COMPLETED => TimelineItem::STATE_STAFFED, + ]; + + foreach ($expected as $status => $state) { + $assignment = $this->assignment(1); + $this->dispose($assignment, 'Anna', $status); + + $this->assertSame($state, $this->items($assignment)[0]->getState(), $status); + } + } + + /** + * @return list + */ + private function items(Assignment $assignment): array + { + $rows = (new TimelineService())->preprocess([$assignment]); + + return array_merge(...array_map('array_values', $rows['DGS'])); + } + + /** + * @param list $items + * + * @return list dispositions first + */ + private function sortByType(array $items): array + { + usort($items, fn (TimelineItem $a, TimelineItem $b) => strcmp($b->getType(), $a->getType())); + + return $items; + } + + private function assignment(int $slots): Assignment + { + $destination = (new Destination()) + ->setHotelCode('DGS01') + ->setDateFrom(new \DateTimeImmutable('2026-12-05')) + ->setDateTo(new \DateTimeImmutable('2026-12-12')) + ; + + return (new Assignment()) + ->setDestination($destination) + ->setJobProfile((new JobProfile())->setName('Reiseleitung')) + ->setAvailableDispositions($slots) + ; + } + + private function dispose(Assignment $assignment, string $firstName, string $status): void + { + $disposition = new Disposition(new Application($assignment, $this->teamer($firstName))); + $disposition->setStatus($status); + $assignment->addDisposition($disposition); + } + + private function teamer(string $firstName): Teamer + { + return (new Teamer())->setFirstName($firstName)->setLastName('Probe'); + } +}