Add machines with day-by-day work lists for job tasks

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:43:55 +02:00
co-authored by Claude Sonnet 5.5
parent 0de0e332b2
commit aac579257a
59 changed files with 3803 additions and 1 deletions
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Contracts;
use App\Modules\Machines\Domain\Entities\Machine;
interface MachineRepository
{
public function findById(int $id): ?Machine;
/**
* Saves the machine with its service units and counters; counters left
* out are removed when nothing refers to them.
*/
public function save(Machine $machine): Machine;
public function isNameTaken(string $name, ?int $exceptId = null): bool;
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Contracts;
use App\Modules\Machines\Domain\Entities\QueueEntry;
use DateTimeImmutable;
interface QueueRepository
{
public function findById(int $id): ?QueueEntry;
public function openEntryForTask(int $taskId): ?QueueEntry;
public function openEntryForOrderLine(int $orderLineId): ?QueueEntry;
/**
* The machine's open rows planned on the day, in position order.
*
* @return list<QueueEntry>
*/
public function openEntriesOn(int $machineId, DateTimeImmutable $day): array;
public function save(QueueEntry $entry): QueueEntry;
public function delete(QueueEntry $entry): void;
}
@@ -0,0 +1,162 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Entities;
use App\Modules\Machines\Domain\Enums\MachineType;
use App\Modules\Machines\Domain\Exceptions\MachineInactive;
use App\Modules\Machines\Domain\Exceptions\MachineNeedsServiceUnit;
use App\Modules\Machines\Domain\ValueObjects\MachineCounter;
use App\Modules\Machines\Domain\ValueObjects\MachineName;
/**
* A machine (kone, spec 19.4): a printing press, a sticker cutter or other,
* in one or more service units, with a responsible person who gets the
* maintenance notifications, and counters. Machines are never deleted: one
* out of use is not offered, but its history stays.
*/
final class Machine
{
/**
* @param list<int> $serviceUnitIds
* @param list<MachineCounter> $counters
*/
private function __construct(
private ?int $id,
private MachineName $name,
private MachineType $type,
private ?string $description,
private array $serviceUnitIds,
private ?int $responsibleUserId,
private array $counters,
private bool $active,
) {
if ($serviceUnitIds === []) {
throw new MachineNeedsServiceUnit;
}
}
/**
* @param list<int> $serviceUnitIds
* @param list<MachineCounter> $counters
*
* @throws MachineNeedsServiceUnit
*/
public static function create(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): self
{
return new self(null, $name, $type, self::text($description), array_values(array_unique($serviceUnitIds)), $responsibleUserId, $counters, true);
}
/**
* @param list<int> $serviceUnitIds
* @param list<MachineCounter> $counters
*/
public static function reconstitute(int $id, MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters, bool $active): self
{
return new self($id, $name, $type, $description, $serviceUnitIds, $responsibleUserId, $counters, $active);
}
/**
* @param list<int> $serviceUnitIds
* @param list<MachineCounter> $counters
*
* @throws MachineNeedsServiceUnit
*/
public function change(MachineName $name, MachineType $type, ?string $description, array $serviceUnitIds, ?int $responsibleUserId, array $counters): void
{
if ($serviceUnitIds === []) {
throw new MachineNeedsServiceUnit;
}
$this->name = $name;
$this->type = $type;
$this->description = self::text($description);
$this->serviceUnitIds = array_values(array_unique($serviceUnitIds));
$this->responsibleUserId = $responsibleUserId;
$this->counters = $counters;
}
/**
* Whether the machine serves any of the service units.
*
* @param list<int> $serviceUnitIds
*/
public function servesAnyOf(array $serviceUnitIds): bool
{
return array_intersect($this->serviceUnitIds, $serviceUnitIds) !== [];
}
/**
* @throws MachineInactive
*/
public function ensureActive(): void
{
if (! $this->active) {
throw new MachineInactive;
}
}
public function activate(): void
{
$this->active = true;
}
public function deactivate(): void
{
$this->active = false;
}
public function id(): ?int
{
return $this->id;
}
public function name(): MachineName
{
return $this->name;
}
public function type(): MachineType
{
return $this->type;
}
public function description(): ?string
{
return $this->description;
}
/**
* @return list<int>
*/
public function serviceUnitIds(): array
{
return $this->serviceUnitIds;
}
public function responsibleUserId(): ?int
{
return $this->responsibleUserId;
}
/**
* @return list<MachineCounter>
*/
public function counters(): array
{
return $this->counters;
}
public function isActive(): bool
{
return $this->active;
}
private static function text(?string $value): ?string
{
$value = $value === null ? '' : trim($value);
return $value === '' ? null : mb_substr($value, 0, 5000);
}
}
@@ -0,0 +1,132 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Entities;
use App\Modules\Machines\Domain\Exceptions\QueueEntryCompleted;
use DateTimeImmutable;
/**
* A row of a machine's work list (spec 19.4): a job's task, or a step of a
* quick sale order line, to be made on the machine on its planned day, in
* its position among that day's rows. A row not made on its day stays open
* and shows as late on the following days. Completed rows leave the list.
*/
final class QueueEntry
{
private function __construct(
private ?int $id,
private int $machineId,
private DateTimeImmutable $plannedOn,
private int $position,
private ?int $taskId,
private ?int $orderLineId,
private ?int $stepPosition,
private ?DateTimeImmutable $completedAt,
private ?int $completedBy,
) {}
public static function forTask(int $machineId, int $taskId, DateTimeImmutable $plannedOn, int $position): self
{
return new self(null, $machineId, $plannedOn, $position, $taskId, null, null, null, null);
}
public static function forOrderLine(int $machineId, int $orderLineId, int $stepPosition, DateTimeImmutable $plannedOn, int $position): self
{
return new self(null, $machineId, $plannedOn, $position, null, $orderLineId, $stepPosition, null, null);
}
public static function reconstitute(int $id, int $machineId, DateTimeImmutable $plannedOn, int $position, ?int $taskId, ?int $orderLineId, ?int $stepPosition, ?DateTimeImmutable $completedAt, ?int $completedBy): self
{
return new self($id, $machineId, $plannedOn, $position, $taskId, $orderLineId, $stepPosition, $completedAt, $completedBy);
}
/**
* @throws QueueEntryCompleted
*/
public function plan(int $machineId, DateTimeImmutable $plannedOn, int $position): void
{
$this->ensureOpen();
$this->machineId = $machineId;
$this->plannedOn = $plannedOn;
$this->position = $position;
}
public function moveTo(int $position): void
{
$this->position = $position;
}
/**
* @param int|null $userId Null when the task was marked done elsewhere.
*
* @throws QueueEntryCompleted
*/
public function complete(?int $userId, DateTimeImmutable $at): void
{
$this->ensureOpen();
$this->completedBy = $userId;
$this->completedAt = $at;
}
/**
* @throws QueueEntryCompleted
*/
public function ensureOpen(): void
{
if ($this->completedAt !== null) {
throw new QueueEntryCompleted;
}
}
public function isOrderLine(): bool
{
return $this->orderLineId !== null;
}
public function id(): ?int
{
return $this->id;
}
public function machineId(): int
{
return $this->machineId;
}
public function plannedOn(): DateTimeImmutable
{
return $this->plannedOn;
}
public function position(): int
{
return $this->position;
}
public function taskId(): ?int
{
return $this->taskId;
}
public function orderLineId(): ?int
{
return $this->orderLineId;
}
public function stepPosition(): ?int
{
return $this->stepPosition;
}
public function completedAt(): ?DateTimeImmutable
{
return $this->completedAt;
}
public function completedBy(): ?int
{
return $this->completedBy;
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Enums;
/**
* The unit of a machine counter (spec 19.5): prints, sheets, printed area,
* ink used, cut length or running hours.
*/
enum CounterUnit: string
{
case Piece = 'piece';
case Sheet = 'sheet';
case SquareMetre = 'square_metre';
case Millilitre = 'millilitre';
case Metre = 'metre';
case Hour = 'hour';
}
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Enums;
/**
* The kind of machine (spec 19.4): printing presses that use ink or toner
* (e.g. wide-format printers), sticker cutters, and others. Labels are in
* lang/*\/machines.php under types.
*/
enum MachineType: string
{
case Printer = 'printer';
case Cutter = 'cutter';
case Other = 'other';
/**
* The counters suggested for a new machine of this type (spec 19.5), as
* translation keys of their names with their units.
*
* @return list<array{name: string, unit: CounterUnit}>
*/
public function suggestedCounters(): array
{
return match ($this) {
self::Printer => [
['name' => 'prints', 'unit' => CounterUnit::Piece],
['name' => 'printed_area', 'unit' => CounterUnit::SquareMetre],
],
self::Cutter => [
['name' => 'cut_length', 'unit' => CounterUnit::Metre],
],
self::Other => [],
};
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidCounter extends DomainException
{
public function __construct()
{
parent::__construct('A counter needs a name of 1-100 characters and a unit.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidMachineName extends DomainException
{
public function __construct()
{
parent::__construct('A machine name must be 1-150 characters.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InvalidPlannedDay extends DomainException
{
public function __construct()
{
parent::__construct('The planned day must be a valid date.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MachineInactive extends DomainException
{
public function __construct()
{
parent::__construct('The machine is no longer in use.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MachineNameTaken extends DomainException
{
public function __construct()
{
parent::__construct('The machine name is already in use.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MachineNeedsServiceUnit extends DomainException
{
public function __construct()
{
parent::__construct('A machine needs at least one service unit that uses stock.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MachineNotForTask extends DomainException
{
public function __construct()
{
parent::__construct('The machine does not belong to the service unit of the task.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MachineNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The machine was not found.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class MachineTaskNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The task was not found.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class QueueEntryCompleted extends DomainException
{
public function __construct()
{
parent::__construct('The work list row has already been completed.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class QueueEntryNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The work list row was not found.');
}
}
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\Services;
/**
* Where a task goes in a day's work list by default (spec 19.4): after
* quick sale orders, then by priority (higher first) and due date (earlier
* first, none last). Rows moved by hand keep their places; a new task
* goes before the first task it outranks.
*/
final class QueueOrder
{
/**
* @param list<array{isOrder: bool, rank: int, dueOn: ?string}> $rows The day's open rows in order.
* @return int The index to insert the new task at.
*/
public static function insertionIndex(array $rows, int $rank, ?string $dueOn): int
{
foreach ($rows as $index => $row) {
if ($row['isOrder']) {
continue;
}
if (self::before($rank, $dueOn, $row['rank'], $row['dueOn'])) {
return $index;
}
}
return count($rows);
}
private static function before(int $rank, ?string $dueOn, int $otherRank, ?string $otherDueOn): bool
{
if ($rank !== $otherRank) {
return $rank > $otherRank;
}
if ($dueOn === null) {
return false;
}
return $otherDueOn === null || $dueOn < $otherDueOn;
}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\ValueObjects;
use App\Modules\Machines\Domain\Enums\CounterUnit;
use App\Modules\Machines\Domain\Exceptions\InvalidCounter;
/**
* A counter of a machine (laskuri, spec 19.5) by which maintenance needs
* are measured, e.g. prints or printed area. $id is null for a new one.
*/
final readonly class MachineCounter
{
public const int MAX_LENGTH = 100;
public string $name;
/**
* @throws InvalidCounter
*/
public function __construct(
public ?int $id,
string $name,
public CounterUnit $unit,
) {
$name = trim(preg_replace('/\s+/u', ' ', $name) ?? $name);
if ($name === '' || mb_strlen($name) > self::MAX_LENGTH) {
throw new InvalidCounter;
}
$this->name = $name;
}
}
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Modules\Machines\Domain\ValueObjects;
use App\Modules\Machines\Domain\Exceptions\InvalidMachineName;
/**
* A machine's name, e.g. "Suurkuvatulostin 1".
*/
final readonly class MachineName
{
public const int MAX_LENGTH = 150;
public string $value;
/**
* @throws InvalidMachineName
*/
public function __construct(string $value)
{
$value = trim(preg_replace('/\s+/u', ' ', $value) ?? $value);
if ($value === '' || mb_strlen($value) > self::MAX_LENGTH) {
throw new InvalidMachineName;
}
$this->value = $value;
}
}