Files
Toiminnanohjaus/app/Modules/Inventory/Application/UseCases/TaskMaterials.php
T

104 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\UseCases;
use App\Modules\Inventory\Application\Contracts\StockTasks;
use App\Modules\Inventory\Application\DTOs\StockUseData;
use App\Modules\Inventory\Application\DTOs\StockUseResult;
use App\Modules\Inventory\Domain\Contracts\TaskMaterialSuggestionRepository;
use App\Modules\Inventory\Domain\Exceptions\InvalidStockQuantity;
use App\Modules\Inventory\Domain\Exceptions\StockItemInactive;
use App\Modules\Inventory\Domain\Exceptions\StockItemNotFound;
use App\Modules\Inventory\Domain\Exceptions\StockTaskNotAvailable;
use App\Modules\Inventory\Domain\Exceptions\StockTaskSuggestionNotFound;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Material used on a job's task (Käytetty materiaali, spec 19.4): recorded
* from a search, or from the default materials suggested by the workflow
* step the task was made of. Material is recorded only on tasks whose
* service unit uses stock and whose job is not locked.
*/
final class TaskMaterials
{
public function __construct(
private RecordStockUse $recordUse,
private StockTasks $tasks,
private TaskMaterialSuggestionRepository $suggestions,
private TransactionManager $transactions,
) {}
/**
* Stores a workflow step's default materials as suggestions.
*
* @param list<array{stockItemId: int, quantityMilli: int}> $materials
*/
public function suggest(int $taskId, array $materials): void
{
$this->transactions->run(function () use ($taskId, $materials): void {
foreach ($materials as $material) {
$this->suggestions->add($taskId, $material['stockItemId'], $material['quantityMilli']);
}
});
}
/**
* @throws StockTaskNotAvailable
* @throws StockItemNotFound
* @throws StockItemInactive
* @throws InvalidStockQuantity
*/
public function record(int $taskId, int $stockItemId, string $quantity, int $actorUserId): StockUseResult
{
$context = $this->tasks->context($taskId);
if ($context === null || ! $context->usesStock || $context->isLocked) {
throw new StockTaskNotAvailable;
}
return $this->recordUse->execute(new StockUseData($stockItemId, $quantity, taskId: $taskId, jobId: $context->jobId), $actorUserId);
}
/**
* Records a suggested material with the quantity confirmed (possibly
* changed) and removes the suggestion.
*
* @throws StockTaskSuggestionNotFound
* @throws StockTaskNotAvailable
* @throws StockItemNotFound
* @throws StockItemInactive
* @throws InvalidStockQuantity
*/
public function confirm(int $taskId, int $suggestionId, string $quantity, int $actorUserId): StockUseResult
{
return $this->transactions->run(function () use ($taskId, $suggestionId, $quantity, $actorUserId): StockUseResult {
$suggestion = $this->suggestions->find($suggestionId);
if ($suggestion === null || $suggestion['taskId'] !== $taskId) {
throw new StockTaskSuggestionNotFound;
}
$result = $this->record($taskId, $suggestion['stockItemId'], $quantity, $actorUserId);
$this->suggestions->remove($suggestionId);
return $result;
});
}
/**
* @throws StockTaskSuggestionNotFound
*/
public function dismiss(int $taskId, int $suggestionId): void
{
$suggestion = $this->suggestions->find($suggestionId);
if ($suggestion === null || $suggestion['taskId'] !== $taskId) {
throw new StockTaskSuggestionNotFound;
}
$this->suggestions->remove($suggestionId);
}
}