forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeTypesService.php
432 lines (404 loc) · 15 KB
/
CodeTypesService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* CodeTypesService.php
*
* @package openemr
* @link http://www.open-emr.org
* @author Stephen Nielson <[email protected]>
* @copyright Copyright (c) 2021 Stephen Nielson <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use OpenEMR\Services\FHIR\FhirCodeSystemConstants;
/**
* Service for code type
*/
class CodeTypesService
{
/**
* @const string
*/
const CODE_TYPE_SNOMED_CT = "SNOMED-CT";
const CODE_TYPE_SNOMED = "SNOMED";
const CODE_TYPE_CPT4 = "CPT4";
const CODE_TYPE_LOINC = "LOINC";
const CODE_TYPE_NUCC = "NUCC";
const CODE_TYPE_RXNORM = "RXNORM";
const CODE_TYPE_RXCUI = "RXCUI";
const CODE_TYPE_ICD10 = 'ICD10';
const CODE_TYPE_ICD10PCS = 'ICD10PCS';
const CODE_TYPE_CPT = 'CPT';
const CODE_TYPE_CVX = 'CVX';
const CODE_TYPE_OID_HEALTHCARE_PROVIDER_TAXONOMY = "2.16.840.1.114222.4.11.1066";
const CODE_TYPE_OID = array(
'2.16.840.1.113883.6.96' => self::CODE_TYPE_SNOMED_CT,
'2.16.840.1.113883.6.12' => self::CODE_TYPE_CPT4,
'2.16.840.1.113883.6.1' => self::CODE_TYPE_LOINC,
'2.16.840.1.113883.6.101' => self::CODE_TYPE_NUCC,
'2.16.840.1.113883.6.88' => self::CODE_TYPE_RXNORM,
'2.16.840.1.113883.6.90' => self::CODE_TYPE_ICD10,
'2.16.840.1.113883.6.103' => 'ICD9-CM',
'2.16.840.1.113883.6.104' => 'ICD9-PCS',
'2.16.840.1.113883.6.4' => 'ICD10-PCS',
'2.16.840.1.113883.6.14' => 'HCP',
'2.16.840.1.113883.6.285' => 'HCPCS',
'2.16.840.1.113883.5.2' => "HL7 Marital Status",
'2.16.840.1.113883.12.292' => 'CVX',
'2.16.840.1.113883.5.83' => 'HITSP C80 Observation Status',
'2.16.840.1.113883.3.26.1.1' => 'NCI Thesaurus',
'2.16.840.1.113883.3.88.12.80.20' => 'FDA',
"2.16.840.1.113883.4.9" => "UNII",
"2.16.840.1.113883.6.69" => "NDC",
'2.16.840.1.113883.5.14' => 'HL7 ActStatus',
'2.16.840.1.113883.6.259' => 'HL7 Healthcare Service Location',
'2.16.840.1.113883.12.112' => 'DischargeDisposition',
'2.16.840.1.113883.5.4' => 'HL7 Act Code',
'2.16.840.1.113883.1.11.18877' => 'HL7 Relationship Code',
'2.16.840.1.113883.6.238' => 'CDC Race',
'2.16.840.1.113883.6.177' => 'NLM MeSH',
'2.16.840.1.113883.5.1076' => "Religious Affiliation",
'2.16.840.1.113883.1.11.19717' => "HL7 ActNoImmunicationReason",
'2.16.840.1.113883.3.88.12.80.33' => "NUBC",
'2.16.840.1.113883.1.11.78' => "HL7 Observation Interpretation",
'2.16.840.1.113883.3.221.5' => "Source of Payment Typology",
'2.16.840.1.113883.6.13' => 'CDT',
'2.16.840.1.113883.18.2' => 'AdministrativeSex',
'2.16.840.1.113883.5.1' => 'AdministrativeGender',
self::CODE_TYPE_OID_HEALTHCARE_PROVIDER_TAXONOMY => 'HealthCareProviderTaxonomy'
);
/**
* @var array
*/
public $installedCodeTypes;
/**
* @var bool
*/
private $snomedInstalled;
private $cpt4Installed;
private $rxnormInstalled;
public function __construct()
{
// currently, our installed code types are
global $code_types;
$this->installedCodeTypes = $code_types;
$this->snomedInstalled = isset($code_types[self::CODE_TYPE_SNOMED_CT]);
$this->cpt4Installed = isset($code_types[self::CODE_TYPE_CPT4]);
$this->rxnormInstalled = isset($code_types[self::CODE_TYPE_RXNORM]);
}
/**
* Lookup the description for a series of codes in the database. Eventually we would want to
* remove the global lookup_code_descriptions and use this so we can mock these codes and improve our unit test speed.
*
* @param string $codes - Is of the form "type:code;type:code; etc.".
* @param string $desc_detail - Can choose either the normal description('code_text') or the brief description('code_text_short').
* @return string - Is of the form "description;description; etc.".
* @see code_types.inc.php
*/
public function lookup_code_description($codes, $desc_detail = "code_text"): string
{
if (empty($codes)) {
return "";
}
$code_text = lookup_code_descriptions($codes, $desc_detail);
if (empty($code_text)) {
// let's return a description of code if available.
// sometimes depending on code, long and/or short description may not be available.
$desc_detail = $desc_detail == "code_text" ? "code_text_short" : "code_text";
$code_text = lookup_code_descriptions($codes, $desc_detail);
}
return $code_text;
}
/**
*
* @return bool - Returns true if the snomed-ct codes are installed
*/
public function isSnomedCodesInstalled(): bool
{
return $this->snomedInstalled;
}
/**
*
* @return bool - Returns whether the system has the cpt4 codes installed
*/
public function isCPT4Installed(): bool
{
return $this->cpt4Installed;
}
/**
*
* @return bool - Returns whether the system has the rxnorm codes installed
*/
public function isRXNORMInstalled(): bool
{
return $this->rxnormInstalled;
}
public function isInstalledCodeType($codeType): bool
{
return isset($this->installedCodeTypes[$codeType]);
}
/**
* @param $code
* @param false $useOid
* @return string|null
*/
public function getSystemForCode($code, $useOid = false)
{
$codeType = $this->getCodeTypeForCode($code);
if (!empty($codeType)) {
return $this->getSystemForCodeType($codeType);
}
return null;
}
/**
* @param $code
* @return array
*/
public function parseCode($code)
{
$parsedCode = $code;
$parsedType = null;
if (is_string($code) && strpos($code, ":") !== false) {
$parts = explode(":", $code);
$parsedCode = $parts[1];
$parsedType = $parts[0];
}
return ['code' => $parsedCode, 'code_type' => $parsedType];
}
/**
* Returns a code with the code type prefixed
*
* @param $code string The value for the code that exists in the given code_type datadatabse
* @param $type string The code_type that the code belongs to (SNOMED, RXCUI, ICD10, etc).
* @return string The fully typed code (TYPE:CODE)
*/
public function getCodeWithType($code, $type, $oe_format = false)
{
if (empty($type) || empty($code)) {
return "";
}
$tmp = explode(':', $code);
if (is_array($tmp) && count($tmp ?? []) === 2) {
if (!$oe_format) {
return $code;
}
// rebuild when code type format flag is set
$type = $tmp[0];
$code = $tmp[1];
}
if ($oe_format) {
$type = $this->formatCodeType($type ?? "");
}
return ($type ?? "") . ":" . ($code ?? "");
}
/**
* @param $code
* @return mixed
*/
public function getCodeTypeForCode($code)
{
$parsedCode = $this->parseCode($code);
return $parsedCode['code_type'];
}
/**
* @param string $codeType
* @param false $useOid
* @return string|null
*/
public function getSystemForCodeType($codeType, $useOid = false)
{
$system = null;
if ($useOid) {
if (self::CODE_TYPE_SNOMED_CT == $codeType) {
$system = '2.16.840.1.113883.6.96';
} elseif (self::CODE_TYPE_SNOMED == $codeType) {
$system = '2.16.840.1.113883.6.96';
} elseif (self::CODE_TYPE_CPT4 == $codeType) {
$system = '2.16.840.1.113883.6.12';
} elseif (self::CODE_TYPE_LOINC == $codeType) {
$system = '2.16.840.1.113883.6.1';
} elseif (self::CODE_TYPE_ICD10 == $codeType) {
$system = '2.16.840.1.113883.6.90';
} elseif (self::CODE_TYPE_RXCUI == $codeType || self::CODE_TYPE_RXNORM == $codeType) {
$system = '2.16.840.1.113883.6.88';
} elseif (self::CODE_TYPE_CPT == $codeType) {
$system = '2.16.840.1.113883.6.12';
} elseif (self::CODE_TYPE_CVX == $codeType) {
$system = '2.16.840.1.113883.12.292';
} elseif (self::CODE_TYPE_ICD10PCS == $codeType) {
$system = '2.16.840.1.113883.6.4';
}
} else {
if (self::CODE_TYPE_SNOMED_CT == $codeType) {
$system = FhirCodeSystemConstants::SNOMED_CT;
} elseif (self::CODE_TYPE_SNOMED == $codeType) {
$system = FhirCodeSystemConstants::SNOMED_CT;
} elseif (self::CODE_TYPE_NUCC == $codeType) {
$system = FhirCodeSystemConstants::NUCC_PROVIDER;
} elseif (self::CODE_TYPE_LOINC == $codeType) {
$system = FhirCodeSystemConstants::LOINC;
} elseif (self::CODE_TYPE_RXNORM == $codeType || self::CODE_TYPE_RXCUI == $codeType) {
$system = FhirCodeSystemConstants::RXNORM;
}
}
if (empty($system)) {
foreach (self::CODE_TYPE_OID as $oid => $system_code) {
if ($system_code == $codeType) {
return $oid;
}
}
}
return $system;
}
/**
* @param string $type
* @return string
*/
public function formatCodeType($type)
{
switch (strtoupper(trim($type))) {
case 'ICD10':
case 'ICD10-CM':
case 'ICD-10-CM':
case 'ICD10CM':
$type = 'ICD10';
break;
case 'SNOMED CT':
case 'SNOMED-CT':
case 'SNOMEDCT':
$type = 'SNOMED-CT';
if (!$this->isSnomedCodesInstalled()) {
$type = 'SNOMED CT'; // for valueset table lookups
break;
}
break;
case 'RXCUI':
case 'RXNORM':
if (!$this->isRXNORMInstalled() && $this->isInstalledCodeType('RXCUI')) {
$type = 'RXCUI'; // then let's use RxCUI for lookups
} else {
$type = 'RXNORM';
}
break;
case 'CPT':
case 'CPT4':
$type = 'CPT4';
break;
default:
if (strpos($type, '2.16.840.1.113883.') !== false) {
$type = $this->getCodeSystemNameFromSystem($type);
}
}
return $type ?? "";
}
public function getCodeSystemNameFromSystem($oid): string
{
return self::CODE_TYPE_OID[$oid] ?? '';
}
/**
* Returns a resolved code set including using valueset table to try and get a code set.
*
* @param $code
* @param $codeType
* @param string $currentCodeText
* @param string $codeDescriptionType
* @return array
*/
public function resolveCode($code, $codeType, $currentCodeText = '', $codeDescriptionType = 'code_text')
{
$valueset = '';
$valueset_name = '';
$default = array(
'code' => $code ?? '',
'formatted_code' => $code . ':' . $codeType,
'formatted_code_type' => $codeType ?? '',
'code_text' => $currentCodeText,
'system_oid' => '',
'valueset' => '',
'valueset_name' => ''
);
if (empty($code)) {
$default['formatted_code'] = '';
return $default;
}
$formatted_type = $this->formatCodeType($codeType ?: '');
$oid = $this->getSystemForCodeType($formatted_type, true);
$formatted_code = $this->getCodeWithType($code ?? '', $formatted_type);
if (empty($currentCodeText) && $this->isInstalledCodeType($formatted_type)) {
$currentCodeText = $this->lookup_code_description($formatted_code, $codeDescriptionType);
}
// use valueset table if code description not found.
if (empty($currentCodeText)) {
if (strpos($codeType, '2.16.840.1.113883.') !== false) {
$oid = trim($codeType);
$codeType = "";
}
$value = $this->lookupFromValueset($code, $formatted_type, $oid);
$formatted_type = ($value['code_type'] ?? null) ?: $formatted_type;
if (!empty($code) && !empty($formatted_type)) {
$formatted_code = $formatted_type . ':' . $code;
}
$oid = $value['code_system'] ?? '';
$currentCodeText = $value['description'] ?? '';
$valueset_name = $value['valueset_name'] ?? '';
$valueset = $value['valueset'] ?? '';
}
return array(
'code' => $code ?? "",
'formatted_code' => $formatted_code ?: $code,
'formatted_code_type' => $formatted_type ?: $codeType,
'code_text' => trim($currentCodeText),
'system_oid' => $oid ?? "",
'valueset' => $valueset ?? "",
'valueset_name' => $valueset_name ?? ""
);
}
public function getInstalledCodeTypes()
{
return $this->installedCodeTypes ?? null;
}
public function lookupFromValueset($code, $codeType, $codeSystem)
{
if (empty($codeSystem) && empty($codeType)) {
$value = sqlQuery(
"Select * From valueset Where code = ? LIMIT 1",
array($code)
);
} else {
$value = sqlQuery(
"Select * From valueset Where code = ? And (code_type = ? Or code_type LIKE ? Or code_system = ?)",
array($code, $codeType, "$codeType%", $codeSystem)
);
}
return $value;
}
public function dischargeOptionIdFromCode($formatted_code)
{
$listService = new ListService();
$ret = $listService->getOptionsByListName('discharge-disposition', ['codes' => $formatted_code]) ?? '';
return $ret[0]['option_id'] ?? '';
}
public function dischargeCodeFromOptionId($option_id)
{
$listService = new ListService();
return $listService->getListOption('discharge-disposition', $option_id)['codes'] ?? '';
}
public function parseCodesIntoCodeableConcepts($codes)
{
$codes = explode(";", $codes);
$codeableConcepts = array();
foreach ($codes as $codeItem) {
$parsedCode = $this->parseCode($codeItem);
$codeType = $parsedCode['code_type'];
$code = $parsedCode['code'];
$system = $this->getSystemForCodeType($codeType);
$codedesc = $this->lookup_code_description($codeItem);
$codeableConcepts[$code] = [
'code' => $code
, 'description' => $codedesc
, 'code_type' => $codeType
, 'system' => $system
];
}
return $codeableConcepts;
}
}