Files
Toiminnanohjaus/app/Modules/Machines/Infrastructure/Adapters/JobsForMachines.php
T
2026-11-26 16:43:55 +02:00

124 lines
4.0 KiB
PHP

<?php
namespace App\Modules\Machines\Infrastructure\Adapters;
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Jobs\Application\Contracts\CustomerJobQueries;
use App\Modules\Jobs\Application\Contracts\TaskQueries;
use App\Modules\Jobs\Application\DTOs\CustomerJobDetails;
use App\Modules\Jobs\Application\UseCases\ChangeTaskStatus;
use App\Modules\Jobs\Domain\Enums\Priority;
use App\Modules\Jobs\Domain\Enums\TaskStatus;
use App\Modules\Machines\Application\Contracts\MachineTasks;
use App\Modules\Machines\Application\DTOs\MachineTaskInfo;
/**
* Reads tasks, their jobs and the service units of their work types from
* the Jobs and Catalog modules, and changes task statuses through Jobs.
*/
class JobsForMachines implements MachineTasks
{
/** @var array<int, CustomerJobDetails|null> */
private array $jobCache = [];
public function __construct(
private TaskQueries $tasks,
private CustomerJobQueries $jobs,
private CatalogQueries $catalog,
private ChangeTaskStatus $changeTaskStatus,
) {}
public function info(int $taskId): ?MachineTaskInfo
{
return $this->infos([$taskId])[$taskId] ?? null;
}
public function infos(array $taskIds): array
{
if ($taskIds === []) {
return [];
}
$stockUnitIds = $this->catalog->stockServiceUnitIds();
$infos = [];
foreach ($this->tasks->bookableTasks($taskIds) as $bookable) {
$task = $this->tasks->findTask($bookable->jobId, $bookable->taskId);
$job = $this->job($bookable->jobId);
if ($task === null || $job === null) {
continue;
}
$infos[$task->id] = new MachineTaskInfo(
taskId: $task->id,
jobId: $job->id,
jobNumber: $job->number,
jobName: $job->name,
customerName: $job->customer->name,
title: $task->title,
description: $task->description,
instructions: $job->instructions,
status: $task->status->value,
priority: $job->priority->value,
priorityRank: self::rank($job->priority),
dueOn: $task->dueOn ?? $job->dueOn,
plannedFrom: $task->plannedFrom,
plannedTo: $task->plannedTo,
estimatedHours: $task->estimatedHours,
allowedServiceUnitIds: $this->allowedUnits($bookable->workTypeId, $job, $stockUnitIds),
isDone: $task->status === TaskStatus::Done,
);
}
return $infos;
}
public function changeStatus(int $taskId, string $status): void
{
$task = $this->tasks->bookableTask($taskId);
if ($task !== null) {
$this->changeTaskStatus->execute($task->jobId, $taskId, TaskStatus::from($status));
}
}
/**
* @param list<int> $stockUnitIds
* @return list<int>
*/
private function allowedUnits(int $workTypeId, CustomerJobDetails $job, array $stockUnitIds): array
{
$workType = $this->catalog->findWorkType($workTypeId);
$unit = $workType === null ? null : $this->catalog->findServiceUnit($workType->serviceUnitId);
if ($unit === null) {
return [];
}
$unitIds = $unit->isShared ? array_map(fn ($jobUnit): int => $jobUnit->id, $job->serviceUnits) : [$unit->id];
return array_values(array_intersect($unitIds, $stockUnitIds));
}
private function job(int $jobId): ?CustomerJobDetails
{
if (! array_key_exists($jobId, $this->jobCache)) {
$this->jobCache[$jobId] = $this->jobs->findDetails($jobId);
}
return $this->jobCache[$jobId];
}
private static function rank(Priority $priority): int
{
return match ($priority) {
Priority::Immediate => 4,
Priority::Urgent => 3,
Priority::First => 2,
Priority::Normal => 1,
Priority::Waiting => 0,
};
}
}