Let a service unit use stock and machines

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:01:59 +02:00
co-authored by Claude Sonnet 5.5
parent d160854b79
commit 361a2974cf
16 changed files with 204 additions and 13 deletions
@@ -38,6 +38,14 @@ interface CatalogQueries
*/
public function activeWorkTypesFor(array $serviceUnitIds): array;
/**
* Active service units where stock and machine features are in use
* (spec 19), in catalog order.
*
* @return list<int>
*/
public function stockServiceUnitIds(): array;
public function findServiceUnit(int $id): ?ServiceUnitSummary;
/**
@@ -11,5 +11,6 @@ final readonly class ServiceUnitSummary
public string $name,
public bool $isShared,
public bool $isActive,
public bool $usesStock = false,
) {}
}
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Application\UseCases;
use App\Modules\Catalog\Domain\Contracts\ServiceUnitRepository;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Domain\Exceptions\SharedUnitCannotUseStock;
use App\Modules\Shared\Application\Contracts\TransactionManager;
/**
* Turns the stock and machine features on or off in a service unit. Turning
* them off hides the features; recorded movements and work lists stay.
*/
final class SetServiceUnitUsesStock
{
public function __construct(
private ServiceUnitRepository $serviceUnits,
private TransactionManager $transactions,
) {}
/**
* @throws CatalogItemNotFound
* @throws SharedUnitCannotUseStock
*/
public function execute(int $serviceUnitId, bool $usesStock): void
{
$this->transactions->run(function () use ($serviceUnitId, $usesStock): void {
$unit = $this->serviceUnits->findById($serviceUnitId) ?? throw new CatalogItemNotFound(CatalogLevel::ServiceUnit, $serviceUnitId);
$unit->useStock($usesStock);
$this->serviceUnits->save($unit);
});
}
}
@@ -4,32 +4,51 @@ declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Entities;
use App\Modules\Catalog\Domain\Exceptions\SharedUnitCannotUseStock;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
/**
* A service unit (palveluyksikkö), e.g. Mediatuotanto. The shared unit
* ("Kaikille yhteiset") holds work types offered in every unit and has no
* services.
* services. A unit may use stock and machines (varaston ja koneiden
* käyttö); the shared unit follows the job's units instead.
*/
final class ServiceUnit extends CatalogItem
{
private function __construct(?int $id, CatalogName $name, private bool $shared, bool $active, int $sortOrder)
private function __construct(?int $id, CatalogName $name, private bool $shared, bool $active, int $sortOrder, private bool $usesStock)
{
parent::__construct($id, $name, $active, $sortOrder);
}
public static function create(CatalogName $name, int $sortOrder, bool $shared = false): self
{
return new self(null, $name, $shared, true, $sortOrder);
return new self(null, $name, $shared, true, $sortOrder, false);
}
public static function reconstitute(int $id, CatalogName $name, bool $shared, bool $active, int $sortOrder): self
public static function reconstitute(int $id, CatalogName $name, bool $shared, bool $active, int $sortOrder, bool $usesStock = false): self
{
return new self($id, $name, $shared, $active, $sortOrder);
return new self($id, $name, $shared, $active, $sortOrder, $usesStock);
}
public function isShared(): bool
{
return $this->shared;
}
public function usesStock(): bool
{
return $this->usesStock;
}
/**
* @throws SharedUnitCannotUseStock
*/
public function useStock(bool $usesStock): void
{
if ($usesStock && $this->shared) {
throw new SharedUnitCannotUseStock;
}
$this->usesStock = $usesStock;
}
}
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\Modules\Catalog\Domain\Exceptions;
use App\Modules\Shared\Domain\Exceptions\DomainException;
final class SharedUnitCannotUseStock extends DomainException
{
public function __construct()
{
parent::__construct('The shared service unit follows the job\'s units and cannot use stock by itself.');
}
}
@@ -15,11 +15,12 @@ use Illuminate\Database\Eloquent\Model;
* @property int $id
* @property string $name
* @property bool $is_shared
* @property bool $uses_stock
* @property bool $is_active
* @property int $sort_order
*/
#[Table('service_units')]
#[Fillable(['name', 'is_shared', 'is_active', 'sort_order'])]
#[Fillable(['name', 'is_shared', 'uses_stock', 'is_active', 'sort_order'])]
#[UseFactory(ServiceUnitFactory::class)]
class ServiceUnitModel extends Model
{
@@ -33,6 +34,7 @@ class ServiceUnitModel extends Model
{
return [
'is_shared' => 'boolean',
'uses_stock' => 'boolean',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
@@ -42,6 +42,18 @@ class EloquentCatalogQueries implements CatalogQueries
return $this->buildTree($units, includeInactive: false);
}
public function stockServiceUnitIds(): array
{
return ServiceUnitModel::query()
->where('uses_stock', true)
->where('is_active', true)
->orderBy('sort_order')
->pluck('id')
->map(fn ($id): int => (int) $id)
->values()
->all();
}
public function findServiceUnit(int $id): ?ServiceUnitSummary
{
$model = ServiceUnitModel::query()->find($id);
@@ -139,7 +151,7 @@ class EloquentCatalogQueries implements CatalogQueries
private function toServiceUnitSummary(ServiceUnitModel $model): ServiceUnitSummary
{
return new ServiceUnitSummary($model->id, $model->name, $model->is_shared, $model->is_active);
return new ServiceUnitSummary($model->id, $model->name, $model->is_shared, $model->is_active, $model->uses_stock);
}
private function toServiceSummary(ServiceModel $model): ServiceSummary
@@ -24,6 +24,7 @@ class EloquentServiceUnitRepository implements ServiceUnitRepository
$model->fill([
'name' => $serviceUnit->name()->value,
'is_shared' => $serviceUnit->isShared(),
'uses_stock' => $serviceUnit->usesStock(),
'is_active' => $serviceUnit->isActive(),
'sort_order' => $serviceUnit->sortOrder(),
])->save();
@@ -56,6 +57,6 @@ class EloquentServiceUnitRepository implements ServiceUnitRepository
private function toEntity(ServiceUnitModel $model): ServiceUnit
{
return ServiceUnit::reconstitute($model->id, new CatalogName($model->name), $model->is_shared, $model->is_active, $model->sort_order);
return ServiceUnit::reconstitute($model->id, new CatalogName($model->name), $model->is_shared, $model->is_active, $model->sort_order, $model->uses_stock);
}
}
@@ -162,6 +162,13 @@ new class extends Component
<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)
@@ -3,6 +3,7 @@
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\UseCases\AddServiceUnit;
use App\Modules\Catalog\Application\UseCases\RenameCatalogItem;
use App\Modules\Catalog\Application\UseCases\SetServiceUnitUsesStock;
use App\Modules\Catalog\Domain\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Presentation\Livewire\Forms\CatalogItemForm;
@@ -11,7 +12,8 @@ use Livewire\Attributes\Locked;
use Livewire\Component;
/**
* Add page (no {serviceUnit}) and edit page (with {serviceUnit}) of a service unit.
* Add page (no {serviceUnit}) and edit page (with {serviceUnit}) of a service unit:
* its name and whether stock and machine features are used in it (spec 19).
*/
new class extends Component
{
@@ -21,6 +23,11 @@ new class extends Component
#[Locked]
public string $unitName = '';
#[Locked]
public bool $isShared = false;
public bool $usesStock = false;
public CatalogItemForm $form;
public function mount(CatalogQueries $catalogQueries, ?int $serviceUnit = null): void
@@ -37,9 +44,11 @@ new class extends Component
$this->serviceUnitId = $summary->id;
$this->form->name = $summary->name;
$this->isShared = $summary->isShared;
$this->usesStock = $summary->usesStock;
}
public function save(AddServiceUnit $addServiceUnit, RenameCatalogItem $renameItem): void
public function save(AddServiceUnit $addServiceUnit, RenameCatalogItem $renameItem, SetServiceUnitUsesStock $setUsesStock): void
{
$this->authorize('catalog.manage');
@@ -47,10 +56,12 @@ new class extends Component
try {
if ($this->serviceUnitId === null) {
$addServiceUnit->execute($name);
$id = $addServiceUnit->execute($name);
$setUsesStock->execute($id, $this->usesStock);
session()->flash('status', __('catalog.status.created', ['name' => trim($name)]));
} else {
$renameItem->execute(CatalogLevel::ServiceUnit, $this->serviceUnitId, $name);
$setUsesStock->execute($this->serviceUnitId, $this->usesStock && ! $this->isShared);
session()->flash('status', __('catalog.status.updated', ['name' => trim($name)]));
}
} catch (CatalogItemNotFound) {
@@ -85,6 +96,13 @@ new class extends Component
<form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.name" :label="__('catalog.fields.name')" autocomplete="off" required />
@unless ($isShared)
<div class="space-y-1">
<x-checkbox id="uses-stock" wire:model="usesStock" :label="__('catalog.service_units.uses_stock')" />
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.service_units.uses_stock_hint') }}</p>
</div>
@endunless
<div class="flex flex-wrap gap-3">
<x-button submit :text="__('catalog.save')" />
<x-button :href="route('catalog.index')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />