From d4c6e58e30b2dd20760d6043f08272c4593677e8 Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Thu, 30 Jan 2025 01:35:07 +0600 Subject: [PATCH] age - dob calculation --- app/Services/Pacs/Sync/StudiesSync.php | 51 +++++++++++++++++++------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/app/Services/Pacs/Sync/StudiesSync.php b/app/Services/Pacs/Sync/StudiesSync.php index 24b2bbb..e0058b2 100644 --- a/app/Services/Pacs/Sync/StudiesSync.php +++ b/app/Services/Pacs/Sync/StudiesSync.php @@ -149,16 +149,28 @@ public function transformData(mixed $orthanc_src): array $patient_name = data_get($orthanc_src, 'PatientMainDicomTags.PatientName'); $name_parts = tokenizeString($patient_name); $patient_age = data_get($orthanc_src, 'RequestedTags.PatientAge'); - if (blank($patient_age)) { + if (blank($patient_age) && ! empty($name_parts)) { // try to get age from last part of patient name - if (! empty($name_parts)) { - $last = end($name_parts); - if (preg_match('/\d+Y/i', $last)) { - $patient_age = strtoupper($last); - } + $last = end($name_parts); + if (preg_match('/^\d+[YMD]$/i', $last)) { + $patient_age = $last; + /* + // sanitize patient name + array_pop($name_parts); + $patient_name = implode(' ', $name_parts); + */ } } + if ($patient_age !== null) { + $age = strtoupper(ltrim($patient_age, '0')); + if (strlen($age) > 1) { + $patient_age = $age; + } + } + + // $patient_name = trim($patient_name, '.^ '); + $descr = $this->getStudyDescription($orthanc_src); $study = [ 'dicom_server_id' => $this->dicomServer->id, @@ -193,27 +205,40 @@ public function transformData(mixed $orthanc_src): array $study['workflow_level'] = $stable_study ? WorkflowLevel::Unassigned->value : WorkflowLevel::Received->value; - $study['patient_birthdate'] = null; + + $patient_birthdate = null; $dob = data_get($orthanc_src, 'PatientMainDicomTags.PatientBirthDate'); if (filled($dob)) { try { - $study['patient_birthdate'] = Carbon::parse($dob); + $patient_birthdate = Carbon::parse($dob); } catch (Exception $e) { Log::error('Failed to parse PatientMainDicomTags.PatientBirthDate: {dob}', ['dob' => $dob, 'exception' => $e->getMessage()]); } } - if (($study['patient_birthdate'] == null) && $patient_age !== null) { + if ($patient_birthdate == null && $patient_age !== null) { try { // $age = (int) preg_replace('/[^0-9]/', '', $patient_age); - $age = (int) filter_var($patient_age, FILTER_SANITIZE_NUMBER_INT); - $study['patient_birthdate'] = Carbon::now()->subYears($age); - + $age_num = (int) filter_var($patient_age, FILTER_SANITIZE_NUMBER_INT); + $now = now(); + switch (strtoupper(substr($patient_age, -1))) { + case 'Y': + $patient_birthdate = $now->subYears($age_num); + break; + case 'M': + $patient_birthdate = $now->subMonths($age_num); + break; + case 'D': + $patient_birthdate = $now->subDays($age_num); + break; + } } catch (Exception) { Log::error('Failed to parse patient_age: {age}', ['age' => $patient_age]); } } + $study['patient_birthdate'] = $patient_birthdate; + // check for priority in patient name or description if (preg_match('/\b(urgent|stat)\b/i', implode(' ', [$descr, $patient_name]))) { $this->setValue($study, 'priority', Priority::Stat->value); @@ -307,7 +332,7 @@ private function getStudyDicomTags(string $study_uuid): array // randomly sample few instances for tags collection $selectedInstances = count($instances) <= $this->maxInstances ? $instances - : array_rand(array_flip($instances), $this->maxInstances); + : array_intersect_key($instances, array_flip(array_rand($instances, $this->maxInstances))); $tags = collect(); foreach ($selectedInstances as $instance) {