55 lines
2.4 KiB
PHP
55 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
final class LabReferenceService
|
|
{
|
|
public static function all(): array
|
|
{
|
|
return DB::pdo()
|
|
->query(
|
|
'SELECT * FROM lab_reference_ranges ORDER BY analyzer_name COLLATE NOCASE,species_code,sort_order,parameter_code COLLATE NOCASE',
|
|
)
|
|
->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
public static function forAnimal(string $species): array
|
|
{
|
|
$species = SpeciesService::normalize($species);
|
|
$s = DB::pdo()->prepare(
|
|
'SELECT analyzer_code,analyzer_name,parameter_code,parameter_name,unit,reference_min,reference_max FROM lab_reference_ranges WHERE species_code=? AND active=1 ORDER BY analyzer_name COLLATE NOCASE,sort_order,parameter_code COLLATE NOCASE',
|
|
);
|
|
$s->execute([$species]);
|
|
$out = [];
|
|
foreach ($s->fetchAll(PDO::FETCH_ASSOC) as $r) {
|
|
$key = $r['analyzer_code'];
|
|
$out[$key]['label'] = $r['analyzer_name'];
|
|
$out[$key]['parameters'][] = [
|
|
'code' => $r['parameter_code'],
|
|
'name' => $r['parameter_name'],
|
|
'unit' => $r['unit'],
|
|
'min' => $r['reference_min'],
|
|
'max' => $r['reference_max'],
|
|
];
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
public static function copySpecies(string $source, string $target, ?int $userId): int
|
|
{
|
|
$source = SpeciesService::normalize($source);
|
|
$target = SpeciesService::normalize($target);
|
|
if ($source === $target || !SpeciesService::exists($source, false) || !SpeciesService::exists($target, false)) {
|
|
throw new RuntimeException(t('lab_reference.invalid_species'));
|
|
}
|
|
$stmt = DB::pdo()->prepare(
|
|
"INSERT OR IGNORE INTO lab_reference_ranges(analyzer_code,analyzer_name,species_code,parameter_code,parameter_name,unit,reference_min,reference_max,source_note,active,sort_order,updated_by,updated_at)
|
|
SELECT analyzer_code,analyzer_name,:target,parameter_code,parameter_name,unit,reference_min,reference_max,source_note,active,sort_order,:user,datetime('now')
|
|
FROM lab_reference_ranges WHERE species_code=:source",
|
|
);
|
|
$stmt->execute([':source' => $source, ':target' => $target, ':user' => $userId]);
|
|
if ($stmt->rowCount() === 0) {
|
|
throw new RuntimeException(t('lab_reference.nothing_copied'));
|
|
}
|
|
return $stmt->rowCount();
|
|
}
|
|
}
|