46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Inventory\Application\UseCases;
|
|
|
|
use App\Modules\Inventory\Application\DTOs\StockItemData;
|
|
use App\Modules\Inventory\Domain\Contracts\StockItemRepository;
|
|
use App\Modules\Inventory\Domain\Entities\StockItem;
|
|
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\Shared\Application\Contracts\TransactionManager;
|
|
|
|
/**
|
|
* Adds a stock item with a zero balance; stock comes in with purchases.
|
|
*/
|
|
final class AddStockItem
|
|
{
|
|
public function __construct(
|
|
private StockItemRepository $items,
|
|
private StockItemFields $fields,
|
|
private TransactionManager $transactions,
|
|
) {}
|
|
|
|
/**
|
|
* @throws InvalidStockName
|
|
* @throws InvalidStockQuantity
|
|
* @throws StockItemGroupNotFound
|
|
* @throws StockNameTaken
|
|
*/
|
|
public function execute(StockItemData $data): int
|
|
{
|
|
$fields = $this->fields->parse($data);
|
|
|
|
return $this->transactions->run(function () use ($data, $fields): int {
|
|
$this->fields->check($data, $fields['name']);
|
|
|
|
$item = StockItem::create($data->groupId, $fields['name'], $fields['unit'], $data->code, $data->supplier, $fields['reorderLevelMilli']);
|
|
|
|
return (int) $this->items->save($item)->id();
|
|
});
|
|
}
|
|
}
|