@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace Tempo\Purchasing\Inventory;
|
||||
|
||||
use Brick\Math\BigRational;
|
||||
|
||||
/** Exact calculation over qualified facts. No stock balance or financial writer. */
|
||||
final class StockCosting
|
||||
{
|
||||
public const METHODS = ['fifo', 'weighted_average'];
|
||||
|
||||
private static function integer(mixed $value, bool $positive = false): int
|
||||
{
|
||||
if (! is_int($value) || $value < ($positive ? 1 : 0)) {
|
||||
throw new \InvalidArgumentException('Qualified integer quantity/value required.');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private static function amount(BigRational $value): array
|
||||
{
|
||||
$numerator = $value->getNumerator();
|
||||
$denominator = $value->getDenominator();
|
||||
$gcd = $numerator->gcd($denominator);
|
||||
$numerator = $numerator->quotient($gcd);
|
||||
$denominator = $denominator->quotient($gcd);
|
||||
$exact = $denominator->isEqualTo(1);
|
||||
|
||||
return ['numerator' => (string) $numerator, 'denominator' => (string) $denominator,
|
||||
'amount_minor' => $exact ? (string) $numerator : null, 'rounding_required' => ! $exact];
|
||||
}
|
||||
|
||||
public static function project(string $method, array $opening, array $movements): array
|
||||
{
|
||||
if (! in_array($method, self::METHODS, true) || ! is_string($opening['reference'] ?? null) || trim($opening['reference']) === '') {
|
||||
throw new \InvalidArgumentException('Explicit method and qualified opening reference required.');
|
||||
}
|
||||
$quantity = self::integer($opening['quantity_milli'] ?? null);
|
||||
$value = BigRational::of(self::integer($opening['value_minor'] ?? null));
|
||||
if ($quantity === 0 && ! $value->isZero()) {
|
||||
throw new \DomainException('Opening value without stock is inconsistent.');
|
||||
}
|
||||
$layers = [];
|
||||
$head = 0;
|
||||
if ($method === 'fifo') {
|
||||
$sumQuantity = 0;
|
||||
$sumValue = BigRational::of(0);
|
||||
foreach ($opening['layers'] ?? [] as $layer) {
|
||||
$q = self::integer($layer['quantity_milli'] ?? null, true);
|
||||
$v = BigRational::of(self::integer($layer['value_minor'] ?? null));
|
||||
if ($q > PHP_INT_MAX - $sumQuantity) {
|
||||
throw new \OverflowException('Opening quantity too large.');
|
||||
}
|
||||
$sumQuantity += $q;
|
||||
$sumValue = $sumValue->plus($v);
|
||||
$layers[] = ['quantity_milli' => $q, 'value' => $v];
|
||||
}
|
||||
if ($sumQuantity !== $quantity || ! $sumValue->isEqualTo($value)) {
|
||||
throw new \DomainException('FIFO opening requires reconciled historical layers.');
|
||||
}
|
||||
}
|
||||
$last = 0;
|
||||
$results = [];
|
||||
foreach ($movements as $movement) {
|
||||
$id = self::integer($movement['id'] ?? null, true);
|
||||
$q = self::integer($movement['quantity_milli'] ?? null, true);
|
||||
if ($id <= $last || ! is_string($movement['reference'] ?? null) || trim($movement['reference']) === '') {
|
||||
throw new \DomainException('Movements require unique ascending IDs and source references.');
|
||||
}
|
||||
$last = $id;
|
||||
if (($movement['kind'] ?? null) === 'receipt') {
|
||||
$amount = BigRational::of(self::integer($movement['value_minor'] ?? null));
|
||||
if ($q > PHP_INT_MAX - $quantity) {
|
||||
throw new \OverflowException('Stock quantity too large.');
|
||||
}
|
||||
$quantity += $q;
|
||||
$value = $value->plus($amount);
|
||||
if ($method === 'fifo') {
|
||||
$layers[] = ['quantity_milli' => $q, 'value' => $amount];
|
||||
}
|
||||
} elseif (($movement['kind'] ?? null) === 'issue') {
|
||||
if (array_key_exists('value_minor', $movement)) {
|
||||
throw new \InvalidArgumentException('An issue cannot override its calculated cost.');
|
||||
}
|
||||
if ($q > $quantity) {
|
||||
throw new \DomainException('Incomplete receipts/opening or insufficient stock; do not assume zero cost.');
|
||||
}
|
||||
$amount = BigRational::of(0);
|
||||
if ($method === 'weighted_average') {
|
||||
$amount = $value->multipliedBy($q)->dividedBy($quantity);
|
||||
} else {
|
||||
$remaining = $q;
|
||||
while ($remaining > 0) {
|
||||
$take = min($remaining, $layers[$head]['quantity_milli']);
|
||||
$part = $layers[$head]['value']->multipliedBy($take)->dividedBy($layers[$head]['quantity_milli']);
|
||||
$amount = $amount->plus($part);
|
||||
$layers[$head]['quantity_milli'] -= $take;
|
||||
$layers[$head]['value'] = $layers[$head]['value']->minus($part);
|
||||
$remaining -= $take;
|
||||
if ($layers[$head]['quantity_milli'] === 0) {
|
||||
unset($layers[$head]);
|
||||
$head++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$quantity -= $q;
|
||||
$value = $value->minus($amount);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Unknown qualified movement kind.');
|
||||
}
|
||||
$results[] = ['id' => $id, 'reference' => $movement['reference'], 'kind' => $movement['kind'], 'quantity_milli' => $q, 'value' => self::amount($amount)];
|
||||
}
|
||||
|
||||
return ['method' => $method, 'opening_reference' => $opening['reference'], 'movements' => $results,
|
||||
'closing' => ['quantity_milli' => $quantity, 'value' => self::amount($value)],
|
||||
'stock_written' => false, 'journals_written' => false];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user