2 include_once("../globals.php");
3 include_once("../../library/acl.inc");
4 include_once("../../custom/code_types.inc.php");
6 include_once("$srcdir/patient.inc");
7 include_once("$srcdir/billrep.inc");
8 include_once(dirname(__FILE__
) . "/../../library/classes/OFX.class.php");
9 include_once(dirname(__FILE__
) . "/../../library/classes/X12Partner.class.php");
11 $EXPORT_INC = "$webserver_root/custom/BillingExport.php";
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";
27 $results = $db->Execute($sql);
29 if ($results->RecordCount() == 0) {
30 echo xl("No Bills Found to Include in OFX Export<br>");
33 while(!$results->EOF
) {
34 $billings[] = $results->fields
;
37 $ofx = new OFX($billings);
38 header("Pragma: public");
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");
48 if ($_POST['mode'] == 'process') {
49 if (exec("ps x | grep 'process_bills[.]php'")) {
50 $alertmsg = xl('Request ignored - claims processing is already running!');
53 exec("cd $webserver_root/library/freeb;" .
54 "php -q process_bills.php bill > process_bills.log 2>&1 &");
55 $alertmsg = xl('Batch processing initiated; this may take a while.');
60 if (!isset($_POST["mode"])) {
61 if (!isset($_POST["from_date"])) {
62 $from_date=date("Y-m-d");
64 $from_date = $_POST["from_date"];
66 if (!isset($_POST["to_date"])) {
67 $to_date = date("Y-m-d");
69 $to_date = $_POST["to_date"];
71 if (!isset($_POST["code_type"])) {
74 $code_type = $_POST["code_type"];
76 if (!isset($_POST["unbilled"])) {
79 $unbilled = $_POST["unbilled"];
81 if (!isset($_POST["authorized"])) {
82 $my_authorized = "on";
84 $my_authorized = $_POST["authorized"];
87 $from_date = $_POST["from_date"];
88 $to_date = $_POST["to_date"];
89 $code_type = $_POST["code_type"];
90 $unbilled = $_POST["unbilled"];
91 $my_authorized = $_POST["authorized"];
94 $ofrom_date = $from_date;
96 $ocode_type = $code_type;
97 $ounbilled = $unbilled;
98 $oauthorized = $my_authorized;
104 <link rel
=stylesheet href
="<?php echo $css_header; ?>" type
="text/css">
106 .subbtn
{ margin
-top
:3px
; margin
-bottom
:3px
; margin
-left
:2px
; margin
-right
:2px
}
110 function select_all() {
111 for($i=0;$i < document
.update_form
.length
;$i++
) {
112 $name = document
.update_form
[$i].name
;
113 if ($name.substring(0,7) == "claims[" && $name.substring($name.length
-6) == "[bill]") {
114 document
.update_form
[$i].checked
= true;
120 function set_button_states() {
121 var f
= document
.update_form
;
125 for($i = 0; $i < f
.length
; ++
$i) {
127 if ($name.substring(0, 7) == "claims[" && $name.substring($name.length
-6) == "[bill]" && f
[$i].checked
== true) {
128 if (f
[$i].value
== '0') ++count0
;
129 else if (f
[$i].value
== '1' || f
[$i].value
== '5') ++count1
;
134 var can_generate
= (count0
> 0 || count1
> 0 || count2
> 0);
135 var can_mark
= (count1
== 0 && (count0
> 0 || count2
> 0));
136 var can_bill
= (count0
== 0 && count1
== 0 && count2
> 0);
138 <?php
if (file_exists($EXPORT_INC)) { ?
>
139 f
.bn_external
.disabled
= !can_generate
;
141 f
.bn_hcfa_print
.disabled
= !can_generate
;
142 f
.bn_hcfa
.disabled
= !can_generate
;
143 f
.bn_ub92_print
.disabled
= !can_generate
;
144 f
.bn_ub92
.disabled
= !can_generate
;
145 f
.bn_x12
.disabled
= !can_generate
;
146 f
.bn_electronic_file
.disabled
= !can_bill
;
148 f
.bn_mark
.disabled
= !can_mark
;
153 <body
<?php
echo $top_bg_line; ?
> topmargin
=0 rightmargin
=0 leftmargin
=2 bottommargin
=0 marginwidth
=2 marginheight
=0>
155 <p style
='margin-top:5px;margin-bottom:5px;margin-left:5px'>
156 <?php
if ($GLOBALS['concurrent_layout']) { ?
>
157 <font
class='title'><?php
xl('Billing Report','e') ?
></font
>
158 <?php
} else if ($userauthorized) { ?
>
159 <a href
="../main/main.php" target
=Main
><font
class=title
><?php
xl('Billing Report','e') ?
></font
><font
class=more
> <?php
echo $tback; ?
></font
></a
>
161 <a href
="../main/onotes/office_comments.php" target
=Main
><font
class=title
><?php
xl('Billing Report','e') ?
></font
><font
class=more
><?php
echo $tback; ?
></font
></a
>
165 <form name
=the_form method
=post action
=billing_report
.php
>
167 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
168 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
169 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
170 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
171 <script type
="text/javascript" src
="../../library/dynarch_calendar_en.js"></script
>
172 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
173 <script language
='JavaScript'>
177 <input type
=hidden name
=mode value
="change">
178 <table width
=100% border
="1" cellspacing
="0" cellpadding
="0">
183  
;<span
class=text
><?php
xl('From: ','e') ?
></span
>
184 <input type
='text' size
='10' name
='from_date' id
='from_date'
185 value
='<?php echo $from_date; ?>'
186 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
187 title
='yyyy-mm-dd last date of this event' />
188 <img src
='../../interface/pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
189 id
='img_fromdate' border
='0' alt
='[?]' style
='cursor:pointer'
190 title
='Click here to choose a date'>
192 Calendar
.setup({inputField
:"from_date", ifFormat
:"%Y-%m-%d", button
:"img_fromdate"});
197  
;<span
class=text
><?php
xl('To: ','e') ?
></span
>
198 <input type
='text' size
='10' name
='to_date' id
='to_date'
199 value
='<?php echo $to_date; ?>'
200 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
201 title
='yyyy-mm-dd last date of this event' />
202 <img src
='../../interface/pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
203 id
='img_todate' border
='0' alt
='[?]' style
='cursor:pointer'
204 title
='Click here to choose a date'>
206 Calendar
.setup({inputField
:"to_date", ifFormat
:"%Y-%m-%d", button
:"img_todate"});
210 <input type
="hidden" name
="code_type" value
="%"></td
>
211 <td nowrap
><input type
=checkbox name
=unbilled
<?php
if ($unbilled == "on") {echo "checked";}; ?
>><span
class=text
><?php
xl('Show Unbilled Only','e') ?
></span
></td
>
212 <td nowrap
><input type
=checkbox name
=authorized
<?php
if ($my_authorized == "on") {echo "checked";}; ?
>><span
class=text
><?php
xl('Show Authorized Only','e') ?
></span
></td
>
213 <td align
='right' width
='10%' nowrap
>
214  
;<span
class=text
><a href
="javascript:document.the_form.mode.value='change';document.the_form.submit()" class=link_submit
><?php
xl('[Change View]','e') ?
></a
>
216 <a href
="javascript:document.the_form.mode.value='export';document.the_form.submit()" class=link_submit
><?php
xl('[Export OFX]','e') ?
></a
></span
> 
;
220 <td nowrap
> 
;<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); ?>" class=link_submit target
=new><?php
xl('[View Printable Report]','e') ?
></a
></td
>
224 $acct_config = $GLOBALS['oer_config']['ws_accounting'];
225 if($acct_config['enabled'] == true) {
226 print '<span class=text><a href="javascript:void window.open(\'' . $acct_config['url_path'] . '\')">' . xl("[SQL-Ledger]") . '</a></span>';
227 if (acl_check('acct', 'rep')) {
228 print '<span class=text> <a href="javascript:void window.open(\'sl_receipts_report.php\')">' . xl('[Reports]') . '</a></span>';
230 if (acl_check('acct', 'eob')) {
231 print '<span class=text> <a href="javascript:void window.open(\'sl_eob_search.php\')">' . xl('[EOBs]') . '</a></span>';
236 <td colspan
='2' nowrap
>
238 <?php
if (! file_exists($EXPORT_INC)) { ?
>
239 <a href
="javascript:document.the_form.mode.value='process';document.the_form.submit()" class="link_submit"
240 title
="Process all queued bills to create electronic data (and print if requested)"><?php
xl('[Start Batch Processing]','e') ?
></a
>
241  
; <a href
='../../library/freeb/process_bills.log' target
='_blank' class='link_submit'
242 title
='See messages from the last batch processing run'><?php
xl('[view log]','e') ?
></a
></span
>
245 <td align
='right' nowrap
>
246 <a href
="javascript:select_all()" class="link_submit"><?php
xl('[Select All]','e') ?
></a
> 
;
252 <form name
=update_form method
=post action
=billing_process
.php
>
256 <?php
if (file_exists($EXPORT_INC)) { ?
>
257 <input type
="submit" class="subbtn" name
="bn_external" value
="Export Billing" title
="<?php xl('Export to external billing system','e') ?>">
258 <input type
="submit" class="subbtn" name
="bn_mark" value
="Mark as Cleared" title
="<?php xl('Mark as billed but skip billing','e') ?>">
260 <input type
="submit" class="subbtn" name
="bn_hcfa_print" value
="Queue HCFA & Print" title
="<?php xl('Queue for HCFA batch processing and printing','e') ?>">
261 <input type
="submit" class="subbtn" name
="bn_hcfa" value
="Queue HCFA" title
="<?php xl('Queue for HCFA batch processing','e')?>">
262 <input type
="submit" class="subbtn" name
="bn_ub92_print" value
="Queue UB92 & Print" title
="<?php xl('Queue for UB-92 batch processing and printing','e')?>">
263 <input type
="submit" class="subbtn" name
="bn_ub92" value
="Queue UB92" title
="<?php xl('Queue for UB-92 batch processing','e')?>">
264 <input type
="submit" class="subbtn" name
="bn_x12" value
="Queue X12" title
="<?php xl('Queue for X12 batch processing','e')?>">
265 <input type
="submit" class="subbtn" name
="bn_mark" value
="Mark as Cleared" title
="<?php xl('Post to accounting and mark as billed','e')?>">
266 <input type
="submit" class="subbtn" name
="bn_electronic_file" value
="Make Electronic Batch & Clear" title
="<?php xl('Download billing file, post to accounting and mark as billed','e')?>">
271 <input type
=hidden name
=mode value
="bill">
272 <input type
=hidden name
=authorized value
="<?php echo $my_authorized; ?>">
273 <input type
=hidden name
=unbilled value
="<?php echo $unbilled; ?>">
274 <input type
=hidden name
=code_type value
="%">
275 <input type
=hidden name
=to_date value
="<?php echo $to_date; ?>">
276 <input type
=hidden name
=from_date value
="<?php echo $from_date; ?>">
278 if ($my_authorized == "on" ) {
279 $my_authorized = "1";
281 $my_authorized = "%";
284 if ($unbilled == "on") {
290 <input type
=hidden name
=bill_list value
="<?php
291 $list = getBillsListBetween($from_date,$to_date,$my_authorized,$unbilled,"%
");
294 <!-- new form
for uploading
-->
296 if (!isset($_POST["mode"])) {
297 if (!isset($_POST["from_date"])) {
298 $from_date=date("Y-m-d");
300 $from_date = $_POST["from_date"];
302 if (!isset($_POST["to_date"])) {
303 $to_date = date("Y-m-d");
305 $to_date = $_POST["to_date"];
307 if (!isset($_POST["code_type"])) {
310 $code_type = $_POST["code_type"];
312 if (!isset($_POST["unbilled"])) {
315 $unbilled = $_POST["unbilled"];
317 if (!isset($_POST["authorized"])) {
318 $my_authorized = "on";
320 $my_authorized = $_POST["authorized"];
323 $from_date = $_POST["from_date"];
324 $to_date = $_POST["to_date"];
325 $code_type = $_POST["code_type"];
326 $unbilled = $_POST["unbilled"];
327 $my_authorized = $_POST["authorized"];
329 if ($my_authorized == "on" ) {
330 $my_authorized = "1";
332 $my_authorized = "%";
335 if ($unbilled == "on") {
343 if (isset($_POST["mode"]) && $_POST["mode"] == "bill") {
344 billCodesList($list);
349 <table border
="0" cellspacing
="0" cellpadding
="0" width
="100%">
352 if ($ret = getBillsBetween($from_date,$to_date,$my_authorized,$unbilled,"%")) {
355 $last_encounter_id = "";
363 foreach ($ret as $iter) {
364 $this_encounter_id = $iter['pid'] . "-" . $iter['encounter'];
366 if ($last_encounter_id != $this_encounter_id) {
368 while ($rcount < $lcount) {
369 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
372 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
373 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n\n";
379 // If there are ANY unauthorized items in this encounter and this is
380 // the normal case of viewing only authorized billing, then skip the
384 if ($my_authorized == '1') {
385 $res = sqlQuery("select count(*) as count from billing where " .
386 "encounter = '" . $iter['encounter'] . "' and " .
387 "pid='" . $iter['pid'] . "' and " .
388 "activity = 1 and authorized = 0");
389 if ($res['count'] > 0) {
391 $last_encounter_id = $this_encounter_id;
396 $name = getPatientData($iter['pid'], "fname, mname, lname");
398 # Check if patient has primary insurance and a subscriber exists for it.
399 # If not we will highlight their name in red.
400 # TBD: more checking here.
402 $res = sqlQuery("select count(*) as count from insurance_data where " .
403 "pid = " . $iter['pid'] . " and " .
404 "type='primary' and " .
405 "subscriber_lname is not null and " .
406 "subscriber_lname != '' limit 1");
407 $namecolor = ($res['count'] > 0) ?
"black" : "#ff7777";
410 $bgcolor = "#" . (($encount & 1) ?
"ddddff" : "ffdddd");
411 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
416 $lhtml .= " <span class=bold><font color='$namecolor'>". $name['fname'] . " " . $name['lname'] . "</font></span><span class=small> (" . $iter['pid'] . "-" . $iter['encounter'] . ")</span>";
417 $lhtml .= " <a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] ."/interface/patient_file/encounter/patient_encounter.php?set_encounter=" . $iter['encounter'] . "&pid=" . $iter['pid'] . "\">[To Encounter]</a>";
418 $lhtml .= " <a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] ."/interface/patient_file/summary/demographics_full.php?&pid=" . $iter['pid'] . "\">[To Demographics]</a>";
419 $lhtml .= "<br />\n";
420 $lhtml .= " <span class=text>Bill: ";
421 $lhtml .= "<select name='claims[" . $this_encounter_id . "][payer]' style='background-color:$bgcolor'>";
423 $query = "SELECT id.provider AS id, id.type, " .
424 "ic.x12_default_partner_id AS ic_x12id, ic.name AS provider " .
425 "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
426 "ic.id = id.provider AND pid = '" . mysql_escape_string($iter['pid']) .
428 $result = sqlStatement($query);
431 $default_x12_partner = $iter['ic_x12id'];
433 while ($row = mysql_fetch_array($result)) {
434 if (strlen($row['provider']) > 0) {
436 // This preserves any existing insurance company selection, which is
437 // important when EOB posting has re-queued for secondary billing.
438 $lhtml .= "<option value=\"" . $row['id'] . "\"";
439 if (($count == 0 && !$iter['payer_id']) ||
$row['id'] == $iter['payer_id']) {
440 $lhtml .= " selected";
441 if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
443 $lhtml .= ">" . $row['type'] . ": " . $row['provider'] . "</option>";
447 $lhtml .= "<option value=\"" .$row['id'] . "\" selected>" . $row['type'] . ": " . $row['provider']. "</option>";
448 if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
451 $lhtml .= "<option value=\"" . $row['id'] . "\">" . $row['type'] . ": " . $row['provider']. "</option>";
459 $lhtml .= "<option value='-1'>Unassigned</option>\n";
460 $lhtml .= "</select> \n";
461 $lhtml .= "<select name='claims[" . $this_encounter_id . "][partner]' style='background-color:$bgcolor'>";
462 $x = new X12Partner();
463 $partners = $x->_utility_array($x->x12_partner_factory());
464 foreach ($partners as $xid => $xname) {
465 $lhtml .= '<option label="' . $xname . '" value="' . $xid .'"';
466 if ($xid == $default_x12_partner) {
467 $lhtml .= "selected";
469 $lhtml .= '>' . $xname . '</option>';
471 $lhtml .= "</select>";
472 $lhtml .= "<br>\n ".xl("Claim was initiated: ") . $iter['date'];
473 if ($iter['billed'] == 1) {
474 $lhtml .= "<br>\n ".xl("Claim was billed: ") . $iter['bill_date'];
477 if ($iter['bill_process'] == 1) {
478 $lhtml .= "<br>\n ".xl("Claim is queued for processing");
481 if ($iter['bill_process'] == 5) {
482 $lhtml .= "<br>\n ".xl("Claim is queued for printing and processing");
485 if ($iter['bill_process'] == 2) {
486 $lhtml .= "<br>\n ".xl("Claim was processed: ") . $iter['process_date'];
487 $lhtml .= '<br>' . "\n" . ' '.xl("Claim is in file:").' <a href="get_claim_file.php?key=' . $iter['process_file'] .'">' . $iter['process_file'] . '</a> or ';
488 $lhtml .= '<a href="get_claim_file.php?action=print&key=' . $iter['process_file'] .'">Print It</a> or ';
489 $lhtml .= '<a target="_new" href="freebtest.php?format=' . $iter['target'] . '&billkey=' . $iter['pid'] . '-' . $iter['encounter'] . '">'.xl('Run Test').'</a>';
490 $lhtml .= '<input type="hidden" name="claims[' . $this_encounter_id . '][file]" value="' . $iter['process_file'] . '">';
493 if ($iter['bill_process'] == 3) {
494 $lhtml .= "<br>\n ".xl("Claim was processed: ") . $iter['process_date'] . xl(" but there was an error: "). $iter['process_file'];
499 if ($skipping) continue;
503 $rhtml .= "<tr bgcolor='$bgcolor'>\n";
505 $rhtml .= "<td width='50'>";
506 if ($oldcode != $iter['code_type']) {
507 $rhtml .= "<span class=text>" . $iter['code_type'] . ": </span>";
509 $oldcode = $iter['code_type'];
512 // if ($iter['code_type'] == "CPT4" || $iter['code_type'] == "HCPCS") {
513 if ($code_types[$iter['code_type']]['just']) {
514 $js = split(":",$iter['justify']);
516 foreach ($js as $j) {
519 $justify .= " (<b>$j</b>)";
529 $rhtml .= "<td><span class=text>" . $iter{"code"}. "</span>" . '<span style="font-size:8pt;">' . $justify . "</span></td>\n";
530 $rhtml .= '<td align="right"><span style="font-size:8pt;"> ';
531 if ($iter['fee'] > 0) {
532 $rhtml .= '$' . $iter['fee'];
534 $rhtml .= "</span></td>\n";
535 $rhtml .= '<td><span style="font-size:8pt;"> ';
536 $rhtml .= getProviderName($iter['provider_id']);
537 $rhtml .= "</span></td>\n";
538 $rhtml .= '<td width=100> <span style="font-size:8pt;">' . date("Y-m-d",strtotime($iter{"date"})) . "</span></td>\n";
539 if ($iter['authorized'] != 1) {
540 $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";
543 $rhtml .= "<td></td>\n";
545 if ($last_encounter_id != $this_encounter_id) {
546 $rhtml .= "<td><input type='checkbox' value='" . $iter['bill_process'] . "$procstatus' name='claims[" . $this_encounter_id . "][bill]' onclick='set_button_states()'> </td>\n";
549 $rhtml .= "<td></td>\n";
552 $last_encounter_id = $this_encounter_id;
556 while ($rcount < $lcount) {
557 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
560 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
561 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
575 echo "alert('$alertmsg');\n";