Add stock items, groups and movements at a moving average cost

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:18:59 +02:00
co-authored by Claude Sonnet 5.5
parent 361a2974cf
commit 30132deb0e
81 changed files with 4968 additions and 115 deletions
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\UseCases;
use App\Modules\Inventory\Application\DTOs\StockItemData;
use App\Modules\Inventory\Domain\Contracts\StockItemGroupRepository;
use App\Modules\Inventory\Domain\Contracts\StockItemRepository;
use App\Modules\Inventory\Domain\Enums\StockUnit;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockName;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
use App\Modules\Inventory\Domain\Exceptions\StockItemGroupNotFound;
use App\Modules\Inventory\Domain\Exceptions\StockNameTaken;
use App\Modules\Inventory\Domain\ValueObjects\StockName;
use App\Modules\Inventory\Domain\ValueObjects\StockQuantity;
/**
* Checks a stock item's entered details, shared by adding and updating.
*/
final class StockItemFields
{
public function __construct(
private StockItemRepository $items,
private StockItemGroupRepository $groups,
) {}
/**
* @return array{name: StockName, unit: StockUnit, reorderLevelMilli: ?int}
*
* @throws InvalidStockName
* @throws InvalidStockQuantity
*/
public function parse(StockItemData $data): array
{
return [
'name' => new StockName($data->name),
'unit' => StockUnit::tryFrom($data->unit) ?? StockUnit::Piece,
'reorderLevelMilli' => StockQuantity::parse($data->reorderLevel)?->milli,
];
}
/**
* A new item needs a group in use; an existing item may stay in its
* group after the group is taken out of use.
*
* @throws StockItemGroupNotFound
* @throws StockNameTaken
*/
public function check(StockItemData $data, StockName $name, ?int $exceptItemId = null): void
{
$group = $this->groups->findById($data->groupId);
if ($group === null || ($exceptItemId === null && ! $group->isActive())) {
throw new StockItemGroupNotFound;
}
if ($this->items->isNameTaken($name->value, $exceptItemId)) {
throw new StockNameTaken;
}
}
}