Files
2026-11-30 06:56:14 +02:00

218 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\TimeTracking\Domain\Entities;
use App\Modules\TimeTracking\Domain\Enums\EntryBillingModel;
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; billed
* hours (on a billing material) stay approved for good.
*/
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,
private ?int $billingExportId = null,
private ?EntryBillingModel $billingModel = null,
private ?string $internalComment = 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, ?int $billingExportId = null, ?EntryBillingModel $billingModel = null, ?string $internalComment = null): self
{
return new self($id, $userId, $taskId, $jobId, $workTypeId, $workedOn, $hours, $note, $approvedBy, $approvedAt, $billingExportId, $billingModel, $internalComment);
}
/**
* The entry's laskutusperuste, billing note (shown to the customer or in
* fixed-price follow-up, required before the week is approved) and
* internal comment. A null billing model follows the job.
*
* @throws TimeApproved
*/
public function describe(?EntryBillingModel $billingModel, ?string $billingNote, ?string $internalComment): void
{
$this->ensureNotApproved();
$this->billingModel = $billingModel;
$this->note = self::text($billingNote);
$this->internalComment = self::text($internalComment);
}
public function hasBillingNote(): bool
{
return $this->note !== null;
}
public function billingModel(): ?EntryBillingModel
{
return $this->billingModel;
}
public function internalComment(): ?string
{
return $this->internalComment;
}
/**
* @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;
}
/**
* Cancels the approval, unless the hours are billed.
*/
public function cancelApproval(): void
{
if ($this->isBilled()) {
return;
}
$this->approvedBy = null;
$this->approvedAt = null;
}
/**
* Marks approved hours billed on the given billing material.
*/
public function markBilled(int $billingExportId): void
{
if ($this->isApproved() && ! $this->isBilled()) {
$this->billingExportId = $billingExportId;
}
}
public function isBilled(): bool
{
return $this->billingExportId !== null;
}
public function billingExportId(): ?int
{
return $this->billingExportId;
}
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;
}
}