@@ -76,3 +76,61 @@ test('half cent rounds upwards and the last issue receives the residual', functi
|
||||
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']);
|
||||
|
||||
Reference in New Issue
Block a user