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]>
133 lines
5.3 KiB
PHP
133 lines
5.3 KiB
PHP
<?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);
|
|
}
|
|
}
|