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
;
151 // Process a click to go to an encounter.
152 function toencounter(pid
, pname
, enc
, datestr
) {
153 <?php
if ($GLOBALS['concurrent_layout']) { ?
>
154 var othername
= (window
.name
== 'RTop') ?
'RBot' : 'RTop';
155 parent
.left_nav
.setPatient(pname
,pid
,'');
156 parent
.left_nav
.setEncounter(datestr
, enc
, othername
);
157 parent
.left_nav
.setRadio(othername
, 'enc');
158 parent
.frames
[othername
].location
.href
=
159 '../patient_file/encounter/encounter_top.php?set_encounter='
160 + enc +
'&pid=' + pid
;
162 location
.href
= '../patient_file/encounter/patient_encounter.php?set_encounter='
163 + enc +
'&pid=' + pid
;
167 // Process a click to go to patient demographics.
168 function topatient(pid
) {
169 <?php
if ($GLOBALS['concurrent_layout']) { ?
>
170 var othername
= (window
.name
== 'RTop') ?
'RBot' : 'RTop';
171 parent
.frames
[othername
].location
.href
=
172 '../patient_file/summary/demographics_full.php?pid=' + pid
;
174 location
.href
= '../patient_file/summary/demographics_full.php?pid=' + pid
;
180 <body
<?php
echo $top_bg_line; ?
> topmargin
=0 rightmargin
=0 leftmargin
=2 bottommargin
=0 marginwidth
=2 marginheight
=0>
182 <p style
='margin-top:5px;margin-bottom:5px;margin-left:5px'>
183 <?php
if ($GLOBALS['concurrent_layout']) { ?
>
184 <font
class='title'><?php
xl('Billing Report','e') ?
></font
>
185 <?php
} else if ($userauthorized) { ?
>
186 <a href
="../main/main.php" target
=Main
><font
class=title
><?php
xl('Billing Report','e') ?
></font
><font
class=more
> <?php
echo $tback; ?
></font
></a
>
188 <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
>
192 <form name
=the_form method
=post action
=billing_report
.php
>
194 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
195 <script type
="text/javascript" src
="../../library/dialog.js"></script
>
196 <script type
="text/javascript" src
="../../library/textformat.js"></script
>
197 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
198 <script type
="text/javascript" src
="../../library/dynarch_calendar_en.js"></script
>
199 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
200 <script language
='JavaScript'>
204 <input type
=hidden name
=mode value
="change">
205 <table width
=100% border
="1" cellspacing
="0" cellpadding
="0">
210  
;<span
class=text
><?php
xl('From: ','e') ?
></span
>
211 <input type
='text' size
='10' name
='from_date' id
='from_date'
212 value
='<?php echo $from_date; ?>'
213 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
214 title
='yyyy-mm-dd last date of this event' />
215 <img src
='../../interface/pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
216 id
='img_fromdate' border
='0' alt
='[?]' style
='cursor:pointer'
217 title
='Click here to choose a date'>
219 Calendar
.setup({inputField
:"from_date", ifFormat
:"%Y-%m-%d", button
:"img_fromdate"});
224  
;<span
class=text
><?php
xl('To: ','e') ?
></span
>
225 <input type
='text' size
='10' name
='to_date' id
='to_date'
226 value
='<?php echo $to_date; ?>'
227 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)'
228 title
='yyyy-mm-dd last date of this event' />
229 <img src
='../../interface/pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
230 id
='img_todate' border
='0' alt
='[?]' style
='cursor:pointer'
231 title
='Click here to choose a date'>
233 Calendar
.setup({inputField
:"to_date", ifFormat
:"%Y-%m-%d", button
:"img_todate"});
237 <input type
="hidden" name
="code_type" value
="%"></td
>
238 <td nowrap
><input type
=checkbox name
=unbilled
<?php
if ($unbilled == "on") {echo "checked";}; ?
>><span
class=text
><?php
xl('Show Unbilled Only','e') ?
></span
></td
>
239 <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
>
240 <td align
='right' width
='10%' nowrap
>
241  
;<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
>
243 <a href
="javascript:document.the_form.mode.value='export';document.the_form.submit()" class=link_submit
><?php
xl('[Export OFX]','e') ?
></a
></span
> 
;
247 <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
>
251 $acct_config = $GLOBALS['oer_config']['ws_accounting'];
252 if($acct_config['enabled'] == true) {
253 print '<span class=text><a href="javascript:void window.open(\'' . $acct_config['url_path'] . '\')">' . xl("[SQL-Ledger]") . '</a></span>';
254 if (acl_check('acct', 'rep')) {
255 print '<span class=text> <a href="javascript:void window.open(\'sl_receipts_report.php\')">' . xl('[Reports]') . '</a></span>';
257 if (acl_check('acct', 'eob')) {
258 print '<span class=text> <a href="javascript:void window.open(\'sl_eob_search.php\')">' . xl('[EOBs]') . '</a></span>';
263 <td colspan
='2' nowrap
>
265 <?php
if (! file_exists($EXPORT_INC)) { ?
>
266 <a href
="javascript:document.the_form.mode.value='process';document.the_form.submit()" class="link_submit"
267 title
="Process all queued bills to create electronic data (and print if requested)"><?php
xl('[Start Batch Processing]','e') ?
></a
>
268  
; <a href
='../../library/freeb/process_bills.log' target
='_blank' class='link_submit'
269 title
='See messages from the last batch processing run'><?php
xl('[view log]','e') ?
></a
></span
>
272 <td align
='right' nowrap
>
273 <a href
="javascript:select_all()" class="link_submit"><?php
xl('[Select All]','e') ?
></a
> 
;
279 <form name
=update_form method
=post action
=billing_process
.php
>
283 <?php
if (file_exists($EXPORT_INC)) { ?
>
284 <input type
="submit" class="subbtn" name
="bn_external" value
="Export Billing" title
="<?php xl('Export to external billing system','e') ?>">
285 <input type
="submit" class="subbtn" name
="bn_mark" value
="Mark as Cleared" title
="<?php xl('Mark as billed but skip billing','e') ?>">
287 <input type
="submit" class="subbtn" name
="bn_hcfa_print" value
="Queue HCFA & Print" title
="<?php xl('Queue for HCFA batch processing and printing','e') ?>">
288 <input type
="submit" class="subbtn" name
="bn_hcfa" value
="Queue HCFA" title
="<?php xl('Queue for HCFA batch processing','e')?>">
289 <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')?>">
290 <input type
="submit" class="subbtn" name
="bn_ub92" value
="Queue UB92" title
="<?php xl('Queue for UB-92 batch processing','e')?>">
291 <input type
="submit" class="subbtn" name
="bn_x12" value
="Queue X12" title
="<?php xl('Queue for X12 batch processing','e')?>">
292 <input type
="submit" class="subbtn" name
="bn_mark" value
="Mark as Cleared" title
="<?php xl('Post to accounting and mark as billed','e')?>">
293 <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')?>">
298 <input type
=hidden name
=mode value
="bill">
299 <input type
=hidden name
=authorized value
="<?php echo $my_authorized; ?>">
300 <input type
=hidden name
=unbilled value
="<?php echo $unbilled; ?>">
301 <input type
=hidden name
=code_type value
="%">
302 <input type
=hidden name
=to_date value
="<?php echo $to_date; ?>">
303 <input type
=hidden name
=from_date value
="<?php echo $from_date; ?>">
305 if ($my_authorized == "on" ) {
306 $my_authorized = "1";
308 $my_authorized = "%";
311 if ($unbilled == "on") {
317 <input type
=hidden name
=bill_list value
="<?php
318 $list = getBillsListBetween($from_date,$to_date,$my_authorized,$unbilled,"%
");
321 <!-- new form
for uploading
-->
323 if (!isset($_POST["mode"])) {
324 if (!isset($_POST["from_date"])) {
325 $from_date=date("Y-m-d");
327 $from_date = $_POST["from_date"];
329 if (!isset($_POST["to_date"])) {
330 $to_date = date("Y-m-d");
332 $to_date = $_POST["to_date"];
334 if (!isset($_POST["code_type"])) {
337 $code_type = $_POST["code_type"];
339 if (!isset($_POST["unbilled"])) {
342 $unbilled = $_POST["unbilled"];
344 if (!isset($_POST["authorized"])) {
345 $my_authorized = "on";
347 $my_authorized = $_POST["authorized"];
350 $from_date = $_POST["from_date"];
351 $to_date = $_POST["to_date"];
352 $code_type = $_POST["code_type"];
353 $unbilled = $_POST["unbilled"];
354 $my_authorized = $_POST["authorized"];
356 if ($my_authorized == "on" ) {
357 $my_authorized = "1";
359 $my_authorized = "%";
362 if ($unbilled == "on") {
370 if (isset($_POST["mode"]) && $_POST["mode"] == "bill") {
371 billCodesList($list);
376 <table border
="0" cellspacing
="0" cellpadding
="0" width
="100%">
379 if ($ret = getBillsBetween($from_date,$to_date,$my_authorized,$unbilled,"%")) {
382 $last_encounter_id = "";
390 foreach ($ret as $iter) {
391 $this_encounter_id = $iter['pid'] . "-" . $iter['encounter'];
393 // echo "<!-- $this_encounter_id -->\n"; // debugging
395 if ($last_encounter_id != $this_encounter_id) {
397 while ($rcount < $lcount) {
398 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
401 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
402 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n\n";
408 // If there are ANY unauthorized items in this encounter and this is
409 // the normal case of viewing only authorized billing, then skip the
413 if ($my_authorized == '1') {
414 $res = sqlQuery("select count(*) as count from billing where " .
415 "encounter = '" . $iter['encounter'] . "' and " .
416 "pid='" . $iter['pid'] . "' and " .
417 "activity = 1 and authorized = 0");
418 if ($res['count'] > 0) {
420 $last_encounter_id = $this_encounter_id;
425 $name = getPatientData($iter['pid'], "fname, mname, lname");
427 # Check if patient has primary insurance and a subscriber exists for it.
428 # If not we will highlight their name in red.
429 # TBD: more checking here.
431 $res = sqlQuery("select count(*) as count from insurance_data where " .
432 "pid = " . $iter['pid'] . " and " .
433 "type='primary' and " .
434 "subscriber_lname is not null and " .
435 "subscriber_lname != '' limit 1");
436 $namecolor = ($res['count'] > 0) ?
"black" : "#ff7777";
439 $bgcolor = "#" . (($encount & 1) ?
"ddddff" : "ffdddd");
440 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
445 $ptname = $name['fname'] . " " . $name['lname'];
446 $raw_encounter_date = date("Y-m-d", strtotime($iter['enc_date']));
448 $lhtml .= " <span class=bold><font color='$namecolor'>$ptname" .
449 "</font></span><span class=small> (" . $iter['pid'] . "-" .
450 $iter['encounter'] . ")</span>";
452 // $lhtml .= " <a class=\"link_submit\" href=\"" .
453 // $GLOBALS['form_exit_url'] . "?set_encounter=" .
454 // $iter['encounter'] . "&pid=" . $iter['pid'] . "\">[To Encounter]</a>";
456 $lhtml .= " <a class=\"link_submit\" " .
457 "href=\"javascript:window.toencounter(" . $iter['pid'] .
458 ",'" . addslashes($ptname) . "'," . $iter['encounter'] .
459 ",'$raw_encounter_date')\">[To Encounter]</a>";
461 // $lhtml .= " <a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] .
462 // "/interface/patient_file/summary/demographics_full.php?&pid=" .
463 // $iter['pid'] . "\">[To Demographics]</a>";
465 $lhtml .= " <a class=\"link_submit\" " .
466 "href=\"javascript:window.topatient(" . $iter['pid'] .
467 ")\">[To Demographics]</a>";
469 $lhtml .= "<br />\n";
470 $lhtml .= " <span class=text>Bill: ";
471 $lhtml .= "<select name='claims[" . $this_encounter_id . "][payer]' style='background-color:$bgcolor'>";
473 $query = "SELECT id.provider AS id, id.type, " .
474 "ic.x12_default_partner_id AS ic_x12id, ic.name AS provider " .
475 "FROM insurance_data AS id, insurance_companies AS ic WHERE " .
476 "ic.id = id.provider AND pid = '" . mysql_escape_string($iter['pid']) .
478 $result = sqlStatement($query);
481 $default_x12_partner = $iter['ic_x12id'];
483 while ($row = mysql_fetch_array($result)) {
484 if (strlen($row['provider']) > 0) {
486 // This preserves any existing insurance company selection, which is
487 // important when EOB posting has re-queued for secondary billing.
488 $lhtml .= "<option value=\"" . $row['id'] . "\"";
489 if (($count == 0 && !$iter['payer_id']) ||
$row['id'] == $iter['payer_id']) {
490 $lhtml .= " selected";
491 if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
493 $lhtml .= ">" . $row['type'] . ": " . $row['provider'] . "</option>";
499 $lhtml .= "<option value='-1'>Unassigned</option>\n";
500 $lhtml .= "</select> \n";
501 $lhtml .= "<select name='claims[" . $this_encounter_id . "][partner]' style='background-color:$bgcolor'>";
502 $x = new X12Partner();
503 $partners = $x->_utility_array($x->x12_partner_factory());
504 foreach ($partners as $xid => $xname) {
505 $lhtml .= '<option label="' . $xname . '" value="' . $xid .'"';
506 if ($xid == $default_x12_partner) {
507 $lhtml .= "selected";
509 $lhtml .= '>' . $xname . '</option>';
511 $lhtml .= "</select>";
512 $lhtml .= "<br>\n ".xl("Claim was initiated: ") . $iter['date'];
513 if ($iter['billed'] == 1) {
514 $lhtml .= "<br>\n ".xl("Claim was billed: ") . $iter['bill_date'];
517 if ($iter['bill_process'] == 1) {
518 $lhtml .= "<br>\n ".xl("Claim is queued for processing");
521 if ($iter['bill_process'] == 5) {
522 $lhtml .= "<br>\n ".xl("Claim is queued for printing and processing");
525 if ($iter['bill_process'] == 2) {
526 $lhtml .= "<br>\n ".xl("Claim was processed: ") . $iter['process_date'];
527 $lhtml .= '<br>' . "\n" . ' '.xl("Claim is in file:").' <a href="get_claim_file.php?key=' . $iter['process_file'] .'">' . $iter['process_file'] . '</a> or ';
528 $lhtml .= '<a href="get_claim_file.php?action=print&key=' . $iter['process_file'] .'">Print It</a> or ';
529 $lhtml .= '<a target="_new" href="freebtest.php?format=' . $iter['target'] . '&billkey=' . $iter['pid'] . '-' . $iter['encounter'] . '">'.xl('Run Test').'</a>';
530 $lhtml .= '<input type="hidden" name="claims[' . $this_encounter_id . '][file]" value="' . $iter['process_file'] . '">';
533 if ($iter['bill_process'] == 3) {
534 $lhtml .= "<br>\n ".xl("Claim was processed: ") . $iter['process_date'] . xl(" but there was an error: "). $iter['process_file'];
539 if ($skipping) continue;
543 $rhtml .= "<tr bgcolor='$bgcolor'>\n";
545 $rhtml .= "<td width='50'>";
546 if ($oldcode != $iter['code_type']) {
547 $rhtml .= "<span class=text>" . $iter['code_type'] . ": </span>";
549 $oldcode = $iter['code_type'];
552 // if ($iter['code_type'] == "CPT4" || $iter['code_type'] == "HCPCS") {
553 if ($code_types[$iter['code_type']]['just']) {
554 $js = split(":",$iter['justify']);
556 foreach ($js as $j) {
559 $justify .= " (<b>$j</b>)";
569 $rhtml .= "<td><span class=text>" . $iter{"code"}. "</span>" . '<span style="font-size:8pt;">' . $justify . "</span></td>\n";
570 $rhtml .= '<td align="right"><span style="font-size:8pt;"> ';
571 if ($iter['fee'] > 0) {
572 $rhtml .= '$' . $iter['fee'];
574 $rhtml .= "</span></td>\n";
575 $rhtml .= '<td><span style="font-size:8pt;"> ';
576 $rhtml .= getProviderName($iter['provider_id']);
577 $rhtml .= "</span></td>\n";
578 $rhtml .= '<td width=100> <span style="font-size:8pt;">' . date("Y-m-d",strtotime($iter{"date"})) . "</span></td>\n";
579 if ($iter['authorized'] != 1) {
580 $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";
583 $rhtml .= "<td></td>\n";
585 if ($last_encounter_id != $this_encounter_id) {
586 $rhtml .= "<td><input type='checkbox' value='" . $iter['bill_process'] . "$procstatus' name='claims[" . $this_encounter_id . "][bill]' onclick='set_button_states()'> </td>\n";
589 $rhtml .= "<td></td>\n";
592 $last_encounter_id = $this_encounter_id;
596 while ($rcount < $lcount) {
597 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'> </td></tr>";
600 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
601 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
615 echo "alert('$alertmsg');\n";