Files
petriandClaude Opus 5.5 97bd23bed5 Add the billing material with drafts, combined invoices and billed marks
Management builds the monthly billing material (laskuaineisto) as a
draft: done jobs are added as invoices of their own, combined when the
customer, billing unit and reference allow it, and their billable hours
are set per work type. Creating the material checks the jobs again,
marks the approved hours and billable expenses billed, forms invoice
lines with price and VAT snapshots and moves the jobs to "Lasku luotu".
Fixed price jobs bill their quoted amount once. Billed hours keep their
approval and billed expenses can no longer change. The Fennoa export
and monthly fee lines come later.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 13:08:24 +02:00

185 lines
4.6 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; 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,
) {}
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): self
{
return new self($id, $userId, $taskId, $jobId, $workTypeId, $workedOn, $hours, $note, $approvedBy, $approvedAt, $billingExportId);
}
/**
* @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;
}
}