2 //////////////////////////////////////////////////////////////////////
3 // ------------------ DO NOT MODIFY VIEW.PHP !!! ---------------------
4 // View.php is an exact duplicate of new.php. If you wish to make
5 // any changes, then change new.php and either (recommended) make
6 // view.php a symbolic link to new.php, or copy new.php to view.php.
8 // And if you check in a change to either module, be sure to check
9 // in the other (identical) module also.
11 // This nonsense will go away if we ever move to subversion.
12 //////////////////////////////////////////////////////////////////////
14 // Copyright (C) 2005-2007 Rod Roark <rod@sunsetsystems.com>
16 // This program is free software; you can redistribute it and/or
17 // modify it under the terms of the GNU General Public License
18 // as published by the Free Software Foundation; either version 2
19 // of the License, or (at your option) any later version.
21 include_once("../../globals.php");
22 include_once("$srcdir/api.inc");
23 include_once("codes.php");
24 include_once("../../../custom/code_types.inc.php");
26 // Possible units of measure for NDC drug quantities.
28 $ndc_uom_choices = array(
35 // $FEE_SHEET_COLUMNS should be defined in codes.php.
36 if (empty($FEE_SHEET_COLUMNS)) $FEE_SHEET_COLUMNS = 2;
38 $returnurl = $GLOBALS['concurrent_layout'] ?
'encounter_top.php' : 'patient_encounter.php';
40 // If Save was clicked, save the new and modified billing lines;
41 // then if no error, redirect to $returnurl.
43 if ($_POST['bn_save']) {
44 $provid = $_POST['ProviderID'];
45 if (! $provid) $provid = $_SESSION["authUserID"];
46 $bill = $_POST['bill'];
47 for ($lino = 1; $bill["$lino"]['code_type']; ++
$lino) {
48 $iter = $bill["$lino"];
50 // Skip disabled (billed) line items.
51 if ($iter['billed']) continue;
54 $code_type = $iter['code_type'];
55 $code = $iter['code'];
56 $modifier = trim($iter['mod']);
57 $units = max(1, intval(trim($iter['units'])));
58 $fee = 0 +
trim($iter['fee']);
59 if ($code_type == 'COPAY') {
60 if ($fee > 0) $fee = 0 - $fee;
61 $code = sprintf('%01.2f', 0 - $fee);
63 $justify = trim($iter['justify']);
64 if ($justify) $justify = str_replace(',', ':', $justify) . ':';
65 // $auth = $iter['auth'] ? "1" : "0";
70 if ($iter['ndcnum']) {
71 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
72 trim($iter['ndcqty']);
75 // If the item is already in the database...
81 // authorizeBilling($id, $auth);
82 sqlQuery("UPDATE billing SET code = '$code', " .
83 "units = '$units', fee = '$fee', modifier = '$modifier', " .
84 "authorized = $auth, provider_id = '$provid', " .
85 "ndc_info = '$ndc_info', justify = '$justify' WHERE " .
86 "id = '$id' AND billed = 0 AND activity = 1");
90 // Otherwise it's a new item...
92 $query = "select code_text from codes where code_type = '" .
93 $code_types[$code_type]['id'] . "' and " .
94 "code = '$code' and ";
96 $query .= "modifier = '$modifier'";
98 $query .= "(modifier is null or modifier = '')";
100 $result = sqlQuery($query);
101 $code_text = addslashes($result['code_text']);
102 addBilling($encounter, $code_type, $code, $code_text, $pid, $auth,
103 $provid, $modifier, $units, $fee, $ndc_info, $justify);
107 formHeader("Redirecting....");
113 $billresult = getBillingByEncounter($pid, $encounter, "*");
117 <link rel
=stylesheet href
="<?echo $css_header;?>" type
="text/css">
119 .billcell
{ font
-family
: sans
-serif
; font
-size
: 10pt
}
121 <script language
="JavaScript">
123 var diags
= new Array();
126 // Generate JavaScript to build the array of diagnoses.
127 function genDiagJS($code_type, $code) {
128 if ($code_type == 'ICD9') {
129 echo "diags.push('$code');\n";
133 foreach ($billresult as $iter) {
134 genDiagJS($iter["code_type"], trim($iter["code"]));
137 if ($_POST['bill']) {
138 foreach ($_POST['bill'] as $iter) {
139 if ($iter["del"]) continue; // skip if Delete was checked
140 if ($iter["id"]) continue; // skip if it came from the database
141 genDiagJS($iter["code_type"], $iter["code"]);
144 if ($_POST['newtype']) {
145 list($code, $modifier) = explode("-", $_POST['newcode']);
146 genDiagJS($_POST['newtype'], $code);
150 function codeselect(selobj
, newtype
) {
151 var i
= selobj
.selectedIndex
;
153 top
.restoreSession();
154 var f
= document
.forms
[0];
155 f
.newcode
.value
= selobj
.options
[i
].value
;
156 f
.newtype
.value
= newtype
;
161 function copayselect() {
162 top
.restoreSession();
163 var f
= document
.forms
[0];
164 f
.newtype
.value
= 'COPAY';
168 function validate(f
) {
169 for (var lino
= 1; f
['bill['+lino+
'][code_type]']; ++lino
) {
170 var pfx
= 'bill['+lino+
']';
171 if (f
[pfx+
'[ndcnum]'] && f
[pfx+
'[ndcnum]'].value
) {
172 // Check NDC number format.
174 var ndc
= f
[pfx+
'[ndcnum]'].value
;
175 var a
= ndc
.split('-');
179 else if (a
[0].length
< 1 || a
[1].length
< 1 || a
[2].length
< 1 ||
180 a
[0].length
> 5 || a
[1].length
> 4 || a
[2].length
> 2) {
184 for (var i
= 0; i
< 3; ++i
) {
185 for (var j
= 0; j
< a
[i
].length
; ++j
) {
186 var c
= a
[i
].charAt(j
);
187 if (c
< '0' || c
> '9') ndcok
= false;
192 alert('<?php xl('Format incorrect
for NDC
','e
') ?> "' + ndc +
193 '", <?php xl('should be like nnnnn
-nnnn
-nn
','e
') ?>');
194 if (f
[pfx+
'[ndcnum]'].focus
) f
[pfx+
'[ndcnum]'].focus();
197 // Check for valid quantity.
198 var qty
= f
[pfx+
'[ndcqty]'].value
- 0;
199 if (isNaN(qty
) || qty
<= 0) {
200 alert('<?php xl('Quantity
for NDC
','e
') ?> "' + ndc +
201 '" <?php xl('is not
valid (decimal fractions are OK
).','e
') ?>');
202 if (f
[pfx+
'[ndcqty]'].focus
) f
[pfx+
'[ndcqty]'].focus();
207 top
.restoreSession();
211 // When a justify selection is made, apply it to the current list for
212 // this procedure and then rebuild its selection list.
214 function setJustify(seljust
) {
215 var theopts
= seljust
.options
;
216 var jdisplay
= theopts
[0].text
;
218 // Compute revised justification string. Note this does nothing if
219 // the first entry is still selected, which is handy at startup.
220 if (seljust
.selectedIndex
> 0) {
221 var newdiag
= seljust
.value
;
222 if (newdiag
.length
== 0) {
226 if (jdisplay
.length
) jdisplay +
= ',';
231 // Rebuild selection list.
232 var jhaystack
= ',' + jdisplay +
',';
235 theopts
[j++
] = new Option(jdisplay
,jdisplay
,true,true);
236 for (var i
= 0; i
< diags
.length
; ++i
) {
237 if (jhaystack
.indexOf(',' + diags
[i
] +
',') < 0) {
238 theopts
[j++
] = new Option(diags
[i
],diags
[i
],false,false);
241 theopts
[j++
] = new Option('Clear','',false,false);
247 <body
<?
echo $top_bg_line;?
> topmargin
="0" rightmargin
="0" leftmargin
="2"
248 bottommargin
="0" marginwidth
="2" marginheight
="0">
249 <form method
="post" action
="<?php echo $rootdir; ?>/forms/fee_sheet/new.php"
250 onsubmit
="return validate(this)">
251 <span
class="title"><?
echo ($GLOBALS['phone_country_code'] == '1') ?
'Fee' : 'Coding' ?
> <?php
xl('Sheet','e');?
></span
><br
>
252 <input type
='hidden' name
='newtype' value
=''>
253 <input type
='hidden' name
='newcode' value
=''>
260 // Create all the drop-lists of preselected codes.
262 foreach ($bcodes as $key0 => $value0) {
263 foreach ($value0 as $key1 => $value1) {
265 echo ($i <= 1) ?
" <tr>\n" : "";
266 echo " <td width='50%' align='center' nowrap>\n";
267 echo " <select name='$key1' style='width:96%' onchange='codeselect(this, \"$key0\")'>\n";
268 echo " <option value=''> $key1\n";
269 foreach ($value0[$key1] as $key2 => $value2) {
270 echo " <option value='$key2'>$key2 $value2\n";
274 if ($i >= $FEE_SHEET_COLUMNS) {
281 $search_type = $default_search_type;
282 if ($_POST['search_type']) $search_type = $_POST['search_type'];
285 // Find out if the patient has any insurance that requires NDC numbers.
286 // Currently this is just Medicaid, type 3.
288 $tmp = sqlQuery("SELECT c.id FROM " .
289 "insurance_data AS i, insurance_companies AS c WHERE " .
290 "i.pid = '$pid' AND i.provider != '' AND " .
291 "c.id = i.provider AND c.freeb_type = 3 " .
293 $ndc_applies = !empty($tmp);
295 $ndc_applies = true; // Looks like more payers are starting to require this.
297 echo $i ?
" <td></td>\n </tr>\n" : "";
299 echo " <td colspan='$FEE_SHEET_COLUMNS' align='center' nowrap>\n";
301 // If Search was clicked, do it and write the list of results here.
302 // There's no limit on the number of results!
305 if ($_POST['bn_search'] && $_POST['search_term']) {
306 $query = "select code, modifier, code_text from codes where " .
307 "(code_text like '%" . $_POST['search_term'] . "%' or " .
308 "code like '%" . $_POST['search_term'] . "%') and " .
309 "code_type = '" . $code_types[$search_type]['id'] . "' " .
311 $res = sqlStatement($query);
312 $numrows = mysql_num_rows($res); // FIXME - not portable!
315 echo " <select name='Search Results' style='width:98%' " .
316 "onchange='codeselect(this, \"$search_type\")'";
317 if (! $numrows) echo ' disabled';
319 echo " <option value=''> Search Results ($numrows items)\n";
322 while ($row = sqlFetchArray($res)) {
323 $code = $row['code'];
324 if ($row['modifier']) $code .= "-" . $row['modifier'];
325 echo " <option value='$code'>$code " . ucfirst(strtolower($row['code_text'])) . "\n";
336 <p style
='margin-top:8px;margin-bottom:8px'>
340 <input type
='button' value
='<?php xl('Add Copay
','e
');?>'
341 onclick
="copayselect()" /> 
; 
; 
; 
; 
;
344 <?php
xl('Search','e'); ?
> 
;
346 foreach ($code_types as $key => $value) {
347 echo " <input type='radio' name='search_type' value='$key'";
348 if ($key == $default_search_type) echo " checked";
349 echo " />$key \n";
352 <?php
xl('for','e'); ?
> 
;
355 <input type
='text' name
='search_term' value
=''>  
;
358 <input type
='submit' name
='bn_search' value
='<?php xl('Search
','e
');?>'>
364 <p style
='margin-top:16px;margin-bottom:8px'>
365 <table cellspacing
='5'>
367 <td
class='billcell'><b
><?php
xl('Type','e');?
></b
></td
>
368 <td
class='billcell'><b
><?php
xl('Code','e');?
></b
></td
>
369 <?
if (modifiers_are_used()) { ?
>
370 <td
class='billcell'><b
><?php
xl('Mod','e');?
></b
></td
>
372 <?
if (fees_are_used()) { ?
>
373 <td
class='billcell' align
='center'><b
><?php
xl('Units','e');?
></b
></td
>
374 <td
class='billcell' align
='right'><b
><?php
xl('Fee','e');?
></b
> 
;</td
>
375 <td
class='billcell' align
='center'><b
><?php
xl('Justify','e');?
></b
></td
>
377 <td
class='billcell' align
='center'><b
><?php
xl('Auth','e');?
></b
></td
>
378 <td
class='billcell' align
='center'><b
><?php
xl('Delete','e');?
></b
></td
>
379 <td
class='billcell'><b
><?php
xl('Description','e');?
></b
></td
>
383 $justinit = "var f = document.forms[0];\n";
385 // This writes a billing line item to the output page.
387 function echoLine($lino, $codetype, $code, $modifier, $ndc_info='',
388 $auth = TRUE, $del = FALSE, $units = NULL, $fee = NULL, $id = NULL,
389 $billed = FALSE, $code_text = NULL, $justify = NULL)
391 global $code_types, $ndc_applies, $ndc_uom_choices, $justinit;
393 if ($codetype == 'COPAY') {
394 if (!$code_text) $code_text = 'Cash';
395 if ($fee > 0) $fee = 0 - $fee;
398 $query = "select units, fee, code_text from codes where code_type = '" .
399 $code_types[$codetype]['id'] . "' and " .
400 "code = '$code' and ";
402 $query .= "modifier = '$modifier'";
404 $query .= "(modifier is null or modifier = '')";
406 $result = sqlQuery($query);
407 $code_text = $result['code_text'];
408 if (empty($units)) $units = max(1, intval($result['units']));
409 if (!isset($fee)) $fee = $result['fee'];
411 $fee = sprintf('%01.2f', $fee);
412 $strike1 = ($id && $del) ?
"<strike>" : "";
413 $strike2 = ($id && $del) ?
"</strike>" : "";
415 echo " <td class='billcell'>$strike1$codetype$strike2";
417 echo "<input type='hidden' name='bill[$lino][id]' value='$id'>";
419 echo "<input type='hidden' name='bill[$lino][code_type]' value='$codetype'>";
420 echo "<input type='hidden' name='bill[$lino][code]' value='$code'>";
421 echo "<input type='hidden' name='bill[$lino][billed]' value='$billed'>";
423 if ($codetype != 'COPAY') {
424 echo " <td class='billcell'>$strike1$code$strike2</td>\n";
426 echo " <td class='billcell'> </td>\n";
429 if (modifiers_are_used()) {
430 echo " <td class='billcell'>$strike1$modifier$strike2" .
431 "<input type='hidden' name='bill[$lino][mod]' value='$modifier'></td>\n";
433 if (fees_are_used()) {
434 if ($codetype != 'COPAY') {
435 echo " <td class='billcell' align='center'>$units</td>\n";
437 echo " <td class='billcell'> </td>\n";
439 echo " <td class='billcell' align='right'>$fee</td>\n";
440 echo " <td class='billcell' align='center'>$justify</td>\n";
442 echo " <td class='billcell' align='center'><input type='checkbox'" .
443 ($auth ?
" checked" : "") . " disabled /></td>\n";
444 echo " <td class='billcell' align='center'><input type='checkbox'" .
445 " disabled /></td>\n";
447 if (modifiers_are_used()) {
448 if ($codetype != 'COPAY' && ($code_types[$codetype]['mod'] ||
$modifier)) {
449 echo " <td class='billcell'><input type='text' name='bill[$lino][mod]' " .
450 "value='$modifier' size='" . $code_types[$codetype]['mod'] . "'></td>\n";
452 echo " <td class='billcell'> </td>\n";
455 if (fees_are_used()) {
456 if ($codetype == 'COPAY' ||
$code_types[$codetype]['fee'] ||
$fee != 0) {
457 echo " <td class='billcell' align='center'>";
458 if ($codetype != 'COPAY') {
459 echo "<input type='text' name='bill[$lino][units]' " .
460 "value='$units' size='2' style='text-align:right'>";
462 echo "<input type='hidden' name='bill[$lino][units]' value='$units'>";
465 echo " <td class='billcell' align='right'>" .
466 "<input type='text' name='bill[$lino][fee]' " .
467 "value='$fee' size='6' style='text-align:right'></td>\n";
468 if ($code_types[$codetype]['just'] ||
$justify) {
469 echo " <td class='billcell' align='center'>";
470 echo "<select name='bill[$lino][justify]' onchange='setJustify(this)'>";
471 echo "<option value='$justify'>$justify</option></select>";
473 $justinit .= "setJustify(f['bill[$lino][justify]']);\n";
475 echo " <td class='billcell'> </td>\n";
478 echo " <td class='billcell'> </td>\n";
479 echo " <td class='billcell'> </td>\n";
480 echo " <td class='billcell'> </td>\n";
483 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][auth]' " .
484 "value='1'" . ($auth ?
" checked" : "") . " /></td>\n";
485 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][del]' " .
486 "value='1'" . ($del ?
" checked" : "") . " /></td>\n";
489 echo " <td class='billcell'>$strike1" . ucfirst(strtolower($code_text)) . "$strike2</td>\n";
492 // If NDC info exists or may be required, add a line for it.
493 if ($codetype == 'HCPCS' && $ndc_applies && !$billed) {
494 $ndcnum = ''; $ndcuom = ''; $ndcqty = '';
495 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $ndc_info, $tmp)) {
496 $ndcnum = $tmp[1]; $ndcuom = $tmp[2]; $ndcqty = $tmp[3];
499 echo " <td class='billcell' colspan='2'> </td>\n";
500 echo " <td class='billcell' colspan='6'> NDC: ";
501 echo "<input type='text' name='bill[$lino][ndcnum]' value='$ndcnum' " .
502 "size='11' style='background-color:transparent'>";
503 echo " Qty: ";
504 echo "<input type='text' name='bill[$lino][ndcqty]' value='$ndcqty' " .
505 "size='3' style='background-color:transparent;text-align:right'>";
507 echo "<select name='bill[$lino][ndcuom]' style='background-color:transparent'>";
508 foreach ($ndc_uom_choices as $key => $value) {
509 echo "<option value='$key'";
510 if ($key == $ndcuom) echo " selected";
511 echo ">$value</option>";
517 else if ($ndc_info) {
519 echo " <td class='billcell' colspan='2'> </td>\n";
520 echo " <td class='billcell' colspan='6'> NDC Data: $ndc_info</td>\n";
525 // Try setting the default provider to that of the new encounter form.
527 $encounter_provid = -1;
528 $tmp = sqlQuery("SELECT users.id FROM forms, users WHERE " .
529 "forms.pid = '$pid' AND forms.encounter = '$encounter' AND " .
530 "forms.formdir='newpatient' AND users.username = forms.user AND " .
531 "users.authorized = 1");
532 if ($tmp['id']) $encounter_provid = $tmp['id'];
534 // Generate lines for items already in the database.
538 foreach ($billresult as $iter) {
540 $bline = $_POST['bill']["$lino"];
541 $del = $bline['del']; // preserve Delete if checked
543 $modifier = trim($iter["modifier"]);
544 $units = $iter["units"];
546 $authorized = $iter["authorized"];
547 $ndc_info = $iter["ndc_info"];
548 $justify = trim($iter['justify']);
549 if ($justify) $justify = substr(str_replace(':', ',', $justify), 0, strlen($justify) - 1);
551 // Also preserve other items from the form, if present.
552 if ($bline['id'] && !$iter["billed"]) {
553 $modifier = trim($bline['mod']);
554 $units = trim($bline['units']);
555 $fee = trim($bline['fee']);
556 $authorized = $bline['auth'];
558 if ($bline['ndcnum']) {
559 $ndc_info = 'N4' . trim($bline['ndcnum']) . ' ' . $bline['ndcuom'] .
560 trim($bline['ndcqty']);
562 $justify = $bline['justify'];
565 // list($code, $modifier) = explode("-", $iter["code"]);
566 echoLine($lino, $iter["code_type"], trim($iter["code"]),
567 $modifier, $ndc_info, $authorized,
568 $del, $units, $fee, $iter["id"], $iter["billed"],
569 $iter["code_text"], $justify);
570 // If no default provider yet then try this one.
571 if ($encounter_provid < 0 && ! $del) $encounter_provid = $iter["provider_id"];
575 // If still no default provider then make it the logged-in user.
577 if ($encounter_provid < 0) $encounter_provid = $_SESSION["authUserID"];
579 // Echo new billing items from this form here, but omit any line
580 // whose Delete checkbox is checked.
582 if ($_POST['bill']) {
583 foreach ($_POST['bill'] as $key => $iter) {
584 if ($iter["id"]) continue; // skip if it came from the database
585 if ($iter["del"]) continue; // skip if Delete was checked
587 if ($iter['ndcnum']) {
588 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
589 trim($iter['ndcqty']);
591 $fee = 0 +
trim($iter['fee']);
592 if ($iter['code_type'] == 'COPAY' && $fee > 0) $fee = 0 - $fee;
593 echoLine(++
$lino, $iter["code_type"], $iter["code"], trim($iter["mod"]),
594 $ndc_info, $iter["auth"], $iter["del"], trim($iter["units"]),
595 $fee, NULL, FALSE, NULL, $iter["justify"]);
599 // If a new billing code was <select>ed, add its line here. As a special
600 // case allow HCPCS codes to be included in the CPT drop-lists, and
601 // CPT4 codes included in OPCS drop-lists.
603 if ($_POST['newtype']) {
604 $newtype = $_POST['newtype'];
605 if ($newtype == 'COPAY') {
606 $tmp = sqlQuery("SELECT copay FROM insurance_data WHERE pid = '$pid' " .
607 "AND type = 'primary' ORDER BY date DESC LIMIT 1");
608 $code = sprintf('%01.2f', 0 +
$tmp['copay']);
609 echoLine(++
$lino, $newtype, $code, '', '', '1', '0', '1',
610 sprintf('%01.2f', 0 - $code));
613 list($code, $modifier) = explode("-", $_POST['newcode']);
614 if ($newtype == "CPT4" && preg_match("/^[A-Z]/", $code))
616 else if ($newtype == "OPCS" && preg_match("/^[0-9]/", $code))
619 // If HCPCS, find last NDC string used for this code.
620 if ($newtype == 'HCPCS' && $ndc_applies) {
621 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
622 "code_type = '$newtype' AND code = '$code' AND ndc_info LIKE 'N4%' " .
623 "ORDER BY date DESC LIMIT 1");
624 if (!empty($tmp)) $ndc_info = $tmp['ndc_info'];
626 echoLine(++
$lino, $newtype, $code, trim($modifier), $ndc_info);
638 <span
class="billcell"><?php
xl('PROVIDER:','e');?
></span
>
641 // Build a drop-down list of providers. This includes users who
642 // have the word "provider" anywhere in their "additional info"
643 // field, so that we can define providers (for billing purposes)
644 // who do not appear in the calendar.
646 $query = "SELECT id, lname, fname FROM users WHERE " .
647 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' " .
648 "ORDER BY lname, fname";
649 $res = sqlStatement($query);
651 echo " <select name='ProviderID'>\n";
652 echo " <option value=''>-- Please Select --\n";
654 while ($row = sqlFetchArray($res)) {
655 $provid = $row['id'];
656 echo " <option value='$provid'";
657 if ($provid == $encounter_provid) echo " selected";
658 echo ">" . $row['lname'] . ", " . $row['fname'] . "\n";
664  
;  
;  
;  
;  
;  
;  
;  
;  
;  
;
666 <input type
='submit' name
='bn_save' value
='<?php xl('Save
','e
');?>' />
668 <input type
='submit' name
='bn_refresh' value
='<?php xl('Refresh
','e
');?>'>
670 <input type
='button' value
='<?php xl('Cancel
','e
');?>'
671 onclick
="top.restoreSession();location='<? echo "$rootdir/patient_file
/encounter
/$returnurl" ?>'" />
673 <?php
if ($code_types['UCSMC']) { ?
>
674 <p style
='font-family:sans-serif;font-size:8pt;color:#666666;'>
676 <?php
xl('UCSMC codes provided by the University of Calgary Sports Medicine Centre','e');?
>
685 // TBD: If $alertmsg, display it with a JavaScript alert().
688 <script language
='JavaScript'>
689 <?php
echo $justinit; ?
>