Interim autoloaded library/classes via composer classmap, take 4. (#422)
[openemr.git] / interface / billing / sl_eob_search.php
blob6912bbe1ccd2a378651e7def5d3152c2763201b6
1 <?php
2 /**
3 * This the first of two pages to support posting of EOBs.
4 * The second is sl_eob_invoice.php.
5 * Windows compatibility and statement downloading:
6 * 2009 Bill Cernansky and Tony McCormick [mi-squared.com]
8 * Copyright (C) 2005-2010 Rod Roark <rod@sunsetsystems.com>
10 * LICENSE: This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
21 * @package OpenEMR
22 * @author Rod Roark <rod@sunsetsystems.com>
23 * @author Roberto Vasquez <robertogagliotta@gmail.com>
24 * @link http://www.open-emr.org
26 require_once("../globals.php");
27 require_once("$srcdir/patient.inc");
28 require_once("$srcdir/invoice_summary.inc.php");
29 require_once("$srcdir/appointments.inc.php");
30 require_once($GLOBALS['OE_SITE_DIR'] . "/statement.inc.php");
31 require_once("$srcdir/parse_era.inc.php");
32 require_once("$srcdir/sl_eob.inc.php");
33 require_once("$srcdir/formatting.inc.php");
34 require_once("$srcdir/api.inc");
35 require_once("$srcdir/forms.inc");
36 require_once("$srcdir/../controllers/C_Document.class.php");
37 require_once("$srcdir/documents.php");
38 require_once("$srcdir/options.inc.php");
39 require_once("$srcdir/acl.inc");
41 $DEBUG = 0; // set to 0 for production, 1 to test
43 $alertmsg = '';
44 $where = '';
45 $eraname = '';
46 $eracount = 0;
48 // This is called back by parse_era() if we are processing X12 835's.
49 function era_callback(&$out) {
50 global $where, $eracount, $eraname;
51 // print_r($out); // debugging
52 ++$eracount;
53 // $eraname = $out['isa_control_number'];
54 $eraname = $out['gs_date'] . '_' . ltrim($out['isa_control_number'], '0') .
55 '_' . ltrim($out['payer_id'], '0');
56 list($pid, $encounter, $invnumber) = slInvoiceNumber($out);
58 if ($pid && $encounter) {
59 if ($where) $where .= ' OR ';
60 $where .= "( f.pid = '$pid' AND f.encounter = '$encounter' )";
64 function bucks($amount) {
65 if ($amount) echo oeFormatMoney($amount);
68 // Upload a file to the client's browser
70 function upload_file_to_client($file_to_send) {
71 header("Pragma: public");
72 header("Expires: 0");
73 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
74 header("Content-Type: application/force-download");
75 header("Content-Length: " . filesize($file_to_send));
76 header("Content-Disposition: attachment; filename=" . basename($file_to_send));
77 header("Content-Description: File Transfer");
78 readfile($file_to_send);
79 // flush the content to the browser. If you don't do this, the text from the subsequent
80 // output from this script will be in the file instead of sent to the browser.
81 flush();
82 exit(); //added to exit from process properly in order to stop bad html code -ehrlive
83 // sleep one second to ensure there's no follow-on.
84 sleep(1);
86 function upload_file_to_client_pdf($file_to_send) {
87 //Function reads a HTML file and converts to pdf.
89 global $STMT_TEMP_FILE_PDF;
90 global $srcdir;
92 if ($GLOBALS['statement_appearance'] == '1') {
93 require_once("$srcdir/html2pdf/vendor/autoload.php");
94 $pdf2 = new HTML2PDF ($GLOBALS['pdf_layout'],
95 $GLOBALS['pdf_size'],
96 $GLOBALS['pdf_language'],
97 true, // default unicode setting is true
98 'UTF-8', // default encoding setting is UTF-8
99 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin']),
100 $_SESSION['language_direction'] == 'rtl' ? true : false
102 ob_start();
103 echo readfile($file_to_send, "r");//this file contains the HTML to be converted to pdf.
104 //echo $file;
105 $content = ob_get_clean();
107 // Fix a nasty html2pdf bug - it ignores document root!
108 global $web_root, $webserver_root;
109 $i = 0;
110 $wrlen = strlen($web_root);
111 $wsrlen = strlen($webserver_root);
112 while (true) {
113 $i = stripos($content, " src='/", $i + 1);
114 if ($i === false) break;
115 if (substr($content, $i+6, $wrlen) === $web_root &&
116 substr($content, $i+6, $wsrlen) !== $webserver_root) {
117 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
120 $pdf2->WriteHTML($content);
121 $temp_filename = $STMT_TEMP_FILE_PDF;
122 $content_pdf = $pdf2->Output($STMT_TEMP_FILE_PDF,'F');
123 } else {
124 $pdf = new Cezpdf('LETTER');//pdf creation starts
125 $pdf->ezSetMargins(45,9,36,10);
126 $pdf->selectFont('Courier');
127 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
128 $countline=1;
129 $file = fopen($file_to_send, "r");//this file contains the text to be converted to pdf.
130 while(!feof($file)) {
131 $OneLine=fgets($file);//one line is read
132 if(stristr($OneLine, "\014") == true && !feof($file))//form feed means we should start a new page.
134 $pdf->ezNewPage();
135 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
136 str_replace("\014", "", $OneLine);
139 if(stristr($OneLine, 'REMIT TO') == true || stristr($OneLine, 'Visit Date') == true || stristr($OneLine, 'Future Appointments') == true || stristr($OneLine, 'Current') == true)//lines are made bold when 'REMIT TO' or 'Visit Date' is there.
140 $pdf->ezText('<b>'.$OneLine.'</b>', 12, array('justification' => 'left', 'leading' => 6));
141 else
142 $pdf->ezText($OneLine, 12, array('justification' => 'left', 'leading' => 6));
143 $countline++;
145 $fh = @fopen($STMT_TEMP_FILE_PDF, 'w');//stored to a pdf file
146 if ($fh) {
147 fwrite($fh, $pdf->ezOutput());
148 fclose($fh);
151 header("Pragma: public");//this section outputs the pdf file to browser
152 header("Expires: 0");
153 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
154 header("Content-Type: application/force-download");
155 header("Content-Length: " . filesize($STMT_TEMP_FILE_PDF));
156 header("Content-Disposition: attachment; filename=" . basename($STMT_TEMP_FILE_PDF));
157 header("Content-Description: File Transfer");
158 readfile($STMT_TEMP_FILE_PDF);
159 // flush the content to the browser. If you don't do this, the text from the subsequent
160 // output from this script will be in the file instead of sent to the browser.
161 flush();
162 exit(); //added to exit from process properly in order to stop bad html code -ehrlive
163 // sleep one second to ensure there's no follow-on.
164 sleep(1);
168 $today = date("Y-m-d");
169 // Print or download statements if requested.
171 if (($_POST['form_print'] || $_POST['form_download'] || $_POST['form_pdf']) && $_POST['form_cb']) {
173 $fhprint = fopen($STMT_TEMP_FILE, 'w');
174 $sqlBindArray = array();
175 $where = "";
176 foreach ($_POST['form_cb'] as $key => $value) {
177 $where .= " OR f.id = ?";
178 array_push($sqlBindArray, $key);
180 $where = substr($where, 4);
182 $res = sqlStatement("SELECT " .
183 "f.id, f.date, f.pid, f.encounter, f.stmt_count, f.last_stmt_date, f.last_level_closed, f.last_level_billed, f.billing_note as enc_billing_note, " .
184 "p.fname, p.mname, p.lname, p.street, p.city, p.state, p.postal_code, p.billing_note as pat_billing_note " .
185 "FROM form_encounter AS f, patient_data AS p " .
186 "WHERE ( $where ) AND " .
187 "p.pid = f.pid " .
188 "ORDER BY p.lname, p.fname, f.pid, f.date, f.encounter", $sqlBindArray);
190 $stmt = array();
191 $stmt_count = 0;
193 // This loops once for each invoice/encounter.
195 while ($row = sqlFetchArray($res)) {
196 $svcdate = substr($row['date'], 0, 10);
197 $duedate = $svcdate; // TBD?
198 $duncount = $row['stmt_count'];
199 $enc_note = $row['enc_billing_note'];
201 // If this is a new patient then print the pending statement
202 // and start a new one. This is an associative array:
204 // cid = same as pid
205 // pid = OpenEMR patient ID
206 // patient = patient name
207 // amount = total amount due
208 // adjust = adjustments (already applied to amount)
209 // duedate = due date of the oldest included invoice
210 // age = number of days from duedate to today
211 // to = array of addressee name/address lines
212 // lines = array of:
213 // dos = date of service "yyyy-mm-dd"
214 // desc = description
215 // amount = charge less adjustments
216 // paid = amount paid
217 // notice = 1 for first notice, 2 for second, etc.
218 // detail = array of details, see invoice_summary.inc.php
220 if ($stmt['cid'] != $row['pid']) {
221 if (!empty($stmt)) ++$stmt_count;
222 $stmt['cid'] = $row['pid'];
223 $stmt['pid'] = $row['pid'];
224 $stmt['dun_count'] = $row['stmt_count'];
225 $stmt['bill_note'] = $row['pat_billing_note'];
226 $stmt['enc_bill_note'] = $row['enc_billing_note'];
227 $stmt['bill_level'] = $row['last_level_billed'];
228 $stmt['level_closed'] = $row['last_level_closed'];
229 $stmt['patient'] = $row['fname'] . ' ' . $row['lname'];
230 $stmt['encounter'] = $row['encounter'];
231 #If you use the field in demographics layout called
232 #guardiansname this will allow you to send statements to the parent
233 #of a child or a guardian etc
234 if(strlen($row['guardiansname']) == 0) {
235 $stmt['to'] = array($row['fname'] . ' ' . $row['lname']);
237 else
239 $stmt['to'] = array($row['guardiansname']);
241 if ($row['street']) $stmt['to'][] = $row['street'];
242 $stmt['to'][] = $row['city'] . ", " . $row['state'] . " " . $row['postal_code'];
243 $stmt['lines'] = array();
244 $stmt['amount'] = '0.00';
245 $stmt['ins_paid'] = 0;
246 $stmt['today'] = $today;
247 $stmt['duedate'] = $duedate;
248 } else {
249 // Report the oldest due date.
250 if ($duedate < $stmt['duedate']) {
251 $stmt['duedate'] = $duedate;
255 // Recompute age at each invoice.
256 $stmt['age'] = round((strtotime($today) - strtotime($stmt['duedate'])) / (24 * 60 * 60));
258 $invlines = ar_get_invoice_summary($row['pid'], $row['encounter'], true);
259 foreach ($invlines as $key => $value) {
260 $line = array();
261 $line['dos'] = $svcdate;
262 if ($GLOBALS['use_custom_statement']) {
263 $line['desc'] = ($key == 'CO-PAY') ? "Patient Payment" : $value['code_text'];
264 } else {
265 $line['desc'] = ($key == 'CO-PAY') ? "Patient Payment" : "Procedure $key";
267 $line['amount'] = sprintf("%.2f", $value['chg']);
268 $line['adjust'] = sprintf("%.2f", $value['adj']);
269 $line['paid'] = sprintf("%.2f", $value['chg'] - $value['bal']);
270 $line['notice'] = $duncount + 1;
271 $line['detail'] = $value['dtl'];
272 $stmt['lines'][] = $line;
273 $stmt['amount'] = sprintf("%.2f", $stmt['amount'] + $value['bal']);
274 $stmt['ins_paid'] = $stmt['ins_paid'] + $value['ins'];
277 // Record that this statement was run.
278 if (! $DEBUG && ! $_POST['form_without']) {
279 sqlStatement("UPDATE form_encounter SET " .
280 "last_stmt_date = '$today', stmt_count = stmt_count + 1 " .
281 "WHERE id = " . $row['id']);
283 fwrite($fhprint, make_statement($stmt));
285 } // end while
287 if (!empty($stmt)) ++$stmt_count;
288 fclose($fhprint);
289 sleep(1);
291 // Download or print the file, as selected
292 if ($_POST['form_download']) {
293 upload_file_to_client($STMT_TEMP_FILE);
294 } elseif ($_POST['form_pdf']) {
295 upload_file_to_client_pdf($STMT_TEMP_FILE);
296 } else { // Must be print!
297 if ($DEBUG) {
298 $alertmsg = xl("Printing skipped; see test output in") .' '. $STMT_TEMP_FILE;
299 } else {
300 exec("$STMT_PRINT_CMD $STMT_TEMP_FILE");
301 if ($_POST['form_without']) {
302 $alertmsg = xl('Now printing') .' '. $stmt_count .' '. xl('statements; invoices will not be updated.');
303 } else {
304 $alertmsg = xl('Now printing') .' '. $stmt_count .' '. xl('statements and updating invoices.');
306 } // end not debug
307 } // end not form_download
308 } // end statements requested
310 <html>
311 <head>
312 <?php html_header_show(); ?>
313 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
314 <title><?php xl('EOB Posting - Search','e'); ?></title>
315 <script type="text/javascript" src="../../library/textformat.js"></script>
317 <script language="JavaScript">
319 var mypcc = '1';
321 function checkAll(checked) {
322 var f = document.forms[0];
323 for (var i = 0; i < f.elements.length; ++i) {
324 var ename = f.elements[i].name;
325 if (ename.indexOf('form_cb[') == 0)
326 f.elements[i].checked = checked;
330 function npopup(pid) {
331 window.open('sl_eob_patient_note.php?patient_id=' + pid, '_blank', 'width=500,height=250,resizable=1');
332 return false;
335 </script>
337 </head>
339 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
340 <center>
342 <form method='post' action='sl_eob_search.php' enctype='multipart/form-data'>
344 <table border='0' cellpadding='5' cellspacing='0'>
345 <tr>
347 <?php
348 // Identify the payer to support resumable posting sessions.
349 echo " <td>\n";
350 echo " " . xl('Payer') . ":\n";
351 echo " </td>\n";
352 echo " <td>\n";
353 $insurancei = getInsuranceProviders();
354 echo " <select name='form_payer_id'>\n";
355 echo " <option value='0'>-- " . xl('Patient') . " --</option>\n";
356 foreach ($insurancei as $iid => $iname) {
357 echo "<option value='$iid'";
358 if ($iid == $_POST['form_payer_id']) echo " selected";
359 echo ">" . $iname . "</option>\n";
361 echo " </select>\n";
362 echo " </td>\n";
365 <td>
366 <?php xl('Source:','e'); ?>
367 </td>
368 <td>
369 <input type='text' name='form_source' size='10' value='<?php echo $_POST['form_source']; ?>'
370 title='<?php xl("A check number or claim number to identify the payment","e"); ?>'>
371 </td>
372 <td>
373 <?php xl('Pay Date:','e'); ?>
374 </td>
375 <td>
376 <input type='text' name='form_paydate' size='10' value='<?php echo $_POST['form_paydate']; ?>'
377 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
378 title='<?php xl("Date of payment yyyy-mm-dd","e"); ?>'>
379 </td>
381 <td>
382 <?php xl('Deposit Date:','e'); ?>
383 </td>
384 <td>
385 <input type='text' name='form_deposit_date' size='10' value='<?php echo $_POST['form_deposit_date']; ?>'
386 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
387 title='<?php xl("Date of bank deposit yyyy-mm-dd","e"); ?>'>
388 </td>
390 <td>
391 <?php xl('Amount:','e'); ?>
392 </td>
393 <td>
394 <input type='text' name='form_amount' size='10' value='<?php echo $_POST['form_amount']; ?>'
395 title='<?php xl("Paid amount that you will allocate","e"); ?>'>
396 </td>
397 <td align='right'>
398 <a href='sl_eob_help.php' target='_blank'><?php xl('Help','e'); ?></a>
399 </td>
401 </tr>
402 </table>
404 <table border='0' cellpadding='5' cellspacing='0'>
406 <tr bgcolor='#ddddff'>
407 <td>
408 <?php xl('Name:','e'); ?>
409 </td>
410 <td>
411 <input type='text' name='form_name' size='10' value='<?php echo $_POST['form_name']; ?>'
412 title='<?php xl("Any part of the patient name, or \"last,first\", or \"X-Y\"","e"); ?>'>
413 </td>
414 <td>
415 <?php xl('Chart ID:','e'); ?>
416 </td>
417 <td>
418 <input type='text' name='form_pid' size='10' value='<?php echo $_POST['form_pid']; ?>'
419 title='<?php xl("Patient chart ID","e"); ?>'>
420 </td>
421 <td>
422 <?php xl('Encounter:','e'); ?>
423 </td>
424 <td>
425 <input type='text' name='form_encounter' size='10' value='<?php echo $_POST['form_encounter']; ?>'
426 title='<?php xl("Encounter number","e"); ?>'>
427 </td>
428 <td>
429 <?php xl('Svc Date:','e'); ?>
430 </td>
431 <td>
432 <input type='text' name='form_date' size='10' value='<?php echo $_POST['form_date']; ?>'
433 title='<?php xl("Date of service mm/dd/yyyy","e"); ?>'>
434 </td>
435 <td>
436 <?php xl('To:','e'); ?>
437 </td>
438 <td>
439 <input type='text' name='form_to_date' size='10' value='<?php echo $_POST['form_to_date']; ?>'
440 title='<?php xl("Ending DOS mm/dd/yyyy if you wish to enter a range","e"); ?>'>
441 </td>
442 <td>
443 <select name='form_category'>
444 <?php
445 foreach (array(xl('Open'), xl('All'), xl('Due Pt'), xl('Due Ins')) as $value) {
446 echo " <option value='$value'";
447 if ($_POST['form_category'] == $value) echo " selected";
448 echo ">$value</option>\n";
451 </select>
452 </td>
453 <td>
454 <input type='submit' name='form_search' value='<?php xl("Search","e"); ?>'>
455 </td>
456 </tr>
458 <!-- Support for X12 835 upload -->
459 <tr bgcolor='#ddddff'>
460 <td colspan='12'>
461 <?php xl('Or upload ERA file:','e'); ?>
462 <input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
463 <input name="form_erafile" type="file" />
464 </td>
465 </tr>
467 <tr>
468 <td height="1" colspan="10">
469 </td>
470 </tr>
472 </table>
474 <?php
475 if ($_POST['form_search'] || $_POST['form_print']) {
476 $form_name = trim($_POST['form_name']);
477 $form_pid = trim($_POST['form_pid']);
478 $form_encounter = trim($_POST['form_encounter']);
479 $form_date = fixDate($_POST['form_date'], "");
480 $form_to_date = fixDate($_POST['form_to_date'], "");
482 $where = "";
484 // Handle X12 835 file upload.
486 if ($_FILES['form_erafile']['size']) {
487 $tmp_name = $_FILES['form_erafile']['tmp_name'];
489 // Handle .zip extension if present. Probably won't work on Windows.
490 if (strtolower(substr($_FILES['form_erafile']['name'], -4)) == '.zip') {
491 rename($tmp_name, "$tmp_name.zip");
492 exec("unzip -p $tmp_name.zip > $tmp_name");
493 unlink("$tmp_name.zip");
496 echo "<!-- Notes from ERA upload processing:\n";
497 $alertmsg .= parse_era($tmp_name, 'era_callback');
498 echo "-->\n";
499 $erafullname = $GLOBALS['OE_SITE_DIR'] . "/era/$eraname.edi";
501 if (is_file($erafullname)) {
502 $alertmsg .= "Warning: Set $eraname was already uploaded ";
503 if (is_file($GLOBALS['OE_SITE_DIR'] . "/era/$eraname.html"))
504 $alertmsg .= "and processed. ";
505 else
506 $alertmsg .= "but not yet processed. ";
508 rename($tmp_name, $erafullname);
509 } // End 835 upload
511 if ($eracount) {
512 // Note that parse_era() modified $eracount and $where.
513 if (! $where) $where = '1 = 2';
515 else {
516 if ($form_name) {
517 if ($where) $where .= " AND ";
518 // Allow the last name to be followed by a comma and some part of a first name.
519 if (preg_match('/^(.*\S)\s*,\s*(.*)/', $form_name, $matches)) {
520 $where .= "p.lname LIKE '" . $matches[1] . "%' AND p.fname LIKE '" . $matches[2] . "%'";
521 // Allow a filter like "A-C" on the first character of the last name.
522 } else if (preg_match('/^(\S)\s*-\s*(\S)$/', $form_name, $matches)) {
523 $tmp = '1 = 2';
524 while (ord($matches[1]) <= ord($matches[2])) {
525 $tmp .= " OR p.lname LIKE '" . $matches[1] . "%'";
526 $matches[1] = chr(ord($matches[1]) + 1);
528 $where .= "( $tmp ) ";
529 } else {
530 $where .= "p.lname LIKE '%$form_name%'";
533 if ($form_pid) {
534 if ($where) $where .= " AND ";
535 $where .= "f.pid = '$form_pid'";
537 if ($form_encounter) {
538 if ($where) $where .= " AND ";
539 $where .= "f.encounter = '$form_encounter'";
541 if ($form_date) {
542 if ($where) $where .= " AND ";
543 if ($form_to_date) {
544 $where .= "f.date >= '$form_date' AND f.date <= '$form_to_date'";
546 else {
547 $where .= "f.date = '$form_date'";
550 if (! $where) {
551 if ($_POST['form_category'] == 'All') {
552 die(xl("At least one search parameter is required if you select All."));
553 } else {
554 $where = "1 = 1";
559 // Notes that as of release 4.1.1 the copays are stored
560 // in the ar_activity table marked with a PCP in the account_code column.
561 $query = "SELECT f.id, f.pid, f.encounter, f.date, " .
562 "f.last_level_billed, f.last_level_closed, f.last_stmt_date, f.stmt_count, " .
563 "p.fname, p.mname, p.lname, p.pubpid, p.billing_note, " .
564 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
565 "b.pid = f.pid AND b.encounter = f.encounter AND " .
566 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
567 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
568 "a.pid = f.pid AND a.encounter = f.encounter AND a.payer_type = 0 AND a.account_code = 'PCP')*-1 AS copays, " .
569 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
570 "a.pid = f.pid AND a.encounter = f.encounter AND a.account_code != 'PCP') AS payments, " .
571 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
572 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
573 "FROM form_encounter AS f " .
574 "JOIN patient_data AS p ON p.pid = f.pid " .
575 "WHERE $where " .
576 "ORDER BY p.lname, p.fname, p.mname, f.pid, f.encounter";
578 // Note that unlike the SQL-Ledger case, this query does not weed
579 // out encounters that are paid up. Also the use of sub-selects
580 // will require MySQL 4.1 or greater.
582 // echo "<!-- $query -->\n"; // debugging
584 $t_res = sqlStatement($query);
586 $num_invoices = sqlNumRows($t_res);
587 if ($eracount && $num_invoices != $eracount) {
588 $alertmsg .= "Of $eracount remittances, there are $num_invoices " .
589 "matching encounters in OpenEMR. ";
593 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
595 <tr bgcolor="#dddddd">
596 <td class="dehead">
597 &nbsp;<?php xl('Patient','e'); ?>
598 </td>
599 <td class="dehead">
600 &nbsp;<?php xl('Invoice','e'); ?>
601 </td>
602 <td class="dehead">
603 &nbsp;<?php xl('Svc Date','e'); ?>
604 </td>
605 <td class="dehead">
606 &nbsp;<?php xl('Last Stmt','e'); ?>
607 </td>
608 <td class="dehead" align="right">
609 <?php xl('Charge','e'); ?>&nbsp;
610 </td>
611 <td class="dehead" align="right">
612 <?php xl('Adjust','e'); ?>&nbsp;
613 </td>
614 <td class="dehead" align="right">
615 <?php xl('Paid','e'); ?>&nbsp;
616 </td>
617 <td class="dehead" align="right">
618 <?php xl('Balance','e'); ?>&nbsp;
619 </td>
620 <td class="dehead" align="center">
621 <?php xl('Prv','e'); ?>
622 </td>
623 <?php if (!$eracount) { ?>
624 <td class="dehead" align="left">
625 <?php xl('Sel','e'); ?>
626 </td>
627 <?php } ?>
628 </tr>
630 <?php
631 $orow = -1;
633 while ($row = sqlFetchArray($t_res)) {
634 $balance = sprintf("%.2f", $row['charges'] + $row['copays'] - $row['payments'] - $row['adjustments']);
636 if ($_POST['form_category'] != 'All' && $eracount == 0 && $balance == 0) continue;
638 // $duncount was originally supposed to be the number of times that
639 // the patient was sent a statement for this invoice.
641 $duncount = $row['stmt_count'];
643 // But if we have not yet billed the patient, then compute $duncount as a
644 // negative count of the number of insurance plans for which we have not
645 // yet closed out insurance.
647 if (! $duncount) {
648 for ($i = 1; $i <= 3 && arGetPayerID($row['pid'], $row['date'], $i); ++$i) ;
649 $duncount = $row['last_level_closed'] + 1 - $i;
652 $isdueany = ($balance > 0);
654 // An invoice is now due from the patient if money is owed and we are
655 // not waiting for insurance to pay.
657 $isduept = ($duncount >= 0 && $isdueany) ? " checked" : "";
659 // Skip invoices not in the desired "Due..." category.
661 if (substr($_POST['form_category'], 0, 3) == 'Due' && !$isdueany) continue;
662 if ($_POST['form_category'] == 'Due Ins' && ($duncount >= 0 || !$isdueany)) continue;
663 if ($_POST['form_category'] == 'Due Pt' && ($duncount < 0 || !$isdueany)) continue;
665 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
667 $svcdate = substr($row['date'], 0, 10);
668 $last_stmt_date = empty($row['last_stmt_date']) ? '' : $row['last_stmt_date'];
670 // Determine if customer is in collections.
672 $billnote = $row['billing_note'];
673 $in_collections = stristr($billnote, 'IN COLLECTIONS') !== false;
675 <tr bgcolor='<?php echo $bgcolor ?>'>
676 <td class="detail">
677 &nbsp;<a href="" onclick="return npopup(<?php echo $row['pid'] ?>)"
678 ><?php echo $row['lname'] . ', ' . $row['fname']; ?></a>
679 </td>
680 <td class="detail">
681 &nbsp;<a href="sl_eob_invoice.php?id=<?php echo $row['id'] ?>"
682 target="_blank"><?php echo $row['pid'] . '.' . $row['encounter']; ?></a>
683 </td>
684 <td class="detail">
685 &nbsp;<?php echo oeFormatShortDate($svcdate) ?>
686 </td>
687 <td class="detail">
688 &nbsp;<?php echo oeFormatShortDate($last_stmt_date) ?>
689 </td>
690 <td class="detail" align="right">
691 <?php bucks($row['charges']) ?>&nbsp;
692 </td>
693 <td class="detail" align="right">
694 <?php bucks($row['adjustments']) ?>&nbsp;
695 </td>
696 <td class="detail" align="right">
697 <?php bucks($row['payments'] - $row['copays']); ?>&nbsp;
698 </td>
699 <td class="detail" align="right">
700 <?php bucks($balance); ?>&nbsp;
701 </td>
702 <td class="detail" align="center">
703 <?php echo $duncount ? $duncount : "&nbsp;" ?>
704 </td>
705 <?php if (!$eracount) { ?>
706 <td class="detail" align="left">
707 <input type='checkbox' name='form_cb[<?php echo($row['id']) ?>]'<?php echo $isduept ?> />
708 <?php if ($in_collections) echo "<b><font color='red'>IC</font></b>"; ?>
709 </td>
710 <?php } ?>
711 </tr>
712 <?php
713 } // end while
714 } // end search/print logic
718 </table>
721 <?php if ($eracount) { ?>
722 <input type='button' value='<?php xl('Process ERA File','e')?>' onclick='processERA()' /> &nbsp;
723 <?php } else { ?>
724 <input type='button' value='<?php xl('Select All','e')?>' onclick='checkAll(true)' /> &nbsp;
725 <input type='button' value='<?php xl('Clear All','e')?>' onclick='checkAll(false)' /> &nbsp;
726 <?php if ($GLOBALS['statement_appearance'] != '1') { ?>
727 <input type='submit' name='form_print' value='<?php xl('Print Selected Statements','e'); ?>' /> &nbsp;
728 <input type='submit' name='form_download' value='<?php xl('Download Selected Statements','e'); ?>' /> &nbsp;
729 <?php } ?>
730 <input type='submit' name='form_pdf' value='<?php xl('PDF Download Selected Statements','e'); ?>' /> &nbsp;
731 <?php } ?>
732 <input type='checkbox' name='form_without' value='1' /> <?php xl('Without Update','e'); ?>
733 </p>
735 </form>
736 </center>
737 <script language="JavaScript">
738 function processERA() {
739 var f = document.forms[0];
740 var debug = f.form_without.checked ? '1' : '0';
741 var paydate = f.form_paydate.value;
742 window.open('sl_eob_process.php?eraname=<?php echo $eraname ?>&debug=' + debug + '&paydate=' + paydate + '&original=original', '_blank');
743 return false;
745 <?php
746 if ($alertmsg) {
747 echo "alert('" . htmlentities($alertmsg) . "');\n";
751 </script>
752 </body>
753 </html>