Support for optional logging of print actions.
[openemr.git] / interface / reports / collections_report.php
blobc1e31a5f5c3dd234137edf3490a308d95a663e52
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 "genericname2 = 'Billing', " .
169 "genericval2 = CONCAT('IN COLLECTIONS " . date("Y-m-d") . "', genericval2) " .
170 "WHERE pid = ? ", array($ptrow['pid']));
172 $export_patient_count += 1;
173 $export_dollars += $pt_balance;
175 else if ($_POST['form_csvexport']) {
176 $export_patient_count += 1;
177 $export_dollars += $pt_balance;
179 else {
180 if ($ptrow['count'] > 1) {
181 echo " <tr bgcolor='$bgcolor'>\n";
182 /***************************************************************
183 echo " <td class='detail' colspan='$initial_colspan'>";
184 echo "&nbsp;</td>\n";
185 echo " <td class='detotal' colspan='$final_colspan'>&nbsp;Total Patient Balance:</td>\n";
186 ***************************************************************/
187 echo " <td class='detotal' colspan='" . ($initial_colspan + $final_colspan) .
188 "'>&nbsp;" . xlt('Total Patient Balance') . ":</td>\n";
189 /**************************************************************/
190 if ($form_age_cols) {
191 for ($c = 0; $c < $form_age_cols; ++$c) {
192 echo " <td class='detotal' align='right'>&nbsp;" .
193 oeFormatMoney($ptrow['agedbal'][$c]) . "&nbsp;</td>\n";
196 else {
197 echo " <td class='detotal' align='right'>&nbsp;" .
198 oeFormatMoney($pt_balance) . "&nbsp;</td>\n";
200 if ($form_cb_idays) echo " <td class='detail'>&nbsp;</td>\n";
201 echo " <td class='detail' colspan='2'>&nbsp;</td>\n";
202 if ($form_cb_err) echo " <td class='detail'>&nbsp;</td>\n";
203 echo " </tr>\n";
206 $grand_total_charges += $ptrow['charges'];
207 $grand_total_adjustments += $ptrow['adjustments'];
208 $grand_total_paid += $ptrow['paid'];
209 for ($c = 0; $c < $form_age_cols; ++$c) {
210 $grand_total_agedbal[$c] += $ptrow['agedbal'][$c];
214 function endInsurance($insrow) {
215 global $export_patient_count, $export_dollars, $bgcolor;
216 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
217 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
218 global $initial_colspan, $form_cb_idays, $form_cb_err;
219 if (!$insrow['pid']) return;
220 $ins_balance = $insrow['amount'] - $insrow['paid'];
221 if ($_POST['form_export'] || $_POST['form_csvexport']) {
222 // No exporting of insurance summaries.
223 $export_patient_count += 1;
224 $export_dollars += $ins_balance;
226 else {
227 echo " <tr bgcolor='$bgcolor'>\n";
228 echo " <td class='detail'>" . text($insrow['insname']) . "</td>\n";
229 echo " <td class='detotal' align='right'>&nbsp;" .
230 oeFormatMoney($insrow['charges']) . "&nbsp;</td>\n";
231 echo " <td class='detotal' align='right'>&nbsp;" .
232 oeFormatMoney($insrow['adjustments']) . "&nbsp;</td>\n";
233 echo " <td class='detotal' align='right'>&nbsp;" .
234 oeFormatMoney($insrow['paid']) . "&nbsp;</td>\n";
235 if ($form_age_cols) {
236 for ($c = 0; $c < $form_age_cols; ++$c) {
237 echo " <td class='detotal' align='right'>&nbsp;" .
238 oeFormatMoney($insrow['agedbal'][$c]) . "&nbsp;</td>\n";
241 else {
242 echo " <td class='detotal' align='right'>&nbsp;" .
243 oeFormatMoney($ins_balance) . "&nbsp;</td>\n";
245 echo " </tr>\n";
247 $grand_total_charges += $insrow['charges'];
248 $grand_total_adjustments += $insrow['adjustments'];
249 $grand_total_paid += $insrow['paid'];
250 for ($c = 0; $c < $form_age_cols; ++$c) {
251 $grand_total_agedbal[$c] += $insrow['agedbal'][$c];
255 function getInsName($payerid) {
256 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = ? ", array($payerid));
257 return $tmp['name'];
260 // In the case of CSV export only, a download will be forced.
261 if ($_POST['form_csvexport']) {
262 header("Pragma: public");
263 header("Expires: 0");
264 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
265 header("Content-Type: application/force-download");
266 header("Content-Disposition: attachment; filename=collections_report.csv");
267 header("Content-Description: File Transfer");
269 else {
271 <html>
272 <head>
273 <?php if (function_exists('html_header_show')) html_header_show(); ?>
274 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
275 <title><?php echo xlt('Collections Report')?></title>
276 <style type="text/css">
278 @media print {
279 #report_parameters {
280 visibility: hidden;
281 display: none;
283 #report_parameters_daterange {
284 visibility: visible;
285 display: inline;
287 #report_results {
288 margin-top: 30px;
292 /* specifically exclude some from the screen */
293 @media screen {
294 #report_parameters_daterange {
295 visibility: hidden;
296 display: none;
300 </style>
302 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
304 <script language="JavaScript">
306 $(document).ready(function() {
307 var win = top.printLogSetup ? top : opener.top;
308 win.printLogSetup(document.getElementById('printbutton'));
311 function checkAll(checked) {
312 var f = document.forms[0];
313 for (var i = 0; i < f.elements.length; ++i) {
314 var ename = f.elements[i].name;
315 if (ename.indexOf('form_cb[') == 0)
316 f.elements[i].checked = checked;
320 </script>
322 </head>
324 <body class="body_top">
326 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Collections'); ?></span>
328 <form method='post' action='collections_report.php' enctype='multipart/form-data' id='theform'>
330 <div id="report_parameters">
332 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
333 <input type='hidden' name='form_export' id='form_export' value=''/>
334 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
336 <table>
337 <tr>
338 <td width='610px'>
339 <div style='float:left'>
341 <table class='text'>
342 <tr>
343 <td class='label'>
344 <table>
345 <tr>
346 <td><?php echo xlt('Displayed Columns') ?>:</td>
347 </tr>
348 <tr>
349 <td>
350 <label><input type='checkbox' name='form_cb_ssn'<?php if ($form_cb_ssn) echo ' checked'; ?>>
351 <?php echo xlt('SSN') ?>&nbsp;</label>
352 </td>
353 <td>
354 <label><input type='checkbox' name='form_cb_dob'<?php if ($form_cb_dob) echo ' checked'; ?>>
355 <?php echo xlt('DOB') ?>&nbsp;</label>
356 </td>
357 <td>
358 <label><input type='checkbox' name='form_cb_pubpid'<?php if ($form_cb_pubpid) echo ' checked'; ?>>
359 <?php echo xlt('ID') ?>&nbsp;</label>
360 </td>
361 <td>
362 <label><input type='checkbox' name='form_cb_policy'<?php if ($form_cb_policy) echo ' checked'; ?>>
363 <?php echo xlt('Policy') ?>&nbsp;</label>
364 </td>
365 <td>
366 <label><input type='checkbox' name='form_cb_phone'<?php if ($form_cb_phone) echo ' checked'; ?>>
367 <?php echo xlt('Phone') ?>&nbsp;</label>
368 </td>
369 <td>
370 <label><input type='checkbox' name='form_cb_city'<?php if ($form_cb_city) echo ' checked'; ?>>
371 <?php echo xlt('City') ?>&nbsp;</label>
372 </td>
373 </tr>
374 <tr>
375 <td>
376 <label><input type='checkbox' name='form_cb_ins1'<?php if ($form_cb_ins1) echo ' checked'; ?>>
377 <?php echo xlt('Primary Ins') ?>&nbsp;</label>
378 </td>
379 <td>
380 <label><input type='checkbox' name='form_cb_referrer'<?php if ($form_cb_referrer) echo ' checked'; ?>>
381 <?php echo xlt('Referrer') ?>&nbsp;</label>
382 </td>
383 <td>
384 <label><input type='checkbox' name='form_cb_adate'<?php if ($form_cb_adate) echo ' checked'; ?>>
385 <?php echo xlt('Act Date') ?>&nbsp;</label>
386 </td>
387 <td>
388 <label><input type='checkbox' name='form_cb_idays'<?php if ($form_cb_idays) echo ' checked'; ?>>
389 <?php echo xlt('Inactive Days') ?>&nbsp;</label>
390 </td>
391 <td>
392 <label><input type='checkbox' name='form_cb_err'<?php if ($form_cb_err) echo ' checked'; ?>>
393 <?php echo xlt('Errors') ?></label>
394 </td>
395 </tr>
396 </table>
397 </td>
398 </tr>
399 </tr>
400 <td>
401 <table>
403 <tr>
404 <td class='label'>
405 <?php echo xlt('Service Date'); ?>:
406 </td>
407 <td>
408 <input type='text' name='form_date' id="form_date" size='10' value='<?php echo attr($form_date) ?>'
409 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
410 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
411 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
412 title='<?php echo xla('Click here to choose a date'); ?>'>
413 </td>
414 <td class='label'>
415 <?php echo xlt('To'); ?>:
416 </td>
417 <td>
418 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo attr($form_to_date) ?>'
419 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
420 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
421 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
422 title='<?php echo xla('Click here to choose a date'); ?>'>
423 </td>
424 <td>
425 <select name='form_category'>
426 <?php
427 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) {
428 echo " <option value='" . attr($key) . "'";
429 if ($_POST['form_category'] == $key) echo " selected";
430 echo ">" . text($value) . "</option>\n";
433 </select>
434 </td>
436 </tr>
439 <tr>
440 <td class='label'>
441 <?php echo xlt('Facility'); ?>:
442 </td>
443 <td>
444 <?php dropdown_facility($form_facility, 'form_facility', false); ?>
445 </td>
447 <td class='label'>
448 <?php echo xlt('Payor'); ?>:
449 </td>
450 <td>
451 <?php # added dropdown for payors (TLH)
452 $insurancei = getInsuranceProviders();
453 echo " <select name='form_payer_id'>\n";
454 echo " <option value='0'>-- " . xlt('All') . " --</option>\n";
455 foreach ($insurancei as $iid => $iname) {
456 echo "<option value='" . attr($iid) . "'";
457 if ($iid == $_POST['form_payer_id']) echo " selected";
458 echo ">" . text($iname) . "</option>\n";
459 if ($iid == $_POST['form_payer_id']) $ins_co_name = $iname;
461 echo " </select>\n";
463 </td>
464 </tr>
466 <tr>
467 <td class='label'>
468 <?php echo xlt('Age By') ?>:
469 </td>
470 <td>
471 <select name='form_ageby'>
472 <?php
473 foreach (array( 'Service Date'=>xl('Service Date'), 'Last Activity Date'=>xl('Last Activity Date')) as $key => $value) {
474 echo " <option value='" . attr($key) . "'";
475 if ($_POST['form_ageby'] == $value) echo " selected";
476 echo ">" . text($value) . "</option>\n";
479 </select>
480 </td>
482 <td class='label'>
483 <?php echo xlt('Provider') ?>:
484 </td>
485 <td>
486 <?php # Build a drop-down list of providers.
487 # Added (TLH)
489 $query = "SELECT id, lname, fname FROM users WHERE ".
490 "authorized = 1 ORDER BY lname, fname"; #(CHEMED) facility filter
492 $ures = sqlStatement($query);
494 echo " <select name='form_provider'>\n";
495 echo " <option value=''>-- " . xlt('All') . " --\n";
497 while ($urow = sqlFetchArray($ures)) {
498 $provid = $urow['id'];
499 echo " <option value='" . attr($provid) . "'";
500 if ($provid == $_POST['form_provider']) echo " selected";
501 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
502 if ($provid == $_POST['form_provider']) $provider_name = $urow['lname'] . ", " . $urow['fname'];
505 echo " </select>\n";
507 </td>
508 </tr>
509 </tr>
510 <td class='label'>
511 <?php echo xlt('Aging Columns') ?>:
512 </td>
513 <td>
514 <input type='text' name='form_age_cols' size='2' value='<?php echo attr($form_age_cols); ?>' />
515 </td>
516 <td class='label'>
517 <?php echo xlt('Days/Col') ?>:
518 </td>
519 <td>
520 <input type='text' name='form_age_inc' size='3' value='<?php echo attr($form_age_inc); ?>' />
521 </td>
522 </tr>
525 </table>
526 </td>
527 </tr>
528 </table>
530 </div>
532 </td>
533 <td align='left' valign='middle' height="100%">
534 <table style='border-left:1px solid; width:100%; height:100%' >
535 <tr>
536 <td>
537 <div style='margin-left:15px'>
538 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
539 <span>
540 <?php echo xlt('Submit'); ?>
541 </span>
542 </a>
544 <?php if ($_POST['form_refresh']) { ?>
545 <a href='#' class='css_button' onclick='window.print()'>
546 <span>
547 <?php echo xlt('Print'); ?>
548 </span>
549 </a>
550 <?php } ?>
551 </div>
552 </td>
553 </tr>
554 </table>
555 </td>
556 </tr>
557 </table>
558 </div>
561 <?php
563 } // end not form_csvexport
565 if ($_POST['form_refresh'] || $_POST['form_export'] || $_POST['form_csvexport']) {
566 $rows = array();
567 $where = "";
569 if ($INTEGRATED_AR) {
570 $sqlArray = array();
571 if ($_POST['form_export'] || $_POST['form_csvexport']) {
573 $where = "( 1 = 2";
574 foreach ($_POST['form_cb'] as $key => $value) {
575 list($key_newval['pid'], $key_newval['encounter']) = explode(".", $key);
576 $newkey = $key_newval['pid'];
577 $newencounter = $key_newval['encounter'];
578 # added this condition to handle the downloading of individual invoices (TLH)
579 if($_POST['form_individual'] ==1){
580 $where .= " OR f.encounter = ? ";
581 array_push($sqlArray, $newencounter);
583 else
585 $where .= " OR f.pid = ? ";
586 array_push($sqlArray, $newkey);
589 $where .= ' )';
591 if ($form_date) {
592 if ($where) $where .= " AND ";
593 if ($form_to_date) {
594 $where .= "f.date >= ? AND f.date <= ? ";
595 array_push($sqlArray, $form_date.' 00:00:00', $form_to_date.' 23:59:59');
597 else {
598 $where .= "f.date >= ? AND f.date <= ? ";
599 array_push($sqlArray, $form_date.' 00:00:00', $form_date.' 23:59:59');
602 if ($form_facility) {
603 if ($where) $where .= " AND ";
604 $where .= "f.facility_id = ? ";
605 array_push($sqlArray, $form_facility);
607 # added for filtering by provider (TLH)
608 if ($form_provider) {
609 if ($where) $where .= " AND ";
610 $where .= "f.provider_id = ? ";
611 array_push($sqlArray, $form_provider);
614 if (! $where) {
615 $where = "1 = 1";
617 # added provider from encounter to the query (TLH)
618 $query = "SELECT f.id, f.date, f.pid, CONCAT(w.lname, ', ', w.fname) AS provider_id, f.encounter, f.last_level_billed, " .
619 "f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, " .
620 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
621 "p.postal_code, p.phone_home, p.ss, p.genericname2, p.genericval2, " .
622 "p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, " .
623 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
624 "b.pid = f.pid AND b.encounter = f.encounter AND " .
625 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
626 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
627 "b.pid = f.pid AND b.encounter = f.encounter AND " .
628 "b.activity = 1 AND b.code_type = 'COPAY' ) AS copays, " .
629 "( SELECT SUM(s.fee) FROM drug_sales AS s WHERE " .
630 "s.pid = f.pid AND s.encounter = f.encounter ) AS sales, " .
631 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
632 "a.pid = f.pid AND a.encounter = f.encounter ) AS payments, " .
633 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
634 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
635 "FROM form_encounter AS f " .
636 "JOIN patient_data AS p ON p.pid = f.pid " .
637 "LEFT OUTER JOIN users AS u ON u.id = p.ref_providerID " .
638 "LEFT OUTER JOIN users AS w ON w.id = f.provider_id " .
639 "WHERE $where " .
640 "ORDER BY f.pid, f.encounter";
642 $eres = sqlStatement($query, $sqlArray);
644 while ($erow = sqlFetchArray($eres)) {
645 $patient_id = $erow['pid'];
646 $encounter_id = $erow['encounter'];
647 $pt_balance = $erow['charges'] + $erow['sales'] + $erow['copays'] - $erow['payments'] - $erow['adjustments'];
648 $pt_balance = 0 + sprintf("%.2f", $pt_balance); // yes this seems to be necessary
649 $svcdate = substr($erow['date'], 0, 10);
651 if ($_POST['form_refresh'] && ! $is_all) {
652 if ($pt_balance == 0) continue;
654 if ($_POST['form_category'] == 'Credits') {
655 if ($pt_balance > 0) continue;
658 // If we have not yet billed the patient, then compute $duncount as a
659 // negative count of the number of insurance plans for which we have not
660 // yet closed out insurance. Here we also compute $insname as the name of
661 // the insurance plan from which we are awaiting payment, and its sequence
662 // number $insposition (1-3).
663 $last_level_closed = $erow['last_level_closed'];
664 $duncount = $erow['stmt_count'];
665 $payerids = array();
666 $insposition = 0;
667 $insname = '';
668 if (! $duncount) {
669 for ($i = 1; $i <= 3; ++$i) {
670 $tmp = arGetPayerID($patient_id, $svcdate, $i);
671 if (empty($tmp)) break;
672 $payerids[] = $tmp;
674 $duncount = $last_level_closed - count($payerids);
675 if ($duncount < 0) {
676 if (!empty($payerids[$last_level_closed])) {
677 $insname = getInsName($payerids[$last_level_closed]);
678 $insposition = $last_level_closed + 1;
683 // Skip invoices not in the desired "Due..." category.
685 if ($is_due_ins && $duncount >= 0) continue;
686 if ($is_due_pt && $duncount < 0) continue;
688 // echo "<!-- " . $erow['encounter'] . ': ' . $erow['charges'] . ' + ' . $erow['sales'] . ' + ' . $erow['copays'] . ' - ' . $erow['payments'] . ' - ' . $erow['adjustments'] . " -->\n"; // debugging
690 // An invoice is due from the patient if money is owed and we are
691 // not waiting for insurance to pay.
692 $isduept = ($duncount >= 0) ? " checked" : "";
694 $row = array();
696 $row['id'] = $erow['id'];
697 $row['invnumber'] = "$patient_id.$encounter_id";
698 $row['custid'] = $patient_id;
699 $row['name'] = $erow['fname'] . ' ' . $erow['lname'];
700 $row['address1'] = $erow['street'];
701 $row['city'] = $erow['city'];
702 $row['state'] = $erow['state'];
703 $row['zipcode'] = $erow['postal_code'];
704 $row['phone'] = $erow['phone_home'];
705 $row['duncount'] = $duncount;
706 $row['dos'] = $svcdate;
707 $row['ss'] = $erow['ss'];
708 $row['DOB'] = $erow['DOB'];
709 $row['pubpid'] = $erow['pubpid'];
710 $row['billnote'] = ($erow['genericname2'] == 'Billing') ? $erow['genericval2'] : '';
711 $row['referrer'] = $erow['referrer'];
712 $row['provider'] = $erow['provider_id'];
713 $row['irnumber'] = $erow['invoice_refno'];
715 // Also get the primary insurance company name whenever there is one.
716 $row['ins1'] = '';
717 if ($insposition == 1) {
718 $row['ins1'] = $insname;
719 } else {
720 if (empty($payerids)) {
721 $tmp = arGetPayerID($patient_id, $svcdate, 1);
722 if (!empty($tmp)) $payerids[] = $tmp;
724 if (!empty($payerids)) {
725 $row['ins1'] = getInsName($payerids[0]);
729 // This computes the invoice's total original charges and adjustments,
730 // date of last activity, and determines if insurance has responded to
731 // all billing items.
732 $invlines = ar_get_invoice_summary($patient_id, $encounter_id, true);
734 // if ($encounter_id == 185) { // debugging
735 // echo "\n<!--\n";
736 // print_r($invlines);
737 // echo "\n-->\n";
738 // }
740 $row['charges'] = 0;
741 $row['adjustments'] = 0;
742 $row['paid'] = 0;
743 $ins_seems_done = true;
744 $ladate = $svcdate;
745 foreach ($invlines as $key => $value) {
746 $row['charges'] += $value['chg'] + $value['adj'];
747 $row['adjustments'] += 0 - $value['adj'];
748 $row['paid'] += $value['chg'] - $value['bal'];
749 foreach ($value['dtl'] as $dkey => $dvalue) {
750 $dtldate = trim(substr($dkey, 0, 10));
751 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
753 $lckey = strtolower($key);
754 if ($lckey == 'co-pay' || $lckey == 'claim') continue;
755 if (count($value['dtl']) <= 1) $ins_seems_done = false;
758 // Simulating ar.amount in SQL-Ledger which is charges with adjustments:
759 $row['amount'] = $row['charges'] + $row['adjustments'];
761 $row['billing_errmsg'] = '';
762 if ($is_due_ins && $last_level_closed < 1 && $ins_seems_done)
763 $row['billing_errmsg'] = 'Ins1 seems done';
764 else if ($last_level_closed >= 1 && !$ins_seems_done)
765 $row['billing_errmsg'] = 'Ins1 seems not done';
767 $row['ladate'] = $ladate;
769 // Compute number of days since last activity.
770 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
771 substr($ladate, 8, 2), substr($ladate, 0, 4));
772 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
774 // Look up insurance policy number if we need it.
775 if ($form_cb_policy) {
776 $instype = ($insposition == 2) ? 'secondary' : (($insposition == 3) ? 'tertiary' : 'primary');
777 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
778 "pid = ? AND type = ? AND date <= ? " .
779 "ORDER BY date DESC LIMIT 1", array($patient_id, $instype, $svcdate));
780 $row['policy'] = $insrow['policy_number'];
783 $ptname = $erow['lname'] . ", " . $erow['fname'];
784 if ($erow['mname']) $ptname .= " " . substr($erow['mname'], 0, 1);
786 if (!$is_due_ins ) $insname = '';
787 $rows[$insname . '|' . $ptname . '|' . $encounter_id] = $row;
788 } // end while
790 } // end $INTEGRATED_AR
791 else {
792 if ($_POST['form_export'] || $_POST['form_csvexport']) {
793 $where = "( 1 = 2";
794 foreach ($_POST['form_cb'] as $key => $value) {
795 list($key_newval['pid'], $key_newval['encounter']) = explode(".", $key);
796 $newkey = $key_newval['pid'];
797 $newencounter = $key_newval['encounter'];
798 # added this condition to handle the downloading of individual invoices (TLH)
799 if($_POST['form_individual'] ==1){
800 $where .= " OR f.encounter = $newencounter ";
802 else
804 $where .= " OR f.pid = $newkey ";
807 $where .= ' )';
810 if ($form_date) {
811 if ($where) $where .= " AND ";
812 $date1 = substr($form_date, 0, 4) . substr($form_date, 5, 2) .
813 substr($form_date, 8, 2);
814 if ($form_to_date) {
815 $date2 = substr($form_to_date, 0, 4) . substr($form_to_date, 5, 2) .
816 substr($form_to_date, 8, 2);
817 $where .= "((CAST (substring(ar.invnumber from position('.' in ar.invnumber) + 1 for 8) AS integer) " .
818 "BETWEEN '$date1' AND '$date2')";
819 $tmp = "date >= '$form_date' AND date <= '$form_to_date'";
821 else {
822 // This catches old converted invoices where we have no encounters:
823 $where .= "(ar.invnumber LIKE '%.$date1'";
824 $tmp = "date = '$form_date'";
826 // Pick out the encounters from MySQL with the desired DOS:
827 $rez = sqlStatement("SELECT pid, encounter FROM form_encounter WHERE $tmp");
828 while ($row = sqlFetchArray($rez)) {
829 $where .= " OR ar.invnumber = '" . $row['pid'] . "." . $row['encounter'] . "'";
831 $where .= ")";
834 if (! $where) {
835 $where = "1 = 1";
838 // Instead of the subselects in the following query, we will call
839 // get_invoice_summary() in order to get data at the procedure level and
840 // thus decide if insurance appears to be done with each invoice.
842 $query = "SELECT ar.id, ar.invnumber, ar.duedate, ar.amount, ar.paid, " .
843 "ar.intnotes, ar.notes, ar.shipvia, " .
844 "customer.id AS custid, customer.name, customer.address1, " .
845 "customer.city, customer.state, customer.zipcode, customer.phone " .
846 // ", (SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
847 // "invoice.trans_id = ar.id AND invoice.fxsellprice > 0) AS charges, " .
848 // "(SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
849 // "invoice.trans_id = ar.id AND invoice.fxsellprice < 0) AS adjustments " .
850 "FROM ar JOIN customer ON customer.id = ar.customer_id " .
851 "WHERE ( $where ) ";
852 if ($_POST['form_refresh'] && ! $is_all) {
853 $query .= "AND ar.amount != ar.paid ";
855 $query .= "ORDER BY ar.invnumber";
857 // echo "<!-- $query -->\n"; // debugging
859 $t_res = SLQuery($query);
860 if ($sl_err) die($sl_err);
861 $num_invoices = SLRowCount($t_res);
863 //////////////////////////////////////////////////////////////////
865 for ($irow = 0; $irow < $num_invoices; ++$irow) {
866 $row = SLGetRow($t_res, $irow);
868 // If a facility was specified then skip invoices whose encounters
869 // do not indicate that facility.
870 if ($form_facility) {
871 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
872 $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
873 "pid = ? AND encounter = ? AND " .
874 "facility_id = ? ", array($patient_id, $encounter_id, $form_facility));
875 if (empty($tmp['count'])) continue;
878 if ($form_provider) {
879 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
880 $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
881 "pid = ? AND encounter = ? AND " .
882 "provider_id = ? ", array($patient_id, $encounter_id, $form_provider));
884 if (empty($tmp['count'])) continue;
887 $pt_balance = sprintf("%.2f",$row['amount']) - sprintf("%.2f",$row['paid']);
889 if ($_POST['form_category'] == 'Credits') {
890 if ($pt_balance > 0) continue;
893 // $duncount was originally supposed to be the number of times that
894 // the patient was sent a statement for this invoice.
896 $duncount = substr_count(strtolower($row['intnotes']), "statement sent");
898 // But if we have not yet billed the patient, then compute $duncount as a
899 // negative count of the number of insurance plans for which we have not
900 // yet closed out insurance. Here we also compute $insname as the name of
901 // the insurance plan from which we are awaiting payment, and its sequence
902 // number $insposition (1-3).
904 $insname = '';
905 $insposition = 0;
906 $inseobs = strtolower($row['shipvia']);
907 $insgot = strtolower($row['notes']);
908 if (! $duncount) {
909 foreach (array('ins1', 'ins2', 'ins3') as $value) {
910 $i = strpos($insgot, $value);
911 if ($i !== false && strpos($inseobs, $value) === false) {
912 --$duncount;
913 if (!$insname && $is_due_ins) {
914 $j = strpos($insgot, "\n", $i);
915 if (!$j) $j = strlen($insgot);
916 $insname = trim(substr($row['notes'], $i + 5, $j - $i - 5));
917 $insposition = substr($value, 3); // 1, 2 or 3
922 $row['insname'] = $insname;
924 // Also get the primary insurance company name whenever there is one.
925 $row['ins1'] = '';
926 $i = strpos($insgot, 'ins1');
927 if ($i !== false) {
928 $j = strpos($insgot, "\n", $i);
929 if (!$j) $j = strlen($insgot);
930 $row['ins1'] = trim(substr($row['notes'], $i + 5, $j - $i - 5));
933 // An invoice is now due from the patient if money is owed and we are
934 // not waiting for insurance to pay. We no longer look at the due date
935 // for this.
937 $isduept = ($duncount >= 0) ? " checked" : "";
939 // Skip invoices not in the desired "Due..." category.
941 if ($is_due_ins && $duncount >= 0) continue;
942 if ($is_due_pt && $duncount < 0) continue;
944 $row['duncount'] = $duncount;
946 // Determine the date of service. An 8-digit encounter number is
947 // presumed to be a date of service imported during conversion.
948 // Otherwise look it up in the form_encounter table.
950 $svcdate = "";
951 list($pid, $encounter) = explode(".", $row['invnumber']);
952 if (strlen($encounter) == 8) {
953 $svcdate = substr($encounter, 0, 4) . "-" . substr($encounter, 4, 2) .
954 "-" . substr($encounter, 6, 2);
956 else if ($encounter) {
957 $tmp = sqlQuery("SELECT date FROM form_encounter WHERE " .
958 "encounter = ? ", array($encounter));
959 $svcdate = substr($tmp['date'], 0, 10);
962 $row['dos'] = $svcdate;
964 // This computes the invoice's total original charges and adjustments,
965 // date of last activity, and determines if insurance has responded to
966 // all billing items.
968 $invlines = get_invoice_summary($row['id'], true);
969 $row['charges'] = 0;
970 $row['adjustments'] = 0;
971 $ins_seems_done = true;
972 $ladate = $svcdate;
973 foreach ($invlines as $key => $value) {
974 $row['charges'] += $value['chg'] + $value['adj'];
975 $row['adjustments'] += 0 - $value['adj'];
976 foreach ($value['dtl'] as $dkey => $dvalue) {
977 $dtldate = trim(substr($dkey, 0, 10));
978 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
980 $lckey = strtolower($key);
981 if ($lckey == 'co-pay' || $lckey == 'claim') continue;
982 if (count($value['dtl']) <= 1) $ins_seems_done = false;
984 $row['billing_errmsg'] = '';
985 if ($is_due_ins && strpos($inseobs, 'ins1') === false && $ins_seems_done)
986 $row['billing_errmsg'] = 'Ins1 seems done';
987 else if (strpos($inseobs, 'ins1') !== false && !$ins_seems_done)
988 $row['billing_errmsg'] = 'Ins1 seems not done';
990 $row['ladate'] = $ladate;
992 // Compute number of days since last activity.
993 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
994 substr($ladate, 8, 2), substr($ladate, 0, 4));
995 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
997 $pdrow = sqlQuery("SELECT pd.fname, pd.lname, pd.mname, pd.ss, " .
998 "pd.genericname2, pd.genericval2, pd.pid, pd.pubpid, pd.DOB, " .
999 "CONCAT(u.lname, ', ', u.fname) AS referrer FROM " .
1000 "integration_mapping AS im, patient_data AS pd " .
1001 "LEFT OUTER JOIN users AS u ON u.id = pd.ref_providerID " .
1002 "WHERE im.foreign_id = ? AND " .
1003 "im.foreign_table = 'customer' AND " .
1004 "pd.id = im.local_id", array($row['custid']));
1006 $row['ss'] = $pdrow['ss'];
1007 $row['DOB'] = $pdrow['DOB'];
1008 $row['pubpid'] = $pdrow['pubpid'];
1009 $row['billnote'] = ($pdrow['genericname2'] == 'Billing') ? $pdrow['genericval2'] : '';
1010 $row['referrer'] = $pdrow['referrer'];
1012 $ptname = $pdrow['lname'] . ", " . $pdrow['fname'];
1013 if ($pdrow['mname']) $ptname .= " " . substr($pdrow['mname'], 0, 1);
1015 // Look up insurance policy number if we need it.
1016 if ($form_cb_policy) {
1017 $patient_id = $pdrow['pid'];
1018 $instype = ($insposition == 2) ? 'secondary' : (($insposition == 3) ? 'tertiary' : 'primary');
1019 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
1020 "pid = ? AND type = ? AND date <= ? " .
1021 "ORDER BY date DESC LIMIT 1", array($patient_id, $instype, $svcdate));
1022 $row['policy'] = $insrow['policy_number'];
1025 $rows[$insname . '|' . $ptname . '|' . $encounter] = $row;
1026 } // end for
1027 } // end not $INTEGRATED_AR
1029 ksort($rows);
1031 if ($_POST['form_export']) {
1032 echo "<textarea rows='35' cols='100' readonly>";
1034 else if ($_POST['form_csvexport']) {
1035 # CSV headers added conditions if they are checked to display then export them (TLH)
1036 if (true) {
1037 echo '"' . xl('Insurance') . '",';
1038 echo '"' . xl('Name') . '",';
1039 if ($form_cb_ssn)
1041 echo '"' . xl('SSN') . '",';
1043 if ($form_cb_dob)
1045 echo '"' . xl('DOB') . '",';
1047 if ($form_cb_pubid)
1049 echo '"' . xl('Pubid') . '",';
1051 if ($form_cb_policy)
1053 echo '"' . xl('Policy') . '",';
1055 if ($form_cb_phone)
1057 echo '"' . xl('Phone') . '",';
1059 if ($form_cb_city)
1061 echo '"' . xl('City') . '",';
1063 echo '"' . xl('Invoice') . '",';
1064 echo '"' . xl('DOS') . '",';
1065 echo '"' . xl('Referrer') . '",';
1066 echo '"' . xl('Provider') . '",';
1067 echo '"' . xl('Charge') . '",';
1068 echo '"' . xl('Adjust') . '",';
1069 echo '"' . xl('Paid') . '",';
1070 echo '"' . xl('Balance') . '",';
1071 echo '"' . xl('IDays') . '",';
1072 if ($form_cb_err)
1074 echo '"' . xl('LADate') . '",';
1075 echo '"' . xl('Error') . '"' . "\n";
1077 else
1079 echo '"' . xl('LADate') . '"' . "\n";
1083 else {
1086 <div id="report_results">
1087 <table>
1089 <thead>
1090 <?php if ($is_due_ins) { ?>
1091 <th>&nbsp;<?php echo xlt('Insurance')?></th>
1092 <?php } ?>
1093 <?php if (!$is_ins_summary) { ?>
1094 <th>&nbsp;<?php echo xlt('Name')?></th>
1095 <?php } ?>
1096 <?php if ($form_cb_ssn) { ?>
1097 <th>&nbsp;<?php echo xlt('SSN')?></th>
1098 <?php } ?>
1099 <?php if ($form_cb_dob) { ?>
1100 <th>&nbsp;<?php echo xlt('DOB')?></th>
1101 <?php } ?>
1102 <?php if ($form_cb_pubpid) { ?>
1103 <th>&nbsp;<?php echo xlt('ID')?></th>
1104 <?php } ?>
1105 <?php if ($form_cb_policy) { ?>
1106 <th>&nbsp;<?php echo xlt('Policy')?></th>
1107 <?php } ?>
1108 <?php if ($form_cb_phone) { ?>
1109 <th>&nbsp;<?php echo xlt('Phone')?></th>
1110 <?php } ?>
1111 <?php if ($form_cb_city) { ?>
1112 <th>&nbsp;<?php echo xlt('City')?></th>
1113 <?php } ?>
1114 <?php if ($form_cb_ins1 || $form_payer_id) { ?>
1115 <th>&nbsp;<?php echo xlt('Primary Ins')?></th>
1116 <?php } ?>
1117 <?php if ($form_provider) { ?>
1118 <th>&nbsp;<?php echo xlt('Provider')?></th>
1119 <?php } ?>
1120 <?php if ($form_cb_referrer) { ?>
1121 <th>&nbsp;<?php echo xlt('Referrer')?></th>
1122 <?php } ?>
1123 <?php if (!$is_ins_summary) { ?>
1124 <th>&nbsp;<?php echo xlt('Invoice') ?></th>
1125 <th>&nbsp;<?php echo xlt('Svc Date') ?></th>
1126 <?php if ($form_cb_adate) { ?>
1127 <th>&nbsp;<?php echo xlt('Act Date')?></th>
1128 <?php } ?>
1129 <?php } ?>
1130 <th align="right"><?php echo xlt('Charge') ?>&nbsp;</th>
1131 <th align="right"><?php echo xlt('Adjust') ?>&nbsp;</th>
1132 <th align="right"><?php echo xlt('Paid') ?>&nbsp;</th>
1133 <?php
1134 // Generate aging headers if appropriate, else balance header.
1135 if ($form_age_cols) {
1136 for ($c = 0; $c < $form_age_cols;) {
1137 echo " <th class='dehead' align='right'>";
1138 echo $form_age_inc * $c;
1139 if (++$c < $form_age_cols) {
1140 echo "-" . ($form_age_inc * $c - 1);
1141 } else {
1142 echo "+";
1144 echo "</th>\n";
1147 else {
1149 <th align="right"><?php echo xlt('Balance') ?>&nbsp;</th>
1150 <?php
1153 <?php if ($form_cb_idays) { ?>
1154 <th align="right"><?php echo xlt('IDays')?>&nbsp;</th>
1155 <?php } ?>
1156 <?php if (!$is_ins_summary) { ?>
1157 <th align="center"><?php echo xlt('Prv') ?></th>
1158 <th align="center"><?php echo xlt('Sel') ?></th>
1159 <?php } ?>
1160 <?php if ($form_cb_err) { ?>
1161 <th>&nbsp;<?php echo xlt('Error')?></th>
1162 <?php } ?>
1163 </thead>
1165 <?php
1166 } // end not export
1168 $ptrow = array('insname' => '', 'pid' => 0);
1169 $orow = -1;
1171 foreach ($rows as $key => $row) {
1172 list($insname, $ptname, $trash) = explode('|', $key);
1173 list($pid, $encounter) = explode(".", $row['invnumber']);
1174 if ($form_payer_id) {
1175 if ($ins_co_name <> $row['ins1']) continue;
1177 if ($is_ins_summary && $insname != $ptrow['insname']) {
1178 endInsurance($ptrow);
1179 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
1180 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1181 foreach ($row as $key => $value) $ptrow[$key] = $value;
1182 $ptrow['agedbal'] = array();
1184 else if (!$is_ins_summary && ($insname != $ptrow['insname'] || $pid != $ptrow['pid'])) {
1185 // For the report, this will write the patient totals. For the
1186 // collections export this writes everything for the patient:
1187 endPatient($ptrow);
1188 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
1189 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1190 foreach ($row as $key => $value) $ptrow[$key] = $value;
1191 $ptrow['agedbal'] = array();
1192 } else {
1193 $ptrow['amount'] += $row['amount'];
1194 $ptrow['paid'] += $row['paid'];
1195 $ptrow['charges'] += $row['charges'];
1196 $ptrow['adjustments'] += $row['adjustments'];
1197 ++$ptrow['count'];
1201 // Compute invoice balance and aging column number, and accumulate aging.
1202 $balance = $row['charges'] + $row['adjustments'] - $row['paid'];
1203 if ($form_age_cols) {
1204 $agedate = $is_ageby_lad ? $row['ladate'] : $row['dos'];
1205 $agetime = mktime(0, 0, 0, substr($agedate, 5, 2),
1206 substr($agedate, 8, 2), substr($agedate, 0, 4));
1207 $days = floor((time() - $agetime) / (60 * 60 * 24));
1208 $agecolno = min($form_age_cols - 1, max(0, floor($days / $form_age_inc)));
1209 $ptrow['agedbal'][$agecolno] += $balance;
1212 if (!$is_ins_summary && !$_POST['form_export'] && !$_POST['form_csvexport']) {
1213 $in_collections = stristr($row['billnote'], 'IN COLLECTIONS') !== false;
1215 <tr bgcolor='<?php echo attr($bgcolor) ?>'>
1216 <?php
1217 if ($ptrow['count'] == 1) {
1218 if ($is_due_ins) {
1219 echo " <td class='detail'>&nbsp;" . attr($insname) ."</td>\n";
1221 echo " <td class='detail'>&nbsp;" . attr($ptname) ."</td>\n";
1222 if ($form_cb_ssn) {
1223 echo " <td class='detail'>&nbsp;" . attr($row['ss']) . "</td>\n";
1225 if ($form_cb_dob) {
1226 echo " <td class='detail'>&nbsp;" . attr(oeFormatShortDate($row['DOB'])) . "</td>\n";
1228 if ($form_cb_pubpid) {
1229 echo " <td class='detail'>&nbsp;" . attr($row['pubpid']) . "</td>\n";
1231 if ($form_cb_policy) {
1232 echo " <td class='detail'>&nbsp;" . attr($row['policy']) . "</td>\n";
1234 if ($form_cb_phone) {
1235 echo " <td class='detail'>&nbsp;" . attr($row['phone']) . "</td>\n";
1237 if ($form_cb_city) {
1238 echo " <td class='detail'>&nbsp;" . attr($row['city']) . "</td>\n";
1240 if ($form_cb_ins1 || $form_payer_id ) {
1241 echo " <td class='detail'>&nbsp;" . attr($row['ins1']) . "</td>\n";
1243 if ($form_provider) {
1244 echo " <td class='detail'>&nbsp;" . attr($provider_name) . "</td>\n";
1246 if ($form_cb_referrer) {
1247 echo " <td class='detail'>&nbsp;" . attr($row['referrer']) . "</td>\n";
1249 } else {
1250 echo " <td class='detail' colspan='$initial_colspan'>";
1251 echo "&nbsp;</td>\n";
1254 <td class="detail">
1255 &nbsp;<a href="../billing/sl_eob_invoice.php?id=<?php echo attr($row['id']) ?>"
1256 target="_blank"><?php echo empty($row['irnumber']) ? $row['invnumber'] : $row['irnumber']; ?></a>
1257 </td>
1258 <td class="detail">
1259 &nbsp;<?php echo attr(oeFormatShortDate($row['dos'])); ?>
1260 </td>
1261 <?php if ($form_cb_adate) { ?>
1262 <td class='detail'>
1263 &nbsp;<?php echo attr(oeFormatShortDate($row['ladate'])); ?>
1264 </td>
1265 <?php } ?>
1266 <td class="detail" align="right">
1267 <?php attr(bucks($row['charges'])) ?>&nbsp;
1268 </td>
1269 <td class="detail" align="right">
1270 <?php attr(bucks($row['adjustments'])) ?>&nbsp;
1271 </td>
1272 <td class="detail" align="right">
1273 <?php attr(bucks($row['paid'])) ?>&nbsp;
1274 </td>
1275 <?php
1276 if ($form_age_cols) {
1277 for ($c = 0; $c < $form_age_cols; ++$c) {
1278 echo " <td class='detail' align='right'>";
1279 if ($c == $agecolno) {
1280 bucks($balance);
1282 echo "&nbsp;</td>\n";
1285 else {
1287 <td class="detail" align="right"><?php bucks($balance) ?>&nbsp;</td>
1288 <?php
1289 } // end else
1291 <?php
1292 if ($form_cb_idays) {
1293 echo " <td class='detail' align='right'>";
1294 echo attr($row['inactive_days']) . "&nbsp;</td>\n";
1297 <td class="detail" align="center">
1298 <?php echo $row['duncount'] ? $row['duncount'] : "&nbsp;" ?>
1299 </td>
1300 <td class="detail" align="center">
1301 <?php
1302 if ($in_collections) {
1303 echo " <b><font color='red'>IC</font></b>\n";
1304 } else {
1305 echo " <input type='checkbox' name='form_cb[" . attr($row['invnumber']) . "]' />\n";
1308 </td>
1309 <?php
1310 if ($form_cb_err) {
1311 echo " <td class='detail'>&nbsp;";
1312 echo text($row['billing_errmsg']) . "</td>\n";
1315 </tr>
1316 <?php
1317 } // end not export and not insurance summary
1319 else if ($_POST['form_csvexport']) {
1321 # The CSV detail line is written here added conditions for checked items (TLH).
1322 $balance = $row['charges'] + $row['adjustments'] - $row['paid'];
1323 if($balance >0) {
1324 // echo '"' . $insname . '",';
1325 echo '"' . $row['ins1'] . '",';
1326 echo '"' . $ptname . '",';
1327 if ($form_cb_ssn)
1329 echo '"' . $row['ss'] . '",';
1331 if ($form_cb_dob)
1333 echo '"' . oeFormatShortDate($row['DOB']) . '",';
1335 if ($form_cb_pubid)
1337 echo '"' . $row['pubpid'] . '",';
1339 if ($form_cb_policy)
1341 echo '"' . $row['policy'] . '",';
1343 if ($form_cb_phone)
1345 echo '"' . $row['phone'] . '",';
1347 if ($form_cb_city)
1349 echo '"' . $row['city'] . '",';
1351 echo '"' . (empty($row['irnumber']) ? $row['invnumber'] : $row['irnumber']) . '",';
1352 echo '"' . oeFormatShortDate($row['dos']) . '",';
1353 echo '"' . $row['referrer'] . '",';
1354 echo '"' . $row['provider'] . '",';
1355 echo '"' . oeFormatMoney($row['charges']) . '",';
1356 echo '"' . oeFormatMoney($row['adjustments']) . '",';
1357 echo '"' . oeFormatMoney($row['paid']) . '",';
1358 echo '"' . oeFormatMoney($balance) . '",';
1359 echo '"' . $row['inactive_days'] . '",';
1360 if ($form_cb_err)
1362 echo '"' . oeFormatShortDate($row['ladate']) . '",';
1363 echo '"' . $row['billing_errmsg'] . '"' . "\n";
1365 else
1367 echo '"' . oeFormatShortDate($row['ladate']) . '"' . "\n";
1370 } // end $form_csvexport
1372 } // end loop
1374 if ($is_ins_summary)
1375 endInsurance($ptrow);
1376 else
1377 endPatient($ptrow);
1379 if ($_POST['form_export']) {
1380 echo "</textarea>\n";
1381 $alertmsg .= "$export_patient_count patients with total of " .
1382 oeFormatMoney($export_dollars) . " have been exported ";
1383 if ($_POST['form_without']) {
1384 $alertmsg .= "but NOT flagged as in collections.";
1385 } else {
1386 $alertmsg .= "AND flagged as in collections.";
1389 else if ($_POST['form_csvexport']) {
1390 // echo "</textarea>\n";
1391 // $alertmsg .= "$export_patient_count patients representing $" .
1392 // sprintf("%.2f", $export_dollars) . " have been exported.";
1394 else {
1395 echo " <tr bgcolor='#ffffff'>\n";
1396 if ($is_ins_summary) {
1397 echo " <td class='dehead'>&nbsp;" . xlt('Report Totals') . ":</td>\n";
1398 } else {
1399 echo " <td class='detail' colspan='" . attr($initial_colspan) . "'>\n";
1400 echo " &nbsp;</td>\n";
1401 echo " <td class='dehead' colspan='" . attr($final_colspan - 3) .
1402 "'>&nbsp;" . xlt('Report Totals') . ":</td>\n";
1404 echo " <td class='dehead' align='right'>&nbsp;" .
1405 oeFormatMoney($grand_total_charges) . "&nbsp;</td>\n";
1406 echo " <td class='dehead' align='right'>&nbsp;" .
1407 oeFormatMoney($grand_total_adjustments) . "&nbsp;</td>\n";
1408 echo " <td class='dehead' align='right'>&nbsp;" .
1409 oeFormatMoney($grand_total_paid) . "&nbsp;</td>\n";
1410 if ($form_age_cols) {
1411 for ($c = 0; $c < $form_age_cols; ++$c) {
1412 echo " <td class='dehead' align='right'>" .
1413 oeFormatMoney($grand_total_agedbal[$c]) . "&nbsp;</td>\n";
1416 else {
1417 echo " <td class='dehead' align='right'>" .
1418 oeFormatMoney($grand_total_charges +
1419 $grand_total_adjustments - $grand_total_paid) . "&nbsp;</td>\n";
1421 if ($form_cb_idays) echo " <td class='detail'>&nbsp;</td>\n";
1422 if (!$is_ins_summary) echo " <td class='detail' colspan='2'>&nbsp;</td>\n";
1423 if ($form_cb_err) echo " <td class='detail'>&nbsp;</td>\n";
1424 echo " </tr>\n";
1425 echo "</table>\n";
1426 echo "</div>\n";
1428 } // end if form_refresh
1430 if (!$INTEGRATED_AR) SLClose();
1432 if (!$_POST['form_csvexport']) {
1433 if (!$_POST['form_export']) {
1436 <div style='float;margin-top:5px'>
1438 <a href='javascript:;' class='css_button' onclick='checkAll(true)'><span><?php echo xlt('Select All'); ?></span></a>
1439 <a href='javascript:;' class='css_button' onclick='checkAll(false)'><span><?php echo xlt('Clear All'); ?></span></a>
1440 <a href='javascript:;' class='css_button' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
1441 <span><?php echo xlt('Export Selected as CSV'); ?></span>
1442 </a>
1443 <a href='javascript:;' class='css_button' onclick='$("#form_export").attr("value","true"); $("#theform").submit();'>
1444 <span><?php echo xlt('Export Selected to Collections'); ?></span>
1445 </a>
1446 </div>
1448 <div style='float:left'>
1449 <label><input type='checkbox' name='form_individual' value='1' /> <?php echo xlt('Export Individual Invoices') ?>&nbsp;&nbsp;</label>
1450 </div>
1452 <div style='float:left'>
1453 <label><input type='checkbox' name='form_without' value='1' /> <?php echo xlt('Without Update') ?></label>
1454 </div>
1456 <?php
1457 } // end not export
1459 </form>
1460 </center>
1461 <script language="JavaScript">
1462 <?php
1463 if ($alertmsg) {
1464 echo "alert('" . addslashes($alertmsg) . "');\n";
1467 </script>
1468 </body>
1469 <!-- stuff for the popup calendar -->
1470 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
1471 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
1472 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
1473 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
1474 <script language="Javascript">
1475 Calendar.setup({inputField:"form_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
1476 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
1477 </script>
1478 </html>
1479 <?php
1480 } // end not form_csvexport