Files
Toiminnanohjaus/app/Modules/Inventory/Infrastructure/Adapters/TasksForStock.php
T

71 lines
2.1 KiB
PHP

<?php
namespace App\Modules\Inventory\Infrastructure\Adapters;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Inventory\Application\Contracts\StockTasks;
use App\Modules\Inventory\Application\DTOs\StockTaskContext;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\DTOs\BookableTask;
/**
* Reads tasks from Jobs and the stock setting of their service units from
* Catalog. A task's work type decides: its unit must use stock, and a
* shared work type's task follows the job's units (spec 19).
*/
class TasksForStock implements StockTasks
{
public function __construct(
private TaskQueries $tasks,
private CustomerJobQueries $jobs,
private CatalogQueries $catalog,
) {}
public function context(int $taskId): ?StockTaskContext
{
$task = $this->tasks->bookableTask($taskId);
if ($task === null) {
return null;
}
return new StockTaskContext($task->taskId, $task->jobId, $task->label(), $this->usesStock($task), $task->isLocked());
}
public function labels(array $taskIds): array
{
$labels = [];
foreach ($this->tasks->bookableTasks($taskIds) as $task) {
$labels[$task->taskId] = $task->label();
}
return $labels;
}
private function usesStock(BookableTask $task): bool
{
$stockUnitIds = $this->catalog->stockServiceUnitIds();
if ($stockUnitIds === []) {
return false;
}
$workType = $this->catalog->findWorkType($task->workTypeId);
$unit = $workType === null ? null : $this->catalog->findServiceUnit($workType->serviceUnitId);
if ($unit === null) {
return false;
}
if (! $unit->isShared) {
return in_array($unit->id, $stockUnitIds, true);
}
$jobUnitIds = array_map(fn ($unit): int => $unit->id, $this->jobs->findDetails($task->jobId)->serviceUnits ?? []);
return array_intersect($jobUnitIds, $stockUnitIds) !== [];
}
}