Record used material on tasks and suggest workflow steps' default materials

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:28:16 +02:00
co-authored by Claude Sonnet 5.5
parent 30132deb0e
commit bca1eed166
41 changed files with 1521 additions and 19 deletions
@@ -7,6 +7,7 @@ namespace App\Modules\Inventory\Application\Contracts;
use App\Modules\Inventory\Application\DTOs\StockGroupSummary;
use App\Modules\Inventory\Application\DTOs\StockItemSummary;
use App\Modules\Inventory\Application\DTOs\StockMovementSummary;
use App\Modules\Inventory\Application\DTOs\TaskMaterialSuggestion;
/**
* Reads stock items, groups and movements. Other modules (machines, quick
@@ -44,6 +45,19 @@ interface StockQueries
public function findMovement(int $id): ?StockMovementSummary;
/**
* Material recorded on a task: its use, write-offs and their reversals,
* newest first. The balance after each is not set (0).
*
* @return list<StockMovementSummary>
*/
public function taskMovements(int $taskId): array;
/**
* @return list<TaskMaterialSuggestion>
*/
public function taskSuggestions(int $taskId): array;
/**
* The private-disk path of the item's image or its thumbnail.
*/
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\Contracts;
use App\Modules\Inventory\Application\DTOs\StockTaskContext;
/**
* Inventory's view of job tasks, read from the Jobs and Catalog modules.
*/
interface StockTasks
{
public function context(int $taskId): ?StockTaskContext;
/**
* Labels such as "2026-0005 Messuvideo – Tulostus" by task id.
*
* @param list<int> $taskIds
* @return array<int, string>
*/
public function labels(array $taskIds): array;
}
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Modules\Inventory\Application\DTOs;
use App\Modules\Inventory\Domain\Enums\StockMovementKind;
use App\Modules\Inventory\Domain\Enums\StockUnit;
/**
* A stock movement in an item's history: signed quantity and the balance
@@ -35,6 +36,8 @@ final readonly class StockMovementSummary
public ?string $documentNumber,
public ?int $reversesMovementId,
public ?int $reversedByMovementId,
public string $itemName = '',
public ?StockUnit $unit = null,
) {}
public function canBeReversed(): bool
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\DTOs;
/**
* A job's task as stock sees it: whether material may be recorded on it
* (its service unit uses stock, spec 19) and whether its job is locked.
*/
final readonly class StockTaskContext
{
public function __construct(
public int $taskId,
public int $jobId,
public string $label,
public bool $usesStock,
public bool $isLocked,
) {}
}
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace App\Modules\Inventory\Application\DTOs;
use App\Modules\Inventory\Domain\Enums\StockUnit;
/**
* A default material suggested on a task, waiting to be confirmed.
*/
final readonly class TaskMaterialSuggestion
{
public function __construct(
public int $id,
public int $taskId,
public int $stockItemId,
public string $itemName,
public StockUnit $unit,
public int $quantityMilli,
) {}
}
@@ -0,0 +1,103 @@
<?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);
}
}