- Time entries store who approved them and when. Approved hours are locked: cells, task page edits and removals are refused. - "Hyväksy viikko" in the weekly view approves the user's own week; subcontractors' hours are approved by management. - Management gets an approval page (unapproved weeks by person, recently approved weeks) and can open anyone's week read-only to approve it or cancel its approval, which only management may do. - Only management may move a done job back to an earlier status, which opens the lock on its hours and expenses; the history marks it. - The Clock contract gets now() for approval times. Co-Authored-By: Claude Opus 5.5 <[email protected]>
156 lines
3.8 KiB
PHP
156 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\TimeTracking\Domain\Entities;
|
|
|
|
use App\Modules\TimeTracking\Domain\Exceptions\TimeApproved;
|
|
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.
|
|
* Approved hours are locked until management cancels the approval.
|
|
*/
|
|
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,
|
|
private ?int $approvedBy = null,
|
|
private ?DateTimeImmutable $approvedAt = null,
|
|
) {}
|
|
|
|
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, ?int $approvedBy = null, ?DateTimeImmutable $approvedAt = null): self
|
|
{
|
|
return new self($id, $userId, $taskId, $jobId, $workTypeId, $workedOn, $hours, $note, $approvedBy, $approvedAt);
|
|
}
|
|
|
|
/**
|
|
* @throws TimeApproved
|
|
*/
|
|
public function changeHours(WorkedHours $hours, ?string $note): void
|
|
{
|
|
$this->ensureNotApproved();
|
|
$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->ensureNotApproved();
|
|
$this->hours = $this->hours->plus($hours);
|
|
$note = self::text($note);
|
|
|
|
if ($note !== null) {
|
|
$this->note = $this->note === null ? $note : $this->note."\n".$note;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Approves the entry; an approved entry keeps its first approval.
|
|
*/
|
|
public function approve(int $approvedBy, DateTimeImmutable $approvedAt): void
|
|
{
|
|
if ($this->isApproved()) {
|
|
return;
|
|
}
|
|
|
|
$this->approvedBy = $approvedBy;
|
|
$this->approvedAt = $approvedAt;
|
|
}
|
|
|
|
public function cancelApproval(): void
|
|
{
|
|
$this->approvedBy = null;
|
|
$this->approvedAt = null;
|
|
}
|
|
|
|
public function isApproved(): bool
|
|
{
|
|
return $this->approvedAt !== null;
|
|
}
|
|
|
|
/**
|
|
* @throws TimeApproved
|
|
*/
|
|
public function ensureNotApproved(): void
|
|
{
|
|
if ($this->isApproved()) {
|
|
throw new TimeApproved;
|
|
}
|
|
}
|
|
|
|
public function approvedBy(): ?int
|
|
{
|
|
return $this->approvedBy;
|
|
}
|
|
|
|
public function approvedAt(): ?DateTimeImmutable
|
|
{
|
|
return $this->approvedAt;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|