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]>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\Contracts;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\CalendarTask;
|
||||
|
||||
/**
|
||||
* Read side of the task calendar. Dates are Y-m-d and inclusive. The
|
||||
* service unit limits tasks to jobs of that unit; onlyAssigneeUserId to
|
||||
* tasks of that person on jobs they are involved in (subcontractors).
|
||||
*/
|
||||
interface TaskCalendarQueries
|
||||
{
|
||||
/**
|
||||
* Tasks whose planned period overlaps the dates.
|
||||
*
|
||||
* @return list<CalendarTask>
|
||||
*/
|
||||
public function scheduledTasks(string $from, string $to, ?int $serviceUnitId = null, ?int $onlyAssigneeUserId = null): array;
|
||||
|
||||
/**
|
||||
* Tasks that are not done and have no planned period, on jobs that are
|
||||
* not archived, by due date.
|
||||
*
|
||||
* @return list<CalendarTask>
|
||||
*/
|
||||
public function unscheduledOpenTasks(?int $serviceUnitId = null, ?int $onlyAssigneeUserId = null, int $limit = 100): array;
|
||||
|
||||
/**
|
||||
* Minutes each person's tasks that are not done book per day.
|
||||
*
|
||||
* @param list<int> $userIds
|
||||
* @return array<int, array<string, int>> user id => Y-m-d => minutes.
|
||||
*/
|
||||
public function openMinutesByUser(array $userIds, string $from, string $to): array;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* One assignee's part of a scheduled task: the first and last booked day
|
||||
* (null when the task books no time, e.g. without an estimate) and the
|
||||
* booked minutes.
|
||||
*/
|
||||
final readonly class CalendarAssignment
|
||||
{
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public ?string $from,
|
||||
public ?string $to,
|
||||
public int $minutes,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A task's bar on one row of the task calendar. Start and span are in
|
||||
* visible day columns (0 = the first day shown); a bar that continues
|
||||
* past the shown days is cut at the edge. Lane is the bar's line inside
|
||||
* the row, so that overlapping tasks do not cover each other.
|
||||
*/
|
||||
final readonly class CalendarBar
|
||||
{
|
||||
public function __construct(
|
||||
public CalendarTask $task,
|
||||
public ?int $userId,
|
||||
public string $from,
|
||||
public string $to,
|
||||
public int $minutes,
|
||||
public int $start,
|
||||
public int $span,
|
||||
public int $lane,
|
||||
public bool $late,
|
||||
) {}
|
||||
|
||||
public function key(): string
|
||||
{
|
||||
return $this->task->taskId.'-'.($this->userId ?? 'none');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* One person's row of the task calendar (userId null: tasks without an
|
||||
* assignee), with the minutes their open tasks book per shown day.
|
||||
*/
|
||||
final readonly class CalendarRow
|
||||
{
|
||||
/**
|
||||
* @param array<string, int> $minutesByDay Y-m-d => minutes, for every shown day.
|
||||
* @param list<CalendarBar> $bars Ordered by start.
|
||||
*/
|
||||
public function __construct(
|
||||
public ?int $userId,
|
||||
public string $name,
|
||||
public array $minutesByDay,
|
||||
public array $bars,
|
||||
public int $lanes,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return list<CalendarBar> The bars that are on the day.
|
||||
*/
|
||||
public function barsOn(int $dayIndex): array
|
||||
{
|
||||
return array_values(array_filter($this->bars, fn (CalendarBar $bar): bool => $bar->start <= $dayIndex && $dayIndex < $bar->start + $bar->span));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
|
||||
/**
|
||||
* A task as the task calendar shows it. Dates are Y-m-d.
|
||||
*/
|
||||
final readonly class CalendarTask
|
||||
{
|
||||
/**
|
||||
* @param list<CalendarAssignment> $assignments One per assignee.
|
||||
*/
|
||||
public function __construct(
|
||||
public int $taskId,
|
||||
public int $jobId,
|
||||
public string $jobNumber,
|
||||
public string $jobName,
|
||||
public string $title,
|
||||
public TaskStatus $status,
|
||||
public ?string $dueOn,
|
||||
public ?string $estimatedHours,
|
||||
public ?string $plannedFrom,
|
||||
public ?string $plannedTo,
|
||||
public array $assignments,
|
||||
) {}
|
||||
|
||||
public function assignmentOf(int $userId): ?CalendarAssignment
|
||||
{
|
||||
foreach ($this->assignments as $assignment) {
|
||||
if ($assignment->userId === $userId) {
|
||||
return $assignment;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* A task's planned schedule after it was moved, resized or given to
|
||||
* another person. Extended: the assignee's days were full, so the task
|
||||
* ends later than asked.
|
||||
*/
|
||||
final readonly class ScheduleResult
|
||||
{
|
||||
public function __construct(
|
||||
public string $plannedFrom,
|
||||
public string $plannedTo,
|
||||
public bool $extended,
|
||||
) {}
|
||||
}
|
||||
@@ -5,7 +5,8 @@ declare(strict_types=1);
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* Input for adding or changing a task. The due date is Y-m-d.
|
||||
* Input for adding or changing a task. Dates are Y-m-d; a planned start
|
||||
* without an end plans one day.
|
||||
*/
|
||||
final readonly class TaskData
|
||||
{
|
||||
@@ -20,5 +21,7 @@ final readonly class TaskData
|
||||
public ?string $dueOn = null,
|
||||
public ?string $estimatedHours = null,
|
||||
public array $assigneeUserIds = [],
|
||||
public ?string $plannedFrom = null,
|
||||
public ?string $plannedTo = null,
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -24,5 +24,7 @@ final readonly class TaskSummary
|
||||
public ?string $estimatedHours,
|
||||
public NamedId $workType,
|
||||
public array $assignees,
|
||||
public ?string $plannedFrom = null,
|
||||
public ?string $plannedTo = null,
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\DTOs;
|
||||
|
||||
/**
|
||||
* Due date (Y-m-d) and estimated hours ("1,5") entered for the task of one
|
||||
* workflow template step. Both are optional.
|
||||
*/
|
||||
final readonly class TemplateTaskSchedule
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $dueOn = null,
|
||||
public ?string $estimatedHours = null,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\Services;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\CalendarBar;
|
||||
use App\Modules\Jobs\Application\DTOs\CalendarRow;
|
||||
use App\Modules\Jobs\Application\DTOs\CalendarTask;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
|
||||
/**
|
||||
* Lays scheduled tasks out on the task calendar's rows: one row per
|
||||
* person (a task with several assignees is on each of their rows) and a
|
||||
* row for tasks without an assignee. Overlapping bars get their own lanes.
|
||||
*/
|
||||
final class TaskCalendarLayout
|
||||
{
|
||||
/**
|
||||
* @param list<string> $days The shown days, Y-m-d, consecutive.
|
||||
* @param list<array{id: ?int, name: string}> $people Rows in order; id null is the "no assignee" row.
|
||||
* @param list<CalendarTask> $tasks
|
||||
* @param array<int, array<string, int>> $minutesByUser user id => Y-m-d => booked minutes.
|
||||
* @return list<CalendarRow>
|
||||
*/
|
||||
public static function build(array $days, array $people, array $tasks, array $minutesByUser): array
|
||||
{
|
||||
$rows = [];
|
||||
|
||||
foreach ($people as $person) {
|
||||
$bars = [];
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
$bar = self::bar($task, $person['id'], $days);
|
||||
|
||||
if ($bar !== null) {
|
||||
$bars[] = $bar;
|
||||
}
|
||||
}
|
||||
|
||||
[$bars, $lanes] = self::stack($bars);
|
||||
$minutes = [];
|
||||
|
||||
foreach ($days as $day) {
|
||||
$minutes[$day] = $person['id'] === null ? 0 : ($minutesByUser[$person['id']][$day] ?? 0);
|
||||
}
|
||||
|
||||
$rows[] = new CalendarRow($person['id'], $person['name'], $minutes, $bars, $lanes);
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $days
|
||||
*/
|
||||
private static function bar(CalendarTask $task, ?int $userId, array $days): ?CalendarBar
|
||||
{
|
||||
if ($task->plannedFrom === null || $task->plannedTo === null || $days === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($userId === null) {
|
||||
if ($task->assignments !== []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$from = $task->plannedFrom;
|
||||
$to = $task->plannedTo;
|
||||
$minutes = 0;
|
||||
} else {
|
||||
$assignment = $task->assignmentOf($userId);
|
||||
|
||||
if ($assignment === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$from = $task->plannedFrom;
|
||||
$to = max($from, $assignment->to ?? $task->plannedTo);
|
||||
$minutes = $assignment->minutes;
|
||||
}
|
||||
|
||||
$first = $days[0];
|
||||
$last = $days[count($days) - 1];
|
||||
|
||||
if ($to < $first || $from > $last) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$start = $from < $first ? 0 : (int) array_search($from, $days, true);
|
||||
$end = $to > $last ? count($days) - 1 : (int) array_search($to, $days, true);
|
||||
$late = $task->status !== TaskStatus::Done && $task->dueOn !== null && $to > $task->dueOn;
|
||||
|
||||
return new CalendarBar($task, $userId, $from, $to, $minutes, $start, $end - $start + 1, 0, $late);
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts each bar on the first lane where it does not overlap another.
|
||||
*
|
||||
* @param list<CalendarBar> $bars
|
||||
* @return array{0: list<CalendarBar>, 1: int}
|
||||
*/
|
||||
private static function stack(array $bars): array
|
||||
{
|
||||
usort($bars, fn (CalendarBar $a, CalendarBar $b): int => [$a->start, -$a->span, $a->task->taskId] <=> [$b->start, -$b->span, $b->task->taskId]);
|
||||
|
||||
$laneEnds = [];
|
||||
$stacked = [];
|
||||
|
||||
foreach ($bars as $bar) {
|
||||
$lane = 0;
|
||||
|
||||
while (isset($laneEnds[$lane]) && $laneEnds[$lane] >= $bar->start) {
|
||||
$lane++;
|
||||
}
|
||||
|
||||
$laneEnds[$lane] = $bar->start + $bar->span - 1;
|
||||
$stacked[] = new CalendarBar($bar->task, $bar->userId, $bar->from, $bar->to, $bar->minutes, $bar->start, $bar->span, $lane, $bar->late);
|
||||
}
|
||||
|
||||
return [$stacked, max(1, count($laneEnds))];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?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));
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\TaskData;
|
||||
use App\Modules\Jobs\Application\Services\TaskPlanner;
|
||||
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
@@ -12,6 +13,7 @@ use App\Modules\Jobs\Domain\Entities\Task;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
@@ -22,6 +24,7 @@ final class AddTask
|
||||
private CustomerJobRepository $jobs,
|
||||
private TaskRepository $tasks,
|
||||
private TaskReferenceChecker $references,
|
||||
private TaskPlanner $planner,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
@@ -31,6 +34,7 @@ final class AddTask
|
||||
* @throws CustomerJobNotFound
|
||||
* @throws TasksNotAllowedYet
|
||||
* @throws InvalidTaskReference
|
||||
* @throws PlanEndsBeforeStart
|
||||
*/
|
||||
public function execute(int $jobId, TaskData $data, int $actorUserId): int
|
||||
{
|
||||
@@ -51,9 +55,17 @@ final class AddTask
|
||||
TaskInput::hours($data->estimatedHours),
|
||||
$data->assigneeUserIds,
|
||||
$actorUserId,
|
||||
TaskInput::date($data->plannedFrom),
|
||||
TaskInput::date($data->plannedTo),
|
||||
);
|
||||
|
||||
return (int) $this->tasks->save($task)->id();
|
||||
$task = $this->tasks->save($task);
|
||||
|
||||
if ($task->isScheduled() && $this->planner->plan($task)) {
|
||||
$task = $this->tasks->save($task);
|
||||
}
|
||||
|
||||
return (int) $task->id();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Catalog\Application\Contracts\WorkflowTemplateQueries;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkflowTemplateDetails;
|
||||
use App\Modules\Jobs\Application\DTOs\TemplateTaskSchedule;
|
||||
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\Task;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TasksNotAllowedYet;
|
||||
use App\Modules\Jobs\Domain\Exceptions\WorkflowTemplateNotAvailable;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Makes a task of every step of a workflow template, in the template's
|
||||
* order: waiting, with the step's work type and the due date and estimate
|
||||
* entered for it, assigned to the user who adds them. The tasks are
|
||||
* copies, so later changes to the template do not touch them.
|
||||
*/
|
||||
final class AddTasksFromWorkflowTemplate
|
||||
{
|
||||
public function __construct(
|
||||
private CustomerJobRepository $jobs,
|
||||
private TaskRepository $tasks,
|
||||
private WorkflowTemplateQueries $templates,
|
||||
private TaskReferenceChecker $references,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param list<TemplateTaskSchedule> $schedules In step order; a missing one leaves the task without a due date and estimate.
|
||||
* @return int How many tasks were added.
|
||||
*
|
||||
* @throws CustomerJobNotFound
|
||||
* @throws InvalidEstimatedHours
|
||||
* @throws TasksNotAllowedYet
|
||||
* @throws WorkflowTemplateNotAvailable
|
||||
* @throws InvalidTaskReference when a step's work type is not available for the job
|
||||
*/
|
||||
public function execute(int $jobId, int $templateId, int $actorUserId, array $schedules = []): int
|
||||
{
|
||||
$dueDates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::date($schedule->dueOn), $schedules);
|
||||
$estimates = array_map(fn (TemplateTaskSchedule $schedule) => TaskInput::hours($schedule->estimatedHours), $schedules);
|
||||
|
||||
return $this->transactions->run(function () use ($jobId, $templateId, $actorUserId, $dueDates, $estimates): int {
|
||||
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
|
||||
$template = $this->availableTemplate($templateId, $job->details()->serviceUnitIds);
|
||||
|
||||
foreach ($template->steps as $index => $step) {
|
||||
$this->references->check($job, $step->effectiveWorkTypeId, [$actorUserId]);
|
||||
|
||||
$this->tasks->save(Task::create(
|
||||
$jobId,
|
||||
$job->details()->status,
|
||||
$step->effectiveWorkTypeId,
|
||||
new TaskTitle($step->title),
|
||||
$step->description,
|
||||
TaskStatus::Waiting,
|
||||
$dueDates[$index] ?? null,
|
||||
$estimates[$index] ?? null,
|
||||
[$actorUserId],
|
||||
$actorUserId,
|
||||
));
|
||||
}
|
||||
|
||||
return count($template->steps);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $serviceUnitIds
|
||||
*/
|
||||
private function availableTemplate(int $templateId, array $serviceUnitIds): WorkflowTemplateDetails
|
||||
{
|
||||
foreach ($this->templates->activeFor($serviceUnitIds) as $template) {
|
||||
if ($template->id === $templateId) {
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
|
||||
throw new WorkflowTemplateNotAvailable($templateId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\Services\TaskPlanner;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Takes a task out of the task calendar: no planned period, no bookings.
|
||||
*/
|
||||
final class ClearTaskSchedule
|
||||
{
|
||||
public function __construct(
|
||||
private TaskRepository $tasks,
|
||||
private TaskPlanner $planner,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws TaskNotFound
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId): void
|
||||
{
|
||||
$this->transactions->run(function () use ($jobId, $taskId): void {
|
||||
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
||||
$task->schedule(null, null);
|
||||
$this->planner->plan($task);
|
||||
$this->tasks->save($task);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\ScheduleResult;
|
||||
use App\Modules\Jobs\Application\Services\TaskPlanner;
|
||||
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Moves a task in the task calendar: a new planned period, and possibly
|
||||
* from one person's row to another's (the person on the row it left is
|
||||
* replaced as an assignee; null is the "no assignee" row). Without an
|
||||
* end, the task lasts as many working days as its estimate needs.
|
||||
*/
|
||||
final class ScheduleTask
|
||||
{
|
||||
public function __construct(
|
||||
private CustomerJobRepository $jobs,
|
||||
private TaskRepository $tasks,
|
||||
private TaskReferenceChecker $references,
|
||||
private TaskPlanner $planner,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws CustomerJobNotFound
|
||||
* @throws TaskNotFound
|
||||
* @throws InvalidTaskReference
|
||||
* @throws PlanEndsBeforeStart
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId, ?int $fromUserId, ?int $toUserId, string $plannedFrom, ?string $plannedTo): ScheduleResult
|
||||
{
|
||||
$from = TaskInput::date($plannedFrom) ?? throw new PlanEndsBeforeStart;
|
||||
$to = TaskInput::date($plannedTo);
|
||||
|
||||
return $this->transactions->run(function () use ($jobId, $taskId, $fromUserId, $toUserId, $from, $to): ScheduleResult {
|
||||
$job = $this->jobs->findById($jobId) ?? throw new CustomerJobNotFound($jobId);
|
||||
$task = $this->tasks->findForJob($jobId, $taskId) ?? throw new TaskNotFound($jobId, $taskId);
|
||||
|
||||
$assignees = $task->assigneeUserIds();
|
||||
|
||||
if ($fromUserId !== $toUserId) {
|
||||
$assignees = array_values(array_filter($assignees, fn (int $userId): bool => $userId !== $fromUserId));
|
||||
|
||||
if ($toUserId !== null) {
|
||||
$assignees[] = $toUserId;
|
||||
}
|
||||
}
|
||||
|
||||
$this->references->check($job, $task->workTypeId(), $assignees, $task);
|
||||
|
||||
$task->changeAssignees($assignees);
|
||||
$task->schedule($from, $to ?? $this->planner->defaultEnd($from, $task));
|
||||
|
||||
$task = $this->tasks->save($task);
|
||||
$extended = $this->planner->plan($task);
|
||||
$task = $this->tasks->save($task);
|
||||
|
||||
return new ScheduleResult(
|
||||
(string) $task->plannedFrom()?->format('Y-m-d'),
|
||||
(string) $task->plannedTo()?->format('Y-m-d'),
|
||||
$extended,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,15 @@ declare(strict_types=1);
|
||||
namespace App\Modules\Jobs\Application\UseCases;
|
||||
|
||||
use App\Modules\Jobs\Application\DTOs\TaskData;
|
||||
use App\Modules\Jobs\Application\Services\TaskPlanner;
|
||||
use App\Modules\Jobs\Application\Services\TaskReferenceChecker;
|
||||
use App\Modules\Jobs\Domain\Contracts\CustomerJobRepository;
|
||||
use App\Modules\Jobs\Domain\Contracts\TaskRepository;
|
||||
use App\Modules\Jobs\Domain\Entities\Task;
|
||||
use App\Modules\Jobs\Domain\Enums\TaskStatus;
|
||||
use App\Modules\Jobs\Domain\Exceptions\CustomerJobNotFound;
|
||||
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskReference;
|
||||
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
|
||||
use App\Modules\Jobs\Domain\Exceptions\TaskNotFound;
|
||||
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
@@ -21,6 +24,7 @@ final class UpdateTask
|
||||
private CustomerJobRepository $jobs,
|
||||
private TaskRepository $tasks,
|
||||
private TaskReferenceChecker $references,
|
||||
private TaskPlanner $planner,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
@@ -28,6 +32,7 @@ final class UpdateTask
|
||||
* @throws CustomerJobNotFound
|
||||
* @throws TaskNotFound
|
||||
* @throws InvalidTaskReference
|
||||
* @throws PlanEndsBeforeStart
|
||||
*/
|
||||
public function execute(int $jobId, int $taskId, TaskData $data): void
|
||||
{
|
||||
@@ -39,6 +44,8 @@ final class UpdateTask
|
||||
|
||||
$this->references->check($job, $data->workTypeId, $data->assigneeUserIds, $task);
|
||||
|
||||
$before = self::planningInput($task);
|
||||
|
||||
$task->changeDetails(
|
||||
$data->workTypeId,
|
||||
$title,
|
||||
@@ -48,8 +55,28 @@ final class UpdateTask
|
||||
TaskInput::hours($data->estimatedHours),
|
||||
$data->assigneeUserIds,
|
||||
);
|
||||
$task->schedule(TaskInput::date($data->plannedFrom), TaskInput::date($data->plannedTo));
|
||||
|
||||
$this->tasks->save($task);
|
||||
$task = $this->tasks->save($task);
|
||||
|
||||
// Rebook only when what the bookings depend on changed.
|
||||
if (self::planningInput($task) !== $before) {
|
||||
$this->planner->plan($task);
|
||||
$this->tasks->save($task);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: ?string, 1: ?string, 2: ?float, 3: list<int>}
|
||||
*/
|
||||
private static function planningInput(Task $task): array
|
||||
{
|
||||
return [
|
||||
$task->plannedFrom()?->format('Y-m-d'),
|
||||
$task->plannedTo()?->format('Y-m-d'),
|
||||
$task->estimatedHours()?->value,
|
||||
$task->assigneeUserIds(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user