228 lines
8.5 KiB
PHP
228 lines
8.5 KiB
PHP
<?php
|
|
|
|
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
|
use App\Modules\Catalog\Application\DTOs\ServiceUnitNode;
|
|
use App\Modules\Catalog\Application\UseCases\MoveCatalogItem;
|
|
use App\Modules\Catalog\Application\UseCases\SetCatalogItemActive;
|
|
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
|
|
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
|
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Component;
|
|
|
|
/**
|
|
* Catalog management (pohjatiedot): service units, services and work types.
|
|
*/
|
|
new class extends Component
|
|
{
|
|
public bool $showInactive = true;
|
|
|
|
public string $statusMessage = '';
|
|
|
|
protected CatalogQueries $catalogQueries;
|
|
|
|
public function boot(CatalogQueries $catalogQueries): void
|
|
{
|
|
$this->catalogQueries = $catalogQueries;
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->authorize('catalog.manage');
|
|
}
|
|
|
|
/**
|
|
* @return list<ServiceUnitNode>
|
|
*/
|
|
#[Computed]
|
|
public function tree(): array
|
|
{
|
|
return $this->catalogQueries->tree($this->showInactive);
|
|
}
|
|
|
|
public function move(string $level, int $id, string $direction, MoveCatalogItem $moveItem): void
|
|
{
|
|
$this->authorize('catalog.manage');
|
|
|
|
$catalogLevel = CatalogLevel::from($level);
|
|
$moveDirection = MoveDirection::from($direction);
|
|
$name = $this->nameOf($catalogLevel, $id);
|
|
|
|
try {
|
|
$moved = $moveItem->execute($catalogLevel, $id, $moveDirection);
|
|
} catch (CatalogItemNotFound) {
|
|
$this->statusMessage = __('catalog.status.not_found');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->statusMessage = __(match (true) {
|
|
$moved && $moveDirection === MoveDirection::Up => 'catalog.status.moved_up',
|
|
$moved => 'catalog.status.moved_down',
|
|
$moveDirection === MoveDirection::Up => 'catalog.status.already_first',
|
|
default => 'catalog.status.already_last',
|
|
}, ['name' => $name]);
|
|
|
|
// The row moved in the DOM: keep focus on the same button.
|
|
$this->js("document.getElementById('move-{$direction}-{$level}-{$id}')?.focus()");
|
|
}
|
|
|
|
public function toggleActive(string $level, int $id, SetCatalogItemActive $setActive): void
|
|
{
|
|
$this->authorize('catalog.manage');
|
|
|
|
$catalogLevel = CatalogLevel::from($level);
|
|
$active = ! $this->isActive($catalogLevel, $id);
|
|
|
|
try {
|
|
$setActive->execute($catalogLevel, $id, $active);
|
|
} catch (CatalogItemNotFound) {
|
|
$this->statusMessage = __('catalog.status.not_found');
|
|
|
|
return;
|
|
}
|
|
|
|
$this->statusMessage = __($active ? 'catalog.status.activated' : 'catalog.status.deactivated', [
|
|
'name' => $this->nameOf($catalogLevel, $id),
|
|
]);
|
|
}
|
|
|
|
public function render(): mixed
|
|
{
|
|
return $this->view()->title(__('catalog.title'));
|
|
}
|
|
|
|
private function nameOf(CatalogLevel $level, int $id): string
|
|
{
|
|
return match ($level) {
|
|
CatalogLevel::ServiceUnit => $this->catalogQueries->findServiceUnit($id)?->name,
|
|
CatalogLevel::Service => $this->catalogQueries->findService($id)?->name,
|
|
CatalogLevel::WorkType => $this->catalogQueries->findWorkType($id)?->name,
|
|
} ?? '';
|
|
}
|
|
|
|
private function isActive(CatalogLevel $level, int $id): bool
|
|
{
|
|
return match ($level) {
|
|
CatalogLevel::ServiceUnit => $this->catalogQueries->findServiceUnit($id)?->isActive,
|
|
CatalogLevel::Service => $this->catalogQueries->findService($id)?->isActive,
|
|
CatalogLevel::WorkType => $this->catalogQueries->findWorkType($id)?->isActive,
|
|
} ?? false;
|
|
}
|
|
};
|
|
?>
|
|
|
|
<div class="mx-auto max-w-5xl space-y-6 p-4 sm:p-6">
|
|
<div class="flex flex-wrap items-center justify-between gap-4">
|
|
<h1 class="text-2xl font-semibold">{{ __('catalog.title') }}</h1>
|
|
|
|
<x-button :href="route('catalog.service-units.create')" wire:navigate icon="plus" :text="__('catalog.actions.add_service_unit')" />
|
|
</div>
|
|
|
|
<div class="space-y-1 text-sm">
|
|
<p class="text-zinc-600 dark:text-zinc-300">{{ __('catalog.intro') }}</p>
|
|
<p><x-link :href="route('catalog.prices')" navigate>{{ __('catalog.prices_link') }}</x-link></p>
|
|
<p><x-link :href="route('catalog.workflow-templates')" navigate>{{ __('catalog.workflows_link') }}</x-link></p>
|
|
</div>
|
|
|
|
@if (session('status'))
|
|
<x-alert role="status" color="green" light icon="check-circle" :text="session('status')" />
|
|
@endif
|
|
|
|
<x-checkbox id="show-inactive" wire:model.live="showInactive" :label="__('catalog.show_inactive')" />
|
|
|
|
<p role="status" aria-live="polite" class="text-sm">{{ $statusMessage }}</p>
|
|
|
|
@foreach ($this->tree as $node)
|
|
@php($unit = $node->serviceUnit)
|
|
|
|
<section
|
|
wire:key="service-unit-{{ $unit->id }}"
|
|
aria-labelledby="service-unit-{{ $unit->id }}-heading"
|
|
class="space-y-4 rounded-lg border border-zinc-200 p-4 dark:border-zinc-700"
|
|
>
|
|
<div class="flex flex-wrap items-center justify-between gap-3">
|
|
<h2 id="service-unit-{{ $unit->id }}-heading" class="text-xl font-semibold break-words">
|
|
{{ $unit->name }}
|
|
@unless ($unit->isActive)
|
|
<span class="ml-2 text-sm font-normal">({{ __('catalog.inactive') }})</span>
|
|
@endunless
|
|
</h2>
|
|
|
|
<x-catalog.item-actions
|
|
level="service_unit"
|
|
:id="$unit->id"
|
|
:name="$unit->name"
|
|
:active="$unit->isActive"
|
|
:edit-url="route('catalog.service-units.edit', $unit->id)"
|
|
/>
|
|
</div>
|
|
|
|
@if ($unit->isShared)
|
|
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.shared') }}</p>
|
|
@endif
|
|
|
|
@if ($unit->usesStock)
|
|
<p class="flex items-center gap-1 text-sm text-zinc-600 dark:text-zinc-300">
|
|
<x-icon name="archive-box" class="size-4" aria-hidden="true" />
|
|
{{ __('catalog.service_units.uses_stock_on') }}
|
|
</p>
|
|
@endif
|
|
|
|
@foreach ($node->services as $serviceNode)
|
|
@php($service = $serviceNode->service)
|
|
|
|
<div wire:key="service-{{ $service->id }}" class="space-y-2 border-l-2 border-zinc-200 pl-4 dark:border-zinc-700">
|
|
<div class="flex flex-wrap items-center justify-between gap-3">
|
|
<h3 class="text-lg font-medium break-words">
|
|
{{ $service->name }}
|
|
@unless ($service->isActive)
|
|
<span class="ml-2 text-sm font-normal">({{ __('catalog.inactive') }})</span>
|
|
@endunless
|
|
</h3>
|
|
|
|
<x-catalog.item-actions
|
|
level="service"
|
|
:id="$service->id"
|
|
:name="$service->name"
|
|
:active="$service->isActive"
|
|
:edit-url="route('catalog.services.edit', $service->id)"
|
|
/>
|
|
</div>
|
|
|
|
<x-catalog.work-type-list :work-types="$serviceNode->workTypes" />
|
|
</div>
|
|
@endforeach
|
|
|
|
@if ($node->workTypes !== [] || $node->services === [])
|
|
<x-catalog.work-type-list :work-types="$node->workTypes" />
|
|
@endif
|
|
|
|
<div class="flex flex-wrap gap-2">
|
|
@unless ($unit->isShared)
|
|
<x-button
|
|
:href="route('catalog.services.create', $unit->id)"
|
|
wire:navigate
|
|
subtle
|
|
color="black"
|
|
sm
|
|
icon="plus"
|
|
:text="__('catalog.actions.add_service')"
|
|
:aria-label="__('catalog.actions.add_service_to', ['name' => $unit->name])"
|
|
/>
|
|
@endunless
|
|
<x-button
|
|
:href="route('catalog.work-types.create', $unit->id)"
|
|
wire:navigate
|
|
subtle
|
|
color="black"
|
|
sm
|
|
icon="plus"
|
|
:text="__('catalog.actions.add_work_type')"
|
|
:aria-label="__('catalog.actions.add_work_type_to', ['name' => $unit->name])"
|
|
/>
|
|
</div>
|
|
</section>
|
|
@endforeach
|
|
</div>
|