@@ -3,6 +3,7 @@
|
||||
namespace Tempo\Purchasing\Inventory;
|
||||
|
||||
use Brick\Math\BigRational;
|
||||
use Brick\Math\RoundingMode;
|
||||
|
||||
/** Exact calculation over qualified facts. No stock balance or financial writer. */
|
||||
final class StockCosting
|
||||
@@ -18,6 +19,7 @@ final class StockCosting
|
||||
return $value;
|
||||
}
|
||||
|
||||
/** @return array{numerator:string,denominator:string,amount_minor:?string,rounding_required:bool} */
|
||||
private static function amount(BigRational $value): array
|
||||
{
|
||||
$numerator = $value->getNumerator();
|
||||
@@ -31,8 +33,16 @@ final class StockCosting
|
||||
'amount_minor' => $exact ? (string) $numerator : null, 'rounding_required' => ! $exact];
|
||||
}
|
||||
|
||||
public static function project(string $method, array $opening, array $movements): array
|
||||
/**
|
||||
* @param array<string,mixed> $opening
|
||||
* @param list<array<string,mixed>> $movements
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public static function project(string $method, array $opening, array $movements, ?string $rounding = null): array
|
||||
{
|
||||
if ($rounding !== null && $rounding !== 'half_up_final_balance') {
|
||||
throw new \InvalidArgumentException('Unknown stock rounding policy.');
|
||||
}
|
||||
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.');
|
||||
}
|
||||
@@ -104,6 +114,12 @@ final class StockCosting
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($rounding === 'half_up_final_balance') {
|
||||
$amount = $q === $quantity ? $value : $amount->toScale(0, RoundingMode::HalfUp)->toBigRational();
|
||||
if ($amount->isGreaterThan($value)) {
|
||||
throw new \DomainException('Rounded cost exceeds the remaining value; reconcile the cost before posting.');
|
||||
}
|
||||
}
|
||||
$quantity -= $q;
|
||||
$value = $value->minus($amount);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user