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; 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; public function findServiceUnit(int $id): ?ServiceUnitSummary;
/** /**
@@ -11,5 +11,6 @@ final readonly class ServiceUnitSummary
public string $name, public string $name,
public bool $isShared, public bool $isShared,
public bool $isActive, 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; namespace App\Modules\Catalog\Domain\Entities;
use App\Modules\Catalog\Domain\Exceptions\SharedUnitCannotUseStock;
use App\Modules\Catalog\Domain\ValueObjects\CatalogName; use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
/** /**
* A service unit (palveluyksikkö), e.g. Mediatuotanto. The shared unit * A service unit (palveluyksikkö), e.g. Mediatuotanto. The shared unit
* ("Kaikille yhteiset") holds work types offered in every unit and has no * ("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 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); parent::__construct($id, $name, $active, $sortOrder);
} }
public static function create(CatalogName $name, int $sortOrder, bool $shared = false): self 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 public function isShared(): bool
{ {
return $this->shared; 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 int $id
* @property string $name * @property string $name
* @property bool $is_shared * @property bool $is_shared
* @property bool $uses_stock
* @property bool $is_active * @property bool $is_active
* @property int $sort_order * @property int $sort_order
*/ */
#[Table('service_units')] #[Table('service_units')]
#[Fillable(['name', 'is_shared', 'is_active', 'sort_order'])] #[Fillable(['name', 'is_shared', 'uses_stock', 'is_active', 'sort_order'])]
#[UseFactory(ServiceUnitFactory::class)] #[UseFactory(ServiceUnitFactory::class)]
class ServiceUnitModel extends Model class ServiceUnitModel extends Model
{ {
@@ -33,6 +34,7 @@ class ServiceUnitModel extends Model
{ {
return [ return [
'is_shared' => 'boolean', 'is_shared' => 'boolean',
'uses_stock' => 'boolean',
'is_active' => 'boolean', 'is_active' => 'boolean',
'sort_order' => 'integer', 'sort_order' => 'integer',
]; ];
@@ -42,6 +42,18 @@ class EloquentCatalogQueries implements CatalogQueries
return $this->buildTree($units, includeInactive: false); 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 public function findServiceUnit(int $id): ?ServiceUnitSummary
{ {
$model = ServiceUnitModel::query()->find($id); $model = ServiceUnitModel::query()->find($id);
@@ -139,7 +151,7 @@ class EloquentCatalogQueries implements CatalogQueries
private function toServiceUnitSummary(ServiceUnitModel $model): ServiceUnitSummary 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 private function toServiceSummary(ServiceModel $model): ServiceSummary
@@ -24,6 +24,7 @@ class EloquentServiceUnitRepository implements ServiceUnitRepository
$model->fill([ $model->fill([
'name' => $serviceUnit->name()->value, 'name' => $serviceUnit->name()->value,
'is_shared' => $serviceUnit->isShared(), 'is_shared' => $serviceUnit->isShared(),
'uses_stock' => $serviceUnit->usesStock(),
'is_active' => $serviceUnit->isActive(), 'is_active' => $serviceUnit->isActive(),
'sort_order' => $serviceUnit->sortOrder(), 'sort_order' => $serviceUnit->sortOrder(),
])->save(); ])->save();
@@ -56,6 +57,6 @@ class EloquentServiceUnitRepository implements ServiceUnitRepository
private function toEntity(ServiceUnitModel $model): ServiceUnit 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> <p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('catalog.shared') }}</p>
@endif @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) @foreach ($node->services as $serviceNode)
@php($service = $serviceNode->service) @php($service = $serviceNode->service)
@@ -3,6 +3,7 @@
use App\Modules\Catalog\Application\Contracts\CatalogQueries; use App\Modules\Catalog\Application\Contracts\CatalogQueries;
use App\Modules\Catalog\Application\UseCases\AddServiceUnit; use App\Modules\Catalog\Application\UseCases\AddServiceUnit;
use App\Modules\Catalog\Application\UseCases\RenameCatalogItem; 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\Enums\CatalogLevel;
use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound; use App\Modules\Catalog\Domain\Exceptions\CatalogItemNotFound;
use App\Modules\Catalog\Presentation\Livewire\Forms\CatalogItemForm; use App\Modules\Catalog\Presentation\Livewire\Forms\CatalogItemForm;
@@ -11,7 +12,8 @@ use Livewire\Attributes\Locked;
use Livewire\Component; 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 new class extends Component
{ {
@@ -21,6 +23,11 @@ new class extends Component
#[Locked] #[Locked]
public string $unitName = ''; public string $unitName = '';
#[Locked]
public bool $isShared = false;
public bool $usesStock = false;
public CatalogItemForm $form; public CatalogItemForm $form;
public function mount(CatalogQueries $catalogQueries, ?int $serviceUnit = null): void public function mount(CatalogQueries $catalogQueries, ?int $serviceUnit = null): void
@@ -37,9 +44,11 @@ new class extends Component
$this->serviceUnitId = $summary->id; $this->serviceUnitId = $summary->id;
$this->form->name = $summary->name; $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'); $this->authorize('catalog.manage');
@@ -47,10 +56,12 @@ new class extends Component
try { try {
if ($this->serviceUnitId === null) { 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)])); session()->flash('status', __('catalog.status.created', ['name' => trim($name)]));
} else { } else {
$renameItem->execute(CatalogLevel::ServiceUnit, $this->serviceUnitId, $name); $renameItem->execute(CatalogLevel::ServiceUnit, $this->serviceUnitId, $name);
$setUsesStock->execute($this->serviceUnitId, $this->usesStock && ! $this->isShared);
session()->flash('status', __('catalog.status.updated', ['name' => trim($name)])); session()->flash('status', __('catalog.status.updated', ['name' => trim($name)]));
} }
} catch (CatalogItemNotFound) { } catch (CatalogItemNotFound) {
@@ -85,6 +96,13 @@ new class extends Component
<form wire:submit="save" class="space-y-6" novalidate> <form wire:submit="save" class="space-y-6" novalidate>
<x-input wire:model="form.name" :label="__('catalog.fields.name')" autocomplete="off" required /> <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"> <div class="flex flex-wrap gap-3">
<x-button submit :text="__('catalog.save')" /> <x-button submit :text="__('catalog.save')" />
<x-button :href="route('catalog.index')" wire:navigate subtle color="black" :text="__('catalog.cancel')" /> <x-button :href="route('catalog.index')" wire:navigate subtle color="black" :text="__('catalog.cancel')" />
@@ -25,6 +25,11 @@ class ServiceUnitFactory extends Factory
]; ];
} }
public function usingStock(): static
{
return $this->state(fn (): array => ['uses_stock' => true]);
}
public function inactive(): static public function inactive(): static
{ {
return $this->state(fn (): array => ['is_active' => false]); return $this->state(fn (): array => ['is_active' => false]);
@@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Whether stock and machine features are used in a service unit
* (spec 19: "Varaston ja koneiden käyttö").
*/
public function up(): void
{
Schema::table('service_units', function (Blueprint $table) {
$table->boolean('uses_stock')->default(false)->after('is_shared');
});
}
public function down(): void
{
Schema::table('service_units', function (Blueprint $table) {
$table->dropColumn('uses_stock');
});
}
};
+3
View File
@@ -91,6 +91,9 @@ return [
'service_units' => [ 'service_units' => [
'create_title' => 'New service unit', 'create_title' => 'New service unit',
'edit_title' => 'Edit 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' => [ 'services' => [
+3
View File
@@ -91,6 +91,9 @@ return [
'service_units' => [ 'service_units' => [
'create_title' => 'Uusi palveluyksikkö', 'create_title' => 'Uusi palveluyksikkö',
'edit_title' => 'Muokkaa 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' => [ 'services' => [
+7 -2
View File
@@ -24,9 +24,14 @@ class FakeCatalogQueries implements CatalogQueries
$this->workTypes[$serviceUnitId][] = new WorkTypeSummary($id, $serviceUnitId, null, $name, true); $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 public function tree(bool $includeInactive = true): array
@@ -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]) Livewire::test('catalog::work-type-form', ['serviceUnit' => ($this->unit)('Markkinointi')->id])
->set('workTypeId', 1); ->set('workTypeId', 1);
})->throws(CannotUpdateLockedPropertyException::class); })->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([]);
});