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
="<?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 <?
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
<?
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'>
157 if ($userauthorized) {
159 <a href
="../main/main.php" target
=Main
><font
class=title
><?
xl('Billing Report','e')?
></font
><font
class=more
> <?
echo $tback;?
></font
></a
>
161 <a href
="../main/onotes/office_comments.php" target
=Main
><font
class=title
><?
xl('Billing Report','e')?
></font
><font
class=more
><?
echo $tback;?
></font
></a
>
167 <form name
=the_form method
=post action
=billing_report
.php
>
169 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
170 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
171 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
172 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
173 <script type
="text/javascript" src
="../../library/dynarch_calendar_en.js"></script
>
174 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
175 <script language
='JavaScript'>
179 <input type
=hidden name
=mode value
="change">
180 <table width
=100% border
="1" cellspacing
="0" cellpadding
="0">
185  
;<span
class=text
><?
xl('From: ','e')?
></span
>
186 <input type
='text' size
='10' name
='from_date' id
='from_date'
187 value
='<?echo $from_date;?>'
188 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
189 title
='yyyy-mm-dd last date of this event' />
190 <img src
='../../interface/pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
191 id
='img_fromdate' border
='0' alt
='[?]' style
='cursor:pointer'
192 title
='Click here to choose a date'>
194 Calendar
.setup({inputField
:"from_date", ifFormat
:"%Y-%m-%d", button
:"img_fromdate"});
199  
;<span
class=text
><?
xl('To: ','e')?
></span
>
200 <input type
='text' size
='10' name
='to_date' id
='to_date'
201 value
='<?echo $to_date;?>'
202 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
203 title
='yyyy-mm-dd last date of this event' />
204 <img src
='../../interface/pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
205 id
='img_todate' border
='0' alt
='[?]' style
='cursor:pointer'
206 title
='Click here to choose a date'>
208 Calendar
.setup({inputField
:"to_date", ifFormat
:"%Y-%m-%d", button
:"img_todate"});
212 <input type
="hidden" name
="code_type" value
="%"></td
>
213 <td nowrap
><input type
=checkbox name
=unbilled
<?
if ($unbilled == "on") {echo "checked";};?
>><span
class=text
><?
xl('Show Unbilled Only','e')?
></span
></td
>
214 <td nowrap
><input type
=checkbox name
=authorized
<?
if ($my_authorized == "on") {echo "checked";};?
>><span
class=text
><?
xl('Show Authorized Only','e')?
></span
></td
>
215 <td align
='right' width
='10%' nowrap
>
216  
;<span
class=text
><a href
="javascript:document.the_form.mode.value='change';document.the_form.submit()" class=link_submit
><?
xl('[Change View]','e')?
></a
>
218 <a href
="javascript:document.the_form.mode.value='export';document.the_form.submit()" class=link_submit
><?
xl('[Export OFX]','e')?
></a
></span
> 
;
222 <td nowrap
> 
;<a href
="print_billing_report.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><?
xl('[View Printable Report]','e')?
></a
></td
>
226 $acct_config = $GLOBALS['oer_config']['ws_accounting'];
227 if($acct_config['enabled'] == true) {
228 print '<span class=text><a href="javascript:void window.open(\'' . $acct_config['url_path'] . '\')">' . xl("[SQL-Ledger]") . '</a></span>';
229 if (acl_check('acct', 'rep')) {
230 print '<span class=text> <a href="javascript:void window.open(\'sl_receipts_report.php\')">' . xl('[Reports]') . '</a></span>';
232 if (acl_check('acct', 'eob')) {
233 print '<span class=text> <a href="javascript:void window.open(\'sl_eob_search.php\')">' . xl('[EOBs]') . '</a></span>';
238 <td colspan
='2' nowrap
>
240 <?
if (! file_exists($EXPORT_INC)) { ?
>
241 <a href
="javascript:document.the_form.mode.value='process';document.the_form.submit()" class="link_submit"
242 title
="Process all queued bills to create electronic data (and print if requested)"><?
xl('[Start Batch Processing]','e')?
></a
>
243  
; <a href
='../../library/freeb/process_bills.log' target
='_blank' class='link_submit'
244 title
='See messages from the last batch processing run'><?
xl('[view log]','e')?
></a
></span
>
247 <td align
='right' nowrap
>
248 <a href
="javascript:select_all()" class="link_submit"><?
xl('[Select All]','e')?
></a
> 
;
254 <form name
=update_form method
=post action
=billing_process
.php
>
258 <?
if (file_exists($EXPORT_INC)) { ?
>
259 <input type
="submit" class="subbtn" name
="bn_external" value
="Export Billing" title
="<?xl('Export to external billing system','e')?>">
260 <input type
="submit" class="subbtn" name
="bn_mark" value
="Mark as Cleared" title
="<?xl('Mark as billed but skip billing','e')?>">
262 <input type
="submit" class="subbtn" name
="bn_hcfa_print" value
="Queue HCFA & Print" title
="<?xl('Queue for HCFA batch processing and printing','e')?>">
263 <input type
="submit" class="subbtn" name
="bn_hcfa" value
="Queue HCFA" title
="<?xl('Queue for HCFA batch processing','e')?>">
264 <input type
="submit" class="subbtn" name
="bn_ub92_print" value
="Queue UB92 & Print" title
="<?xl('Queue for UB-92 batch processing and printing','e')?>">
265 <input type
="submit" class="subbtn" name
="bn_ub92" value
="Queue UB92" title
="<?xl('Queue for UB-92 batch processing','e')?>">
266 <input type
="submit" class="subbtn" name
="bn_x12" value
="Queue X12" title
="<?xl('Queue for X12 batch processing','e')?>">
267 <input type
="submit" class="subbtn" name
="bn_mark" value
="Mark as Cleared" title
="<?xl('Post to accounting and mark as billed','e')?>">
268 <input type
="submit" class="subbtn" name
="bn_electronic_file" value
="Make Electronic Batch & Clear" title
="<?xl('Download billing file, post to accounting and mark as billed','e')?>">
273 <input type
=hidden name
=mode value
="bill">
274 <input type
=hidden name
=authorized value
="<?echo $my_authorized;?>">
275 <input type
=hidden name
=unbilled value
="<?echo $unbilled;?>">
276 <input type
=hidden name
=code_type value
="%">
277 <input type
=hidden name
=to_date value
="<?echo $to_date;?>">
278 <input type
=hidden name
=from_date value
="<?echo $from_date;?>">
280 if ($my_authorized == "on" ) {
281 $my_authorized = "1";
283 $my_authorized = "%";
286 if ($unbilled == "on") {
292 <input type
=hidden name
=bill_list value
="<?
293 $list = getBillsListBetween($from_date,$to_date,$my_authorized,$unbilled,"%
");
296 <!-- new form
for uploading
-->
298 if (!isset($_POST["mode"])) {
299 if (!isset($_POST["from_date"])) {
300 $from_date=date("Y-m-d");
302 $from_date = $_POST["from_date"];
304 if (!isset($_POST["to_date"])) {
305 $to_date = date("Y-m-d");
307 $to_date = $_POST["to_date"];
309 if (!isset($_POST["code_type"])) {
312 $code_type = $_POST["code_type"];
314 if (!isset($_POST["unbilled"])) {
317 $unbilled = $_POST["unbilled"];
319 if (!isset($_POST["authorized"])) {
320 $my_authorized = "on";
322 $my_authorized = $_POST["authorized"];
325 $from_date = $_POST["from_date"];
326 $to_date = $_POST["to_date"];
327 $code_type = $_POST["code_type"];
328 $unbilled = $_POST["unbilled"];
329 $my_authorized = $_POST["authorized"];
331 if ($my_authorized == "on" ) {
332 $my_authorized = "1";
334 $my_authorized = "%";
337 if ($unbilled == "on") {
345 if (isset($_POST["mode"]) && $_POST["mode"] == "bill") {
346 billCodesList($list);
351 <table border
="0" cellspacing
="0" cellpadding
="0" width
="100%">
354 if ($ret = getBillsBetween($from_date,$to_date,$my_authorized,$unbilled,"%")) {
357 $last_encounter_id = "";
365 foreach ($ret as $iter) {
366 $this_encounter_id = $iter['pid'] . "-" . $iter['encounter'];
368 if ($last_encounter_id != $this_encounter_id) {
370 while ($rcount < $lcount) {
371 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
374 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
375 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n\n";
381 // If there are ANY unauthorized items in this encounter and this is
382 // the normal case of viewing only authorized billing, then skip the
386 if ($my_authorized == '1') {
387 $res = sqlQuery("select count(*) as count from billing where " .
388 "encounter = '" . $iter['encounter'] . "' and " .
389 "pid='" . $iter['pid'] . "' and " .
390 "activity = 1 and authorized = 0");
391 if ($res['count'] > 0) {
393 $last_encounter_id = $this_encounter_id;
398 $name = getPatientData($iter['pid'], "fname, mname, lname");
400 # Check if patient has primary insurance and a subscriber exists for it.
401 # If not we will highlight their name in red.
402 # TBD: more checking here.
404 $res = sqlQuery("select count(*) as count from insurance_data where " .
405 "pid = " . $iter['pid'] . " and " .
406 "type='primary' and " .
407 "subscriber_lname is not null and " .
408 "subscriber_lname != '' limit 1");
409 $namecolor = ($res['count'] > 0) ?
"black" : "#ff7777";
412 $bgcolor = "#" . (($encount & 1) ?
"ddddff" : "ffdddd");
413 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
418 $lhtml .= " <span class=bold><font color='$namecolor'>". $name['fname'] . " " . $name['lname'] . "</font></span><span class=small> (" . $iter['pid'] . "-" . $iter['encounter'] . ")</span>";
419 $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>";
420 $lhtml .= " <a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] ."/interface/patient_file/summary/demographics_full.php?&pid=" . $iter['pid'] . "\">[To Demographics]</a>";
421 $lhtml .= "<br />\n";
422 $lhtml .= " <span class=text>Bill: ";
423 $lhtml .= "<select name='claims[" . $this_encounter_id . "][payer]' style='background-color:$bgcolor'>";
424 $query = "SELECT id.provider as id, id.type, ic.x12_default_partner_id as ic_x12id, ic.name as provider FROM insurance_data as id, insurance_companies as ic WHERE ic.id = id.provider AND pid = '" . mysql_escape_string($iter['pid']) . "' order by type";
426 $result = sqlStatement($query);
428 $default_x12_partner = $iter['ic_x12id'];
430 while ($row = mysql_fetch_array($result)) {
431 if (strlen($row['provider']) > 0) {
433 $lhtml .= "<option value=\"" .$row['id'] . "\" selected>" . $row['type'] . ": " . $row['provider']. "</option>";
434 if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
437 $lhtml .= "<option value=\"" . $row['id'] . "\">" . $row['type'] . ": " . $row['provider']. "</option>";
442 $lhtml .= "<option value='-1'>Unassigned</option>\n";
443 $lhtml .= "</select> \n";
444 $lhtml .= "<select name='claims[" . $this_encounter_id . "][partner]' style='background-color:$bgcolor'>";
445 $x = new X12Partner();
446 $partners = $x->_utility_array($x->x12_partner_factory());
447 foreach ($partners as $xid => $xname) {
448 $lhtml .= '<option label="' . $xname . '" value="' . $xid .'"';
449 if ($xid == $default_x12_partner) {
450 $lhtml .= "selected";
452 $lhtml .= '>' . $xname . '</option>';
454 $lhtml .= "</select>";
455 $lhtml .= "<br>\n ".xl("Claim was initiated: ") . $iter['date'];
456 if ($iter['billed'] == 1) {
457 $lhtml .= "<br>\n ".xl("Claim was billed: ") . $iter['bill_date'];
460 if ($iter['bill_process'] == 1) {
461 $lhtml .= "<br>\n ".xl("Claim is queued for processing");
464 if ($iter['bill_process'] == 5) {
465 $lhtml .= "<br>\n ".xl("Claim is queued for printing and processing");
468 if ($iter['bill_process'] == 2) {
469 $lhtml .= "<br>\n ".xl("Claim was processed: ") . $iter['process_date'];
470 $lhtml .= '<br>' . "\n" . ' '.xl("Claim is in file:").' <a href="get_claim_file.php?key=' . $iter['process_file'] .'">' . $iter['process_file'] . '</a> or ';
471 $lhtml .= '<a href="get_claim_file.php?action=print&key=' . $iter['process_file'] .'">Print It</a> or ';
472 $lhtml .= '<a target="_new" href="freebtest.php?format=' . $iter['target'] . '&billkey=' . $iter['pid'] . '-' . $iter['encounter'] . '">'.xl('Run Test').'</a>';
473 $lhtml .= '<input type="hidden" name="claims[' . $this_encounter_id . '][file]" value="' . $iter['process_file'] . '">';
476 if ($iter['bill_process'] == 3) {
477 $lhtml .= "<br>\n ".xl("Claim was processed: ") . $iter['process_date'] . xl(" but there was an error: "). $iter['process_file'];
482 if ($skipping) continue;
486 $rhtml .= "<tr bgcolor='$bgcolor'>\n";
488 $rhtml .= "<td width='50'>";
489 if ($oldcode != $iter['code_type']) {
490 $rhtml .= "<span class=text>" . $iter['code_type'] . ": </span>";
492 $oldcode = $iter['code_type'];
495 // if ($iter['code_type'] == "CPT4" || $iter['code_type'] == "HCPCS") {
496 if ($code_types[$iter['code_type']]['just']) {
497 $js = split(":",$iter['justify']);
499 foreach ($js as $j) {
502 $justify .= " (<b>$j</b>)";
512 $rhtml .= "<td><span class=text>" . $iter{"code"}. "</span>" . '<span style="font-size:8pt;">' . $justify . "</span></td>\n";
513 $rhtml .= '<td align="right"><span style="font-size:8pt;"> ';
514 if ($iter['fee'] > 0) {
515 $rhtml .= '$' . $iter['fee'];
517 $rhtml .= "</span></td>\n";
518 $rhtml .= '<td><span style="font-size:8pt;"> ';
519 $rhtml .= getProviderName($iter['provider_id']);
520 $rhtml .= "</span></td>\n";
521 $rhtml .= '<td width=100> <span style="font-size:8pt;">' . date("Y-m-d",strtotime($iter{"date"})) . "</span></td>\n";
522 if ($iter['authorized'] != 1) {
523 $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";
526 $rhtml .= "<td></td>\n";
528 if ($last_encounter_id != $this_encounter_id) {
529 $rhtml .= "<td><input type='checkbox' value='" . $iter['bill_process'] . "$procstatus' name='claims[" . $this_encounter_id . "][bill]' onclick='set_button_states()'> </td>\n";
532 $rhtml .= "<td></td>\n";
535 $last_encounter_id = $this_encounter_id;
539 while ($rcount < $lcount) {
540 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
543 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
544 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
558 echo "alert('$alertmsg');\n";