Kanban / timesheet update

This commit is contained in:
2026-11-30 06:56:14 +02:00
parent 6332ff0fa9
commit a4dbe8ce09
46 changed files with 8091 additions and 331 deletions
@@ -6,7 +6,9 @@ namespace App\Modules\TimeTracking\Application\Contracts;
use App\Modules\TimeTracking\Application\DTOs\ApprovalWeek;
use App\Modules\TimeTracking\Application\DTOs\JobBillingHours;
use App\Modules\TimeTracking\Application\DTOs\MonthSummary;
use App\Modules\TimeTracking\Application\DTOs\TimeEntrySummary;
use App\Modules\TimeTracking\Application\DTOs\TimesheetWeek;
use App\Modules\TimeTracking\Application\DTOs\WeekSheet;
interface TimeEntryQueries
@@ -19,6 +21,19 @@ interface TimeEntryQueries
*/
public function week(int $userId, string $monday, array $extraTaskIds = []): WeekSheet;
/**
* The time sheet of a week: scheduled and pinned tasks with their
* entries, grouped by customer and job (docs/ui-timesheet.md 4).
*
* @param string $monday Y-m-d
*/
public function timesheet(int $userId, string $monday): TimesheetWeek;
/**
* The user's hours in a month (Y-m), billable against internal.
*/
public function monthSummary(int $userId, string $month): MonthSummary;
/**
* The user's entries on a task, newest day first.
*
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
/**
* An unapproved entry of the week without a billing note.
*/
final readonly class MissingBillingNote
{
public function __construct(
public int $taskId,
public string $day,
public int $quarters,
public string $customerName,
public string $jobNumber,
public string $taskTitle,
) {}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
/**
* A user's hours in a month: in all, billable or within a fixed price, and
* internal (not billable), by each entry's laskutusperuste.
*/
final readonly class MonthSummary
{
public function __construct(
public string $month,
public int $totalQuarters,
public int $billableQuarters,
public int $internalQuarters,
) {}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
use App\Modules\TimeTracking\Domain\Enums\EntryBillingModel;
/**
* The user's entry on one task and day of the time sheet.
*/
final readonly class TimesheetCell
{
public function __construct(
public int $quarters,
public ?string $billingNote,
public ?string $internalComment,
public ?EntryBillingModel $billingModel,
public bool $approved,
) {}
public function hasBillingNote(): bool
{
return $this->billingNote !== null && trim($this->billingNote) !== '';
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
/**
* A customer on the time sheet (level 1), in its own colour, with its jobs.
*/
final readonly class TimesheetCustomerGroup
{
/**
* @param list<TimesheetJobGroup> $jobs
*/
public function __construct(
public int $customerId,
public string $name,
public ?string $headerColor,
public array $jobs,
) {}
public function taskCount(): int
{
return array_sum(array_map(fn (TimesheetJobGroup $job): int => count($job->rows), $this->jobs));
}
}
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\TimeTracking\Domain\Enums\EntryBillingModel;
/**
* A customer job on the time sheet (level 2) with its task rows.
*/
final readonly class TimesheetJobGroup
{
/**
* @param list<TimesheetTaskRow> $rows
*/
public function __construct(
public int $jobId,
public string $number,
public string $name,
public JobStatus $status,
public ?string $paymentBasis,
public int $accumulatedQuarters,
public array $rows,
) {}
public function billingModel(): EntryBillingModel
{
return EntryBillingModel::forPaymentBasis($this->paymentBasis);
}
public function isContinuing(): bool
{
return $this->status === JobStatus::Ongoing;
}
}
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
use App\Modules\Jobs\Application\DTOs\BookableTask;
use App\Modules\TimeTracking\Domain\Enums\EntryBillingModel;
/**
* A task row of the time sheet (level 3): its entries by day.
*/
final readonly class TimesheetTaskRow
{
/**
* @param array<string, TimesheetCell> $cells by Y-m-d
*/
public function __construct(
public BookableTask $task,
public array $cells,
public int $totalQuarters,
public bool $pinned,
) {}
public function cell(string $day): ?TimesheetCell
{
return $this->cells[$day] ?? null;
}
/**
* The laskutusperuste new entries get: from the job's payment basis.
*/
public function defaultBillingModel(): EntryBillingModel
{
return EntryBillingModel::forPaymentBasis($this->task->paymentBasis);
}
}
@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\DTOs;
/**
* One user's time sheet for a week (docs/ui-timesheet.md 4): the
* "Aikataulutettu" part (tasks scheduled for the week and tasks with hours
* that are not pinned) and the "Lukitut työt" part (pinned tasks that can
* still take hours, or have hours this week), both grouped customer A–Ö →
* job → task.
*/
final readonly class TimesheetWeek
{
/**
* @param list<string> $days Y-m-d, Monday to Sunday
* @param list<TimesheetCustomerGroup> $scheduled
* @param list<TimesheetCustomerGroup> $pinned
* @param array<string, int> $dayTotals
* @param list<MissingBillingNote> $missingNotes
*/
public function __construct(
public array $days,
public array $scheduled,
public array $pinned,
public array $dayTotals,
public int $totalQuarters,
public int $unapprovedQuarters,
public ?WeekApproval $lastApproval,
public array $missingNotes,
) {}
public function isApproved(): bool
{
return $this->totalQuarters > 0 && $this->unapprovedQuarters === 0;
}
/**
* @return list<TimesheetTaskRow>
*/
public function rows(): array
{
$rows = [];
foreach ([...$this->scheduled, ...$this->pinned] as $customer) {
foreach ($customer->jobs as $job) {
foreach ($job->rows as $row) {
$rows[] = $row;
}
}
}
return $rows;
}
public function row(int $taskId): ?TimesheetTaskRow
{
foreach ($this->rows() as $row) {
if ($row->task->taskId === $taskId) {
return $row;
}
}
return null;
}
public function scheduledTaskCount(): int
{
return array_sum(array_map(fn (TimesheetCustomerGroup $customer): int => $customer->taskCount(), $this->scheduled));
}
public function pinnedTaskCount(): int
{
return array_sum(array_map(fn (TimesheetCustomerGroup $customer): int => $customer->taskCount(), $this->pinned));
}
}
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\Services;
use App\Modules\TimeTracking\Application\DTOs\TimesheetCustomerGroup;
use App\Modules\TimeTracking\Application\DTOs\TimesheetJobGroup;
use App\Modules\TimeTracking\Application\DTOs\TimesheetTaskRow;
use Collator;
/**
* Splits a week's task rows into the time sheet's two parts and groups each
* customer A–Ö (Finnish order) → job by number → task by title
* (docs/ui-timesheet.md 4):
*
* - "Lukitut työt": pinned tasks that can still take hours, or have hours
* this week; a pin of a done or invoiced job or task thus lapses on later
* weeks but stays on the weeks with hours;
* - "Aikataulutettu": tasks scheduled for the week or with hours this week
* that are not in the pinned part.
*/
final class TimesheetLayout
{
/**
* @param list<TimesheetTaskRow> $rows every candidate row, pinned or not
* @param list<int> $scheduledTaskIds
* @param array<int, int> $accumulatedQuarters by job id
* @return array{scheduled: list<TimesheetCustomerGroup>, pinned: list<TimesheetCustomerGroup>}
*/
public function arrange(array $rows, array $scheduledTaskIds, array $accumulatedQuarters): array
{
$scheduled = [];
$pinned = [];
foreach ($rows as $row) {
$open = ! $row->task->isLocked() && $row->task->taskStatus->value !== 'done';
if ($row->pinned && ($open || $row->totalQuarters > 0)) {
$pinned[] = $row;
} elseif ($row->totalQuarters > 0 || ($open && in_array($row->task->taskId, $scheduledTaskIds, true))) {
$scheduled[] = $row;
}
}
return [
'scheduled' => $this->grouped($scheduled, $accumulatedQuarters),
'pinned' => $this->grouped($pinned, $accumulatedQuarters),
];
}
/**
* @param list<TimesheetTaskRow> $rows
* @param array<int, int> $accumulatedQuarters
* @return list<TimesheetCustomerGroup>
*/
private function grouped(array $rows, array $accumulatedQuarters): array
{
$collator = class_exists(Collator::class) ? new Collator('fi_FI') : null;
$compare = fn (string $a, string $b): int => $collator?->compare($a, $b) ?: strcmp(mb_strtolower($a), mb_strtolower($b));
usort($rows, fn (TimesheetTaskRow $a, TimesheetTaskRow $b): int => $compare($a->task->customerName, $b->task->customerName)
?: ($a->task->customerId <=> $b->task->customerId)
?: strnatcmp($a->task->jobNumber, $b->task->jobNumber)
?: $compare($a->task->taskTitle, $b->task->taskTitle)
?: ($a->task->taskId <=> $b->task->taskId));
$customers = [];
foreach ($rows as $row) {
$customers[$row->task->customerId]['name'] = $row->task->customerName;
$customers[$row->task->customerId]['color'] = $row->task->customerHeaderColor;
$customers[$row->task->customerId]['jobs'][$row->task->jobId][] = $row;
}
$groups = [];
foreach ($customers as $customerId => $customer) {
$jobs = [];
foreach ($customer['jobs'] as $jobId => $jobRows) {
$task = $jobRows[0]->task;
$jobs[] = new TimesheetJobGroup($jobId, $task->jobNumber, $task->jobName, $task->jobStatus, $task->paymentBasis, $accumulatedQuarters[$jobId] ?? 0, $jobRows);
}
$groups[] = new TimesheetCustomerGroup($customerId, $customer['name'], $customer['color'], $jobs);
}
return $groups;
}
}
@@ -7,6 +7,7 @@ namespace App\Modules\TimeTracking\Application\UseCases;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\TimeTracking\Application\Services\TimeBooking;
use App\Modules\TimeTracking\Domain\Contracts\TimeEntryRepository;
use App\Modules\TimeTracking\Domain\Exceptions\BillingNotesMissing;
use App\Modules\Users\Application\Contracts\Clock;
/**
@@ -24,6 +25,8 @@ final class ApproveWeek
/**
* @param string $monday Y-m-d of the week's Monday.
* @return int How many entries were approved now.
*
* @throws BillingNotesMissing when an unapproved entry has no billing note
*/
public function execute(int $userId, string $monday, int $approverId): int
{
@@ -32,8 +35,20 @@ final class ApproveWeek
return $this->transactions->run(function () use ($userId, $start, $approverId, $now): int {
$approved = 0;
$entries = $this->entries->forUserBetween($userId, $start, $start->modify('+6 days'));
$missing = [];
foreach ($this->entries->forUserBetween($userId, $start, $start->modify('+6 days')) as $entry) {
foreach ($entries as $entry) {
if (! $entry->isApproved() && ! $entry->hasBillingNote()) {
$missing[] = (int) $entry->id();
}
}
if ($missing !== []) {
throw new BillingNotesMissing($missing);
}
foreach ($entries as $entry) {
if ($entry->isApproved()) {
continue;
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\UseCases;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\TimeTracking\Application\Services\TimeBooking;
use App\Modules\TimeTracking\Domain\Contracts\TimeEntryRepository;
use App\Modules\TimeTracking\Domain\Enums\EntryBillingModel;
use App\Modules\TimeTracking\Domain\Exceptions\TimeApproved;
use App\Modules\TimeTracking\Domain\Exceptions\TimeEntryNotFound;
/**
* "Muokkaa kirjausta" on the time sheet: the laskutusperuste, billing note
* and internal comment of the user's entry on a task and day.
*/
final class DescribeTimeEntry
{
public function __construct(
private TimeEntryRepository $entries,
private TransactionManager $transactions,
) {}
/**
* @throws TimeEntryNotFound
* @throws TimeApproved
*/
public function execute(int $userId, int $taskId, string $day, ?EntryBillingModel $billingModel, ?string $billingNote, ?string $internalComment): void
{
$date = TimeBooking::day($day);
$this->transactions->run(function () use ($userId, $taskId, $date, $billingModel, $billingNote, $internalComment): void {
$entry = $this->entries->findForDay($userId, $taskId, $date) ?? throw new TimeEntryNotFound(0);
$entry->describe($billingModel, $billingNote, $internalComment);
$this->entries->save($entry);
});
}
}
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Application\UseCases;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\TimeTracking\Domain\Contracts\PinnedTaskRepository;
use App\Modules\TimeTracking\Domain\Exceptions\TaskNotBookable;
use App\Modules\TimeTracking\Domain\Exceptions\TimeLocked;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Pins tasks to the "Lukitut työt" part of a user's time sheet (★), or
* unpins them. Only tasks that can take hours (open task of an unlocked
* job) can be pinned; unpinning always works.
*/
final class PinTimesheetTasks
{
public function __construct(
private PinnedTaskRepository $pins,
private TaskQueries $tasks,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @param list<int> $taskIds
*
* @throws TaskNotBookable
* @throws TimeLocked
*/
public function pin(int $userId, array $taskIds): void
{
$now = $this->clock->now();
$this->transactions->run(function () use ($userId, $taskIds, $now): void {
foreach ($taskIds as $taskId) {
$task = $this->tasks->bookableTask($taskId) ?? throw new TaskNotBookable($taskId);
if ($task->isLocked()) {
throw new TimeLocked;
}
$this->pins->pin($userId, $taskId, $now);
}
});
}
public function unpin(int $userId, int $taskId): void
{
$this->transactions->run(fn () => $this->pins->unpin($userId, $taskId));
}
/**
* "Poista kaikki kiinnitykset".
*/
public function unpinAll(int $userId): int
{
return $this->transactions->run(fn (): int => $this->pins->unpinAll($userId));
}
}