add hcfa text forms by bo2999, tracker artifact 2770036
[openemr.git] / interface / billing / billing_report.php
blob4bd61613721afa61e27ec0da934d1c56ff0aa757
1 <?php
2 require_once("../globals.php");
3 require_once("../../library/acl.inc");
4 require_once("../../custom/code_types.inc.php");
6 require_once("$srcdir/patient.inc");
7 require_once("$srcdir/billrep.inc");
8 require_once(dirname(__FILE__) . "/../../library/classes/OFX.class.php");
9 require_once(dirname(__FILE__) . "/../../library/classes/X12Partner.class.php");
11 $EXPORT_INC = "$webserver_root/custom/BillingExport.php";
13 $alertmsg = '';
15 if ($_POST['mode'] == 'export') {
16 $sdate = $_POST['from_date'];
17 $edate = $_POST['to_date'];
19 $sql = "SELECT billing.*, concat(pd.fname, ' ', pd.lname) as name from billing "
20 . "join patient_data as pd on pd.pid = billing.pid where billed = '1' and "
21 . "(process_date > '" . mysql_real_escape_string($sdate)
22 . "' or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($sdate) ."') "
23 . "and (process_date < '" . mysql_real_escape_string($edate)
24 . "'or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($edate) ."') "
25 . "order by pid,encounter";
26 $db = get_db();
27 $results = $db->Execute($sql);
28 $billings = array();
29 if ($results->RecordCount() == 0) {
30 echo xl("No Bills Found to Include in OFX Export<br>");
32 else {
33 while(!$results->EOF) {
34 $billings[] = $results->fields;
35 $results->MoveNext();
37 $ofx = new OFX($billings);
38 header("Pragma: public");
39 header("Expires: 0");
40 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
41 header("Content-Disposition: attachment; filename=openemr_ofx.ofx");
42 header("Content-Type: text/xml");
43 echo $ofx->get_OFX();
44 exit;
48 // This is obsolete.
49 if ($_POST['mode'] == 'process') {
50 if (exec("ps x | grep 'process_bills[.]php'")) {
51 $alertmsg = xl('Request ignored - claims processing is already running!');
53 else {
54 exec("cd $webserver_root/library/freeb;" .
55 "php -q process_bills.php bill > process_bills.log 2>&1 &");
56 $alertmsg = xl('Batch processing initiated; this may take a while.');
60 //global variables:
61 if (!isset($_POST["mode"])) {
62 $from_date = isset($_POST['from_date']) ? $_POST['from_date'] : date('Y-m-d');
63 $to_date = isset($_POST['to_date' ]) ? $_POST['to_date' ] : '';
64 $code_type = isset($_POST['code_type']) ? $_POST['code_type'] : 'all';
65 $unbilled = isset($_POST['unbilled' ]) ? $_POST['unbilled' ] : 'on';
66 $my_authorized = $_POST["authorized"];
67 } else {
68 $from_date = $_POST["from_date"];
69 $to_date = $_POST["to_date"];
70 $code_type = $_POST["code_type"];
71 $unbilled = $_POST["unbilled"];
72 $my_authorized = $_POST["authorized"];
76 $from_date = isset($_POST['from_date']) ? $_POST['from_date'] : date('Y-m-d');
77 $to_date = empty($_POST['to_date' ]) ? $from_date : $_POST['to_date'];
78 $code_type = isset($_POST['code_type']) ? $_POST['code_type'] : 'all';
79 $unbilled = isset($_POST['unbilled' ]) ? $_POST['unbilled' ] : 'on';
80 $my_authorized = $_POST["authorized"];
83 $left_margin = isset($_POST["left_margin"]) ? $_POST["left_margin"] : 24;
84 $top_margin = isset($_POST["top_margin"] ) ? $_POST["top_margin" ] : 20;
86 $ofrom_date = $from_date;
87 $oto_date = $to_date;
88 $ocode_type = $code_type;
89 $ounbilled = $unbilled;
90 $oauthorized = $my_authorized;
93 <html>
94 <head>
95 <?php if (function_exists(html_header_show)) html_header_show(); ?>
96 <link rel="stylesheet" href="<?php echo $css_header; ?>" type="text/css">
97 <style>
98 .subbtn { margin-top:3px; margin-bottom:3px; margin-left:2px; margin-right:2px }
99 </style>
100 <script>
102 function select_all() {
103 for($i=0;$i < document.update_form.length;$i++) {
104 $name = document.update_form[$i].name;
105 if ($name.substring(0,7) == "claims[" && $name.substring($name.length -6) == "[bill]") {
106 document.update_form[$i].checked = true;
109 set_button_states();
112 function set_button_states() {
113 var f = document.update_form;
114 var count0 = 0; // selected and not billed or queued
115 var count1 = 0; // selected and queued
116 var count2 = 0; // selected and billed
117 for($i = 0; $i < f.length; ++$i) {
118 $name = f[$i].name;
119 if ($name.substring(0, 7) == "claims[" && $name.substring($name.length -6) == "[bill]" && f[$i].checked == true) {
120 if (f[$i].value == '0') ++count0;
121 else if (f[$i].value == '1' || f[$i].value == '5') ++count1;
122 else ++count2;
126 var can_generate = (count0 > 0 || count1 > 0 || count2 > 0);
127 var can_mark = (count1 > 0 || count0 > 0 || count2 > 0);
128 var can_bill = (count0 == 0 && count1 == 0 && count2 > 0);
130 <?php if (file_exists($EXPORT_INC)) { ?>
131 f.bn_external.disabled = !can_generate;
132 <?php } else { ?>
133 // f.bn_hcfa_print.disabled = !can_generate;
134 // f.bn_hcfa.disabled = !can_generate;
135 // f.bn_ub92_print.disabled = !can_generate;
136 // f.bn_ub92.disabled = !can_generate;
137 f.bn_x12.disabled = !can_generate;
138 <?php if ($GLOBALS['support_encounter_claims']) { ?>
139 f.bn_x12_encounter.disabled = !can_generate;
140 <?php } ?>
141 f.bn_process_hcfa.disabled = !can_generate;
142 f.bn_hcfa_txt_file.disabled = !can_generate;
143 // f.bn_electronic_file.disabled = !can_bill;
144 f.bn_reopen.disabled = !can_bill;
145 <?php } ?>
146 f.bn_mark.disabled = !can_mark;
149 // Process a click to go to an encounter.
150 function toencounter(pid, pubpid, pname, enc, datestr) {
151 top.restoreSession();
152 <?php if ($GLOBALS['concurrent_layout']) { ?>
153 var othername = (window.name == 'RTop') ? 'RBot' : 'RTop';
154 parent.left_nav.setPatient(pname,pid,pubpid,'');
155 parent.left_nav.setEncounter(datestr, enc, othername);
156 parent.left_nav.setRadio(othername, 'enc');
157 parent.frames[othername].location.href =
158 '../patient_file/encounter/encounter_top.php?set_encounter='
159 + enc + '&pid=' + pid;
160 <?php } else { ?>
161 location.href = '../patient_file/encounter/patient_encounter.php?set_encounter='
162 + enc + '&pid=' + pid;
163 <?php } ?>
166 // Process a click to go to patient demographics.
167 // larry :: dbc insert
168 <?php if( $GLOBALS['dutchpc'] )
171 function topatient(pid) {
172 top.restoreSession();
173 <?php if ($GLOBALS['concurrent_layout']) { ?>
174 var othername = (window.name == 'RTop') ? 'RBot' : 'RTop';
175 parent.frames[othername].location.href =
176 '../patient_file/summary/demographics_full_dutch.php?pid=' + pid;
177 <?php } else { ?>
178 location.href = '../patient_file/summary/demographics_full_dutch.php?pid=' + pid;
179 <?php } ?>
182 <?php } else
183 { ?>
185 function topatient(pid) {
186 top.restoreSession();
187 <?php if ($GLOBALS['concurrent_layout']) { ?>
188 var othername = (window.name == 'RTop') ? 'RBot' : 'RTop';
189 parent.frames[othername].location.href =
190 '../patient_file/summary/demographics_full.php?pid=' + pid;
191 <?php } else { ?>
192 location.href = '../patient_file/summary/demographics_full.php?pid=' + pid;
193 <?php } ?>
195 <?php } ?>
196 // larry :: end of dbc change
198 </script>
199 </head>
200 <body class="body_top">
202 <p style='margin-top:5px;margin-bottom:5px;margin-left:5px'>
204 <?php if ($GLOBALS['concurrent_layout']) { ?>
205 <font class='title'><?php xl('Billing Report','e') ?></font>
206 <?php } else if ($userauthorized) { ?>
207 <a href="../main/main.php" target='Main' onclick='top.restoreSession()'><font class=title><?php xl('Billing Report','e') ?></font><font class=more> <?php echo $tback; ?></font></a>
208 <?php } else { ?>
209 <a href="../main/onotes/office_comments.php" target='Main' onclick='top.restoreSession()'><font class=title><?php xl('Billing Report','e') ?></font><font class=more><?php echo $tback; ?></font></a>
210 <?php } ?>
212 </p>
214 <form name='the_form' method='post' action='billing_report.php' onsubmit='return top.restoreSession()'>
216 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
217 <script type="text/javascript" src="../../library/dialog.js"></script>
218 <script type="text/javascript" src="../../library/textformat.js"></script>
219 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
220 <script type="text/javascript" src="../../library/dynarch_calendar_en.js"></script>
221 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
222 <script language='JavaScript'>
223 var mypcc = '1';
224 </script>
226 <input type='hidden' name='mode' value='change'>
228 <table width='100%' border="1" cellspacing="0" cellpadding="0">
229 <tr>
230 <td nowrap>
231 &nbsp;<span class='text'><?php xl('From: ','e') ?></span>
232 <input type='text' size='10' name='from_date' id='from_date'
233 value='<?php echo $from_date; ?>'
234 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
235 title=<?php xl('yyyy-mm-dd last date of this event','e','\'','\''); ?> />
236 <img src='../../interface/pic/show_calendar.gif' align='absbottom' width='24' height='22'
237 id='img_fromdate' border='0' alt='[?]' style='cursor:pointer'
238 title=<?php xl('Click here to choose a date','e','\'','\''); ?> />
239 <script>
240 Calendar.setup({inputField:"from_date", ifFormat:"%Y-%m-%d", button:"img_fromdate"});
241 </script>
242 </td>
244 <td nowrap>
245 &nbsp;<span class='text'><?php xl('To: ','e') ?></span>
246 <input type='text' size='10' name='to_date' id='to_date'
247 value='<?php echo $to_date; ?>'
248 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
249 title=<?php xl('yyyy-mm-dd last date of this event','e','\'','\''); ?> />
250 <img src='../../interface/pic/show_calendar.gif' align='absbottom' width='24' height='22'
251 id='img_todate' border='0' alt='[?]' style='cursor:pointer'
252 title=<?php xl('Click here to choose a date','e','\'','\''); ?> />
253 <script>
254 Calendar.setup({inputField:"to_date", ifFormat:"%Y-%m-%d", button:"img_todate"});
255 </script>
256 <input type="hidden" name="code_type" value="%" />
257 </td>
259 <td nowrap>
260 <input type='checkbox' name='unbilled' <?php if ($unbilled == "on") {echo "checked";}; ?> />
261 <span class='text'><?php xl('Show Unbilled Only','e') ?></span>
262 </td>
264 <td nowrap>
265 <input type='checkbox' name='authorized' <?php if ($my_authorized == "on") {echo "checked";}; ?> />
266 <span class='text'><?php xl('Show Authorized Only','e') ?></span>
267 </td>
269 <td align='right' width='10%' nowrap>
270 &nbsp;<span class='text'><a href="javascript:top.restoreSession();document.the_form.mode.value='change';document.the_form.submit()" class=link_submit>[<?php xl('Update List','e') ?>]</a>
272 <a href="javascript:top.restoreSession();document.the_form.mode.value='export';document.the_form.submit()" class='link_submit'><?php xl('[Export OFX]','e') ?></a></span>&nbsp;
273 </td>
275 </tr>
277 <tr>
279 <td nowrap>
280 &nbsp;<a href="print_billing_report.php?<?php print "from_date=".urlencode($ofrom_date)."&to_date=".urlencode($oto_date)."&code_type=".urlencode($ocode_type)."&unbilled=".urlencode($ounbilled)."&authorized=".urlencode($oauthorized); ?>"
281 class='link_submit' target='new' onclick='top.restoreSession()'><?php xl('[View Printable Report]','e') ?></a>
282 </td>
284 <td nowrap>
285 <?php
286 print '&nbsp;';
287 $acct_config = $GLOBALS['oer_config']['ws_accounting'];
288 if($acct_config['enabled']) {
289 if($acct_config['enabled'] !== 2) {
290 print '<span class=text><a href="javascript:void window.open(\'' . $acct_config['url_path'] . '\')">' . xl("[SQL-Ledger]") . '</a> &nbsp; </span>';
292 if (acl_check('acct', 'rep')) {
293 print '<span class=text><a href="javascript:void window.open(\'sl_receipts_report.php\')" onclick="top.restoreSession()">' . xl('[Reports]') . '</a> &nbsp; </span>';
295 if (acl_check('acct', 'eob')) {
296 print '<span class=text><a href="javascript:void window.open(\'sl_eob_search.php\')" onclick="top.restoreSession()">' . xl('[EOBs]') . '</a></span>';
300 </td>
302 <td colspan='2' class='text' nowrap>
303 &nbsp;
304 <?php if (! file_exists($EXPORT_INC)) { ?>
305 <!--
306 <a href="javascript:top.restoreSession();document.the_form.mode.value='process';document.the_form.submit()" class="link_submit"
307 title="Process all queued bills to create electronic data (and print if requested)"><?php xl('[Start Batch Processing]','e') ?></a>
308 &nbsp;
310 <a href='../../library/freeb/process_bills.log' target='_blank' class='link_submit'
311 title=<?php xl('See messages from the last set of generated claims','e','\'','\''); ?>><?php xl('[View Log]','e') ?></a>
312 <?php } ?>
313 </td>
315 <td align='right' nowrap>
316 <a href="javascript:select_all()" class="link_submit"><?php xl('[Select All]','e') ?></a>&nbsp;
317 </td>
319 </tr>
320 </table>
322 </form>
324 <form name='update_form' method='post' action='billing_process.php' onsubmit='return top.restoreSession()'>
326 <center>
328 <span class='text'>
329 <?php if (file_exists($EXPORT_INC)) { ?>
330 <input type="submit" class="subbtn" name="bn_external" value="Export Billing" title="<?php xl('Export to external billing system','e') ?>">
331 <input type="submit" class="subbtn" name="bn_mark" value="Mark as Cleared" title="<?php xl('Mark as billed but skip billing','e') ?>">
332 <?php } else { ?>
333 <!--
334 <input type="submit" class="subbtn" name="bn_hcfa_print" value="Queue HCFA &amp; Print" title="<?php xl('Queue for HCFA batch processing and printing','e') ?>">
335 <input type="submit" class="subbtn" name="bn_hcfa" value="Queue HCFA" title="<?php xl('Queue for HCFA batch processing','e')?>">
336 <input type="submit" class="subbtn" name="bn_ub92_print" value="Queue UB92 &amp; Print" title="<?php xl('Queue for UB-92 batch processing and printing','e')?>">
337 <input type="submit" class="subbtn" name="bn_ub92" value="Queue UB92" title="<?php xl('Queue for UB-92 batch processing','e')?>">
339 <input type="submit" class="subbtn" name="bn_x12" value="<?php xl('Generate X12','e')?>"
340 title="<?php xl('Generate and download X12 batch','e')?>"
341 onclick="alert('<?php xl('After saving your batch, click [View Log] to check for errors.','e'); ?>')">
342 <?php if ($GLOBALS['support_encounter_claims']) { ?>
343 <input type="submit" class="subbtn" name="bn_x12_encounter" value="<?php xl('Generate X12 Encounter','e')?>"
344 title="<?php xl('Generate and download X12 encounter claim batch','e')?>"
345 onclick="alert('<?php xl('After saving your batch, click [View Log] to check for errors.','e'); ?>')">
346 <?php } ?>
347 <input type="submit" class="subbtn" name="bn_process_hcfa" value="<?php xl('Generate CMS 1500 PDF','e')?>"
348 title="<?php xl('Generate and download CMS 1500 paper claims','e')?>"
349 onclick="alert('<?php xl('After saving the PDF, click [View Log] to check for errors.','e'); ?>')">
350 <input type="submit" class="subbtn" name="bn_hcfa_txt_file" value="<?php xl('Generate CMS 1500 TEXT','e')?>"
351 title="<?php xl('Making batch text files for uploading to Clearing House and will mark as billed', 'e')?>"
352 onclick="alert('<?php xl('After saving the TEXT file(s), click [View Log] to check for errors.','e'); ?>')">
353 <input type="submit" class="subbtn" name="bn_mark" value="<?php xl('Mark as Cleared','e')?>" title="<?php xl('Post to accounting and mark as billed','e')?>">
354 <input type="submit" class="subbtn" name="bn_reopen" value="<?php xl('Re-Open','e')?>" title="<?php xl('Mark as not billed','e')?>">
355 <!--
356 <input type="submit" class="subbtn" name="bn_electronic_file" value="Make Electronic Batch &amp; Clear" title="<?php xl('Download billing file, post to accounting and mark as billed','e')?>">
358 &nbsp;&nbsp;&nbsp;
359 <?php xl('CMS 1500 Margins','e'); ?>:
360 &nbsp;<?php xl('Left','e'); ?>:
361 <input type='text' size='2' name='left_margin'
362 value='<?php echo $left_margin; ?>'
363 title=<?php xl('HCFA left margin in points','e','\'','\''); ?> />
364 &nbsp;<?php xl('Top','e'); ?>:
365 <input type='text' size='2' name='top_margin'
366 value='<?php echo $top_margin; ?>'
367 title=<?php xl('HCFA top margin in points','e','\'','\''); ?> />
368 </span>
369 <?php } ?>
371 </center>
373 <input type='hidden' name='mode' value="bill" />
374 <input type='hidden' name='authorized' value="<?php echo $my_authorized; ?>" />
375 <input type='hidden' name='unbilled' value="<?php echo $unbilled; ?>" />
376 <input type='hidden' name='code_type' value="%" />
377 <input type='hidden' name='to_date' value="<?php echo $to_date; ?>" />
378 <input type='hidden' name='from_date' value="<?php echo $from_date; ?>" />
380 <?php
381 if ($my_authorized == "on" ) {
382 $my_authorized = "1";
383 } else {
384 $my_authorized = "%";
386 if ($unbilled == "on") {
387 $unbilled = "0";
388 } else {
389 $unbilled = "%";
392 $list = getBillsListBetween($from_date,
393 empty($to_date) ? $from_date : $to_date,
394 $my_authorized,$unbilled,"%");
397 <input type='hidden' name='bill_list' value="<?php echo $list; ?>" />
399 <!-- new form for uploading -->
401 <?php
402 if (!isset($_POST["mode"])) {
403 if (!isset($_POST["from_date"])) {
404 $from_date = date("Y-m-d");
405 } else {
406 $from_date = $_POST["from_date"];
408 if (empty($_POST["to_date"])) {
409 $to_date = '';
410 } else {
411 $to_date = $_POST["to_date"];
413 if (!isset($_POST["code_type"])) {
414 $code_type="all";
415 } else {
416 $code_type = $_POST["code_type"];
418 if (!isset($_POST["unbilled"])) {
419 $unbilled = "on";
420 } else {
421 $unbilled = $_POST["unbilled"];
423 if (!isset($_POST["authorized"])) {
424 $my_authorized = "on";
425 } else {
426 $my_authorized = $_POST["authorized"];
428 } else {
429 $from_date = $_POST["from_date"];
430 $to_date = $_POST["to_date"];
431 $code_type = $_POST["code_type"];
432 $unbilled = $_POST["unbilled"];
433 $my_authorized = $_POST["authorized"];
436 if ($my_authorized == "on" ) {
437 $my_authorized = "1";
438 } else {
439 $my_authorized = "%";
442 if ($unbilled == "on") {
443 $unbilled = "0";
444 } else {
445 $unbilled = "%";
448 if (isset($_POST["mode"]) && $_POST["mode"] == "bill") {
449 billCodesList($list);
454 <table border="0" cellspacing="0" cellpadding="0" width="100%">
456 <?php
457 if ($ret = getBillsBetween($from_date,
458 empty($to_date) ? $from_date : $to_date,
459 $my_authorized, $unbilled, "%"))
461 $loop = 0;
462 $oldcode = "";
463 $last_encounter_id = "";
464 $lhtml = "";
465 $rhtml = "";
466 $lcount = 0;
467 $rcount = 0;
468 $bgcolor = "";
469 $skipping = FALSE;
471 foreach ($ret as $iter) {
473 // We include encounters here that have never been billed. However
474 // if it had no selected billing items but does have non-selected
475 // billing items, then it is not of interest.
476 if (!$iter['id']) {
477 $res = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
478 "encounter = '" . $iter['enc_encounter'] . "' AND " .
479 "pid='" . $iter['enc_pid'] . "' AND " .
480 "activity = 1");
481 if ($res['count'] > 0) continue;
484 $this_encounter_id = $iter['enc_pid'] . "-" . $iter['enc_encounter'];
486 if ($last_encounter_id != $this_encounter_id) {
487 if ($lhtml) {
488 while ($rcount < $lcount) {
489 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'>&nbsp;</td></tr>";
490 ++$rcount;
492 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
493 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n\n";
496 $lhtml = "";
497 $rhtml = "";
499 // If there are ANY unauthorized items in this encounter and this is
500 // the normal case of viewing only authorized billing, then skip the
501 // entire encounter.
503 $skipping = FALSE;
504 if ($my_authorized == '1') {
505 $res = sqlQuery("select count(*) as count from billing where " .
506 "encounter = '" . $iter['enc_encounter'] . "' and " .
507 "pid='" . $iter['enc_pid'] . "' and " .
508 "activity = 1 and authorized = 0");
509 if ($res['count'] > 0) {
510 $skipping = TRUE;
511 $last_encounter_id = $this_encounter_id;
512 continue;
516 $name = getPatientData($iter['enc_pid'], "fname, mname, lname, pubpid");
518 # Check if patient has primary insurance and a subscriber exists for it.
519 # If not we will highlight their name in red.
520 # TBD: more checking here.
522 $res = sqlQuery("select count(*) as count from insurance_data where " .
523 "pid = " . $iter['enc_pid'] . " and " .
524 "type='primary' and " .
525 "subscriber_lname is not null and " .
526 "subscriber_lname != '' limit 1");
527 $namecolor = ($res['count'] > 0) ? "black" : "#ff7777";
529 ++$encount;
530 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
531 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
532 $lcount = 1;
533 $rcount = 0;
534 $oldcode = "";
536 $ptname = $name['fname'] . " " . $name['lname'];
537 $raw_encounter_date = date("Y-m-d", strtotime($iter['enc_date']));
539 // Add Encounter Date to display with "To Encounter" button 2/17/09 JCH
540 $lhtml .= "&nbsp;<span class=bold><font color='$namecolor'>$ptname" .
541 "</font></span><span class=small>&nbsp;(" . $iter['enc_pid'] . "-" .
542 $iter['enc_encounter'] . ")</span>";
544 // Not sure why the next section seems to do nothing except post "To Encounter" button 2/17/09 JCH
545 $lhtml .= "&nbsp;&nbsp;&nbsp;<a class=\"link_submit\" " .
546 "href=\"javascript:window.toencounter(" . $iter['enc_pid'] .
547 ",'" . addslashes($name['pubpid']) .
548 "','" . addslashes($ptname) . "'," . $iter['enc_encounter'] .
549 ",'$raw_encounter_date')\">[" . xl('To Enctr') . " $raw_encounter_date]</a>";
551 // Changed "To xxx" buttons to allow room for encounter date display 2/17/09 JCH
552 $lhtml .= "&nbsp;&nbsp;&nbsp;<a class=\"link_submit\" " .
553 "href=\"javascript:window.topatient(" . $iter['enc_pid'] .
554 ")\">[" . xl('To Dems') . "]</a>";
556 if ($iter['id']) {
558 $lcount += 2;
559 $lhtml .= "<br />\n";
560 $lhtml .= "&nbsp;<span class=text>Bill: ";
561 $lhtml .= "<select name='claims[" . $this_encounter_id . "][payer]' style='background-color:$bgcolor'>";
563 $query = "SELECT id.provider AS id, id.type, id.date, " .
564 "ic.x12_default_partner_id AS ic_x12id, ic.name AS provider " .
565 "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
566 "ic.id = id.provider AND " .
567 "id.pid = '" . mysql_escape_string($iter['enc_pid']) . "' AND " .
568 "id.date <= '$raw_encounter_date' " .
569 "ORDER BY id.type ASC, id.date DESC";
571 $result = sqlStatement($query);
572 $count = 0;
573 $default_x12_partner = $iter['ic_x12id'];
574 $prevtype = '';
576 while ($row = mysql_fetch_array($result)) {
577 if (strcmp($row['type'], $prevtype) == 0) continue;
578 $prevtype = $row['type'];
579 if (strlen($row['provider']) > 0) {
580 // This preserves any existing insurance company selection, which is
581 // important when EOB posting has re-queued for secondary billing.
582 $lhtml .= "<option value=\"" . strtoupper(substr($row['type'],0,1)) . $row['id'] . "\"";
583 if (($count == 0 && !$iter['payer_id']) || $row['id'] == $iter['payer_id']) {
584 $lhtml .= " selected";
585 if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
587 $lhtml .= ">" . $row['type'] . ": " . $row['provider'] . "</option>";
589 $count++;
592 $lhtml .= "<option value='-1'>Unassigned</option>\n";
593 $lhtml .= "</select>&nbsp;&nbsp;\n";
594 $lhtml .= "<select name='claims[" . $this_encounter_id . "][partner]' style='background-color:$bgcolor'>";
595 $x = new X12Partner();
596 $partners = $x->_utility_array($x->x12_partner_factory());
597 foreach ($partners as $xid => $xname) {
598 $lhtml .= '<option label="' . $xname . '" value="' . $xid .'"';
599 if ($xid == $default_x12_partner) {
600 $lhtml .= "selected";
602 $lhtml .= '>' . $xname . '</option>';
604 $lhtml .= "</select>";
605 $lhtml .= "<br>\n&nbsp;" . substr($iter['date'], 0, 16) . " " .
606 xl("Encounter was coded");
608 $query = "SELECT * FROM claims WHERE " .
609 "patient_id = '" . $iter['enc_pid'] . "' AND " .
610 "encounter_id = '" . $iter['enc_encounter'] . "' " .
611 "ORDER BY version";
612 $cres = sqlStatement($query);
614 $lastcrow = false;
616 while ($crow = sqlFetchArray($cres)) {
617 $query = "SELECT id.type, ic.name " .
618 "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
619 "id.pid = '" . $iter['enc_pid'] . "' AND " .
620 "id.provider = '" . $crow['payer_id'] . "' AND " .
621 "id.date <= '$raw_encounter_date' AND " .
622 "ic.id = id.provider " .
623 "ORDER BY id.type ASC, id.date DESC";
625 $irow= sqlQuery($query);
627 if ($crow['bill_process']) {
628 $lhtml .= "<br>\n&nbsp;" . substr($crow['bill_time'], 0, 16) . " " .
629 xl("Queued for") . " {$irow['type']} {$crow['target']} " .
630 xl("billing to ") . $irow['name'];
631 ++$lcount;
633 else if ($crow['status'] > 1) {
634 $lhtml .= "<br>\n&nbsp;" . substr($crow['bill_time'], 0, 16) . " " .
635 xl("Marked as cleared");
636 ++$lcount;
638 else {
639 $lhtml .= "<br>\n&nbsp;" . substr($crow['bill_time'], 0, 16) . " " .
640 xl("Re-opened");
641 ++$lcount;
644 if ($crow['process_time']) {
645 $lhtml .= "<br>\n&nbsp;" . substr($crow['process_time'], 0, 16) . " " .
646 xl("Claim was generated to file ") .
647 "<a href='get_claim_file.php?key=" . $crow['process_file'] .
648 "' onclick='top.restoreSession()'>" .
649 $crow['process_file'] . "</a>";
650 ++$lcount;
653 $lastcrow = $crow;
654 } // end while ($crow = sqlFetchArray($cres))
656 if ($lastcrow && $lastcrow['status'] == 4) {
657 $lhtml .= "<br>\n&nbsp;This claim has been closed.";
658 ++$lcount;
661 if ($lastcrow && $lastcrow['status'] == 5) {
662 $lhtml .= "<br>\n&nbsp;This claim has been canceled.";
663 ++$lcount;
665 } // end if ($iter['id'])
667 } // end if ($last_encounter_id != $this_encounter_id)
669 if ($skipping) continue;
671 ++$rcount;
673 if ($rhtml) {
674 $rhtml .= "<tr bgcolor='$bgcolor'>\n";
676 $rhtml .= "<td width='50'>";
677 if ($iter['id'] && $oldcode != $iter['code_type']) {
678 $rhtml .= "<span class=text>" . $iter['code_type'] . ": </span>";
681 $oldcode = $iter['code_type'];
682 $rhtml .= "</td>\n";
683 $justify = "";
685 if ($iter['id'] && $code_types[$iter['code_type']]['just']) {
686 $js = split(":",$iter['justify']);
687 $counter = 0;
688 foreach ($js as $j) {
689 if(!empty($j)) {
690 if ($counter == 0) {
691 $justify .= " (<b>$j</b>)";
693 else {
694 $justify .= " ($j)";
696 $counter++;
701 $rhtml .= "<td><span class=text>" . $iter{"code"}. "</span>" . '<span style="font-size:8pt;">' . $justify . "</span></td>\n";
702 $rhtml .= '<td align="right"><span style="font-size:8pt;">&nbsp;&nbsp;&nbsp;';
703 if ($iter['id'] && $iter['fee'] > 0) {
704 $rhtml .= '$' . $iter['fee'];
706 $rhtml .= "</span></td>\n";
707 $rhtml .= '<td><span style="font-size:8pt;">&nbsp;&nbsp;&nbsp;';
708 if ($iter['id']) $rhtml .= getProviderName(empty($iter['provider_id']) ? $iter['enc_provider_id'] : $iter['provider_id']);
709 $rhtml .= "</span></td>\n";
710 $rhtml .= '<td width=100>&nbsp;&nbsp;&nbsp;<span style="font-size:8pt;">';
711 if ($iter['id']) $rhtml .= date("Y-m-d",strtotime($iter{"date"}));
712 $rhtml .= "</span></td>\n";
713 if ($iter['id'] && $iter['authorized'] != 1) {
714 $rhtml .= "<td><span class=alert>".xl("Note: This code was not entered by an authorized user. Only authorized codes may be uploaded to the Open Medical Billing Network for processing. If you wish to upload these codes, please select an authorized user here.")."</span></td>\n";
716 else {
717 $rhtml .= "<td></td>\n";
719 if ($iter['id'] && $last_encounter_id != $this_encounter_id) {
720 $tmpbpr = $iter['bill_process'];
721 if ($tmpbpr == '0' && $iter['billed']) $tmpbpr = '2';
722 $rhtml .= "<td><input type='checkbox' value='$tmpbpr' name='claims[" . $this_encounter_id . "][bill]' onclick='set_button_states()'>&nbsp;</td>\n";
724 else {
725 $rhtml .= "<td></td>\n";
727 $rhtml .= "</tr>\n";
728 $last_encounter_id = $this_encounter_id;
730 } // end foreach
732 if ($lhtml) {
733 while ($rcount < $lcount) {
734 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'>&nbsp;</td></tr>";
735 ++$rcount;
737 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
738 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
745 </table>
746 </form>
748 <script>
749 set_button_states();
750 <?php
751 if ($alertmsg) {
752 echo "alert('$alertmsg');\n";
755 </script>
757 </body>
758 </html>