Files
Toiminnanohjaus/app/Modules/Machines/Presentation/Livewire/⚡task-machine.blade.php
T
2026-11-26 16:43:55 +02:00

211 lines
8.6 KiB
PHP

<?php
use App\Modules\Machines\Application\Contracts\MachineQueries;
use App\Modules\Machines\Application\Contracts\MachineTasks;
use App\Modules\Machines\Application\DTOs\MachineSummary;
use App\Modules\Machines\Application\DTOs\MachineTaskInfo;
use App\Modules\Machines\Application\DTOs\QueueRow;
use App\Modules\Machines\Application\UseCases\MachineWorkList;
use App\Modules\Machines\Domain\Exceptions\InvalidPlannedDay;
use App\Modules\Machines\Domain\Exceptions\MachineInactive;
use App\Modules\Machines\Domain\Exceptions\MachineNotForTask;
use App\Modules\Machines\Domain\Exceptions\MachineNotFound;
use App\Modules\Machines\Domain\Exceptions\MachineTaskNotFound;
use App\Modules\Machines\Presentation\Livewire\Concerns\FormatsMachines;
use App\Modules\Users\Application\Contracts\Clock;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* A task's machine in its card on the job page (spec 19.4): "Move to
* machine" puts the task on a machine's work list for a day (by default
* the start of its schedule, else today); the machine and day can be
* changed later, and the task taken off. Shown only when the task's
* service unit uses stock, or when the task already is on a list.
*/
new class extends Component
{
use FormatsMachines;
#[Locked]
public int $jobId;
#[Locked]
public int $taskId;
#[Locked]
public string $taskTitle = '';
#[Locked]
public bool $editing = false;
public string $machineId = '';
public string $day = '';
#[Locked]
public string $message = '';
public function mount(): void
{
$this->authorize('jobs.view-job', $this->jobId);
abort_if($this->task !== null && $this->task->jobId !== $this->jobId, 404);
}
#[Computed]
public function task(): ?MachineTaskInfo
{
return app(MachineTasks::class)->info($this->taskId);
}
#[Computed]
public function row(): ?QueueRow
{
return app(MachineQueries::class)->rowForTask($this->taskId);
}
/**
* @return list<MachineSummary>
*/
#[Computed]
public function machines(): array
{
return $this->task === null ? [] : app(MachineQueries::class)->machinesFor($this->task->allowedServiceUnitIds);
}
public function machineName(int $machineId): string
{
return app(MachineQueries::class)->findMachine($machineId)->name ?? '';
}
public function canPlan(): bool
{
return $this->task !== null && ! $this->task->isDone && $this->task->allowedServiceUnitIds !== [] && auth()->user()?->can('machines.use') === true;
}
public function startEdit(): void
{
$this->authorizePlanning();
$today = app(Clock::class)->today()->format('Y-m-d');
$this->editing = true;
$this->machineId = (string) ($this->row->machineId ?? ($this->machines[0]->id ?? ''));
$this->day = $this->row !== null && ! $this->row->isLate
? $this->row->plannedOn
: ($this->task?->plannedFrom !== null && $this->task->plannedFrom > $today ? $this->task->plannedFrom : $today);
$this->resetErrorBag();
$this->js("setTimeout(() => document.getElementById('task-{$this->taskId}-machine')?.focus())");
}
public function cancel(): void
{
$this->editing = false;
$this->js("document.getElementById('task-{$this->taskId}-move')?.focus()");
}
public function save(MachineWorkList $workList): void
{
$this->authorizePlanning();
try {
$workList->planTask($this->taskId, (int) $this->machineId, $this->day);
} catch (InvalidPlannedDay) {
$this->addError('day', __('machines.errors.day'));
return;
} catch (MachineNotFound|MachineInactive|MachineNotForTask) {
$this->addError('machineId', __('machines.errors.machine'));
return;
} catch (MachineTaskNotFound) {
$this->message = __('machines.errors.task_done');
return;
}
$this->message = __('machines.task.moved', ['machine' => $this->machineName((int) $this->machineId), 'date' => $this->dateLabel($this->day)]);
$this->editing = false;
unset($this->row);
$this->js("document.getElementById('task-{$this->taskId}-machine-status')?.focus()");
}
public function remove(MachineWorkList $workList): void
{
$this->authorizePlanning();
$workList->removeTask($this->taskId);
$this->message = __('machines.task.removed');
unset($this->row);
$this->js("document.getElementById('task-{$this->taskId}-machine-status')?.focus()");
}
private function authorizePlanning(): void
{
$this->authorize('jobs.view-job', $this->jobId);
abort_unless($this->canPlan(), 403);
}
};
?>
<div>
@if ($this->canPlan() || $this->row !== null)
<section aria-label="{{ __('machines.task.list_of', ['title' => $taskTitle]) }}" class="space-y-2">
<h4 class="font-medium">{{ __('machines.task.title') }}</h4>
<p id="task-{{ $taskId }}-machine-status" tabindex="-1" role="status" aria-live="polite" class="text-sm focus:outline-none">{{ $message }}</p>
@if ($this->row !== null)
<p class="text-sm">
<x-link :href="route('machines.show', ['machine' => $this->row->machineId, 'paiva' => $this->row->plannedOn])" navigate>
{{ __('machines.task.on_machine', ['machine' => $this->machineName($this->row->machineId), 'date' => $this->dateLabel($this->row->plannedOn)]) }}
</x-link>
</p>
@if ($this->row->plannedOn < app(Clock::class)->today()->format('Y-m-d'))
<p class="flex items-center gap-1 text-sm font-medium text-red-700 dark:text-red-400">
<x-icon name="exclamation-triangle" class="size-4" aria-hidden="true" />
{{ __('machines.task.late') }}
</p>
@elseif ($this->task !== null && $this->task->isOutsidePlan($this->row->plannedOn))
<p class="flex items-center gap-1 text-sm">
<x-icon name="information-circle" class="size-4" aria-hidden="true" />
{{ __('machines.show.outside_plan') }}
</p>
@endif
@else
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('machines.task.not_on_machine') }}</p>
@endif
@if ($this->canPlan())
@if ($editing)
@if ($this->machines === [])
<p class="text-sm">{{ __('machines.task.no_machines') }}</p>
<x-button wire:click="cancel" subtle color="black" sm :text="__('machines.task.cancel')" />
@else
<form wire:submit="save" class="flex flex-wrap items-end gap-2" novalidate>
<div class="min-w-48">
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
<x-select.native id="task-{{ $taskId }}-machine" wire:model="machineId" :label="__('machines.task.machine')" :aria-label="__('machines.task.machine')">
@foreach ($this->machines as $machine)
<option value="{{ $machine->id }}">{{ $machine->name }}</option>
@endforeach
</x-select.native>
</div>
<x-input id="task-{{ $taskId }}-machine-day" wire:model="day" type="date" :label="__('machines.task.day')" :hint="__('machines.task.day_hint')" />
<x-button submit sm :text="__('machines.task.save')" />
<x-button wire:click="cancel" subtle color="black" sm :text="__('machines.task.cancel')" />
</form>
@endif
@else
<div class="flex flex-wrap gap-2">
<x-button id="task-{{ $taskId }}-move" wire:click="startEdit" subtle color="black" sm icon="printer" :text="$this->row === null ? __('machines.task.move') : __('machines.task.change')" />
@if ($this->row !== null)
<x-button wire:click="remove" subtle color="black" sm icon="x-mark" :text="__('machines.task.remove')" />
@endif
</div>
@endif
@endif
</section>
@endif
</div>