Add time tracking on tasks with a weekly sheet

Phase 1, item 8 (last of phase 1). New TimeTracking module:
- One entry per user, task and day (0.25-24 h in quarters, with a note);
  the job and work type are copied from the task. A user's day cannot
  exceed 24 h, and hours on a done, invoiced, billed or archived job
  cannot be added or changed.
- Weekly sheet /time: tasks as rows (own open tasks, tasks with hours
  that week, tasks added by search), days as columns, row, day and week
  totals, week navigation. A cell saves on change and an empty cell
  removes the entry; on phones one day at a time. Saves and errors are
  announced, errors are tied to their cell.
- Task booking page /time/tasks/{task}: date, hours and note, added to
  the day's entry; own entries with edit and remove.
- Anyone but subcontractors may book on any unlocked task;
  subcontractors only on jobs they are attached to.

Jobs module:
- JobStatus::locksTime(), BookableTask and task queries for booking.
- A task with time entries cannot be removed (TaskUsage contract,
  provided by TimeTracking); the job page links tasks to booking.
- The "involved in a job" rule is shared in JobInvolvement.

Spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 05:52:45 +02:00
co-authored by Claude Opus 5.5
parent 02a62eb16e
commit 3922c744a7
53 changed files with 2496 additions and 19 deletions
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Contracts;
use App\Modules\TimeTracking\Domain\Entities\TimeEntry;
use DateTimeImmutable;
interface TimeEntryRepository
{
public function findForDay(int $userId, int $taskId, DateTimeImmutable $day): ?TimeEntry;
/**
* Null when the entry does not exist or belongs to another user.
*/
public function findForUser(int $userId, int $entryId): ?TimeEntry;
public function save(TimeEntry $entry): TimeEntry;
public function delete(TimeEntry $entry): void;
/**
* The user's recorded quarters of an hour on the day, all tasks.
*/
public function quartersOn(int $userId, DateTimeImmutable $day, ?int $exceptEntryId = null): int;
}
@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Entities;
use App\Modules\TimeTracking\Domain\ValueObjects\WorkedHours;
use DateTimeImmutable;
/**
* A user's hours on one task on one day (one entry per day). The job and
* work type are copied from the task when the entry is made, for billing.
*/
final class TimeEntry
{
private function __construct(
private ?int $id,
private int $userId,
private int $taskId,
private int $jobId,
private int $workTypeId,
private DateTimeImmutable $workedOn,
private WorkedHours $hours,
private ?string $note,
) {}
public static function create(int $userId, int $taskId, int $jobId, int $workTypeId, DateTimeImmutable $workedOn, WorkedHours $hours, ?string $note = null): self
{
return new self(null, $userId, $taskId, $jobId, $workTypeId, $workedOn, $hours, self::text($note));
}
public static function reconstitute(int $id, int $userId, int $taskId, int $jobId, int $workTypeId, DateTimeImmutable $workedOn, WorkedHours $hours, ?string $note): self
{
return new self($id, $userId, $taskId, $jobId, $workTypeId, $workedOn, $hours, $note);
}
public function changeHours(WorkedHours $hours, ?string $note): void
{
$this->hours = $hours;
$this->note = self::text($note);
}
/**
* Adds more hours to the day's entry; a new note continues the old one.
*/
public function addHours(WorkedHours $hours, ?string $note): void
{
$this->hours = $this->hours->plus($hours);
$note = self::text($note);
if ($note !== null) {
$this->note = $this->note === null ? $note : $this->note."\n".$note;
}
}
public function id(): ?int
{
return $this->id;
}
public function userId(): int
{
return $this->userId;
}
public function taskId(): int
{
return $this->taskId;
}
public function jobId(): int
{
return $this->jobId;
}
public function workTypeId(): int
{
return $this->workTypeId;
}
public function workedOn(): DateTimeImmutable
{
return $this->workedOn;
}
public function hours(): WorkedHours
{
return $this->hours;
}
public function note(): ?string
{
return $this->note;
}
private static function text(?string $value): ?string
{
$value = $value === null ? '' : trim($value);
return $value === '' ? null : $value;
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class DailyHoursExceeded extends DomainException
{
public function __construct()
{
parent::__construct('The hours of one day cannot exceed 24.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidWorkedHours extends DomainException
{
public function __construct()
{
parent::__construct('Hours must be 0.25–24, in quarters of an hour.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class TaskNotBookable extends DomainException
{
public function __construct(public readonly int $taskId)
{
parent::__construct("Task {$taskId} was not found.");
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* The entry does not exist or belongs to another user.
*/
final class TimeEntryNotFound extends DomainException
{
public function __construct(public readonly int $entryId)
{
parent::__construct("Time entry {$entryId} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class TimeLocked extends DomainException
{
public function __construct()
{
parent::__construct('The job is done, so its hours are locked.');
}
}
@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\ValueObjects;
use App\Modules\TimeTracking\Domain\Exceptions\InvalidWorkedHours;
/**
* Hours worked on one day, 0.25–24 to the nearest quarter of an hour.
* Stored as quarters so sums stay exact.
*/
final readonly class WorkedHours
{
public const int MAX_QUARTERS = 96;
public int $quarters;
public function __construct(int $quarters)
{
if ($quarters < 1 || $quarters > self::MAX_QUARTERS) {
throw new InvalidWorkedHours;
}
$this->quarters = $quarters;
}
/**
* Parses "2,5", "2.5" or "2"; null for an empty value.
*
* @throws InvalidWorkedHours
*/
public static function parse(string $value): ?self
{
$value = str_replace(',', '.', trim($value));
if ($value === '') {
return null;
}
if (! is_numeric($value)) {
throw new InvalidWorkedHours;
}
$quarters = (float) $value * 4;
if (abs($quarters - round($quarters)) > 1e-9) {
throw new InvalidWorkedHours;
}
return new self((int) round($quarters));
}
public static function fromDecimal(float $hours): self
{
return new self((int) round($hours * 4));
}
public function decimal(): float
{
return $this->quarters / 4;
}
public function plus(self $other): self
{
return new self($this->quarters + $other->quarters);
}
/**
* Finnish notation without trailing zeros, e.g. "7,5" or "8".
*/
public function format(): string
{
return self::formatQuarters($this->quarters);
}
public static function formatQuarters(int $quarters): string
{
return str_replace('.', ',', rtrim(rtrim(number_format($quarters / 4, 2, '.', ''), '0'), '.'));
}
}