Files
Toiminnanohjaus/tests/Feature/Modules/Inventory/InventoryCountTest.php
2026-11-26 16:34:08 +02:00

96 lines
5.0 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Inventory\Domain\Enums\StockMovementKind;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\InventoryCountModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemGroupModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->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->inks = StockItemGroupModel::factory()->create(['name' => 'Musteet', 'sort_order' => 2]);
$this->paper = StockItemModel::factory()->create(['stock_item_group_id' => $this->papers->id, 'name' => 'Valokuvapaperi', 'unit' => 'sheet', 'average_cost_hundredths' => 5_000]);
$this->card = StockItemModel::factory()->create(['stock_item_group_id' => $this->papers->id, 'name' => 'Kartonki', 'average_cost_hundredths' => 10_000]);
$this->ink = StockItemModel::factory()->create(['stock_item_group_id' => $this->inks->id, 'name' => 'Muste', 'unit' => 'litre']);
foreach ([[$this->paper, 100_000], [$this->card, 10_000]] as [$item, $milli]) {
StockMovementModel::query()->create([
'stock_item_id' => $item->id, 'kind' => 'purchase', 'quantity_milli' => $milli, 'unit_cost_hundredths' => $item->average_cost_hundredths,
'value_cents' => intdiv($milli * $item->average_cost_hundredths, 100_000), 'occurred_on' => now()->toDateString(), 'recorded_at' => now(), 'user_id' => $this->manager->id,
]);
}
});
it('counts the chosen groups, keeps the book balance of the moment and corrects the differences on confirmation', function () {
$this->actingAs($this->manager);
Livewire::test('inventory::counts')->set('groupIds', [(string) $this->papers->id])->call('start')->assertHasNoErrors();
$count = InventoryCountModel::query()->sole();
Livewire::test('inventory::counts')->call('start')->assertHasErrors('groupIds');
$this->actingAs($this->employee);
Livewire::test('inventory::count', ['count' => $count->id])
->assertSee('Valokuvapaperi')
->assertDontSee('Muste')
->assertSee('2 nimikettä laskematta.')
->call('saveCounted', $this->paper->id, '95')
->assertSet('message', "Valokuvapaperi: laskettu 95\u{00A0}arkki, ero -5\u{00A0}arkki.")
->call('saveCounted', $this->card->id, 'x')
->assertHasErrors('counted.'.$this->card->id)
->set('onlyUncounted', true)
->assertSee('Kartonki')
->assertDontSee('kirjanpitosaldo 100');
Livewire::test('inventory::count', ['count' => $count->id])->call('saveCounted', $this->ink->id, '1')->assertNotFound();
// Use recorded during the count does not change the counted difference.
StockMovementModel::query()->create([
'stock_item_id' => $this->paper->id, 'kind' => 'consumption', 'quantity_milli' => -10_000, 'unit_cost_hundredths' => 5_000,
'value_cents' => -500, 'occurred_on' => now()->toDateString(), 'recorded_at' => now(), 'user_id' => $this->employee->id, 'reason' => 'Testi',
]);
Livewire::test('inventory::count', ['count' => $count->id])->call('confirmCount')->assertForbidden();
$this->actingAs($this->manager);
Livewire::test('inventory::count', ['count' => $count->id])
->assertSee('1 nimike laskematta.')
->call('confirmCount')
->assertSee('1 nimike on laskematta, eikä sen saldoa muuteta.')
->call('confirm')
->assertSet('message', 'Inventaario vahvistettu: 1 korjaus kirjattu.');
$adjustment = StockMovementModel::query()->where('kind', StockMovementKind::Adjustment)->sole();
$count->refresh();
expect($adjustment->quantity_milli)->toBe(-5_000)
->and($adjustment->inventory_count_id)->toBe($count->id)
->and($count->status)->toBe('confirmed')
->and($count->value_before_cents)->toBe(4_500 + 1_000)
->and($count->value_after_cents)->toBe(4_250 + 1_000)
->and((int) StockMovementModel::query()->where('stock_item_id', $this->paper->id)->sum('quantity_milli'))->toBe(85_000);
Livewire::test('inventory::count', ['count' => $count->id])
->call('saveCounted', $this->paper->id, '9')
->assertSet('message', __('inventory.counts.errors.confirmed'));
$csv = $this->get(route('inventory.counts.csv', $count->id))->assertOk()->streamedContent();
expect($csv)->toContain('Valokuvapaperi;;Paperit;arkki;100;95;-5;0,5000;5000;4750;-250');
$this->actingAs($this->employee);
$this->get(route('inventory.counts.csv', $count->id))->assertForbidden();
});
it('keeps subcontractors out of stock counts', function () {
$this->actingAs(User::factory()->subcontractor()->create());
$this->get(route('inventory.counts'))->assertForbidden();
});