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,26 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\Contracts;
use App\Modules\Inventory\Application\DTOs\InventoryCountLine;
use App\Modules\Inventory\Application\DTOs\InventoryCountSummary;
interface InventoryCountQueries
{
/**
* @return list<InventoryCountSummary> Newest first.
*/
public function counts(): array;
public function findCount(int $id): ?InventoryCountSummary;
/**
* The items of the count in group order and by name: those in its
* groups that are in use, and every item counted in it.
*
* @return list<InventoryCountLine>
*/
public function lines(int $countId): array;
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\DTOs;
use App\Modules\Inventory\Domain\Enums\StockUnit;
/**
* An item on a stock count's list: its current balance and, once counted,
* the counted quantity and the book balance at the moment of counting.
* The unit cost is the one stored at confirmation, or the current average
* cost while the count is open.
*/
final readonly class InventoryCountLine
{
public function __construct(
public int $itemId,
public string $itemName,
public ?string $code,
public int $groupId,
public string $groupName,
public StockUnit $unit,
public int $currentBalanceMilli,
public ?int $bookMilli,
public ?int $countedMilli,
public int $unitCostHundredths,
public ?string $countedByName,
public ?string $countedAt,
) {}
public function isCounted(): bool
{
return $this->countedMilli !== null;
}
public function differenceMilli(): ?int
{
return $this->countedMilli === null || $this->bookMilli === null ? null : $this->countedMilli - $this->bookMilli;
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\DTOs;
/**
* A stock count as listed. $groupNames is empty for the whole stock;
* $confirmedAt is "Y-m-d H:i" in the company's time zone.
*/
final readonly class InventoryCountSummary
{
/**
* @param list<string> $groupNames
*/
public function __construct(
public int $id,
public string $countedOn,
public bool $isConfirmed,
public array $groupNames,
public string $startedByName,
public ?string $confirmedByName,
public ?string $confirmedAt,
public int $countedItems,
public ?int $valueBeforeCents,
public ?int $valueAfterCents,
) {}
}
@@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\UseCases;
use App\Modules\Inventory\Domain\Contracts\InventoryCountRepository;
use App\Modules\Inventory\Domain\Contracts\StockItemRepository;
use App\Modules\Inventory\Domain\Contracts\StockMovementRepository;
use App\Modules\Inventory\Domain\Entities\InventoryCount;
use App\Modules\Inventory\Domain\Entities\StockMovement;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
use App\Modules\Inventory\Domain\Exceptions\InventoryCountAlreadyOpen;
use App\Modules\Inventory\Domain\Exceptions\InventoryCountConfirmed;
use App\Modules\Inventory\Domain\Exceptions\InventoryCountNotFound;
use App\Modules\Inventory\Domain\Exceptions\StockItemNotFound;
use App\Modules\Inventory\Domain\Exceptions\StockItemNotInCount;
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* Stock counts (inventaario, spec 19.3): management starts one for the
* whole stock or chosen groups (one open at a time), anyone using stock
* enters counted quantities, possibly in parts and on several devices,
* and management confirms it: every difference becomes an adjustment at
* the average cost, and the stock value before and after is stored.
* Items not counted are left as they are.
*/
final class InventoryCounts
{
public function __construct(
private InventoryCountRepository $counts,
private StockItemRepository $items,
private StockMovementRepository $movements,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @param list<int> $groupIds Empty: the whole stock.
*
* @throws InventoryCountAlreadyOpen
*/
public function start(array $groupIds, int $actorUserId): int
{
return $this->transactions->run(function () use ($groupIds, $actorUserId): int {
if ($this->counts->hasOpen()) {
throw new InventoryCountAlreadyOpen;
}
return (int) $this->counts->save(InventoryCount::start($this->clock->today(), array_values(array_unique($groupIds)), $actorUserId))->id();
});
}
/**
* Enters a counted quantity with the item's book balance now; an empty
* value takes the item back to not counted.
*
* @return int|null The book balance stored with the count, null when cleared.
*
* @throws InventoryCountNotFound
* @throws InventoryCountConfirmed
* @throws StockItemNotFound
* @throws StockItemNotInCount
* @throws InvalidStockQuantity
*/
public function record(int $countId, int $itemId, string $counted, int $actorUserId): ?int
{
$quantity = StockQuantity::parse($counted);
return $this->transactions->run(function () use ($countId, $itemId, $quantity, $actorUserId): ?int {
$count = $this->counts->lockById($countId) ?? throw new InventoryCountNotFound;
$count->ensureOpen();
$item = $this->items->lockById($itemId) ?? throw new StockItemNotFound;
if (! $count->covers($item->groupId())) {
throw new StockItemNotInCount;
}
if ($quantity === null) {
$this->counts->removeLine($countId, $itemId);
return null;
}
$book = $this->movements->balanceOf($itemId);
$this->counts->recordLine($countId, $itemId, $book, $quantity->milli, $actorUserId, $this->clock->now());
return $book;
});
}
/**
* @return int The number of adjustments made.
*
* @throws InventoryCountNotFound
* @throws InventoryCountConfirmed
*/
public function confirm(int $countId, int $actorUserId): int
{
return $this->transactions->run(function () use ($countId, $actorUserId): int {
$count = $this->counts->lockById($countId) ?? throw new InventoryCountNotFound;
$count->ensureOpen();
$valueBefore = $this->items->stockValueCents();
$now = $this->clock->now();
$adjustments = 0;
foreach ($this->counts->lines($countId) as $line) {
$item = $this->items->lockById($line['itemId']) ?? throw new StockItemNotFound;
$this->counts->setLineUnitCost($countId, $line['itemId'], $item->averageCostHundredths());
$difference = $line['countedMilli'] - $line['bookMilli'];
if ($difference !== 0) {
$this->movements->add(StockMovement::adjustment($item, $difference, $countId, $count->countedOn(), $now, $actorUserId));
$adjustments++;
}
}
$count->confirm($actorUserId, $now, $valueBefore, $this->items->stockValueCents());
$this->counts->save($count);
return $adjustments;
});
}
}