Files
Toiminnanohjaus/app/Modules/Jobs/Domain/Entities/CustomerJob.php
T
petriandClaude Opus 5.5 c149c56388 Add the quoted amount and completion time to customer jobs
- Jobs get a budget / quoted amount (cents, VAT 0). Only users who may
  see quotes see and edit it on the job form and page, and only they see
  its changes in the history. Others' saves keep the stored amount: the
  server decides whether the amount is set, not the browser.
- Jobs record when they were done: set when a job first reaches a status
  that locks its hours, kept through the billing statuses, cleared when
  management opens the lock. The billing export will use it.

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

110 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Entities;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\ValueObjects\FieldChange;
use App\Modules\Jobs\Domain\ValueObjects\JobDetails;
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
use DateTimeImmutable;
/**
* A customer job (asiakastyö): the work done for a customer, which tasks,
* time entries and invoices are attached to. It knows when it was done:
* reaching a status that locks hours sets the time, and opening the lock
* clears it.
*/
final class CustomerJob
{
private function __construct(
private ?int $id,
private JobNumber $number,
private JobDetails $details,
private int $createdBy,
private ?DateTimeImmutable $completedAt = null,
) {}
public static function open(JobNumber $number, JobDetails $details, int $createdBy): self
{
return new self(null, $number, $details, $createdBy);
}
public static function reconstitute(int $id, JobNumber $number, JobDetails $details, int $createdBy, ?DateTimeImmutable $completedAt = null): self
{
return new self($id, $number, $details, $createdBy, $completedAt);
}
/**
* Keeps the completion time in step with the status: set when the job
* first reaches a locking status, kept while it stays locked, cleared
* when the lock is opened.
*/
public function recordCompletion(DateTimeImmutable $now): void
{
if (! $this->details->status->locksTime()) {
$this->completedAt = null;
return;
}
$this->completedAt ??= $now;
}
public function completedAt(): ?DateTimeImmutable
{
return $this->completedAt;
}
/**
* Replaces the details and returns what changed, for the history.
*
* @return list<FieldChange>
*/
public function changeDetails(JobDetails $details): array
{
$before = $this->details->comparable();
$after = $details->comparable();
$this->details = $details;
$changes = [];
foreach ($after as $field => $value) {
if (($before[$field] ?? null) !== $value) {
$changes[] = new FieldChange($field, $before[$field] ?? null, $value);
}
}
return $changes;
}
/**
* @return list<FieldChange>
*/
public function changeStatus(JobStatus $status): array
{
return $this->changeDetails($this->details->withStatus($status));
}
public function id(): ?int
{
return $this->id;
}
public function number(): JobNumber
{
return $this->number;
}
public function details(): JobDetails
{
return $this->details;
}
public function createdBy(): int
{
return $this->createdBy;
}
}