From 361a2974cfb64902fe7d7b111eb70dc94ab67680 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Thu, 26 Nov 2026 16:01:59 +0200 Subject: [PATCH] Let a service unit use stock and machines Co-Authored-By: Claude Sonnet 5.5 --- .../Application/Contracts/CatalogQueries.php | 8 +++++ .../Application/DTOs/ServiceUnitSummary.php | 1 + .../UseCases/SetServiceUnitUsesStock.php | 36 +++++++++++++++++++ .../Catalog/Domain/Entities/ServiceUnit.php | 29 ++++++++++++--- .../Exceptions/SharedUnitCannotUseStock.php | 15 ++++++++ .../Persistence/Eloquent/ServiceUnitModel.php | 4 ++- .../Repositories/EloquentCatalogQueries.php | 14 +++++++- .../EloquentServiceUnitRepository.php | 3 +- .../Presentation/Livewire/⚡index.blade.php | 7 ++++ .../Livewire/⚡service-unit-form.blade.php | 24 +++++++++++-- database/factories/ServiceUnitFactory.php | 5 +++ ..._add_uses_stock_to_service_units_table.php | 26 ++++++++++++++ lang/en/catalog.php | 3 ++ lang/fi/catalog.php | 3 ++ tests/Fakes/FakeCatalogQueries.php | 9 +++-- tests/Feature/Modules/Catalog/CatalogTest.php | 30 ++++++++++++++++ 16 files changed, 204 insertions(+), 13 deletions(-) create mode 100644 app/Modules/Catalog/Application/UseCases/SetServiceUnitUsesStock.php create mode 100644 app/Modules/Catalog/Domain/Exceptions/SharedUnitCannotUseStock.php create mode 100644 database/migrations/2026_11_26_140030_add_uses_stock_to_service_units_table.php diff --git a/app/Modules/Catalog/Application/Contracts/CatalogQueries.php b/app/Modules/Catalog/Application/Contracts/CatalogQueries.php index f974156..73263a0 100644 --- a/app/Modules/Catalog/Application/Contracts/CatalogQueries.php +++ b/app/Modules/Catalog/Application/Contracts/CatalogQueries.php @@ -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 + */ + public function stockServiceUnitIds(): array; + public function findServiceUnit(int $id): ?ServiceUnitSummary; /** diff --git a/app/Modules/Catalog/Application/DTOs/ServiceUnitSummary.php b/app/Modules/Catalog/Application/DTOs/ServiceUnitSummary.php index 7211730..692ead6 100644 --- a/app/Modules/Catalog/Application/DTOs/ServiceUnitSummary.php +++ b/app/Modules/Catalog/Application/DTOs/ServiceUnitSummary.php @@ -11,5 +11,6 @@ final readonly class ServiceUnitSummary public string $name, public bool $isShared, public bool $isActive, + public bool $usesStock = false, ) {} } diff --git a/app/Modules/Catalog/Application/UseCases/SetServiceUnitUsesStock.php b/app/Modules/Catalog/Application/UseCases/SetServiceUnitUsesStock.php new file mode 100644 index 0000000..b34644c --- /dev/null +++ b/app/Modules/Catalog/Application/UseCases/SetServiceUnitUsesStock.php @@ -0,0 +1,36 @@ +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); + }); + } +} diff --git a/app/Modules/Catalog/Domain/Entities/ServiceUnit.php b/app/Modules/Catalog/Domain/Entities/ServiceUnit.php index 8d40943..75ec21d 100644 --- a/app/Modules/Catalog/Domain/Entities/ServiceUnit.php +++ b/app/Modules/Catalog/Domain/Entities/ServiceUnit.php @@ -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; + } } diff --git a/app/Modules/Catalog/Domain/Exceptions/SharedUnitCannotUseStock.php b/app/Modules/Catalog/Domain/Exceptions/SharedUnitCannotUseStock.php new file mode 100644 index 0000000..3810f6e --- /dev/null +++ b/app/Modules/Catalog/Domain/Exceptions/SharedUnitCannotUseStock.php @@ -0,0 +1,15 @@ + 'boolean', + 'uses_stock' => 'boolean', 'is_active' => 'boolean', 'sort_order' => 'integer', ]; diff --git a/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentCatalogQueries.php b/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentCatalogQueries.php index ae2b7e8..46e172a 100644 --- a/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentCatalogQueries.php +++ b/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentCatalogQueries.php @@ -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 diff --git a/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentServiceUnitRepository.php b/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentServiceUnitRepository.php index e946181..4dc650e 100644 --- a/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentServiceUnitRepository.php +++ b/app/Modules/Catalog/Infrastructure/Persistence/Repositories/EloquentServiceUnitRepository.php @@ -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); } } diff --git a/app/Modules/Catalog/Presentation/Livewire/⚡index.blade.php b/app/Modules/Catalog/Presentation/Livewire/⚡index.blade.php index f9c2e2c..c3966b2 100644 --- a/app/Modules/Catalog/Presentation/Livewire/⚡index.blade.php +++ b/app/Modules/Catalog/Presentation/Livewire/⚡index.blade.php @@ -162,6 +162,13 @@ new class extends Component

{{ __('catalog.shared') }}

@endif + @if ($unit->usesStock) +

+

+ @endif + @foreach ($node->services as $serviceNode) @php($service = $serviceNode->service) diff --git a/app/Modules/Catalog/Presentation/Livewire/⚡service-unit-form.blade.php b/app/Modules/Catalog/Presentation/Livewire/⚡service-unit-form.blade.php index 3586269..ef081bf 100644 --- a/app/Modules/Catalog/Presentation/Livewire/⚡service-unit-form.blade.php +++ b/app/Modules/Catalog/Presentation/Livewire/⚡service-unit-form.blade.php @@ -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
+ @unless ($isShared) +
+ +

{{ __('catalog.service_units.uses_stock_hint') }}

+
+ @endunless +
diff --git a/database/factories/ServiceUnitFactory.php b/database/factories/ServiceUnitFactory.php index d4e687b..b300022 100644 --- a/database/factories/ServiceUnitFactory.php +++ b/database/factories/ServiceUnitFactory.php @@ -25,6 +25,11 @@ class ServiceUnitFactory extends Factory ]; } + public function usingStock(): static + { + return $this->state(fn (): array => ['uses_stock' => true]); + } + public function inactive(): static { return $this->state(fn (): array => ['is_active' => false]); diff --git a/database/migrations/2026_11_26_140030_add_uses_stock_to_service_units_table.php b/database/migrations/2026_11_26_140030_add_uses_stock_to_service_units_table.php new file mode 100644 index 0000000..4ae0280 --- /dev/null +++ b/database/migrations/2026_11_26_140030_add_uses_stock_to_service_units_table.php @@ -0,0 +1,26 @@ +boolean('uses_stock')->default(false)->after('is_shared'); + }); + } + + public function down(): void + { + Schema::table('service_units', function (Blueprint $table) { + $table->dropColumn('uses_stock'); + }); + } +}; diff --git a/lang/en/catalog.php b/lang/en/catalog.php index 58e4457..7423e63 100644 --- a/lang/en/catalog.php +++ b/lang/en/catalog.php @@ -91,6 +91,9 @@ return [ 'service_units' => [ 'create_title' => 'New service unit', 'edit_title' => 'Edit service unit', + 'uses_stock' => 'Use stock and machines', + 'uses_stock_hint' => 'When on, tasks in the unit show Used material and Move to machine, and machines can be chosen for the unit. Turning it off hides the features, but recorded data is kept.', + 'uses_stock_on' => 'Stock and machines in use', ], 'services' => [ diff --git a/lang/fi/catalog.php b/lang/fi/catalog.php index f08148e..71ad054 100644 --- a/lang/fi/catalog.php +++ b/lang/fi/catalog.php @@ -91,6 +91,9 @@ return [ 'service_units' => [ 'create_title' => 'Uusi palveluyksikkö', 'edit_title' => 'Muokkaa palveluyksikköä', + 'uses_stock' => 'Varaston ja koneiden käyttö', + 'uses_stock_hint' => 'Kun asetus on päällä, yksikön tehtävillä näkyvät Käytetty materiaali ja Siirrä koneelle, ja yksikölle voi valita koneita. Poisto piilottaa toiminnot, mutta kirjatut tiedot säilyvät.', + 'uses_stock_on' => 'Varasto ja koneet käytössä', ], 'services' => [ diff --git a/tests/Fakes/FakeCatalogQueries.php b/tests/Fakes/FakeCatalogQueries.php index 4a582bb..7f2bf4d 100644 --- a/tests/Fakes/FakeCatalogQueries.php +++ b/tests/Fakes/FakeCatalogQueries.php @@ -24,9 +24,14 @@ class FakeCatalogQueries implements CatalogQueries $this->workTypes[$serviceUnitId][] = new WorkTypeSummary($id, $serviceUnitId, null, $name, true); } - public function addServiceUnit(int $id, string $name, bool $active = true, bool $shared = false): void + public function addServiceUnit(int $id, string $name, bool $active = true, bool $shared = false, bool $usesStock = false): void { - $this->serviceUnits[$id] = new ServiceUnitSummary($id, $name, $shared, $active); + $this->serviceUnits[$id] = new ServiceUnitSummary($id, $name, $shared, $active, $usesStock); + } + + public function stockServiceUnitIds(): array + { + return array_values(array_keys(array_filter($this->serviceUnits, fn (ServiceUnitSummary $unit): bool => $unit->isActive && $unit->usesStock))); } public function tree(bool $includeInactive = true): array diff --git a/tests/Feature/Modules/Catalog/CatalogTest.php b/tests/Feature/Modules/Catalog/CatalogTest.php index 1758d20..02df79b 100644 --- a/tests/Feature/Modules/Catalog/CatalogTest.php +++ b/tests/Feature/Modules/Catalog/CatalogTest.php @@ -117,3 +117,33 @@ it('does not let the edited item be changed in the browser', function () { Livewire::test('catalog::work-type-form', ['serviceUnit' => ($this->unit)('Markkinointi')->id]) ->set('workTypeId', 1); })->throws(CannotUpdateLockedPropertyException::class); + +it('turns stock and machine features on for a service unit, but not for the shared unit', function () { + $print = ($this->unit)('Painotuotanto'); + $shared = ($this->unit)('Kaikille yhteiset'); + + Livewire::test('catalog::service-unit-form', ['serviceUnit' => $print->id]) + ->assertSee(__('catalog.service_units.uses_stock')) + ->set('usesStock', true) + ->call('save') + ->assertHasNoErrors(); + + expect($print->fresh()->uses_stock)->toBeTrue() + ->and(app(CatalogQueries::class)->stockServiceUnitIds())->toBe([$print->id]); + + Livewire::test('catalog::index')->assertSee(__('catalog.service_units.uses_stock_on')); + + Livewire::test('catalog::service-unit-form', ['serviceUnit' => $shared->id]) + ->assertDontSee(__('catalog.service_units.uses_stock')) + ->set('usesStock', true) + ->call('save'); + + expect($shared->fresh()->uses_stock)->toBeFalse(); + + Livewire::test('catalog::service-unit-form', ['serviceUnit' => $print->id]) + ->assertSet('usesStock', true) + ->set('usesStock', false) + ->call('save'); + + expect(app(CatalogQueries::class)->stockServiceUnitIds())->toBe([]); +});