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";
37 require_once "$srcdir/formdata.inc.php";
42 $export_patient_count = 0;
45 $today = date("Y-m-d");
47 $form_date = fixDate($_POST['form_date'], "");
48 $form_to_date = fixDate($_POST['form_to_date'], "");
49 $is_ins_summary = $_POST['form_category'] == 'Ins Summary';
50 $is_due_ins = ($_POST['form_category'] == 'Due Ins') ||
$is_ins_summary;
51 $is_due_pt = $_POST['form_category'] == 'Due Pt';
52 $is_all = $_POST['form_category'] == 'All';
53 $is_ageby_lad = strpos($_POST['form_ageby'], 'Last') !== false;
54 $form_facility = $_POST['form_facility'];
55 $form_provider = $_POST['form_provider'];
56 $form_payer_id = $_POST['form_payer_id'];
58 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
59 if ($is_ins_summary) {
62 $form_cb_pubpid = false;
63 $form_cb_adate = false;
64 $form_cb_policy = false;
65 $form_cb_phone = false;
66 $form_cb_city = false;
67 $form_cb_ins1 = false;
68 $form_cb_referrer = false;
69 $form_cb_idays = false;
72 $form_cb_ssn = $_POST['form_cb_ssn'] ?
true : false;
73 $form_cb_dob = $_POST['form_cb_dob'] ?
true : false;
74 $form_cb_pubpid = $_POST['form_cb_pubpid'] ?
true : false;
75 $form_cb_adate = $_POST['form_cb_adate'] ?
true : false;
76 $form_cb_policy = $_POST['form_cb_policy'] ?
true : false;
77 $form_cb_phone = $_POST['form_cb_phone'] ?
true : false;
78 $form_cb_city = $_POST['form_cb_city'] ?
true : false;
79 $form_cb_ins1 = $_POST['form_cb_ins1'] ?
true : false;
80 $form_cb_referrer = $_POST['form_cb_referrer'] ?
true : false;
81 $form_cb_idays = $_POST['form_cb_idays'] ?
true : false;
82 $form_cb_err = $_POST['form_cb_err'] ?
true : false;
87 $form_cb_pubpid = false;
88 $form_cb_adate = false;
89 $form_cb_policy = false;
90 $form_cb_phone = true;
91 $form_cb_city = false;
92 $form_cb_ins1 = false;
93 $form_cb_referrer = false;
94 $form_cb_idays = false;
97 $form_age_cols = (int) $_POST['form_age_cols'];
98 $form_age_inc = (int) $_POST['form_age_inc'];
99 if ($form_age_cols > 0 && $form_age_cols < 50) {
100 if ($form_age_inc <= 0) $form_age_inc = 30;
106 $initial_colspan = 1;
107 if ($is_due_ins ) ++
$initial_colspan;
108 if ($form_cb_ssn ) ++
$initial_colspan;
109 if ($form_cb_dob ) ++
$initial_colspan;
110 if ($form_cb_pubpid ) ++
$initial_colspan;
111 if ($form_cb_policy ) ++
$initial_colspan;
112 if ($form_cb_phone ) ++
$initial_colspan;
113 if ($form_cb_city ) ++
$initial_colspan;
114 if ($form_cb_ins1 ) ++
$initial_colspan;
115 if ($form_cb_referrer) ++
$initial_colspan;
116 if ($form_provider ) ++
$initial_colspan;
117 if ($form_payer_id ) ++
$initial_colspan;
119 $final_colspan = $form_cb_adate ?
6 : 5;
121 $grand_total_charges = 0;
122 $grand_total_adjustments = 0;
123 $grand_total_paid = 0;
124 $grand_total_agedbal = array();
125 for ($c = 0; $c < $form_age_cols; ++
$c) $grand_total_agedbal[$c] = 0;
128 function bucks($amount) {
130 echo oeFormatMoney($amount); // was printf("%.2f", $amount);
133 function endPatient($ptrow) {
134 global $export_patient_count, $export_dollars, $bgcolor;
135 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
136 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
137 global $initial_colspan, $final_colspan, $form_cb_idays, $form_cb_err;
139 if (!$ptrow['pid']) return;
141 $pt_balance = $ptrow['amount'] - $ptrow['paid'];
143 if ($_POST['form_export']) {
144 // This is a fixed-length format used by Transworld Systems. Your
145 // needs will surely be different, so consider this just an example.
147 echo "1896H"; // client number goes here
148 echo "000"; // filler
149 echo sprintf("%-30s", substr($ptrow['ptname'], 0, 30));
150 echo sprintf("%-30s", " ");
151 echo sprintf("%-30s", substr($ptrow['address1'], 0, 30));
152 echo sprintf("%-15s", substr($ptrow['city'], 0, 15));
153 echo sprintf("%-2s", substr($ptrow['state'], 0, 2));
154 echo sprintf("%-5s", $ptrow['zipcode'] ?
substr($ptrow['zipcode'], 0, 5) : '00000');
155 echo "1"; // service code
156 echo sprintf("%010.0f", $ptrow['pid']); // transmittal number = patient id
158 echo sprintf("%-15s", substr($ptrow['ss'], 0, 15));
159 echo substr($ptrow['dos'], 5, 2) . substr($ptrow['dos'], 8, 2) . substr($ptrow['dos'], 2, 2);
160 echo sprintf("%08.0f", $pt_balance * 100);
161 echo sprintf("%-9s\n", " ");
163 if (!$_POST['form_without']) {
164 sqlStatement("UPDATE patient_data SET " .
165 "billing_note = CONCAT('IN COLLECTIONS " . date("Y-m-d") . "', billing_note) " .
166 "WHERE pid = ? ", array($ptrow['pid']));
168 $export_patient_count +
= 1;
169 $export_dollars +
= $pt_balance;
171 else if ($_POST['form_csvexport']) {
172 $export_patient_count +
= 1;
173 $export_dollars +
= $pt_balance;
176 if ($ptrow['count'] > 1) {
177 echo " <tr bgcolor='$bgcolor'>\n";
178 /***************************************************************
179 echo " <td class='detail' colspan='$initial_colspan'>";
180 echo " </td>\n";
181 echo " <td class='detotal' colspan='$final_colspan'> Total Patient Balance:</td>\n";
182 ***************************************************************/
183 echo " <td class='detotal' colspan='" . ($initial_colspan +
$final_colspan) .
184 "'> " . xlt('Total Patient Balance') . ":</td>\n";
185 /**************************************************************/
186 if ($form_age_cols) {
187 for ($c = 0; $c < $form_age_cols; ++
$c) {
188 echo " <td class='detotal' align='right'> " .
189 oeFormatMoney($ptrow['agedbal'][$c]) . " </td>\n";
193 echo " <td class='detotal' align='right'> " .
194 oeFormatMoney($pt_balance) . " </td>\n";
196 if ($form_cb_idays) echo " <td class='detail'> </td>\n";
197 echo " <td class='detail' colspan='2'> </td>\n";
198 if ($form_cb_err) echo " <td class='detail'> </td>\n";
202 $grand_total_charges +
= $ptrow['charges'];
203 $grand_total_adjustments +
= $ptrow['adjustments'];
204 $grand_total_paid +
= $ptrow['paid'];
205 for ($c = 0; $c < $form_age_cols; ++
$c) {
206 $grand_total_agedbal[$c] +
= $ptrow['agedbal'][$c];
210 function endInsurance($insrow) {
211 global $export_patient_count, $export_dollars, $bgcolor;
212 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
213 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
214 global $initial_colspan, $form_cb_idays, $form_cb_err;
215 if (!$insrow['pid']) return;
216 $ins_balance = $insrow['amount'] - $insrow['paid'];
217 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
218 // No exporting of insurance summaries.
219 $export_patient_count +
= 1;
220 $export_dollars +
= $ins_balance;
223 echo " <tr bgcolor='$bgcolor'>\n";
224 echo " <td class='detail'>" . text($insrow['insname']) . "</td>\n";
225 echo " <td class='detotal' align='right'> " .
226 oeFormatMoney($insrow['charges']) . " </td>\n";
227 echo " <td class='detotal' align='right'> " .
228 oeFormatMoney($insrow['adjustments']) . " </td>\n";
229 echo " <td class='detotal' align='right'> " .
230 oeFormatMoney($insrow['paid']) . " </td>\n";
231 if ($form_age_cols) {
232 for ($c = 0; $c < $form_age_cols; ++
$c) {
233 echo " <td class='detotal' align='right'> " .
234 oeFormatMoney($insrow['agedbal'][$c]) . " </td>\n";
238 echo " <td class='detotal' align='right'> " .
239 oeFormatMoney($ins_balance) . " </td>\n";
243 $grand_total_charges +
= $insrow['charges'];
244 $grand_total_adjustments +
= $insrow['adjustments'];
245 $grand_total_paid +
= $insrow['paid'];
246 for ($c = 0; $c < $form_age_cols; ++
$c) {
247 $grand_total_agedbal[$c] +
= $insrow['agedbal'][$c];
251 function getInsName($payerid) {
252 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = ? ", array($payerid));
256 // In the case of CSV export only, a download will be forced.
257 if ($_POST['form_csvexport']) {
258 header("Pragma: public");
259 header("Expires: 0");
260 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
261 header("Content-Type: application/force-download");
262 header("Content-Disposition: attachment; filename=collections_report.csv");
263 header("Content-Description: File Transfer");
269 <?php
if (function_exists('html_header_show')) html_header_show(); ?
>
270 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
271 <title
><?php
echo xlt('Collections Report')?
></title
>
272 <style type
="text/css">
279 #report_parameters_daterange {
288 /* specifically exclude some from the screen */
290 #report_parameters_daterange {
298 <script type
="text/javascript" src
="../../library/js/jquery-1.9.1.min.js"></script
>
299 <script type
="text/javascript" src
="../../library/js/report_helper.js?v=<?php echo $v_js_includes; ?>"></script
>
301 <script language
="JavaScript">
303 $
(document
).ready(function() {
304 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
305 var win
= top
.printLogSetup ? top
: opener
.top
;
306 win
.printLogSetup(document
.getElementById('printbutton'));
309 function checkAll(checked
) {
310 var f
= document
.forms
[0];
311 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
312 var ename
= f
.elements
[i
].name
;
313 if (ename
.indexOf('form_cb[') == 0)
314 f
.elements
[i
].checked
= checked
;
322 <body
class="body_top">
324 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Collections'); ?
></span
>
326 <form method
='post' action
='collections_report.php' enctype
='multipart/form-data' id
='theform'>
328 <div id
="report_parameters">
330 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
331 <input type
='hidden' name
='form_export' id
='form_export' value
=''/>
332 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
337 <div style
='float:left'>
344 <td
><?php
echo xlt('Displayed Columns') ?
>:</td
>
348 <label
><input type
='checkbox' name
='form_cb_ssn'<?php
if ($form_cb_ssn) echo ' checked'; ?
>>
349 <?php
echo xlt('SSN') ?
> 
;</label
>
352 <label
><input type
='checkbox' name
='form_cb_dob'<?php
if ($form_cb_dob) echo ' checked'; ?
>>
353 <?php
echo xlt('DOB') ?
> 
;</label
>
356 <label
><input type
='checkbox' name
='form_cb_pubpid'<?php
if ($form_cb_pubpid) echo ' checked'; ?
>>
357 <?php
echo xlt('ID') ?
> 
;</label
>
360 <label
><input type
='checkbox' name
='form_cb_policy'<?php
if ($form_cb_policy) echo ' checked'; ?
>>
361 <?php
echo xlt('Policy') ?
> 
;</label
>
364 <label
><input type
='checkbox' name
='form_cb_phone'<?php
if ($form_cb_phone) echo ' checked'; ?
>>
365 <?php
echo xlt('Phone') ?
> 
;</label
>
368 <label
><input type
='checkbox' name
='form_cb_city'<?php
if ($form_cb_city) echo ' checked'; ?
>>
369 <?php
echo xlt('City') ?
> 
;</label
>
374 <label
><input type
='checkbox' name
='form_cb_ins1'<?php
if ($form_cb_ins1) echo ' checked'; ?
>>
375 <?php
echo xlt('Primary Ins') ?
> 
;</label
>
378 <label
><input type
='checkbox' name
='form_cb_referrer'<?php
if ($form_cb_referrer) echo ' checked'; ?
>>
379 <?php
echo xlt('Referrer') ?
> 
;</label
>
382 <label
><input type
='checkbox' name
='form_cb_adate'<?php
if ($form_cb_adate) echo ' checked'; ?
>>
383 <?php
echo xlt('Act Date') ?
> 
;</label
>
386 <label
><input type
='checkbox' name
='form_cb_idays'<?php
if ($form_cb_idays) echo ' checked'; ?
>>
387 <?php
echo xlt('Inactive Days') ?
> 
;</label
>
390 <label
><input type
='checkbox' name
='form_cb_err'<?php
if ($form_cb_err) echo ' checked'; ?
>>
391 <?php
echo xlt('Errors') ?
></label
>
403 <?php
echo xlt('Service Date'); ?
>:
406 <input type
='text' name
='form_date' id
="form_date" size
='10' value
='<?php echo attr($form_date) ?>'
407 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
408 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
409 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
410 title
='<?php echo xla('Click here to choose a date
'); ?>'>
413 <?php
echo xlt('To'); ?
>:
416 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
417 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
418 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
419 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
420 title
='<?php echo xla('Click here to choose a date
'); ?>'>
423 <select name
='form_category'>
425 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) {
426 echo " <option value='" . attr($key) . "'";
427 if ($_POST['form_category'] == $key) echo " selected";
428 echo ">" . text($value) . "</option>\n";
439 <?php
echo xlt('Facility'); ?
>:
442 <?php
dropdown_facility($form_facility, 'form_facility', false); ?
>
446 <?php
echo xlt('Payor'); ?
>:
449 <?php
# added dropdown for payors (TLH)
450 $insurancei = getInsuranceProviders();
451 echo " <select name='form_payer_id'>\n";
452 echo " <option value='0'>-- " . xlt('All') . " --</option>\n";
453 foreach ($insurancei as $iid => $iname) {
454 echo "<option value='" . attr($iid) . "'";
455 if ($iid == $_POST['form_payer_id']) echo " selected";
456 echo ">" . text($iname) . "</option>\n";
457 if ($iid == $_POST['form_payer_id']) $ins_co_name = $iname;
466 <?php
echo xlt('Age By') ?
>:
469 <select name
='form_ageby'>
471 foreach (array( 'Service Date'=>xl('Service Date'), 'Last Activity Date'=>xl('Last Activity Date')) as $key => $value) {
472 echo " <option value='" . attr($key) . "'";
473 if ($_POST['form_ageby'] == $value) echo " selected";
474 echo ">" . text($value) . "</option>\n";
481 <?php
echo xlt('Provider') ?
>:
484 <?php
# Build a drop-down list of providers.
487 $query = "SELECT id, lname, fname FROM users WHERE ".
488 "authorized = 1 ORDER BY lname, fname"; #(CHEMED) facility filter
490 $ures = sqlStatement($query);
492 echo " <select name='form_provider'>\n";
493 echo " <option value=''>-- " . xlt('All') . " --\n";
495 while ($urow = sqlFetchArray($ures)) {
496 $provid = $urow['id'];
497 echo " <option value='" . attr($provid) . "'";
498 if ($provid == $_POST['form_provider']) echo " selected";
499 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
500 if ($provid == $_POST['form_provider']) $provider_name = $urow['lname'] . ", " . $urow['fname'];
509 <?php
echo xlt('Aging Columns') ?
>:
512 <input type
='text' name
='form_age_cols' size
='2' value
='<?php echo attr($form_age_cols); ?>' />
515 <?php
echo xlt('Days/Col') ?
>:
518 <input type
='text' name
='form_age_inc' size
='3' value
='<?php echo attr($form_age_inc); ?>' />
531 <td align
='left' valign
='middle' height
="100%">
532 <table style
='border-left:1px solid; width:100%; height:100%' >
535 <div style
='margin-left:15px'>
536 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
538 <?php
echo xlt('Submit'); ?
>
542 <?php
if ($_POST['form_refresh']) { ?
>
543 <a href
='#' class='css_button' onclick
='window.print()'>
545 <?php
echo xlt('Print'); ?
>
561 } // end not form_csvexport
563 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
567 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
570 foreach ($_POST['form_cb'] as $key => $value) {
571 list($key_newval['pid'], $key_newval['encounter']) = explode(".", $key);
572 $newkey = $key_newval['pid'];
573 $newencounter = $key_newval['encounter'];
574 # added this condition to handle the downloading of individual invoices (TLH)
575 if($_POST['form_individual'] ==1){
576 $where .= " OR f.encounter = ? ";
577 array_push($sqlArray, $newencounter);
581 $where .= " OR f.pid = ? ";
582 array_push($sqlArray, $newkey);
588 if ($where) $where .= " AND ";
590 $where .= "f.date >= ? AND f.date <= ? ";
591 array_push($sqlArray, $form_date.' 00:00:00', $form_to_date.' 23:59:59');
594 $where .= "f.date >= ? AND f.date <= ? ";
595 array_push($sqlArray, $form_date.' 00:00:00', $form_date.' 23:59:59');
598 if ($form_facility) {
599 if ($where) $where .= " AND ";
600 $where .= "f.facility_id = ? ";
601 array_push($sqlArray, $form_facility);
603 # added for filtering by provider (TLH)
604 if ($form_provider) {
605 if ($where) $where .= " AND ";
606 $where .= "f.provider_id = ? ";
607 array_push($sqlArray, $form_provider);
613 # added provider from encounter to the query (TLH)
614 $query = "SELECT f.id, f.date, f.pid, CONCAT(w.lname, ', ', w.fname) AS provider_id, f.encounter, f.last_level_billed, " .
615 "f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, " .
616 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
617 "p.postal_code, p.phone_home, p.ss, p.billing_note, " .
618 "p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, " .
619 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
620 "b.pid = f.pid AND b.encounter = f.encounter AND " .
621 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
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 copays, " .
625 "( SELECT SUM(s.fee) FROM drug_sales AS s WHERE " .
626 "s.pid = f.pid AND s.encounter = f.encounter ) AS sales, " .
627 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
628 "a.pid = f.pid AND a.encounter = f.encounter ) AS payments, " .
629 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
630 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
631 "FROM form_encounter AS f " .
632 "JOIN patient_data AS p ON p.pid = f.pid " .
633 "LEFT OUTER JOIN users AS u ON u.id = p.ref_providerID " .
634 "LEFT OUTER JOIN users AS w ON w.id = f.provider_id " .
636 "ORDER BY f.pid, f.encounter";
638 $eres = sqlStatement($query, $sqlArray);
640 while ($erow = sqlFetchArray($eres)) {
641 $patient_id = $erow['pid'];
642 $encounter_id = $erow['encounter'];
643 $pt_balance = $erow['charges'] +
$erow['sales'] +
$erow['copays'] - $erow['payments'] - $erow['adjustments'];
644 $pt_balance = 0 +
sprintf("%.2f", $pt_balance); // yes this seems to be necessary
645 $svcdate = substr($erow['date'], 0, 10);
647 if ($_POST['form_refresh'] && ! $is_all) {
648 if ($pt_balance == 0) continue;
650 if ($_POST['form_category'] == 'Credits') {
651 if ($pt_balance > 0) continue;
654 // If we have not yet billed the patient, then compute $duncount as a
655 // negative count of the number of insurance plans for which we have not
656 // yet closed out insurance. Here we also compute $insname as the name of
657 // the insurance plan from which we are awaiting payment, and its sequence
658 // number $insposition (1-3).
659 $last_level_closed = $erow['last_level_closed'];
660 $duncount = $erow['stmt_count'];
665 for ($i = 1; $i <= 3; ++
$i) {
666 $tmp = arGetPayerID($patient_id, $svcdate, $i);
667 if (empty($tmp)) break;
670 $duncount = $last_level_closed - count($payerids);
672 if (!empty($payerids[$last_level_closed])) {
673 $insname = getInsName($payerids[$last_level_closed]);
674 $insposition = $last_level_closed +
1;
679 // Skip invoices not in the desired "Due..." category.
681 if ($is_due_ins && $duncount >= 0) continue;
682 if ($is_due_pt && $duncount < 0) continue;
684 // echo "<!-- " . $erow['encounter'] . ': ' . $erow['charges'] . ' + ' . $erow['sales'] . ' + ' . $erow['copays'] . ' - ' . $erow['payments'] . ' - ' . $erow['adjustments'] . " -->\n"; // debugging
686 // An invoice is due from the patient if money is owed and we are
687 // not waiting for insurance to pay.
688 $isduept = ($duncount >= 0) ?
" checked" : "";
692 $row['id'] = $erow['id'];
693 $row['invnumber'] = "$patient_id.$encounter_id";
694 $row['custid'] = $patient_id;
695 $row['name'] = $erow['fname'] . ' ' . $erow['lname'];
696 $row['address1'] = $erow['street'];
697 $row['city'] = $erow['city'];
698 $row['state'] = $erow['state'];
699 $row['zipcode'] = $erow['postal_code'];
700 $row['phone'] = $erow['phone_home'];
701 $row['duncount'] = $duncount;
702 $row['dos'] = $svcdate;
703 $row['ss'] = $erow['ss'];
704 $row['DOB'] = $erow['DOB'];
705 $row['pubpid'] = $erow['pubpid'];
706 $row['billnote'] = $erow['billing_note'];
707 $row['referrer'] = $erow['referrer'];
708 $row['provider'] = $erow['provider_id'];
709 $row['irnumber'] = $erow['invoice_refno'];
711 // Also get the primary insurance company name whenever there is one.
713 if ($insposition == 1) {
714 $row['ins1'] = $insname;
716 if (empty($payerids)) {
717 $tmp = arGetPayerID($patient_id, $svcdate, 1);
718 if (!empty($tmp)) $payerids[] = $tmp;
720 if (!empty($payerids)) {
721 $row['ins1'] = getInsName($payerids[0]);
725 // This computes the invoice's total original charges and adjustments,
726 // date of last activity, and determines if insurance has responded to
727 // all billing items.
728 $invlines = ar_get_invoice_summary($patient_id, $encounter_id, true);
730 // if ($encounter_id == 185) { // debugging
732 // print_r($invlines);
737 $row['adjustments'] = 0;
739 $ins_seems_done = true;
741 foreach ($invlines as $key => $value) {
742 $row['charges'] +
= $value['chg'] +
$value['adj'];
743 $row['adjustments'] +
= 0 - $value['adj'];
744 $row['paid'] +
= $value['chg'] - $value['bal'];
745 foreach ($value['dtl'] as $dkey => $dvalue) {
746 $dtldate = trim(substr($dkey, 0, 10));
747 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
749 $lckey = strtolower($key);
750 if ($lckey == 'co-pay' ||
$lckey == 'claim') continue;
751 if (count($value['dtl']) <= 1) $ins_seems_done = false;
754 // Simulating ar.amount in SQL-Ledger which is charges with adjustments:
755 $row['amount'] = $row['charges'] +
$row['adjustments'];
757 $row['billing_errmsg'] = '';
758 if ($is_due_ins && $last_level_closed < 1 && $ins_seems_done)
759 $row['billing_errmsg'] = 'Ins1 seems done';
760 else if ($last_level_closed >= 1 && !$ins_seems_done)
761 $row['billing_errmsg'] = 'Ins1 seems not done';
763 $row['ladate'] = $ladate;
765 // Compute number of days since last activity.
766 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
767 substr($ladate, 8, 2), substr($ladate, 0, 4));
768 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
770 // Look up insurance policy number if we need it.
771 if ($form_cb_policy) {
772 $instype = ($insposition == 2) ?
'secondary' : (($insposition == 3) ?
'tertiary' : 'primary');
773 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
774 "pid = ? AND type = ? AND date <= ? " .
775 "ORDER BY date DESC LIMIT 1", array($patient_id, $instype, $svcdate));
776 $row['policy'] = $insrow['policy_number'];
779 $ptname = $erow['lname'] . ", " . $erow['fname'];
780 if ($erow['mname']) $ptname .= " " . substr($erow['mname'], 0, 1);
782 if (!$is_due_ins ) $insname = '';
783 $rows[$insname . '|' . $ptname . '|' . $encounter_id] = $row;
789 if ($_POST['form_export']) {
790 echo "<textarea rows='35' cols='100' readonly>";
792 else if ($_POST['form_csvexport']) {
793 # CSV headers added conditions if they are checked to display then export them (TLH)
795 echo '"' . xl('Insurance') . '",';
796 echo '"' . xl('Name') . '",';
799 echo '"' . xl('SSN') . '",';
803 echo '"' . xl('DOB') . '",';
807 echo '"' . xl('Pubid') . '",';
811 echo '"' . xl('Policy') . '",';
815 echo '"' . xl('Phone') . '",';
819 echo '"' . xl('City') . '",';
821 echo '"' . xl('Invoice') . '",';
822 echo '"' . xl('DOS') . '",';
823 echo '"' . xl('Referrer') . '",';
824 echo '"' . xl('Provider') . '",';
825 echo '"' . xl('Charge') . '",';
826 echo '"' . xl('Adjust') . '",';
827 echo '"' . xl('Paid') . '",';
828 echo '"' . xl('Balance') . '",';
829 echo '"' . xl('IDays') . '",';
832 echo '"' . xl('LADate') . '",';
833 echo '"' . xl('Error') . '"' . "\n";
837 echo '"' . xl('LADate') . '"' . "\n";
844 <div id
="report_results">
845 <table id
='mymaintable'>
848 <?php
if ($is_due_ins) { ?
>
849 <th
> 
;<?php
echo xlt('Insurance')?
></th
>
851 <?php
if (!$is_ins_summary) { ?
>
852 <th
> 
;<?php
echo xlt('Name')?
></th
>
854 <?php
if ($form_cb_ssn) { ?
>
855 <th
> 
;<?php
echo xlt('SSN')?
></th
>
857 <?php
if ($form_cb_dob) { ?
>
858 <th
> 
;<?php
echo xlt('DOB')?
></th
>
860 <?php
if ($form_cb_pubpid) { ?
>
861 <th
> 
;<?php
echo xlt('ID')?
></th
>
863 <?php
if ($form_cb_policy) { ?
>
864 <th
> 
;<?php
echo xlt('Policy')?
></th
>
866 <?php
if ($form_cb_phone) { ?
>
867 <th
> 
;<?php
echo xlt('Phone')?
></th
>
869 <?php
if ($form_cb_city) { ?
>
870 <th
> 
;<?php
echo xlt('City')?
></th
>
872 <?php
if ($form_cb_ins1 ||
$form_payer_id) { ?
>
873 <th
> 
;<?php
echo xlt('Primary Ins')?
></th
>
875 <?php
if ($form_provider) { ?
>
876 <th
> 
;<?php
echo xlt('Provider')?
></th
>
878 <?php
if ($form_cb_referrer) { ?
>
879 <th
> 
;<?php
echo xlt('Referrer')?
></th
>
881 <?php
if (!$is_ins_summary) { ?
>
882 <th
> 
;<?php
echo xlt('Invoice') ?
></th
>
883 <th
> 
;<?php
echo xlt('Svc Date') ?
></th
>
884 <?php
if ($form_cb_adate) { ?
>
885 <th
> 
;<?php
echo xlt('Act Date')?
></th
>
888 <th align
="right"><?php
echo xlt('Charge') ?
> 
;</th
>
889 <th align
="right"><?php
echo xlt('Adjust') ?
> 
;</th
>
890 <th align
="right"><?php
echo xlt('Paid') ?
> 
;</th
>
892 // Generate aging headers if appropriate, else balance header.
893 if ($form_age_cols) {
894 for ($c = 0; $c < $form_age_cols;) {
895 echo " <th class='dehead' align='right'>";
896 echo $form_age_inc * $c;
897 if (++
$c < $form_age_cols) {
898 echo "-" . ($form_age_inc * $c - 1);
907 <th align
="right"><?php
echo xlt('Balance') ?
> 
;</th
>
911 <?php
if ($form_cb_idays) { ?
>
912 <th align
="right"><?php
echo xlt('IDays')?
> 
;</th
>
914 <?php
if (!$is_ins_summary) { ?
>
915 <th align
="center"><?php
echo xlt('Prv') ?
></th
>
916 <th align
="center"><?php
echo xlt('Sel') ?
></th
>
918 <?php
if ($form_cb_err) { ?
>
919 <th
> 
;<?php
echo xlt('Error')?
></th
>
926 $ptrow = array('insname' => '', 'pid' => 0);
929 foreach ($rows as $key => $row) {
930 list($insname, $ptname, $trash) = explode('|', $key);
931 list($pid, $encounter) = explode(".", $row['invnumber']);
932 if ($form_payer_id) {
933 if ($ins_co_name <> $row['ins1']) continue;
935 if ($is_ins_summary && $insname != $ptrow['insname']) {
936 endInsurance($ptrow);
937 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
938 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
939 foreach ($row as $key => $value) $ptrow[$key] = $value;
940 $ptrow['agedbal'] = array();
942 else if (!$is_ins_summary && ($insname != $ptrow['insname'] ||
$pid != $ptrow['pid'])) {
943 // For the report, this will write the patient totals. For the
944 // collections export this writes everything for the patient:
946 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
947 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
948 foreach ($row as $key => $value) $ptrow[$key] = $value;
949 $ptrow['agedbal'] = array();
951 $ptrow['amount'] +
= $row['amount'];
952 $ptrow['paid'] +
= $row['paid'];
953 $ptrow['charges'] +
= $row['charges'];
954 $ptrow['adjustments'] +
= $row['adjustments'];
959 // Compute invoice balance and aging column number, and accumulate aging.
960 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
961 if ($form_age_cols) {
962 $agedate = $is_ageby_lad ?
$row['ladate'] : $row['dos'];
963 $agetime = mktime(0, 0, 0, substr($agedate, 5, 2),
964 substr($agedate, 8, 2), substr($agedate, 0, 4));
965 $days = floor((time() - $agetime) / (60 * 60 * 24));
966 $agecolno = min($form_age_cols - 1, max(0, floor($days / $form_age_inc)));
967 $ptrow['agedbal'][$agecolno] +
= $balance;
970 if (!$is_ins_summary && !$_POST['form_export'] && !$_POST['form_csvexport']) {
971 $in_collections = stristr($row['billnote'], 'IN COLLECTIONS') !== false;
973 <tr bgcolor
='<?php echo attr($bgcolor) ?>'>
975 if ($ptrow['count'] == 1) {
977 echo " <td class='detail'> " . attr($insname) ."</td>\n";
979 echo " <td class='detail'> " . attr($ptname) ."</td>\n";
981 echo " <td class='detail'> " . attr($row['ss']) . "</td>\n";
984 echo " <td class='detail'> " . attr(oeFormatShortDate($row['DOB'])) . "</td>\n";
986 if ($form_cb_pubpid) {
987 echo " <td class='detail'> " . attr($row['pubpid']) . "</td>\n";
989 if ($form_cb_policy) {
990 echo " <td class='detail'> " . attr($row['policy']) . "</td>\n";
992 if ($form_cb_phone) {
993 echo " <td class='detail'> " . attr($row['phone']) . "</td>\n";
996 echo " <td class='detail'> " . attr($row['city']) . "</td>\n";
998 if ($form_cb_ins1 ||
$form_payer_id ) {
999 echo " <td class='detail'> " . attr($row['ins1']) . "</td>\n";
1001 if ($form_provider) {
1002 echo " <td class='detail'> " . attr($provider_name) . "</td>\n";
1004 if ($form_cb_referrer) {
1005 echo " <td class='detail'> " . attr($row['referrer']) . "</td>\n";
1008 echo " <td class='detail' colspan='$initial_colspan'>";
1009 echo " </td>\n";
1013  
;<a href
="../billing/sl_eob_invoice.php?id=<?php echo attr($row['id']) ?>"
1014 target
="_blank"><?php
echo empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']; ?
></a
>
1017  
;<?php
echo attr(oeFormatShortDate($row['dos'])); ?
>
1019 <?php
if ($form_cb_adate) { ?
>
1021  
;<?php
echo attr(oeFormatShortDate($row['ladate'])); ?
>
1024 <td
class="detail" align
="right">
1025 <?php
attr(bucks($row['charges'])) ?
> 
;
1027 <td
class="detail" align
="right">
1028 <?php
attr(bucks($row['adjustments'])) ?
> 
;
1030 <td
class="detail" align
="right">
1031 <?php
attr(bucks($row['paid'])) ?
> 
;
1034 if ($form_age_cols) {
1035 for ($c = 0; $c < $form_age_cols; ++
$c) {
1036 echo " <td class='detail' align='right'>";
1037 if ($c == $agecolno) {
1040 echo " </td>\n";
1045 <td
class="detail" align
="right"><?php
bucks($balance) ?
> 
;</td
>
1050 if ($form_cb_idays) {
1051 echo " <td class='detail' align='right'>";
1052 echo attr($row['inactive_days']) . " </td>\n";
1055 <td
class="detail" align
="center">
1056 <?php
echo $row['duncount'] ?
$row['duncount'] : " " ?
>
1058 <td
class="detail" align
="center">
1060 if ($in_collections) {
1061 echo " <b><font color='red'>IC</font></b>\n";
1063 echo " <input type='checkbox' name='form_cb[" . attr($row['invnumber']) . "]' />\n";
1069 echo " <td class='detail'> ";
1070 echo text($row['billing_errmsg']) . "</td>\n";
1075 } // end not export and not insurance summary
1077 else if ($_POST['form_csvexport']) {
1079 # The CSV detail line is written here added conditions for checked items (TLH).
1080 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
1082 // echo '"' . $insname . '",';
1083 echo '"' . $row['ins1'] . '",';
1084 echo '"' . $ptname . '",';
1087 echo '"' . $row['ss'] . '",';
1091 echo '"' . oeFormatShortDate($row['DOB']) . '",';
1095 echo '"' . $row['pubpid'] . '",';
1097 if ($form_cb_policy)
1099 echo '"' . $row['policy'] . '",';
1103 echo '"' . $row['phone'] . '",';
1107 echo '"' . $row['city'] . '",';
1109 echo '"' . (empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']) . '",';
1110 echo '"' . oeFormatShortDate($row['dos']) . '",';
1111 echo '"' . $row['referrer'] . '",';
1112 echo '"' . $row['provider'] . '",';
1113 echo '"' . oeFormatMoney($row['charges']) . '",';
1114 echo '"' . oeFormatMoney($row['adjustments']) . '",';
1115 echo '"' . oeFormatMoney($row['paid']) . '",';
1116 echo '"' . oeFormatMoney($balance) . '",';
1117 echo '"' . $row['inactive_days'] . '",';
1120 echo '"' . oeFormatShortDate($row['ladate']) . '",';
1121 echo '"' . $row['billing_errmsg'] . '"' . "\n";
1125 echo '"' . oeFormatShortDate($row['ladate']) . '"' . "\n";
1128 } // end $form_csvexport
1132 if ($is_ins_summary)
1133 endInsurance($ptrow);
1137 if ($_POST['form_export']) {
1138 echo "</textarea>\n";
1139 $alertmsg .= "$export_patient_count patients with total of " .
1140 oeFormatMoney($export_dollars) . " have been exported ";
1141 if ($_POST['form_without']) {
1142 $alertmsg .= "but NOT flagged as in collections.";
1144 $alertmsg .= "AND flagged as in collections.";
1147 else if ($_POST['form_csvexport']) {
1148 // echo "</textarea>\n";
1149 // $alertmsg .= "$export_patient_count patients representing $" .
1150 // sprintf("%.2f", $export_dollars) . " have been exported.";
1153 echo " <tr bgcolor='#ffffff'>\n";
1154 if ($is_ins_summary) {
1155 echo " <td class='dehead'> " . xlt('Report Totals') . ":</td>\n";
1157 echo " <td class='detail' colspan='" . attr($initial_colspan) . "'>\n";
1158 echo " </td>\n";
1159 echo " <td class='dehead' colspan='" . attr($final_colspan - 3) .
1160 "'> " . xlt('Report Totals') . ":</td>\n";
1162 echo " <td class='dehead' align='right'> " .
1163 oeFormatMoney($grand_total_charges) . " </td>\n";
1164 echo " <td class='dehead' align='right'> " .
1165 oeFormatMoney($grand_total_adjustments) . " </td>\n";
1166 echo " <td class='dehead' align='right'> " .
1167 oeFormatMoney($grand_total_paid) . " </td>\n";
1168 if ($form_age_cols) {
1169 for ($c = 0; $c < $form_age_cols; ++
$c) {
1170 echo " <td class='dehead' align='right'>" .
1171 oeFormatMoney($grand_total_agedbal[$c]) . " </td>\n";
1175 echo " <td class='dehead' align='right'>" .
1176 oeFormatMoney($grand_total_charges +
1177 $grand_total_adjustments - $grand_total_paid) . " </td>\n";
1179 if ($form_cb_idays) echo " <td class='detail'> </td>\n";
1180 if (!$is_ins_summary) echo " <td class='detail' colspan='2'> </td>\n";
1181 if ($form_cb_err) echo " <td class='detail'> </td>\n";
1186 } // end if form_refresh
1189 if (!$_POST['form_csvexport']) {
1190 if (!$_POST['form_export']) {
1193 <div style
='float;margin-top:5px'>
1195 <a href
='javascript:;' class='css_button' onclick
='checkAll(true)'><span
><?php
echo xlt('Select All'); ?
></span
></a
>
1196 <a href
='javascript:;' class='css_button' onclick
='checkAll(false)'><span
><?php
echo xlt('Clear All'); ?
></span
></a
>
1197 <a href
='javascript:;' class='css_button' onclick
='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
1198 <span
><?php
echo xlt('Export Selected as CSV'); ?
></span
>
1200 <a href
='javascript:;' class='css_button' onclick
='$("#form_export").attr("value","true"); $("#theform").submit();'>
1201 <span
><?php
echo xlt('Export Selected to Collections'); ?
></span
>
1205 <div style
='float:left'>
1206 <label
><input type
='checkbox' name
='form_individual' value
='1' /> <?php
echo xlt('Export Individual Invoices') ?
> 
; 
;</label
>
1209 <div style
='float:left'>
1210 <label
><input type
='checkbox' name
='form_without' value
='1' /> <?php
echo xlt('Without Update') ?
></label
>
1218 <script language
="JavaScript">
1221 echo "alert('" . addslashes($alertmsg) . "');\n";
1226 <!-- stuff
for the popup calendar
-->
1227 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
1228 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
1229 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
1230 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
1231 <script language
="Javascript">
1232 Calendar
.setup({inputField
:"form_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
1233 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
1237 } // end not form_csvexport