manager = User::factory()->management()->create(['name' => 'Maija Johto']); $this->employee = User::factory()->employee()->create(['name' => 'Eero Työntekijä']); $this->papers = StockItemGroupModel::factory()->create(['name' => 'Paperit', 'sort_order' => 1]); $this->paper = StockItemModel::factory()->create([ 'stock_item_group_id' => $this->papers->id, 'name' => 'Valokuvapaperi 10×15', 'unit' => 'sheet', 'code' => '6417000123', 'reorder_level_milli' => 20_000, ]); }); function purchaseStock(int $itemId, string $quantity, string $total = '', string $unitPrice = ''): mixed { return Livewire::test('inventory::item', ['item' => $itemId]) ->set('purchaseQuantity', $quantity) ->set('purchaseTotal', $total) ->set('purchaseUnitPrice', $unitPrice) ->call('recordPurchase'); } it('shows the stock menu link only when a service unit uses stock', function () { $this->actingAs($this->employee); $this->get(route('home'))->assertDontSee(__('layout.inventory')); ServiceUnitModel::factory()->usingStock()->create(); $this->get(route('home'))->assertSee(__('layout.inventory')); $this->get(route('inventory.index'))->assertOk()->assertSee('Valokuvapaperi 10×15'); $this->actingAs(User::factory()->subcontractor()->create()); $this->get(route('inventory.index'))->assertForbidden(); }); it('records purchases at a moving average cost and shows costs only to management', function () { $this->actingAs($this->manager); purchaseStock($this->paper->id, '100', total: '50,00')->assertHasNoErrors(); purchaseStock($this->paper->id, '100', unitPrice: '0,70') ->assertHasNoErrors() ->assertSet('message', fn (string $message) => str_contains($message, "Saldo nyt 200\u{00A0}arkki, keskihinta 0,60\u{00A0}€")); expect($this->paper->fresh()->average_cost_hundredths)->toBe(6_000) ->and(StockMovementModel::query()->where('kind', 'purchase')->sum('value_cents'))->toBe(12_000); Livewire::test('inventory::index')->assertSee(__('inventory.columns.average_cost'))->assertSee("120,00\u{00A0}€", false); $this->actingAs($this->employee); Livewire::test('inventory::index') ->assertSee("200\u{00A0}arkki", false) ->assertDontSee(__('inventory.columns.average_cost')) ->assertDontSee("120,00\u{00A0}€", false); Livewire::test('inventory::item', ['item' => $this->paper->id]) ->assertDontSee(__('inventory.purchase.title')) ->call('recordPurchase') ->assertForbidden(); }); it('requires a price and a positive quantity for a purchase', function () { $this->actingAs($this->manager); purchaseStock($this->paper->id, '10')->assertHasErrors('purchaseTotal'); purchaseStock($this->paper->id, '0', total: '5')->assertHasErrors('purchaseQuantity'); purchaseStock($this->paper->id, '10', unitPrice: '0,12345')->assertHasErrors('purchaseUnitPrice'); expect(StockMovementModel::query()->count())->toBe(0); }); it('lets anyone using stock record use with a reason, going negative with a warning, and undo it the same day', function () { $this->actingAs($this->employee); Livewire::test('inventory::item', ['item' => $this->paper->id]) ->set('useQuantity', '3') ->call('recordUse') ->assertHasErrors('useReason') ->set('useReason', 'Koeajo') ->call('recordUse') ->assertHasNoErrors() ->assertSee(__('inventory.status.negative')) ->assertSee(__('inventory.use.negative_warning')); $use = StockMovementModel::query()->sole(); expect($use->kind)->toBe(StockMovementKind::Consumption)->and($use->quantity_milli)->toBe(-3_000); Livewire::test('inventory::index', ['onlyReorder' => true])->assertSee('Valokuvapaperi 10×15'); Livewire::test('inventory::item', ['item' => $this->paper->id]) ->call('confirmReverse', $use->id) ->call('reverse') ->assertSet('message', fn (string $message) => str_contains($message, 'Vastakirjaus kirjattu')); expect(StockMovementModel::query()->where('reverses_movement_id', $use->id)->sole()->quantity_milli)->toBe(3_000); $this->travel(1)->day(); Livewire::test('inventory::item', ['item' => $this->paper->id])->call('confirmReverse', $use->id)->assertForbidden(); $other = StockMovementModel::query()->create([ 'stock_item_id' => $this->paper->id, 'kind' => 'consumption', 'quantity_milli' => -1000, 'unit_cost_hundredths' => 0, 'value_cents' => 0, 'occurred_on' => now()->toDateString(), 'recorded_at' => now(), 'user_id' => $this->manager->id, 'reason' => 'Testi', ]); Livewire::test('inventory::item', ['item' => $this->paper->id])->call('confirmReverse', $other->id)->assertForbidden(); }); it('records write-offs with a reason for management only', function () { $this->actingAs($this->manager); Livewire::test('inventory::item', ['item' => $this->paper->id]) ->set('writeOffQuantity', '2,5') ->call('recordWriteOff') ->assertHasErrors('writeOffReason') ->set('writeOffReason', 'Vaurioitunut') ->call('recordWriteOff') ->assertHasNoErrors(); expect(StockMovementModel::query()->sole()->only(['kind', 'quantity_milli', 'reason'])) ->toBe(['kind' => StockMovementKind::WriteOff, 'quantity_milli' => -2_500, 'reason' => 'Vaurioitunut']); }); it('does not let an item out of use be used', function () { $this->actingAs($this->manager); Livewire::test('inventory::item', ['item' => $this->paper->id]) ->call('toggleActive') ->assertSee(__('inventory.item.inactive_note')) ->assertDontSee(__('inventory.use.title')); Livewire::test('inventory::index')->assertDontSee('Valokuvapaperi 10×15'); Livewire::test('inventory::index')->set('includeInactive', true)->assertSee('Valokuvapaperi 10×15'); }); it('searches items by parts of the name or code', function () { $this->actingAs($this->employee); StockItemModel::factory()->create(['stock_item_group_id' => $this->papers->id, 'name' => 'Tarramateriaali kiiltävä']); Livewire::test('inventory::index') ->set('search', 'KIILTAVA') ->assertSee('Tarramateriaali kiiltävä') ->assertDontSee('Valokuvapaperi') ->set('search', '6417') ->assertSee('Valokuvapaperi'); }); it('adds and edits items and groups, with an image', function () { Storage::fake('local'); $this->actingAs($this->manager); Livewire::test('inventory::groups') ->set('newName', 'Musteet') ->call('add') ->assertHasNoErrors() ->set('newName', 'musteet') ->call('add') ->assertHasErrors('newName'); $inks = StockItemGroupModel::query()->where('name', 'Musteet')->sole(); Livewire::test('inventory::groups') ->call('move', $inks->id, 'up') ->assertSet('message', 'Musteet siirretty ylöspäin.') ->call('startRename', $inks->id) ->set('editingName', 'Musteet ja värit') ->call('rename') ->assertHasNoErrors(); expect($inks->fresh()->name)->toBe('Musteet ja värit') ->and($inks->fresh()->sort_order)->toBeLessThan($this->papers->fresh()->sort_order); Livewire::test('inventory::item-form') ->set('groupId', (string) $inks->id) ->set('name', 'Muste syaani') ->set('unit', 'litre') ->set('reorderLevel', '0,5') ->set('image', UploadedFile::fake()->image('muste.jpg', 300, 200)) ->call('save') ->assertHasNoErrors(); $ink = StockItemModel::query()->where('name', 'Muste syaani')->sole(); expect($ink->reorder_level_milli)->toBe(500) ->and($ink->image_path)->not->toBeNull(); Storage::disk('local')->assertExists($ink->thumbnail_path); $this->get(route('inventory.items.image', [$ink->id, 'thumbnail']))->assertOk(); Livewire::test('inventory::item-form', ['item' => $ink->id]) ->set('name', 'Valokuvapaperi 10×15') ->call('save') ->assertHasErrors('name') ->set('name', 'Muste syaani 1 l') ->set('removeImage', true) ->call('save') ->assertHasNoErrors(); expect($ink->fresh()->image_path)->toBeNull(); Storage::disk('local')->assertMissing($ink->thumbnail_path); $this->actingAs($this->employee); $this->get(route('inventory.items.create'))->assertForbidden(); $this->get(route('inventory.groups'))->assertForbidden(); });