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:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Inventory\Application\UseCases;
|
||||
|
||||
use App\Modules\Inventory\Domain\Contracts\StockItemGroupRepository;
|
||||
use App\Modules\Inventory\Domain\Entities\StockItemGroup;
|
||||
use App\Modules\Inventory\Domain\Exceptions\InvalidStockName;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockNameTaken;
|
||||
use App\Modules\Inventory\Domain\ValueObjects\StockName;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Adds a stock item group last in order.
|
||||
*/
|
||||
final class AddStockItemGroup
|
||||
{
|
||||
public function __construct(
|
||||
private StockItemGroupRepository $groups,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws InvalidStockName
|
||||
* @throws StockNameTaken
|
||||
*/
|
||||
public function execute(string $name): int
|
||||
{
|
||||
$name = new StockName($name);
|
||||
|
||||
return $this->transactions->run(function () use ($name): int {
|
||||
if ($this->groups->isNameTaken($name->value)) {
|
||||
throw new StockNameTaken;
|
||||
}
|
||||
|
||||
return (int) $this->groups->save(StockItemGroup::create($name, $this->groups->nextSortOrder()))->id();
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user