@@ -52,8 +52,9 @@ final class StockCosting
|
||||
throw new \DomainException('Opening value without stock is inconsistent.');
|
||||
}
|
||||
$layers = [];
|
||||
$origins = [];
|
||||
$head = 0;
|
||||
if ($method === 'fifo') {
|
||||
if ($method === 'fifo' || ($opening['layers'] ?? []) !== []) {
|
||||
$sumQuantity = 0;
|
||||
$sumValue = BigRational::of(0);
|
||||
foreach ($opening['layers'] ?? [] as $layer) {
|
||||
@@ -64,12 +65,22 @@ final class StockCosting
|
||||
}
|
||||
$sumQuantity += $q;
|
||||
$sumValue = $sumValue->plus($v);
|
||||
$layers[] = ['quantity_milli' => $q, 'value' => $v];
|
||||
$reference = $layer['reference'] ?? (count($opening['layers'] ?? []) === 1 ? $opening['reference'] : null);
|
||||
$layers[] = ['quantity_milli' => $q, 'value' => $v, 'reference' => $reference];
|
||||
if (is_string($reference) && trim($reference) !== '') {
|
||||
if (isset($origins[$reference])) {
|
||||
throw new \DomainException('Duplicate opening receipt reference.');
|
||||
}
|
||||
$origins[$reference] = ['available_milli' => $q, 'unit_value' => $v->dividedBy($q)];
|
||||
}
|
||||
}
|
||||
if ($sumQuantity !== $quantity || ! $sumValue->isEqualTo($value)) {
|
||||
throw new \DomainException('FIFO opening requires reconciled historical layers.');
|
||||
}
|
||||
}
|
||||
if ($method === 'weighted_average' && $quantity > 0 && ($opening['layers'] ?? []) === []) {
|
||||
$origins[$opening['reference']] = ['available_milli' => $quantity, 'unit_value' => $value->dividedBy($quantity)];
|
||||
}
|
||||
$last = 0;
|
||||
$results = [];
|
||||
foreach ($movements as $movement) {
|
||||
@@ -86,9 +97,50 @@ final class StockCosting
|
||||
}
|
||||
$quantity += $q;
|
||||
$value = $value->plus($amount);
|
||||
if ($method === 'fifo') {
|
||||
$layers[] = ['quantity_milli' => $q, 'value' => $amount];
|
||||
if (isset($origins[$movement['reference']])) {
|
||||
throw new \DomainException('Duplicate receipt reference.');
|
||||
}
|
||||
$origins[$movement['reference']] = ['available_milli' => $q, 'unit_value' => $amount->dividedBy($q)];
|
||||
if ($method === 'fifo') {
|
||||
$layers[] = ['quantity_milli' => $q, 'value' => $amount, 'reference' => $movement['reference']];
|
||||
}
|
||||
} elseif (($movement['kind'] ?? null) === 'receipt_return') {
|
||||
$reference = $movement['receipt_reference'] ?? null;
|
||||
if (! is_string($reference) || ! isset($origins[$reference]) || $q > $quantity) {
|
||||
throw new \DomainException('An identified original receipt and sufficient stock are required.');
|
||||
}
|
||||
$amount = $origins[$reference]['unit_value']->multipliedBy($q);
|
||||
if ($method === 'fifo') {
|
||||
$index = null;
|
||||
foreach ($layers as $key => $layer) {
|
||||
if ($layer['reference'] === $reference) {
|
||||
$index = $key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($index === null || $layers[$index]['quantity_milli'] < $q) {
|
||||
throw new \DomainException('Original receipt layer already consumed; independently reconciled opening required.');
|
||||
}
|
||||
$layers[$index]['quantity_milli'] -= $q;
|
||||
$layers[$index]['value'] = $layers[$index]['value']->minus($amount);
|
||||
if ($layers[$index]['quantity_milli'] === 0) {
|
||||
unset($layers[$index]);
|
||||
}
|
||||
} elseif ($origins[$reference]['available_milli'] < $q) {
|
||||
throw new \DomainException('Original receipt availability is not proven after prior issues; independent reconciliation required.');
|
||||
}
|
||||
if ($rounding === 'half_up_final_balance') {
|
||||
$amount = $q === $quantity ? $value : $amount->toScale(0, RoundingMode::HalfUp)->toBigRational();
|
||||
}
|
||||
if ($amount->isGreaterThan($value)) {
|
||||
throw new \DomainException('Return exceeds remaining stock value; independent reconciliation required.');
|
||||
}
|
||||
if (array_key_exists('value_minor', $movement) && ! $amount->isEqualTo(self::integer($movement['value_minor']))) {
|
||||
throw new \DomainException('Posted return differs from its original receipt cost; never revalue prior journals.');
|
||||
}
|
||||
$origins[$reference]['available_milli'] -= $q;
|
||||
$quantity -= $q;
|
||||
$value = $value->minus($amount);
|
||||
} elseif (($movement['kind'] ?? null) === 'issue') {
|
||||
if (array_key_exists('value_minor', $movement)) {
|
||||
throw new \InvalidArgumentException('An issue cannot override its calculated cost.');
|
||||
@@ -102,6 +154,9 @@ final class StockCosting
|
||||
} else {
|
||||
$remaining = $q;
|
||||
while ($remaining > 0) {
|
||||
while (! isset($layers[$head])) {
|
||||
$head++;
|
||||
}
|
||||
$take = min($remaining, $layers[$head]['quantity_milli']);
|
||||
$part = $layers[$head]['value']->multipliedBy($take)->dividedBy($layers[$head]['quantity_milli']);
|
||||
$amount = $amount->plus($part);
|
||||
@@ -122,10 +177,20 @@ final class StockCosting
|
||||
}
|
||||
$quantity -= $q;
|
||||
$value = $value->minus($amount);
|
||||
if ($method === 'weighted_average') {
|
||||
// Without qualified lot allocations no receipt may claim units already issued.
|
||||
foreach ($origins as &$origin) {
|
||||
$origin['available_milli'] = max(0, $origin['available_milli'] - $q);
|
||||
}
|
||||
unset($origin);
|
||||
}
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Unknown qualified movement kind.');
|
||||
}
|
||||
$results[] = ['id' => $id, 'reference' => $movement['reference'], 'kind' => $movement['kind'], 'quantity_milli' => $q, 'value' => self::amount($amount)];
|
||||
if ($movement['kind'] === 'receipt_return') {
|
||||
$results[array_key_last($results)]['receipt_reference'] = $movement['receipt_reference'];
|
||||
}
|
||||
}
|
||||
|
||||
return ['method' => $method, 'opening_reference' => $opening['reference'], 'movements' => $results,
|
||||
|
||||
Reference in New Issue
Block a user