Support new security model in the formSubmit function - bug fix
[openemr.git] / custom / code_types.inc.php
blob17c98bccb9bc22e921fcc79dd98f713494910d3b
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 * </pre>
32 * Copyright (C) 2006-2010 Rod Roark <rod@sunsetsystems.com>
34 * LICENSE: This program is free software; you can redistribute it and/or
35 * modify it under the terms of the GNU General Public License
36 * as published by the Free Software Foundation; either version 2
37 * of the License, or (at your option) any later version.
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
45 * @package OpenEMR
46 * @author Rod Roark <rod@sunsetsystems.com>
47 * @author Brady Miller <brady@sparmy.com>
48 * @link http://www.open-emr.org
51 require_once(dirname(__FILE__)."/../library/csv_like_join.php");
53 $code_types = array();
54 $default_search_type = '';
55 $ctres = sqlStatement("SELECT * FROM code_types WHERE ct_active=1 ORDER BY ct_seq, ct_key");
56 while ($ctrow = sqlFetchArray($ctres)) {
57 $code_types[$ctrow['ct_key']] = array(
58 'active' => $ctrow['ct_active' ],
59 'id' => $ctrow['ct_id' ],
60 'fee' => $ctrow['ct_fee' ],
61 'mod' => $ctrow['ct_mod' ],
62 'just' => $ctrow['ct_just'],
63 'rel' => $ctrow['ct_rel' ],
64 'nofs' => $ctrow['ct_nofs'],
65 'diag' => $ctrow['ct_diag'],
66 'mask' => $ctrow['ct_mask'],
67 'label'=> ( (empty($ctrow['ct_label'])) ? $ctrow['ct_key'] : $ctrow['ct_label'] ),
68 'external'=> $ctrow['ct_external'],
69 'claim' => $ctrow['ct_claim'],
70 'proc' => $ctrow['ct_proc'],
72 if ($default_search_type === '') $default_search_type = $ctrow['ct_key'];
75 /**
76 * This array stores the external table options. See above for $code_types array
77 * 'external' attribute for explanation of the option listings.
78 * @var array
80 $cd_external_options = array(
81 '0' => xl('No'),
82 '4' => xl('ICD9 Diagnosis'),
83 '5' => xl('ICD9 Procedure/Service'),
84 '1' => xl('ICD10 Diagnosis'),
85 '6' => xl('ICD10 Procedure/Service'),
86 '2' => xl('SNOMED (RF1) Diagnosis'),
87 '3' => xl('SNOMED (RF2) Diagnosis'),
90 /**
91 * Checks is fee are applicable to any of the code types.
93 * @return boolean
95 function fees_are_used() {
96 global $code_types;
97 foreach ($code_types as $value) { if ($value['fee'] && $value['active']) return true; }
98 return false;
102 * Checks is modifiers are applicable to any of the code types.
103 * (If a code type is not set to show in the fee sheet, then is ignored)
105 * @param boolean $fee_sheet Will ignore code types that are not shown in the fee sheet
106 * @return boolean
108 function modifiers_are_used($fee_sheet=false) {
109 global $code_types;
110 foreach ($code_types as $value) {
111 if ($fee_sheet && !empty($value['nofs'])) continue;
112 if ($value['mod'] && $value['active']) return true;
114 return false;
118 * Checks if justifiers are applicable to any of the code types.
120 * @return boolean
122 function justifiers_are_used() {
123 global $code_types;
124 foreach ($code_types as $value) { if (!empty($value['just']) && $value['active']) return true; }
125 return false;
129 * Checks is related codes are applicable to any of the code types.
131 * @return boolean
133 function related_codes_are_used() {
134 global $code_types;
135 foreach ($code_types as $value) { if ($value['rel'] && $value['active']) return true; }
136 return false;
140 * Convert a code type id (ct_id) to the key string (ct_key)
142 * @param integer $id
143 * @return string
145 function convert_type_id_to_key($id) {
146 global $code_types;
147 foreach ($code_types as $key => $value) {
148 if ($value['id'] == $id) return $key;
153 * Return listing of pertinent and active code types.
155 * Function will return listing (ct_key) of pertinent
156 * active code types, such as diagnosis codes or procedure
157 * codes in a chosen format. Supported returned formats include
158 * as 1) an array and as 2) a comma-separated lists that has been
159 * process by urlencode() in order to place into URL address safely.
161 * @param string $category category of code types('diagnosis' or 'procedure')
162 * @param string $return_format format or returned code types ('array' or 'csv')
163 * @return string/array
165 function collect_codetypes($category,$return_format="array") {
166 global $code_types;
168 $return = array();
170 foreach ($code_types as $ct_key => $ct_arr) {
171 if (!$ct_arr['active']) continue;
173 if ($category == "diagnosis") {
174 if ($ct_arr['diag']) {
175 array_push($return,$ct_key);
178 else if ($category == "procedure") {
179 if ($ct_arr['proc']) {
180 array_push($return,$ct_key);
183 else {
184 //return nothing since no supported category was chosen
188 if ($return_format == "csv") {
189 //return it as a csv string
190 return csv_like_join($return);
192 else { //$return_format == "array"
193 //return the array
194 return $return;
199 * Main code set searching function.
201 * Function is able to search a variety of code sets. See the 'external' items in the comments at top
202 * of this page for a listing of the code sets supported. Also note that Products (using PROD as code type)
203 * is also supported.
205 * @param string $form_code_type code set key (special keywords are PROD and --ALL--)
206 * @param string $search_term search term
207 * @param boolean $count if true, then will only return the number of entries
208 * @param boolean $active if true, then will only return active entries (not pertinent for PROD code sets)
209 * @param boolean $return_only_one if true, then will only return one perfect matching item
210 * @param integer $start Query start limit
211 * @param integer $number Query number returned
212 * @param array $filter_elements Array that contains elements to filter
213 * @return recordset
215 function code_set_search($form_code_type,$search_term="",$count=false,$active=true,$return_only_one=false,$start=NULL,$number=NULL,$filter_elements=array()) {
216 global $code_types;
218 $limit_query = '';
219 if ( !is_null($start) && !is_null($number) ) {
220 $limit_query = " LIMIT $start, $number ";
222 if ($return_only_one) {
223 $limit_query = " LIMIT 1 ";
226 // build the filter_elements sql code
227 $query_filter_elements="";
228 if (!empty($filter_elements)) {
229 foreach ($filter_elements as $key => $element) {
230 $query_filter_elements .= " AND c." . add_escape_custom($key) . "=" . "'" . add_escape_custom($element) . "' ";
234 if ($form_code_type == 'PROD') { // Search for products/drugs
235 $query = "SELECT dt.drug_id, dt.selector, d.name " .
236 "FROM drug_templates AS dt, drugs AS d WHERE " .
237 "( d.name LIKE ? OR " .
238 "dt.selector LIKE ? ) " .
239 "AND d.drug_id = dt.drug_id " .
240 "ORDER BY d.name, dt.selector, dt.drug_id $limit_query";
241 $res = sqlStatement($query, array("%".$search_term."%", "%".$search_term."%") );
243 else if ($form_code_type == '--ALL--') { // Search all codes from the default codes table
244 // Note this will not search the external code sets
245 $active_query = '';
246 if ($active) {
247 // Only filter for active codes
248 $active_query=" AND c.active = 1 ";
250 $query = "SELECT c.id, c.code_text, c.code_text_short, c.code, c.code_type, c.modifier, c.units, c.fee, " .
251 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
252 "ct.ct_key as code_type_name " .
253 "FROM `codes` as c " .
254 "LEFT OUTER JOIN `code_types` as ct " .
255 "ON c.code_type = ct.ct_id " .
256 "WHERE (c.code_text LIKE ? OR " .
257 "c.code LIKE ?) AND ct.ct_external = '0' " .
258 " $active_query " .
259 " $query_filter_elements " .
260 "ORDER BY code_type,code+0,code $limit_query";
261 $res = sqlStatement($query, array("%".$search_term."%", "%".$search_term."%") );
263 else if ( !($code_types[$form_code_type]['external']) ) { // Search from default codes table
264 $active_query = '';
265 if ($active) {
266 // Only filter for active codes
267 $active_query=" AND c.active = 1 ";
269 $sql_bind_array = array();
270 $query = "SELECT c.id, c.code_text, c.code_text_short, c.code, c.code_type, c.modifier, c.units, c.fee, " .
271 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
272 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
273 "FROM `codes` as c ";
274 if ($return_only_one) {
275 $query .= "WHERE c.code = ? ";
276 array_push($sql_bind_array,$search_term);
278 else {
279 $query .= "WHERE (c.code_text LIKE ? OR c.code LIKE ?) ";
280 array_push($sql_bind_array,"%".$search_term."%", "%".$search_term."%");
282 $query .= "AND c.code_type = ? $active_query $query_filter_elements " .
283 "ORDER BY c.code+0,c.code $limit_query";
284 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
285 $res = sqlStatement($query,$sql_bind_array);
287 else if ($code_types[$form_code_type]['external'] == 1 ) { // Search from ICD10 diagnosis codeset tables
288 $active_query = '';
289 if ($active) {
290 // Only filter for active codes
291 // If there is no entry in codes sql table, then default to active
292 // (this is reason for including NULL below)
293 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
295 // Ensure the icd10_dx_order_code sql table exists
296 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_dx_order_code'");
297 if ( !(empty($check_table)) ) {
298 $sql_bind_array = array();
299 $query = "SELECT ref.formatted_dx_code as code, ref.long_desc as code_text, " .
300 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
301 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
302 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
303 "FROM `icd10_dx_order_code` as ref " .
304 "LEFT OUTER JOIN `codes` as c " .
305 "ON ref.formatted_dx_code = c.code AND c.code_type = ? ";
306 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
307 if ($return_only_one) {
308 $query .= "WHERE ref.formatted_dx_code = ? AND ref.valid_for_coding = '1' AND ref.active = '1' $active_query $query_filter_elements ";
309 array_push($sql_bind_array,$search_term);
311 else {
312 $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 ";
313 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
315 $query .= "ORDER BY ref.formatted_dx_code+0, ref.formatted_dx_code $limit_query";
316 $res = sqlStatement($query,$sql_bind_array);
319 else if ($code_types[$form_code_type]['external'] == 2 ) { // Search from SNOMED (RF1) diagnosis codeset tables
320 if ($active) {
321 // Only filter for active codes
322 // If there is no entry in codes sql table, then default to active
323 // (this is reason for including NULL below)
324 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
326 // Ensure the sct_concepts sql table exists
327 $check_table = sqlQuery("SHOW TABLES LIKE 'sct_concepts'");
328 if ( !(empty($check_table)) ) {
329 $sql_bind_array = array();
330 $query = "SELECT ref.ConceptId as code, ref.FullySpecifiedName as code_text, " .
331 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
332 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
333 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
334 "FROM `sct_concepts` as ref " .
335 "LEFT OUTER JOIN `codes` as c " .
336 "ON ref.ConceptId = c.code AND c.code_type = ? ";
337 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
338 if ($return_only_one) {
339 $query .= "WHERE (ref.ConceptId = ? AND ref.FullySpecifiedName LIKE '%(disorder)') $active_query $query_filter_elements ";
340 array_push($sql_bind_array,$search_term);
342 else {
343 $query .= "WHERE ((ref.FullySpecifiedName LIKE ? OR ref.ConceptId LIKE ?) AND ref.FullySpecifiedName LIKE '%(disorder)') $active_query $query_filter_elements ";
344 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
346 $query .= "AND ref.ConceptStatus = 0 " .
347 "ORDER BY ref.ConceptId $limit_query";
348 $res = sqlStatement($query,$sql_bind_array);
351 else if ($code_types[$form_code_type]['external'] == 3 ) { // Search from SNOMED (RF2) diagnosis codeset tables
352 //placeholder
354 else if ($code_types[$form_code_type]['external'] == 4 ) { // Search from ICD9 diagnosis codeset tables
355 if ($active) {
356 // Only filter for active codes
357 // If there is no entry in codes sql table, then default to active
358 // (this is reason for including NULL below)
359 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
361 // Ensure the icd9_dx_code sql table exists
362 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_dx_code'");
363 if ( !(empty($check_table)) ) {
364 $sql_bind_array = array();
365 $query = "SELECT ref.formatted_dx_code as code, ref.long_desc as code_text, " .
366 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
367 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, c.financial_reporting, " .
368 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
369 "FROM `icd9_dx_code` as ref " .
370 "LEFT OUTER JOIN `codes` as c " .
371 "ON ref.formatted_dx_code = c.code AND c.code_type = ? ";
372 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
373 if ($return_only_one) {
374 $query .= "WHERE ref.formatted_dx_code = ? AND ref.active = '1' $active_query $query_filter_elements ";
375 array_push($sql_bind_array,$search_term);
377 else {
378 $query .= "WHERE (ref.long_desc LIKE ? OR ref.formatted_dx_code LIKE ?) AND ref.active = '1' $active_query $query_filter_elements ";
379 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
381 $query .= "ORDER BY ref.formatted_dx_code+0, ref.formatted_dx_code $limit_query";
382 $res = sqlStatement($query,$sql_bind_array);
385 else if ($code_types[$form_code_type]['external'] == 5 ) { // Search from ICD9 Procedure/Service codeset tables
386 if ($active) {
387 // Only filter for active codes
388 // If there is no entry in codes sql table, then default to active
389 // (this is reason for including NULL below)
390 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
392 // Ensure the icd9_sg_code sql table exists
393 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_sg_code'");
394 if ( !(empty($check_table)) ) {
395 $sql_bind_array = array();
396 $query = "SELECT ref.formatted_sg_code as code, ref.long_desc as code_text, " .
397 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
398 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, " .
399 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
400 "FROM `icd9_sg_code` as ref " .
401 "LEFT OUTER JOIN `codes` as c " .
402 "ON ref.formatted_sg_code = c.code AND c.code_type = ? ";
403 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
404 if ($return_only_one) {
405 $query .= "WHERE ref.formatted_sg_code = ? AND ref.active = '1' $active_query ";
406 array_push($sql_bind_array,$search_term);
408 else {
409 $query .= "WHERE (ref.long_desc LIKE ? OR ref.formatted_sg_code LIKE ?) AND ref.active = '1' $active_query ";
410 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
412 $query .= "ORDER BY ref.formatted_sg_code+0, ref.formatted_sg_code $limit_query";
413 $res = sqlStatement($query,$sql_bind_array);
416 else if ($code_types[$form_code_type]['external'] == 6 ) { // Search from ICD10 Procedure/Service codeset tables
417 $active_query = '';
418 if ($active) {
419 // Only filter for active codes
420 // If there is no entry in codes sql table, then default to active
421 // (this is reason for including NULL below)
422 $active_query=" AND (c.active = 1 || c.active IS NULL) ";
424 // Ensure the icd10_dx_order_code sql table exists
425 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_pcs_order_code'");
426 if ( !(empty($check_table)) ) {
427 $sql_bind_array = array();
428 $query = "SELECT ref.pcs_code as code, ref.long_desc as code_text, " .
429 "c.id, c.code_type, c.modifier, c.units, c.fee, " .
430 "c.superbill, c.related_code, c.taxrates, c.cyp_factor, c.active, c.reportable, " .
431 "'" . add_escape_custom($form_code_type) . "' as code_type_name " .
432 "FROM `icd10_pcs_order_code` as ref " .
433 "LEFT OUTER JOIN `codes` as c " .
434 "ON ref.pcs_code = c.code AND c.code_type = ? ";
435 array_push($sql_bind_array,$code_types[$form_code_type]['id']);
436 if ($return_only_one) {
437 $query .= "WHERE ref.pcs_code = ? AND ref.valid_for_coding = '1' AND ref.active = '1' $active_query ";
438 array_push($sql_bind_array,$search_term);
440 else {
441 $query .= "WHERE (ref.long_desc LIKE ? OR ref.pcs_code LIKE ?) AND ref.valid_for_coding = '1' AND ref.active = '1' $active_query ";
442 array_push($sql_bind_array,"%".$search_term."%","%".$search_term."%");
444 $query .= "ORDER BY ref.pcs_code+0, ref.pcs_code $limit_query";
445 $res = sqlStatement($query,$sql_bind_array);
448 else {
449 //using an external code that is not yet supported, so skip.
451 if (isset($res)) {
452 if ($count) {
453 // just return the count
454 return sqlNumRows($res);
456 else {
457 // return the data
458 return $res;
464 * Lookup Code Descriptions for one or more billing codes.
466 * Function is able to lookup code descriptions from a variety of code sets. See the 'external'
467 * items in the comments at top of this page for a listing of the code sets supported.
469 * @param string $codes Is of the form "type:code;type:code; etc.".
470 * @return string Is of the form "description;description; etc.".
472 function lookup_code_descriptions($codes) {
473 global $code_types;
474 $code_text = '';
475 if (!empty($codes)) {
476 $relcodes = explode(';', $codes);
477 foreach ($relcodes as $codestring) {
478 if ($codestring === '') continue;
479 list($codetype, $code) = explode(':', $codestring);
480 if ( !($code_types[$codetype]['external']) ) { // Collect from default codes table
481 $wheretype = "";
482 $sqlArray = array();
483 if (empty($code)) {
484 $code = $codetype;
485 } else {
486 $wheretype = "code_type = ? AND ";
487 array_push($sqlArray,$code_types[$codetype]['id']);
489 $sql = "SELECT code_text FROM codes WHERE " .
490 "$wheretype code = ? ORDER BY id LIMIT 1";
491 array_push($sqlArray,$code);
492 $crow = sqlQuery($sql,$sqlArray);
493 if (!empty($crow['code_text'])) {
494 if ($code_text) $code_text .= '; ';
495 $code_text .= $crow['code_text'];
498 else if ($code_types[$codetype]['external'] == 1) { // Collect from ICD10 Diagnosis codeset tables
499 // Ensure the icd10_dx_order_code sql table exists
500 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_dx_order_code'");
501 if ( !(empty($check_table)) ) {
502 if ( !(empty($code)) ) {
503 // Will grab from previous inactive revisions if unable to find in current revision
504 $sql = "SELECT `long_desc` FROM `icd10_dx_order_code` " .
505 "WHERE `formatted_dx_code` = ? ORDER BY `revision` DESC LIMIT 1";
506 $crow = sqlQuery($sql, array($code) );
507 if (!empty($crow['long_desc'])) {
508 if ($code_text) $code_text .= '; ';
509 $code_text .= $crow['long_desc'];
514 else if ($code_types[$codetype]['external'] == 2) { // Collect from SNOMED (RF1) Diagnosis codeset tables
515 // Ensure the sct_concepts sql table exists
516 $check_table = sqlQuery("SHOW TABLES LIKE 'sct_concepts'");
517 if ( !(empty($check_table)) ) {
518 if ( !(empty($code)) ) {
519 $sql = "SELECT `FullySpecifiedName` FROM `sct_concepts` " .
520 "WHERE `ConceptId` = ? AND `ConceptStatus` = 0 LIMIT 1";
521 $crow = sqlQuery($sql, array($code) );
522 if (!empty($crow['FullySpecifiedName'])) {
523 if ($code_text) $code_text .= '; ';
524 $code_text .= $crow['FullySpecifiedName'];
529 else if ($code_types[$codetype]['external'] == 3) { // Collect from SNOMED (RF2) Diagnosis codeset tables
530 //placeholder
532 else if ($code_types[$codetype]['external'] == 4) { // Collect from ICD9 Diagnosis codeset tables
533 // Ensure the icd9_dx_code sql table exists
534 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_dx_code'");
535 if ( !(empty($check_table)) ) {
536 if ( !(empty($code)) ) {
537 // Will grab from previous inactive revisions if unable to find in current revision
538 $sql = "SELECT `long_desc` FROM `icd9_dx_code` " .
539 "WHERE `formatted_dx_code` = ? ORDER BY `revision` DESC LIMIT 1";
540 $crow = sqlQuery($sql, array($code) );
541 if (!empty($crow['long_desc'])) {
542 if ($code_text) $code_text .= '; ';
543 $code_text .= $crow['long_desc'];
548 else if ($code_types[$codetype]['external'] == 5) { // Collect from ICD9 Procedure/Service codeset tables
549 // Ensure the icd9_dx_code sql table exists
550 $check_table = sqlQuery("SHOW TABLES LIKE 'icd9_sg_code'");
551 if ( !(empty($check_table)) ) {
552 if ( !(empty($code)) ) {
553 // Will grab from previous inactive revisions if unable to find in current revision
554 $sql = "SELECT `long_desc` FROM `icd9_sg_code` " .
555 "WHERE `formatted_sg_code` = ? ORDER BY `revision` DESC LIMIT 1";
556 $crow = sqlQuery($sql, array($code) );
557 if (!empty($crow['long_desc'])) {
558 if ($code_text) $code_text .= '; ';
559 $code_text .= $crow['long_desc'];
564 else if ($code_types[$codetype]['external'] == 6) { // Collect from ICD10 PRocedure/Service codeset tables
565 // Ensure the icd10_dx_order_code sql table exists
566 $check_table = sqlQuery("SHOW TABLES LIKE 'icd10_pcs_order_code'");
567 if ( !(empty($check_table)) ) {
568 if ( !(empty($code)) ) {
569 // Will grab from previous inactive revisions if unable to find in current revision
570 $sql = "SELECT `long_desc` FROM `icd10_pcs_order_code` " .
571 "WHERE `pcs_code` = ? ORDER BY `revision` DESC LIMIT 1";
572 $crow = sqlQuery($sql, array($code) );
573 if (!empty($crow['long_desc'])) {
574 if ($code_text) $code_text .= '; ';
575 $code_text .= $crow['long_desc'];
581 else {
582 //using an external code that is not yet supported, so skip.
586 return $code_text;