34 lines
896 B
PHP
34 lines
896 B
PHP
<?php
|
|
|
|
namespace App\Domain\Reports\Calculators;
|
|
|
|
use App\Domain\Reports\Models\MetricAggregate;
|
|
use App\Domain\Reports\Models\OperationInterval;
|
|
|
|
final class PreoperativeDaysCalculator
|
|
{
|
|
/**
|
|
* @param iterable<OperationInterval> $intervals
|
|
*/
|
|
public function calculate(iterable $intervals): MetricAggregate
|
|
{
|
|
$totalDays = 0;
|
|
$patientCount = 0;
|
|
|
|
foreach ($intervals as $interval) {
|
|
if ($interval->operationAt < $interval->admittedAt) {
|
|
continue;
|
|
}
|
|
|
|
$totalDays += $interval->admittedAt->setTime(0, 0)->diff($interval->operationAt->setTime(0, 0))->days;
|
|
$patientCount++;
|
|
}
|
|
|
|
return new MetricAggregate(
|
|
total: $totalDays,
|
|
count: $patientCount,
|
|
average: $patientCount > 0 ? round($totalDays / $patientCount, 1) : 0.0,
|
|
);
|
|
}
|
|
}
|