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,46 @@
|
||||
<?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\Exceptions\InvalidStockName;
|
||||
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockItemGroupNotFound;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockItemNotFound;
|
||||
use App\Modules\Inventory\Domain\Exceptions\StockNameTaken;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
|
||||
/**
|
||||
* Changes a stock item's details. Its movements and balance stay.
|
||||
*/
|
||||
final class UpdateStockItem
|
||||
{
|
||||
public function __construct(
|
||||
private StockItemRepository $items,
|
||||
private StockItemFields $fields,
|
||||
private TransactionManager $transactions,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws InvalidStockName
|
||||
* @throws InvalidStockQuantity
|
||||
* @throws StockItemGroupNotFound
|
||||
* @throws StockItemNotFound
|
||||
* @throws StockNameTaken
|
||||
*/
|
||||
public function execute(int $itemId, StockItemData $data): void
|
||||
{
|
||||
$fields = $this->fields->parse($data);
|
||||
|
||||
$this->transactions->run(function () use ($itemId, $data, $fields): void {
|
||||
$item = $this->items->findById($itemId) ?? throw new StockItemNotFound;
|
||||
$this->fields->check($data, $fields['name'], $itemId);
|
||||
|
||||
$item->change($data->groupId, $fields['name'], $fields['unit'], $data->code, $data->supplier, $fields['reorderLevelMilli']);
|
||||
$this->items->save($item);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user