allow exclusion of individual code types from the fee sheet
[openemr.git] / interface / forms / fee_sheet / view.php
blob569102b4bc335fe417ca92d178562ad3e39e4b97
1 <?php
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.
7 //
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-2008 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 require_once("../../globals.php");
22 require_once("$srcdir/acl.inc");
23 require_once("$srcdir/api.inc");
24 require_once("codes.php");
25 require_once("../../../custom/code_types.inc.php");
26 require_once("../../drugs/drugs.inc.php");
28 // Possible units of measure for NDC drug quantities.
30 $ndc_uom_choices = array(
31 'ML' => 'ML',
32 'GR' => 'Grams',
33 'F2' => 'I.U.',
34 'UN' => 'Units'
37 // $FEE_SHEET_COLUMNS should be defined in codes.php.
38 if (empty($FEE_SHEET_COLUMNS)) $FEE_SHEET_COLUMNS = 2;
40 $returnurl = $GLOBALS['concurrent_layout'] ? 'encounter_top.php' : 'patient_encounter.php';
42 // If Save was clicked, save the new and modified billing lines;
43 // then if no error, redirect to $returnurl.
45 if ($_POST['bn_save']) {
46 $provid = $_POST['ProviderID'];
47 if (! $provid) $provid = $_SESSION["authUserID"];
49 $bill = $_POST['bill'];
50 for ($lino = 1; $bill["$lino"]['code_type']; ++$lino) {
51 $iter = $bill["$lino"];
53 // Skip disabled (billed) line items.
54 if ($iter['billed']) continue;
56 $id = $iter['id'];
57 $code_type = $iter['code_type'];
58 $code = $iter['code'];
59 $modifier = trim($iter['mod']);
60 $units = max(1, intval(trim($iter['units'])));
61 $fee = 0 + trim($iter['fee']);
62 if ($code_type == 'COPAY') {
63 if ($fee > 0) $fee = 0 - $fee;
64 $code = sprintf('%01.2f', 0 - $fee);
66 $justify = trim($iter['justify']);
67 if ($justify) $justify = str_replace(',', ':', $justify) . ':';
68 // $auth = $iter['auth'] ? "1" : "0";
69 $auth = "1";
70 $del = $iter['del'];
72 $ndc_info = '';
73 if ($iter['ndcnum']) {
74 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
75 trim($iter['ndcqty']);
78 // If the item is already in the database...
79 if ($id) {
80 if ($del) {
81 deleteBilling($id);
83 else {
84 // authorizeBilling($id, $auth);
85 sqlQuery("UPDATE billing SET code = '$code', " .
86 "units = '$units', fee = '$fee', modifier = '$modifier', " .
87 "authorized = $auth, provider_id = '$provid', " .
88 "ndc_info = '$ndc_info', justify = '$justify' WHERE " .
89 "id = '$id' AND billed = 0 AND activity = 1");
93 // Otherwise it's a new item...
94 else if (! $del) {
95 $query = "select code_text from codes where code_type = '" .
96 $code_types[$code_type]['id'] . "' and " .
97 "code = '$code' and ";
98 if ($modifier) {
99 $query .= "modifier = '$modifier'";
100 } else {
101 $query .= "(modifier is null or modifier = '')";
103 $result = sqlQuery($query);
104 $code_text = addslashes($result['code_text']);
105 addBilling($encounter, $code_type, $code, $code_text, $pid, $auth,
106 $provid, $modifier, $units, $fee, $ndc_info, $justify);
108 } // end for
110 // Doing similarly to the above but for products.
111 $prod = $_POST['prod'];
112 for ($lino = 1; $prod["$lino"]['drug_id']; ++$lino) {
113 $iter = $prod["$lino"];
115 if (!empty($iter['billed'])) continue;
117 $drug_id = $iter['drug_id'];
118 $sale_id = $iter['sale_id']; // present only if already saved
119 $units = max(1, intval(trim($iter['units'])));
120 $fee = 0 + trim($iter['fee']);
121 $del = $iter['del'];
123 // If the item is already in the database...
124 if ($sale_id) {
125 if ($del) {
126 // Zero out this sale and reverse its inventory update. We bring in
127 // drug_sales twice so that the original quantity can be referenced
128 // unambiguously.
129 sqlStatement("UPDATE drug_sales AS dsr, drug_sales AS ds, " .
130 "drug_inventory AS di " .
131 "SET di.on_hand = di.on_hand + dsr.quantity, " .
132 "ds.quantity = 0, ds.fee = 0 WHERE " .
133 "dsr.sale_id = '$sale_id' AND ds.sale_id = dsr.sale_id AND " .
134 "di.inventory_id = ds.inventory_id");
135 // And delete the sale for good measure.
136 sqlStatement("DELETE FROM drug_sales WHERE sale_id = '$sale_id'");
138 else {
139 // Modify the sale and adjust inventory accordingly.
140 $query = "UPDATE drug_sales AS dsr, drug_sales AS ds, " .
141 "drug_inventory AS di " .
142 "SET di.on_hand = di.on_hand + dsr.quantity - $units, " .
143 "ds.quantity = '$units', ds.fee = '$fee' WHERE " .
144 "dsr.sale_id = '$sale_id' AND ds.sale_id = dsr.sale_id AND " .
145 "di.inventory_id = ds.inventory_id";
146 sqlStatement($query);
150 // Otherwise it's a new item...
151 else if (! $del) {
152 $sale_id = sellDrug($drug_id, $units, $fee, $pid, $encounter);
153 if (!$sale_id) die("Insufficient inventory for product ID \"$drug_id\".");
155 } // end for
157 // Note: I was going to compute taxes here, but now I think better to
158 // do that at checkout time (in pos_checkout.php which also posts to SL).
159 // Currently taxes with insurance claims make no sense, so for now we'll
160 // ignore tax computation in the insurance billing logic.
162 formHeader("Redirecting....");
163 formJump();
164 formFooter();
165 exit;
168 $billresult = getBillingByEncounter($pid, $encounter, "*");
170 <html>
171 <head>
172 <?php html_header_show(); ?>
173 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
174 <style>
175 .billcell { font-family: sans-serif; font-size: 10pt }
176 </style>
177 <script language="JavaScript">
179 var diags = new Array();
181 <?php
182 // Generate JavaScript to build the array of diagnoses.
183 function genDiagJS($code_type, $code) {
184 if ($code_type == 'ICD9') {
185 echo "diags.push('$code');\n";
188 if ($billresult) {
189 foreach ($billresult as $iter) {
190 genDiagJS($iter["code_type"], trim($iter["code"]));
193 if ($_POST['bill']) {
194 foreach ($_POST['bill'] as $iter) {
195 if ($iter["del"]) continue; // skip if Delete was checked
196 if ($iter["id"]) continue; // skip if it came from the database
197 genDiagJS($iter["code_type"], $iter["code"]);
200 if ($_POST['newcodes']) {
201 $arrcodes = explode('~', $_POST['newcodes']);
202 foreach ($arrcodes as $codestring) {
203 if ($codestring === '') continue;
204 $arrcode = explode('|', $codestring);
205 list($code, $modifier) = explode(":", $arrcode[1]);
206 genDiagJS($arrcode[0], $code);
211 // This is invoked by <select onchange> for the various dropdowns,
212 // including search results.
213 function codeselect(selobj) {
214 var i = selobj.selectedIndex;
215 if (i > 0) {
216 top.restoreSession();
217 var f = document.forms[0];
218 f.newcodes.value = selobj.options[i].value;
219 f.submit();
223 function copayselect() {
224 top.restoreSession();
225 var f = document.forms[0];
226 f.newcodes.value = 'COPAY||';
227 f.submit();
230 function validate(f) {
231 for (var lino = 1; f['bill['+lino+'][code_type]']; ++lino) {
232 var pfx = 'bill['+lino+']';
233 if (f[pfx+'[ndcnum]'] && f[pfx+'[ndcnum]'].value) {
234 // Check NDC number format.
235 var ndcok = true;
236 var ndc = f[pfx+'[ndcnum]'].value;
237 var a = ndc.split('-');
238 if (a.length != 3) {
239 ndcok = false;
241 else if (a[0].length < 1 || a[1].length < 1 || a[2].length < 1 ||
242 a[0].length > 5 || a[1].length > 4 || a[2].length > 2) {
243 ndcok = false;
245 else {
246 for (var i = 0; i < 3; ++i) {
247 for (var j = 0; j < a[i].length; ++j) {
248 var c = a[i].charAt(j);
249 if (c < '0' || c > '9') ndcok = false;
253 if (!ndcok) {
254 alert('<?php xl('Format incorrect for NDC','e') ?> "' + ndc +
255 '", <?php xl('should be like nnnnn-nnnn-nn','e') ?>');
256 if (f[pfx+'[ndcnum]'].focus) f[pfx+'[ndcnum]'].focus();
257 return false;
259 // Check for valid quantity.
260 var qty = f[pfx+'[ndcqty]'].value - 0;
261 if (isNaN(qty) || qty <= 0) {
262 alert('<?php xl('Quantity for NDC','e') ?> "' + ndc +
263 '" <?php xl('is not valid (decimal fractions are OK).','e') ?>');
264 if (f[pfx+'[ndcqty]'].focus) f[pfx+'[ndcqty]'].focus();
265 return false;
269 top.restoreSession();
270 return true;
273 // When a justify selection is made, apply it to the current list for
274 // this procedure and then rebuild its selection list.
276 function setJustify(seljust) {
277 var theopts = seljust.options;
278 var jdisplay = theopts[0].text;
279 // Compute revised justification string. Note this does nothing if
280 // the first entry is still selected, which is handy at startup.
281 if (seljust.selectedIndex > 0) {
282 var newdiag = seljust.value;
283 if (newdiag.length == 0) {
284 jdisplay = '';
286 else {
287 if (jdisplay.length) jdisplay += ',';
288 jdisplay += newdiag;
291 // Rebuild selection list.
292 var jhaystack = ',' + jdisplay + ',';
293 var j = 0;
294 theopts.length = 0;
295 theopts[j++] = new Option(jdisplay,jdisplay,true,true);
296 for (var i = 0; i < diags.length; ++i) {
297 if (jhaystack.indexOf(',' + diags[i] + ',') < 0) {
298 theopts[j++] = new Option(diags[i],diags[i],false,false);
301 theopts[j++] = new Option('Clear','',false,false);
304 </script>
305 </head>
307 <body <?echo $top_bg_line;?> topmargin="0" rightmargin="0" leftmargin="2"
308 bottommargin="0" marginwidth="2" marginheight="0">
309 <form method="post" action="<?php echo $rootdir; ?>/forms/fee_sheet/new.php"
310 onsubmit="return validate(this)">
311 <span class="title"><?php xl('Fee Sheet','e'); ?></span><br>
312 <input type='hidden' name='newcodes' value=''>
314 <center>
315 <table width='95%'>
316 <?php
317 $i = 0;
318 $last_category = '';
320 // Helper function for creating drop-lists.
321 function endFSCategory() {
322 global $i, $last_category, $FEE_SHEET_COLUMNS;
323 if (! $last_category) return;
324 echo " </select>\n";
325 echo " </td>\n";
326 if ($i >= $FEE_SHEET_COLUMNS) {
327 echo " </tr>\n";
328 $i = 0;
332 // Create all the drop-lists of preselected service codes.
333 $res = sqlStatement("SELECT * FROM fee_sheet_options " .
334 "ORDER BY fs_category, fs_option");
335 while ($row = sqlFetchArray($res)) {
336 $fs_category = $row['fs_category'];
337 $fs_option = $row['fs_option'];
338 $fs_codes = $row['fs_codes'];
339 if($fs_category !== $last_category) {
340 endFSCategory();
341 $last_category = $fs_category;
342 ++$i;
343 echo ($i <= 1) ? " <tr>\n" : "";
344 echo " <td width='50%' align='center' nowrap>\n";
345 echo " <select style='width:96%' onchange='codeselect(this)'>\n";
346 echo " <option value=''> " . substr($fs_category, 1) . "\n";
348 echo " <option value='$fs_codes'>" . substr($fs_option, 1) . "\n";
350 endFSCategory();
352 // Create one more drop-list, for Products.
353 if ($GLOBALS['sell_non_drug_products']) {
354 ++$i;
355 echo ($i <= 1) ? " <tr>\n" : "";
356 echo " <td width='50%' align='center' nowrap>\n";
357 echo " <select name='Products' style='width:96%' onchange='codeselect(this)'>\n";
358 echo " <option value=''> " . xl('Products') . "\n";
359 $tres = sqlStatement("SELECT dt.drug_id, dt.selector, d.name " .
360 "FROM drug_templates AS dt, drugs AS d WHERE " .
361 "d.drug_id = dt.drug_id " .
362 "ORDER BY d.name, dt.selector, dt.drug_id");
363 while ($trow = sqlFetchArray($tres)) {
364 echo " <option value='PROD|" . $trow['drug_id'] . '|' . $trow['selector'] . "'>" .
365 $trow['drug_id'] . ':' . $trow['selector'] . ' ' . $trow['name'] . "</option>\n";
367 echo " </select>\n";
368 echo " </td>\n";
369 if ($i >= $FEE_SHEET_COLUMNS) {
370 echo " </tr>\n";
371 $i = 0;
375 $search_type = $default_search_type;
376 if ($_POST['search_type']) $search_type = $_POST['search_type'];
378 $ndc_applies = true; // Assume all payers require NDC info.
380 echo $i ? " <td></td>\n </tr>\n" : "";
381 echo " <tr>\n";
382 echo " <td colspan='$FEE_SHEET_COLUMNS' align='center' nowrap>\n";
384 // If Search was clicked, do it and write the list of results here.
385 // There's no limit on the number of results!
387 $numrows = 0;
388 if ($_POST['bn_search'] && $_POST['search_term']) {
389 $query = "select code, modifier, code_text from codes where " .
390 "(code_text like '%" . $_POST['search_term'] . "%' or " .
391 "code like '%" . $_POST['search_term'] . "%') and " .
392 "code_type = '" . $code_types[$search_type]['id'] . "' " .
393 "order by code";
394 $res = sqlStatement($query);
395 $numrows = mysql_num_rows($res); // FIXME - not portable!
398 echo " <select name='Search Results' style='width:98%' " .
399 "onchange='codeselect(this)'";
400 if (! $numrows) echo ' disabled';
401 echo ">\n";
402 echo " <option value=''> Search Results ($numrows items)\n";
404 if ($numrows) {
405 while ($row = sqlFetchArray($res)) {
406 $code = $row['code'];
407 if ($row['modifier']) $code .= ":" . $row['modifier'];
408 echo " <option value='$search_type|$code|'>$code " .
409 ucfirst(strtolower($row['code_text'])) . "</option>\n";
413 echo " </select>\n";
414 echo " </td>\n";
415 echo " </tr>\n";
418 </table>
420 <p style='margin-top:8px;margin-bottom:8px'>
421 <table>
422 <tr>
423 <td>
424 <input type='button' value='<?php xl('Add Copay','e');?>'
425 onclick="copayselect()" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
426 </td>
427 <td>
428 <?php xl('Search','e'); ?>&nbsp;
430 foreach ($code_types as $key => $value) {
431 if (!empty($value['nofs'])) continue;
432 echo " <input type='radio' name='search_type' value='$key'";
433 if ($key == $default_search_type) echo " checked";
434 echo " />$key&nbsp;\n";
437 <?php xl('for','e'); ?>&nbsp;
438 </td>
439 <td>
440 <input type='text' name='search_term' value=''> &nbsp;
441 </td>
442 <td>
443 <input type='submit' name='bn_search' value='<?php xl('Search','e');?>'>
444 </td>
445 </tr>
446 </table>
447 </p>
449 <p style='margin-top:16px;margin-bottom:8px'>
450 <table cellspacing='5'>
451 <tr>
452 <td class='billcell'><b><?php xl('Type','e');?></b></td>
453 <td class='billcell'><b><?php xl('Code','e');?></b></td>
454 <? if (modifiers_are_used(true)) { ?>
455 <td class='billcell'><b><?php xl('Mod','e');?></b></td>
456 <? } ?>
457 <? if (fees_are_used()) { ?>
458 <td class='billcell' align='center'><b><?php xl('Units','e');?></b></td>
459 <td class='billcell' align='right'><b><?php xl('Fee','e');?></b>&nbsp;</td>
460 <td class='billcell' align='center'><b><?php xl('Justify','e');?></b></td>
461 <? } ?>
462 <td class='billcell' align='center'><b><?php xl('Auth','e');?></b></td>
463 <td class='billcell' align='center'><b><?php xl('Delete','e');?></b></td>
464 <td class='billcell'><b><?php xl('Description','e');?></b></td>
465 </tr>
467 <?php
468 $justinit = "var f = document.forms[0];\n";
470 // This writes a billing line item to the output page.
472 function echoLine($lino, $codetype, $code, $modifier, $ndc_info='',
473 $auth = TRUE, $del = FALSE, $units = NULL, $fee = NULL, $id = NULL,
474 $billed = FALSE, $code_text = NULL, $justify = NULL)
476 global $code_types, $ndc_applies, $ndc_uom_choices, $justinit, $pid;
478 if ($codetype == 'COPAY') {
479 if (!$code_text) $code_text = 'Cash';
480 if ($fee > 0) $fee = 0 - $fee;
482 if (! $code_text) {
483 // $query = "select units, fee, code_text from codes where code_type = '" .
484 $query = "select id, units, code_text from codes where code_type = '" .
485 $code_types[$codetype]['id'] . "' and " .
486 "code = '$code' and ";
487 if ($modifier) {
488 $query .= "modifier = '$modifier'";
489 } else {
490 $query .= "(modifier is null or modifier = '')";
492 $result = sqlQuery($query);
493 $code_text = $result['code_text'];
494 if (empty($units)) $units = max(1, intval($result['units']));
495 if (!isset($fee)) {
496 // $fee = $result['fee'];
497 // The above is obsolete now, fees come from the prices table:
498 $query = "SELECT prices.pr_price " .
499 "FROM patient_data, prices WHERE " .
500 "patient_data.pid = '$pid' AND " .
501 "prices.pr_id = '" . $result['id'] . "' AND " .
502 "prices.pr_selector = '' AND " .
503 "prices.pr_level = patient_data.pricelevel " .
504 "LIMIT 1";
505 echo "\n<!-- $query -->\n"; // debugging
506 $prrow = sqlQuery($query);
507 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
510 $fee = sprintf('%01.2f', $fee);
511 $strike1 = ($id && $del) ? "<strike>" : "";
512 $strike2 = ($id && $del) ? "</strike>" : "";
513 echo " <tr>\n";
514 echo " <td class='billcell'>$strike1$codetype$strike2";
515 if ($id) {
516 echo "<input type='hidden' name='bill[$lino][id]' value='$id'>";
518 echo "<input type='hidden' name='bill[$lino][code_type]' value='$codetype'>";
519 echo "<input type='hidden' name='bill[$lino][code]' value='$code'>";
520 echo "<input type='hidden' name='bill[$lino][billed]' value='$billed'>";
521 echo "</td>\n";
522 if ($codetype != 'COPAY') {
523 echo " <td class='billcell'>$strike1$code$strike2</td>\n";
524 } else {
525 echo " <td class='billcell'>&nbsp;</td>\n";
527 if ($billed) {
528 if (modifiers_are_used(true)) {
529 echo " <td class='billcell'>$strike1$modifier$strike2" .
530 "<input type='hidden' name='bill[$lino][mod]' value='$modifier'></td>\n";
532 if (fees_are_used()) {
533 if ($codetype != 'COPAY') {
534 echo " <td class='billcell' align='center'>$units</td>\n";
535 } else {
536 echo " <td class='billcell'>&nbsp;</td>\n";
538 echo " <td class='billcell' align='right'>$fee</td>\n";
539 echo " <td class='billcell' align='center'>$justify</td>\n";
541 echo " <td class='billcell' align='center'><input type='checkbox'" .
542 ($auth ? " checked" : "") . " disabled /></td>\n";
543 echo " <td class='billcell' align='center'><input type='checkbox'" .
544 " disabled /></td>\n";
545 } else {
546 if (modifiers_are_used(true)) {
547 if ($codetype != 'COPAY' && ($code_types[$codetype]['mod'] || $modifier)) {
548 echo " <td class='billcell'><input type='text' name='bill[$lino][mod]' " .
549 "value='$modifier' size='" . $code_types[$codetype]['mod'] . "'></td>\n";
550 } else {
551 echo " <td class='billcell'>&nbsp;</td>\n";
554 if (fees_are_used()) {
555 if ($codetype == 'COPAY' || $code_types[$codetype]['fee'] || $fee != 0) {
556 echo " <td class='billcell' align='center'>";
557 if ($codetype != 'COPAY') {
558 echo "<input type='text' name='bill[$lino][units]' " .
559 "value='$units' size='2' style='text-align:right'>";
560 } else {
561 echo "<input type='hidden' name='bill[$lino][units]' value='$units'>";
563 echo "</td>\n";
564 echo " <td class='billcell' align='right'>" .
565 "<input type='text' name='bill[$lino][fee]' " .
566 "value='$fee' size='6'";
567 if (acl_check('acct','disc'))
568 echo " style='text-align:right'";
569 else
570 echo " style='text-align:right;background-color:transparent' readonly";
571 echo "></td>\n";
572 if ($code_types[$codetype]['just'] || $justify) {
573 echo " <td class='billcell' align='center'>";
574 echo "<select name='bill[$lino][justify]' onchange='setJustify(this)'>";
575 echo "<option value='$justify'>$justify</option></select>";
576 echo "</td>\n";
577 $justinit .= "setJustify(f['bill[$lino][justify]']);\n";
578 } else {
579 echo " <td class='billcell'>&nbsp;</td>\n";
581 } else {
582 echo " <td class='billcell'>&nbsp;</td>\n";
583 echo " <td class='billcell'>&nbsp;</td>\n";
584 echo " <td class='billcell'>&nbsp;</td>\n";
587 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][auth]' " .
588 "value='1'" . ($auth ? " checked" : "") . " /></td>\n";
589 echo " <td class='billcell' align='center'><input type='checkbox' name='bill[$lino][del]' " .
590 "value='1'" . ($del ? " checked" : "") . " /></td>\n";
593 echo " <td class='billcell'>$strike1" . ucfirst(strtolower($code_text)) . "$strike2</td>\n";
594 echo " </tr>\n";
596 // If NDC info exists or may be required, add a line for it.
597 if ($codetype == 'HCPCS' && $ndc_applies && !$billed) {
598 $ndcnum = ''; $ndcuom = ''; $ndcqty = '';
599 if (preg_match('/^N4(\S+)\s+(\S\S)(.*)/', $ndc_info, $tmp)) {
600 $ndcnum = $tmp[1]; $ndcuom = $tmp[2]; $ndcqty = $tmp[3];
602 echo " <tr>\n";
603 echo " <td class='billcell' colspan='2'>&nbsp;</td>\n";
604 echo " <td class='billcell' colspan='6'>&nbsp;NDC:&nbsp;";
605 echo "<input type='text' name='bill[$lino][ndcnum]' value='$ndcnum' " .
606 "size='11' style='background-color:transparent'>";
607 echo " &nbsp;Qty:&nbsp;";
608 echo "<input type='text' name='bill[$lino][ndcqty]' value='$ndcqty' " .
609 "size='3' style='background-color:transparent;text-align:right'>";
610 echo " ";
611 echo "<select name='bill[$lino][ndcuom]' style='background-color:transparent'>";
612 foreach ($ndc_uom_choices as $key => $value) {
613 echo "<option value='$key'";
614 if ($key == $ndcuom) echo " selected";
615 echo ">$value</option>";
617 echo "</select>";
618 echo "</td>\n";
619 echo " </tr>\n";
621 else if ($ndc_info) {
622 echo " <tr>\n";
623 echo " <td class='billcell' colspan='2'>&nbsp;</td>\n";
624 echo " <td class='billcell' colspan='6'>&nbsp;NDC Data: $ndc_info</td>\n";
625 echo " </tr>\n";
629 // This writes a product (drug_sales) line item to the output page.
631 function echoProdLine($lino, $drug_id, $del = FALSE, $units = NULL,
632 $fee = NULL, $sale_id = 0, $billed = FALSE)
634 global $code_types, $ndc_applies, $pid;
636 /*******************************************************************
637 list ($drug_id, $selector) = explode(':', $drugsel);
638 if (! $units) { // if this is a new selection then apply defaults for it
639 $query = "SELECT dt.*, d.name FROM drug_templates, drugs WHERE " .
640 "dt.drug_id = '$drug_id' AND dt.selector = '$selector' AND " .
641 "d.drug_id = dt.drug_id";
642 $result = sqlQuery($query);
643 $code_text = $result['name'] . " ($selector)";
644 if (empty($units)) $units = max(1, intval($result['quantity']));
645 if (!isset($fee)) {
646 // Fees come from the prices table:
647 $query = "SELECT prices.pr_price " .
648 "FROM patient_data, prices WHERE " .
649 "patient_data.pid = '$pid' AND " .
650 "prices.pr_id = '$drug_id' AND " .
651 "prices.pr_selector = '$selector' AND " .
652 "prices.pr_level = patient_data.pricelevel " .
653 "LIMIT 1";
654 // echo "\n<!-- $query -->\n"; // debugging
655 $prrow = sqlQuery($query);
656 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
659 *******************************************************************/
660 $drow = sqlQuery("SELECT name FROM drugs WHERE drug_id = '$drug_id'");
661 $code_text = $drow['name'];
662 /******************************************************************/
664 $fee = sprintf('%01.2f', $fee);
665 $strike1 = ($sale_id && $del) ? "<strike>" : "";
666 $strike2 = ($sale_id && $del) ? "</strike>" : "";
667 echo " <tr>\n";
668 echo " <td class='billcell'>{$strike1}Product$strike2";
669 echo "<input type='hidden' name='prod[$lino][sale_id]' value='$sale_id'>";
670 echo "<input type='hidden' name='prod[$lino][drug_id]' value='$drug_id'>";
671 echo "<input type='hidden' name='prod[$lino][billed]' value='$billed'>";
672 echo "</td>\n";
673 echo " <td class='billcell'>$strike1$drug_id$strike2</td>\n";
674 if (modifiers_are_used(true)) {
675 echo " <td class='billcell'>&nbsp;</td>\n";
677 if ($billed) {
678 if (fees_are_used()) {
679 echo " <td class='billcell' align='center'>$units</td>\n";
680 echo " <td class='billcell' align='right'>$fee</td>\n";
681 echo " <td class='billcell' align='center'>&nbsp;</td>\n"; // justify
683 echo " <td class='billcell' align='center'>&nbsp;</td>\n"; // auth
684 echo " <td class='billcell' align='center'><input type='checkbox'" . // del
685 " disabled /></td>\n";
686 } else {
687 if (fees_are_used()) {
688 echo " <td class='billcell' align='center'>";
689 echo "<input type='text' name='prod[$lino][units]' " .
690 "value='$units' size='2' style='text-align:right'>";
691 echo "</td>\n";
692 echo " <td class='billcell' align='right'>" .
693 "<input type='text' name='prod[$lino][fee]' " .
694 "value='$fee' size='6'";
695 if (acl_check('acct','disc'))
696 echo " style='text-align:right'";
697 else
698 echo " style='text-align:right;background-color:transparent' readonly";
699 echo "></td>\n";
700 echo " <td class='billcell'>&nbsp;</td>\n";
702 echo " <td class='billcell' align='center'>&nbsp;</td>\n"; // auth
703 echo " <td class='billcell' align='center'><input type='checkbox' name='prod[$lino][del]' " .
704 "value='1'" . ($del ? " checked" : "") . " /></td>\n";
707 echo " <td class='billcell'>$strike1" . ucfirst(strtolower($code_text)) . "$strike2</td>\n";
708 echo " </tr>\n";
711 $encounter_provid = -1;
713 // Generate lines for items already in the billing table for this encounter,
714 // and also set the rendering provider if we come across one.
716 $bill_lino = 0;
717 if ($billresult) {
718 foreach ($billresult as $iter) {
719 ++$bill_lino;
720 $bline = $_POST['bill']["$bill_lino"];
721 $del = $bline['del']; // preserve Delete if checked
723 $modifier = trim($iter["modifier"]);
724 $units = $iter["units"];
725 $fee = $iter["fee"];
726 $authorized = $iter["authorized"];
727 $ndc_info = $iter["ndc_info"];
728 $justify = trim($iter['justify']);
729 if ($justify) $justify = substr(str_replace(':', ',', $justify), 0, strlen($justify) - 1);
731 // Also preserve other items from the form, if present.
732 if ($bline['id'] && !$iter["billed"]) {
733 $modifier = trim($bline['mod']);
734 $units = trim($bline['units']);
735 $fee = trim($bline['fee']);
736 $authorized = $bline['auth'];
737 $ndc_info = '';
738 if ($bline['ndcnum']) {
739 $ndc_info = 'N4' . trim($bline['ndcnum']) . ' ' . $bline['ndcuom'] .
740 trim($bline['ndcqty']);
742 $justify = $bline['justify'];
745 // list($code, $modifier) = explode("-", $iter["code"]);
746 echoLine($bill_lino, $iter["code_type"], trim($iter["code"]),
747 $modifier, $ndc_info, $authorized,
748 $del, $units, $fee, $iter["id"], $iter["billed"],
749 $iter["code_text"], $justify);
750 // If no default provider yet then try this one.
751 if ($encounter_provid < 0 && ! $del) $encounter_provid = $iter["provider_id"];
755 // Echo new billing items from this form here, but omit any line
756 // whose Delete checkbox is checked.
758 if ($_POST['bill']) {
759 foreach ($_POST['bill'] as $key => $iter) {
760 if ($iter["id"]) continue; // skip if it came from the database
761 if ($iter["del"]) continue; // skip if Delete was checked
762 $ndc_info = '';
763 if ($iter['ndcnum']) {
764 $ndc_info = 'N4' . trim($iter['ndcnum']) . ' ' . $iter['ndcuom'] .
765 trim($iter['ndcqty']);
767 $fee = 0 + trim($iter['fee']);
768 if ($iter['code_type'] == 'COPAY' && $fee > 0) $fee = 0 - $fee;
769 echoLine(++$bill_lino, $iter["code_type"], $iter["code"], trim($iter["mod"]),
770 $ndc_info, $iter["auth"], $iter["del"], trim($iter["units"]),
771 $fee, NULL, FALSE, NULL, $iter["justify"]);
775 // Generate lines for items already in the drug_sales table for this encounter.
777 $query = "SELECT * FROM drug_sales WHERE " .
778 "pid = '$pid' AND encounter = '$encounter' " .
779 "ORDER BY sale_id";
780 $sres = sqlStatement($query);
781 $prod_lino = 0;
782 while ($srow = sqlFetchArray($sres)) {
783 ++$prod_lino;
784 $pline = $_POST['prod']["$prod_lino"];
785 $del = $pline['del']; // preserve Delete if checked
786 $sale_id = $srow['sale_id'];
787 $drug_id = $srow['drug_id'];
788 $units = $srow['quantity'];
789 $fee = $srow['fee'];
790 $billed = $srow['billed'];
791 // Also preserve other items from the form, if present and unbilled.
792 if ($pline['sale_id'] && !$srow['billed']) {
793 $units = trim($pline['units']);
794 $fee = trim($pline['fee']);
796 echoProdLine($prod_lino, $drug_id, $del, $units, $fee, $sale_id, $billed);
799 // Echo new product items from this form here, but omit any line
800 // whose Delete checkbox is checked.
802 if ($_POST['prod']) {
803 foreach ($_POST['prod'] as $key => $iter) {
804 if ($iter["sale_id"]) continue; // skip if it came from the database
805 if ($iter["del"]) continue; // skip if Delete was checked
806 $fee = 0 + trim($iter['fee']);
807 echoProdLine(++$prod_lino, $iter['drug_id'], FALSE, trim($iter["units"]), $fee);
811 // If new billing code(s) were <select>ed, add their line(s) here.
813 if ($_POST['newcodes']) {
814 $arrcodes = explode('~', $_POST['newcodes']);
815 foreach ($arrcodes as $codestring) {
816 if ($codestring === '') continue;
817 $arrcode = explode('|', $codestring);
818 $newtype = $arrcode[0];
819 $newcode = $arrcode[1];
820 $newsel = $arrcode[2];
821 if ($newtype == 'COPAY') {
822 $tmp = sqlQuery("SELECT copay FROM insurance_data WHERE pid = '$pid' " .
823 "AND type = 'primary' ORDER BY date DESC LIMIT 1");
824 $code = sprintf('%01.2f', 0 + $tmp['copay']);
825 echoLine(++$bill_lino, $newtype, $code, '', '', '1', '0', '1',
826 sprintf('%01.2f', 0 - $code));
828 else if ($newtype == 'PROD') {
829 $result = sqlQuery("SELECT * FROM drug_templates WHERE " .
830 "drug_id = '$newcode' AND selector = '$newsel'");
831 $units = max(1, intval($result['quantity']));
832 $prrow = sqlQuery("SELECT prices.pr_price " .
833 "FROM patient_data, prices WHERE " .
834 "patient_data.pid = '$pid' AND " .
835 "prices.pr_id = '$newcode' AND " .
836 "prices.pr_selector = '$newsel' AND " .
837 "prices.pr_level = patient_data.pricelevel " .
838 "LIMIT 1");
839 $fee = empty($prrow) ? 0 : $prrow['pr_price'];
840 echoProdLine(++$prod_lino, $newcode, FALSE, $units, $fee);
842 else {
843 list($code, $modifier) = explode(":", $newcode);
844 $ndc_info = '';
845 // If HCPCS, find last NDC string used for this code.
846 if ($newtype == 'HCPCS' && $ndc_applies) {
847 $tmp = sqlQuery("SELECT ndc_info FROM billing WHERE " .
848 "code_type = '$newtype' AND code = '$code' AND ndc_info LIKE 'N4%' " .
849 "ORDER BY date DESC LIMIT 1");
850 if (!empty($tmp)) $ndc_info = $tmp['ndc_info'];
852 echoLine(++$bill_lino, $newtype, $code, trim($modifier), $ndc_info);
857 // If no valid provider yet, try setting it to that of the new encounter form.
859 $tmp = sqlQuery("SELECT authorized FROM users WHERE id = '$encounter_provid'");
860 if (empty($tmp['authorized'])) {
861 $encounter_provid = -1;
862 $tmp = sqlQuery("SELECT users.id FROM forms, users WHERE " .
863 "forms.pid = '$pid' AND forms.encounter = '$encounter' AND " .
864 "forms.formdir='newpatient' AND users.username = forms.user AND " .
865 "users.authorized = 1");
866 if ($tmp['id']) $encounter_provid = $tmp['id'];
869 // If still no default provider then make it the logged-in user.
871 if ($encounter_provid < 0) $encounter_provid = $_SESSION["authUserID"];
873 </table>
874 </p>
876 <br>
877 &nbsp;
879 <span class="billcell"><?php xl('PROVIDER:','e');?></span>
881 <?php
882 // Build a drop-down list of providers. This includes users who
883 // have the word "provider" anywhere in their "additional info"
884 // field, so that we can define providers (for billing purposes)
885 // who do not appear in the calendar.
887 $query = "SELECT id, lname, fname FROM users WHERE " .
888 "( authorized = 1 OR info LIKE '%provider%' ) AND username != '' " .
889 "ORDER BY lname, fname";
890 $res = sqlStatement($query);
892 echo " <select name='ProviderID'>\n";
893 echo " <option value=''>-- Please Select --\n";
895 while ($row = sqlFetchArray($res)) {
896 $provid = $row['id'];
897 echo " <option value='$provid'";
898 if ($provid == $encounter_provid) echo " selected";
899 echo ">" . $row['lname'] . ", " . $row['fname'] . "\n";
902 echo " </select>\n";
905 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
907 <input type='submit' name='bn_save' value='<?php xl('Save','e');?>' />
908 &nbsp;
909 <input type='submit' name='bn_refresh' value='<?php xl('Refresh','e');?>'>
910 &nbsp;
911 <input type='button' value='<?php xl('Cancel','e');?>'
912 onclick="top.restoreSession();location='<? echo "$rootdir/patient_file/encounter/$returnurl" ?>'" />
914 <?php if ($code_types['UCSMC']) { ?>
915 <p style='font-family:sans-serif;font-size:8pt;color:#666666;'>
916 &nbsp;<br>
917 <?php xl('UCSMC codes provided by the University of Calgary Sports Medicine Centre','e');?>
918 </p>
919 <? } ?>
921 </center>
923 </form>
925 <?php
926 // TBD: If $alertmsg, display it with a JavaScript alert().
929 <script language='JavaScript'>
930 <?php echo $justinit; ?>
931 </script>
933 </body>
934 </html>