137 lines
9.3 KiB
PHP
137 lines
9.3 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();
|
|
});
|
|
|
|
test('configured half up preserves the final balance without changing the exact default', function (string $method, array $expected) {
|
|
$facts = [costingReceipt(1, 3000, 100), costingIssue(2, 1000), costingIssue(3, 1000), costingIssue(4, 1000)];
|
|
$result = StockCosting::project($method, costingOpening(), $facts, 'half_up_final_balance');
|
|
expect(array_column(array_column(array_slice($result['movements'], 1), 'value'), 'amount_minor'))->toBe($expected)
|
|
->and($result['closing']['value']['amount_minor'])->toBe('0')
|
|
->and($result['closing']['quantity_milli'])->toBe(0)
|
|
->and(StockCosting::project($method, costingOpening(), $facts)['movements'][1]['value']['rounding_required'])->toBeTrue();
|
|
})->with(['fifo' => ['fifo', ['33', '33', '34']], 'average' => ['weighted_average', ['33', '34', '33']]]);
|
|
|
|
test('half cent rounds upwards and the last issue receives the residual', function (string $method) {
|
|
$result = StockCosting::project($method, costingOpening(), [costingReceipt(1, 2000, 101), costingIssue(2, 1000), costingIssue(3, 1000)], 'half_up_final_balance');
|
|
expect($result['movements'][1]['value']['amount_minor'])->toBe('51')
|
|
->and($result['movements'][2]['value']['amount_minor'])->toBe('50');
|
|
})->with(['fifo', 'weighted_average']);
|
|
|
|
test('an unrecognised rounding policy cannot silently change costs', function () {
|
|
expect(fn () => StockCosting::project('fifo', costingOpening(), [], 'automatic'))->toThrow(InvalidArgumentException::class);
|
|
});
|
|
|
|
test('approved zero issues preserve half up and the exact final balance', function (string $method, array $quantities, array $amounts) {
|
|
$facts = [costingReceipt(1, 1000, 1)];
|
|
foreach ($quantities as $index => $quantity) {
|
|
$facts[] = costingIssue($index + 2, $quantity);
|
|
}
|
|
$result = StockCosting::project($method, costingOpening(), $facts, 'half_up_final_balance');
|
|
expect(array_column(array_column(array_slice($result['movements'], 1), 'value'), 'amount_minor'))->toBe($amounts)
|
|
->and($result['closing']['quantity_milli'])->toBe(0)
|
|
->and($result['closing']['value']['amount_minor'])->toBe('0')
|
|
->and($result['stock_written'])->toBeFalse()->and($result['journals_written'])->toBeFalse();
|
|
})->with([
|
|
['fifo', [100, 400, 500], ['0', '0', '1']],
|
|
['weighted_average', [100, 400, 500], ['0', '0', '1']],
|
|
['fifo', [500, 100, 400], ['1', '0', '0']],
|
|
['weighted_average', [500, 100, 400], ['1', '0', '0']],
|
|
]);
|
|
|
|
test('supplier return uses its identified receipt rather than the oldest or average price', function (string $method, string $nextCost) {
|
|
$facts = [costingReceipt(1, 1000, 100), costingReceipt(2, 1000, 300),
|
|
['id' => 3, 'reference' => 'synthetic-return-3', 'kind' => 'receipt_return', 'quantity_milli' => 500, 'receipt_reference' => 'synthetic-receipt-2', 'value_minor' => 150], costingIssue(4, 500)];
|
|
$result = StockCosting::project($method, costingOpening(), $facts, 'half_up_final_balance');
|
|
expect($result['movements'][2]['value']['amount_minor'])->toBe('150')
|
|
->and($result['movements'][3]['value']['amount_minor'])->toBe($nextCost)
|
|
->and($result['movements'][0]['value']['amount_minor'])->toBe('100')
|
|
->and($result['stock_written'])->toBeFalse()->and($result['journals_written'])->toBeFalse();
|
|
})->with([['fifo', '50'], ['weighted_average', '83']]);
|
|
|
|
test('returning an exhausted original layer requires reconciliation without changing prior costs', function (string $method) {
|
|
$facts = [costingReceipt(1, 1000, 100), costingIssue(2, 1000), costingReceipt(3, 1000, 300),
|
|
['id' => 4, 'reference' => 'synthetic-return-4', 'kind' => 'receipt_return', 'quantity_milli' => 500, 'receipt_reference' => 'synthetic-receipt-1']];
|
|
expect(fn () => StockCosting::project($method, costingOpening(), $facts))->toThrow(DomainException::class);
|
|
expect(StockCosting::project($method, costingOpening(), array_slice($facts, 0, 3))['movements'][1]['value']['amount_minor'])->toBe('100');
|
|
})->with(['fifo', 'weighted_average']);
|
|
|
|
test('FIFO skips a completely returned layer and never consumes its value again', function () {
|
|
$facts = [costingReceipt(1, 1000, 100), costingReceipt(2, 1000, 300),
|
|
['id' => 3, 'reference' => 'synthetic-return-3', 'kind' => 'receipt_return', 'quantity_milli' => 1000, 'receipt_reference' => 'synthetic-receipt-1'], costingIssue(4, 1000)];
|
|
$result = StockCosting::project('fifo', costingOpening(), $facts);
|
|
expect($result['movements'][3]['value']['amount_minor'])->toBe('300')->and($result['closing']['value']['amount_minor'])->toBe('0');
|
|
});
|
|
|
|
test('an incompatible posted return is refused instead of rewriting a journal', function () {
|
|
$facts = [costingReceipt(1, 1000, 100), ['id' => 2, 'reference' => 'synthetic-return-2', 'kind' => 'receipt_return', 'quantity_milli' => 500, 'receipt_reference' => 'synthetic-receipt-1', 'value_minor' => 51]];
|
|
expect(fn () => StockCosting::project('fifo', costingOpening(), $facts))->toThrow(DomainException::class);
|
|
});
|
|
|
|
test('approved reconstructed layers retain receipt cost for a return and conserve the final balance', function (string $method) {
|
|
$opening = ['reference' => 'approved-opening-1', 'quantity_milli' => 2000, 'value_minor' => 400,
|
|
'layers' => [['reference' => 'original-receipt-1', 'quantity_milli' => 1000, 'value_minor' => 100],
|
|
['reference' => 'reconciled-balance-1', 'quantity_milli' => 1000, 'value_minor' => 300]]];
|
|
$facts = [['id' => 10, 'reference' => 'return-10', 'kind' => 'receipt_return', 'quantity_milli' => 500, 'receipt_reference' => 'original-receipt-1', 'value_minor' => 50], costingIssue(11, 1500)];
|
|
$result = StockCosting::project($method, $opening, $facts, 'half_up_final_balance');
|
|
expect($result['movements'][0]['value']['amount_minor'])->toBe('50')
|
|
->and($result['movements'][1]['value']['amount_minor'])->toBe('350')
|
|
->and($result['closing']['value']['amount_minor'])->toBe('0')
|
|
->and($result['closing']['quantity_milli'])->toBe(0);
|
|
})->with(['fifo', 'weighted_average']);
|