Files
Toiminnanohjaus/app/Modules/Dashboard/Application/UseCases/ManagementOverviewQuery.php
T
petriandClaude Sonnet 5.5 487a7d34ba Replace the demo home page with a dashboard
Everyone sees their open tasks on an agenda, their open jobs, this
week's hours and quick links. Admin and management also get a lazily
loaded management panel: things to do, the month so far against the
previous month, jobs and late work, and everyone's workload.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-25 09:20:02 +02:00

99 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Dashboard\Application\UseCases;
use App\Modules\Dashboard\Application\Contracts\DashboardAdmin;
use App\Modules\Dashboard\Application\Contracts\DashboardFigures;
use App\Modules\Dashboard\Application\Contracts\DashboardTime;
use App\Modules\Dashboard\Application\Contracts\DashboardWork;
use App\Modules\Dashboard\Application\DTOs\ManagementOverview;
use App\Modules\Dashboard\Application\DTOs\PersonLoad;
use App\Modules\Dashboard\Application\DTOs\WorkTask;
use App\Modules\Dashboard\Domain\ValueObjects\Workload;
use DateTimeImmutable;
/**
* The management panel of the home page for admin and management on a day.
* The month so far is compared with the same days of the previous month
* (to its end when it is shorter).
*/
final readonly class ManagementOverviewQuery
{
public const int OVERDUE_TASK_LIMIT = 5;
public function __construct(
private DashboardWork $work,
private DashboardTime $time,
private DashboardAdmin $admin,
private DashboardFigures $figures,
) {}
public function execute(DateTimeImmutable $today): ManagementOverview
{
$day = $today->format('Y-m-d');
$monthFrom = $today->format('Y-m-01');
$previousStart = $today->modify('first day of previous month');
$previousFrom = $previousStart->format('Y-m-d');
$previousTo = $previousStart->format('Y-m-').sprintf('%02d', min((int) $today->format('j'), (int) $previousStart->format('t')));
$jobCounts = $this->work->jobCountsByStatus();
$thisMonday = $today->modify('monday this week');
$nextMonday = $thisMonday->modify('+7 days');
return new ManagementOverview(
$this->time->pendingApprovals(),
$jobCounts['done'] ?? 0,
$this->admin->openDraftId(),
$this->admin->unclosedCashDaysBefore($day),
$this->figures->unbilledValueCents($day),
$monthFrom,
$day,
$previousFrom,
$previousTo,
$this->figures->between($monthFrom, $day),
$this->figures->between($previousFrom, $previousTo),
$jobCounts,
$this->work->overdueJobCount($day),
$this->work->overdueTaskCount($day),
array_map(fn (WorkTask $task): WorkTask => $task->late(), $this->work->oldestOverdueTasks($day, self::OVERDUE_TASK_LIMIT)),
$thisMonday->format('Y-m-d'),
$nextMonday->format('Y-m-d'),
$this->workloads($thisMonday, $nextMonday),
);
}
/**
* @return list<PersonLoad>
*/
private function workloads(DateTimeImmutable $thisMonday, DateTimeImmutable $nextMonday): array
{
$staff = $this->admin->staff();
$daily = $this->work->dailyCapacityMinutes();
$weeks = [];
foreach ([$thisMonday, $nextMonday] as $monday) {
$from = $monday->format('Y-m-d');
$to = $monday->modify('+6 days')->format('Y-m-d');
$weeks[] = [
'booked' => $this->work->bookedMinutes(array_keys($staff), $from, $to),
'days' => Workload::workingDays($from, $to, $this->admin->holidaysBetween($from, $to)),
];
}
$result = [];
foreach ($staff as $userId => $name) {
$result[] = new PersonLoad(
$userId,
$name,
Workload::of($weeks[0]['booked'][$userId] ?? 0, $daily, $weeks[0]['days']),
Workload::of($weeks[1]['booked'][$userId] ?? 0, $daily, $weeks[1]['days']),
);
}
return $result;
}
}