Files
Toiminnanohjaus/app/Modules/Shared/Application/UseCases/HolidayCalendar.php
T
petriandClaude Opus 5.5 b4f2f1cfaa Add the task calendar, holidays, workflow templates and customer removal
Task calendar (/jobs/calendar) replaces per-unit task Kanban columns
(K34): a two-week Gantt of tasks per person. Bars move, resize and
change person by dragging or through a keyboard dialog. Each task's
estimate is booked evenly over working days into task_bookings, and
what exceeds the daily maximum (jobs.daily_capacity_hours, 8 h) moves
to the next working day. Tasks get planned_from/planned_to on the form.

Holidays (/settings/holidays): Finnish public holidays calculated per
year, which the admin can take out of use, plus company days off.

Also includes the earlier uncommitted work: workflow templates with
adding tasks from a template, and removing customers no module refers to.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 18:13:11 +02:00

149 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Shared\Application\UseCases;
use App\Modules\Shared\Application\Contracts\HolidayOverrides;
use App\Modules\Shared\Application\DTOs\Holiday;
use App\Modules\Shared\Application\DTOs\HolidayOverride;
use App\Modules\Shared\Domain\Exceptions\HolidayAlreadyExists;
use App\Modules\Shared\Domain\Exceptions\InvalidHolidayName;
use App\Modules\Shared\Domain\Exceptions\NotAHoliday;
use App\Modules\Shared\Domain\Services\FinnishHolidays;
use DateTimeImmutable;
use InvalidArgumentException;
/**
* The holidays (arkipyhät) of the calendar: the Finnish public holidays,
* calculated for every year, minus those the admin has taken out of use,
* plus the company's own days off (Asetukset → Arkipyhät). Holidays are
* shown like weekends, but work can still be scheduled on them. Who may
* change them is checked by the caller (admin).
*/
final class HolidayCalendar
{
public function __construct(private HolidayOverrides $overrides) {}
/**
* Every holiday between the dates, including those out of use.
*
* @return list<Holiday> In date order.
*/
public function between(string $from, string $to): array
{
$fromDate = self::date($from);
$toDate = self::date($to);
$holidays = [];
for ($year = (int) $fromDate->format('Y'); $year <= (int) $toDate->format('Y'); $year++) {
foreach (FinnishHolidays::forYear($year) as $date => $key) {
if ($date >= $from && $date <= $to) {
$holidays[$date] = new Holiday($date, $key, null, true);
}
}
}
foreach ($this->overrides->between($from, $to) as $override) {
$calculated = $holidays[$override->date] ?? null;
if ($calculated !== null) {
$holidays[$override->date] = new Holiday($override->date, $calculated->key, null, $override->isHoliday);
} elseif ($override->isHoliday) {
$holidays[$override->date] = new Holiday($override->date, null, $override->name, true);
}
}
ksort($holidays);
return array_values($holidays);
}
/**
* The holidays in use between the dates.
*
* @return list<string> Y-m-d dates in order.
*/
public function activeDatesBetween(string $from, string $to): array
{
return array_values(array_map(
fn (Holiday $holiday): string => $holiday->date,
array_filter($this->between($from, $to), fn (Holiday $holiday): bool => $holiday->active),
));
}
/**
* @throws NotAHoliday
*/
public function disable(string $date): void
{
$this->calculatedKey($date);
$this->overrides->save(new HolidayOverride($date, false));
}
/**
* @throws NotAHoliday
*/
public function enable(string $date): void
{
$this->calculatedKey($date);
$this->overrides->delete($date);
}
/**
* @throws InvalidHolidayName
* @throws HolidayAlreadyExists
*/
public function addCompanyDay(string $date, string $name): void
{
$name = trim((string) preg_replace('/\s+/u', ' ', $name));
if ($name === '' || mb_strlen($name) > 100) {
throw new InvalidHolidayName;
}
self::date($date);
if (isset(FinnishHolidays::forYear((int) substr($date, 0, 4))[$date]) || $this->overrides->find($date) !== null) {
throw new HolidayAlreadyExists;
}
$this->overrides->save(new HolidayOverride($date, true, $name));
}
/**
* @throws NotAHoliday
*/
public function removeCompanyDay(string $date): void
{
$override = $this->overrides->find($date);
if ($override === null || ! $override->isHoliday || isset(FinnishHolidays::forYear((int) substr($date, 0, 4))[$date])) {
throw new NotAHoliday;
}
$this->overrides->delete($date);
}
/**
* @throws NotAHoliday
*/
private function calculatedKey(string $date): string
{
self::date($date);
return FinnishHolidays::forYear((int) substr($date, 0, 4))[$date] ?? throw new NotAHoliday;
}
private static function date(string $value): DateTimeImmutable
{
$date = DateTimeImmutable::createFromFormat('!Y-m-d', $value);
if ($date === false || $date->format('Y-m-d') !== $value) {
throw new InvalidArgumentException("Not a Y-m-d date: {$value}");
}
return $date;
}
}