Add customer jobs with numbering, members, tags, links and history

Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-23 18:57:18 +02:00
co-authored by Claude Opus 5.5
parent 51b2d4a252
commit 70e107f6e2
95 changed files with 4631 additions and 26 deletions
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Contracts;
use App\Modules\Jobs\Domain\Entities\CustomerJob;
use App\Modules\Jobs\Domain\ValueObjects\FieldChange;
interface CustomerJobRepository
{
public function findById(int $id): ?CustomerJob;
/**
* Inserts a new job or updates a stored one, with its service units,
* members, tags and links, and returns the stored job.
*/
public function save(CustomerJob $job): CustomerJob;
/**
* @param list<FieldChange> $changes
*/
public function recordChanges(int $jobId, int $userId, array $changes): void;
}
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Contracts;
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
interface JobNumberSequence
{
/**
* The next free number of the year. Must be called inside the
* transaction that stores the job, so numbers are never reused.
*/
public function next(int $year): JobNumber;
}
@@ -0,0 +1,84 @@
<?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;
/**
* A customer job (asiakastyö): the work done for a customer, which tasks,
* time entries and invoices are attached to.
*/
final class CustomerJob
{
private function __construct(
private ?int $id,
private JobNumber $number,
private JobDetails $details,
private int $createdBy,
) {}
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): self
{
return new self($id, $number, $details, $createdBy);
}
/**
* 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;
}
}
+16
View File
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Enums;
/**
* Kesto / laji (spec 5.2).
*/
enum JobKind: string
{
case Service = 'service';
case OneOff = 'one_off';
case FixedTerm = 'fixed_term';
case Ongoing = 'ongoing';
}
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Enums;
/**
* Tila (spec 5.3). The cases are in their usual order; any status may be
* chosen. The statuses are also the columns of the job Kanban board.
*/
enum JobStatus: string
{
case Prospect = 'prospect';
case Tentative = 'tentative';
case QuoteRequest = 'quote_request';
case Order = 'order';
case InProduction = 'in_production';
case OnHold = 'on_hold';
case Done = 'done';
case Invoiced = 'invoiced';
case Billed = 'billed';
case Archived = 'archived';
/**
* Statuses set when the job is invoiced: only management and the
* job's biller may set them.
*/
public function isBillingStatus(): bool
{
return $this === self::Invoiced || $this === self::Billed;
}
/**
* The Kanban columns: every status except archived.
*
* @return list<self>
*/
public static function kanbanStatuses(): array
{
$statuses = [];
foreach (self::cases() as $status) {
if ($status !== self::Archived) {
$statuses[] = $status;
}
}
return $statuses;
}
}
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Enums;
/**
* Maksuperuste (spec 5.5).
*/
enum PaymentBasis: string
{
case PriceList = 'price_list';
case FixedQuote = 'fixed_quote';
case MonthlyFee = 'monthly_fee';
case Hourly = 'hourly';
case NotBillable = 'not_billable';
}
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Enums;
/**
* Prioriteetti (spec 5.4). Always shown with text, never colour alone.
*/
enum Priority: string
{
case Waiting = 'waiting';
case Normal = 'normal';
case First = 'first';
case Urgent = 'urgent';
case Immediate = 'immediate';
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class CustomerJobNotFound extends DomainException
{
public function __construct(public readonly int $jobId)
{
parent::__construct("Customer job {$jobId} was not found.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class DueBeforeStart extends DomainException
{
public function __construct()
{
parent::__construct('The due date cannot be before the start date.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidEstimatedHours extends DomainException
{
public function __construct()
{
parent::__construct('Estimated hours must be zero or more, in quarters of an hour.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidJobLink extends DomainException
{
public function __construct()
{
parent::__construct('A link needs a title and an http or https address.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidJobName extends DomainException
{
public function __construct()
{
parent::__construct('The job name must be 1–255 characters.');
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
/**
* A chosen customer, contact, billing unit, service unit or user does not
* exist, is not in use, or belongs to another customer.
*/
final class InvalidJobReference extends DomainException
{
public function __construct(public readonly string $field)
{
parent::__construct("The job's {$field} is not a valid choice.");
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidTag extends DomainException
{
public function __construct()
{
parent::__construct('A tag must be 1–50 characters.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class PlanEndsBeforeStart extends DomainException
{
public function __construct()
{
parent::__construct('The planned end cannot be before the planned start.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class ServiceUnitRequired extends DomainException
{
public function __construct()
{
parent::__construct('A customer job needs at least one service unit.');
}
}
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
/**
* Estimated work in hours, to the nearest quarter of an hour.
*/
final readonly class EstimatedHours
{
public const float MAX = 99999.75;
public float $value;
public function __construct(float $value)
{
if ($value < 0 || $value > self::MAX || fmod($value * 4, 1.0) !== 0.0) {
throw new InvalidEstimatedHours;
}
$this->value = $value;
}
public static function fromString(string $value): self
{
$value = str_replace(',', '.', trim($value));
if (! is_numeric($value)) {
throw new InvalidEstimatedHours;
}
return new self((float) $value);
}
/**
* E.g. "12.5", without trailing zeros.
*/
public function format(): string
{
return rtrim(rtrim(number_format($this->value, 2, '.', ''), '0'), '.');
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
/**
* One changed field in a job's history, with raw values (ids, enum keys,
* Y-m-d dates); the page turns them into names when shown.
*/
final readonly class FieldChange
{
public function __construct(
public string $field,
public ?string $oldValue,
public ?string $newValue,
) {}
}
@@ -0,0 +1,152 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use App\Modules\Jobs\Domain\Enums\JobKind;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\PaymentBasis;
use App\Modules\Jobs\Domain\Enums\Priority;
use App\Modules\Jobs\Domain\Exceptions\DueBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired;
use DateTimeImmutable;
/**
* Everything about a customer job that users edit. Kept in one value so a
* change can be compared field by field for the job's history.
*/
final readonly class JobDetails
{
/** @var list<int> */
public array $serviceUnitIds;
/** @var list<int> */
public array $memberUserIds;
/** @var list<Tag> */
public array $tags;
/**
* @param list<int> $serviceUnitIds
* @param list<int> $memberUserIds
* @param list<Tag> $tags
* @param list<JobLink> $links
*/
public function __construct(
public JobName $name,
public ?string $description,
public int $customerId,
array $serviceUnitIds,
public ?DateTimeImmutable $startsOn,
public ?DateTimeImmutable $dueOn,
public ?DateTimeImmutable $plannedFrom,
public ?DateTimeImmutable $plannedTo,
public JobKind $kind,
public JobStatus $status,
public Priority $priority,
public ?PaymentBasis $paymentBasis,
public ?EstimatedHours $estimatedHours,
public ?string $customerReference,
public ?string $instructions,
public int $responsibleUserId,
public ?int $contactUserId,
public ?int $customerContactId,
public ?int $billerUserId,
public ?int $billingUnitId,
array $memberUserIds,
array $tags,
public array $links,
) {
$serviceUnitIds = array_values(array_unique($serviceUnitIds));
if ($serviceUnitIds === []) {
throw new ServiceUnitRequired;
}
if ($startsOn !== null && $dueOn !== null && $dueOn < $startsOn) {
throw new DueBeforeStart;
}
if ($plannedFrom !== null && $plannedTo !== null && $plannedTo < $plannedFrom) {
throw new PlanEndsBeforeStart;
}
sort($serviceUnitIds);
$memberUserIds = array_values(array_unique($memberUserIds));
sort($memberUserIds);
$uniqueTags = [];
foreach ($tags as $tag) {
$uniqueTags[mb_strtolower($tag->value)] ??= $tag;
}
$this->serviceUnitIds = $serviceUnitIds;
$this->memberUserIds = $memberUserIds;
$this->tags = array_values($uniqueTags);
}
public function withStatus(JobStatus $status): self
{
return new self(
$this->name, $this->description, $this->customerId, $this->serviceUnitIds,
$this->startsOn, $this->dueOn, $this->plannedFrom, $this->plannedTo,
$this->kind, $status, $this->priority, $this->paymentBasis, $this->estimatedHours,
$this->customerReference, $this->instructions, $this->responsibleUserId, $this->contactUserId,
$this->customerContactId, $this->billerUserId, $this->billingUnitId, $this->memberUserIds,
$this->tags, $this->links,
);
}
/**
* Whether the user is a named member of the job in any role.
*/
public function involves(int $userId): bool
{
return in_array($userId, [$this->responsibleUserId, $this->contactUserId, $this->billerUserId], true)
|| in_array($userId, $this->memberUserIds, true);
}
/**
* Every field as a comparable string (or null), keyed by the field name
* used in the job's history.
*
* @return array<string, string|null>
*/
public function comparable(): array
{
$date = fn (?DateTimeImmutable $date): ?string => $date?->format('Y-m-d');
$ids = fn (array $ids): ?string => $ids === [] ? null : implode(',', $ids);
// Tags are shared and matched ignoring case, so a change of case is no change.
$tagNames = array_map(fn (Tag $tag): string => mb_strtolower($tag->value), $this->tags);
sort($tagNames);
return [
'name' => $this->name->value,
'description' => $this->description,
'customer' => (string) $this->customerId,
'service_units' => $ids($this->serviceUnitIds),
'starts_on' => $date($this->startsOn),
'due_on' => $date($this->dueOn),
'planned_from' => $date($this->plannedFrom),
'planned_to' => $date($this->plannedTo),
'kind' => $this->kind->value,
'status' => $this->status->value,
'priority' => $this->priority->value,
'payment_basis' => $this->paymentBasis?->value,
'estimated_hours' => $this->estimatedHours?->format(),
'customer_reference' => $this->customerReference,
'instructions' => $this->instructions,
'responsible_user' => (string) $this->responsibleUserId,
'contact_user' => $this->contactUserId === null ? null : (string) $this->contactUserId,
'customer_contact' => $this->customerContactId === null ? null : (string) $this->customerContactId,
'biller_user' => $this->billerUserId === null ? null : (string) $this->billerUserId,
'billing_unit' => $this->billingUnitId === null ? null : (string) $this->billingUnitId,
'members' => $ids($this->memberUserIds),
'tags' => $tagNames === [] ? null : implode(', ', $tagNames),
'links' => $this->links === [] ? null : implode("\n", array_map(fn (JobLink $link): string => $link->title.' <'.$link->url.'>', $this->links)),
];
}
}
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobLink;
/**
* A link to material of the job (offer, brief, files in Drive or Dropbox).
* Files are not stored in the system (K26).
*/
final readonly class JobLink
{
public string $title;
public string $url;
public function __construct(string $title, string $url)
{
$title = trim(preg_replace('/\s+/u', ' ', $title) ?? $title);
$url = trim($url);
if ($title === '' || mb_strlen($title) > 255 || mb_strlen($url) > 2000
|| filter_var($url, FILTER_VALIDATE_URL) === false
|| ! in_array(strtolower((string) parse_url($url, PHP_URL_SCHEME)), ['http', 'https'], true)) {
throw new InvalidJobLink;
}
$this->title = $title;
$this->url = $url;
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobName;
/**
* Name of a customer job.
*/
final readonly class JobName
{
public const int MAX_LENGTH = 255;
public string $value;
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidJobName;
}
$this->value = $value;
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use InvalidArgumentException;
/**
* Running job number, e.g. 2026-0042. The sequence restarts every year.
*/
final readonly class JobNumber
{
public function __construct(
public int $year,
public int $sequence,
) {
if ($year < 2000 || $year > 9999 || $sequence < 1) {
throw new InvalidArgumentException('A job number needs a four-digit year and a positive sequence.');
}
}
public static function fromString(string $value): self
{
if (preg_match('/^(\d{4})-(\d{4,})$/', $value, $parts) !== 1) {
throw new InvalidArgumentException("Not a job number: {$value}");
}
return new self((int) $parts[1], (int) $parts[2]);
}
public function format(): string
{
return sprintf('%04d-%04d', $this->year, $this->sequence);
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Jobs\Domain\ValueObjects;
use App\Modules\Jobs\Domain\Exceptions\InvalidTag;
/**
* A free-form tag of a customer job, e.g. "messut". Compared ignoring case.
*/
final readonly class Tag
{
public const int MAX_LENGTH = 50;
public string $value;
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidTag;
}
$this->value = $value;
}
}