Merge branch 'master' of git://github.com/openemr/openemr
[openemr.git] / interface / billing / billing_report.php
blobccadf142eadd1400f6b60c1234cc3ed013005db8
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 require_once("../globals.php");
8 require_once("../../library/acl.inc");
9 require_once("../../custom/code_types.inc.php");
10 require_once("$srcdir/patient.inc");
11 require_once("$srcdir/billrep.inc");
12 require_once(dirname(__FILE__) . "/../../library/classes/OFX.class.php");
13 require_once(dirname(__FILE__) . "/../../library/classes/X12Partner.class.php");
14 require_once("$srcdir/formatting.inc.php");
16 $EXPORT_INC = "$webserver_root/custom/BillingExport.php";
18 $alertmsg = '';
20 if ($_POST['mode'] == 'export') {
21 $sdate = $_POST['from_date'];
22 $edate = $_POST['to_date'];
24 $sql = "SELECT billing.*, concat(pd.fname, ' ', pd.lname) as name from billing "
25 . "join patient_data as pd on pd.pid = billing.pid where billed = '1' and "
26 . "(process_date > '" . mysql_real_escape_string($sdate)
27 . "' or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($sdate) ."') "
28 . "and (process_date < '" . mysql_real_escape_string($edate)
29 . "'or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($edate) ."') "
30 . "order by pid,encounter";
31 $db = get_db();
32 $results = $db->Execute($sql);
33 $billings = array();
34 if ($results->RecordCount() == 0) {
35 echo xl("No Bills Found to Include in OFX Export<br>");
37 else {
38 while(!$results->EOF) {
39 $billings[] = $results->fields;
40 $results->MoveNext();
42 $ofx = new OFX($billings);
43 header("Pragma: public");
44 header("Expires: 0");
45 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
46 header("Content-Disposition: attachment; filename=openemr_ofx.ofx");
47 header("Content-Type: text/xml");
48 echo $ofx->get_OFX();
49 exit;
53 // This is obsolete.
54 if ($_POST['mode'] == 'process') {
55 if (exec("ps x | grep 'process_bills[.]php'")) {
56 $alertmsg = xl('Request ignored - claims processing is already running!');
58 else {
59 exec("cd $webserver_root/library/freeb;" .
60 "php -q process_bills.php bill > process_bills.log 2>&1 &");
61 $alertmsg = xl('Batch processing initiated; this may take a while.');
65 //global variables:
66 if (!isset($_POST["mode"])) {
67 $from_date = isset($_POST['from_date']) ? $_POST['from_date'] : date('Y-m-d');
68 $to_date = isset($_POST['to_date' ]) ? $_POST['to_date' ] : '';
69 $code_type = isset($_POST['code_type']) ? $_POST['code_type'] : 'all';
70 $unbilled = isset($_POST['unbilled' ]) ? $_POST['unbilled' ] : 'on';
71 $my_authorized = $_POST["authorized"];
72 } else {
73 $from_date = $_POST["from_date"];
74 $to_date = $_POST["to_date"];
75 $code_type = $_POST["code_type"];
76 $unbilled = $_POST["unbilled"];
77 $my_authorized = $_POST["authorized"];
80 // This tells us if only encounters that appear to be missing a "25" modifier
81 // are to be reported.
82 $missing_mods_only = !empty($_POST['missing_mods_only']);
85 $from_date = isset($_POST['from_date']) ? $_POST['from_date'] : date('Y-m-d');
86 $to_date = empty($_POST['to_date' ]) ? $from_date : $_POST['to_date'];
87 $code_type = isset($_POST['code_type']) ? $_POST['code_type'] : 'all';
88 $unbilled = isset($_POST['unbilled' ]) ? $_POST['unbilled' ] : 'on';
89 $my_authorized = $_POST["authorized"];
92 $left_margin = isset($_POST["left_margin"]) ? $_POST["left_margin"] : 24;
93 $top_margin = isset($_POST["top_margin"] ) ? $_POST["top_margin" ] : 20;
95 $ofrom_date = $from_date;
96 $oto_date = $to_date;
97 $ocode_type = $code_type;
98 $ounbilled = $unbilled;
99 $oauthorized = $my_authorized;
102 <html>
103 <head>
104 <?php if (function_exists(html_header_show)) html_header_show(); ?>
105 <link rel="stylesheet" href="<?php echo $css_header; ?>" type="text/css">
106 <style>
107 .subbtn { margin-top:3px; margin-bottom:3px; margin-left:2px; margin-right:2px }
108 </style>
109 <script>
111 function select_all() {
112 for($i=0;$i < document.update_form.length;$i++) {
113 $name = document.update_form[$i].name;
114 if ($name.substring(0,7) == "claims[" && $name.substring($name.length -6) == "[bill]") {
115 document.update_form[$i].checked = true;
118 set_button_states();
121 function set_button_states() {
122 var f = document.update_form;
123 var count0 = 0; // selected and not billed or queued
124 var count1 = 0; // selected and queued
125 var count2 = 0; // selected and billed
126 for($i = 0; $i < f.length; ++$i) {
127 $name = f[$i].name;
128 if ($name.substring(0, 7) == "claims[" && $name.substring($name.length -6) == "[bill]" && f[$i].checked == true) {
129 if (f[$i].value == '0') ++count0;
130 else if (f[$i].value == '1' || f[$i].value == '5') ++count1;
131 else ++count2;
135 var can_generate = (count0 > 0 || count1 > 0 || count2 > 0);
136 var can_mark = (count1 > 0 || count0 > 0 || count2 > 0);
137 var can_bill = (count0 == 0 && count1 == 0 && count2 > 0);
139 <?php if (file_exists($EXPORT_INC)) { ?>
140 f.bn_external.disabled = !can_generate;
141 <?php } else { ?>
142 // f.bn_hcfa_print.disabled = !can_generate;
143 // f.bn_hcfa.disabled = !can_generate;
144 // f.bn_ub92_print.disabled = !can_generate;
145 // f.bn_ub92.disabled = !can_generate;
146 f.bn_x12.disabled = !can_generate;
147 <?php if ($GLOBALS['support_encounter_claims']) { ?>
148 f.bn_x12_encounter.disabled = !can_generate;
149 <?php } ?>
150 f.bn_process_hcfa.disabled = !can_generate;
151 f.bn_hcfa_txt_file.disabled = !can_generate;
152 // f.bn_electronic_file.disabled = !can_bill;
153 f.bn_reopen.disabled = !can_bill;
154 <?php } ?>
155 f.bn_mark.disabled = !can_mark;
158 // Process a click to go to an encounter.
159 function toencounter(pid, pubpid, pname, enc, datestr, dobstr) {
160 top.restoreSession();
161 <?php if ($GLOBALS['concurrent_layout']) { ?>
162 var othername = (window.name == 'RTop') ? 'RBot' : 'RTop';
163 parent.left_nav.setPatient(pname,pid,pubpid,'',dobstr);
164 parent.left_nav.setEncounter(datestr, enc, othername);
165 parent.left_nav.setRadio(othername, 'enc');
166 parent.frames[othername].location.href =
167 '../patient_file/encounter/encounter_top.php?set_encounter='
168 + enc + '&pid=' + pid;
169 <?php } else { ?>
170 location.href = '../patient_file/encounter/patient_encounter.php?set_encounter='
171 + enc + '&pid=' + pid;
172 <?php } ?>
175 function topatient(pid) {
176 top.restoreSession();
177 <?php if ($GLOBALS['concurrent_layout']) { ?>
178 var othername = (window.name == 'RTop') ? 'RBot' : 'RTop';
179 parent.frames[othername].location.href =
180 '../patient_file/summary/demographics_full.php?pid=' + pid;
181 <?php } else { ?>
182 location.href = '../patient_file/summary/demographics_full.php?pid=' + pid;
183 <?php } ?>
186 </script>
187 </head>
188 <body class="body_top">
190 <p style='margin-top:5px;margin-bottom:5px;margin-left:5px'>
192 <?php if ($GLOBALS['concurrent_layout']) { ?>
193 <font class='title'><?php xl('Billing Report','e') ?></font>
194 <?php } else if ($userauthorized) { ?>
195 <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>
196 <?php } else { ?>
197 <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>
198 <?php } ?>
200 </p>
202 <form name='the_form' method='post' action='billing_report.php' onsubmit='return top.restoreSession()'>
204 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
205 <script type="text/javascript" src="../../library/dialog.js"></script>
206 <script type="text/javascript" src="../../library/textformat.js"></script>
207 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
208 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
209 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
210 <script language='JavaScript'>
211 var mypcc = '1';
212 </script>
214 <input type='hidden' name='mode' value='change'>
216 <table width='100%' border="1" cellspacing="0" cellpadding="0">
217 <tr>
218 <td nowrap>
219 &nbsp;<span class='text'><?php xl('From: ','e') ?></span>
220 <input type='text' size='10' name='from_date' id='from_date'
221 value='<?php echo $from_date; ?>'
222 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
223 title=<?php xl('yyyy-mm-dd last date of this event','e','\'','\''); ?> />
224 <img src='../../interface/pic/show_calendar.gif' align='absbottom' width='24' height='22'
225 id='img_fromdate' border='0' alt='[?]' style='cursor:pointer'
226 title=<?php xl('Click here to choose a date','e','\'','\''); ?> />
227 <script>
228 Calendar.setup({inputField:"from_date", ifFormat:"%Y-%m-%d", button:"img_fromdate"});
229 </script>
230 </td>
232 <td nowrap>
233 &nbsp;<span class='text'><?php xl('To: ','e') ?></span>
234 <input type='text' size='10' name='to_date' id='to_date'
235 value='<?php echo $to_date; ?>'
236 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
237 title=<?php xl('yyyy-mm-dd last date of this event','e','\'','\''); ?> />
238 <img src='../../interface/pic/show_calendar.gif' align='absbottom' width='24' height='22'
239 id='img_todate' border='0' alt='[?]' style='cursor:pointer'
240 title=<?php xl('Click here to choose a date','e','\'','\''); ?> />
241 <script>
242 Calendar.setup({inputField:"to_date", ifFormat:"%Y-%m-%d", button:"img_todate"});
243 </script>
244 <input type="hidden" name="code_type" value="%" />
245 </td>
247 <td nowrap>
248 <input type='checkbox' name='unbilled' <?php if ($unbilled == "on") {echo "checked";}; ?> />
249 <span class='text'><?php xl('Unbilled Only','e') ?></span>
250 &nbsp;
251 <input type='checkbox' name='authorized' <?php if ($my_authorized == "on") {echo "checked";}; ?> />
252 <span class='text'><?php xl('Authorized Only','e') ?></span>
253 <?php if (!empty($GLOBALS['missing_mods_option'])) { ?>
254 &nbsp;
255 <input type='checkbox' name='missing_mods_only' <?php if ($missing_mods_only) echo "checked"; ?> />
256 <span class='text'><?php xl('Missing Mods Only','e') ?></span>
257 <?php } ?>
258 </td>
260 <td align='right' width='10%' nowrap>
261 &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>
263 <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;
264 </td>
266 </tr>
268 <tr>
270 <td nowrap>
271 &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); ?>"
272 class='link_submit' target='new' onclick='top.restoreSession()'><?php xl('[View Printable Report]','e') ?></a>
273 </td>
275 <td nowrap>
276 <?php
277 print '&nbsp;';
278 $acct_config = $GLOBALS['oer_config']['ws_accounting'];
279 if($acct_config['enabled']) {
280 if($acct_config['enabled'] !== 2) {
281 print '<span class=text><a href="javascript:void window.open(\'' . $acct_config['url_path'] . '\')">' . xl("[SQL-Ledger]") . '</a> &nbsp; </span>';
283 if (acl_check('acct', 'rep')) {
284 print '<span class=text><a href="javascript:void window.open(\'sl_receipts_report.php\')" onclick="top.restoreSession()">' . xl('[Reports]') . '</a> &nbsp; </span>';
286 if (acl_check('acct', 'eob')) {
287 print '<span class=text><a href="javascript:void window.open(\'sl_eob_search.php\')" onclick="top.restoreSession()">' . xl('[EOBs]') . '</a></span>';
291 </td>
293 <td class='text' nowrap>
294 &nbsp;
295 <?php if (! file_exists($EXPORT_INC)) { ?>
296 <!--
297 <a href="javascript:top.restoreSession();document.the_form.mode.value='process';document.the_form.submit()" class="link_submit"
298 title="Process all queued bills to create electronic data (and print if requested)"><?php xl('[Start Batch Processing]','e') ?></a>
299 &nbsp;
301 <a href='../../library/freeb/process_bills.log' target='_blank' class='link_submit'
302 title=<?php xl('See messages from the last set of generated claims','e','\'','\''); ?>><?php xl('[View Log]','e') ?></a>
303 <?php } ?>
304 </td>
306 <td align='right' nowrap>
307 <a href="javascript:select_all()" class="link_submit"><?php xl('[Select All]','e') ?></a>&nbsp;
308 </td>
310 </tr>
311 </table>
313 </form>
315 <form name='update_form' method='post' action='billing_process.php' onsubmit='return top.restoreSession()'>
317 <center>
319 <span class='text'>
320 <?php if (file_exists($EXPORT_INC)) { ?>
321 <input type="submit" class="subbtn" name="bn_external" value="Export Billing" title="<?php xl('Export to external billing system','e') ?>">
322 <input type="submit" class="subbtn" name="bn_mark" value="Mark as Cleared" title="<?php xl('Mark as billed but skip billing','e') ?>">
323 <?php } else { ?>
324 <!--
325 <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') ?>">
326 <input type="submit" class="subbtn" name="bn_hcfa" value="Queue HCFA" title="<?php xl('Queue for HCFA batch processing','e')?>">
327 <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')?>">
328 <input type="submit" class="subbtn" name="bn_ub92" value="Queue UB92" title="<?php xl('Queue for UB-92 batch processing','e')?>">
330 <input type="submit" class="subbtn" name="bn_x12" value="<?php xl('Generate X12','e')?>"
331 title="<?php xl('Generate and download X12 batch','e')?>"
332 onclick="alert('<?php xl('After saving your batch, click [View Log] to check for errors.','e'); ?>')">
333 <?php if ($GLOBALS['support_encounter_claims']) { ?>
334 <input type="submit" class="subbtn" name="bn_x12_encounter" value="<?php xl('Generate X12 Encounter','e')?>"
335 title="<?php xl('Generate and download X12 encounter claim batch','e')?>"
336 onclick="alert('<?php xl('After saving your batch, click [View Log] to check for errors.','e'); ?>')">
337 <?php } ?>
338 <input type="submit" class="subbtn" name="bn_process_hcfa" value="<?php xl('Generate CMS 1500 PDF','e')?>"
339 title="<?php xl('Generate and download CMS 1500 paper claims','e')?>"
340 onclick="alert('<?php xl('After saving the PDF, click [View Log] to check for errors.','e'); ?>')">
341 <input type="submit" class="subbtn" name="bn_hcfa_txt_file" value="<?php xl('Generate CMS 1500 TEXT','e')?>"
342 title="<?php xl('Making batch text files for uploading to Clearing House and will mark as billed', 'e')?>"
343 onclick="alert('<?php xl('After saving the TEXT file(s), click [View Log] to check for errors.','e'); ?>')">
344 <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')?>">
345 <input type="submit" class="subbtn" name="bn_reopen" value="<?php xl('Re-Open','e')?>" title="<?php xl('Mark as not billed','e')?>">
346 <!--
347 <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')?>">
349 &nbsp;&nbsp;&nbsp;
350 <?php xl('CMS 1500 Margins','e'); ?>:
351 &nbsp;<?php xl('Left','e'); ?>:
352 <input type='text' size='2' name='left_margin'
353 value='<?php echo $left_margin; ?>'
354 title=<?php xl('HCFA left margin in points','e','\'','\''); ?> />
355 &nbsp;<?php xl('Top','e'); ?>:
356 <input type='text' size='2' name='top_margin'
357 value='<?php echo $top_margin; ?>'
358 title=<?php xl('HCFA top margin in points','e','\'','\''); ?> />
359 </span>
360 <?php } ?>
362 </center>
364 <input type='hidden' name='mode' value="bill" />
365 <input type='hidden' name='authorized' value="<?php echo $my_authorized; ?>" />
366 <input type='hidden' name='unbilled' value="<?php echo $unbilled; ?>" />
367 <input type='hidden' name='code_type' value="%" />
368 <input type='hidden' name='to_date' value="<?php echo $to_date; ?>" />
369 <input type='hidden' name='from_date' value="<?php echo $from_date; ?>" />
371 <?php
372 if ($my_authorized == "on" ) {
373 $my_authorized = "1";
374 } else {
375 $my_authorized = "%";
377 if ($unbilled == "on") {
378 $unbilled = "0";
379 } else {
380 $unbilled = "%";
383 $list = getBillsListBetween($from_date,
384 empty($to_date) ? $from_date : $to_date,
385 $my_authorized,$unbilled,"%");
388 <input type='hidden' name='bill_list' value="<?php echo $list; ?>" />
390 <!-- new form for uploading -->
392 <?php
393 if (!isset($_POST["mode"])) {
394 if (!isset($_POST["from_date"])) {
395 $from_date = date("Y-m-d");
396 } else {
397 $from_date = $_POST["from_date"];
399 if (empty($_POST["to_date"])) {
400 $to_date = '';
401 } else {
402 $to_date = $_POST["to_date"];
404 if (!isset($_POST["code_type"])) {
405 $code_type="all";
406 } else {
407 $code_type = $_POST["code_type"];
409 if (!isset($_POST["unbilled"])) {
410 $unbilled = "on";
411 } else {
412 $unbilled = $_POST["unbilled"];
414 if (!isset($_POST["authorized"])) {
415 $my_authorized = "on";
416 } else {
417 $my_authorized = $_POST["authorized"];
419 } else {
420 $from_date = $_POST["from_date"];
421 $to_date = $_POST["to_date"];
422 $code_type = $_POST["code_type"];
423 $unbilled = $_POST["unbilled"];
424 $my_authorized = $_POST["authorized"];
427 if ($my_authorized == "on" ) {
428 $my_authorized = "1";
429 } else {
430 $my_authorized = "%";
433 if ($unbilled == "on") {
434 $unbilled = "0";
435 } else {
436 $unbilled = "%";
439 if (isset($_POST["mode"]) && $_POST["mode"] == "bill") {
440 billCodesList($list);
445 <table border="0" cellspacing="0" cellpadding="0" width="100%">
447 <?php
448 if ($ret = getBillsBetween($from_date,
449 empty($to_date) ? $from_date : $to_date,
450 $my_authorized, $unbilled, "%"))
452 $loop = 0;
453 $oldcode = "";
454 $last_encounter_id = "";
455 $lhtml = "";
456 $rhtml = "";
457 $lcount = 0;
458 $rcount = 0;
459 $bgcolor = "";
460 $skipping = FALSE;
462 $mmo_empty_mod = false;
463 $mmo_num_charges = 0;
465 foreach ($ret as $iter) {
467 // We include encounters here that have never been billed. However
468 // if it had no selected billing items but does have non-selected
469 // billing items, then it is not of interest.
470 if (!$iter['id']) {
471 $res = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
472 "encounter = '" . $iter['enc_encounter'] . "' AND " .
473 "pid='" . $iter['enc_pid'] . "' AND " .
474 "activity = 1");
475 if ($res['count'] > 0) continue;
478 $this_encounter_id = $iter['enc_pid'] . "-" . $iter['enc_encounter'];
480 if ($last_encounter_id != $this_encounter_id) {
482 // This dumps all HTML for the previous encounter.
484 if ($lhtml) {
485 while ($rcount < $lcount) {
486 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'>&nbsp;</td></tr>";
487 ++$rcount;
489 // This test handles the case where we are only listing encounters
490 // that appear to have a missing "25" modifier.
491 if (!$missing_mods_only || ($mmo_empty_mod && $mmo_num_charges > 1)) {
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";
494 ++$encount;
498 $lhtml = "";
499 $rhtml = "";
500 $mmo_empty_mod = false;
501 $mmo_num_charges = 0;
503 // If there are ANY unauthorized items in this encounter and this is
504 // the normal case of viewing only authorized billing, then skip the
505 // entire encounter.
507 $skipping = FALSE;
508 if ($my_authorized == '1') {
509 $res = sqlQuery("select count(*) as count from billing where " .
510 "encounter = '" . $iter['enc_encounter'] . "' and " .
511 "pid='" . $iter['enc_pid'] . "' and " .
512 "activity = 1 and authorized = 0");
513 if ($res['count'] > 0) {
514 $skipping = TRUE;
515 $last_encounter_id = $this_encounter_id;
516 continue;
520 $name = getPatientData($iter['enc_pid'], "fname, mname, lname, pubpid, DATE_FORMAT(DOB,'%Y-%m-%d') as DOB_YMD");
522 # Check if patient has primary insurance and a subscriber exists for it.
523 # If not we will highlight their name in red.
524 # TBD: more checking here.
526 $res = sqlQuery("select count(*) as count from insurance_data where " .
527 "pid = " . $iter['enc_pid'] . " and " .
528 "type='primary' and " .
529 "subscriber_lname is not null and " .
530 "subscriber_lname != '' limit 1");
531 $namecolor = ($res['count'] > 0) ? "black" : "#ff7777";
533 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
534 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
535 $lcount = 1;
536 $rcount = 0;
537 $oldcode = "";
539 $ptname = $name['fname'] . " " . $name['lname'];
540 $raw_encounter_date = date("Y-m-d", strtotime($iter['enc_date']));
542 // Add Encounter Date to display with "To Encounter" button 2/17/09 JCH
543 $lhtml .= "&nbsp;<span class=bold><font color='$namecolor'>$ptname" .
544 "</font></span><span class=small>&nbsp;(" . $iter['enc_pid'] . "-" .
545 $iter['enc_encounter'] . ")</span>";
547 // Not sure why the next section seems to do nothing except post "To Encounter" button 2/17/09 JCH
548 $lhtml .= "&nbsp;&nbsp;&nbsp;<a class=\"link_submit\" " .
549 "href=\"javascript:window.toencounter(" . $iter['enc_pid'] .
550 ",'" . addslashes($name['pubpid']) .
551 "','" . addslashes($ptname) . "'," . $iter['enc_encounter'] .
552 ",'" . oeFormatShortDate($raw_encounter_date) . "',' " .
553 xl('DOB') . ": " . oeFormatShortDate($name['DOB_YMD']) . " " . xl('Age') . ": " . getPatientAge($name['DOB_YMD']) . "')\">[" .
554 xl('To Enctr') . " " . oeFormatShortDate($raw_encounter_date) . "]</a>";
556 // Changed "To xxx" buttons to allow room for encounter date display 2/17/09 JCH
557 $lhtml .= "&nbsp;&nbsp;&nbsp;<a class=\"link_submit\" " .
558 "href=\"javascript:window.topatient(" . $iter['enc_pid'] .
559 ")\">[" . xl('To Dems') . "]</a>";
561 if ($iter['id']) {
563 $lcount += 2;
564 $lhtml .= "<br />\n";
565 $lhtml .= "&nbsp;<span class=text>Bill: ";
566 $lhtml .= "<select name='claims[" . $this_encounter_id . "][payer]' style='background-color:$bgcolor'>";
568 $query = "SELECT id.provider AS id, id.type, id.date, " .
569 "ic.x12_default_partner_id AS ic_x12id, ic.name AS provider " .
570 "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
571 "ic.id = id.provider AND " .
572 "id.pid = '" . mysql_escape_string($iter['enc_pid']) . "' AND " .
573 "id.date <= '$raw_encounter_date' " .
574 "ORDER BY id.type ASC, id.date DESC";
576 $result = sqlStatement($query);
577 $count = 0;
578 $default_x12_partner = $iter['ic_x12id'];
579 $prevtype = '';
581 while ($row = mysql_fetch_array($result)) {
582 if (strcmp($row['type'], $prevtype) == 0) continue;
583 $prevtype = $row['type'];
584 if (strlen($row['provider']) > 0) {
585 // This preserves any existing insurance company selection, which is
586 // important when EOB posting has re-queued for secondary billing.
587 $lhtml .= "<option value=\"" . strtoupper(substr($row['type'],0,1)) . $row['id'] . "\"";
588 if (($count == 0 && !$iter['payer_id']) || $row['id'] == $iter['payer_id']) {
589 $lhtml .= " selected";
590 if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
592 $lhtml .= ">" . $row['type'] . ": " . $row['provider'] . "</option>";
594 $count++;
597 $lhtml .= "<option value='-1'>Unassigned</option>\n";
598 $lhtml .= "</select>&nbsp;&nbsp;\n";
599 $lhtml .= "<select name='claims[" . $this_encounter_id . "][partner]' style='background-color:$bgcolor'>";
600 $x = new X12Partner();
601 $partners = $x->_utility_array($x->x12_partner_factory());
602 foreach ($partners as $xid => $xname) {
603 $lhtml .= '<option label="' . $xname . '" value="' . $xid .'"';
604 if ($xid == $default_x12_partner) {
605 $lhtml .= "selected";
607 $lhtml .= '>' . $xname . '</option>';
609 $lhtml .= "</select>";
610 $lhtml .= "<br>\n&nbsp;" . oeFormatShortDate(substr($iter['date'], 0, 10))
611 . substr($iter['date'], 10, 6) . " " . xl("Encounter was coded");
613 $query = "SELECT * FROM claims WHERE " .
614 "patient_id = '" . $iter['enc_pid'] . "' AND " .
615 "encounter_id = '" . $iter['enc_encounter'] . "' " .
616 "ORDER BY version";
617 $cres = sqlStatement($query);
619 $lastcrow = false;
621 while ($crow = sqlFetchArray($cres)) {
622 $query = "SELECT id.type, ic.name " .
623 "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
624 "id.pid = '" . $iter['enc_pid'] . "' AND " .
625 "id.provider = '" . $crow['payer_id'] . "' AND " .
626 "id.date <= '$raw_encounter_date' AND " .
627 "ic.id = id.provider " .
628 "ORDER BY id.type ASC, id.date DESC";
630 $irow= sqlQuery($query);
632 if ($crow['bill_process']) {
633 $lhtml .= "<br>\n&nbsp;" .
634 oeFormatShortDate(substr($crow['bill_time'], 0, 10)) .
635 substr($crow['bill_time'], 10, 6) . " " .
636 xl("Queued for") . " {$irow['type']} {$crow['target']} " .
637 xl("billing to ") . $irow['name'];
638 ++$lcount;
640 else if ($crow['status'] > 1) {
641 $lhtml .= "<br>\n&nbsp;" .
642 oeFormatShortDate(substr($crow['bill_time'], 0, 10)) .
643 substr($crow['bill_time'], 10, 6) . " " .
644 xl("Marked as cleared");
645 ++$lcount;
647 else {
648 $lhtml .= "<br>\n&nbsp;" .
649 oeFormatShortDate(substr($crow['bill_time'], 0, 10)) .
650 substr($crow['bill_time'], 10, 6) . " " .
651 xl("Re-opened");
652 ++$lcount;
655 if ($crow['process_time']) {
656 $lhtml .= "<br>\n&nbsp;" .
657 oeFormatShortDate(substr($crow['process_time'], 0, 10)) .
658 substr($crow['process_time'], 10, 6) . " " .
659 xl("Claim was generated to file ") .
660 "<a href='get_claim_file.php?key=" . $crow['process_file'] .
661 "' onclick='top.restoreSession()'>" .
662 $crow['process_file'] . "</a>";
663 ++$lcount;
666 $lastcrow = $crow;
667 } // end while ($crow = sqlFetchArray($cres))
669 if ($lastcrow && $lastcrow['status'] == 4) {
670 $lhtml .= "<br>\n&nbsp;This claim has been closed.";
671 ++$lcount;
674 if ($lastcrow && $lastcrow['status'] == 5) {
675 $lhtml .= "<br>\n&nbsp;This claim has been canceled.";
676 ++$lcount;
678 } // end if ($iter['id'])
680 } // end if ($last_encounter_id != $this_encounter_id)
682 if ($skipping) continue;
684 // Collect info related to the missing modifiers test.
685 if ($iter['fee'] > 0) {
686 ++$mmo_num_charges;
687 $tmp = substr($iter['code'], 0, 3);
688 if (($tmp == '992' || $tmp == '993') && empty($iter['modifier']))
689 $mmo_empty_mod = true;
692 ++$rcount;
694 if ($rhtml) {
695 $rhtml .= "<tr bgcolor='$bgcolor'>\n";
697 $rhtml .= "<td width='50'>";
698 if ($iter['id'] && $oldcode != $iter['code_type']) {
699 $rhtml .= "<span class=text>" . $iter['code_type'] . ": </span>";
702 $oldcode = $iter['code_type'];
703 $rhtml .= "</td>\n";
704 $justify = "";
706 if ($iter['id'] && $code_types[$iter['code_type']]['just']) {
707 $js = split(":",$iter['justify']);
708 $counter = 0;
709 foreach ($js as $j) {
710 if(!empty($j)) {
711 if ($counter == 0) {
712 $justify .= " (<b>$j</b>)";
714 else {
715 $justify .= " ($j)";
717 $counter++;
722 $rhtml .= "<td><span class='text'>" .
723 ($iter['code_type'] == 'COPAY' ? oeFormatMoney($iter['code']) : $iter['code']);
724 if ($iter['modifier']) $rhtml .= ":" . $iter['modifier'];
725 $rhtml .= "</span><span style='font-size:8pt;'>$justify</span></td>\n";
727 $rhtml .= '<td align="right"><span style="font-size:8pt;">&nbsp;&nbsp;&nbsp;';
728 if ($iter['id'] && $iter['fee'] > 0) {
729 $rhtml .= oeFormatMoney($iter['fee']);
731 $rhtml .= "</span></td>\n";
732 $rhtml .= '<td><span style="font-size:8pt;">&nbsp;&nbsp;&nbsp;';
733 if ($iter['id']) $rhtml .= getProviderName(empty($iter['provider_id']) ? $iter['enc_provider_id'] : $iter['provider_id']);
734 $rhtml .= "</span></td>\n";
735 $rhtml .= '<td width=100>&nbsp;&nbsp;&nbsp;<span style="font-size:8pt;">';
736 if ($iter['id']) $rhtml .= oeFormatSDFT(strtotime($iter{"date"}));
737 $rhtml .= "</span></td>\n";
738 if ($iter['id'] && $iter['authorized'] != 1) {
739 $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";
741 else {
742 $rhtml .= "<td></td>\n";
744 if ($iter['id'] && $last_encounter_id != $this_encounter_id) {
745 $tmpbpr = $iter['bill_process'];
746 if ($tmpbpr == '0' && $iter['billed']) $tmpbpr = '2';
747 $rhtml .= "<td><input type='checkbox' value='$tmpbpr' name='claims[" . $this_encounter_id . "][bill]' onclick='set_button_states()'>&nbsp;</td>\n";
749 else {
750 $rhtml .= "<td></td>\n";
752 $rhtml .= "</tr>\n";
753 $last_encounter_id = $this_encounter_id;
755 } // end foreach
757 if ($lhtml) {
758 while ($rcount < $lcount) {
759 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'>&nbsp;</td></tr>";
760 ++$rcount;
762 if (!$missing_mods_only || ($mmo_empty_mod && $mmo_num_charges > 1)) {
763 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
764 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
772 </table>
773 </form>
775 <script>
776 set_button_states();
777 <?php
778 if ($alertmsg) {
779 echo "alert('$alertmsg');\n";
782 </script>
784 </body>
785 </html>