60 lines
3.4 KiB
PHP
60 lines
3.4 KiB
PHP
<?php
|
|
|
|
use Tempo\Purchasing\Inventory\StockCosting;
|
|
|
|
require_once __DIR__.'/../../packages/purchasing/src/StockCosting.php';
|
|
|
|
function costingOpening(): array
|
|
{
|
|
return ['reference' => 'synthetic-empty-cut', 'quantity_milli' => 0, 'value_minor' => 0, 'layers' => []];
|
|
}
|
|
function costingReceipt(int $id, int $quantity, int $value): array
|
|
{
|
|
return ['id' => $id, 'reference' => 'synthetic-receipt-'.$id, 'kind' => 'receipt', 'quantity_milli' => $quantity, 'value_minor' => $value];
|
|
}
|
|
function costingIssue(int $id, int $quantity): array
|
|
{
|
|
return ['id' => $id, 'reference' => 'synthetic-issue-'.$id, 'kind' => 'issue', 'quantity_milli' => $quantity];
|
|
}
|
|
|
|
test('FIFO and weighted average are selected explicitly and give distinct exact costs', function () {
|
|
$facts = [costingReceipt(1, 1000, 100), costingReceipt(2, 1000, 200), costingIssue(3, 1000)];
|
|
$fifo = StockCosting::project('fifo', costingOpening(), $facts);
|
|
$average = StockCosting::project('weighted_average', costingOpening(), $facts);
|
|
expect($fifo['movements'][2]['value']['amount_minor'])->toBe('100')
|
|
->and($average['movements'][2]['value']['amount_minor'])->toBe('150')
|
|
->and($fifo['closing']['value']['amount_minor'])->toBe('200')
|
|
->and($average['closing']['value']['amount_minor'])->toBe('150');
|
|
});
|
|
|
|
test('partial FIFO layers preserve the full remaining value', function () {
|
|
$r = StockCosting::project('fifo', costingOpening(), [costingReceipt(1, 3000, 100), costingIssue(2, 1000), costingIssue(3, 2000)]);
|
|
expect($r['movements'][1]['value'])->toMatchArray(['numerator' => '100', 'denominator' => '3', 'amount_minor' => null, 'rounding_required' => true])
|
|
->and($r['movements'][2]['value']['numerator'])->toBe('200')
|
|
->and($r['closing'])->toMatchArray(['quantity_milli' => 0, 'value' => ['numerator' => '0', 'denominator' => '1', 'amount_minor' => '0', 'rounding_required' => false]]);
|
|
});
|
|
|
|
test('unknown receipt costs are not converted to zero', function () {
|
|
$r = costingReceipt(1, 1000, 100);
|
|
unset($r['value_minor']);
|
|
expect(fn () => StockCosting::project('weighted_average', costingOpening(), [$r]))->toThrow(InvalidArgumentException::class);
|
|
});
|
|
|
|
test('FIFO does not invent layers for an opening balance', function () {
|
|
expect(fn () => StockCosting::project('fifo', ['reference' => 'opening', 'quantity_milli' => 1000, 'value_minor' => 100], []))->toThrow(DomainException::class);
|
|
});
|
|
|
|
test('unknown methods, reordered facts and negative stock are refused', function (string $method, array $facts, string $exception) {
|
|
expect(fn () => StockCosting::project($method, costingOpening(), $facts))->toThrow($exception);
|
|
})->with([
|
|
'unknown method' => ['default', [], InvalidArgumentException::class],
|
|
'reordered' => ['fifo', [costingReceipt(2, 1000, 100), costingReceipt(1, 1000, 100)], DomainException::class],
|
|
'shortage' => ['weighted_average', [costingReceipt(1, 1000, 100), costingIssue(2, 1001)], DomainException::class],
|
|
'override' => ['fifo', [costingReceipt(1, 1000, 100), costingIssue(2, 1000) + ['value_minor' => 1]], InvalidArgumentException::class],
|
|
]);
|
|
|
|
test('explicit zero-value receipt differs from unknown cost', function () {
|
|
$r = StockCosting::project('fifo', costingOpening(), [costingReceipt(1, 1000, 0), costingIssue(2, 1000)]);
|
|
expect($r['movements'][1]['value']['amount_minor'])->toBe('0')->and($r['journals_written'])->toBeFalse();
|
|
});
|