Fixed bug: https://sourceforge.net/p/openemr/bugs/416/
[openemr.git] / interface / reports / collections_report.php
blobe0b7b2cb7bcb45ceef950c8f7e58761a915a69f0
1 <?php
2 /*
3 * Collections report
5 * (TLH) Added payor,provider,fixed cvs download to included selected fields
6 * (TLH) Added ability to download selected invoices only or all for patient
8 * Copyright (C) 2015 Terry Hill <terry@lillysystems.com>
9 * Copyright (C) 2006-2010 Rod Roark <rod@sunsetsystems.com>
11 * LICENSE: This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
22 * @package OpenEMR
23 * @author Rod Roark <rod@sunsetsystems.com>
24 * @author Terry Hill <terry@lilysystems.com>
25 * @link http://www.open-emr.org
28 $fake_register_globals=false;
29 $sanitize_all_escapes=true;
31 require_once("../globals.php");
32 require_once("../../library/patient.inc");
33 require_once("../../library/sql-ledger.inc");
34 require_once("../../library/invoice_summary.inc.php");
35 require_once("../../library/sl_eob.inc.php");
36 require_once("../../library/formatting.inc.php");
37 require_once "$srcdir/options.inc.php";
38 require_once "$srcdir/formdata.inc.php";
40 $INTEGRATED_AR = $GLOBALS['oer_config']['ws_accounting']['enabled'] === 2;
42 $alertmsg = '';
43 $bgcolor = "#aaaaaa";
44 $export_patient_count = 0;
45 $export_dollars = 0;
47 $today = date("Y-m-d");
49 $form_date = fixDate($_POST['form_date'], "");
50 $form_to_date = fixDate($_POST['form_to_date'], "");
51 $is_ins_summary = $_POST['form_category'] == 'Ins Summary';
52 $is_due_ins = ($_POST['form_category'] == 'Due Ins') || $is_ins_summary;
53 $is_due_pt = $_POST['form_category'] == 'Due Pt';
54 $is_all = $_POST['form_category'] == 'All';
55 $is_ageby_lad = strpos($_POST['form_ageby'], 'Last') !== false;
56 $form_facility = $_POST['form_facility'];
57 $form_provider = $_POST['form_provider'];
58 $form_payer_id = $_POST['form_payer_id'];
60 if ($_POST['form_refresh'] || $_POST['form_export'] || $_POST['form_csvexport']) {
61 if ($is_ins_summary) {
62 $form_cb_ssn = false;
63 $form_cb_dob = false;
64 $form_cb_pubpid = false;
65 $form_cb_adate = false;
66 $form_cb_policy = false;
67 $form_cb_phone = false;
68 $form_cb_city = false;
69 $form_cb_ins1 = false;
70 $form_cb_referrer = false;
71 $form_cb_idays = false;
72 $form_cb_err = false;
73 } else {
74 $form_cb_ssn = $_POST['form_cb_ssn'] ? true : false;
75 $form_cb_dob = $_POST['form_cb_dob'] ? true : false;
76 $form_cb_pubpid = $_POST['form_cb_pubpid'] ? true : false;
77 $form_cb_adate = $_POST['form_cb_adate'] ? true : false;
78 $form_cb_policy = $_POST['form_cb_policy'] ? true : false;
79 $form_cb_phone = $_POST['form_cb_phone'] ? true : false;
80 $form_cb_city = $_POST['form_cb_city'] ? true : false;
81 $form_cb_ins1 = $_POST['form_cb_ins1'] ? true : false;
82 $form_cb_referrer = $_POST['form_cb_referrer'] ? true : false;
83 $form_cb_idays = $_POST['form_cb_idays'] ? true : false;
84 $form_cb_err = $_POST['form_cb_err'] ? true : false;
86 } else {
87 $form_cb_ssn = true;
88 $form_cb_dob = false;
89 $form_cb_pubpid = false;
90 $form_cb_adate = false;
91 $form_cb_policy = false;
92 $form_cb_phone = true;
93 $form_cb_city = false;
94 $form_cb_ins1 = false;
95 $form_cb_referrer = false;
96 $form_cb_idays = false;
97 $form_cb_err = false;
99 $form_age_cols = (int) $_POST['form_age_cols'];
100 $form_age_inc = (int) $_POST['form_age_inc'];
101 if ($form_age_cols > 0 && $form_age_cols < 50) {
102 if ($form_age_inc <= 0) $form_age_inc = 30;
103 } else {
104 $form_age_cols = 0;
105 $form_age_inc = 0;
108 $initial_colspan = 1;
109 if ($is_due_ins ) ++$initial_colspan;
110 if ($form_cb_ssn ) ++$initial_colspan;
111 if ($form_cb_dob ) ++$initial_colspan;
112 if ($form_cb_pubpid ) ++$initial_colspan;
113 if ($form_cb_policy ) ++$initial_colspan;
114 if ($form_cb_phone ) ++$initial_colspan;
115 if ($form_cb_city ) ++$initial_colspan;
116 if ($form_cb_ins1 ) ++$initial_colspan;
117 if ($form_cb_referrer) ++$initial_colspan;
118 if ($form_provider ) ++$initial_colspan;
119 if ($form_payer_id ) ++$initial_colspan;
121 $final_colspan = $form_cb_adate ? 6 : 5;
123 $grand_total_charges = 0;
124 $grand_total_adjustments = 0;
125 $grand_total_paid = 0;
126 $grand_total_agedbal = array();
127 for ($c = 0; $c < $form_age_cols; ++$c) $grand_total_agedbal[$c] = 0;
129 if (!$INTEGRATED_AR) SLConnect();
131 function bucks($amount) {
132 if ($amount)
133 echo oeFormatMoney($amount); // was printf("%.2f", $amount);
136 function endPatient($ptrow) {
137 global $export_patient_count, $export_dollars, $bgcolor;
138 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
139 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
140 global $initial_colspan, $final_colspan, $form_cb_idays, $form_cb_err;
142 if (!$ptrow['pid']) return;
144 $pt_balance = $ptrow['amount'] - $ptrow['paid'];
146 if ($_POST['form_export']) {
147 // This is a fixed-length format used by Transworld Systems. Your
148 // needs will surely be different, so consider this just an example.
150 echo "1896H"; // client number goes here
151 echo "000"; // filler
152 echo sprintf("%-30s", substr($ptrow['ptname'], 0, 30));
153 echo sprintf("%-30s", " ");
154 echo sprintf("%-30s", substr($ptrow['address1'], 0, 30));
155 echo sprintf("%-15s", substr($ptrow['city'], 0, 15));
156 echo sprintf("%-2s", substr($ptrow['state'], 0, 2));
157 echo sprintf("%-5s", $ptrow['zipcode'] ? substr($ptrow['zipcode'], 0, 5) : '00000');
158 echo "1"; // service code
159 echo sprintf("%010.0f", $ptrow['pid']); // transmittal number = patient id
160 echo " "; // filler
161 echo sprintf("%-15s", substr($ptrow['ss'], 0, 15));
162 echo substr($ptrow['dos'], 5, 2) . substr($ptrow['dos'], 8, 2) . substr($ptrow['dos'], 2, 2);
163 echo sprintf("%08.0f", $pt_balance * 100);
164 echo sprintf("%-9s\n", " ");
166 if (!$_POST['form_without']) {
167 sqlStatement("UPDATE patient_data SET " .
168 "billing_note = CONCAT('IN COLLECTIONS " . date("Y-m-d") . "', billing_note) " .
169 "WHERE pid = ? ", array($ptrow['pid']));
171 $export_patient_count += 1;
172 $export_dollars += $pt_balance;
174 else if ($_POST['form_csvexport']) {
175 $export_patient_count += 1;
176 $export_dollars += $pt_balance;
178 else {
179 if ($ptrow['count'] > 1) {
180 echo " <tr bgcolor='$bgcolor'>\n";
181 /***************************************************************
182 echo " <td class='detail' colspan='$initial_colspan'>";
183 echo "&nbsp;</td>\n";
184 echo " <td class='detotal' colspan='$final_colspan'>&nbsp;Total Patient Balance:</td>\n";
185 ***************************************************************/
186 echo " <td class='detotal' colspan='" . ($initial_colspan + $final_colspan) .
187 "'>&nbsp;" . xlt('Total Patient Balance') . ":</td>\n";
188 /**************************************************************/
189 if ($form_age_cols) {
190 for ($c = 0; $c < $form_age_cols; ++$c) {
191 echo " <td class='detotal' align='right'>&nbsp;" .
192 oeFormatMoney($ptrow['agedbal'][$c]) . "&nbsp;</td>\n";
195 else {
196 echo " <td class='detotal' align='right'>&nbsp;" .
197 oeFormatMoney($pt_balance) . "&nbsp;</td>\n";
199 if ($form_cb_idays) echo " <td class='detail'>&nbsp;</td>\n";
200 echo " <td class='detail' colspan='2'>&nbsp;</td>\n";
201 if ($form_cb_err) echo " <td class='detail'>&nbsp;</td>\n";
202 echo " </tr>\n";
205 $grand_total_charges += $ptrow['charges'];
206 $grand_total_adjustments += $ptrow['adjustments'];
207 $grand_total_paid += $ptrow['paid'];
208 for ($c = 0; $c < $form_age_cols; ++$c) {
209 $grand_total_agedbal[$c] += $ptrow['agedbal'][$c];
213 function endInsurance($insrow) {
214 global $export_patient_count, $export_dollars, $bgcolor;
215 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
216 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
217 global $initial_colspan, $form_cb_idays, $form_cb_err;
218 if (!$insrow['pid']) return;
219 $ins_balance = $insrow['amount'] - $insrow['paid'];
220 if ($_POST['form_export'] || $_POST['form_csvexport']) {
221 // No exporting of insurance summaries.
222 $export_patient_count += 1;
223 $export_dollars += $ins_balance;
225 else {
226 echo " <tr bgcolor='$bgcolor'>\n";
227 echo " <td class='detail'>" . text($insrow['insname']) . "</td>\n";
228 echo " <td class='detotal' align='right'>&nbsp;" .
229 oeFormatMoney($insrow['charges']) . "&nbsp;</td>\n";
230 echo " <td class='detotal' align='right'>&nbsp;" .
231 oeFormatMoney($insrow['adjustments']) . "&nbsp;</td>\n";
232 echo " <td class='detotal' align='right'>&nbsp;" .
233 oeFormatMoney($insrow['paid']) . "&nbsp;</td>\n";
234 if ($form_age_cols) {
235 for ($c = 0; $c < $form_age_cols; ++$c) {
236 echo " <td class='detotal' align='right'>&nbsp;" .
237 oeFormatMoney($insrow['agedbal'][$c]) . "&nbsp;</td>\n";
240 else {
241 echo " <td class='detotal' align='right'>&nbsp;" .
242 oeFormatMoney($ins_balance) . "&nbsp;</td>\n";
244 echo " </tr>\n";
246 $grand_total_charges += $insrow['charges'];
247 $grand_total_adjustments += $insrow['adjustments'];
248 $grand_total_paid += $insrow['paid'];
249 for ($c = 0; $c < $form_age_cols; ++$c) {
250 $grand_total_agedbal[$c] += $insrow['agedbal'][$c];
254 function getInsName($payerid) {
255 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = ? ", array($payerid));
256 return $tmp['name'];
259 // In the case of CSV export only, a download will be forced.
260 if ($_POST['form_csvexport']) {
261 header("Pragma: public");
262 header("Expires: 0");
263 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
264 header("Content-Type: application/force-download");
265 header("Content-Disposition: attachment; filename=collections_report.csv");
266 header("Content-Description: File Transfer");
268 else {
270 <html>
271 <head>
272 <?php if (function_exists('html_header_show')) html_header_show(); ?>
273 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
274 <title><?php echo xlt('Collections Report')?></title>
275 <style type="text/css">
277 @media print {
278 #report_parameters {
279 visibility: hidden;
280 display: none;
282 #report_parameters_daterange {
283 visibility: visible;
284 display: inline;
286 #report_results {
287 margin-top: 30px;
291 /* specifically exclude some from the screen */
292 @media screen {
293 #report_parameters_daterange {
294 visibility: hidden;
295 display: none;
299 </style>
301 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
303 <script language="JavaScript">
305 $(document).ready(function() {
306 var win = top.printLogSetup ? top : opener.top;
307 win.printLogSetup(document.getElementById('printbutton'));
310 function checkAll(checked) {
311 var f = document.forms[0];
312 for (var i = 0; i < f.elements.length; ++i) {
313 var ename = f.elements[i].name;
314 if (ename.indexOf('form_cb[') == 0)
315 f.elements[i].checked = checked;
319 </script>
321 </head>
323 <body class="body_top">
325 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Collections'); ?></span>
327 <form method='post' action='collections_report.php' enctype='multipart/form-data' id='theform'>
329 <div id="report_parameters">
331 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
332 <input type='hidden' name='form_export' id='form_export' value=''/>
333 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
335 <table>
336 <tr>
337 <td width='610px'>
338 <div style='float:left'>
340 <table class='text'>
341 <tr>
342 <td class='label'>
343 <table>
344 <tr>
345 <td><?php echo xlt('Displayed Columns') ?>:</td>
346 </tr>
347 <tr>
348 <td>
349 <label><input type='checkbox' name='form_cb_ssn'<?php if ($form_cb_ssn) echo ' checked'; ?>>
350 <?php echo xlt('SSN') ?>&nbsp;</label>
351 </td>
352 <td>
353 <label><input type='checkbox' name='form_cb_dob'<?php if ($form_cb_dob) echo ' checked'; ?>>
354 <?php echo xlt('DOB') ?>&nbsp;</label>
355 </td>
356 <td>
357 <label><input type='checkbox' name='form_cb_pubpid'<?php if ($form_cb_pubpid) echo ' checked'; ?>>
358 <?php echo xlt('ID') ?>&nbsp;</label>
359 </td>
360 <td>
361 <label><input type='checkbox' name='form_cb_policy'<?php if ($form_cb_policy) echo ' checked'; ?>>
362 <?php echo xlt('Policy') ?>&nbsp;</label>
363 </td>
364 <td>
365 <label><input type='checkbox' name='form_cb_phone'<?php if ($form_cb_phone) echo ' checked'; ?>>
366 <?php echo xlt('Phone') ?>&nbsp;</label>
367 </td>
368 <td>
369 <label><input type='checkbox' name='form_cb_city'<?php if ($form_cb_city) echo ' checked'; ?>>
370 <?php echo xlt('City') ?>&nbsp;</label>
371 </td>
372 </tr>
373 <tr>
374 <td>
375 <label><input type='checkbox' name='form_cb_ins1'<?php if ($form_cb_ins1) echo ' checked'; ?>>
376 <?php echo xlt('Primary Ins') ?>&nbsp;</label>
377 </td>
378 <td>
379 <label><input type='checkbox' name='form_cb_referrer'<?php if ($form_cb_referrer) echo ' checked'; ?>>
380 <?php echo xlt('Referrer') ?>&nbsp;</label>
381 </td>
382 <td>
383 <label><input type='checkbox' name='form_cb_adate'<?php if ($form_cb_adate) echo ' checked'; ?>>
384 <?php echo xlt('Act Date') ?>&nbsp;</label>
385 </td>
386 <td>
387 <label><input type='checkbox' name='form_cb_idays'<?php if ($form_cb_idays) echo ' checked'; ?>>
388 <?php echo xlt('Inactive Days') ?>&nbsp;</label>
389 </td>
390 <td>
391 <label><input type='checkbox' name='form_cb_err'<?php if ($form_cb_err) echo ' checked'; ?>>
392 <?php echo xlt('Errors') ?></label>
393 </td>
394 </tr>
395 </table>
396 </td>
397 </tr>
398 </tr>
399 <td>
400 <table>
402 <tr>
403 <td class='label'>
404 <?php echo xlt('Service Date'); ?>:
405 </td>
406 <td>
407 <input type='text' name='form_date' id="form_date" size='10' value='<?php echo attr($form_date) ?>'
408 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
409 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
410 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
411 title='<?php echo xla('Click here to choose a date'); ?>'>
412 </td>
413 <td class='label'>
414 <?php echo xlt('To'); ?>:
415 </td>
416 <td>
417 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
418 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
419 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
420 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
421 title='<?php echo xla('Click here to choose a date'); ?>'>
422 </td>
423 <td>
424 <select name='form_category'>
425 <?php
426 foreach (array('Open' => xl('Open'),'Due Pt' => xl('Due Pt'),'Due Ins' => xl('Due Ins'),'Ins Summary' => xl('Ins Summary'),'Credits' => xl('Credits'),'All' => xl('All')) as $key => $value) {
427 echo " <option value='" . attr($key) . "'";
428 if ($_POST['form_category'] == $key) echo " selected";
429 echo ">" . text($value) . "</option>\n";
432 </select>
433 </td>
435 </tr>
438 <tr>
439 <td class='label'>
440 <?php echo xlt('Facility'); ?>:
441 </td>
442 <td>
443 <?php dropdown_facility($form_facility, 'form_facility', false); ?>
444 </td>
446 <td class='label'>
447 <?php echo xlt('Payor'); ?>:
448 </td>
449 <td>
450 <?php # added dropdown for payors (TLH)
451 $insurancei = getInsuranceProviders();
452 echo " <select name='form_payer_id'>\n";
453 echo " <option value='0'>-- " . xlt('All') . " --</option>\n";
454 foreach ($insurancei as $iid => $iname) {
455 echo "<option value='" . attr($iid) . "'";
456 if ($iid == $_POST['form_payer_id']) echo " selected";
457 echo ">" . text($iname) . "</option>\n";
458 if ($iid == $_POST['form_payer_id']) $ins_co_name = $iname;
460 echo " </select>\n";
462 </td>
463 </tr>
465 <tr>
466 <td class='label'>
467 <?php echo xlt('Age By') ?>:
468 </td>
469 <td>
470 <select name='form_ageby'>
471 <?php
472 foreach (array( 'Service Date'=>xl('Service Date'), 'Last Activity Date'=>xl('Last Activity Date')) as $key => $value) {
473 echo " <option value='" . attr($key) . "'";
474 if ($_POST['form_ageby'] == $value) echo " selected";
475 echo ">" . text($value) . "</option>\n";
478 </select>
479 </td>
481 <td class='label'>
482 <?php echo xlt('Provider') ?>:
483 </td>
484 <td>
485 <?php # Build a drop-down list of providers.
486 # Added (TLH)
488 $query = "SELECT id, lname, fname FROM users WHERE ".
489 "authorized = 1 ORDER BY lname, fname"; #(CHEMED) facility filter
491 $ures = sqlStatement($query);
493 echo " <select name='form_provider'>\n";
494 echo " <option value=''>-- " . xlt('All') . " --\n";
496 while ($urow = sqlFetchArray($ures)) {
497 $provid = $urow['id'];
498 echo " <option value='" . attr($provid) . "'";
499 if ($provid == $_POST['form_provider']) echo " selected";
500 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
501 if ($provid == $_POST['form_provider']) $provider_name = $urow['lname'] . ", " . $urow['fname'];
504 echo " </select>\n";
506 </td>
507 </tr>
508 </tr>
509 <td class='label'>
510 <?php echo xlt('Aging Columns') ?>:
511 </td>
512 <td>
513 <input type='text' name='form_age_cols' size='2' value='<?php echo attr($form_age_cols); ?>' />
514 </td>
515 <td class='label'>
516 <?php echo xlt('Days/Col') ?>:
517 </td>
518 <td>
519 <input type='text' name='form_age_inc' size='3' value='<?php echo attr($form_age_inc); ?>' />
520 </td>
521 </tr>
524 </table>
525 </td>
526 </tr>
527 </table>
529 </div>
531 </td>
532 <td align='left' valign='middle' height="100%">
533 <table style='border-left:1px solid; width:100%; height:100%' >
534 <tr>
535 <td>
536 <div style='margin-left:15px'>
537 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
538 <span>
539 <?php echo xlt('Submit'); ?>
540 </span>
541 </a>
543 <?php if ($_POST['form_refresh']) { ?>
544 <a href='#' class='css_button' onclick='window.print()'>
545 <span>
546 <?php echo xlt('Print'); ?>
547 </span>
548 </a>
549 <?php } ?>
550 </div>
551 </td>
552 </tr>
553 </table>
554 </td>
555 </tr>
556 </table>
557 </div>
560 <?php
562 } // end not form_csvexport
564 if ($_POST['form_refresh'] || $_POST['form_export'] || $_POST['form_csvexport']) {
565 $rows = array();
566 $where = "";
568 if ($INTEGRATED_AR) {
569 $sqlArray = array();
570 if ($_POST['form_export'] || $_POST['form_csvexport']) {
572 $where = "( 1 = 2";
573 foreach ($_POST['form_cb'] as $key => $value) {
574 list($key_newval['pid'], $key_newval['encounter']) = explode(".", $key);
575 $newkey = $key_newval['pid'];
576 $newencounter = $key_newval['encounter'];
577 # added this condition to handle the downloading of individual invoices (TLH)
578 if($_POST['form_individual'] ==1){
579 $where .= " OR f.encounter = ? ";
580 array_push($sqlArray, $newencounter);
582 else
584 $where .= " OR f.pid = ? ";
585 array_push($sqlArray, $newkey);
588 $where .= ' )';
590 if ($form_date) {
591 if ($where) $where .= " AND ";
592 if ($form_to_date) {
593 $where .= "f.date >= ? AND f.date <= ? ";
594 array_push($sqlArray, $form_date.' 00:00:00', $form_to_date.' 23:59:59');
596 else {
597 $where .= "f.date >= ? AND f.date <= ? ";
598 array_push($sqlArray, $form_date.' 00:00:00', $form_date.' 23:59:59');
601 if ($form_facility) {
602 if ($where) $where .= " AND ";
603 $where .= "f.facility_id = ? ";
604 array_push($sqlArray, $form_facility);
606 # added for filtering by provider (TLH)
607 if ($form_provider) {
608 if ($where) $where .= " AND ";
609 $where .= "f.provider_id = ? ";
610 array_push($sqlArray, $form_provider);
613 if (! $where) {
614 $where = "1 = 1";
616 # added provider from encounter to the query (TLH)
617 $query = "SELECT f.id, f.date, f.pid, CONCAT(w.lname, ', ', w.fname) AS provider_id, f.encounter, f.last_level_billed, " .
618 "f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, " .
619 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
620 "p.postal_code, p.phone_home, p.ss, p.billing_note, " .
621 "p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, " .
622 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
623 "b.pid = f.pid AND b.encounter = f.encounter AND " .
624 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
625 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
626 "b.pid = f.pid AND b.encounter = f.encounter AND " .
627 "b.activity = 1 AND b.code_type = 'COPAY' ) AS copays, " .
628 "( SELECT SUM(s.fee) FROM drug_sales AS s WHERE " .
629 "s.pid = f.pid AND s.encounter = f.encounter ) AS sales, " .
630 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
631 "a.pid = f.pid AND a.encounter = f.encounter ) AS payments, " .
632 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
633 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
634 "FROM form_encounter AS f " .
635 "JOIN patient_data AS p ON p.pid = f.pid " .
636 "LEFT OUTER JOIN users AS u ON u.id = p.ref_providerID " .
637 "LEFT OUTER JOIN users AS w ON w.id = f.provider_id " .
638 "WHERE $where " .
639 "ORDER BY f.pid, f.encounter";
641 $eres = sqlStatement($query, $sqlArray);
643 while ($erow = sqlFetchArray($eres)) {
644 $patient_id = $erow['pid'];
645 $encounter_id = $erow['encounter'];
646 $pt_balance = $erow['charges'] + $erow['sales'] + $erow['copays'] - $erow['payments'] - $erow['adjustments'];
647 $pt_balance = 0 + sprintf("%.2f", $pt_balance); // yes this seems to be necessary
648 $svcdate = substr($erow['date'], 0, 10);
650 if ($_POST['form_refresh'] && ! $is_all) {
651 if ($pt_balance == 0) continue;
653 if ($_POST['form_category'] == 'Credits') {
654 if ($pt_balance > 0) continue;
657 // If we have not yet billed the patient, then compute $duncount as a
658 // negative count of the number of insurance plans for which we have not
659 // yet closed out insurance. Here we also compute $insname as the name of
660 // the insurance plan from which we are awaiting payment, and its sequence
661 // number $insposition (1-3).
662 $last_level_closed = $erow['last_level_closed'];
663 $duncount = $erow['stmt_count'];
664 $payerids = array();
665 $insposition = 0;
666 $insname = '';
667 if (! $duncount) {
668 for ($i = 1; $i <= 3; ++$i) {
669 $tmp = arGetPayerID($patient_id, $svcdate, $i);
670 if (empty($tmp)) break;
671 $payerids[] = $tmp;
673 $duncount = $last_level_closed - count($payerids);
674 if ($duncount < 0) {
675 if (!empty($payerids[$last_level_closed])) {
676 $insname = getInsName($payerids[$last_level_closed]);
677 $insposition = $last_level_closed + 1;
682 // Skip invoices not in the desired "Due..." category.
684 if ($is_due_ins && $duncount >= 0) continue;
685 if ($is_due_pt && $duncount < 0) continue;
687 // echo "<!-- " . $erow['encounter'] . ': ' . $erow['charges'] . ' + ' . $erow['sales'] . ' + ' . $erow['copays'] . ' - ' . $erow['payments'] . ' - ' . $erow['adjustments'] . " -->\n"; // debugging
689 // An invoice is due from the patient if money is owed and we are
690 // not waiting for insurance to pay.
691 $isduept = ($duncount >= 0) ? " checked" : "";
693 $row = array();
695 $row['id'] = $erow['id'];
696 $row['invnumber'] = "$patient_id.$encounter_id";
697 $row['custid'] = $patient_id;
698 $row['name'] = $erow['fname'] . ' ' . $erow['lname'];
699 $row['address1'] = $erow['street'];
700 $row['city'] = $erow['city'];
701 $row['state'] = $erow['state'];
702 $row['zipcode'] = $erow['postal_code'];
703 $row['phone'] = $erow['phone_home'];
704 $row['duncount'] = $duncount;
705 $row['dos'] = $svcdate;
706 $row['ss'] = $erow['ss'];
707 $row['DOB'] = $erow['DOB'];
708 $row['pubpid'] = $erow['pubpid'];
709 $row['billnote'] = $erow['billing_note'];
710 $row['referrer'] = $erow['referrer'];
711 $row['provider'] = $erow['provider_id'];
712 $row['irnumber'] = $erow['invoice_refno'];
714 // Also get the primary insurance company name whenever there is one.
715 $row['ins1'] = '';
716 if ($insposition == 1) {
717 $row['ins1'] = $insname;
718 } else {
719 if (empty($payerids)) {
720 $tmp = arGetPayerID($patient_id, $svcdate, 1);
721 if (!empty($tmp)) $payerids[] = $tmp;
723 if (!empty($payerids)) {
724 $row['ins1'] = getInsName($payerids[0]);
728 // This computes the invoice's total original charges and adjustments,
729 // date of last activity, and determines if insurance has responded to
730 // all billing items.
731 $invlines = ar_get_invoice_summary($patient_id, $encounter_id, true);
733 // if ($encounter_id == 185) { // debugging
734 // echo "\n<!--\n";
735 // print_r($invlines);
736 // echo "\n-->\n";
737 // }
739 $row['charges'] = 0;
740 $row['adjustments'] = 0;
741 $row['paid'] = 0;
742 $ins_seems_done = true;
743 $ladate = $svcdate;
744 foreach ($invlines as $key => $value) {
745 $row['charges'] += $value['chg'] + $value['adj'];
746 $row['adjustments'] += 0 - $value['adj'];
747 $row['paid'] += $value['chg'] - $value['bal'];
748 foreach ($value['dtl'] as $dkey => $dvalue) {
749 $dtldate = trim(substr($dkey, 0, 10));
750 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
752 $lckey = strtolower($key);
753 if ($lckey == 'co-pay' || $lckey == 'claim') continue;
754 if (count($value['dtl']) <= 1) $ins_seems_done = false;
757 // Simulating ar.amount in SQL-Ledger which is charges with adjustments:
758 $row['amount'] = $row['charges'] + $row['adjustments'];
760 $row['billing_errmsg'] = '';
761 if ($is_due_ins && $last_level_closed < 1 && $ins_seems_done)
762 $row['billing_errmsg'] = 'Ins1 seems done';
763 else if ($last_level_closed >= 1 && !$ins_seems_done)
764 $row['billing_errmsg'] = 'Ins1 seems not done';
766 $row['ladate'] = $ladate;
768 // Compute number of days since last activity.
769 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
770 substr($ladate, 8, 2), substr($ladate, 0, 4));
771 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
773 // Look up insurance policy number if we need it.
774 if ($form_cb_policy) {
775 $instype = ($insposition == 2) ? 'secondary' : (($insposition == 3) ? 'tertiary' : 'primary');
776 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
777 "pid = ? AND type = ? AND date <= ? " .
778 "ORDER BY date DESC LIMIT 1", array($patient_id, $instype, $svcdate));
779 $row['policy'] = $insrow['policy_number'];
782 $ptname = $erow['lname'] . ", " . $erow['fname'];
783 if ($erow['mname']) $ptname .= " " . substr($erow['mname'], 0, 1);
785 if (!$is_due_ins ) $insname = '';
786 $rows[$insname . '|' . $ptname . '|' . $encounter_id] = $row;
787 } // end while
789 } // end $INTEGRATED_AR
790 else {
791 if ($_POST['form_export'] || $_POST['form_csvexport']) {
792 $where = "( 1 = 2";
793 foreach ($_POST['form_cb'] as $key => $value) {
794 list($key_newval['pid'], $key_newval['encounter']) = explode(".", $key);
795 $newkey = $key_newval['pid'];
796 $newencounter = $key_newval['encounter'];
797 # added this condition to handle the downloading of individual invoices (TLH)
798 if($_POST['form_individual'] ==1){
799 $where .= " OR f.encounter = $newencounter ";
801 else
803 $where .= " OR f.pid = $newkey ";
806 $where .= ' )';
809 if ($form_date) {
810 if ($where) $where .= " AND ";
811 $date1 = substr($form_date, 0, 4) . substr($form_date, 5, 2) .
812 substr($form_date, 8, 2);
813 if ($form_to_date) {
814 $date2 = substr($form_to_date, 0, 4) . substr($form_to_date, 5, 2) .
815 substr($form_to_date, 8, 2);
816 $where .= "((CAST (substring(ar.invnumber from position('.' in ar.invnumber) + 1 for 8) AS integer) " .
817 "BETWEEN '$date1' AND '$date2')";
818 $tmp = "date >= '$form_date' AND date <= '$form_to_date'";
820 else {
821 // This catches old converted invoices where we have no encounters:
822 $where .= "(ar.invnumber LIKE '%.$date1'";
823 $tmp = "date = '$form_date'";
825 // Pick out the encounters from MySQL with the desired DOS:
826 $rez = sqlStatement("SELECT pid, encounter FROM form_encounter WHERE $tmp");
827 while ($row = sqlFetchArray($rez)) {
828 $where .= " OR ar.invnumber = '" . $row['pid'] . "." . $row['encounter'] . "'";
830 $where .= ")";
833 if (! $where) {
834 $where = "1 = 1";
837 // Instead of the subselects in the following query, we will call
838 // get_invoice_summary() in order to get data at the procedure level and
839 // thus decide if insurance appears to be done with each invoice.
841 $query = "SELECT ar.id, ar.invnumber, ar.duedate, ar.amount, ar.paid, " .
842 "ar.intnotes, ar.notes, ar.shipvia, " .
843 "customer.id AS custid, customer.name, customer.address1, " .
844 "customer.city, customer.state, customer.zipcode, customer.phone " .
845 // ", (SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
846 // "invoice.trans_id = ar.id AND invoice.fxsellprice > 0) AS charges, " .
847 // "(SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
848 // "invoice.trans_id = ar.id AND invoice.fxsellprice < 0) AS adjustments " .
849 "FROM ar JOIN customer ON customer.id = ar.customer_id " .
850 "WHERE ( $where ) ";
851 if ($_POST['form_refresh'] && ! $is_all) {
852 $query .= "AND ar.amount != ar.paid ";
854 $query .= "ORDER BY ar.invnumber";
856 // echo "<!-- $query -->\n"; // debugging
858 $t_res = SLQuery($query);
859 if ($sl_err) die($sl_err);
860 $num_invoices = SLRowCount($t_res);
862 //////////////////////////////////////////////////////////////////
864 for ($irow = 0; $irow < $num_invoices; ++$irow) {
865 $row = SLGetRow($t_res, $irow);
867 // If a facility was specified then skip invoices whose encounters
868 // do not indicate that facility.
869 if ($form_facility) {
870 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
871 $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
872 "pid = ? AND encounter = ? AND " .
873 "facility_id = ? ", array($patient_id, $encounter_id, $form_facility));
874 if (empty($tmp['count'])) continue;
877 if ($form_provider) {
878 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
879 $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
880 "pid = ? AND encounter = ? AND " .
881 "provider_id = ? ", array($patient_id, $encounter_id, $form_provider));
883 if (empty($tmp['count'])) continue;
886 $pt_balance = sprintf("%.2f",$row['amount']) - sprintf("%.2f",$row['paid']);
888 if ($_POST['form_category'] == 'Credits') {
889 if ($pt_balance > 0) continue;
892 // $duncount was originally supposed to be the number of times that
893 // the patient was sent a statement for this invoice.
895 $duncount = substr_count(strtolower($row['intnotes']), "statement sent");
897 // But if we have not yet billed the patient, then compute $duncount as a
898 // negative count of the number of insurance plans for which we have not
899 // yet closed out insurance. Here we also compute $insname as the name of
900 // the insurance plan from which we are awaiting payment, and its sequence
901 // number $insposition (1-3).
903 $insname = '';
904 $insposition = 0;
905 $inseobs = strtolower($row['shipvia']);
906 $insgot = strtolower($row['notes']);
907 if (! $duncount) {
908 foreach (array('ins1', 'ins2', 'ins3') as $value) {
909 $i = strpos($insgot, $value);
910 if ($i !== false && strpos($inseobs, $value) === false) {
911 --$duncount;
912 if (!$insname && $is_due_ins) {
913 $j = strpos($insgot, "\n", $i);
914 if (!$j) $j = strlen($insgot);
915 $insname = trim(substr($row['notes'], $i + 5, $j - $i - 5));
916 $insposition = substr($value, 3); // 1, 2 or 3
921 $row['insname'] = $insname;
923 // Also get the primary insurance company name whenever there is one.
924 $row['ins1'] = '';
925 $i = strpos($insgot, 'ins1');
926 if ($i !== false) {
927 $j = strpos($insgot, "\n", $i);
928 if (!$j) $j = strlen($insgot);
929 $row['ins1'] = trim(substr($row['notes'], $i + 5, $j - $i - 5));
932 // An invoice is now due from the patient if money is owed and we are
933 // not waiting for insurance to pay. We no longer look at the due date
934 // for this.
936 $isduept = ($duncount >= 0) ? " checked" : "";
938 // Skip invoices not in the desired "Due..." category.
940 if ($is_due_ins && $duncount >= 0) continue;
941 if ($is_due_pt && $duncount < 0) continue;
943 $row['duncount'] = $duncount;
945 // Determine the date of service. An 8-digit encounter number is
946 // presumed to be a date of service imported during conversion.
947 // Otherwise look it up in the form_encounter table.
949 $svcdate = "";
950 list($pid, $encounter) = explode(".", $row['invnumber']);
951 if (strlen($encounter) == 8) {
952 $svcdate = substr($encounter, 0, 4) . "-" . substr($encounter, 4, 2) .
953 "-" . substr($encounter, 6, 2);
955 else if ($encounter) {
956 $tmp = sqlQuery("SELECT date FROM form_encounter WHERE " .
957 "encounter = ? ", array($encounter));
958 $svcdate = substr($tmp['date'], 0, 10);
961 $row['dos'] = $svcdate;
963 // This computes the invoice's total original charges and adjustments,
964 // date of last activity, and determines if insurance has responded to
965 // all billing items.
967 $invlines = get_invoice_summary($row['id'], true);
968 $row['charges'] = 0;
969 $row['adjustments'] = 0;
970 $ins_seems_done = true;
971 $ladate = $svcdate;
972 foreach ($invlines as $key => $value) {
973 $row['charges'] += $value['chg'] + $value['adj'];
974 $row['adjustments'] += 0 - $value['adj'];
975 foreach ($value['dtl'] as $dkey => $dvalue) {
976 $dtldate = trim(substr($dkey, 0, 10));
977 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
979 $lckey = strtolower($key);
980 if ($lckey == 'co-pay' || $lckey == 'claim') continue;
981 if (count($value['dtl']) <= 1) $ins_seems_done = false;
983 $row['billing_errmsg'] = '';
984 if ($is_due_ins && strpos($inseobs, 'ins1') === false && $ins_seems_done)
985 $row['billing_errmsg'] = 'Ins1 seems done';
986 else if (strpos($inseobs, 'ins1') !== false && !$ins_seems_done)
987 $row['billing_errmsg'] = 'Ins1 seems not done';
989 $row['ladate'] = $ladate;
991 // Compute number of days since last activity.
992 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
993 substr($ladate, 8, 2), substr($ladate, 0, 4));
994 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
996 $pdrow = sqlQuery("SELECT pd.fname, pd.lname, pd.mname, pd.ss, " .
997 "pd.billing_note, pd.pid, pd.pubpid, pd.DOB, " .
998 "CONCAT(u.lname, ', ', u.fname) AS referrer FROM " .
999 "integration_mapping AS im, patient_data AS pd " .
1000 "LEFT OUTER JOIN users AS u ON u.id = pd.ref_providerID " .
1001 "WHERE im.foreign_id = ? AND " .
1002 "im.foreign_table = 'customer' AND " .
1003 "pd.id = im.local_id", array($row['custid']));
1005 $row['ss'] = $pdrow['ss'];
1006 $row['DOB'] = $pdrow['DOB'];
1007 $row['pubpid'] = $pdrow['pubpid'];
1008 $row['billnote'] = $pdrow['billing_note'];
1009 $row['referrer'] = $pdrow['referrer'];
1011 $ptname = $pdrow['lname'] . ", " . $pdrow['fname'];
1012 if ($pdrow['mname']) $ptname .= " " . substr($pdrow['mname'], 0, 1);
1014 // Look up insurance policy number if we need it.
1015 if ($form_cb_policy) {
1016 $patient_id = $pdrow['pid'];
1017 $instype = ($insposition == 2) ? 'secondary' : (($insposition == 3) ? 'tertiary' : 'primary');
1018 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
1019 "pid = ? AND type = ? AND date <= ? " .
1020 "ORDER BY date DESC LIMIT 1", array($patient_id, $instype, $svcdate));
1021 $row['policy'] = $insrow['policy_number'];
1024 $rows[$insname . '|' . $ptname . '|' . $encounter] = $row;
1025 } // end for
1026 } // end not $INTEGRATED_AR
1028 ksort($rows);
1030 if ($_POST['form_export']) {
1031 echo "<textarea rows='35' cols='100' readonly>";
1033 else if ($_POST['form_csvexport']) {
1034 # CSV headers added conditions if they are checked to display then export them (TLH)
1035 if (true) {
1036 echo '"' . xl('Insurance') . '",';
1037 echo '"' . xl('Name') . '",';
1038 if ($form_cb_ssn)
1040 echo '"' . xl('SSN') . '",';
1042 if ($form_cb_dob)
1044 echo '"' . xl('DOB') . '",';
1046 if ($form_cb_pubid)
1048 echo '"' . xl('Pubid') . '",';
1050 if ($form_cb_policy)
1052 echo '"' . xl('Policy') . '",';
1054 if ($form_cb_phone)
1056 echo '"' . xl('Phone') . '",';
1058 if ($form_cb_city)
1060 echo '"' . xl('City') . '",';
1062 echo '"' . xl('Invoice') . '",';
1063 echo '"' . xl('DOS') . '",';
1064 echo '"' . xl('Referrer') . '",';
1065 echo '"' . xl('Provider') . '",';
1066 echo '"' . xl('Charge') . '",';
1067 echo '"' . xl('Adjust') . '",';
1068 echo '"' . xl('Paid') . '",';
1069 echo '"' . xl('Balance') . '",';
1070 echo '"' . xl('IDays') . '",';
1071 if ($form_cb_err)
1073 echo '"' . xl('LADate') . '",';
1074 echo '"' . xl('Error') . '"' . "\n";
1076 else
1078 echo '"' . xl('LADate') . '"' . "\n";
1082 else {
1085 <div id="report_results">
1086 <table>
1088 <thead>
1089 <?php if ($is_due_ins) { ?>
1090 <th>&nbsp;<?php echo xlt('Insurance')?></th>
1091 <?php } ?>
1092 <?php if (!$is_ins_summary) { ?>
1093 <th>&nbsp;<?php echo xlt('Name')?></th>
1094 <?php } ?>
1095 <?php if ($form_cb_ssn) { ?>
1096 <th>&nbsp;<?php echo xlt('SSN')?></th>
1097 <?php } ?>
1098 <?php if ($form_cb_dob) { ?>
1099 <th>&nbsp;<?php echo xlt('DOB')?></th>
1100 <?php } ?>
1101 <?php if ($form_cb_pubpid) { ?>
1102 <th>&nbsp;<?php echo xlt('ID')?></th>
1103 <?php } ?>
1104 <?php if ($form_cb_policy) { ?>
1105 <th>&nbsp;<?php echo xlt('Policy')?></th>
1106 <?php } ?>
1107 <?php if ($form_cb_phone) { ?>
1108 <th>&nbsp;<?php echo xlt('Phone')?></th>
1109 <?php } ?>
1110 <?php if ($form_cb_city) { ?>
1111 <th>&nbsp;<?php echo xlt('City')?></th>
1112 <?php } ?>
1113 <?php if ($form_cb_ins1 || $form_payer_id) { ?>
1114 <th>&nbsp;<?php echo xlt('Primary Ins')?></th>
1115 <?php } ?>
1116 <?php if ($form_provider) { ?>
1117 <th>&nbsp;<?php echo xlt('Provider')?></th>
1118 <?php } ?>
1119 <?php if ($form_cb_referrer) { ?>
1120 <th>&nbsp;<?php echo xlt('Referrer')?></th>
1121 <?php } ?>
1122 <?php if (!$is_ins_summary) { ?>
1123 <th>&nbsp;<?php echo xlt('Invoice') ?></th>
1124 <th>&nbsp;<?php echo xlt('Svc Date') ?></th>
1125 <?php if ($form_cb_adate) { ?>
1126 <th>&nbsp;<?php echo xlt('Act Date')?></th>
1127 <?php } ?>
1128 <?php } ?>
1129 <th align="right"><?php echo xlt('Charge') ?>&nbsp;</th>
1130 <th align="right"><?php echo xlt('Adjust') ?>&nbsp;</th>
1131 <th align="right"><?php echo xlt('Paid') ?>&nbsp;</th>
1132 <?php
1133 // Generate aging headers if appropriate, else balance header.
1134 if ($form_age_cols) {
1135 for ($c = 0; $c < $form_age_cols;) {
1136 echo " <th class='dehead' align='right'>";
1137 echo $form_age_inc * $c;
1138 if (++$c < $form_age_cols) {
1139 echo "-" . ($form_age_inc * $c - 1);
1140 } else {
1141 echo "+";
1143 echo "</th>\n";
1146 else {
1148 <th align="right"><?php echo xlt('Balance') ?>&nbsp;</th>
1149 <?php
1152 <?php if ($form_cb_idays) { ?>
1153 <th align="right"><?php echo xlt('IDays')?>&nbsp;</th>
1154 <?php } ?>
1155 <?php if (!$is_ins_summary) { ?>
1156 <th align="center"><?php echo xlt('Prv') ?></th>
1157 <th align="center"><?php echo xlt('Sel') ?></th>
1158 <?php } ?>
1159 <?php if ($form_cb_err) { ?>
1160 <th>&nbsp;<?php echo xlt('Error')?></th>
1161 <?php } ?>
1162 </thead>
1164 <?php
1165 } // end not export
1167 $ptrow = array('insname' => '', 'pid' => 0);
1168 $orow = -1;
1170 foreach ($rows as $key => $row) {
1171 list($insname, $ptname, $trash) = explode('|', $key);
1172 list($pid, $encounter) = explode(".", $row['invnumber']);
1173 if ($form_payer_id) {
1174 if ($ins_co_name <> $row['ins1']) continue;
1176 if ($is_ins_summary && $insname != $ptrow['insname']) {
1177 endInsurance($ptrow);
1178 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
1179 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1180 foreach ($row as $key => $value) $ptrow[$key] = $value;
1181 $ptrow['agedbal'] = array();
1183 else if (!$is_ins_summary && ($insname != $ptrow['insname'] || $pid != $ptrow['pid'])) {
1184 // For the report, this will write the patient totals. For the
1185 // collections export this writes everything for the patient:
1186 endPatient($ptrow);
1187 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
1188 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1189 foreach ($row as $key => $value) $ptrow[$key] = $value;
1190 $ptrow['agedbal'] = array();
1191 } else {
1192 $ptrow['amount'] += $row['amount'];
1193 $ptrow['paid'] += $row['paid'];
1194 $ptrow['charges'] += $row['charges'];
1195 $ptrow['adjustments'] += $row['adjustments'];
1196 ++$ptrow['count'];
1200 // Compute invoice balance and aging column number, and accumulate aging.
1201 $balance = $row['charges'] + $row['adjustments'] - $row['paid'];
1202 if ($form_age_cols) {
1203 $agedate = $is_ageby_lad ? $row['ladate'] : $row['dos'];
1204 $agetime = mktime(0, 0, 0, substr($agedate, 5, 2),
1205 substr($agedate, 8, 2), substr($agedate, 0, 4));
1206 $days = floor((time() - $agetime) / (60 * 60 * 24));
1207 $agecolno = min($form_age_cols - 1, max(0, floor($days / $form_age_inc)));
1208 $ptrow['agedbal'][$agecolno] += $balance;
1211 if (!$is_ins_summary && !$_POST['form_export'] && !$_POST['form_csvexport']) {
1212 $in_collections = stristr($row['billnote'], 'IN COLLECTIONS') !== false;
1214 <tr bgcolor='<?php echo attr($bgcolor) ?>'>
1215 <?php
1216 if ($ptrow['count'] == 1) {
1217 if ($is_due_ins) {
1218 echo " <td class='detail'>&nbsp;" . attr($insname) ."</td>\n";
1220 echo " <td class='detail'>&nbsp;" . attr($ptname) ."</td>\n";
1221 if ($form_cb_ssn) {
1222 echo " <td class='detail'>&nbsp;" . attr($row['ss']) . "</td>\n";
1224 if ($form_cb_dob) {
1225 echo " <td class='detail'>&nbsp;" . attr(oeFormatShortDate($row['DOB'])) . "</td>\n";
1227 if ($form_cb_pubpid) {
1228 echo " <td class='detail'>&nbsp;" . attr($row['pubpid']) . "</td>\n";
1230 if ($form_cb_policy) {
1231 echo " <td class='detail'>&nbsp;" . attr($row['policy']) . "</td>\n";
1233 if ($form_cb_phone) {
1234 echo " <td class='detail'>&nbsp;" . attr($row['phone']) . "</td>\n";
1236 if ($form_cb_city) {
1237 echo " <td class='detail'>&nbsp;" . attr($row['city']) . "</td>\n";
1239 if ($form_cb_ins1 || $form_payer_id ) {
1240 echo " <td class='detail'>&nbsp;" . attr($row['ins1']) . "</td>\n";
1242 if ($form_provider) {
1243 echo " <td class='detail'>&nbsp;" . attr($provider_name) . "</td>\n";
1245 if ($form_cb_referrer) {
1246 echo " <td class='detail'>&nbsp;" . attr($row['referrer']) . "</td>\n";
1248 } else {
1249 echo " <td class='detail' colspan='$initial_colspan'>";
1250 echo "&nbsp;</td>\n";
1253 <td class="detail">
1254 &nbsp;<a href="../billing/sl_eob_invoice.php?id=<?php echo attr($row['id']) ?>"
1255 target="_blank"><?php echo empty($row['irnumber']) ? $row['invnumber'] : $row['irnumber']; ?></a>
1256 </td>
1257 <td class="detail">
1258 &nbsp;<?php echo attr(oeFormatShortDate($row['dos'])); ?>
1259 </td>
1260 <?php if ($form_cb_adate) { ?>
1261 <td class='detail'>
1262 &nbsp;<?php echo attr(oeFormatShortDate($row['ladate'])); ?>
1263 </td>
1264 <?php } ?>
1265 <td class="detail" align="right">
1266 <?php attr(bucks($row['charges'])) ?>&nbsp;
1267 </td>
1268 <td class="detail" align="right">
1269 <?php attr(bucks($row['adjustments'])) ?>&nbsp;
1270 </td>
1271 <td class="detail" align="right">
1272 <?php attr(bucks($row['paid'])) ?>&nbsp;
1273 </td>
1274 <?php
1275 if ($form_age_cols) {
1276 for ($c = 0; $c < $form_age_cols; ++$c) {
1277 echo " <td class='detail' align='right'>";
1278 if ($c == $agecolno) {
1279 bucks($balance);
1281 echo "&nbsp;</td>\n";
1284 else {
1286 <td class="detail" align="right"><?php bucks($balance) ?>&nbsp;</td>
1287 <?php
1288 } // end else
1290 <?php
1291 if ($form_cb_idays) {
1292 echo " <td class='detail' align='right'>";
1293 echo attr($row['inactive_days']) . "&nbsp;</td>\n";
1296 <td class="detail" align="center">
1297 <?php echo $row['duncount'] ? $row['duncount'] : "&nbsp;" ?>
1298 </td>
1299 <td class="detail" align="center">
1300 <?php
1301 if ($in_collections) {
1302 echo " <b><font color='red'>IC</font></b>\n";
1303 } else {
1304 echo " <input type='checkbox' name='form_cb[" . attr($row['invnumber']) . "]' />\n";
1307 </td>
1308 <?php
1309 if ($form_cb_err) {
1310 echo " <td class='detail'>&nbsp;";
1311 echo text($row['billing_errmsg']) . "</td>\n";
1314 </tr>
1315 <?php
1316 } // end not export and not insurance summary
1318 else if ($_POST['form_csvexport']) {
1320 # The CSV detail line is written here added conditions for checked items (TLH).
1321 $balance = $row['charges'] + $row['adjustments'] - $row['paid'];
1322 if($balance >0) {
1323 // echo '"' . $insname . '",';
1324 echo '"' . $row['ins1'] . '",';
1325 echo '"' . $ptname . '",';
1326 if ($form_cb_ssn)
1328 echo '"' . $row['ss'] . '",';
1330 if ($form_cb_dob)
1332 echo '"' . oeFormatShortDate($row['DOB']) . '",';
1334 if ($form_cb_pubid)
1336 echo '"' . $row['pubpid'] . '",';
1338 if ($form_cb_policy)
1340 echo '"' . $row['policy'] . '",';
1342 if ($form_cb_phone)
1344 echo '"' . $row['phone'] . '",';
1346 if ($form_cb_city)
1348 echo '"' . $row['city'] . '",';
1350 echo '"' . (empty($row['irnumber']) ? $row['invnumber'] : $row['irnumber']) . '",';
1351 echo '"' . oeFormatShortDate($row['dos']) . '",';
1352 echo '"' . $row['referrer'] . '",';
1353 echo '"' . $row['provider'] . '",';
1354 echo '"' . oeFormatMoney($row['charges']) . '",';
1355 echo '"' . oeFormatMoney($row['adjustments']) . '",';
1356 echo '"' . oeFormatMoney($row['paid']) . '",';
1357 echo '"' . oeFormatMoney($balance) . '",';
1358 echo '"' . $row['inactive_days'] . '",';
1359 if ($form_cb_err)
1361 echo '"' . oeFormatShortDate($row['ladate']) . '",';
1362 echo '"' . $row['billing_errmsg'] . '"' . "\n";
1364 else
1366 echo '"' . oeFormatShortDate($row['ladate']) . '"' . "\n";
1369 } // end $form_csvexport
1371 } // end loop
1373 if ($is_ins_summary)
1374 endInsurance($ptrow);
1375 else
1376 endPatient($ptrow);
1378 if ($_POST['form_export']) {
1379 echo "</textarea>\n";
1380 $alertmsg .= "$export_patient_count patients with total of " .
1381 oeFormatMoney($export_dollars) . " have been exported ";
1382 if ($_POST['form_without']) {
1383 $alertmsg .= "but NOT flagged as in collections.";
1384 } else {
1385 $alertmsg .= "AND flagged as in collections.";
1388 else if ($_POST['form_csvexport']) {
1389 // echo "</textarea>\n";
1390 // $alertmsg .= "$export_patient_count patients representing $" .
1391 // sprintf("%.2f", $export_dollars) . " have been exported.";
1393 else {
1394 echo " <tr bgcolor='#ffffff'>\n";
1395 if ($is_ins_summary) {
1396 echo " <td class='dehead'>&nbsp;" . xlt('Report Totals') . ":</td>\n";
1397 } else {
1398 echo " <td class='detail' colspan='" . attr($initial_colspan) . "'>\n";
1399 echo " &nbsp;</td>\n";
1400 echo " <td class='dehead' colspan='" . attr($final_colspan - 3) .
1401 "'>&nbsp;" . xlt('Report Totals') . ":</td>\n";
1403 echo " <td class='dehead' align='right'>&nbsp;" .
1404 oeFormatMoney($grand_total_charges) . "&nbsp;</td>\n";
1405 echo " <td class='dehead' align='right'>&nbsp;" .
1406 oeFormatMoney($grand_total_adjustments) . "&nbsp;</td>\n";
1407 echo " <td class='dehead' align='right'>&nbsp;" .
1408 oeFormatMoney($grand_total_paid) . "&nbsp;</td>\n";
1409 if ($form_age_cols) {
1410 for ($c = 0; $c < $form_age_cols; ++$c) {
1411 echo " <td class='dehead' align='right'>" .
1412 oeFormatMoney($grand_total_agedbal[$c]) . "&nbsp;</td>\n";
1415 else {
1416 echo " <td class='dehead' align='right'>" .
1417 oeFormatMoney($grand_total_charges +
1418 $grand_total_adjustments - $grand_total_paid) . "&nbsp;</td>\n";
1420 if ($form_cb_idays) echo " <td class='detail'>&nbsp;</td>\n";
1421 if (!$is_ins_summary) echo " <td class='detail' colspan='2'>&nbsp;</td>\n";
1422 if ($form_cb_err) echo " <td class='detail'>&nbsp;</td>\n";
1423 echo " </tr>\n";
1424 echo "</table>\n";
1425 echo "</div>\n";
1427 } // end if form_refresh
1429 if (!$INTEGRATED_AR) SLClose();
1431 if (!$_POST['form_csvexport']) {
1432 if (!$_POST['form_export']) {
1435 <div style='float;margin-top:5px'>
1437 <a href='javascript:;' class='css_button' onclick='checkAll(true)'><span><?php echo xlt('Select All'); ?></span></a>
1438 <a href='javascript:;' class='css_button' onclick='checkAll(false)'><span><?php echo xlt('Clear All'); ?></span></a>
1439 <a href='javascript:;' class='css_button' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
1440 <span><?php echo xlt('Export Selected as CSV'); ?></span>
1441 </a>
1442 <a href='javascript:;' class='css_button' onclick='$("#form_export").attr("value","true"); $("#theform").submit();'>
1443 <span><?php echo xlt('Export Selected to Collections'); ?></span>
1444 </a>
1445 </div>
1447 <div style='float:left'>
1448 <label><input type='checkbox' name='form_individual' value='1' /> <?php echo xlt('Export Individual Invoices') ?>&nbsp;&nbsp;</label>
1449 </div>
1451 <div style='float:left'>
1452 <label><input type='checkbox' name='form_without' value='1' /> <?php echo xlt('Without Update') ?></label>
1453 </div>
1455 <?php
1456 } // end not export
1458 </form>
1459 </center>
1460 <script language="JavaScript">
1461 <?php
1462 if ($alertmsg) {
1463 echo "alert('" . addslashes($alertmsg) . "');\n";
1466 </script>
1467 </body>
1468 <!-- stuff for the popup calendar -->
1469 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
1470 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
1471 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
1472 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
1473 <script language="Javascript">
1474 Calendar.setup({inputField:"form_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
1475 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
1476 </script>
1477 </html>
1478 <?php
1479 } // end not form_csvexport