milli)->toBe(2500) ->and(StockQuantity::parsePositive('1 200')->milli)->toBe(1_200_000) ->and(StockQuantity::parsePositive('0.125')->milli)->toBe(125) ->and(StockQuantity::parse(''))->toBeNull() ->and(StockQuantity::format(2500))->toBe('2,5') ->and(StockQuantity::format(-1000))->toBe('-1') ->and(StockQuantity::format(1_234_500))->toBe("1\u{00A0}234,5") ->and(StockQuantity::inputValue(1_234_500))->toBe('1234,5'); }); it('rejects quantities that are zero, negative or too precise', function (string $value) { StockQuantity::parsePositive($value); })->with(['0', '-1', '1,2345', 'abc'])->throws(InvalidStockQuantity::class); it('values stock at the moving average cost', function () { // 10 sheets for 5,00 € → 0,50 € each (5 000 hundredths of a cent). $average = StockValuation::averageAfterPurchase(0, 0, 10_000, 500); expect($average)->toBe(5_000); // 10 more for 7,00 € → (5,00 + 7,00) / 20 = 0,60 €. $average = StockValuation::averageAfterPurchase(10_000, $average, 10_000, 700); expect($average)->toBe(6_000) ->and(StockValuation::valueCents(2_500, $average))->toBe(150) ->and(StockValuation::valueCents(-2_500, $average))->toBe(-150); // After a negative balance the purchase's own price is the average. expect(StockValuation::averageAfterPurchase(-3_000, 6_000, 10_000, 1_000))->toBe(10_000) // Ink at 0,0123 €/ml keeps its precision. ->and(StockValuation::unitCostOf(1_000_000, 1_230))->toBe(123); }); it('records use and write-offs negated at the average cost, and reverses them once', function () { $item = StockItem::reconstitute(1, 1, new StockName('Paperi'), StockUnit::Sheet, null, null, null, 5_000, null, null, true); $day = new DateTimeImmutable('2026-11-26'); $use = StockMovement::consumption($item, StockQuantity::parsePositive('3'), new StockUseTarget(taskId: 4, jobId: 2), $day, $day, 7); $reversal = StockMovement::reversalOf(StockMovement::reconstitute(9, 1, StockMovementKind::Consumption, $use->quantityMilli(), 5_000, $use->valueCents(), $day, $day, 7, $use->target(), null, null, null, null, null), $day, $day, 7); expect($use->quantityMilli())->toBe(-3_000) ->and($use->valueCents())->toBe(-150) ->and($use->isOwnUseOf(7, $day))->toBeTrue() ->and($use->isOwnUseOf(8, $day))->toBeFalse() ->and($use->isOwnUseOf(7, $day->modify('+1 day')))->toBeFalse() ->and($reversal->quantityMilli())->toBe(3_000) ->and($reversal->valueCents())->toBe(150) ->and($reversal->reversesMovementId())->toBe(9) ->and($reversal->target()?->taskId)->toBe(4); StockMovement::reversalOf($reversal, $day, $day, 7); })->throws(StockMovementNotReversible::class); it('needs a reason for use without a target', function () { new StockUseTarget; })->throws(StockReasonRequired::class); it('matches searches by words of the name or code regardless of case and dots', function () { expect(StockSearch::matches('valokuva 10', 'Valokuvapaperi 10×15', null))->toBeTrue() ->and(StockSearch::matches('PAAPERI', 'Pääperi', null))->toBeTrue() ->and(StockSearch::matches('6417', 'Muste', '6417000123'))->toBeTrue() ->and(StockSearch::matches('muste syaani', 'Muste magenta', null))->toBeFalse(); });