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:
2026-11-24 18:13:11 +02:00
co-authored by Claude Opus 5.5
parent dfe4941f02
commit b4f2f1cfaa
134 changed files with 6930 additions and 45 deletions
@@ -13,7 +13,7 @@ use Illuminate\Database\Eloquent\Model;
/**
* Persistence model of a task, used only inside the Jobs Infrastructure
* layer. Assignees are in task_assignees.
* layer. Assignees are in task_assignees, day bookings in task_bookings.
*
* @property int $id
* @property int $customer_job_id
@@ -22,11 +22,13 @@ use Illuminate\Database\Eloquent\Model;
* @property string|null $description
* @property TaskStatus $status
* @property CarbonImmutable|null $due_on
* @property CarbonImmutable|null $planned_from
* @property CarbonImmutable|null $planned_to
* @property string|null $estimated_hours
* @property int $created_by
*/
#[Table('tasks')]
#[Fillable(['customer_job_id', 'work_type_id', 'title', 'description', 'status', 'due_on', 'estimated_hours', 'created_by'])]
#[Fillable(['customer_job_id', 'work_type_id', 'title', 'description', 'status', 'due_on', 'planned_from', 'planned_to', 'estimated_hours', 'created_by'])]
#[UseFactory(TaskFactory::class)]
class TaskModel extends Model
{
@@ -43,6 +45,8 @@ class TaskModel extends Model
'work_type_id' => 'integer',
'status' => TaskStatus::class,
'due_on' => 'immutable_date',
'planned_from' => 'immutable_date',
'planned_to' => 'immutable_date',
'created_by' => 'integer',
];
}
@@ -0,0 +1,50 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Domain\Contracts\TaskBookingRepository;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use Illuminate\Support\Facades\DB;
class EloquentTaskBookingRepository implements TaskBookingRepository
{
public function replaceFor(int $taskId, int $userId, array $minutesByDay): void
{
DB::table('task_bookings')->where('task_id', $taskId)->where('user_id', $userId)->delete();
$rows = [];
foreach ($minutesByDay as $day => $minutes) {
if ($minutes > 0) {
$rows[] = ['task_id' => $taskId, 'user_id' => $userId, 'day' => $day, 'minutes' => $minutes];
}
}
if ($rows !== []) {
DB::table('task_bookings')->insert($rows);
}
}
public function deleteForTask(int $taskId, array $keepUserIds = []): void
{
DB::table('task_bookings')
->where('task_id', $taskId)
->when($keepUserIds !== [], fn ($query) => $query->whereNotIn('user_id', $keepUserIds))
->delete();
}
public function otherOpenMinutes(int $userId, string $from, string $to, int $exceptTaskId): array
{
return DB::table('task_bookings')
->join('tasks', 'tasks.id', '=', 'task_bookings.task_id')
->where('task_bookings.user_id', $userId)
->where('task_bookings.task_id', '!=', $exceptTaskId)
->where('tasks.status', '!=', TaskStatus::Done->value)
->whereBetween('task_bookings.day', [$from, $to])
->groupBy('task_bookings.day')
->selectRaw('task_bookings.day as day, SUM(task_bookings.minutes) as minutes')
->pluck('minutes', 'day')
->mapWithKeys(fn ($minutes, $day): array => [substr((string) $day, 0, 10) => (int) $minutes])
->all();
}
}
@@ -0,0 +1,132 @@
<?php
namespace App\Modules\Jobs\Infrastructure\Persistence\Repositories;
use App\Modules\Jobs\Application\Contracts\TaskCalendarQueries;
use App\Modules\Jobs\Application\DTOs\CalendarAssignment;
use App\Modules\Jobs\Application\DTOs\CalendarTask;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Jobs\Infrastructure\Persistence\JobInvolvement;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;
class EloquentTaskCalendarQueries implements TaskCalendarQueries
{
public function scheduledTasks(string $from, string $to, ?int $serviceUnitId = null, ?int $onlyAssigneeUserId = null): array
{
$query = $this->tasks($serviceUnitId, $onlyAssigneeUserId)
->whereNotNull('tasks.planned_from')
->where('tasks.planned_from', '<=', $to)
->where('tasks.planned_to', '>=', $from)
->orderBy('tasks.planned_from')
->orderBy('tasks.id');
return $this->calendarTasks($query);
}
public function unscheduledOpenTasks(?int $serviceUnitId = null, ?int $onlyAssigneeUserId = null, int $limit = 100): array
{
$query = $this->tasks($serviceUnitId, $onlyAssigneeUserId)
->whereNull('tasks.planned_from')
->where('tasks.status', '!=', TaskStatus::Done->value)
->where('customer_jobs.status', '!=', JobStatus::Archived->value)
->orderByRaw('tasks.due_on IS NULL')
->orderBy('tasks.due_on')
->orderBy('tasks.id')
->limit($limit);
return $this->calendarTasks($query);
}
public function openMinutesByUser(array $userIds, string $from, string $to): array
{
if ($userIds === []) {
return [];
}
$minutes = [];
DB::table('task_bookings')
->join('tasks', 'tasks.id', '=', 'task_bookings.task_id')
->whereIn('task_bookings.user_id', $userIds)
->where('tasks.status', '!=', TaskStatus::Done->value)
->whereBetween('task_bookings.day', [$from, $to])
->groupBy('task_bookings.user_id', 'task_bookings.day')
->selectRaw('task_bookings.user_id as user_id, task_bookings.day as day, SUM(task_bookings.minutes) as minutes')
->get()
->each(function ($row) use (&$minutes): void {
$minutes[(int) $row->user_id][substr((string) $row->day, 0, 10)] = (int) $row->minutes;
});
return $minutes;
}
private function tasks(?int $serviceUnitId, ?int $onlyAssigneeUserId): Builder
{
$query = DB::table('tasks')->join('customer_jobs', 'customer_jobs.id', '=', 'tasks.customer_job_id');
if ($serviceUnitId !== null) {
$query->whereIn('customer_jobs.id', DB::table('customer_job_service_unit')->select('customer_job_id')->where('service_unit_id', $serviceUnitId));
}
if ($onlyAssigneeUserId !== null) {
JobInvolvement::restrict($query, $onlyAssigneeUserId);
$query->whereIn('tasks.id', DB::table('task_assignees')->select('task_id')->where('user_id', $onlyAssigneeUserId));
}
return $query;
}
/**
* @return list<CalendarTask>
*/
private function calendarTasks(Builder $query): array
{
$rows = $query->get([
'tasks.id', 'tasks.customer_job_id', 'tasks.title', 'tasks.status', 'tasks.due_on', 'tasks.estimated_hours',
'tasks.planned_from', 'tasks.planned_to', 'customer_jobs.number', 'customer_jobs.name',
]);
$taskIds = $rows->pluck('id')->map(fn ($id): int => (int) $id)->all();
if ($taskIds === []) {
return [];
}
$assignees = DB::table('task_assignees')->whereIn('task_id', $taskIds)->orderBy('user_id')->get()->groupBy('task_id');
$bookings = DB::table('task_bookings')
->whereIn('task_id', $taskIds)
->groupBy('task_id', 'user_id')
->selectRaw('task_id, user_id, MIN(day) as first_day, MAX(day) as last_day, SUM(minutes) as minutes')
->get()
->keyBy(fn ($row): string => $row->task_id.'-'.$row->user_id);
return $rows->map(fn ($row): CalendarTask => new CalendarTask(
(int) $row->id,
(int) $row->customer_job_id,
(string) $row->number,
(string) $row->name,
(string) $row->title,
TaskStatus::from((string) $row->status),
self::date($row->due_on),
$row->estimated_hours === null ? null : (string) (float) $row->estimated_hours,
self::date($row->planned_from),
self::date($row->planned_to),
($assignees->get($row->id) ?? collect())->map(function ($assignee) use ($bookings, $row): CalendarAssignment {
$booking = $bookings->get($row->id.'-'.$assignee->user_id);
return new CalendarAssignment(
(int) $assignee->user_id,
self::date($booking?->first_day),
self::date($booking?->last_day),
(int) ($booking->minutes ?? 0),
);
})->values()->all(),
))->values()->all();
}
private static function date(mixed $value): ?string
{
return $value === null ? null : substr((string) $value, 0, 10);
}
}
@@ -164,6 +164,8 @@ class EloquentTaskQueries implements TaskQueries
->sortBy('name')
->values()
->all(),
plannedFrom: $model->planned_from?->format('Y-m-d'),
plannedTo: $model->planned_to?->format('Y-m-d'),
))->values()->all();
}
}
@@ -7,6 +7,7 @@ use App\Modules\Jobs\Domain\Entities\Task;
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
use App\Modules\Jobs\Domain\ValueObjects\TaskTitle;
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
use Carbon\CarbonImmutable;
use DateTimeImmutable;
use Illuminate\Support\Facades\DB;
@@ -37,6 +38,8 @@ class EloquentTaskRepository implements TaskRepository
'description' => $task->description(),
'status' => $task->status(),
'due_on' => $task->dueOn()?->format('Y-m-d'),
'planned_from' => $task->plannedFrom()?->format('Y-m-d'),
'planned_to' => $task->plannedTo()?->format('Y-m-d'),
'estimated_hours' => $task->estimatedHours()?->value,
'created_by' => $task->createdBy(),
])->save();
@@ -64,10 +67,17 @@ class EloquentTaskRepository implements TaskRepository
new TaskTitle($model->title),
$model->description,
$model->status,
$model->due_on === null ? null : (DateTimeImmutable::createFromFormat('!Y-m-d', $model->due_on->format('Y-m-d')) ?: null),
self::date($model->due_on),
$model->estimated_hours === null ? null : new EstimatedHours((float) $model->estimated_hours),
DB::table('task_assignees')->where('task_id', $model->id)->pluck('user_id')->map(fn ($id): int => (int) $id)->all(),
$model->created_by,
self::date($model->planned_from),
self::date($model->planned_to),
);
}
private static function date(?CarbonImmutable $value): ?DateTimeImmutable
{
return $value === null ? null : (DateTimeImmutable::createFromFormat('!Y-m-d', $value->format('Y-m-d')) ?: null);
}
}