- Dark indigo header and tinted page background; the mobile main menu gets its own row, in the same order as the keyboard reaches it. - Page titles with an icon chip (x-page-title, x-page, x-page-header) and card sections (x-panel, the card utility); x-table styled as a card with an indigo header row in AppServiceProvider::styleTables(). - Job and task statuses, late work, unapproved hours and stock states as badges with icon and text (x-status-badge, x-jobs.status, x-jobs.task-status), never colour alone. - Empty lists as x-empty-state; dashboard quick link tiles, week hour bars and trend arrows; Kanban columns and task cards edged by status; calendar today column and bar accents; quick sale, reports and machine console restyled; signed-out pages show the app name. - Focus rings use the primary colour in both themes (white in the header). - Document the components in CLAUDE.md; test the status badges. Co-Authored-By: Claude Opus 5.5 <[email protected]>
368 lines
17 KiB
PHP
368 lines
17 KiB
PHP
<?php
|
|
|
|
use App\Modules\Machines\Application\Contracts\MachineQueries;
|
|
use App\Modules\Machines\Application\Contracts\MaintenanceQueries;
|
|
use App\Modules\Machines\Application\DTOs\MachineSummary;
|
|
use App\Modules\Machines\Application\DTOs\MaintenanceItemStatus;
|
|
use App\Modules\Machines\Application\DTOs\MaintenanceRecordSummary;
|
|
use App\Modules\Machines\Application\UseCases\MachineMaintenance;
|
|
use App\Modules\Machines\Domain\Exceptions\CounterNotFound;
|
|
use App\Modules\Machines\Domain\Exceptions\InvalidReading;
|
|
use App\Modules\Machines\Domain\Exceptions\MaintenanceItemNotFound;
|
|
use App\Modules\Machines\Domain\Exceptions\MaintenanceRecordCancelled;
|
|
use App\Modules\Machines\Domain\Exceptions\MaintenanceRecordNotFound;
|
|
use App\Modules\Machines\Presentation\Livewire\Concerns\DescribesMaintenance;
|
|
use Carbon\CarbonImmutable;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Attributes\Locked;
|
|
use Livewire\Component;
|
|
|
|
/**
|
|
* A machine's counters, maintenance items and maintenance history on its
|
|
* page (spec 19.5). Anyone using the machine records how much of a
|
|
* counter a job used (counters are cumulative) and maintenance done;
|
|
* management keeps the items and cancels mistaken records. The history is
|
|
* filtered by item and days. Without counters and maintenance items the
|
|
* sections are left out, except the add button for management.
|
|
*/
|
|
new class extends Component
|
|
{
|
|
use DescribesMaintenance;
|
|
|
|
#[Locked]
|
|
public int $machineId;
|
|
|
|
/**
|
|
* Usage typed, by counter id.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
public array $usage = [];
|
|
|
|
#[Locked]
|
|
public ?int $recordingItemId = null;
|
|
|
|
public string $recordNote = '';
|
|
|
|
public string $historyItem = '';
|
|
|
|
public string $historyFrom = '';
|
|
|
|
public string $historyTo = '';
|
|
|
|
#[Locked]
|
|
public ?int $cancelRecordId = null;
|
|
|
|
#[Locked]
|
|
public string $message = '';
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->authorize('machines.use');
|
|
abort_if(app(MachineQueries::class)->findMachine($this->machineId) === null, 404);
|
|
}
|
|
|
|
#[Computed]
|
|
public function machine(): MachineSummary
|
|
{
|
|
return app(MachineQueries::class)->findMachine($this->machineId) ?? abort(404);
|
|
}
|
|
|
|
/**
|
|
* @return list<MaintenanceItemStatus>
|
|
*/
|
|
#[Computed]
|
|
public function items(): array
|
|
{
|
|
return app(MaintenanceQueries::class)->itemsOf($this->machineId, auth()->user()?->can('machines.manage') === true);
|
|
}
|
|
|
|
/**
|
|
* @return list<MaintenanceRecordSummary>
|
|
*/
|
|
#[Computed]
|
|
public function history(): array
|
|
{
|
|
return app(MaintenanceQueries::class)->history(
|
|
$this->machineId,
|
|
$this->historyItem === '' ? null : (int) $this->historyItem,
|
|
$this->validDay($this->historyFrom),
|
|
$this->validDay($this->historyTo),
|
|
);
|
|
}
|
|
|
|
public function updated(string $property): void
|
|
{
|
|
$this->authorize('machines.use');
|
|
|
|
if (str_starts_with($property, 'history')) {
|
|
unset($this->history);
|
|
}
|
|
}
|
|
|
|
public function saveUsage(int $counterId, MachineMaintenance $maintenance): void
|
|
{
|
|
$this->authorize('machines.use');
|
|
$this->resetErrorBag('usage.'.$counterId);
|
|
|
|
$counter = collect($this->machine->counters)->firstWhere('id', $counterId) ?? abort(404);
|
|
$usage = $this->usage[$counterId] ?? '';
|
|
|
|
try {
|
|
$milli = $maintenance->recordUsage($counterId, $usage, (int) auth()->id());
|
|
} catch (InvalidReading) {
|
|
$this->addError('usage.'.$counterId, __('machines.maintenance.errors.usage'));
|
|
|
|
return;
|
|
} catch (CounterNotFound) {
|
|
abort(404);
|
|
}
|
|
|
|
$this->message = $this->usageSaved($counter, $usage, $milli);
|
|
unset($this->usage[$counterId], $this->machine, $this->items);
|
|
}
|
|
|
|
public function startRecord(int $itemId): void
|
|
{
|
|
$this->authorize('machines.use');
|
|
|
|
$this->recordingItemId = $this->item($itemId)->id;
|
|
$this->recordNote = '';
|
|
$this->resetErrorBag();
|
|
$this->js("setTimeout(() => document.getElementById('record-note')?.focus())");
|
|
}
|
|
|
|
public function cancelRecording(): void
|
|
{
|
|
$itemId = $this->recordingItemId;
|
|
$this->recordingItemId = null;
|
|
$this->js("document.getElementById('item-{$itemId}-done')?.focus()");
|
|
}
|
|
|
|
public function record(MachineMaintenance $maintenance): void
|
|
{
|
|
$this->authorize('machines.use');
|
|
|
|
$item = $this->item((int) $this->recordingItemId);
|
|
|
|
try {
|
|
$result = $maintenance->recordMaintenance($item->id, (int) auth()->id(), $this->recordNote);
|
|
} catch (MaintenanceItemNotFound) {
|
|
abort(404);
|
|
}
|
|
|
|
$this->message = __('machines.maintenance.recorded', ['name' => $item->name])
|
|
.($result['stockRecorded'] ? '' : ' '.__('machines.maintenance.stock_not_recorded'));
|
|
$this->recordingItemId = null;
|
|
unset($this->items, $this->history, $this->machine);
|
|
$this->js("document.getElementById('maintenance-status')?.focus()");
|
|
}
|
|
|
|
public function confirmCancel(int $recordId): void
|
|
{
|
|
$this->authorize('machines.manage');
|
|
|
|
$this->cancelRecordId = $recordId;
|
|
$this->js("document.getElementById('cancel-record-dialog').showModal()");
|
|
}
|
|
|
|
public function cancelRecord(MachineMaintenance $maintenance): void
|
|
{
|
|
$this->authorize('machines.manage');
|
|
|
|
$record = collect($this->history)->firstWhere('id', $this->cancelRecordId) ?? abort(404);
|
|
|
|
try {
|
|
$maintenance->cancelRecord($record->id, (int) auth()->id());
|
|
$this->message = __('machines.maintenance.cancelled', ['name' => $record->itemName]);
|
|
} catch (MaintenanceRecordNotFound|MaintenanceRecordCancelled) {
|
|
$this->message = __('machines.errors.not_found');
|
|
}
|
|
|
|
$this->cancelRecordId = null;
|
|
unset($this->items, $this->history);
|
|
$this->js("document.getElementById('cancel-record-dialog').close(); document.getElementById('maintenance-status').focus()");
|
|
}
|
|
|
|
public function render(): mixed
|
|
{
|
|
return $this->view();
|
|
}
|
|
|
|
private function item(int $itemId): MaintenanceItemStatus
|
|
{
|
|
return collect($this->items)->firstWhere('id', $itemId) ?? abort(404);
|
|
}
|
|
|
|
private function validDay(string $value): ?string
|
|
{
|
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) !== 1) {
|
|
return null;
|
|
}
|
|
|
|
$date = CarbonImmutable::createFromFormat('!Y-m-d', $value);
|
|
|
|
return $date !== null && $date->format('Y-m-d') === $value ? $value : null;
|
|
}
|
|
};
|
|
?>
|
|
|
|
<div class="space-y-6">
|
|
<p id="maintenance-status" tabindex="-1" role="status" aria-live="polite" class="text-sm font-medium focus:outline-none">{{ $message }}</p>
|
|
|
|
@if ($this->machine->counters !== [])
|
|
<section aria-labelledby="counters-heading" class="card space-y-3">
|
|
<h2 id="counters-heading" class="text-xl font-semibold">{{ __('machines.maintenance.counters') }}</h2>
|
|
<ul class="space-y-3">
|
|
@foreach ($this->machine->counters as $counter)
|
|
<li wire:key="counter-{{ $counter->id }}" class="flex flex-wrap items-end gap-3 rounded-lg border border-zinc-300 p-3 dark:border-zinc-600">
|
|
<div class="min-w-0 flex-1">
|
|
<p class="font-medium">{{ $counter->name }}</p>
|
|
<p class="text-sm">
|
|
@if ($counter->readingMilli === null)
|
|
{{ __('machines.maintenance.no_reading') }}
|
|
@else
|
|
{{ __('machines.maintenance.latest_reading', ['reading' => $this->reading($counter->readingMilli), 'unit' => __('machines.counter_units.'.$counter->unit->value), 'at' => $this->dateLabel((string) $counter->readAt)]) }}
|
|
@endif
|
|
</p>
|
|
</div>
|
|
<form wire:submit="saveUsage({{ $counter->id }})" class="flex flex-wrap items-end gap-2" novalidate>
|
|
<div class="w-40">
|
|
<x-input
|
|
id="usage-{{ $counter->id }}"
|
|
wire:model="usage.{{ $counter->id }}"
|
|
inputmode="decimal"
|
|
:label="__('machines.maintenance.usage', ['unit' => __('machines.counter_units.'.$counter->unit->value)])"
|
|
autocomplete="off"
|
|
/>
|
|
</div>
|
|
<x-button submit sm :text="__('machines.maintenance.save_usage')" :aria-label="__('machines.maintenance.save_usage_named', ['counter' => $counter->name])" />
|
|
</form>
|
|
</li>
|
|
@endforeach
|
|
</ul>
|
|
</section>
|
|
@endif
|
|
|
|
@php($hasMaintenance = $this->machine->counters !== [] || $this->items !== [])
|
|
|
|
@if ($hasMaintenance || auth()->user()?->can('machines.manage'))
|
|
<section aria-labelledby="maintenance-heading" class="card space-y-3">
|
|
<h2 id="maintenance-heading" class="text-xl font-semibold">{{ __('machines.maintenance.items') }}</h2>
|
|
@can('machines.manage')
|
|
<p><x-button :href="route('machines.maintenance.create', $machineId)" wire:navigate sm icon="plus" :text="__('machines.maintenance.add')" /></p>
|
|
@endcan
|
|
|
|
@if ($this->items === [])
|
|
<x-empty-state icon="cpu-chip">{{ __('machines.maintenance.none') }}</x-empty-state>
|
|
@else
|
|
<ul class="space-y-3">
|
|
@foreach ($this->items as $item)
|
|
<li wire:key="item-{{ $item->id }}" class="space-y-2 rounded-lg border border-zinc-300 p-3 dark:border-zinc-600">
|
|
<div class="flex flex-wrap items-start justify-between gap-2">
|
|
<div class="min-w-0 space-y-1">
|
|
<p class="font-medium break-words">{{ $item->name }}@unless ($item->isActive) ({{ __('machines.index.inactive') }})@endunless</p>
|
|
<p class="flex flex-wrap items-center gap-1 text-sm font-medium @if ($item->level->value === 'overdue') text-red-700 dark:text-red-400 @elseif ($item->level->value === 'approaching') text-amber-800 dark:text-amber-300 @endif">
|
|
<x-icon :name="$this->levelIcon($item)" class="size-4" aria-hidden="true" />
|
|
{{ $this->levelLabel($item) }}@if ($this->stateText($item) !== ''): {{ $this->stateText($item) }}@endif
|
|
</p>
|
|
<p class="text-sm text-zinc-700 dark:text-zinc-300">{{ __('machines.maintenance.interval_text', ['interval' => $this->intervalText($item)]) }}</p>
|
|
<p class="text-sm text-zinc-700 dark:text-zinc-300">
|
|
@if ($item->lastPerformedAt === null)
|
|
{{ __('machines.maintenance.never_done') }}
|
|
@else
|
|
{{ __('machines.maintenance.last_done', ['at' => $this->dateLabel($item->lastPerformedAt), 'name' => $item->lastPerformedByName]) }}
|
|
@endif
|
|
</p>
|
|
</div>
|
|
@can('machines.manage')
|
|
<x-link :href="route('machines.maintenance.edit', [$machineId, $item->id])" navigate class="text-sm">{{ __('machines.maintenance.edit_named', ['name' => $item->name]) }}</x-link>
|
|
@endcan
|
|
</div>
|
|
|
|
@if ($item->instructions !== null)
|
|
<details class="text-sm">
|
|
<summary class="cursor-pointer font-medium">{{ __('machines.maintenance.instructions') }}</summary>
|
|
<p class="mt-1 whitespace-pre-line">{{ $item->instructions }}</p>
|
|
</details>
|
|
@endif
|
|
|
|
@if ($item->isActive)
|
|
@if ($recordingItemId === $item->id)
|
|
<form wire:submit="record" class="flex flex-wrap items-end gap-2" novalidate>
|
|
<div class="min-w-48 flex-1">
|
|
<x-input id="record-note" wire:model="recordNote" :label="__('machines.maintenance.note')" autocomplete="off" maxlength="2000" />
|
|
</div>
|
|
<x-button submit sm icon="check" :text="__('machines.maintenance.confirm_done')" />
|
|
<x-button wire:click="cancelRecording" subtle color="black" sm :text="__('machines.task.cancel')" />
|
|
</form>
|
|
@else
|
|
<x-button id="item-{{ $item->id }}-done" wire:click="startRecord({{ $item->id }})" sm icon="wrench" :text="__('machines.maintenance.done')" :aria-label="__('machines.maintenance.done_named', ['name' => $item->name])" />
|
|
@endif
|
|
@endif
|
|
</li>
|
|
@endforeach
|
|
</ul>
|
|
@endif
|
|
</section>
|
|
@endif
|
|
|
|
@if ($hasMaintenance)
|
|
<section aria-labelledby="history-heading" class="card space-y-3">
|
|
<h2 id="history-heading" class="text-xl font-semibold">{{ __('machines.maintenance.history') }}</h2>
|
|
<div class="grid gap-3 sm:grid-cols-3">
|
|
{{-- TallStackUI's native select does not tie its label to the control, so aria-label names it. --}}
|
|
<x-select.native id="history-item" wire:model.live="historyItem" :label="__('machines.maintenance.history_item')" :aria-label="__('machines.maintenance.history_item')">
|
|
<option value="">{{ __('machines.maintenance.all_items') }}</option>
|
|
@foreach ($this->items as $item)
|
|
<option value="{{ $item->id }}">{{ $item->name }}</option>
|
|
@endforeach
|
|
</x-select.native>
|
|
<x-input id="history-from" wire:model.live="historyFrom" type="date" :label="__('machines.maintenance.from')" />
|
|
<x-input id="history-to" wire:model.live="historyTo" type="date" :label="__('machines.maintenance.to')" />
|
|
</div>
|
|
|
|
<p role="status" aria-live="polite" class="text-sm">{{ trans_choice('machines.maintenance.history_count', count($this->history), ['count' => count($this->history)]) }}</p>
|
|
|
|
@if ($this->history !== [])
|
|
<ul class="divide-y divide-zinc-200 rounded-lg border border-zinc-300 text-sm dark:divide-zinc-700 dark:border-zinc-600">
|
|
@foreach ($this->history as $record)
|
|
<li wire:key="record-{{ $record->id }}" class="flex flex-wrap items-center justify-between gap-2 p-3">
|
|
<div class="min-w-0">
|
|
<p class="font-medium">{{ $record->itemName }} · {{ $this->dateLabel($record->performedAt) }} · {{ $record->userName }}</p>
|
|
@if ($record->readingMilli !== null)
|
|
<p>{{ __('machines.maintenance.reading_was', ['reading' => $this->reading($record->readingMilli)]) }}</p>
|
|
@endif
|
|
@if ($record->note !== null)
|
|
<p class="whitespace-pre-line">{{ $record->note }}</p>
|
|
@endif
|
|
@if ($record->cancelledAt !== null)
|
|
<p class="flex items-center gap-1 font-medium">
|
|
<x-icon name="x-circle" class="size-4" aria-hidden="true" />
|
|
{{ __('machines.maintenance.cancelled_by', ['at' => $this->dateLabel($record->cancelledAt), 'name' => $record->cancelledByName]) }}
|
|
</p>
|
|
@endif
|
|
</div>
|
|
@if ($record->cancelledAt === null)
|
|
@can('machines.manage')
|
|
<x-button wire:click="confirmCancel({{ $record->id }})" subtle color="black" sm icon="x-mark" :text="__('machines.maintenance.cancel')" :aria-label="__('machines.maintenance.cancel_named', ['name' => $record->itemName, 'at' => $this->dateLabel($record->performedAt)])" />
|
|
@endcan
|
|
@endif
|
|
</li>
|
|
@endforeach
|
|
</ul>
|
|
@endif
|
|
</section>
|
|
@endif
|
|
|
|
@can('machines.manage')
|
|
<x-confirm-dialog id="cancel-record-dialog" :title="__('machines.maintenance.cancel_title')">
|
|
<p>{{ __('machines.maintenance.cancel_text') }}</p>
|
|
|
|
<x-slot:actions>
|
|
<x-button wire:click="cancelRecord" color="red" :text="__('machines.maintenance.cancel')" />
|
|
</x-slot:actions>
|
|
</x-confirm-dialog>
|
|
@endcan
|
|
</div>
|