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-2016 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>;.
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/invoice_summary.inc.php");
34 require_once("../../library/sl_eob.inc.php");
35 require_once("../../library/formatting.inc.php");
36 require_once "$srcdir/options.inc.php";
41 $export_patient_count = 0;
44 $today = date("Y-m-d");
46 $form_date = fixDate($_POST['form_date'], "");
47 $form_to_date = fixDate($_POST['form_to_date'], "");
48 $is_ins_summary = $_POST['form_category'] == 'Ins Summary';
49 $is_due_ins = ($_POST['form_category'] == 'Due Ins') ||
$is_ins_summary;
50 $is_due_pt = $_POST['form_category'] == 'Due Pt';
51 $is_all = $_POST['form_category'] == 'All';
52 $is_ageby_lad = strpos($_POST['form_ageby'], 'Last') !== false;
53 $form_facility = $_POST['form_facility'];
54 $form_provider = $_POST['form_provider'];
55 $form_payer_id = $_POST['form_payer_id'];
57 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
58 if ($is_ins_summary) {
61 $form_cb_pubpid = false;
62 $form_cb_adate = false;
63 $form_cb_policy = false;
64 $form_cb_phone = false;
65 $form_cb_city = false;
66 $form_cb_ins1 = false;
67 $form_cb_referrer = false;
68 $form_cb_idays = false;
71 $form_cb_ssn = $_POST['form_cb_ssn'] ?
true : false;
72 $form_cb_dob = $_POST['form_cb_dob'] ?
true : false;
73 $form_cb_pubpid = $_POST['form_cb_pubpid'] ?
true : false;
74 $form_cb_adate = $_POST['form_cb_adate'] ?
true : false;
75 $form_cb_policy = $_POST['form_cb_policy'] ?
true : false;
76 $form_cb_phone = $_POST['form_cb_phone'] ?
true : false;
77 $form_cb_city = $_POST['form_cb_city'] ?
true : false;
78 $form_cb_ins1 = $_POST['form_cb_ins1'] ?
true : false;
79 $form_cb_referrer = $_POST['form_cb_referrer'] ?
true : false;
80 $form_cb_idays = $_POST['form_cb_idays'] ?
true : false;
81 $form_cb_err = $_POST['form_cb_err'] ?
true : false;
86 $form_cb_pubpid = false;
87 $form_cb_adate = false;
88 $form_cb_policy = false;
89 $form_cb_phone = true;
90 $form_cb_city = false;
91 $form_cb_ins1 = false;
92 $form_cb_referrer = false;
93 $form_cb_idays = false;
96 $form_age_cols = (int) $_POST['form_age_cols'];
97 $form_age_inc = (int) $_POST['form_age_inc'];
98 if ($form_age_cols > 0 && $form_age_cols < 50) {
99 if ($form_age_inc <= 0) $form_age_inc = 30;
105 $initial_colspan = 1;
106 if ($is_due_ins ) ++
$initial_colspan;
107 if ($form_cb_ssn ) ++
$initial_colspan;
108 if ($form_cb_dob ) ++
$initial_colspan;
109 if ($form_cb_pubpid ) ++
$initial_colspan;
110 if ($form_cb_policy ) ++
$initial_colspan;
111 if ($form_cb_phone ) ++
$initial_colspan;
112 if ($form_cb_city ) ++
$initial_colspan;
113 if ($form_cb_ins1 ) ++
$initial_colspan;
114 if ($form_cb_referrer) ++
$initial_colspan;
115 if ($form_provider ) ++
$initial_colspan;
116 if ($form_payer_id ) ++
$initial_colspan;
118 $final_colspan = $form_cb_adate ?
6 : 5;
120 $grand_total_charges = 0;
121 $grand_total_adjustments = 0;
122 $grand_total_paid = 0;
123 $grand_total_agedbal = array();
124 for ($c = 0; $c < $form_age_cols; ++
$c) $grand_total_agedbal[$c] = 0;
127 function bucks($amount) {
129 echo oeFormatMoney($amount); // was printf("%.2f", $amount);
132 function endPatient($ptrow) {
133 global $export_patient_count, $export_dollars, $bgcolor;
134 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
135 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
136 global $initial_colspan, $final_colspan, $form_cb_idays, $form_cb_err;
138 if (!$ptrow['pid']) return;
140 $pt_balance = $ptrow['amount'] - $ptrow['paid'];
142 if ($_POST['form_export']) {
143 // This is a fixed-length format used by Transworld Systems. Your
144 // needs will surely be different, so consider this just an example.
146 echo "1896H"; // client number goes here
147 echo "000"; // filler
148 echo sprintf("%-30s", substr($ptrow['ptname'], 0, 30));
149 echo sprintf("%-30s", " ");
150 echo sprintf("%-30s", substr($ptrow['address1'], 0, 30));
151 echo sprintf("%-15s", substr($ptrow['city'], 0, 15));
152 echo sprintf("%-2s", substr($ptrow['state'], 0, 2));
153 echo sprintf("%-5s", $ptrow['zipcode'] ?
substr($ptrow['zipcode'], 0, 5) : '00000');
154 echo "1"; // service code
155 echo sprintf("%010.0f", $ptrow['pid']); // transmittal number = patient id
157 echo sprintf("%-15s", substr($ptrow['ss'], 0, 15));
158 echo substr($ptrow['dos'], 5, 2) . substr($ptrow['dos'], 8, 2) . substr($ptrow['dos'], 2, 2);
159 echo sprintf("%08.0f", $pt_balance * 100);
160 echo sprintf("%-9s\n", " ");
162 if (!$_POST['form_without']) {
163 sqlStatement("UPDATE patient_data SET " .
164 "billing_note = CONCAT('IN COLLECTIONS " . date("Y-m-d") . "', billing_note) " .
165 "WHERE pid = ? ", array($ptrow['pid']));
167 $export_patient_count +
= 1;
168 $export_dollars +
= $pt_balance;
170 else if ($_POST['form_csvexport']) {
171 $export_patient_count +
= 1;
172 $export_dollars +
= $pt_balance;
175 if ($ptrow['count'] > 1) {
176 echo " <tr bgcolor='$bgcolor'>\n";
177 /***************************************************************
178 echo " <td class='detail' colspan='$initial_colspan'>";
179 echo " </td>\n";
180 echo " <td class='detotal' colspan='$final_colspan'> Total Patient Balance:</td>\n";
181 ***************************************************************/
182 echo " <td class='detotal' colspan='" . ($initial_colspan +
$final_colspan) .
183 "'> " . xlt('Total Patient Balance') . ":</td>\n";
184 /**************************************************************/
185 if ($form_age_cols) {
186 for ($c = 0; $c < $form_age_cols; ++
$c) {
187 echo " <td class='detotal' align='right'> " .
188 oeFormatMoney($ptrow['agedbal'][$c]) . " </td>\n";
192 echo " <td class='detotal' align='right'> " .
193 oeFormatMoney($pt_balance) . " </td>\n";
195 if ($form_cb_idays) echo " <td class='detail'> </td>\n";
196 echo " <td class='detail' colspan='2'> </td>\n";
197 if ($form_cb_err) echo " <td class='detail'> </td>\n";
201 $grand_total_charges +
= $ptrow['charges'];
202 $grand_total_adjustments +
= $ptrow['adjustments'];
203 $grand_total_paid +
= $ptrow['paid'];
204 for ($c = 0; $c < $form_age_cols; ++
$c) {
205 $grand_total_agedbal[$c] +
= $ptrow['agedbal'][$c];
209 function endInsurance($insrow) {
210 global $export_patient_count, $export_dollars, $bgcolor;
211 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
212 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
213 global $initial_colspan, $form_cb_idays, $form_cb_err;
214 if (!$insrow['pid']) return;
215 $ins_balance = $insrow['amount'] - $insrow['paid'];
216 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
217 // No exporting of insurance summaries.
218 $export_patient_count +
= 1;
219 $export_dollars +
= $ins_balance;
222 echo " <tr bgcolor='$bgcolor'>\n";
223 echo " <td class='detail'>" . text($insrow['insname']) . "</td>\n";
224 echo " <td class='detotal' align='right'> " .
225 oeFormatMoney($insrow['charges']) . " </td>\n";
226 echo " <td class='detotal' align='right'> " .
227 oeFormatMoney($insrow['adjustments']) . " </td>\n";
228 echo " <td class='detotal' align='right'> " .
229 oeFormatMoney($insrow['paid']) . " </td>\n";
230 if ($form_age_cols) {
231 for ($c = 0; $c < $form_age_cols; ++
$c) {
232 echo " <td class='detotal' align='right'> " .
233 oeFormatMoney($insrow['agedbal'][$c]) . " </td>\n";
237 echo " <td class='detotal' align='right'> " .
238 oeFormatMoney($ins_balance) . " </td>\n";
242 $grand_total_charges +
= $insrow['charges'];
243 $grand_total_adjustments +
= $insrow['adjustments'];
244 $grand_total_paid +
= $insrow['paid'];
245 for ($c = 0; $c < $form_age_cols; ++
$c) {
246 $grand_total_agedbal[$c] +
= $insrow['agedbal'][$c];
250 function getInsName($payerid) {
251 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = ? ", array($payerid));
255 // In the case of CSV export only, a download will be forced.
256 if ($_POST['form_csvexport']) {
257 header("Pragma: public");
258 header("Expires: 0");
259 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
260 header("Content-Type: application/force-download");
261 header("Content-Disposition: attachment; filename=collections_report.csv");
262 header("Content-Description: File Transfer");
268 <?php
if (function_exists('html_header_show')) html_header_show(); ?
>
269 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
270 <title
><?php
echo xlt('Collections Report')?
></title
>
271 <style type
="text/css">
278 #report_parameters_daterange {
287 /* specifically exclude some from the screen */
289 #report_parameters_daterange {
297 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-9-1/index.js"></script
>
298 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
300 <script language
="JavaScript">
302 $
(document
).ready(function() {
303 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
304 var win
= top
.printLogSetup ? top
: opener
.top
;
305 win
.printLogSetup(document
.getElementById('printbutton'));
308 function checkAll(checked
) {
309 var f
= document
.forms
[0];
310 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
311 var ename
= f
.elements
[i
].name
;
312 if (ename
.indexOf('form_cb[') == 0)
313 f
.elements
[i
].checked
= checked
;
321 <body
class="body_top">
323 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Collections'); ?
></span
>
325 <form method
='post' action
='collections_report.php' enctype
='multipart/form-data' id
='theform'>
327 <div id
="report_parameters">
329 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
330 <input type
='hidden' name
='form_export' id
='form_export' value
=''/>
331 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
336 <div style
='float:left'>
343 <td
><?php
echo xlt('Displayed Columns') ?
>:</td
>
347 <label
><input type
='checkbox' name
='form_cb_ssn'<?php
if ($form_cb_ssn) echo ' checked'; ?
>>
348 <?php
echo xlt('SSN') ?
> 
;</label
>
351 <label
><input type
='checkbox' name
='form_cb_dob'<?php
if ($form_cb_dob) echo ' checked'; ?
>>
352 <?php
echo xlt('DOB') ?
> 
;</label
>
355 <label
><input type
='checkbox' name
='form_cb_pubpid'<?php
if ($form_cb_pubpid) echo ' checked'; ?
>>
356 <?php
echo xlt('ID') ?
> 
;</label
>
359 <label
><input type
='checkbox' name
='form_cb_policy'<?php
if ($form_cb_policy) echo ' checked'; ?
>>
360 <?php
echo xlt('Policy') ?
> 
;</label
>
363 <label
><input type
='checkbox' name
='form_cb_phone'<?php
if ($form_cb_phone) echo ' checked'; ?
>>
364 <?php
echo xlt('Phone') ?
> 
;</label
>
367 <label
><input type
='checkbox' name
='form_cb_city'<?php
if ($form_cb_city) echo ' checked'; ?
>>
368 <?php
echo xlt('City') ?
> 
;</label
>
373 <label
><input type
='checkbox' name
='form_cb_ins1'<?php
if ($form_cb_ins1) echo ' checked'; ?
>>
374 <?php
echo xlt('Primary Ins') ?
> 
;</label
>
377 <label
><input type
='checkbox' name
='form_cb_referrer'<?php
if ($form_cb_referrer) echo ' checked'; ?
>>
378 <?php
echo xlt('Referrer') ?
> 
;</label
>
381 <label
><input type
='checkbox' name
='form_cb_adate'<?php
if ($form_cb_adate) echo ' checked'; ?
>>
382 <?php
echo xlt('Act Date') ?
> 
;</label
>
385 <label
><input type
='checkbox' name
='form_cb_idays'<?php
if ($form_cb_idays) echo ' checked'; ?
>>
386 <?php
echo xlt('Inactive Days') ?
> 
;</label
>
389 <label
><input type
='checkbox' name
='form_cb_err'<?php
if ($form_cb_err) echo ' checked'; ?
>>
390 <?php
echo xlt('Errors') ?
></label
>
402 <?php
echo xlt('Service Date'); ?
>:
405 <input type
='text' name
='form_date' id
="form_date" size
='10' value
='<?php echo attr($form_date) ?>'
406 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
407 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
408 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
409 title
='<?php echo xla('Click here to choose a date
'); ?>'>
412 <?php
echo xlt('To'); ?
>:
415 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
416 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
417 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
418 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
419 title
='<?php echo xla('Click here to choose a date
'); ?>'>
422 <select name
='form_category'>
424 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) {
425 echo " <option value='" . attr($key) . "'";
426 if ($_POST['form_category'] == $key) echo " selected";
427 echo ">" . text($value) . "</option>\n";
438 <?php
echo xlt('Facility'); ?
>:
441 <?php
dropdown_facility($form_facility, 'form_facility', false); ?
>
445 <?php
echo xlt('Payor'); ?
>:
448 <?php
# added dropdown for payors (TLH)
449 $insurancei = getInsuranceProviders();
450 echo " <select name='form_payer_id'>\n";
451 echo " <option value='0'>-- " . xlt('All') . " --</option>\n";
452 foreach ($insurancei as $iid => $iname) {
453 echo "<option value='" . attr($iid) . "'";
454 if ($iid == $_POST['form_payer_id']) echo " selected";
455 echo ">" . text($iname) . "</option>\n";
456 if ($iid == $_POST['form_payer_id']) $ins_co_name = $iname;
465 <?php
echo xlt('Age By') ?
>:
468 <select name
='form_ageby'>
470 foreach (array( 'Service Date'=>xl('Service Date'), 'Last Activity Date'=>xl('Last Activity Date')) as $key => $value) {
471 echo " <option value='" . attr($key) . "'";
472 if ($_POST['form_ageby'] == $value) echo " selected";
473 echo ">" . text($value) . "</option>\n";
480 <?php
echo xlt('Provider') ?
>:
483 <?php
# Build a drop-down list of providers.
486 $query = "SELECT id, lname, fname FROM users WHERE ".
487 "authorized = 1 ORDER BY lname, fname"; #(CHEMED) facility filter
489 $ures = sqlStatement($query);
491 echo " <select name='form_provider'>\n";
492 echo " <option value=''>-- " . xlt('All') . " --\n";
494 while ($urow = sqlFetchArray($ures)) {
495 $provid = $urow['id'];
496 echo " <option value='" . attr($provid) . "'";
497 if ($provid == $_POST['form_provider']) echo " selected";
498 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
499 if ($provid == $_POST['form_provider']) $provider_name = $urow['lname'] . ", " . $urow['fname'];
508 <?php
echo xlt('Aging Columns') ?
>:
511 <input type
='text' name
='form_age_cols' size
='2' value
='<?php echo attr($form_age_cols); ?>' />
514 <?php
echo xlt('Days/Col') ?
>:
517 <input type
='text' name
='form_age_inc' size
='3' value
='<?php echo attr($form_age_inc); ?>' />
530 <td align
='left' valign
='middle' height
="100%">
531 <table style
='border-left:1px solid; width:100%; height:100%' >
534 <div style
='margin-left:15px'>
535 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
537 <?php
echo xlt('Submit'); ?
>
541 <?php
if ($_POST['form_refresh']) { ?
>
542 <a href
='#' class='css_button' onclick
='window.print()'>
544 <?php
echo xlt('Print'); ?
>
560 } // end not form_csvexport
562 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
566 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
569 foreach ($_POST['form_cb'] as $key => $value) {
570 list($key_newval['pid'], $key_newval['encounter']) = explode(".", $key);
571 $newkey = $key_newval['pid'];
572 $newencounter = $key_newval['encounter'];
573 # added this condition to handle the downloading of individual invoices (TLH)
574 if($_POST['form_individual'] ==1){
575 $where .= " OR f.encounter = ? ";
576 array_push($sqlArray, $newencounter);
580 $where .= " OR f.pid = ? ";
581 array_push($sqlArray, $newkey);
587 if ($where) $where .= " AND ";
589 $where .= "f.date >= ? AND f.date <= ? ";
590 array_push($sqlArray, $form_date.' 00:00:00', $form_to_date.' 23:59:59');
593 $where .= "f.date >= ? AND f.date <= ? ";
594 array_push($sqlArray, $form_date.' 00:00:00', $form_date.' 23:59:59');
597 if ($form_facility) {
598 if ($where) $where .= " AND ";
599 $where .= "f.facility_id = ? ";
600 array_push($sqlArray, $form_facility);
602 # added for filtering by provider (TLH)
603 if ($form_provider) {
604 if ($where) $where .= " AND ";
605 $where .= "f.provider_id = ? ";
606 array_push($sqlArray, $form_provider);
612 # added provider from encounter to the query (TLH)
613 $query = "SELECT f.id, f.date, f.pid, CONCAT(w.lname, ', ', w.fname) AS provider_id, f.encounter, f.last_level_billed, " .
614 "f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, " .
615 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
616 "p.postal_code, p.phone_home, p.ss, p.billing_note, " .
617 "p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, " .
618 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
619 "b.pid = f.pid AND b.encounter = f.encounter AND " .
620 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
621 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
622 "b.pid = f.pid AND b.encounter = f.encounter AND " .
623 "b.activity = 1 AND b.code_type = 'COPAY' ) AS copays, " .
624 "( SELECT SUM(s.fee) FROM drug_sales AS s WHERE " .
625 "s.pid = f.pid AND s.encounter = f.encounter ) AS sales, " .
626 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
627 "a.pid = f.pid AND a.encounter = f.encounter ) AS payments, " .
628 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
629 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
630 "FROM form_encounter AS f " .
631 "JOIN patient_data AS p ON p.pid = f.pid " .
632 "LEFT OUTER JOIN users AS u ON u.id = p.ref_providerID " .
633 "LEFT OUTER JOIN users AS w ON w.id = f.provider_id " .
635 "ORDER BY f.pid, f.encounter";
637 $eres = sqlStatement($query, $sqlArray);
639 while ($erow = sqlFetchArray($eres)) {
640 $patient_id = $erow['pid'];
641 $encounter_id = $erow['encounter'];
642 $pt_balance = $erow['charges'] +
$erow['sales'] +
$erow['copays'] - $erow['payments'] - $erow['adjustments'];
643 $pt_balance = 0 +
sprintf("%.2f", $pt_balance); // yes this seems to be necessary
644 $svcdate = substr($erow['date'], 0, 10);
646 if ($_POST['form_refresh'] && ! $is_all) {
647 if ($pt_balance == 0) continue;
649 if ($_POST['form_category'] == 'Credits') {
650 if ($pt_balance > 0) continue;
653 // If we have not yet billed the patient, then compute $duncount as a
654 // negative count of the number of insurance plans for which we have not
655 // yet closed out insurance. Here we also compute $insname as the name of
656 // the insurance plan from which we are awaiting payment, and its sequence
657 // number $insposition (1-3).
658 $last_level_closed = $erow['last_level_closed'];
659 $duncount = $erow['stmt_count'];
664 for ($i = 1; $i <= 3; ++
$i) {
665 $tmp = arGetPayerID($patient_id, $svcdate, $i);
666 if (empty($tmp)) break;
669 $duncount = $last_level_closed - count($payerids);
671 if (!empty($payerids[$last_level_closed])) {
672 $insname = getInsName($payerids[$last_level_closed]);
673 $insposition = $last_level_closed +
1;
678 // Skip invoices not in the desired "Due..." category.
680 if ($is_due_ins && $duncount >= 0) continue;
681 if ($is_due_pt && $duncount < 0) continue;
683 // echo "<!-- " . $erow['encounter'] . ': ' . $erow['charges'] . ' + ' . $erow['sales'] . ' + ' . $erow['copays'] . ' - ' . $erow['payments'] . ' - ' . $erow['adjustments'] . " -->\n"; // debugging
685 // An invoice is due from the patient if money is owed and we are
686 // not waiting for insurance to pay.
687 $isduept = ($duncount >= 0) ?
" checked" : "";
691 $row['id'] = $erow['id'];
692 $row['invnumber'] = "$patient_id.$encounter_id";
693 $row['custid'] = $patient_id;
694 $row['name'] = $erow['fname'] . ' ' . $erow['lname'];
695 $row['address1'] = $erow['street'];
696 $row['city'] = $erow['city'];
697 $row['state'] = $erow['state'];
698 $row['zipcode'] = $erow['postal_code'];
699 $row['phone'] = $erow['phone_home'];
700 $row['duncount'] = $duncount;
701 $row['dos'] = $svcdate;
702 $row['ss'] = $erow['ss'];
703 $row['DOB'] = $erow['DOB'];
704 $row['pubpid'] = $erow['pubpid'];
705 $row['billnote'] = $erow['billing_note'];
706 $row['referrer'] = $erow['referrer'];
707 $row['provider'] = $erow['provider_id'];
708 $row['irnumber'] = $erow['invoice_refno'];
710 // Also get the primary insurance company name whenever there is one.
712 if ($insposition == 1) {
713 $row['ins1'] = $insname;
715 if (empty($payerids)) {
716 $tmp = arGetPayerID($patient_id, $svcdate, 1);
717 if (!empty($tmp)) $payerids[] = $tmp;
719 if (!empty($payerids)) {
720 $row['ins1'] = getInsName($payerids[0]);
724 // This computes the invoice's total original charges and adjustments,
725 // date of last activity, and determines if insurance has responded to
726 // all billing items.
727 $invlines = ar_get_invoice_summary($patient_id, $encounter_id, true);
729 // if ($encounter_id == 185) { // debugging
731 // print_r($invlines);
736 $row['adjustments'] = 0;
738 $ins_seems_done = true;
740 foreach ($invlines as $key => $value) {
741 $row['charges'] +
= $value['chg'] +
$value['adj'];
742 $row['adjustments'] +
= 0 - $value['adj'];
743 $row['paid'] +
= $value['chg'] - $value['bal'];
744 foreach ($value['dtl'] as $dkey => $dvalue) {
745 $dtldate = trim(substr($dkey, 0, 10));
746 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
748 $lckey = strtolower($key);
749 if ($lckey == 'co-pay' ||
$lckey == 'claim') continue;
750 if (count($value['dtl']) <= 1) $ins_seems_done = false;
753 // Simulating ar.amount in SQL-Ledger which is charges with adjustments:
754 $row['amount'] = $row['charges'] +
$row['adjustments'];
756 $row['billing_errmsg'] = '';
757 if ($is_due_ins && $last_level_closed < 1 && $ins_seems_done)
758 $row['billing_errmsg'] = 'Ins1 seems done';
759 else if ($last_level_closed >= 1 && !$ins_seems_done)
760 $row['billing_errmsg'] = 'Ins1 seems not done';
762 $row['ladate'] = $ladate;
764 // Compute number of days since last activity.
765 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
766 substr($ladate, 8, 2), substr($ladate, 0, 4));
767 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
769 // Look up insurance policy number if we need it.
770 if ($form_cb_policy) {
771 $instype = ($insposition == 2) ?
'secondary' : (($insposition == 3) ?
'tertiary' : 'primary');
772 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
773 "pid = ? AND type = ? AND date <= ? " .
774 "ORDER BY date DESC LIMIT 1", array($patient_id, $instype, $svcdate));
775 $row['policy'] = $insrow['policy_number'];
778 $ptname = $erow['lname'] . ", " . $erow['fname'];
779 if ($erow['mname']) $ptname .= " " . substr($erow['mname'], 0, 1);
781 if (!$is_due_ins ) $insname = '';
782 $rows[$insname . '|' . $ptname . '|' . $encounter_id] = $row;
788 if ($_POST['form_export']) {
789 echo "<textarea rows='35' cols='100' readonly>";
791 else if ($_POST['form_csvexport']) {
792 # CSV headers added conditions if they are checked to display then export them (TLH)
794 echo '"' . xl('Insurance') . '",';
795 echo '"' . xl('Name') . '",';
798 echo '"' . xl('SSN') . '",';
802 echo '"' . xl('DOB') . '",';
806 echo '"' . xl('Pubid') . '",';
810 echo '"' . xl('Policy') . '",';
814 echo '"' . xl('Phone') . '",';
818 echo '"' . xl('City') . '",';
820 echo '"' . xl('Invoice') . '",';
821 echo '"' . xl('DOS') . '",';
822 echo '"' . xl('Referrer') . '",';
823 echo '"' . xl('Provider') . '",';
824 echo '"' . xl('Charge') . '",';
825 echo '"' . xl('Adjust') . '",';
826 echo '"' . xl('Paid') . '",';
827 echo '"' . xl('Balance') . '",';
828 echo '"' . xl('IDays') . '",';
831 echo '"' . xl('LADate') . '",';
832 echo '"' . xl('Error') . '"' . "\n";
836 echo '"' . xl('LADate') . '"' . "\n";
843 <div id
="report_results">
844 <table id
='mymaintable'>
847 <?php
if ($is_due_ins) { ?
>
848 <th
> 
;<?php
echo xlt('Insurance')?
></th
>
850 <?php
if (!$is_ins_summary) { ?
>
851 <th
> 
;<?php
echo xlt('Name')?
></th
>
853 <?php
if ($form_cb_ssn) { ?
>
854 <th
> 
;<?php
echo xlt('SSN')?
></th
>
856 <?php
if ($form_cb_dob) { ?
>
857 <th
> 
;<?php
echo xlt('DOB')?
></th
>
859 <?php
if ($form_cb_pubpid) { ?
>
860 <th
> 
;<?php
echo xlt('ID')?
></th
>
862 <?php
if ($form_cb_policy) { ?
>
863 <th
> 
;<?php
echo xlt('Policy')?
></th
>
865 <?php
if ($form_cb_phone) { ?
>
866 <th
> 
;<?php
echo xlt('Phone')?
></th
>
868 <?php
if ($form_cb_city) { ?
>
869 <th
> 
;<?php
echo xlt('City')?
></th
>
871 <?php
if ($form_cb_ins1 ||
$form_payer_id) { ?
>
872 <th
> 
;<?php
echo xlt('Primary Ins')?
></th
>
874 <?php
if ($form_provider) { ?
>
875 <th
> 
;<?php
echo xlt('Provider')?
></th
>
877 <?php
if ($form_cb_referrer) { ?
>
878 <th
> 
;<?php
echo xlt('Referrer')?
></th
>
880 <?php
if (!$is_ins_summary) { ?
>
881 <th
> 
;<?php
echo xlt('Invoice') ?
></th
>
882 <th
> 
;<?php
echo xlt('Svc Date') ?
></th
>
883 <?php
if ($form_cb_adate) { ?
>
884 <th
> 
;<?php
echo xlt('Act Date')?
></th
>
887 <th align
="right"><?php
echo xlt('Charge') ?
> 
;</th
>
888 <th align
="right"><?php
echo xlt('Adjust') ?
> 
;</th
>
889 <th align
="right"><?php
echo xlt('Paid') ?
> 
;</th
>
891 // Generate aging headers if appropriate, else balance header.
892 if ($form_age_cols) {
893 for ($c = 0; $c < $form_age_cols;) {
894 echo " <th class='dehead' align='right'>";
895 echo $form_age_inc * $c;
896 if (++
$c < $form_age_cols) {
897 echo "-" . ($form_age_inc * $c - 1);
906 <th align
="right"><?php
echo xlt('Balance') ?
> 
;</th
>
910 <?php
if ($form_cb_idays) { ?
>
911 <th align
="right"><?php
echo xlt('IDays')?
> 
;</th
>
913 <?php
if (!$is_ins_summary) { ?
>
914 <th align
="center"><?php
echo xlt('Prv') ?
></th
>
915 <th align
="center"><?php
echo xlt('Sel') ?
></th
>
917 <?php
if ($form_cb_err) { ?
>
918 <th
> 
;<?php
echo xlt('Error')?
></th
>
925 $ptrow = array('insname' => '', 'pid' => 0);
928 foreach ($rows as $key => $row) {
929 list($insname, $ptname, $trash) = explode('|', $key);
930 list($pid, $encounter) = explode(".", $row['invnumber']);
931 if ($form_payer_id) {
932 if ($ins_co_name <> $row['ins1']) continue;
934 if ($is_ins_summary && $insname != $ptrow['insname']) {
935 endInsurance($ptrow);
936 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
937 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
938 foreach ($row as $key => $value) $ptrow[$key] = $value;
939 $ptrow['agedbal'] = array();
941 else if (!$is_ins_summary && ($insname != $ptrow['insname'] ||
$pid != $ptrow['pid'])) {
942 // For the report, this will write the patient totals. For the
943 // collections export this writes everything for the patient:
945 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
946 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
947 foreach ($row as $key => $value) $ptrow[$key] = $value;
948 $ptrow['agedbal'] = array();
950 $ptrow['amount'] +
= $row['amount'];
951 $ptrow['paid'] +
= $row['paid'];
952 $ptrow['charges'] +
= $row['charges'];
953 $ptrow['adjustments'] +
= $row['adjustments'];
958 // Compute invoice balance and aging column number, and accumulate aging.
959 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
960 if ($form_age_cols) {
961 $agedate = $is_ageby_lad ?
$row['ladate'] : $row['dos'];
962 $agetime = mktime(0, 0, 0, substr($agedate, 5, 2),
963 substr($agedate, 8, 2), substr($agedate, 0, 4));
964 $days = floor((time() - $agetime) / (60 * 60 * 24));
965 $agecolno = min($form_age_cols - 1, max(0, floor($days / $form_age_inc)));
966 $ptrow['agedbal'][$agecolno] +
= $balance;
969 if (!$is_ins_summary && !$_POST['form_export'] && !$_POST['form_csvexport']) {
970 $in_collections = stristr($row['billnote'], 'IN COLLECTIONS') !== false;
972 <tr bgcolor
='<?php echo attr($bgcolor) ?>'>
974 if ($ptrow['count'] == 1) {
976 echo " <td class='detail'> " . attr($insname) ."</td>\n";
978 echo " <td class='detail'> " . attr($ptname) ."</td>\n";
980 echo " <td class='detail'> " . attr($row['ss']) . "</td>\n";
983 echo " <td class='detail'> " . attr(oeFormatShortDate($row['DOB'])) . "</td>\n";
985 if ($form_cb_pubpid) {
986 echo " <td class='detail'> " . attr($row['pubpid']) . "</td>\n";
988 if ($form_cb_policy) {
989 echo " <td class='detail'> " . attr($row['policy']) . "</td>\n";
991 if ($form_cb_phone) {
992 echo " <td class='detail'> " . attr($row['phone']) . "</td>\n";
995 echo " <td class='detail'> " . attr($row['city']) . "</td>\n";
997 if ($form_cb_ins1 ||
$form_payer_id ) {
998 echo " <td class='detail'> " . attr($row['ins1']) . "</td>\n";
1000 if ($form_provider) {
1001 echo " <td class='detail'> " . attr($provider_name) . "</td>\n";
1003 if ($form_cb_referrer) {
1004 echo " <td class='detail'> " . attr($row['referrer']) . "</td>\n";
1007 echo " <td class='detail' colspan='$initial_colspan'>";
1008 echo " </td>\n";
1012  
;<a href
="../billing/sl_eob_invoice.php?id=<?php echo attr($row['id']) ?>"
1013 target
="_blank"><?php
echo empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']; ?
></a
>
1016  
;<?php
echo attr(oeFormatShortDate($row['dos'])); ?
>
1018 <?php
if ($form_cb_adate) { ?
>
1020  
;<?php
echo attr(oeFormatShortDate($row['ladate'])); ?
>
1023 <td
class="detail" align
="right">
1024 <?php
attr(bucks($row['charges'])) ?
> 
;
1026 <td
class="detail" align
="right">
1027 <?php
attr(bucks($row['adjustments'])) ?
> 
;
1029 <td
class="detail" align
="right">
1030 <?php
attr(bucks($row['paid'])) ?
> 
;
1033 if ($form_age_cols) {
1034 for ($c = 0; $c < $form_age_cols; ++
$c) {
1035 echo " <td class='detail' align='right'>";
1036 if ($c == $agecolno) {
1039 echo " </td>\n";
1044 <td
class="detail" align
="right"><?php
bucks($balance) ?
> 
;</td
>
1049 if ($form_cb_idays) {
1050 echo " <td class='detail' align='right'>";
1051 echo attr($row['inactive_days']) . " </td>\n";
1054 <td
class="detail" align
="center">
1055 <?php
echo $row['duncount'] ?
$row['duncount'] : " " ?
>
1057 <td
class="detail" align
="center">
1059 if ($in_collections) {
1060 echo " <b><font color='red'>IC</font></b>\n";
1062 echo " <input type='checkbox' name='form_cb[" . attr($row['invnumber']) . "]' />\n";
1068 echo " <td class='detail'> ";
1069 echo text($row['billing_errmsg']) . "</td>\n";
1074 } // end not export and not insurance summary
1076 else if ($_POST['form_csvexport']) {
1078 # The CSV detail line is written here added conditions for checked items (TLH).
1079 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
1081 // echo '"' . $insname . '",';
1082 echo '"' . $row['ins1'] . '",';
1083 echo '"' . $ptname . '",';
1086 echo '"' . $row['ss'] . '",';
1090 echo '"' . oeFormatShortDate($row['DOB']) . '",';
1094 echo '"' . $row['pubpid'] . '",';
1096 if ($form_cb_policy)
1098 echo '"' . $row['policy'] . '",';
1102 echo '"' . $row['phone'] . '",';
1106 echo '"' . $row['city'] . '",';
1108 echo '"' . (empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']) . '",';
1109 echo '"' . oeFormatShortDate($row['dos']) . '",';
1110 echo '"' . $row['referrer'] . '",';
1111 echo '"' . $row['provider'] . '",';
1112 echo '"' . oeFormatMoney($row['charges']) . '",';
1113 echo '"' . oeFormatMoney($row['adjustments']) . '",';
1114 echo '"' . oeFormatMoney($row['paid']) . '",';
1115 echo '"' . oeFormatMoney($balance) . '",';
1116 echo '"' . $row['inactive_days'] . '",';
1119 echo '"' . oeFormatShortDate($row['ladate']) . '",';
1120 echo '"' . $row['billing_errmsg'] . '"' . "\n";
1124 echo '"' . oeFormatShortDate($row['ladate']) . '"' . "\n";
1127 } // end $form_csvexport
1131 if ($is_ins_summary)
1132 endInsurance($ptrow);
1136 if ($_POST['form_export']) {
1137 echo "</textarea>\n";
1138 $alertmsg .= "$export_patient_count patients with total of " .
1139 oeFormatMoney($export_dollars) . " have been exported ";
1140 if ($_POST['form_without']) {
1141 $alertmsg .= "but NOT flagged as in collections.";
1143 $alertmsg .= "AND flagged as in collections.";
1146 else if ($_POST['form_csvexport']) {
1147 // echo "</textarea>\n";
1148 // $alertmsg .= "$export_patient_count patients representing $" .
1149 // sprintf("%.2f", $export_dollars) . " have been exported.";
1152 echo " <tr bgcolor='#ffffff'>\n";
1153 if ($is_ins_summary) {
1154 echo " <td class='dehead'> " . xlt('Report Totals') . ":</td>\n";
1156 echo " <td class='detail' colspan='" . attr($initial_colspan) . "'>\n";
1157 echo " </td>\n";
1158 echo " <td class='dehead' colspan='" . attr($final_colspan - 3) .
1159 "'> " . xlt('Report Totals') . ":</td>\n";
1161 echo " <td class='dehead' align='right'> " .
1162 oeFormatMoney($grand_total_charges) . " </td>\n";
1163 echo " <td class='dehead' align='right'> " .
1164 oeFormatMoney($grand_total_adjustments) . " </td>\n";
1165 echo " <td class='dehead' align='right'> " .
1166 oeFormatMoney($grand_total_paid) . " </td>\n";
1167 if ($form_age_cols) {
1168 for ($c = 0; $c < $form_age_cols; ++
$c) {
1169 echo " <td class='dehead' align='right'>" .
1170 oeFormatMoney($grand_total_agedbal[$c]) . " </td>\n";
1174 echo " <td class='dehead' align='right'>" .
1175 oeFormatMoney($grand_total_charges +
1176 $grand_total_adjustments - $grand_total_paid) . " </td>\n";
1178 if ($form_cb_idays) echo " <td class='detail'> </td>\n";
1179 if (!$is_ins_summary) echo " <td class='detail' colspan='2'> </td>\n";
1180 if ($form_cb_err) echo " <td class='detail'> </td>\n";
1185 } // end if form_refresh
1188 if (!$_POST['form_csvexport']) {
1189 if (!$_POST['form_export']) {
1192 <div style
='float;margin-top:5px'>
1194 <a href
='javascript:;' class='css_button' onclick
='checkAll(true)'><span
><?php
echo xlt('Select All'); ?
></span
></a
>
1195 <a href
='javascript:;' class='css_button' onclick
='checkAll(false)'><span
><?php
echo xlt('Clear All'); ?
></span
></a
>
1196 <a href
='javascript:;' class='css_button' onclick
='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
1197 <span
><?php
echo xlt('Export Selected as CSV'); ?
></span
>
1199 <a href
='javascript:;' class='css_button' onclick
='$("#form_export").attr("value","true"); $("#theform").submit();'>
1200 <span
><?php
echo xlt('Export Selected to Collections'); ?
></span
>
1204 <div style
='float:left'>
1205 <label
><input type
='checkbox' name
='form_individual' value
='1' /> <?php
echo xlt('Export Individual Invoices') ?
> 
; 
;</label
>
1208 <div style
='float:left'>
1209 <label
><input type
='checkbox' name
='form_without' value
='1' /> <?php
echo xlt('Without Update') ?
></label
>
1217 <script language
="JavaScript">
1220 echo "alert('" . addslashes($alertmsg) . "');\n";
1225 <!-- stuff
for the popup calendar
-->
1226 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
1227 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
1228 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
1229 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
1230 <script language
="Javascript">
1231 Calendar
.setup({inputField
:"form_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
1232 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
1236 } // end not form_csvexport