fixes to prior statements improvements
[openemr.git] / interface / billing / sl_eob_search.php
blob9f2c1fe3d9eae4b475ba9ba72d0be9c675cfa190
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");
40 require_once("$srcdir/classes/Document.class.php");
41 require_once("$srcdir/classes/Note.class.php");
43 $DEBUG = 0; // set to 0 for production, 1 to test
45 $alertmsg = '';
46 $where = '';
47 $eraname = '';
48 $eracount = 0;
50 // This is called back by parse_era() if we are processing X12 835's.
51 function era_callback(&$out) {
52 global $where, $eracount, $eraname;
53 // print_r($out); // debugging
54 ++$eracount;
55 // $eraname = $out['isa_control_number'];
56 $eraname = $out['gs_date'] . '_' . ltrim($out['isa_control_number'], '0') .
57 '_' . ltrim($out['payer_id'], '0');
58 list($pid, $encounter, $invnumber) = slInvoiceNumber($out);
60 if ($pid && $encounter) {
61 if ($where) $where .= ' OR ';
62 $where .= "( f.pid = '$pid' AND f.encounter = '$encounter' )";
66 function bucks($amount) {
67 if ($amount) echo oeFormatMoney($amount);
70 // Upload a file to the client's browser
72 function upload_file_to_client($file_to_send) {
73 header("Pragma: public");
74 header("Expires: 0");
75 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
76 header("Content-Type: application/force-download");
77 header("Content-Length: " . filesize($file_to_send));
78 header("Content-Disposition: attachment; filename=" . basename($file_to_send));
79 header("Content-Description: File Transfer");
80 readfile($file_to_send);
81 // flush the content to the browser. If you don't do this, the text from the subsequent
82 // output from this script will be in the file instead of sent to the browser.
83 flush();
84 exit(); //added to exit from process properly in order to stop bad html code -ehrlive
85 // sleep one second to ensure there's no follow-on.
86 sleep(1);
88 function upload_file_to_client_pdf($file_to_send) {
89 //Function reads a HTML file and converts to pdf.
91 global $STMT_TEMP_FILE_PDF;
92 global $srcdir;
94 if ($GLOBALS['statement_appearance'] == '1') {
95 require_once("$srcdir/html2pdf/vendor/autoload.php");
96 $pdf2 = new HTML2PDF ($GLOBALS['pdf_layout'],
97 $GLOBALS['pdf_size'],
98 $GLOBALS['pdf_language'],
99 true, // default unicode setting is true
100 'UTF-8', // default encoding setting is UTF-8
101 array($GLOBALS['pdf_left_margin'],$GLOBALS['pdf_top_margin'],$GLOBALS['pdf_right_margin'],$GLOBALS['pdf_bottom_margin']),
102 $_SESSION['language_direction'] == 'rtl' ? true : false
104 ob_start();
105 echo readfile($file_to_send, "r");//this file contains the HTML to be converted to pdf.
106 //echo $file;
107 $content = ob_get_clean();
109 // Fix a nasty html2pdf bug - it ignores document root!
110 global $web_root, $webserver_root;
111 $i = 0;
112 $wrlen = strlen($web_root);
113 $wsrlen = strlen($webserver_root);
114 while (true) {
115 $i = stripos($content, " src='/", $i + 1);
116 if ($i === false) break;
117 if (substr($content, $i+6, $wrlen) === $web_root &&
118 substr($content, $i+6, $wsrlen) !== $webserver_root) {
119 $content = substr($content, 0, $i + 6) . $webserver_root . substr($content, $i + 6 + $wrlen);
122 $pdf2->WriteHTML($content);
123 $temp_filename = $STMT_TEMP_FILE_PDF;
124 $content_pdf = $pdf2->Output($STMT_TEMP_FILE_PDF,'F');
125 } else {
126 $pdf = new Cezpdf('LETTER');//pdf creation starts
127 $pdf->ezSetMargins(45,9,36,10);
128 $pdf->selectFont('Courier');
129 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
130 $countline=1;
131 $file = fopen($file_to_send, "r");//this file contains the text to be converted to pdf.
132 while(!feof($file)) {
133 $OneLine=fgets($file);//one line is read
134 if(stristr($OneLine, "\014") == true && !feof($file))//form feed means we should start a new page.
136 $pdf->ezNewPage();
137 $pdf->ezSetY($pdf->ez['pageHeight'] - $pdf->ez['topMargin']);
138 str_replace("\014", "", $OneLine);
141 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.
142 $pdf->ezText('<b>'.$OneLine.'</b>', 12, array('justification' => 'left', 'leading' => 6));
143 else
144 $pdf->ezText($OneLine, 12, array('justification' => 'left', 'leading' => 6));
145 $countline++;
147 $fh = @fopen($STMT_TEMP_FILE_PDF, 'w');//stored to a pdf file
148 if ($fh) {
149 fwrite($fh, $pdf->ezOutput());
150 fclose($fh);
153 header("Pragma: public");//this section outputs the pdf file to browser
154 header("Expires: 0");
155 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
156 header("Content-Type: application/force-download");
157 header("Content-Length: " . filesize($STMT_TEMP_FILE_PDF));
158 header("Content-Disposition: attachment; filename=" . basename($STMT_TEMP_FILE_PDF));
159 header("Content-Description: File Transfer");
160 readfile($STMT_TEMP_FILE_PDF);
161 // flush the content to the browser. If you don't do this, the text from the subsequent
162 // output from this script will be in the file instead of sent to the browser.
163 flush();
164 exit(); //added to exit from process properly in order to stop bad html code -ehrlive
165 // sleep one second to ensure there's no follow-on.
166 sleep(1);
170 $today = date("Y-m-d");
171 // Print or download statements if requested.
173 if (($_POST['form_print'] || $_POST['form_download'] || $_POST['form_pdf']) && $_POST['form_cb']) {
175 $fhprint = fopen($STMT_TEMP_FILE, 'w');
176 $sqlBindArray = array();
177 $where = "";
178 foreach ($_POST['form_cb'] as $key => $value) {
179 $where .= " OR f.id = ?";
180 array_push($sqlBindArray, $key);
182 $where = substr($where, 4);
184 $res = sqlStatement("SELECT " .
185 "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, " .
186 "p.fname, p.mname, p.lname, p.street, p.city, p.state, p.postal_code, p.billing_note as pat_billing_note " .
187 "FROM form_encounter AS f, patient_data AS p " .
188 "WHERE ( $where ) AND " .
189 "p.pid = f.pid " .
190 "ORDER BY p.lname, p.fname, f.pid, f.date, f.encounter", $sqlBindArray);
192 $stmt = array();
193 $stmt_count = 0;
195 // This loops once for each invoice/encounter.
197 while ($row = sqlFetchArray($res)) {
198 $svcdate = substr($row['date'], 0, 10);
199 $duedate = $svcdate; // TBD?
200 $duncount = $row['stmt_count'];
201 $enc_note = $row['enc_billing_note'];
203 // If this is a new patient then print the pending statement
204 // and start a new one. This is an associative array:
206 // cid = same as pid
207 // pid = OpenEMR patient ID
208 // patient = patient name
209 // amount = total amount due
210 // adjust = adjustments (already applied to amount)
211 // duedate = due date of the oldest included invoice
212 // age = number of days from duedate to today
213 // to = array of addressee name/address lines
214 // lines = array of:
215 // dos = date of service "yyyy-mm-dd"
216 // desc = description
217 // amount = charge less adjustments
218 // paid = amount paid
219 // notice = 1 for first notice, 2 for second, etc.
220 // detail = array of details, see invoice_summary.inc.php
222 if ($stmt['cid'] != $row['pid']) {
223 if (!empty($stmt)) ++$stmt_count;
224 $stmt['cid'] = $row['pid'];
225 $stmt['pid'] = $row['pid'];
226 $stmt['dun_count'] = $row['stmt_count'];
227 $stmt['bill_note'] = $row['pat_billing_note'];
228 $stmt['enc_bill_note'] = $row['enc_billing_note'];
229 $stmt['bill_level'] = $row['last_level_billed'];
230 $stmt['level_closed'] = $row['last_level_closed'];
231 $stmt['patient'] = $row['fname'] . ' ' . $row['lname'];
232 $stmt['encounter'] = $row['encounter'];
233 #If you use the field in demographics layout called
234 #guardiansname this will allow you to send statements to the parent
235 #of a child or a guardian etc
236 if(strlen($row['guardiansname']) == 0) {
237 $stmt['to'] = array($row['fname'] . ' ' . $row['lname']);
239 else
241 $stmt['to'] = array($row['guardiansname']);
243 if ($row['street']) $stmt['to'][] = $row['street'];
244 $stmt['to'][] = $row['city'] . ", " . $row['state'] . " " . $row['postal_code'];
245 $stmt['lines'] = array();
246 $stmt['amount'] = '0.00';
247 $stmt['ins_paid'] = 0;
248 $stmt['today'] = $today;
249 $stmt['duedate'] = $duedate;
250 } else {
251 // Report the oldest due date.
252 if ($duedate < $stmt['duedate']) {
253 $stmt['duedate'] = $duedate;
257 // Recompute age at each invoice.
258 $stmt['age'] = round((strtotime($today) - strtotime($stmt['duedate'])) / (24 * 60 * 60));
260 $invlines = ar_get_invoice_summary($row['pid'], $row['encounter'], true);
261 foreach ($invlines as $key => $value) {
262 $line = array();
263 $line['dos'] = $svcdate;
264 if ($GLOBALS['use_custom_statement']) {
265 $line['desc'] = ($key == 'CO-PAY') ? "Patient Payment" : $value['code_text'];
266 } else {
267 $line['desc'] = ($key == 'CO-PAY') ? "Patient Payment" : "Procedure $key";
269 $line['amount'] = sprintf("%.2f", $value['chg']);
270 $line['adjust'] = sprintf("%.2f", $value['adj']);
271 $line['paid'] = sprintf("%.2f", $value['chg'] - $value['bal']);
272 $line['notice'] = $duncount + 1;
273 $line['detail'] = $value['dtl'];
274 $stmt['lines'][] = $line;
275 $stmt['amount'] = sprintf("%.2f", $stmt['amount'] + $value['bal']);
276 $stmt['ins_paid'] = $stmt['ins_paid'] + $value['ins'];
279 // Record that this statement was run.
280 if (! $DEBUG && ! $_POST['form_without']) {
281 sqlStatement("UPDATE form_encounter SET " .
282 "last_stmt_date = '$today', stmt_count = stmt_count + 1 " .
283 "WHERE id = " . $row['id']);
285 fwrite($fhprint, make_statement($stmt));
287 } // end while
289 if (!empty($stmt)) ++$stmt_count;
290 fclose($fhprint);
291 sleep(1);
293 // Download or print the file, as selected
294 if ($_POST['form_download']) {
295 upload_file_to_client($STMT_TEMP_FILE);
296 } elseif ($_POST['form_pdf']) {
297 upload_file_to_client_pdf($STMT_TEMP_FILE);
298 } else { // Must be print!
299 if ($DEBUG) {
300 $alertmsg = xl("Printing skipped; see test output in") .' '. $STMT_TEMP_FILE;
301 } else {
302 exec("$STMT_PRINT_CMD $STMT_TEMP_FILE");
303 if ($_POST['form_without']) {
304 $alertmsg = xl('Now printing') .' '. $stmt_count .' '. xl('statements; invoices will not be updated.');
305 } else {
306 $alertmsg = xl('Now printing') .' '. $stmt_count .' '. xl('statements and updating invoices.');
308 } // end not debug
309 } // end not form_download
310 } // end statements requested
312 <html>
313 <head>
314 <?php html_header_show(); ?>
315 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
316 <title><?php xl('EOB Posting - Search','e'); ?></title>
317 <script type="text/javascript" src="../../library/textformat.js"></script>
319 <script language="JavaScript">
321 var mypcc = '1';
323 function checkAll(checked) {
324 var f = document.forms[0];
325 for (var i = 0; i < f.elements.length; ++i) {
326 var ename = f.elements[i].name;
327 if (ename.indexOf('form_cb[') == 0)
328 f.elements[i].checked = checked;
332 function npopup(pid) {
333 window.open('sl_eob_patient_note.php?patient_id=' + pid, '_blank', 'width=500,height=250,resizable=1');
334 return false;
337 </script>
339 </head>
341 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
342 <center>
344 <form method='post' action='sl_eob_search.php' enctype='multipart/form-data'>
346 <table border='0' cellpadding='5' cellspacing='0'>
347 <tr>
349 <?php
350 // Identify the payer to support resumable posting sessions.
351 echo " <td>\n";
352 echo " " . xl('Payer') . ":\n";
353 echo " </td>\n";
354 echo " <td>\n";
355 $insurancei = getInsuranceProviders();
356 echo " <select name='form_payer_id'>\n";
357 echo " <option value='0'>-- " . xl('Patient') . " --</option>\n";
358 foreach ($insurancei as $iid => $iname) {
359 echo "<option value='$iid'";
360 if ($iid == $_POST['form_payer_id']) echo " selected";
361 echo ">" . $iname . "</option>\n";
363 echo " </select>\n";
364 echo " </td>\n";
367 <td>
368 <?php xl('Source:','e'); ?>
369 </td>
370 <td>
371 <input type='text' name='form_source' size='10' value='<?php echo $_POST['form_source']; ?>'
372 title='<?php xl("A check number or claim number to identify the payment","e"); ?>'>
373 </td>
374 <td>
375 <?php xl('Pay Date:','e'); ?>
376 </td>
377 <td>
378 <input type='text' name='form_paydate' size='10' value='<?php echo $_POST['form_paydate']; ?>'
379 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
380 title='<?php xl("Date of payment yyyy-mm-dd","e"); ?>'>
381 </td>
383 <td>
384 <?php xl('Deposit Date:','e'); ?>
385 </td>
386 <td>
387 <input type='text' name='form_deposit_date' size='10' value='<?php echo $_POST['form_deposit_date']; ?>'
388 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
389 title='<?php xl("Date of bank deposit yyyy-mm-dd","e"); ?>'>
390 </td>
392 <td>
393 <?php xl('Amount:','e'); ?>
394 </td>
395 <td>
396 <input type='text' name='form_amount' size='10' value='<?php echo $_POST['form_amount']; ?>'
397 title='<?php xl("Paid amount that you will allocate","e"); ?>'>
398 </td>
399 <td align='right'>
400 <a href='sl_eob_help.php' target='_blank'><?php xl('Help','e'); ?></a>
401 </td>
403 </tr>
404 </table>
406 <table border='0' cellpadding='5' cellspacing='0'>
408 <tr bgcolor='#ddddff'>
409 <td>
410 <?php xl('Name:','e'); ?>
411 </td>
412 <td>
413 <input type='text' name='form_name' size='10' value='<?php echo $_POST['form_name']; ?>'
414 title='<?php xl("Any part of the patient name, or \"last,first\", or \"X-Y\"","e"); ?>'>
415 </td>
416 <td>
417 <?php xl('Chart ID:','e'); ?>
418 </td>
419 <td>
420 <input type='text' name='form_pid' size='10' value='<?php echo $_POST['form_pid']; ?>'
421 title='<?php xl("Patient chart ID","e"); ?>'>
422 </td>
423 <td>
424 <?php xl('Encounter:','e'); ?>
425 </td>
426 <td>
427 <input type='text' name='form_encounter' size='10' value='<?php echo $_POST['form_encounter']; ?>'
428 title='<?php xl("Encounter number","e"); ?>'>
429 </td>
430 <td>
431 <?php xl('Svc Date:','e'); ?>
432 </td>
433 <td>
434 <input type='text' name='form_date' size='10' value='<?php echo $_POST['form_date']; ?>'
435 title='<?php xl("Date of service mm/dd/yyyy","e"); ?>'>
436 </td>
437 <td>
438 <?php xl('To:','e'); ?>
439 </td>
440 <td>
441 <input type='text' name='form_to_date' size='10' value='<?php echo $_POST['form_to_date']; ?>'
442 title='<?php xl("Ending DOS mm/dd/yyyy if you wish to enter a range","e"); ?>'>
443 </td>
444 <td>
445 <select name='form_category'>
446 <?php
447 foreach (array(xl('Open'), xl('All'), xl('Due Pt'), xl('Due Ins')) as $value) {
448 echo " <option value='$value'";
449 if ($_POST['form_category'] == $value) echo " selected";
450 echo ">$value</option>\n";
453 </select>
454 </td>
455 <td>
456 <input type='submit' name='form_search' value='<?php xl("Search","e"); ?>'>
457 </td>
458 </tr>
460 <!-- Support for X12 835 upload -->
461 <tr bgcolor='#ddddff'>
462 <td colspan='12'>
463 <?php xl('Or upload ERA file:','e'); ?>
464 <input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
465 <input name="form_erafile" type="file" />
466 </td>
467 </tr>
469 <tr>
470 <td height="1" colspan="10">
471 </td>
472 </tr>
474 </table>
476 <?php
477 if ($_POST['form_search'] || $_POST['form_print']) {
478 $form_name = trim($_POST['form_name']);
479 $form_pid = trim($_POST['form_pid']);
480 $form_encounter = trim($_POST['form_encounter']);
481 $form_date = fixDate($_POST['form_date'], "");
482 $form_to_date = fixDate($_POST['form_to_date'], "");
484 $where = "";
486 // Handle X12 835 file upload.
488 if ($_FILES['form_erafile']['size']) {
489 $tmp_name = $_FILES['form_erafile']['tmp_name'];
491 // Handle .zip extension if present. Probably won't work on Windows.
492 if (strtolower(substr($_FILES['form_erafile']['name'], -4)) == '.zip') {
493 rename($tmp_name, "$tmp_name.zip");
494 exec("unzip -p $tmp_name.zip > $tmp_name");
495 unlink("$tmp_name.zip");
498 echo "<!-- Notes from ERA upload processing:\n";
499 $alertmsg .= parse_era($tmp_name, 'era_callback');
500 echo "-->\n";
501 $erafullname = $GLOBALS['OE_SITE_DIR'] . "/era/$eraname.edi";
503 if (is_file($erafullname)) {
504 $alertmsg .= "Warning: Set $eraname was already uploaded ";
505 if (is_file($GLOBALS['OE_SITE_DIR'] . "/era/$eraname.html"))
506 $alertmsg .= "and processed. ";
507 else
508 $alertmsg .= "but not yet processed. ";
510 rename($tmp_name, $erafullname);
511 } // End 835 upload
513 if ($eracount) {
514 // Note that parse_era() modified $eracount and $where.
515 if (! $where) $where = '1 = 2';
517 else {
518 if ($form_name) {
519 if ($where) $where .= " AND ";
520 // Allow the last name to be followed by a comma and some part of a first name.
521 if (preg_match('/^(.*\S)\s*,\s*(.*)/', $form_name, $matches)) {
522 $where .= "p.lname LIKE '" . $matches[1] . "%' AND p.fname LIKE '" . $matches[2] . "%'";
523 // Allow a filter like "A-C" on the first character of the last name.
524 } else if (preg_match('/^(\S)\s*-\s*(\S)$/', $form_name, $matches)) {
525 $tmp = '1 = 2';
526 while (ord($matches[1]) <= ord($matches[2])) {
527 $tmp .= " OR p.lname LIKE '" . $matches[1] . "%'";
528 $matches[1] = chr(ord($matches[1]) + 1);
530 $where .= "( $tmp ) ";
531 } else {
532 $where .= "p.lname LIKE '%$form_name%'";
535 if ($form_pid) {
536 if ($where) $where .= " AND ";
537 $where .= "f.pid = '$form_pid'";
539 if ($form_encounter) {
540 if ($where) $where .= " AND ";
541 $where .= "f.encounter = '$form_encounter'";
543 if ($form_date) {
544 if ($where) $where .= " AND ";
545 if ($form_to_date) {
546 $where .= "f.date >= '$form_date' AND f.date <= '$form_to_date'";
548 else {
549 $where .= "f.date = '$form_date'";
552 if (! $where) {
553 if ($_POST['form_category'] == 'All') {
554 die(xl("At least one search parameter is required if you select All."));
555 } else {
556 $where = "1 = 1";
561 // Notes that as of release 4.1.1 the copays are stored
562 // in the ar_activity table marked with a PCP in the account_code column.
563 $query = "SELECT f.id, f.pid, f.encounter, f.date, " .
564 "f.last_level_billed, f.last_level_closed, f.last_stmt_date, f.stmt_count, " .
565 "p.fname, p.mname, p.lname, p.pubpid, p.billing_note, " .
566 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
567 "b.pid = f.pid AND b.encounter = f.encounter AND " .
568 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
569 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
570 "a.pid = f.pid AND a.encounter = f.encounter AND a.payer_type = 0 AND a.account_code = 'PCP')*-1 AS copays, " .
571 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
572 "a.pid = f.pid AND a.encounter = f.encounter AND a.account_code != 'PCP') AS payments, " .
573 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
574 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
575 "FROM form_encounter AS f " .
576 "JOIN patient_data AS p ON p.pid = f.pid " .
577 "WHERE $where " .
578 "ORDER BY p.lname, p.fname, p.mname, f.pid, f.encounter";
580 // Note that unlike the SQL-Ledger case, this query does not weed
581 // out encounters that are paid up. Also the use of sub-selects
582 // will require MySQL 4.1 or greater.
584 // echo "<!-- $query -->\n"; // debugging
586 $t_res = sqlStatement($query);
588 $num_invoices = sqlNumRows($t_res);
589 if ($eracount && $num_invoices != $eracount) {
590 $alertmsg .= "Of $eracount remittances, there are $num_invoices " .
591 "matching encounters in OpenEMR. ";
595 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
597 <tr bgcolor="#dddddd">
598 <td class="dehead">
599 &nbsp;<?php xl('Patient','e'); ?>
600 </td>
601 <td class="dehead">
602 &nbsp;<?php xl('Invoice','e'); ?>
603 </td>
604 <td class="dehead">
605 &nbsp;<?php xl('Svc Date','e'); ?>
606 </td>
607 <td class="dehead">
608 &nbsp;<?php xl('Last Stmt','e'); ?>
609 </td>
610 <td class="dehead" align="right">
611 <?php xl('Charge','e'); ?>&nbsp;
612 </td>
613 <td class="dehead" align="right">
614 <?php xl('Adjust','e'); ?>&nbsp;
615 </td>
616 <td class="dehead" align="right">
617 <?php xl('Paid','e'); ?>&nbsp;
618 </td>
619 <td class="dehead" align="right">
620 <?php xl('Balance','e'); ?>&nbsp;
621 </td>
622 <td class="dehead" align="center">
623 <?php xl('Prv','e'); ?>
624 </td>
625 <?php if (!$eracount) { ?>
626 <td class="dehead" align="left">
627 <?php xl('Sel','e'); ?>
628 </td>
629 <?php } ?>
630 </tr>
632 <?php
633 $orow = -1;
635 while ($row = sqlFetchArray($t_res)) {
636 $balance = sprintf("%.2f", $row['charges'] + $row['copays'] - $row['payments'] - $row['adjustments']);
638 if ($_POST['form_category'] != 'All' && $eracount == 0 && $balance == 0) continue;
640 // $duncount was originally supposed to be the number of times that
641 // the patient was sent a statement for this invoice.
643 $duncount = $row['stmt_count'];
645 // But if we have not yet billed the patient, then compute $duncount as a
646 // negative count of the number of insurance plans for which we have not
647 // yet closed out insurance.
649 if (! $duncount) {
650 for ($i = 1; $i <= 3 && arGetPayerID($row['pid'], $row['date'], $i); ++$i) ;
651 $duncount = $row['last_level_closed'] + 1 - $i;
654 $isdueany = ($balance > 0);
656 // An invoice is now due from the patient if money is owed and we are
657 // not waiting for insurance to pay.
659 $isduept = ($duncount >= 0 && $isdueany) ? " checked" : "";
661 // Skip invoices not in the desired "Due..." category.
663 if (substr($_POST['form_category'], 0, 3) == 'Due' && !$isdueany) continue;
664 if ($_POST['form_category'] == 'Due Ins' && ($duncount >= 0 || !$isdueany)) continue;
665 if ($_POST['form_category'] == 'Due Pt' && ($duncount < 0 || !$isdueany)) continue;
667 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
669 $svcdate = substr($row['date'], 0, 10);
670 $last_stmt_date = empty($row['last_stmt_date']) ? '' : $row['last_stmt_date'];
672 // Determine if customer is in collections.
674 $billnote = $row['billing_note'];
675 $in_collections = stristr($billnote, 'IN COLLECTIONS') !== false;
677 <tr bgcolor='<?php echo $bgcolor ?>'>
678 <td class="detail">
679 &nbsp;<a href="" onclick="return npopup(<?php echo $row['pid'] ?>)"
680 ><?php echo $row['lname'] . ', ' . $row['fname']; ?></a>
681 </td>
682 <td class="detail">
683 &nbsp;<a href="sl_eob_invoice.php?id=<?php echo $row['id'] ?>"
684 target="_blank"><?php echo $row['pid'] . '.' . $row['encounter']; ?></a>
685 </td>
686 <td class="detail">
687 &nbsp;<?php echo oeFormatShortDate($svcdate) ?>
688 </td>
689 <td class="detail">
690 &nbsp;<?php echo oeFormatShortDate($last_stmt_date) ?>
691 </td>
692 <td class="detail" align="right">
693 <?php bucks($row['charges']) ?>&nbsp;
694 </td>
695 <td class="detail" align="right">
696 <?php bucks($row['adjustments']) ?>&nbsp;
697 </td>
698 <td class="detail" align="right">
699 <?php bucks($row['payments'] - $row['copays']); ?>&nbsp;
700 </td>
701 <td class="detail" align="right">
702 <?php bucks($balance); ?>&nbsp;
703 </td>
704 <td class="detail" align="center">
705 <?php echo $duncount ? $duncount : "&nbsp;" ?>
706 </td>
707 <?php if (!$eracount) { ?>
708 <td class="detail" align="left">
709 <input type='checkbox' name='form_cb[<?php echo($row['id']) ?>]'<?php echo $isduept ?> />
710 <?php if ($in_collections) echo "<b><font color='red'>IC</font></b>"; ?>
711 </td>
712 <?php } ?>
713 </tr>
714 <?php
715 } // end while
716 } // end search/print logic
720 </table>
723 <?php if ($eracount) { ?>
724 <input type='button' value='<?php xl('Process ERA File','e')?>' onclick='processERA()' /> &nbsp;
725 <?php } else { ?>
726 <input type='button' value='<?php xl('Select All','e')?>' onclick='checkAll(true)' /> &nbsp;
727 <input type='button' value='<?php xl('Clear All','e')?>' onclick='checkAll(false)' /> &nbsp;
728 <?php if ($GLOBALS['statement_appearance'] != '1') { ?>
729 <input type='submit' name='form_print' value='<?php xl('Print Selected Statements','e'); ?>' /> &nbsp;
730 <input type='submit' name='form_download' value='<?php xl('Download Selected Statements','e'); ?>' /> &nbsp;
731 <?php } ?>
732 <input type='submit' name='form_pdf' value='<?php xl('PDF Download Selected Statements','e'); ?>' /> &nbsp;
733 <?php } ?>
734 <input type='checkbox' name='form_without' value='1' /> <?php xl('Without Update','e'); ?>
735 </p>
737 </form>
738 </center>
739 <script language="JavaScript">
740 function processERA() {
741 var f = document.forms[0];
742 var debug = f.form_without.checked ? '1' : '0';
743 var paydate = f.form_paydate.value;
744 window.open('sl_eob_process.php?eraname=<?php echo $eraname ?>&debug=' + debug + '&paydate=' + paydate + '&original=original', '_blank');
745 return false;
747 <?php
748 if ($alertmsg) {
749 echo "alert('" . htmlentities($alertmsg) . "');\n";
753 </script>
754 </body>
755 </html>