The Second Reminders email bug fix - contributed by arnabnaha
[openemr.git] / custom / code_types.inc.php
blob58744583b341fe170082e68876444dcc573e346b
1 <?php
2 /**
3 * Library and data structure to manage Code Types and code type lookups.
5 * The data structure is the $code_types array.
6 * The $code_types array is built from the code_types sql table and provides
7 * abstraction of diagnosis/billing code types. This is desirable
8 * because different countries or fields of practice use different methods for
9 * coding diagnoses, procedures and supplies. Fees will not be relevant where
10 * medical care is socialized.
11 * <pre>Attributes of the $code_types array are:
12 * active - 1 if this code type is activated
13 * id - the numeric identifier of this code type in the codes table
14 * claim - 1 if this code type is used in claims
15 * fee - 1 if fees are used, else 0
16 * mod - the maximum length of a modifier, 0 if modifiers are not used
17 * just - the code type used for justification, empty if none
18 * rel - 1 if other billing codes may be "related" to this code type
19 * nofs - 1 if this code type should NOT appear in the Fee Sheet
20 * diag - 1 if this code type is for diagnosis
21 * proc - 1 if this code type is a procedure/service
22 * label - label used for code type
23 * external - 0 for storing codes in the code table
24 * 1 for storing codes in external ICD10 Diagnosis tables
25 * 2 for storing codes in external SNOMED (RF1) Diagnosis tables
26 * 3 for storing codes in external SNOMED (RF2) Diagnosis tables
27 * 4 for storing codes in external ICD9 Diagnosis tables
28 * 5 for storing codes in external ICD9 Procedure/Service tables
29 * 6 for storing codes in external ICD10 Procedure/Service tables
30 * 7 for storing codes in external SNOMED Clinical Term tables
31 * term - 1 if this code type is used as a clinical term
32 * </pre>
34 * Copyright (C) 2006-2010 Rod Roark <rod@sunsetsystems.com>
36 * LICENSE: This program is free software; you can redistribute it and/or
37 * modify it under the terms of the GNU General Public License
38 * as published by the Free Software Foundation; either version 2
39 * of the License, or (at your option) any later version.
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 * You should have received a copy of the GNU General Public License
45 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
47 * @package OpenEMR
48 * @author Rod Roark <rod@sunsetsystems.com>
49 * @author Brady Miller <brady@sparmy.com>
50 * @link http://www.open-emr.org
53 require_once(dirname(__FILE__)."/../library/csv_like_join.php");
55 $code_types = array();
56 $default_search_type = '';
57 $ctres = sqlStatement("SELECT * FROM code_types WHERE ct_active=1 ORDER BY ct_seq, ct_key");
58 while ($ctrow = sqlFetchArray($ctres)) {
59 $code_types[$ctrow['ct_key']] = array(
60 'active' => $ctrow['ct_active' ],
61 'id' => $ctrow['ct_id' ],
62 'fee' => $ctrow['ct_fee' ],
63 'mod' => $ctrow['ct_mod' ],
64 'just' => $ctrow['ct_just'],
65 'rel' => $ctrow['ct_rel' ],
66 'nofs' => $ctrow['ct_nofs'],
67 'diag' => $ctrow['ct_diag'],
68 'mask' => $ctrow['ct_mask'],
69 'label'=> ( (empty($ctrow['ct_label'])) ? $ctrow['ct_key'] : $ctrow['ct_label'] ),
70 'external'=> $ctrow['ct_external'],
71 'claim' => $ctrow['ct_claim'],
72 'proc' => $ctrow['ct_proc'],
73 'term' => $ctrow['ct_term']
75 if ($default_search_type === '') $default_search_type = $ctrow['ct_key'];
78 /**
79 * This array stores the external table options. See above for $code_types array
80 * 'external' attribute for explanation of the option listings.
81 * @var array
83 $cd_external_options = array(
84 '0' => xl('No'),
85 '4' => xl('ICD9 Diagnosis'),
86 '5' => xl('ICD9 Procedure/Service'),
87 '1' => xl('ICD10 Diagnosis'),
88 '6' => xl('ICD10 Procedure/Service'),
89 '2' => xl('SNOMED (RF1) Diagnosis'),
90 '3' => xl('SNOMED (RF2) Diagnosis'),
91 '7' => xl('SNOMED Clinical Term')
94 /**
95 * Checks is fee are applicable to any of the code types.
97 * @return boolean
99 function fees_are_used() {
100 global $code_types;
101 foreach ($code_types as $value) { if ($value['fee'] && $value['active']) return true; }
102 return false;
106 * Checks is modifiers are applicable to any of the code types.
107 * (If a code type is not set to show in the fee sheet, then is ignored)
109 * @param boolean $fee_sheet Will ignore code types that are not shown in the fee sheet
110 * @return boolean
112 function modifiers_are_used($fee_sheet=false) {
113 global $code_types;
114 foreach ($code_types as $value) {
115 if ($fee_sheet && !empty($value['nofs'])) continue;
116 if ($value['mod'] && $value['active']) return true;
118 return false;
122 * Checks if justifiers are applicable to any of the code types.
124 * @return boolean
126 function justifiers_are_used() {
127 global $code_types;
128 foreach ($code_types as $value) { if (!empty($value['just']) && $value['active']) return true; }
129 return false;
133 * Checks is related codes are applicable to any of the code types.
135 * @return boolean
137 function related_codes_are_used() {
138 global $code_types;
139 foreach ($code_types as $value) { if ($value['rel'] && $value['active']) return true; }
140 return false;
144 * Convert a code type id (ct_id) to the key string (ct_key)
146 * @param integer $id
147 * @return string
149 function convert_type_id_to_key($id) {
150 global $code_types;
151 foreach ($code_types as $key => $value) {
152 if ($value['id'] == $id) return $key;
157 * Return listing of pertinent and active code types.
159 * Function will return listing (ct_key) of pertinent
160 * active code types, such as diagnosis codes or procedure
161 * codes in a chosen format. Supported returned formats include
162 * as 1) an array and as 2) a comma-separated lists that has been
163 * process by urlencode() in order to place into URL address safely.
165 * @param string $category category of code types('diagnosis', 'procedure' or 'clinical_term')
166 * @param string $return_format format or returned code types ('array' or 'csv')
167 * @return string/array
169 function collect_codetypes($category,$return_format="array") {
170 global $code_types;
172 $return = array();
174 foreach ($code_types as $ct_key => $ct_arr) {
175 if (!$ct_arr['active']) continue;
177 if ($category == "diagnosis") {
178 if ($ct_arr['diag']) {
179 array_push($return,$ct_key);
182 else if ($category == "procedure") {
183 if ($ct_arr['proc']) {
184 array_push($return,$ct_key);
187 else if ($category == "clinical_term") {
188 if ($ct_arr['term']) {
189 array_push($return,$ct_key);
192 else {
193 //return nothing since no supported category was chosen
197 if ($return_format == "csv") {
198 //return it as a csv string
199 return csv_like_join($return);
201 else { //$return_format == "array"
202 //return the array
203 return $return;
208 * Main code set searching function.
210 * Function is able to search a variety of code sets. See the 'external' items in the comments at top
211 * of this page for a listing of the code sets supported. Also note that Products (using PROD as code type)
212 * is also supported.
214 * @param string $form_code_type code set key (special keywords are PROD and --ALL--)
215 * @param string $search_term search term
216 * @param boolean $count if true, then will only return the number of entries
217 * @param boolean $active if true, then will only return active entries (not pertinent for PROD code sets)
218 * @param boolean $return_only_one if true, then will only return one perfect matching item
219 * @param integer $start Query start limit
220 * @param integer $number Query number returned
221 * @param array $filter_elements Array that contains elements to filter
222 * @return recordset
224 function code_set_search($form_code_type,$search_term="",$count=false,$active=true,$return_only_one=false,$start=NULL,$number=NULL,$filter_elements=array()) {
225 global $code_types;
227 $limit_query = '';
228 if ( !is_null($start) && !is_null($number) ) {
229 $limit_query = " LIMIT $start, $number ";
231 if ($return_only_one) {
232 $limit_query = " LIMIT 1 ";
235 // build the filter_elements sql code
236 $query_filter_elements="";
237 if (!empty($filter_elements)) {
238 foreach ($filter_elements as $key => $element) {
239 $query_filter_elements .= " AND c." . add_escape_custom($key) . "=" . "'" . add_escape_custom($element) . "' ";
243 if ($form_code_type == 'PROD') { // Search for products/drugs
244 $query = "SELECT dt.drug_id, dt.selector, d.name " .
245 "FROM drug_templates AS dt, drugs AS d WHERE " .
246 "( d.name LIKE ? OR " .
247 "dt.selector LIKE ? ) " .
248 "AND d.drug_id = dt.drug_id " .
249 "ORDER BY d.name, dt.selector, dt.drug_id $limit_query";
250 $res = sqlStatement($query, array("%".$search_term."%", "%".$search_term."%") );
252 else if ($form_code_type == '--ALL--') { // Search all codes from the default codes table
253 // Note this will not search the external code sets
254 $active_query = '';
255 if ($active) {
256 // Only filter for active codes
257 $active_query=" AND c.active = 1 ";
259 $query = "SELECT c.id, c.code_text, c.code_text_short, c.code, c.code_type, c.modifier, c.units, c.fee, " .
260 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
261 "ct.ct_key as code_type_name " .
262 "FROM `codes` as c " .
263 "LEFT OUTER JOIN `code_types` as ct " .
264 "ON c.code_type = ct.ct_id " .
265 "WHERE (c.code_text LIKE ? OR " .
266 "c.code LIKE ?) AND ct.ct_external = '0' " .
267 " $active_query " .
268 " $query_filter_elements " .
269 "ORDER BY code_type,code+0,code $limit_query";
270 $res = sqlStatement($query, array("%".$search_term."%", "%".$search_term."%") );
272 else if ( !($code_types[$form_code_type]['external']) ) { // Search from default codes table
273 $active_query = '';
274 if ($active) {
275 // Only filter for active codes
276 $active_query=" AND c.active = 1 ";
278 $sql_bind_array = array();
279 $query = "SELECT c.id, c.code_text, c.code_text_short, c.code, c.code_type, c.modifier, c.units, c.fee, " .
280 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
281 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
282 "FROM `codes` as c ";
283 if ($return_only_one) {
284 $query .= "WHERE c.code = ? ";
285 array_push($sql_bind_array,$search_term);
287 else {
288 $query .= "WHERE (c.code_text LIKE ? OR c.code LIKE ?) ";
289 array_push($sql_bind_array,"%".$search_term."%", "%".$search_term."%");
291 $query .= "AND c.code_type = ? $active_query $query_filter_elements " .
292 "ORDER BY c.code+0,c.code $limit_query";
293 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
294 $res = sqlStatement($query,$sql_bind_array);
296 else if ($code_types[$form_code_type]['external'] == 1 ) { // Search from ICD10 diagnosis codeset tables
297 $active_query = '';
298 if ($active) {
299 // Only filter for active codes
300 // If there is no entry in codes sql table, then default to active
301 // (this is reason for including NULL below)
302 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
304 // Ensure the icd10_dx_order_code sql table exists
305 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_dx_order_code'");
306 if ( !(empty($check_table)) ) {
307 $sql_bind_array = array();
308 $query = "SELECT ref.formatted_dx_code as code, ref.long_desc as code_text, " .
309 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
310 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
311 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
312 "FROM `icd10_dx_order_code` as ref " .
313 "LEFT OUTER JOIN `codes` as c " .
314 "ON ref.formatted_dx_code = c.code AND c.code_type = ? ";
315 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
316 if ($return_only_one) {
317 $query .= "WHERE ref.formatted_dx_code = ? AND ref.valid_for_coding = '1' AND ref.active = '1' $active_query $query_filter_elements ";
318 array_push($sql_bind_array,$search_term);
320 else {
321 $query .= "WHERE (ref.long_desc LIKE ? OR ref.formatted_dx_code LIKE ?) AND ref.valid_for_coding = '1' AND ref.active = '1' $active_query $query_filter_elements ";
322 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
324 $query .= "ORDER BY ref.formatted_dx_code+0, ref.formatted_dx_code $limit_query";
325 $res = sqlStatement($query,$sql_bind_array);
328 else if ($code_types[$form_code_type]['external'] == 2 || $code_types[$form_code_type]['external'] == 7) {
329 // Search from SNOMED (RF1) diagnosis codeset tables OR Search from SNOMED (RF1) clinical terms codeset tables
330 if ($code_types[$form_code_type]['external'] == 2) {
331 // Search from SNOMED (RF1) diagnosis codeset tables
332 $diagnosis_sql_specific = " ref.FullySpecifiedName LIKE '%(disorder)' ";
334 else {
335 // Search from SNOMED (RF1) clinical terms codeset tables
336 $diagnosis_sql_specific = " 1=1 ";
338 if ($active) {
339 // Only filter for active codes
340 // If there is no entry in codes sql table, then default to active
341 // (this is reason for including NULL below)
342 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
344 // Ensure the sct_concepts sql table exists
345 $check_table = sqlQuery("SHOW TABLES LIKE 'sct_concepts'");
346 if ( !(empty($check_table)) ) {
347 $sql_bind_array = array();
348 $query = "SELECT ref.ConceptId as code, ref.FullySpecifiedName as code_text, " .
349 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
350 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
351 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
352 "FROM `sct_concepts` as ref " .
353 "LEFT OUTER JOIN `codes` as c " .
354 "ON ref.ConceptId = c.code AND c.code_type = ? ";
355 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
356 if ($return_only_one) {
357 $query .= "WHERE (ref.ConceptId = ? AND $diagnosis_sql_specific) $active_query $query_filter_elements ";
358 array_push($sql_bind_array,$search_term);
360 else {
361 $query .= "WHERE ((ref.FullySpecifiedName LIKE ? OR ref.ConceptId LIKE ?) AND $diagnosis_sql_specific ) $active_query $query_filter_elements ";
362 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
364 $query .= "AND ref.ConceptStatus = 0 " .
365 "ORDER BY ref.ConceptId $limit_query";
366 $res = sqlStatement($query,$sql_bind_array);
369 else if ($code_types[$form_code_type]['external'] == 3 ) { // Search from SNOMED (RF2) diagnosis codeset tables
370 //placeholder
372 else if ($code_types[$form_code_type]['external'] == 4 ) { // Search from ICD9 diagnosis codeset tables
373 if ($active) {
374 // Only filter for active codes
375 // If there is no entry in codes sql table, then default to active
376 // (this is reason for including NULL below)
377 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
379 // Ensure the icd9_dx_code sql table exists
380 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_dx_code'");
381 if ( !(empty($check_table)) ) {
382 $sql_bind_array = array();
383 $query = "SELECT ref.formatted_dx_code as code, ref.long_desc as code_text, " .
384 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
385 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
386 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
387 "FROM `icd9_dx_code` as ref " .
388 "LEFT OUTER JOIN `codes` as c " .
389 "ON ref.formatted_dx_code = c.code AND c.code_type = ? ";
390 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
391 if ($return_only_one) {
392 $query .= "WHERE ref.formatted_dx_code = ? AND ref.active = '1' $active_query $query_filter_elements ";
393 array_push($sql_bind_array,$search_term);
395 else {
396 $query .= "WHERE (ref.long_desc LIKE ? OR ref.formatted_dx_code LIKE ?) AND ref.active = '1' $active_query $query_filter_elements ";
397 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
399 $query .= "ORDER BY ref.formatted_dx_code+0, ref.formatted_dx_code $limit_query";
400 $res = sqlStatement($query,$sql_bind_array);
403 else if ($code_types[$form_code_type]['external'] == 5 ) { // Search from ICD9 Procedure/Service codeset tables
404 if ($active) {
405 // Only filter for active codes
406 // If there is no entry in codes sql table, then default to active
407 // (this is reason for including NULL below)
408 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
410 // Ensure the icd9_sg_code sql table exists
411 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_sg_code'");
412 if ( !(empty($check_table)) ) {
413 $sql_bind_array = array();
414 $query = "SELECT ref.formatted_sg_code as code, ref.long_desc as code_text, " .
415 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
416 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, " .
417 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
418 "FROM `icd9_sg_code` as ref " .
419 "LEFT OUTER JOIN `codes` as c " .
420 "ON ref.formatted_sg_code = c.code AND c.code_type = ? ";
421 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
422 if ($return_only_one) {
423 $query .= "WHERE ref.formatted_sg_code = ? AND ref.active = '1' $active_query ";
424 array_push($sql_bind_array,$search_term);
426 else {
427 $query .= "WHERE (ref.long_desc LIKE ? OR ref.formatted_sg_code LIKE ?) AND ref.active = '1' $active_query ";
428 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
430 $query .= "ORDER BY ref.formatted_sg_code+0, ref.formatted_sg_code $limit_query";
431 $res = sqlStatement($query,$sql_bind_array);
434 else if ($code_types[$form_code_type]['external'] == 6 ) { // Search from ICD10 Procedure/Service codeset tables
435 $active_query = '';
436 if ($active) {
437 // Only filter for active codes
438 // If there is no entry in codes sql table, then default to active
439 // (this is reason for including NULL below)
440 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
442 // Ensure the icd10_dx_order_code sql table exists
443 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_pcs_order_code'");
444 if ( !(empty($check_table)) ) {
445 $sql_bind_array = array();
446 $query = "SELECT ref.pcs_code as code, ref.long_desc as code_text, " .
447 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
448 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, " .
449 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
450 "FROM `icd10_pcs_order_code` as ref " .
451 "LEFT OUTER JOIN `codes` as c " .
452 "ON ref.pcs_code = c.code AND c.code_type = ? ";
453 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
454 if ($return_only_one) {
455 $query .= "WHERE ref.pcs_code = ? AND ref.valid_for_coding = '1' AND ref.active = '1' $active_query ";
456 array_push($sql_bind_array,$search_term);
458 else {
459 $query .= "WHERE (ref.long_desc LIKE ? OR ref.pcs_code LIKE ?) AND ref.valid_for_coding = '1' AND ref.active = '1' $active_query ";
460 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
462 $query .= "ORDER BY ref.pcs_code+0, ref.pcs_code $limit_query";
463 $res = sqlStatement($query,$sql_bind_array);
466 else {
467 //using an external code that is not yet supported, so skip.
469 if (isset($res)) {
470 if ($count) {
471 // just return the count
472 return sqlNumRows($res);
474 else {
475 // return the data
476 return $res;
482 * Lookup Code Descriptions for one or more billing codes.
484 * Function is able to lookup code descriptions from a variety of code sets. See the 'external'
485 * items in the comments at top of this page for a listing of the code sets supported.
487 * @param string $codes Is of the form "type:code;type:code; etc.".
488 * @return string Is of the form "description;description; etc.".
490 function lookup_code_descriptions($codes) {
491 global $code_types;
492 $code_text = '';
493 if (!empty($codes)) {
494 $relcodes = explode(';', $codes);
495 foreach ($relcodes as $codestring) {
496 if ($codestring === '') continue;
497 list($codetype, $code) = explode(':', $codestring);
498 if ( !($code_types[$codetype]['external']) ) { // Collect from default codes table
499 $wheretype = "";
500 $sqlArray = array();
501 if (empty($code)) {
502 $code = $codetype;
503 } else {
504 $wheretype = "code_type = ? AND ";
505 array_push($sqlArray,$code_types[$codetype]['id']);
507 $sql = "SELECT code_text FROM codes WHERE " .
508 "$wheretype code = ? ORDER BY id LIMIT 1";
509 array_push($sqlArray,$code);
510 $crow = sqlQuery($sql,$sqlArray);
511 if (!empty($crow['code_text'])) {
512 if ($code_text) $code_text .= '; ';
513 $code_text .= $crow['code_text'];
516 else if ($code_types[$codetype]['external'] == 1) { // Collect from ICD10 Diagnosis codeset tables
517 // Ensure the icd10_dx_order_code sql table exists
518 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_dx_order_code'");
519 if ( !(empty($check_table)) ) {
520 if ( !(empty($code)) ) {
521 // Will grab from previous inactive revisions if unable to find in current revision
522 $sql = "SELECT `long_desc` FROM `icd10_dx_order_code` " .
523 "WHERE `formatted_dx_code` = ? ORDER BY `revision` DESC LIMIT 1";
524 $crow = sqlQuery($sql, array($code) );
525 if (!empty($crow['long_desc'])) {
526 if ($code_text) $code_text .= '; ';
527 $code_text .= $crow['long_desc'];
532 else if ($code_types[$codetype]['external'] == 2 || $code_types[$codetype]['external'] == 7) {
533 // Collect from SNOMED (RF1) Diagnosis codeset tables OR Search from SNOMED (RF1) clinical terms codeset tables
534 // Ensure the sct_concepts sql table exists
535 $check_table = sqlQuery("SHOW TABLES LIKE 'sct_concepts'");
536 if ( !(empty($check_table)) ) {
537 if ( !(empty($code)) ) {
538 $sql = "SELECT `FullySpecifiedName` FROM `sct_concepts` " .
539 "WHERE `ConceptId` = ? AND `ConceptStatus` = 0 LIMIT 1";
540 $crow = sqlQuery($sql, array($code) );
541 if (!empty($crow['FullySpecifiedName'])) {
542 if ($code_text) $code_text .= '; ';
543 $code_text .= $crow['FullySpecifiedName'];
548 else if ($code_types[$codetype]['external'] == 3) { // Collect from SNOMED (RF2) Diagnosis codeset tables
549 //placeholder
551 else if ($code_types[$codetype]['external'] == 4) { // Collect from ICD9 Diagnosis codeset tables
552 // Ensure the icd9_dx_code sql table exists
553 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_dx_code'");
554 if ( !(empty($check_table)) ) {
555 if ( !(empty($code)) ) {
556 // Will grab from previous inactive revisions if unable to find in current revision
557 $sql = "SELECT `long_desc` FROM `icd9_dx_code` " .
558 "WHERE `formatted_dx_code` = ? ORDER BY `revision` DESC LIMIT 1";
559 $crow = sqlQuery($sql, array($code) );
560 if (!empty($crow['long_desc'])) {
561 if ($code_text) $code_text .= '; ';
562 $code_text .= $crow['long_desc'];
567 else if ($code_types[$codetype]['external'] == 5) { // Collect from ICD9 Procedure/Service codeset tables
568 // Ensure the icd9_dx_code sql table exists
569 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_sg_code'");
570 if ( !(empty($check_table)) ) {
571 if ( !(empty($code)) ) {
572 // Will grab from previous inactive revisions if unable to find in current revision
573 $sql = "SELECT `long_desc` FROM `icd9_sg_code` " .
574 "WHERE `formatted_sg_code` = ? ORDER BY `revision` DESC LIMIT 1";
575 $crow = sqlQuery($sql, array($code) );
576 if (!empty($crow['long_desc'])) {
577 if ($code_text) $code_text .= '; ';
578 $code_text .= $crow['long_desc'];
583 else if ($code_types[$codetype]['external'] == 6) { // Collect from ICD10 PRocedure/Service codeset tables
584 // Ensure the icd10_dx_order_code sql table exists
585 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_pcs_order_code'");
586 if ( !(empty($check_table)) ) {
587 if ( !(empty($code)) ) {
588 // Will grab from previous inactive revisions if unable to find in current revision
589 $sql = "SELECT `long_desc` FROM `icd10_pcs_order_code` " .
590 "WHERE `pcs_code` = ? ORDER BY `revision` DESC LIMIT 1";
591 $crow = sqlQuery($sql, array($code) );
592 if (!empty($crow['long_desc'])) {
593 if ($code_text) $code_text .= '; ';
594 $code_text .= $crow['long_desc'];
600 else {
601 //using an external code that is not yet supported, so skip.
605 return $code_text;