Files
2026-11-26 16:34:08 +02:00

122 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Entities;
use App\Modules\Inventory\Domain\Exceptions\InventoryCountConfirmed;
use DateTimeImmutable;
/**
* A stock count (inventaario, spec 19.3) of the whole stock or of chosen
* item groups. It is open while counted and confirmed once by management;
* a confirmed count is never changed.
*/
final class InventoryCount
{
/**
* @param list<int>|null $groupIds Null: the whole stock.
*/
private function __construct(
private ?int $id,
private DateTimeImmutable $countedOn,
private ?array $groupIds,
private int $startedBy,
private ?int $confirmedBy,
private ?DateTimeImmutable $confirmedAt,
private ?int $valueBeforeCents,
private ?int $valueAfterCents,
) {}
/**
* @param list<int>|null $groupIds
*/
public static function start(DateTimeImmutable $countedOn, ?array $groupIds, int $startedBy): self
{
return new self(null, $countedOn, $groupIds === [] ? null : $groupIds, $startedBy, null, null, null, null);
}
/**
* @param list<int>|null $groupIds
*/
public static function reconstitute(int $id, DateTimeImmutable $countedOn, ?array $groupIds, int $startedBy, ?int $confirmedBy, ?DateTimeImmutable $confirmedAt, ?int $valueBeforeCents, ?int $valueAfterCents): self
{
return new self($id, $countedOn, $groupIds, $startedBy, $confirmedBy, $confirmedAt, $valueBeforeCents, $valueAfterCents);
}
/**
* @throws InventoryCountConfirmed
*/
public function ensureOpen(): void
{
if ($this->isConfirmed()) {
throw new InventoryCountConfirmed;
}
}
/**
* @throws InventoryCountConfirmed
*/
public function confirm(int $userId, DateTimeImmutable $at, int $valueBeforeCents, int $valueAfterCents): void
{
$this->ensureOpen();
$this->confirmedBy = $userId;
$this->confirmedAt = $at;
$this->valueBeforeCents = $valueBeforeCents;
$this->valueAfterCents = $valueAfterCents;
}
public function covers(int $groupId): bool
{
return $this->groupIds === null || in_array($groupId, $this->groupIds, true);
}
public function isConfirmed(): bool
{
return $this->confirmedAt !== null;
}
public function id(): ?int
{
return $this->id;
}
public function countedOn(): DateTimeImmutable
{
return $this->countedOn;
}
/**
* @return list<int>|null
*/
public function groupIds(): ?array
{
return $this->groupIds;
}
public function startedBy(): int
{
return $this->startedBy;
}
public function confirmedBy(): ?int
{
return $this->confirmedBy;
}
public function confirmedAt(): ?DateTimeImmutable
{
return $this->confirmedAt;
}
public function valueBeforeCents(): ?int
{
return $this->valueBeforeCents;
}
public function valueAfterCents(): ?int
{
return $this->valueAfterCents;
}
}