InventoryService.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. declare(strict_types=1);
  3. namespace App;
  4. final class InventoryService
  5. {
  6. public function calculate(array $slot, float $distanceMm): array
  7. {
  8. $full = (float) ($slot['full_distance_mm'] ?? 0);
  9. $empty = (float) ($slot['empty_distance_mm'] ?? 0);
  10. $perUnit = max(0.01, (float) ($slot['distance_per_unit'] ?? 1));
  11. $denominator = $full - $empty;
  12. $ratio = $denominator === 0.0 ? 0.0 : (($distanceMm - $empty) / $denominator);
  13. $ratio = max(0.0, min(1.0, $ratio));
  14. $maxUnits = (int) max(0, round(abs($full - $empty) / $perUnit));
  15. $unitsEstimated = (int) max(0, min($maxUnits, round($ratio * $maxUnits)));
  16. $fillPercent = (int) round($ratio * 100);
  17. $alertBelowUnits = (int) ($slot['alert_below_units'] ?? 0);
  18. $state = $unitsEstimated < $alertBelowUnits ? 'critical' : 'ok';
  19. return [
  20. 'distance_mm' => round($distanceMm, 2),
  21. 'fill_ratio' => $ratio,
  22. 'fill_percent' => $fillPercent,
  23. 'units_estimated' => $unitsEstimated,
  24. 'max_units' => $maxUnits,
  25. 'alert_below_units' => $alertBelowUnits,
  26. 'state' => $state,
  27. ];
  28. }
  29. }