feat: redesign the timeline as day columns with a bar per placement

This commit is contained in:
fromme committed 2026-11-12 11:14:25 +01:00
1 parent 4e50f23558
commit e976342c09
8 files changed
+413 -190

No files matched your search

@@ -1,22 +1,26 @@
import { Controller } from '@hotwired/stimulus' import { Controller } from '@hotwired/stimulus'
export default class extends Controller { export default class extends Controller {
static targets = [ 'today' ] static targets = [ 'scroller', 'today' ]
connect() { connect() {
if (false === this.hasTodayTarget) { requestAnimationFrame(() => this.scrollToToday({ behavior: 'instant' }))
}
// centres today's column; the house column stays in front of the left part
scrollToToday({ behavior = 'smooth' } = {}) {
if (false === this.hasScrollerTarget || false === this.hasTodayTarget) {
return return
} }
requestAnimationFrame(() => { const scroller = this.scrollerTarget
const containerRect = this.element.getBoundingClientRect() const scrollerRect = scroller.getBoundingClientRect()
const todayRect = this.todayTarget.getBoundingClientRect() const todayRect = this.todayTarget.getBoundingClientRect()
const scrollLeft = todayRect.left - containerRect.left + this.element.scrollLeft const scrollLeft = todayRect.left - scrollerRect.left + scroller.scrollLeft
const centered = scrollLeft - (this.element.clientWidth / 2) + (todayRect.width / 2) const centered = scrollLeft - (scroller.clientWidth / 2) + (todayRect.width / 2)
const maxScrollLeft = this.element.scrollWidth - this.element.clientWidth const maxScrollLeft = scroller.scrollWidth - scroller.clientWidth
this.element.scrollLeft = Math.min(Math.max(0, centered), maxScrollLeft) scroller.scrollTo({ left: Math.min(Math.max(0, centered), maxScrollLeft), behavior })
})
} }
} }
+54 -2
View File
@@ -1,4 +1,56 @@
.scroll-container { /*
* The timeline (administrative/assignment/timeline.html.twig): one grid per house with a fixed
* house column and one column per day. The header, the background and every house share the
* column template, so their columns line up; --timeline-days is set on .timeline.
*/
.timeline {
--timeline-label: 11rem;
--timeline-day: 1.75rem;
@apply overflow-auto; @apply overflow-auto;
max-height: calc(100vh - 320px); max-height: calc(100vh - 260px);
}
.timeline-grid {
display: grid;
grid-template-columns: var(--timeline-label) repeat(var(--timeline-days), var(--timeline-day));
}
.timeline-background {
@apply pointer-events-none absolute inset-0;
grid-template-rows: 100%;
}
.timeline-header {
@apply sticky top-0 z-20 border-b-2 border-brand bg-white;
}
.timeline-corner {
@apply sticky left-0 z-10 border-r border-line bg-white;
}
.timeline-house {
@apply relative border-b border-line py-1;
grid-auto-rows: 1.75rem;
}
.timeline-house-label {
@apply sticky left-0 z-10 -my-1 border-r border-line bg-white px-3 py-2;
}
.timeline-bar {
@apply relative z-[1] mx-px my-0.5 flex min-w-0 items-center gap-x-1 overflow-hidden whitespace-nowrap px-1.5 text-xs leading-none;
}
/* the states differ in shape too, not only in colour: dashed, outlined, filled */
.timeline-bar--open {
@apply border border-dashed border-muted bg-white text-muted;
}
.timeline-bar--pending {
@apply border border-brand bg-brand-light text-brand-dark;
}
.timeline-bar--staffed {
@apply border border-emerald-800 bg-emerald-700 text-white;
} }
@@ -44,11 +44,11 @@ class TimelineController extends AbstractController
->getListQuery($filterDto, 'destination.dateFrom', $hotelCodes) ->getListQuery($filterDto, 'destination.dateFrom', $hotelCodes)
->getResult() ->getResult()
; ;
$timelineData = $this->timelineService->preprocess($assignments); $period = $filterDto->getPeriod();
return $this->render('administrative/assignment/timeline.html.twig', [ return $this->render('administrative/assignment/timeline.html.twig', [
'data' => $timelineData, 'houses' => $this->timelineService->preprocess($assignments, $period),
'period' => $period, 'calendar' => $this->timelineService->getCalendar($period),
'filterDto' => $filterDto, 'filterDto' => $filterDto,
]); ]);
} }
+70
View File
@@ -0,0 +1,70 @@
<?php
namespace App\Model;
/**
* One house of the timeline: its rows of non-overlapping bars and the count of vacancies nobody
* applied for.
*/
class TimelineHouse
{
/** @var list<list<TimelineItem>> */
private array $rows = [[]];
public function __construct(
private readonly string $code,
private readonly ?string $name,
) {
}
public function getCode(): string
{
return $this->code;
}
public function getName(): ?string
{
return $this->name;
}
/**
* @return list<list<TimelineItem>>
*/
public function getRows(): array
{
return $this->rows;
}
public function getOpenSlots(): int
{
$count = 0;
foreach ($this->rows as $row) {
foreach ($row as $item) {
if (TimelineItem::STATE_OPEN === $item->getState()) {
++$count;
}
}
}
return $count;
}
/**
* Puts the item into the first row it does not overlap, or into a new one.
*/
public function addItem(TimelineItem $item): void
{
foreach ($this->rows as $index => $row) {
foreach ($row as $entry) {
if ($entry->getDateTo() >= $item->getDateFrom() && $entry->getDateFrom() <= $item->getDateTo()) {
continue 2;
}
}
$this->rows[$index][] = $item;
return;
}
$this->rows[] = [$item];
}
}
+25 -7
View File
@@ -25,6 +25,8 @@ class TimelineItem
private ?string $jobProfile = null; private ?string $jobProfile = null;
private ?string $teamerName = null; private ?string $teamerName = null;
private ?string $teamerUuid = null; private ?string $teamerUuid = null;
private int $column = 1;
private int $span = 1;
public function __construct(private readonly string $type) public function __construct(private readonly string $type)
{ {
@@ -196,17 +198,33 @@ class TimelineItem
return $this; return $this;
} }
public function getKey(): ?string /**
* The first day column of the bar, counted from 1.
*/
public function getColumn(): int
{ {
return $this->getDateFrom()?->format('Ymd'); return $this->column;
} }
public function getNumberOfDays(): int public function setColumn(int $column): static
{ {
if (null === $this->getDateFrom() || null === $this->getDateTo()) { $this->column = $column;
return 1;
}
return $this->getDateFrom()->diff($this->getDateTo())->days + 1; return $this;
}
/**
* The number of day columns the bar covers.
*/
public function getSpan(): int
{
return $this->span;
}
public function setSpan(int $span): static
{
$this->span = $span;
return $this;
} }
} }
+72 -48
View File
@@ -2,29 +2,40 @@
namespace App\Service\Assignment; namespace App\Service\Assignment;
use App\Config\HouseCatalog;
use App\Entity\Assignment; use App\Entity\Assignment;
use App\Model\TimelineHouse;
use App\Model\TimelineItem; use App\Model\TimelineItem;
use Carbon\CarbonImmutable;
use Carbon\CarbonPeriodImmutable; use Carbon\CarbonPeriodImmutable;
/** /**
* Service for processing assignments into timeline visualization data. * Service for processing assignments into timeline visualization data.
* Organizes assignments and dispositions by hotel in a timeline format, * Organizes assignments and dispositions by house in a timeline format,
* handling overlapping dates by distributing items across multiple rows. * handling overlapping dates by distributing items across multiple rows.
*/ */
class TimelineService class TimelineService
{ {
public function __construct(private readonly HouseCatalog $houseCatalog)
{
}
/** /**
* Preprocesses assignments into a timeline structure organized by hotel. * Preprocesses assignments into a timeline structure organized by house.
* Creates timeline items for both vacant assignment slots and actual dispositions, * Creates timeline items for both vacant assignment slots and actual dispositions,
* distributing them across multiple rows to avoid date overlaps. * placed on the period's day columns and distributed across rows to avoid date overlaps.
* *
* @param Assignment[] $assignments Array of assignments to process * @param Assignment[] $assignments Array of assignments to process
* *
* @return array Multi-dimensional array: [hotelCode][rowIndex][itemKey] => TimelineItem * @return list<TimelineHouse> sorted by house name
*/ */
public function preprocess(array $assignments): array public function preprocess(array $assignments, CarbonPeriodImmutable $period): array
{ {
$rows = []; $periodFrom = $period->first()->startOfDay();
$periodTo = $period->last()->startOfDay();
/** @var array<string, TimelineHouse> $houses */
$houses = [];
foreach ($assignments as $assignment) { foreach ($assignments as $assignment) {
// exclude called off assignments // exclude called off assignments
@@ -32,72 +43,70 @@ class TimelineService
if (Assignment::STATUS_CALLED_OFF === $assignment->getStatus()) { if (Assignment::STATUS_CALLED_OFF === $assignment->getStatus()) {
continue; continue;
} }
$destination = $assignment->getDestination(); $hotelCode = $assignment->getDestination()->getHotelCodeNormalized();
$hotelCode = $destination->getHotelCodeNormalized();
// add first row for current hotel unless existing $houses[$hotelCode] ??= new TimelineHouse($hotelCode, $this->houseCatalog->getName($hotelCode));
if (false === isset($rows[$hotelCode])) {
$rows[$hotelCode] = [[]];
}
// called-off dispositions free their slot again // called-off dispositions free their slot again
$dispositions = $assignment->getActiveDispositions(); $dispositions = $assignment->getActiveDispositions();
$items = [];
// Create items for the vacant slots, if any // Create items for the vacant slots, if any
$item = TimelineItem::fromAssignment($assignment);
for ($i = $dispositions->count(); $i < $assignment->getAvailableDispositions(); ++$i) { for ($i = $dispositions->count(); $i < $assignment->getAvailableDispositions(); ++$i) {
$this->addItem($rows[$hotelCode], $item); $items[] = TimelineItem::fromAssignment($assignment);
} }
// Create items for actual dispositions // Create items for actual dispositions
foreach ($dispositions as $disposition) { foreach ($dispositions as $disposition) {
$this->addItem($rows[$hotelCode], TimelineItem::fromDisposition($disposition)); $items[] = TimelineItem::fromDisposition($disposition);
}
foreach ($items as $item) {
// an item without dates has no place on the timeline
if (null === $item->getDateFrom() || null === $item->getDateTo()) {
continue;
}
$this->place($item, $periodFrom, $periodTo);
$houses[$hotelCode]->addItem($item);
} }
} }
return $rows; usort($houses, fn (TimelineHouse $a, TimelineHouse $b) => strcmp($a->getName() ?? $a->getCode(), $b->getName() ?? $b->getCode()));
return $houses;
} }
/** /**
* Adds a timeline item to the appropriate row, ensuring no date overlaps. * The days of the period with what the header and the background need, and the months
* Finds the first available row where the item doesn't overlap with existing items, * spanning them. Columns count from 1.
* or creates a new row if necessary.
* *
* @param array $rows Reference to the rows array for a specific hotel * @return array{days: list<array{date: CarbonImmutable, column: int, weekend: bool, today: bool, monthStart: bool}>, months: list<array{label: string, column: int, span: int}>}
* @param TimelineItem $item The timeline item to add
*/ */
private function addItem(array &$rows, TimelineItem $item): void public function getCalendar(CarbonPeriodImmutable $period): array
{ {
// The key identifies the item within its row, so one without it cannot be placed. $days = [];
$key = $item->getKey(); $months = [];
if (null === $key) { $today = CarbonImmutable::today();
return;
}
$rowIndex = 0; foreach ($period as $index => $day) {
$fitsRow = false; $day = $day->startOfDay()->locale('de');
$column = $index + 1;
$monthStart = 1 === $column || 1 === $day->day;
// Find first row where item doesn't overlap with existing items $days[] = [
foreach ($rows as $index => $row) { 'date' => $day,
$fitsRow = true; 'column' => $column,
foreach ($row as $entry) { 'weekend' => $day->isWeekend(),
/** @var TimelineItem $entry */ 'today' => $day->equalTo($today),
// Check for date overlap: item overlaps if its end >= entry start AND item start <= entry end 'monthStart' => $monthStart,
if ($entry->getDateTo() >= $item->getDateFrom() && $entry->getDateFrom() <= $item->getDateTo()) { ];
$fitsRow = false;
} if (true === $monthStart) {
} $months[] = ['label' => $day->isoFormat('MMMM YYYY'), 'column' => $column, 'span' => 0];
$rowIndex = $index;
if (true === $fitsRow) {
break;
} }
++$months[array_key_last($months)]['span'];
} }
// If no existing row fits, create a new row return ['days' => $days, 'months' => $months];
if (false === $fitsRow) {
++$rowIndex;
}
$rows[$rowIndex][$key] = $item;
} }
/** /**
@@ -114,4 +123,19 @@ class TimelineService
return CarbonPeriodImmutable::create($dateFrom, $dateTo); return CarbonPeriodImmutable::create($dateFrom, $dateTo);
} }
/**
* Sets the item's day columns, clipped to the period: the timeline also shows trips that
* started before it or end after it.
*/
private function place(TimelineItem $item, CarbonImmutable $periodFrom, CarbonImmutable $periodTo): void
{
$from = max($periodFrom, CarbonImmutable::instance($item->getDateFrom())->startOfDay());
$to = min($periodTo, CarbonImmutable::instance($item->getDateTo())->startOfDay());
$item
->setColumn((int) $periodFrom->diffInDays($from) + 1)
->setSpan(max(1, (int) $from->diffInDays($to) + 1))
;
}
} }
@@ -2,129 +2,138 @@
{% block title %}Zeitleiste{% endblock %} {% block title %}Zeitleiste{% endblock %}
{% macro cell(item, colspan) %} {% set state_labels = { open: 'offen', pending: 'in Bewerbung', staffed: 'besetzt' } %}
<td class="{{ html_classes('border border-gray-700', {
'bg-gray-100': item.state == 'open', {% macro bar(item, row, state_labels) %}
'bg-blue-500 text-gray-50': item.state == 'pending', {% set is_disposition = item.type == 'disposition' %}
'bg-green-500 text-gray-50': item.state == 'staffed', <div class="timeline-bar timeline-bar--{{ item.state }}"
}) }}" colspan="{{ colspan }}"> style="grid-row: {{ row }}; grid-column: {{ item.column + 1 }} / span {{ item.span }}"
{% if item.type == 'disposition' %} title="{{ item.jobProfile }}{{ is_disposition ? ' · ' ~ item.teamerName }} · {{ item.dateRange }} · {{ state_labels[item.state] }}">
<div class="flex items-center space-x-2"> <span class="sr-only">{{ state_labels[item.state] }}:</span>
{% if is_granted('ROLE_HOUSE_MANAGER') %} {% if not is_disposition and item.state == 'open' %}
<span class="inline-block text-xs font-semibold">{{ item.jobProfile }}</span> <span class="font-bold uppercase">offen</span>
{% else %}
<a href="{{ path('app_administrative_assignment_detail', { 'uuid': item.assignmentUuid, 'r': return_url() }) }}"
class="inline-block text-xs font-semibold">
{{ item.jobProfile }}
</a>
{% endif %}
<span>|</span>
<button type="button"
class="flex items-center space-x-2"
title="Teamer:inneninfo {{ item.teamerName }}"
hx-get="{{ path('app_administrative_teamer_info', { 'uuid': item.teamerUuid, 'r': return_url() }) }}"
hx-target="body"
hx-swap="beforeend">
<span class="text-xs font-semibold">{{ item.teamerName }}</span>
</button>
</div>
{% else %}
{% if is_granted('ROLE_HOUSE_MANAGER') %}
<span class="text-xs font-semibold">{{ item.jobProfile }}</span>
{% else %}
<a href="{{ path('app_administrative_assignment_detail', { 'uuid': item.assignmentUuid, 'r': return_url() }) }}"
class="text-xs font-semibold">
{{ item.jobProfile }}
</a>
{% endif %}
{% endif %} {% endif %}
</td> {% if is_granted('ROLE_HOUSE_MANAGER') %}
<span class="truncate">{{ item.jobProfile }}</span>
{% else %}
<a href="{{ path('app_administrative_assignment_detail', { 'uuid': item.assignmentUuid, 'r': return_url() }) }}"
class="truncate hover:underline">{{ item.jobProfile }}</a>
{% endif %}
{% if is_disposition %}
<span aria-hidden="true">·</span>
<button type="button"
class="truncate font-bold hover:underline"
title="Teamer:inneninfo {{ item.teamerName }}"
hx-get="{{ path('app_administrative_teamer_info', { 'uuid': item.teamerUuid, 'r': return_url() }) }}"
hx-target="body"
hx-swap="beforeend">{{ item.teamerName }}</button>
{% endif %}
</div>
{% endmacro %} {% endmacro %}
{% block content %} {% block content %}
<div class="flex items-start justify-between pb-4"> <div {{ stimulus_controller('timeline-scroll') }}>
<h1 class="text-2xl font-bold"> <div class="flex items-start justify-between pb-4">
Zeitleiste <h1 class="text-2xl font-bold">
</h1> Zeitleiste
<div class="flex flex-col items-end space-y-2 md:flex-row md:items-center md:space-x-2 md:space-y-0"> </h1>
<button type="button" <div class="flex flex-col items-end space-y-2 md:flex-row md:items-center md:space-x-2 md:space-y-0">
class="{{ html_classes('btn btn--small', { 'btn--secondary': filterDto.active }) }}" <button type="button"
title="Filter" class="btn btn--small btn--secondary"
hx-get="{{ path('app_common_timeline_filter', { 'r': return_url() }) }}" {{ stimulus_action('timeline-scroll', 'scrollToToday') }}>
hx-target="body" {{ icon('calendar', 'w-4 h-4 shrink-0') }}
hx-swap="beforeend"> <span>Heute</span>
{{ icon('filter', 'w-4 h-4 shrink-0') }} </button>
<button type="button"
class="{{ html_classes('btn btn--small', { 'btn--secondary': filterDto.active }) }}"
title="Filter"
hx-get="{{ path('app_common_timeline_filter', { 'r': return_url() }) }}"
hx-target="body"
hx-swap="beforeend">
{{ icon('filter', 'w-4 h-4 shrink-0') }}
{% if filterDto.active %}
<span class="whitespace-nowrap">{{ filterDto.activeFiltersCount }} Filter aktiv</span>
{% else %}
<span>Filter</span>
{% endif %}
</button>
{% if filterDto.active %} {% if filterDto.active %}
<span class="whitespace-nowrap">{{ filterDto.activeFiltersCount }} Filter aktiv</span> <a href="{{ path('app_common_timeline_filter_reset', { 'r': return_url() }) }}" class="btn btn--small btn--secondary">
{% else %} Reset
<span>Filter</span> </a>
{% endif %} {% endif %}
</button> </div>
{% if filterDto.active %}
<a href="{{ path('app_common_timeline_filter_reset', { 'r': return_url() }) }}" class="btn btn--small btn--secondary">
Reset
</a>
{% endif %}
</div> </div>
</div>
<div class="scroll-container border border-gray-300 mb-4" {{ stimulus_controller('timeline-scroll') }}> {% if houses is empty %}
<table> <div class="card p-4 mb-4 text-muted">Keine Ergebnisse</div>
<tr class="[&_th]:sticky [&_th]:top-0 [&_th]:z-10"> {% else %}
<th></th> <div class="timeline card mb-4"
{% for day in filterDto.period %} style="--timeline-days: {{ calendar.days|length }}"
<th{% if day|date('Y-m-d') == 'now'|date('Y-m-d') %} {{ stimulus_target('timeline-scroll', 'today') }}{% endif %}> {{ stimulus_target('timeline-scroll', 'scroller') }}>
{{ day | date('d.m.Y') }} <div class="relative w-max min-w-full">
</th> {# weekends, month boundaries and today, behind the bars #}
{% endfor %} <div class="timeline-grid timeline-background" aria-hidden="true">
</tr> {% for day in calendar.days %}
{% for hotelCode, rows in data %} {% if day.weekend or day.today or day.monthStart %}
<tr class="border-t-2 border-gray-700"> <div class="{{ html_classes({
<td class="border border-gray-700 bg-gray-200 align-middle font-bold sticky left-0 z-10" rowspan="{{ rows | length }}"> 'bg-page': day.weekend,
{{ hotelCode }} 'border-l border-slate-300': day.monthStart and day.column > 1,
</td> }) }}" style="grid-column: {{ day.column + 1 }}">
{% for day in filterDto.period %} {% if day.today %}
{% if colspan is defined and colspan > 1 %} <div class="mx-auto h-full w-0.5 bg-brand-bright"></div>
{% set colspan = colspan - 1 %} {% endif %}
{% elseif rows[0][day|date('Ymd')] is defined %} </div>
{% set item = rows[0][day|date('Ymd')] %}
{% set colspan = item.numberOfDays %}
{{ _self.cell(item, colspan) }}
{% else %}
<td class="border border-gray-700"></td>
{% endif %}
{% endfor %}
</tr>
{% for row in rows[1:] %}
<tr>
{% for day in filterDto.period %}
{% if colspan is defined and colspan > 1 %}
{% set colspan = colspan - 1 %}
{% elseif row[day|date('Ymd')] is defined %}
{% set item = row[day|date('Ymd')] %}
{% set colspan = item.numberOfDays %}
{{ _self.cell(item, colspan) }}
{% else %}
<td class="border border-gray-700"></td>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</tr> </div>
{% endfor %}
{% else %} <div class="timeline-grid timeline-header">
<td colspan="{{ filterDto.period | length }}"> <div class="timeline-corner" style="grid-row: 1 / span 2"></div>
Keine Ergebnisse {% for month in calendar.months %}
</td> <div class="{{ html_classes('truncate px-2 py-1 text-xs font-bold uppercase tracking-wide text-brand', { 'border-l border-slate-300': not loop.first }) }}"
{% endfor %} style="grid-row: 1; grid-column: {{ month.column + 1 }} / span {{ month.span }}">
</table> {{ month.label }}
</div> </div>
<div class="flex space-x-2"> {% endfor %}
<div class="flex items-center space-x-2 bg-gray-100 px-4 py-1"> {% for day in calendar.days %}
<span>Bedarf</span> <div class="{{ html_classes('flex flex-col items-center py-1 text-[11px] leading-tight', {
</div> 'text-muted': day.weekend and not day.today,
<div class="flex items-center space-x-2 bg-blue-500 text-gray-50 px-4 py-1"> 'bg-brand-bright text-white font-bold': day.today,
<span>in Bewerbung</span> }) }}"
</div> style="grid-row: 2; grid-column: {{ day.column + 1 }}"
<div class="flex items-center space-x-2 bg-green-500 text-gray-50 px-4 py-1"> {% if day.today %}{{ stimulus_target('timeline-scroll', 'today') }}{% endif %}>
<span>besetzt</span> <span class="font-bold">{{ day.date.day }}</span>
</div> <span>{{ day.date.isoFormat('dd') }}</span>
</div>
{% endfor %}
</div>
{% for house in houses %}
<div class="timeline-grid timeline-house">
<div class="timeline-house-label" style="grid-row: 1 / span {{ house.rows|length }}">
<span class="block truncate font-bold">{{ house.name ?? house.code }}</span>
<span class="block text-xs text-muted">
{{ house.code }}{% if house.openSlots > 0 %} · <span class="font-bold text-ink">{{ house.openSlots }} offen</span>{% endif %}
</span>
</div>
{% for row in house.rows %}
{% set row_number = loop.index %}
{% for item in row %}
{{ _self.bar(item, row_number, state_labels) }}
{% endfor %}
{% endfor %}
</div>
{% endfor %}
</div>
</div>
{% endif %}
<ul class="flex flex-wrap gap-x-6 gap-y-2 text-xs" aria-label="Legende">
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--staffed w-10 h-5"></span> besetzt</li>
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--pending w-10 h-5"></span> in Bewerbung oder Vertrag in Prüfung</li>
<li class="flex items-center gap-x-2"><span class="timeline-bar timeline-bar--open w-10 h-5"></span> offen, noch keine Bewerbung</li>
<li class="flex items-center gap-x-2"><span class="inline-block h-5 w-0.5 bg-brand-bright"></span> heute</li>
</ul>
</div> </div>
{% endblock %} {% endblock %}
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Tests\Service\Assignment; namespace App\Tests\Service\Assignment;
use App\Config\HouseCatalog;
use App\Entity\Application; use App\Entity\Application;
use App\Entity\Assignment; use App\Entity\Assignment;
use App\Entity\Destination; use App\Entity\Destination;
@@ -12,6 +13,7 @@ use App\Entity\JobProfile;
use App\Entity\Teamer; use App\Entity\Teamer;
use App\Model\TimelineItem; use App\Model\TimelineItem;
use App\Service\Assignment\TimelineService; use App\Service\Assignment\TimelineService;
use Carbon\CarbonPeriodImmutable;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class TimelineServiceTest extends TestCase class TimelineServiceTest extends TestCase
@@ -82,14 +84,58 @@ class TimelineServiceTest extends TestCase
} }
} }
/**
* Trips running into or out of the window are clipped to its columns.
*/
public function testItemsArePlacedOnTheDayColumnsAndClipped(): void
{
$inside = $this->assignment(1);
$startsBefore = $this->assignment(1, '2026-11-28', '2026-12-03');
$endsAfter = $this->assignment(1, '2027-01-28', '2027-02-03');
$columns = array_map(
fn (Assignment $assignment) => [$this->items($assignment)[0]->getColumn(), $this->items($assignment)[0]->getSpan()],
[$inside, $startsBefore, $endsAfter],
);
// window 2026-12-01 to 2027-01-31: 5 December is column 5, eight days
$this->assertSame([[5, 8], [1, 3], [59, 4]], $columns);
}
public function testHousesCarryNameRowsAndOpenSlots(): void
{
$first = $this->assignment(2);
$overlapping = $this->assignment(1, '2026-12-10', '2026-12-14');
$overlapping->addApplication(new Application($overlapping, $this->teamer('Carla')));
$other = $this->assignment(1);
$other->getDestination()->setHotelCode('AGR02');
$houses = $this->service()->preprocess([$first, $overlapping, $other], $this->period());
// sorted by name: Rotbach (AGR) before Spinabad (DGS)
$this->assertSame(['Rotbach', 'Spinabad'], array_map(fn ($house) => $house->getName(), $houses));
$this->assertCount(3, $houses[1]->getRows());
$this->assertSame(2, $houses[1]->getOpenSlots());
}
/** /**
* @return list<TimelineItem> * @return list<TimelineItem>
*/ */
private function items(Assignment $assignment): array private function items(Assignment $assignment): array
{ {
$rows = (new TimelineService())->preprocess([$assignment]); $houses = $this->service()->preprocess([$assignment], $this->period());
return array_merge(...array_map('array_values', $rows['DGS'])); return array_merge(...$houses[0]->getRows());
}
private function service(): TimelineService
{
return new TimelineService(new HouseCatalog(['AGR' => 'Rotbach', 'DGS' => 'Spinabad']));
}
private function period(): CarbonPeriodImmutable
{
return CarbonPeriodImmutable::create('2026-12-01', '2027-01-31');
} }
/** /**
@@ -104,12 +150,12 @@ class TimelineServiceTest extends TestCase
return $items; return $items;
} }
private function assignment(int $slots): Assignment private function assignment(int $slots, string $from = '2026-12-05', string $to = '2026-12-12'): Assignment
{ {
$destination = (new Destination()) $destination = (new Destination())
->setHotelCode('DGS01') ->setHotelCode('DGS01')
->setDateFrom(new \DateTimeImmutable('2026-12-05')) ->setDateFrom(new \DateTimeImmutable($from))
->setDateTo(new \DateTimeImmutable('2026-12-12')) ->setDateTo(new \DateTimeImmutable($to))
; ;
return (new Assignment()) return (new Assignment())