Integration of external ICD9/10 and SNOMED
[openemr.git] / interface / patient_file / encounter / superbill_custom_full.php
blob7494cc8da950952427c1efd94c5d92d79bb64c3e
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 $fake_register_globals=false;
8 $sanitize_all_escapes=true;
10 require_once("../../globals.php");
11 require_once("../../../custom/code_types.inc.php");
12 require_once("$srcdir/sql.inc");
13 require_once("$srcdir/options.inc.php");
14 require_once("$srcdir/formatting.inc.php");
15 require_once("$srcdir/formdata.inc.php");
17 // Translation for form fields.
18 function ffescape($field) {
19 $field = add_escape_custom($field);
20 return trim($field);
23 // Format dollars for display.
25 function bucks($amount) {
26 if ($amount) {
27 $amount = oeFormatMoney($amount);
28 return $amount;
30 return '';
33 $alertmsg = '';
34 $pagesize = 100;
35 $mode = $_POST['mode'];
36 $code_id = 0;
37 $related_code = '';
38 $active = 1;
39 $reportable = 0;
41 if (isset($mode)) {
42 $code_id = empty($_POST['code_id']) ? '' : $_POST['code_id'] + 0;
43 $code = $_POST['code'];
44 $code_type = $_POST['code_type'];
45 $code_text = $_POST['code_text'];
46 $modifier = $_POST['modifier'];
47 $superbill = $_POST['form_superbill'];
48 $related_code = $_POST['related_code'];
49 $cyp_factor = $_POST['cyp_factor'] + 0;
50 $active = empty($_POST['active']) ? 0 : 1;
51 $reportable = empty($_POST['reportable']) ? 0 : 1;
53 $taxrates = "";
54 if (!empty($_POST['taxrate'])) {
55 foreach ($_POST['taxrate'] as $key => $value) {
56 $taxrates .= "$key:";
60 if ($mode == "delete") {
61 sqlStatement("DELETE FROM codes WHERE id = ?", array($code_id) );
62 $code_id = 0;
64 else if ($mode == "add" || $mode == "modify_complete") { // this covers both adding and modifying
65 $crow = sqlQuery("SELECT COUNT(*) AS count FROM codes WHERE " .
66 "code_type = '" . ffescape($code_type) . "' AND " .
67 "code = '" . ffescape($code) . "' AND " .
68 "modifier = '" . ffescape($modifier) . "' AND " .
69 "id != '" . add_escape_custom($code_id) . "'");
70 if ($crow['count']) {
71 $alertmsg = xl('Cannot add/update this entry because a duplicate already exists!');
73 else {
74 $sql =
75 "code = '" . ffescape($code) . "', " .
76 "code_type = '" . ffescape($code_type) . "', " .
77 "code_text = '" . ffescape($code_text) . "', " .
78 "modifier = '" . ffescape($modifier) . "', " .
79 "superbill = '" . ffescape($superbill) . "', " .
80 "related_code = '" . ffescape($related_code) . "', " .
81 "cyp_factor = '" . ffescape($cyp_factor) . "', " .
82 "taxrates = '" . ffescape($taxrates) . "', " .
83 "active = " . add_escape_custom($active) . ", " .
84 "reportable = " . add_escape_custom($reportable);
85 if ($code_id) {
86 $query = "UPDATE codes SET $sql WHERE id = ?";
87 sqlStatement($query, array($code_id) );
88 sqlStatement("DELETE FROM prices WHERE pr_id = ? AND " .
89 "pr_selector = ''", array($code_id) );
91 else {
92 $code_id = sqlInsert("INSERT INTO codes SET $sql");
94 if (!$alertmsg) {
95 foreach ($_POST['fee'] as $key => $value) {
96 $value = $value + 0;
97 if ($value) {
98 sqlStatement("INSERT INTO prices ( " .
99 "pr_id, pr_selector, pr_level, pr_price ) VALUES ( " .
100 "?, '', ?, ?)", array($code_id,$key,$value) );
103 $code = $code_type = $code_text = $modifier = $superbill = "";
104 $code_id = 0;
105 $related_code = '';
106 $cyp_factor = 0;
107 $taxrates = '';
108 $active = 1;
109 $reportable = 0;
113 else if ($mode == "edit") { // someone clicked [Edit]
114 $sql = "SELECT * FROM codes WHERE id = ?";
115 $results = sqlStatement($sql, array($code_id) );
116 while ($row = sqlFetchArray($results)) {
117 $code = $row['code'];
118 $code_text = $row['code_text'];
119 $code_type = $row['code_type'];
120 $modifier = $row['modifier'];
121 // $units = $row['units'];
122 $superbill = $row['superbill'];
123 $related_code = $row['related_code'];
124 $cyp_factor = $row['cyp_factor'];
125 $taxrates = $row['taxrates'];
126 $active = 0 + $row['active'];
127 $reportable = 0 + $row['reportable'];
130 else if ($mode == "modify") { // someone clicked [Modify]
131 // this is to modify external code types, of which the modifications
132 // are stored in the codes table
133 $code_type_name_external = $_POST['code_type_name_external'];
134 $code_external = $_POST['code_external'];
135 $code_id = $_POST['code_id'];
136 $results = code_set_search($code_type_name_external,$code_external,false,false,true); // only will return one item
137 while ($row = sqlFetchArray($results)) {
138 $code = $row['code'];
139 $code_text = $row['code_text'];
140 $code_type = $code_types[$code_type_name_external]['id'];
141 $modifier = $row['modifier'];
142 // $units = $row['units'];
143 $superbill = $row['superbill'];
144 $related_code = $row['related_code'];
145 $cyp_factor = $row['cyp_factor'];
146 $taxrates = $row['taxrates'];
147 $active = $row['active'];
148 $reportable = $row['reportable'];
153 $related_desc = '';
154 if (!empty($related_code)) {
155 $related_desc = $related_code;
158 $fstart = $_REQUEST['fstart'] + 0;
159 $filter = $_REQUEST['filter'] + 0;
160 if ($filter) {
161 $filter_key = convert_type_id_to_key($filter);
163 $search = $_REQUEST['search'];
165 if ($filter) {
166 $count = code_set_search($filter_key,$search,true,false);
168 else {
169 $count = code_set_search("--ALL--",$search,true,false);
171 if ($fstart >= $count) $fstart -= $pagesize;
172 if ($fstart < 0) $fstart = 0;
173 $fend = $fstart + $pagesize;
174 if ($fend > $count) $fend = $count;
177 <html>
178 <head>
179 <?php html_header_show(); ?>
180 <link rel="stylesheet" href="<?php echo attr($css_header);?>" type="text/css">
181 <script type="text/javascript" src="../../../library/dialog.js"></script>
182 <script type="text/javascript" src="../../../library/textformat.js"></script>
184 <script language="JavaScript">
186 // This is for callback by the find-code popup.
187 // Appends to or erases the current list of related codes.
188 function set_related(codetype, code, selector, codedesc) {
189 var f = document.forms[0];
190 var s = f.related_code.value;
191 if (code) {
192 if (s.length > 0) s += ';';
193 s += codetype + ':' + code;
194 } else {
195 s = '';
197 f.related_code.value = s;
198 f.related_desc.value = s;
201 // This invokes the find-code popup.
202 function sel_related() {
203 var f = document.forms[0];
204 var i = f.code_type.selectedIndex;
205 var codetype = '';
206 if (i >= 0) {
207 var myid = f.code_type.options[i].value;
208 <?php
209 foreach ($code_types as $key => $value) {
210 $codeid = $value['id'];
211 $coderel = $value['rel'];
212 if (!$coderel) continue;
213 echo " if (myid == $codeid) codetype = '$coderel';";
217 if (!codetype) {
218 alert('<?php echo addslashes( xl('This code type does not accept relations.') ); ?>');
219 return;
221 dlgopen('find_code_popup.php', '_blank', 500, 400);
224 // Some validation for saving a new code entry.
225 function validEntry(f) {
226 if (!f.code.value) {
227 alert('<?php echo addslashes( xl('No code was specified!') ); ?>');
228 return false;
230 <?php if ($GLOBALS['ippf_specific']) { ?>
231 if (f.code_type.value == 12 && !f.related_code.value) {
232 alert('<?php echo addslashes( xl('A related IPPF code is required!') ); ?>');
233 return false;
235 <?php } ?>
236 return true;
239 function submitAdd() {
240 var f = document.forms[0];
241 if (!validEntry(f)) return;
242 f.mode.value = 'add';
243 f.code_id.value = '';
244 f.submit();
247 function submitUpdate() {
248 var f = document.forms[0];
249 if (! parseInt(f.code_id.value)) {
250 alert('<?php echo addslashes( xl('Cannot update because you are not editing an existing entry!') ); ?>');
251 return;
253 if (!validEntry(f)) return;
254 f.mode.value = 'add';
255 f.submit();
258 function submitModifyComplete() {
259 var f = document.forms[0];
260 f.mode.value = 'modify_complete';
261 f.submit();
264 function submitList(offset) {
265 var f = document.forms[0];
266 var i = parseInt(f.fstart.value) + offset;
267 if (i < 0) i = 0;
268 f.fstart.value = i;
269 f.submit();
272 function submitEdit(id) {
273 var f = document.forms[0];
274 f.mode.value = 'edit';
275 f.code_id.value = id;
276 f.submit();
279 function submitModify(code_type_name,code,id) {
280 var f = document.forms[0];
281 f.mode.value = 'modify';
282 f.code_external.value = code;
283 f.code_id.value = id;
284 f.code_type_name_external.value = code_type_name;
285 f.submit();
290 function submitDelete(id) {
291 var f = document.forms[0];
292 f.mode.value = 'delete';
293 f.code_id.value = id;
294 f.submit();
297 function getCTMask() {
298 var ctid = document.forms[0].code_type.value;
299 <?php
300 foreach ($code_types as $key => $value) {
301 $ctid = attr($value['id']);
302 $ctmask = attr($value['mask']);
303 echo " if (ctid == '$ctid') return '$ctmask';\n";
306 return '';
309 </script>
311 </head>
312 <body class="body_top" >
314 <?php if ($GLOBALS['concurrent_layout']) {
315 } else { ?>
316 <a href='patient_encounter.php?codefrom=superbill' target='Main'>
317 <span class='title'><?php echo xlt('Superbill Codes'); ?></span>
318 <font class='more'><?php echo text($tback);?></font></a>
319 <?php } ?>
321 <form method='post' action='superbill_custom_full.php' name='theform'>
323 <input type='hidden' name='mode' value=''>
325 <br>
327 <center>
328 <table border='0' cellpadding='0' cellspacing='0'>
330 <tr>
331 <td colspan="3"> <?php echo xlt('Not all fields are required for all codes or code types.'); ?><br><br></td>
332 </tr>
334 <tr>
335 <td><?php echo xlt('Type'); ?>:</td>
336 <td width="5">
337 </td>
338 <td>
340 <?php if ($mode != "modify") { ?>
341 <select name="code_type">
342 <?php } ?>
344 <?php $external_sets = array(); ?>
345 <?php foreach ($code_types as $key => $value) { ?>
346 <?php if ( !($value['external']) ) { ?>
347 <?php if ($mode != "modify") { ?>
348 <option value="<?php echo attr($value['id']) ?>"<?php if ($code_type == $value['id']) echo " selected" ?>><?php echo xlt($value['label']) ?></option>
349 <?php } ?>
350 <?php } ?>
351 <?php if ($value['external']) {
352 array_push($external_sets,$key);
353 } ?>
354 <?php } // end foreach ?>
356 <?php if ($mode != "modify") { ?>
357 </select>
358 <?php } ?>
360 <?php if ($mode == "modify") { ?>
361 <input type='text' size='4' name='code_type' readonly='readonly' style='display:none' value='<?php echo attr($code_type) ?>' />
362 <?php echo attr($code_type_name_external) ?>
363 <?php } ?>
365 &nbsp;&nbsp;
366 <?php echo xlt('Code'); ?>:
368 <?php if ($mode == "modify") { ?>
369 <input type='text' size='6' name='code' readonly='readonly' value='<?php echo attr($code) ?>' />
370 <?php } else { ?>
371 <input type='text' size='6' name='code' value='<?php echo attr($code) ?>'
372 onkeyup='maskkeyup(this,getCTMask())'
373 onblur='maskblur(this,getCTMask())'
375 <?php } ?>
377 <?php if (modifiers_are_used()) { ?>
378 &nbsp;&nbsp;<?php echo xlt('Modifier'); ?>:
379 <?php if ($mode == "modify") { ?>
380 <input type='text' size='3' name='modifier' readonly='readonly' value='<?php echo attr($modifier) ?>'>
381 <?php } else { ?>
382 <input type='text' size='3' name='modifier' value='<?php echo attr($modifier) ?>'>
383 <?php } ?>
384 <?php } else { ?>
385 <input type='hidden' name='modifier' value=''>
386 <?php } ?>
388 &nbsp;&nbsp;
389 <input type='checkbox' name='active' value='1'<?php if (!empty($active) || ($mode == 'modify' && $active == NULL) ) echo ' checked'; ?> />
390 <?php echo xlt('Active'); ?>
391 </td>
392 </tr>
394 <tr>
395 <td><?php echo xlt('Description'); ?>:</td>
396 <td></td>
397 <td>
399 <?php if ($mode == "modify") { ?>
400 <input type='text' size='50' name="code_text" readonly="readonly" value='<?php echo attr($code_text) ?>'>
401 <?php } else { ?>
402 <input type='text' size='50' name="code_text" value='<?php echo attr($code_text) ?>'>
403 <?php } ?>
405 </td>
406 </tr>
408 <tr>
409 <td><?php echo xlt('Category'); ?>:</td>
410 <td></td>
411 <td>
412 <?php
413 generate_form_field(array('data_type'=>1,'field_id'=>'superbill','list_id'=>'superbill'), $superbill);
415 &nbsp;&nbsp;
416 <input type='checkbox' name='reportable' value='1'<?php if (!empty($reportable)) echo ' checked'; ?> />
417 <?php echo xlt('Reportable'); ?>
418 </td>
419 </tr>
421 <tr<?php if (empty($GLOBALS['ippf_specific'])) echo " style='display:none'"; ?>>
422 <td><?php echo xlt('CYP Factor'); ?>:</td>
423 <td></td>
424 <td>
425 <input type='text' size='10' maxlength='20' name="cyp_factor" value='<?php echo attr($cyp_factor) ?>'>
426 </td>
427 </tr>
429 <tr<?php if (!related_codes_are_used()) echo " style='display:none'"; ?>>
430 <td><?php echo xlt('Relate To'); ?>:</td>
431 <td></td>
432 <td>
433 <input type='text' size='50' name='related_desc'
434 value='<?php echo attr($related_desc) ?>' onclick="sel_related()"
435 title='<?php echo xla('Click to select related code'); ?>' readonly />
436 <input type='hidden' name='related_code' value='<?php echo attr($related_code) ?>' />
437 </td>
438 </tr>
440 <tr>
441 <td><?php echo xlt('Fees'); ?>:</td>
442 <td></td>
443 <td>
444 <?php
445 $pres = sqlStatement("SELECT lo.option_id, lo.title, p.pr_price " .
446 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
447 "p.pr_id = ? AND p.pr_selector = '' AND p.pr_level = lo.option_id " .
448 "WHERE list_id = 'pricelevel' ORDER BY lo.seq", array($code_id) );
449 for ($i = 0; $prow = sqlFetchArray($pres); ++$i) {
450 if ($i) echo "&nbsp;&nbsp;";
451 echo text(xl_list_label($prow['title'])) . " ";
452 echo "<input type='text' size='6' name='fee[" . attr($prow['option_id']) . "]' " .
453 "value='" . attr($prow['pr_price']) . "' >\n";
456 </td>
457 </tr>
459 <?php
460 $taxline = '';
461 $pres = sqlStatement("SELECT option_id, title FROM list_options " .
462 "WHERE list_id = 'taxrate' ORDER BY seq");
463 while ($prow = sqlFetchArray($pres)) {
464 if ($taxline) $taxline .= "&nbsp;&nbsp;";
465 $taxline .= "<input type='checkbox' name='taxrate[" . attr($prow['option_id']) . "]' value='1'";
466 if (strpos(":$taxrates", $prow['option_id']) !== false) $taxline .= " checked";
467 $taxline .= " />\n";
468 $taxline .= text(xl_list_label($prow['title'])) . "\n";
470 if ($taxline) {
472 <tr>
473 <td><?php echo xlt('Taxes'); ?>:</td>
474 <td></td>
475 <td>
476 <?php echo $taxline ?>
477 </td>
478 </tr>
479 <?php } ?>
481 <tr>
482 <td colspan="3" align="center">
483 <input type="hidden" name="code_id" value="<?php echo attr($code_id) ?>"><br>
484 <input type="hidden" name="code_type_name_external" value="<?php echo attr($code_type_name_external) ?>">
485 <input type="hidden" name="code_external" value="<?php echo attr($code_external) ?>">
486 <?php if ($mode == "modify") { ?>
487 <a href='javascript:submitModifyComplete();' class='link'>[<?php echo xlt('Update'); ?>]</a>
488 <?php } else { ?>
489 <a href='javascript:submitUpdate();' class='link'>[<?php echo xlt('Update'); ?>]</a>
490 &nbsp;&nbsp;
491 <a href='javascript:submitAdd();' class='link'>[<?php echo xlt('Add as New'); ?>]</a>
492 <?php } ?>
493 </td>
494 </tr>
496 </table>
498 <table border='0' cellpadding='5' cellspacing='0' width='96%'>
499 <tr>
501 <td class='text'>
502 <select name='filter' onchange='submitList(0)'>
503 <option value='0'>
504 <?php $all_string = xlt("All");
505 if ( !(empty($external_sets) )) {
506 // Show the external code sets that will not work with All selection
507 $all_string .= " (" . xlt("Except") . " ";
508 $first_flag = true;
509 foreach ($external_sets as $set) {
510 if ($first_flag) { //deal with the comma
511 $first_flag = false;
513 else {
514 $all_string .= ",";
516 $all_string .= $code_types[$set]['label'];
518 $all_string .= ")";
520 echo $all_string;
522 </option>
523 <?php
524 foreach ($code_types as $key => $value) {
525 echo "<option value='" . attr($value['id']) . "'";
526 if ($value['id'] == $filter) echo " selected";
527 echo ">" . xlt($value['label']) . "</option>\n";
530 </select>
531 &nbsp;&nbsp;&nbsp;&nbsp;
533 <input type="text" name="search" size="5" value="<?php echo attr($search) ?>">&nbsp;
534 <input type="submit" name="go" value='<?php echo xla('Search'); ?>'>
535 <input type='hidden' name='fstart' value='<?php echo attr($fstart) ?>'>
536 </td>
538 <td class='text' align='right'>
539 <?php if ($fstart) { ?>
540 <a href="javascript:submitList(-<?php echo attr($pagesize) ?>)">
541 &lt;&lt;
542 </a>
543 &nbsp;&nbsp;
544 <?php } ?>
545 <?php echo ($fstart + 1) . " - $fend of $count" ?>
546 &nbsp;&nbsp;
547 <a href="javascript:submitList(<?php echo attr($pagesize) ?>)">
548 &gt;&gt;
549 </a>
550 </td>
552 </tr>
553 </table>
555 </form>
557 <table border='0' cellpadding='5' cellspacing='0' width='96%'>
558 <tr>
559 <td><span class='bold'><?php echo xlt('Code'); ?></span></td>
560 <td><span class='bold'><?php echo xlt('Mod'); ?></span></td>
561 <td><span class='bold'><?php echo xlt('Act'); ?></span></td>
562 <td><span class='bold'><?php echo xlt('Rep'); ?></span></td>
563 <td><span class='bold'><?php echo xlt('Type'); ?></span></td>
564 <td><span class='bold'><?php echo xlt('Description'); ?></span></td>
565 <?php if (related_codes_are_used()) { ?>
566 <td><span class='bold'><?php echo xlt('Related'); ?></span></td>
567 <?php } ?>
568 <?php
569 $pres = sqlStatement("SELECT title FROM list_options " .
570 "WHERE list_id = 'pricelevel' ORDER BY seq");
571 while ($prow = sqlFetchArray($pres)) {
572 echo " <td class='bold' align='right' nowrap>" . text(xl_list_label($prow['title'])) . "</td>\n";
575 <td></td>
576 <td></td>
577 </tr>
578 <?php
579 // Flag is this is from an external set
580 $is_external_set=false;
581 if (in_array($filter_key,$external_sets)) {
582 $is_external_set=true;
585 if ($filter) {
586 $res = code_set_search($filter_key,$search,false,false,false,$fstart,($fend - $fstart));
588 else {
589 $res = code_set_search("--ALL--",$search,false,false,false,$fstart,($fend - $fstart));
592 for ($i = 0; $row = sqlFetchArray($res); $i++) $all[$i] = $row;
594 if (!empty($all)) {
595 $count = 0;
596 foreach($all as $iter) {
597 $count++;
599 $has_fees = false;
600 foreach ($code_types as $key => $value) {
601 if ($value['id'] == $iter['code_type']) {
602 $has_fees = $value['fee'];
603 break;
607 echo " <tr>\n";
608 echo " <td class='text'>" . text($iter["code"]) . "</td>\n";
609 echo " <td class='text'>" . text($iter["modifier"]) . "</td>\n";
610 if ($is_external_set) {
611 echo " <td class='text'>" . ( ($iter["active"] || $iter["active"]==NULL) ? xlt('Yes') : xlt('No')) . "</td>\n";
613 else {
614 echo " <td class='text'>" . ( ($iter["active"]) ? xlt('Yes') : xlt('No')) . "</td>\n";
616 echo " <td class='text'>" . ($iter["reportable"] ? xlt('Yes') : xlt('No')) . "</td>\n";
617 echo " <td class='text'>" . text($iter['code_type_name']) . "</td>\n";
618 echo " <td class='text'>" . text($iter['code_text']) . "</td>\n";
620 if (related_codes_are_used()) {
621 // Show related codes.
622 echo " <td class='text'>";
623 $arel = explode(';', $iter['related_code']);
624 foreach ($arel as $tmp) {
625 list($reltype, $relcode) = explode(':', $tmp);
626 $code_description = lookup_code_descriptions($reltype.":".$relcode);
627 echo text($relcode) . ' ' . text(trim($code_description)) . '<br />';
629 echo "</td>\n";
632 $pres = sqlStatement("SELECT p.pr_price " .
633 "FROM list_options AS lo LEFT OUTER JOIN prices AS p ON " .
634 "p.pr_id = ? AND p.pr_selector = '' AND p.pr_level = lo.option_id " .
635 "WHERE list_id = 'pricelevel' ORDER BY lo.seq", array($iter['id']) );
636 while ($prow = sqlFetchArray($pres)) {
637 echo "<td class='text' align='right'>" . text(bucks($prow['pr_price'])) . "</td>\n";
640 if ($is_external_set) {
641 echo " <td align='right'><a class='link' href='javascript:submitModify(\"" . attr($iter['code_type_name']) . "\",\"" . attr($iter['code']) . "\",\"" . attr($iter['id']) . "\")'>[" . xlt('Modify') . "]</a></td>\n";
643 else {
644 echo " <td align='right'><a class='link' href='javascript:submitDelete(" . attr($iter['id']) . ")'>[" . xlt('Delete') . "]</a></td>\n";
645 echo " <td align='right'><a class='link' href='javascript:submitEdit(" . attr($iter['id']) . ")'>[" . xlt('Edit') . "]</a></td>\n";
648 echo " </tr>\n";
655 </table>
657 </center>
659 <script language="JavaScript">
660 <?php
661 if ($alertmsg) {
662 echo "alert('" . addslashes($alertmsg) . "');\n";
665 </script>
667 </body>
668 </html>