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]>
107 lines
3.5 KiB
PHP
107 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Jobs\Application\Services;
|
|
|
|
use App\Modules\Jobs\Domain\Contracts\TaskBookingRepository;
|
|
use App\Modules\Jobs\Domain\Entities\Task;
|
|
use App\Modules\Jobs\Domain\Services\TaskScheduler;
|
|
use App\Modules\Shared\Application\Contracts\Settings;
|
|
use App\Modules\Shared\Application\UseCases\HolidayCalendar;
|
|
use DateInterval;
|
|
use DateTimeImmutable;
|
|
|
|
/**
|
|
* Books a saved task's estimate from each assignee's days with the
|
|
* TaskScheduler, using the holidays in use and the daily maximum
|
|
* (setting `jobs.daily_capacity_hours`, 8 h by default). When the days
|
|
* are full, the task's planned end moves later.
|
|
*/
|
|
final class TaskPlanner
|
|
{
|
|
public const string CAPACITY_KEY = 'jobs.daily_capacity_hours';
|
|
|
|
private const int DEFAULT_CAPACITY_HOURS = 8;
|
|
|
|
/** Days past the start in which holidays and other bookings are looked up. */
|
|
private const int LOOKAHEAD_DAYS = 400;
|
|
|
|
public function __construct(
|
|
private TaskBookingRepository $bookings,
|
|
private HolidayCalendar $holidays,
|
|
private Settings $settings,
|
|
) {}
|
|
|
|
public function capacityMinutes(): int
|
|
{
|
|
$hours = (float) str_replace(',', '.', $this->settings->get(self::CAPACITY_KEY) ?? '');
|
|
|
|
return (int) round(($hours > 0 && $hours <= 24 ? $hours : self::DEFAULT_CAPACITY_HOURS) * 60);
|
|
}
|
|
|
|
/**
|
|
* Rebooks the task's assignees and, if their days were full, moves the
|
|
* task's planned end later. The task must be saved (have an id);
|
|
* save it again afterwards.
|
|
*
|
|
* @return bool Whether the planned end moved later.
|
|
*/
|
|
public function plan(Task $task): bool
|
|
{
|
|
$taskId = (int) $task->id();
|
|
$from = $task->plannedFrom();
|
|
$to = $task->plannedTo();
|
|
|
|
if ($from === null || $to === null) {
|
|
$this->bookings->deleteForTask($taskId);
|
|
|
|
return false;
|
|
}
|
|
|
|
$this->bookings->deleteForTask($taskId, $task->assigneeUserIds());
|
|
|
|
$minutes = (int) round(($task->estimatedHours()->value ?? 0) * 60);
|
|
$lookahead = $to->add(new DateInterval('P'.self::LOOKAHEAD_DAYS.'D'))->format('Y-m-d');
|
|
$holidays = $this->holidays->activeDatesBetween($from->format('Y-m-d'), $lookahead);
|
|
$capacity = $this->capacityMinutes();
|
|
$end = $to;
|
|
|
|
foreach ($task->assigneeUserIds() as $userId) {
|
|
$schedule = TaskScheduler::schedule(
|
|
$from,
|
|
$to,
|
|
$minutes,
|
|
$this->bookings->otherOpenMinutes($userId, $from->format('Y-m-d'), $lookahead, $taskId),
|
|
$capacity,
|
|
$holidays,
|
|
);
|
|
|
|
$this->bookings->replaceFor($taskId, $userId, $schedule->minutesByDay);
|
|
|
|
if ($schedule->end > $end) {
|
|
$end = $schedule->end;
|
|
}
|
|
}
|
|
|
|
if ($end > $to) {
|
|
$task->schedule($from, $end);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* The end of a task placed on a day without an end (TaskScheduler::defaultEnd).
|
|
*/
|
|
public function defaultEnd(DateTimeImmutable $from, Task $task): DateTimeImmutable
|
|
{
|
|
$minutes = (int) round(($task->estimatedHours()->value ?? 0) * 60);
|
|
$lookahead = $from->add(new DateInterval('P'.self::LOOKAHEAD_DAYS.'D'))->format('Y-m-d');
|
|
|
|
return TaskScheduler::defaultEnd($from, $minutes, $this->capacityMinutes(), $this->holidays->activeDatesBetween($from->format('Y-m-d'), $lookahead));
|
|
}
|
|
}
|