tranlsation function xl()
[openemr.git] / interface / billing / billing_report.php
blob859cd7a6efc4d7d2f17da5fb569f82cdc13627be
1 <?
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 $alertmsg = '';
13 if ($_POST['mode'] == 'export') {
14 $sdate = $_POST['from_date'];
15 $edate = $_POST['to_date'];
17 $sql = "SELECT billing.*, concat(pd.fname, ' ', pd.lname) as name from billing "
18 . "join patient_data as pd on pd.pid = billing.pid where billed = '1' and "
19 . "(process_date > '" . mysql_real_escape_string($sdate)
20 . "' or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($sdate) ."') "
21 . "and (process_date < '" . mysql_real_escape_string($edate)
22 . "'or DATE_FORMAT( process_date, '%Y-%m-%d' ) = '" . mysql_real_escape_string($edate) ."') "
23 . "order by pid,encounter";
24 $db = get_db();
25 $results = $db->Execute($sql);
26 $billings = array();
27 if ($results->RecordCount() == 0) {
28 echo "No Bills Found to Include in OFX Export<br>";
30 else {
31 while(!$results->EOF) {
32 $billings[] = $results->fields;
33 $results->MoveNext();
35 $ofx = new OFX($billings);
36 header("Pragma: public");
37 header("Expires: 0");
38 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
39 header("Content-Disposition: attachment; filename=openemr_ofx.ofx");
40 header("Content-Type: text/xml");
41 echo $ofx->get_OFX();
42 exit;
46 if ($_POST['mode'] == 'process') {
47 if (exec("ps x | grep 'process_bills[.]php'")) {
48 $alertmsg = 'Request ignored - claims processing is already running!';
50 else {
51 exec("cd $webserver_root/library/freeb;" .
52 "php -q process_bills.php bill > process_bills.log 2>&1 &");
53 $alertmsg = 'Batch processing initiated; this may take a while.';
57 //global variables:
58 if (!isset($_POST["mode"])) {
59 if (!isset($_POST["from_date"])) {
60 $from_date=date("Y-m-d");
61 } else {
62 $from_date = $_POST["from_date"];
64 if (!isset($_POST["to_date"])) {
65 $to_date = date("Y-m-d");
66 } else {
67 $to_date = $_POST["to_date"];
69 if (!isset($_POST["code_type"])) {
70 $code_type="all";
71 } else {
72 $code_type = $_POST["code_type"];
74 if (!isset($_POST["unbilled"])) {
75 $unbilled = "on";
76 } else {
77 $unbilled = $_POST["unbilled"];
79 if (!isset($_POST["authorized"])) {
80 $my_authorized = "on";
81 } else {
82 $my_authorized = $_POST["authorized"];
84 } else {
85 $from_date = $_POST["from_date"];
86 $to_date = $_POST["to_date"];
87 $code_type = $_POST["code_type"];
88 $unbilled = $_POST["unbilled"];
89 $my_authorized = $_POST["authorized"];
92 $ofrom_date = $from_date;
93 $oto_date = $to_date;
94 $ocode_type = $code_type;
95 $ounbilled = $unbilled;
96 $oauthorized = $my_authorized;
100 <html>
101 <head>
103 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
104 <script>
106 function select_all() {
107 for($i=0;$i < document.update_form.length;$i++) {
108 $name = document.update_form[$i].name;
109 if ($name.substring(0,7) == "claims[" && $name.substring($name.length -6) == "[bill]") {
110 document.update_form[$i].checked = true;
113 set_button_states();
116 function set_button_states() {
117 var f = document.update_form;
118 var count0 = 0;
119 var count1 = 0;
120 var count2 = 0;
121 for($i = 0; $i < f.length; ++$i) {
122 $name = f[$i].name;
123 if ($name.substring(0, 7) == "claims[" && $name.substring($name.length -6) == "[bill]" && f[$i].checked == true) {
124 if (f[$i].value == '0') ++count0;
125 else if (f[$i].value == '1' || f[$i].value == '5') ++count1;
126 else ++count2;
130 var can_generate = (count0 > 0 || count1 > 0 || count2 > 0);
131 var can_mark = (count1 == 0 && (count0 > 0 || count2 > 0));
132 var can_bill = (count0 == 0 && count1 == 0 && count2 > 0);
134 f.bn_hcfa_print.disabled = !can_generate;
135 f.bn_hcfa.disabled = !can_generate;
136 f.bn_ub92_print.disabled = !can_generate;
137 f.bn_ub92.disabled = !can_generate;
138 f.bn_x12.disabled = !can_generate;
139 f.bn_mark.disabled = !can_mark;
140 f.bn_electronic_file.disabled = !can_bill;
143 </script>
144 </head>
145 <body <?echo $top_bg_line;?> topmargin=0 rightmargin=0 leftmargin=2 bottommargin=0 marginwidth=2 marginheight=0>
147 <p style='margin-top:5px;margin-bottom:5px;margin-left:5px'>
149 if ($userauthorized) {
151 <a href="../main/main.php" target=Main><font class=title>Billing Report</font><font class=more> <?echo $tback;?></font></a>
152 <?} else {?>
153 <a href="../main/onotes/office_comments.php" target=Main><font class=title>Billing Report</font><font class=more><?echo $tback;?></font></a>
157 </p>
159 <form name=the_form method=post action=billing_report.php>
160 <input type=hidden name=mode value="change">
161 <table width=100% border="1" cellspacing="0" cellpadding="0">
162 <tr>
163 <td nowrap>&nbsp;<span class=text>From: </span><input type=entry name=from_date size=11 value="<?echo $from_date;?>"></td>
164 <td nowrap>&nbsp;<span class=text>To: </span><input type=entry name=to_date size=11 value="<?echo $to_date;?>">
165 <input type="hidden" name="code_type" value="%"></td>
166 <td nowrap><input type=checkbox name=unbilled <?if ($unbilled == "on") {echo "checked";};?>><span class=text>Show Unbilled Only</span></td>
167 <td nowrap><input type=checkbox name=authorized <?if ($my_authorized == "on") {echo "checked";};?>><span class=text>Show Authorized Only</span></td>
168 <td align='right' width='10%' nowrap>
169 &nbsp;<span class=text><a href="javascript:document.the_form.mode.value='change';document.the_form.submit()" class=link_submit>[Change View]</a>
171 <a href="javascript:document.the_form.mode.value='export';document.the_form.submit()" class=link_submit>[Export OFX]</a></span>&nbsp;
172 </td>
173 </tr>
174 <tr>
175 <td nowrap>&nbsp;<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>[View Printable Report]</a></td>
176 <td nowrap>
177 <?php
178 print '&nbsp;';
179 $acct_config = $GLOBALS['oer_config']['ws_accounting'];
180 if($acct_config['enabled'] == true) {
181 print '<span class=text><a href="javascript:void window.open(\''.$acct_config['url_path'].'\')">[SQL-Ledger]</a></span>';
182 if (acl_check('acct', 'rep')) {
183 print '<span class=text> &nbsp; <a href="javascript:void window.open(\'sl_receipts_report.php\')">[Reports]</a></span>';
185 if (acl_check('acct', 'eob')) {
186 print '<span class=text> &nbsp; <a href="javascript:void window.open(\'sl_eob_search.php\')">[EOBs]</a></span>';
190 </td>
191 <td colspan='2' nowrap>
192 &nbsp;
193 <a href="javascript:document.the_form.mode.value='process';document.the_form.submit()" class="link_submit"
194 title="Process all queued bills to create electronic data (and print if requested)">[Start Batch Processing]</a>
195 &nbsp; <a href='../../library/freeb/process_bills.log' target='_blank' class='link_submit'
196 title='See messages from the last batch processing run'>[view log]</a></span>
197 </td>
198 <td align='right' nowrap>
199 <a href="javascript:select_all()" class="link_submit">[Select All]</a>&nbsp;
200 </td>
201 </tr>
202 </table>
203 </form>
205 <form name=update_form method=post action=billing_process.php>
207 <center>
208 <input type="submit" name="bn_hcfa_print" value="Queue HCFA &amp; Print" title="Queue for HCFA batch processing and printing">
209 <input type="submit" name="bn_hcfa" value="Queue HCFA" title="Queue for HCFA batch processing">
210 <input type="submit" name="bn_ub92_print" value="Queue UB92 &amp; Print" title="Queue for UB-92 batch processing and printing">
211 <input type="submit" name="bn_ub92" value="Queue UB92" title="Queue for UB-92 batch processing">
212 <input type="submit" name="bn_x12" value="Queue X12" title="Queue for X12 batch processing">
213 <input type="submit" name="bn_mark" value="Mark as Cleared" title="Post to accounting and mark as billed">
214 <input type="submit" name="bn_electronic_file" value="Make Electronic Batch &amp; Clear" title="Download billing file, post to accounting and mark as billed">
215 </center>
217 <input type=hidden name=mode value="bill">
218 <input type=hidden name=authorized value="<?echo $my_authorized;?>">
219 <input type=hidden name=unbilled value="<?echo $unbilled;?>">
220 <input type=hidden name=code_type value="%">
221 <input type=hidden name=to_date value="<?echo $to_date;?>">
222 <input type=hidden name=from_date value="<?echo $from_date;?>">
224 if ($my_authorized == "on" ) {
225 $my_authorized = "1";
226 } else {
227 $my_authorized = "%";
230 if ($unbilled == "on") {
231 $unbilled = "0";
232 } else {
233 $unbilled = "%";
236 <input type=hidden name=bill_list value="<?
237 $list = getBillsListBetween($from_date,$to_date,$my_authorized,$unbilled,"%");
238 print $list;
239 ?>">
240 <!-- new form for uploading -->
241 <?php
242 if (!isset($_POST["mode"])) {
243 if (!isset($_POST["from_date"])) {
244 $from_date=date("Y-m-d");
245 } else {
246 $from_date = $_POST["from_date"];
248 if (!isset($_POST["to_date"])) {
249 $to_date = date("Y-m-d");
250 } else {
251 $to_date = $_POST["to_date"];
253 if (!isset($_POST["code_type"])) {
254 $code_type="all";
255 } else {
256 $code_type = $_POST["code_type"];
258 if (!isset($_POST["unbilled"])) {
259 $unbilled = "on";
260 } else {
261 $unbilled = $_POST["unbilled"];
263 if (!isset($_POST["authorized"])) {
264 $my_authorized = "on";
265 } else {
266 $my_authorized = $_POST["authorized"];
268 } else {
269 $from_date = $_POST["from_date"];
270 $to_date = $_POST["to_date"];
271 $code_type = $_POST["code_type"];
272 $unbilled = $_POST["unbilled"];
273 $my_authorized = $_POST["authorized"];
275 if ($my_authorized == "on" ) {
276 $my_authorized = "1";
277 } else {
278 $my_authorized = "%";
281 if ($unbilled == "on") {
282 $unbilled = "0";
283 } else {
284 $unbilled = "%";
289 if (isset($_POST["mode"]) && $_POST["mode"] == "bill") {
290 billCodesList($list);
295 <table border="0" cellspacing="0" cellpadding="0" width="100%">
298 if ($ret = getBillsBetween($from_date,$to_date,$my_authorized,$unbilled,"%")) {
299 $loop = 0;
300 $oldcode = "";
301 $last_encounter_id = "";
302 $lhtml = "";
303 $rhtml = "";
304 $lcount = 0;
305 $rcount = 0;
306 $bgcolor = "";
307 $skipping = FALSE;
309 foreach ($ret as $iter) {
310 $this_encounter_id = $iter['pid'] . "-" . $iter['encounter'];
312 if ($last_encounter_id != $this_encounter_id) {
313 if ($lhtml) {
314 while ($rcount < $lcount) {
315 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'>&nbsp;</td></tr>";
316 ++$rcount;
318 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
319 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n\n";
322 $lhtml = "";
323 $rhtml = "";
325 // If there are ANY unauthorized items in this encounter and this is
326 // the normal case of viewing only authorized billing, then skip the
327 // entire encounter.
329 $skipping = FALSE;
330 if ($my_authorized == '1') {
331 $res = sqlQuery("select count(*) as count from billing where " .
332 "encounter = '" . $iter['encounter'] . "' and " .
333 "pid='" . $iter['pid'] . "' and " .
334 "activity = 1 and authorized = 0");
335 if ($res['count'] > 0) {
336 $skipping = TRUE;
337 $last_encounter_id = $this_encounter_id;
338 continue;
342 $name = getPatientData($iter['pid'], "fname, mname, lname");
344 # Check if patient has primary insurance and a subscriber exists for it.
345 # If not we will highlight their name in red.
346 # TBD: more checking here.
348 $res = sqlQuery("select count(*) as count from insurance_data where " .
349 "pid = " . $iter['pid'] . " and " .
350 "type='primary' and " .
351 "subscriber_lname is not null and " .
352 "subscriber_lname != '' limit 1");
353 $namecolor = ($res['count'] > 0) ? "black" : "#ff7777";
355 ++$encount;
356 $bgcolor = "#" . (($encount & 1) ? "ddddff" : "ffdddd");
357 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
358 $lcount = 3;
359 $rcount = 0;
360 $oldcode = "";
362 $lhtml .= "&nbsp;<span class=bold><font color='$namecolor'>". $name['fname'] . "&nbsp;" . $name['lname'] . "</font></span><span class=small>&nbsp;(" . $iter['pid'] . "-" . $iter['encounter'] . ")</span>";
363 $lhtml .= "&nbsp;&nbsp;&nbsp;<a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] ."/interface/patient_file/encounter/patient_encounter.php?set_encounter=" . $iter['encounter'] . "&pid=" . $iter['pid'] . "\">[To&nbsp;Encounter]</a>";
364 $lhtml .= "&nbsp;&nbsp;&nbsp;<a class=\"link_submit\" href=\"" . $GLOBALS['webroot'] ."/interface/patient_file/summary/demographics_full.php?&pid=" . $iter['pid'] . "\">[To&nbsp;Demographics]</a>";
365 $lhtml .= "<br />\n";
366 $lhtml .= "&nbsp;<span class=text>Bill: ";
367 $lhtml .= "<select name='claims[" . $this_encounter_id . "][payer]' style='background-color:$bgcolor'>";
368 $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";
370 $result = sqlStatement($query);
371 $count = 0;
372 $default_x12_partner = $iter['ic_x12id'];
374 while ($row = mysql_fetch_array($result)) {
375 if (strlen($row['provider']) > 0) {
376 if ($count == 0) {
377 $lhtml .= "<option value=\"" .$row['id'] . "\" selected>" . $row['type'] . ": " . $row['provider']. "</option>";
378 if (!is_numeric($default_x12_partner)) $default_x12_partner = $row['ic_x12id'];
380 else {
381 $lhtml .= "<option value=\"" . $row['id'] . "\">" . $row['type'] . ": " . $row['provider']. "</option>";
384 $count++;
386 $lhtml .= "<option value='-1'>Unassigned</option>\n";
387 $lhtml .= "</select>&nbsp;&nbsp;\n";
388 $lhtml .= "<select name='claims[" . $this_encounter_id . "][partner]' style='background-color:$bgcolor'>";
389 $x = new X12Partner();
390 $partners = $x->_utility_array($x->x12_partner_factory());
391 foreach ($partners as $xid => $xname) {
392 $lhtml .= '<option label="' . $xname . '" value="' . $xid .'"';
393 if ($xid == $default_x12_partner) {
394 $lhtml .= "selected";
396 $lhtml .= '>' . $xname . '</option>';
398 $lhtml .= "</select>";
399 $lhtml .= "<br>\n&nbsp;Claim was initiated: " . $iter['date'];
400 if ($iter['billed'] == 1) {
401 $lhtml .= "<br>\n&nbsp;Claim was billed: " . $iter['bill_date'];
402 ++$lcount;
404 if ($iter['bill_process'] == 1) {
405 $lhtml .= "<br>\n&nbsp;Claim is queued for processing";
406 ++$lcount;
408 if ($iter['bill_process'] == 5) {
409 $lhtml .= "<br>\n&nbsp;Claim is queued for printing and processing";
410 ++$lcount;
412 if ($iter['bill_process'] == 2) {
413 $lhtml .= "<br>\n&nbsp;Claim was processed: " . $iter['process_date'];
414 $lhtml .= '<br>' . "\n" . '&nbsp;Claim is in file: <a href="get_claim_file.php?key=' . $iter['process_file'] .'">' . $iter['process_file'] . '</a> or ';
415 $lhtml .= '<a href="get_claim_file.php?action=print&key=' . $iter['process_file'] .'">Print It</a> or ';
416 $lhtml .= '<a target="_new" href="freebtest.php?format=' . $iter['target'] . '&billkey=' . $iter['pid'] . '-' . $iter['encounter'] . '">Run Test</a>';
417 $lhtml .= '<input type="hidden" name="claims[' . $this_encounter_id . '][file]" value="' . $iter['process_file'] . '">';
418 $lcount += 2;
420 if ($iter['bill_process'] == 3) {
421 $lhtml .= "<br>\n&nbsp;Claim was processed: " . $iter['process_date'] . " but there was an error: ". $iter['process_file'];
422 ++$lcount;
426 if ($skipping) continue;
428 ++$rcount;
429 if ($rhtml) {
430 $rhtml .= "<tr bgcolor='$bgcolor'>\n";
432 $rhtml .= "<td width='50'>";
433 if ($oldcode != $iter['code_type']) {
434 $rhtml .= "<span class=text>" . $iter['code_type'] . ": </span>";
436 $oldcode = $iter['code_type'];
437 $rhtml .= "</td>\n";
438 $justify = "";
439 // if ($iter['code_type'] == "CPT4" || $iter['code_type'] == "HCPCS") {
440 if ($code_types[$iter['code_type']]['just']) {
441 $js = split(":",$iter['justify']);
442 $counter = 0;
443 foreach ($js as $j) {
444 if(!empty($j)) {
445 if ($counter == 0) {
446 $justify .= " (<b>$j</b>)";
448 else {
449 $justify .= " ($j)";
451 $counter++;
456 $rhtml .= "<td><span class=text>" . $iter{"code"}. "</span>" . '<span style="font-size:8pt;">' . $justify . "</span></td>\n";
457 $rhtml .= '<td align="right"><span style="font-size:8pt;">&nbsp;&nbsp;&nbsp;';
458 if ($iter['fee'] > 0) {
459 $rhtml .= '$' . $iter['fee'];
461 $rhtml .= "</span></td>\n";
462 $rhtml .= '<td><span style="font-size:8pt;">&nbsp;&nbsp;&nbsp;';
463 $rhtml .= getProviderName($iter['provider_id']);
464 $rhtml .= "</span></td>\n";
465 $rhtml .= '<td width=100>&nbsp;&nbsp;&nbsp;<span style="font-size:8pt;">' . date("Y-m-d",strtotime($iter{"date"})) . "</span></td>\n";
466 if ($iter['authorized'] != 1) {
467 $rhtml .= "<td><span class=alert>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";
469 else {
470 $rhtml .= "<td></td>\n";
472 if ($last_encounter_id != $this_encounter_id) {
473 $rhtml .= "<td><input type='checkbox' value='" . $iter['bill_process'] . "$procstatus' name='claims[" . $this_encounter_id . "][bill]' onclick='set_button_states()'>&nbsp;</td>\n";
475 else {
476 $rhtml .= "<td></td>\n";
478 $rhtml .= "</tr>\n";
479 $last_encounter_id = $this_encounter_id;
482 if ($lhtml) {
483 while ($rcount < $lcount) {
484 $rhtml .= "<tr bgcolor='$bgcolor'><td colspan='7'>&nbsp;</td></tr>";
485 ++$rcount;
487 echo "<tr bgcolor='$bgcolor'>\n<td rowspan='$rcount' valign='top'>\n$lhtml</td>$rhtml\n";
488 echo "<tr bgcolor='$bgcolor'><td colspan='8' height='5'></td></tr>\n";
494 </table>
496 </form>
497 <script>
498 set_button_states();
501 if ($alertmsg) {
502 echo "alert('$alertmsg');\n";
506 </script>
507 </body>
508 </html>