Count stock and correct the differences on confirmation

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:34:08 +02:00
co-authored by Claude Sonnet 5.5
parent bca1eed166
commit 0de0e332b2
27 changed files with 1568 additions and 0 deletions
@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Contracts;
use App\Modules\Inventory\Domain\Entities\InventoryCount;
use DateTimeImmutable;
interface InventoryCountRepository
{
public function findById(int $id): ?InventoryCount;
/**
* The count, locked until the end of the transaction.
*/
public function lockById(int $id): ?InventoryCount;
public function hasOpen(): bool;
public function save(InventoryCount $count): InventoryCount;
/**
* Stores a counted quantity with the book balance of the moment; a
* recount replaces the line.
*/
public function recordLine(int $countId, int $itemId, int $bookMilli, int $countedMilli, int $userId, DateTimeImmutable $at): void;
public function removeLine(int $countId, int $itemId): void;
/**
* @return list<array{itemId: int, bookMilli: int, countedMilli: int}>
*/
public function lines(int $countId): array;
public function setLineUnitCost(int $countId, int $itemId, int $unitCostHundredths): void;
}
@@ -19,4 +19,9 @@ interface StockItemRepository
public function save(StockItem $item): StockItem;
public function isNameTaken(string $name, ?int $exceptId = null): bool;
/**
* The value of all stock: each item's balance at its average cost.
*/
public function stockValueCents(): int;
}
@@ -0,0 +1,121 @@
<?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;
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InventoryCountAlreadyOpen extends DomainException
{
public function __construct()
{
parent::__construct('Another stock count is open.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InventoryCountConfirmed extends DomainException
{
public function __construct()
{
parent::__construct('The stock count has been confirmed and cannot be changed.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class InventoryCountNotFound extends DomainException
{
public function __construct()
{
parent::__construct('The stock count was not found.');
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class StockItemNotInCount extends DomainException
{
public function __construct()
{
parent::__construct('The item is not in this stock count.');
}
}