2 // Copyright (C) 2006-2010 Rod Roark <rod@sunsetsystems.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once("../globals.php");
10 require_once("../../library/patient.inc");
11 require_once("../../library/sql-ledger.inc");
12 require_once("../../library/invoice_summary.inc.php");
13 require_once("../../library/sl_eob.inc.php");
14 require_once("../../library/formatting.inc.php");
15 require_once "$srcdir/options.inc.php";
16 require_once "$srcdir/formdata.inc.php";
18 $INTEGRATED_AR = $GLOBALS['oer_config']['ws_accounting']['enabled'] === 2;
22 $export_patient_count = 0;
25 $today = date("Y-m-d");
27 $form_date = fixDate($_POST['form_date'], "");
28 $form_to_date = fixDate($_POST['form_to_date'], "");
29 $is_ins_summary = $_POST['form_category'] == 'Ins Summary';
30 $is_due_ins = ($_POST['form_category'] == 'Due Ins') ||
$is_ins_summary;
31 $is_due_pt = $_POST['form_category'] == 'Due Pt';
32 $is_all = $_POST['form_category'] == 'All';
33 $is_ageby_lad = strpos($_POST['form_ageby'], 'Last') !== false;
34 $form_facility = $_POST['form_facility'];
36 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
37 if ($is_ins_summary) {
40 $form_cb_pubpid = false;
41 $form_cb_adate = false;
42 $form_cb_policy = false;
43 $form_cb_phone = false;
44 $form_cb_city = false;
45 $form_cb_ins1 = false;
46 $form_cb_referrer = false;
47 $form_cb_idays = false;
50 $form_cb_ssn = $_POST['form_cb_ssn'] ?
true : false;
51 $form_cb_dob = $_POST['form_cb_dob'] ?
true : false;
52 $form_cb_pubpid = $_POST['form_cb_pubpid'] ?
true : false;
53 $form_cb_adate = $_POST['form_cb_adate'] ?
true : false;
54 $form_cb_policy = $_POST['form_cb_policy'] ?
true : false;
55 $form_cb_phone = $_POST['form_cb_phone'] ?
true : false;
56 $form_cb_city = $_POST['form_cb_city'] ?
true : false;
57 $form_cb_ins1 = $_POST['form_cb_ins1'] ?
true : false;
58 $form_cb_referrer = $_POST['form_cb_referrer'] ?
true : false;
59 $form_cb_idays = $_POST['form_cb_idays'] ?
true : false;
60 $form_cb_err = $_POST['form_cb_err'] ?
true : false;
65 $form_cb_pubpid = false;
66 $form_cb_adate = false;
67 $form_cb_policy = false;
68 $form_cb_phone = true;
69 $form_cb_city = false;
70 $form_cb_ins1 = false;
71 $form_cb_referrer = false;
72 $form_cb_idays = false;
75 $form_age_cols = (int) $_POST['form_age_cols'];
76 $form_age_inc = (int) $_POST['form_age_inc'];
77 if ($form_age_cols > 0 && $form_age_cols < 50) {
78 if ($form_age_inc <= 0) $form_age_inc = 30;
85 if ($is_due_ins ) ++
$initial_colspan;
86 if ($form_cb_ssn ) ++
$initial_colspan;
87 if ($form_cb_dob ) ++
$initial_colspan;
88 if ($form_cb_pubpid ) ++
$initial_colspan;
89 if ($form_cb_policy ) ++
$initial_colspan;
90 if ($form_cb_phone ) ++
$initial_colspan;
91 if ($form_cb_city ) ++
$initial_colspan;
92 if ($form_cb_ins1 ) ++
$initial_colspan;
93 if ($form_cb_referrer) ++
$initial_colspan;
95 $final_colspan = $form_cb_adate ?
6 : 5;
97 $grand_total_charges = 0;
98 $grand_total_adjustments = 0;
99 $grand_total_paid = 0;
100 $grand_total_agedbal = array();
101 for ($c = 0; $c < $form_age_cols; ++
$c) $grand_total_agedbal[$c] = 0;
103 if (!$INTEGRATED_AR) SLConnect();
105 function bucks($amount) {
107 echo oeFormatMoney($amount); // was printf("%.2f", $amount);
110 function endPatient($ptrow) {
111 global $export_patient_count, $export_dollars, $bgcolor;
112 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
113 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
114 global $initial_colspan, $final_colspan, $form_cb_idays, $form_cb_err;
116 if (!$ptrow['pid']) return;
118 $pt_balance = $ptrow['amount'] - $ptrow['paid'];
120 if ($_POST['form_export']) {
121 // This is a fixed-length format used by Transworld Systems. Your
122 // needs will surely be different, so consider this just an example.
124 echo "1896H"; // client number goes here
125 echo "000"; // filler
126 echo sprintf("%-30s", substr($ptrow['ptname'], 0, 30));
127 echo sprintf("%-30s", " ");
128 echo sprintf("%-30s", substr($ptrow['address1'], 0, 30));
129 echo sprintf("%-15s", substr($ptrow['city'], 0, 15));
130 echo sprintf("%-2s", substr($ptrow['state'], 0, 2));
131 echo sprintf("%-5s", $ptrow['zipcode'] ?
substr($ptrow['zipcode'], 0, 5) : '00000');
132 echo "1"; // service code
133 echo sprintf("%010.0f", $ptrow['pid']); // transmittal number = patient id
135 echo sprintf("%-15s", substr($ptrow['ss'], 0, 15));
136 echo substr($ptrow['dos'], 5, 2) . substr($ptrow['dos'], 8, 2) . substr($ptrow['dos'], 2, 2);
137 echo sprintf("%08.0f", $pt_balance * 100);
138 echo sprintf("%-9s\n", " ");
140 if (!$_POST['form_without']) {
141 sqlStatement("UPDATE patient_data SET " .
142 "genericname2 = 'Billing', " .
143 "genericval2 = CONCAT('IN COLLECTIONS " . date("Y-m-d") . "', genericval2) " .
144 "WHERE pid = '" . $ptrow['pid'] . "'");
146 $export_patient_count +
= 1;
147 $export_dollars +
= $pt_balance;
149 else if ($_POST['form_csvexport']) {
150 $export_patient_count +
= 1;
151 $export_dollars +
= $pt_balance;
154 if ($ptrow['count'] > 1) {
155 echo " <tr bgcolor='$bgcolor'>\n";
156 /***************************************************************
157 echo " <td class='detail' colspan='$initial_colspan'>";
158 echo " </td>\n";
159 echo " <td class='detotal' colspan='$final_colspan'> Total Patient Balance:</td>\n";
160 ***************************************************************/
161 echo " <td class='detotal' colspan='" . ($initial_colspan +
$final_colspan) .
162 "'> " . xl('Total Patient Balance') . ":</td>\n";
163 /**************************************************************/
164 if ($form_age_cols) {
165 for ($c = 0; $c < $form_age_cols; ++
$c) {
166 echo " <td class='detotal' align='right'> " .
167 oeFormatMoney($ptrow['agedbal'][$c]) . " </td>\n";
171 echo " <td class='detotal' align='right'> " .
172 oeFormatMoney($pt_balance) . " </td>\n";
174 if ($form_cb_idays) echo " <td class='detail'> </td>\n";
175 echo " <td class='detail' colspan='2'> </td>\n";
176 if ($form_cb_err) echo " <td class='detail'> </td>\n";
180 $grand_total_charges +
= $ptrow['charges'];
181 $grand_total_adjustments +
= $ptrow['adjustments'];
182 $grand_total_paid +
= $ptrow['paid'];
183 for ($c = 0; $c < $form_age_cols; ++
$c) {
184 $grand_total_agedbal[$c] +
= $ptrow['agedbal'][$c];
188 function endInsurance($insrow) {
189 global $export_patient_count, $export_dollars, $bgcolor;
190 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
191 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
192 global $initial_colspan, $form_cb_idays, $form_cb_err;
193 if (!$insrow['pid']) return;
194 $ins_balance = $insrow['amount'] - $insrow['paid'];
195 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
196 // No exporting of insurance summaries.
197 $export_patient_count +
= 1;
198 $export_dollars +
= $ins_balance;
201 echo " <tr bgcolor='$bgcolor'>\n";
202 echo " <td class='detail'>" . $insrow['insname'] . "</td>\n";
203 echo " <td class='detotal' align='right'> " .
204 oeFormatMoney($insrow['charges']) . " </td>\n";
205 echo " <td class='detotal' align='right'> " .
206 oeFormatMoney($insrow['adjustments']) . " </td>\n";
207 echo " <td class='detotal' align='right'> " .
208 oeFormatMoney($insrow['paid']) . " </td>\n";
209 if ($form_age_cols) {
210 for ($c = 0; $c < $form_age_cols; ++
$c) {
211 echo " <td class='detotal' align='right'> " .
212 oeFormatMoney($insrow['agedbal'][$c]) . " </td>\n";
216 echo " <td class='detotal' align='right'> " .
217 oeFormatMoney($ins_balance) . " </td>\n";
221 $grand_total_charges +
= $insrow['charges'];
222 $grand_total_adjustments +
= $insrow['adjustments'];
223 $grand_total_paid +
= $insrow['paid'];
224 for ($c = 0; $c < $form_age_cols; ++
$c) {
225 $grand_total_agedbal[$c] +
= $insrow['agedbal'][$c];
229 function getInsName($payerid) {
230 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = '$payerid'");
234 // In the case of CSV export only, a download will be forced.
235 if ($_POST['form_csvexport']) {
236 header("Pragma: public");
237 header("Expires: 0");
238 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
239 header("Content-Type: application/force-download");
240 header("Content-Disposition: attachment; filename=collections_report.csv");
241 header("Content-Description: File Transfer");
247 <?php
if (function_exists('html_header_show')) html_header_show(); ?
>
248 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
249 <title
><?php
xl('Collections Report','e')?
></title
>
250 <style type
="text/css">
257 #report_parameters_daterange {
266 /* specifically exclude some from the screen */
268 #report_parameters_daterange {
276 <script language
="JavaScript">
278 function checkAll(checked
) {
279 var f
= document
.forms
[0];
280 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
281 var ename
= f
.elements
[i
].name
;
282 if (ename
.indexOf('form_cb[') == 0)
283 f
.elements
[i
].checked
= checked
;
291 <body
class="body_top">
293 <span
class='title'><?php
xl('Report','e'); ?
> - <?php
xl('Collections','e'); ?
></span
>
295 <form method
='post' action
='collections_report.php' enctype
='multipart/form-data' id
='theform'>
297 <div id
="report_parameters">
299 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
300 <input type
='hidden' name
='form_export' id
='form_export' value
=''/>
301 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
306 <div style
='float:left'>
313 <td
><?php
xl('Displayed Columns','e') ?
>:</td
>
317 <input type
='checkbox' name
='form_cb_ssn'<?php
if ($form_cb_ssn) echo ' checked'; ?
>>
318 <?php
xl('SSN','e') ?
> 
;
321 <input type
='checkbox' name
='form_cb_dob'<?php
if ($form_cb_dob) echo ' checked'; ?
>>
322 <?php
xl('DOB','e') ?
> 
;
325 <input type
='checkbox' name
='form_cb_pubpid'<?php
if ($form_cb_pubpid) echo ' checked'; ?
>>
326 <?php
xl('ID','e') ?
> 
;
329 <input type
='checkbox' name
='form_cb_policy'<?php
if ($form_cb_policy) echo ' checked'; ?
>>
330 <?php
xl('Policy','e') ?
> 
;
333 <input type
='checkbox' name
='form_cb_phone'<?php
if ($form_cb_phone) echo ' checked'; ?
>>
334 <?php
xl('Phone','e') ?
> 
;
337 <input type
='checkbox' name
='form_cb_city'<?php
if ($form_cb_city) echo ' checked'; ?
>>
338 <?php
xl('City','e') ?
> 
;
343 <input type
='checkbox' name
='form_cb_ins1'<?php
if ($form_cb_ins1) echo ' checked'; ?
>>
344 <?php
xl('Primary Ins','e') ?
> 
;
347 <input type
='checkbox' name
='form_cb_referrer'<?php
if ($form_cb_referrer) echo ' checked'; ?
>>
348 <?php
xl('Referrer','e') ?
> 
;
351 <input type
='checkbox' name
='form_cb_adate'<?php
if ($form_cb_adate) echo ' checked'; ?
>>
352 <?php
xl('Act Date','e') ?
> 
;
355 <input type
='checkbox' name
='form_cb_idays'<?php
if ($form_cb_idays) echo ' checked'; ?
>>
356 <?php
xl('Inactive Days','e') ?
> 
;
359 <input type
='checkbox' name
='form_cb_err'<?php
if ($form_cb_err) echo ' checked'; ?
>>
360 <?php
xl('Errors','e') ?
>
372 <?php
xl('Service Date','e'); ?
>:
375 <input type
='text' name
='form_date' id
="form_date" size
='10' value
='<?php echo $form_date ?>'
376 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
377 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
378 id
='img_from_date' border
='0' alt
='[?]' style
='cursor:pointer'
379 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
382 <?php
xl('To','e'); ?
>:
385 <input type
='text' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo $form_to_date ?>'
386 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'>
387 <img src
='../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
388 id
='img_to_date' border
='0' alt
='[?]' style
='cursor:pointer'
389 title
='<?php xl('Click here to choose a date
','e
'); ?>'>
392 <select name
='form_category'>
394 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) {
395 echo " <option value='$key'";
396 if ($_POST['form_category'] == $key) echo " selected";
397 echo ">$value</option>\n";
408 <?php
xl('Facility','e'); ?
>:
411 <?php
dropdown_facility(strip_escape_custom($form_facility), 'form_facility', false); ?
>
417 <?php
xl('Age By','e') ?
>:
420 <select name
='form_ageby'>
422 foreach (array('Service Date', 'Last Activity Date') as $value) {
423 echo " <option value='$value'";
424 if ($_POST['form_ageby'] == $value) echo " selected";
425 echo ">" . xl($value) . "</option>\n";
433 <?php
xl('Aging Columns:','e') ?
>
436 <input type
='text' name
='form_age_cols' size
='2' value
='<?php echo $form_age_cols; ?>' />
439 <?php
xl('Days/Col:','e') ?
>
442 <input type
='text' name
='form_age_inc' size
='3' value
='<?php echo $form_age_inc; ?>' />
455 <td align
='left' valign
='middle' height
="100%">
456 <table style
='border-left:1px solid; width:100%; height:100%' >
459 <div style
='margin-left:15px'>
460 <a href
='#' class='css_button' onclick
='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
462 <?php
xl('Submit','e'); ?
>
466 <?php
if ($_POST['form_refresh']) { ?
>
467 <a href
='#' class='css_button' onclick
='window.print()'>
469 <?php
xl('Print','e'); ?
>
485 } // end not form_csvexport
487 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
491 if ($INTEGRATED_AR) {
492 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
494 foreach ($_POST['form_cb'] as $key => $value) $where .= " OR f.pid = $key";
498 if ($where) $where .= " AND ";
500 $where .= "f.date >= '$form_date 00:00:00' AND f.date <= '$form_to_date 23:59:59'";
503 $where .= "f.date >= '$form_date 00:00:00' AND f.date <= '$form_date 23:59:59'";
506 if ($form_facility) {
507 if ($where) $where .= " AND ";
508 $where .= "f.facility_id = '$form_facility'";
514 $query = "SELECT f.id, f.date, f.pid, f.encounter, f.last_level_billed, " .
515 "f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, " .
516 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
517 "p.postal_code, p.phone_home, p.ss, p.genericname2, p.genericval2, " .
518 "p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, " .
519 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
520 "b.pid = f.pid AND b.encounter = f.encounter AND " .
521 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
522 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
523 "b.pid = f.pid AND b.encounter = f.encounter AND " .
524 "b.activity = 1 AND b.code_type = 'COPAY' ) AS copays, " .
525 "( SELECT SUM(s.fee) FROM drug_sales AS s WHERE " .
526 "s.pid = f.pid AND s.encounter = f.encounter ) AS sales, " .
527 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
528 "a.pid = f.pid AND a.encounter = f.encounter ) AS payments, " .
529 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
530 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
531 "FROM form_encounter AS f " .
532 "JOIN patient_data AS p ON p.pid = f.pid " .
533 "LEFT OUTER JOIN users AS u ON u.id = p.providerID " .
535 "ORDER BY f.pid, f.encounter";
536 $eres = sqlStatement($query);
538 while ($erow = sqlFetchArray($eres)) {
539 $patient_id = $erow['pid'];
540 $encounter_id = $erow['encounter'];
541 $pt_balance = $erow['charges'] +
$erow['sales'] +
$erow['copays'] - $erow['payments'] - $erow['adjustments'];
542 $pt_balance = 0 +
sprintf("%.2f", $pt_balance); // yes this seems to be necessary
543 $svcdate = substr($erow['date'], 0, 10);
545 if ($_POST['form_refresh'] && ! $is_all) {
546 if ($pt_balance == 0) continue;
548 if ($_POST['form_category'] == 'Credits') {
549 if ($pt_balance > 0) continue;
552 // If we have not yet billed the patient, then compute $duncount as a
553 // negative count of the number of insurance plans for which we have not
554 // yet closed out insurance. Here we also compute $insname as the name of
555 // the insurance plan from which we are awaiting payment, and its sequence
556 // number $insposition (1-3).
557 $last_level_closed = $erow['last_level_closed'];
558 $duncount = $erow['stmt_count'];
563 for ($i = 1; $i <= 3; ++
$i) {
564 $tmp = arGetPayerID($patient_id, $svcdate, $i);
565 if (empty($tmp)) break;
568 $duncount = $last_level_closed - count($payerids);
570 if (!empty($payerids[$last_level_closed])) {
571 $insname = getInsName($payerids[$last_level_closed]);
572 $insposition = $last_level_closed +
1;
577 // Skip invoices not in the desired "Due..." category.
579 if ($is_due_ins && $duncount >= 0) continue;
580 if ($is_due_pt && $duncount < 0) continue;
582 // echo "<!-- " . $erow['encounter'] . ': ' . $erow['charges'] . ' + ' . $erow['sales'] . ' + ' . $erow['copays'] . ' - ' . $erow['payments'] . ' - ' . $erow['adjustments'] . " -->\n"; // debugging
584 // An invoice is due from the patient if money is owed and we are
585 // not waiting for insurance to pay.
586 $isduept = ($duncount >= 0) ?
" checked" : "";
590 $row['id'] = $erow['id'];
591 $row['invnumber'] = "$patient_id.$encounter_id";
592 $row['custid'] = $patient_id;
593 $row['name'] = $erow['fname'] . ' ' . $erow['lname'];
594 $row['address1'] = $erow['street'];
595 $row['city'] = $erow['city'];
596 $row['state'] = $erow['state'];
597 $row['zipcode'] = $erow['postal_code'];
598 $row['phone'] = $erow['phone_home'];
599 $row['duncount'] = $duncount;
600 $row['dos'] = $svcdate;
601 $row['ss'] = $erow['ss'];
602 $row['DOB'] = $erow['DOB'];
603 $row['pubpid'] = $erow['pubpid'];
604 $row['billnote'] = ($erow['genericname2'] == 'Billing') ?
$erow['genericval2'] : '';
605 $row['referrer'] = $erow['referrer'];
606 $row['irnumber'] = $erow['invoice_refno'];
608 // Also get the primary insurance company name whenever there is one.
610 if ($insposition == 1) {
611 $row['ins1'] = $insname;
613 if (empty($payerids)) {
614 $tmp = arGetPayerID($patient_id, $svcdate, 1);
615 if (!empty($tmp)) $payerids[] = $tmp;
617 if (!empty($payerids)) {
618 $row['ins1'] = getInsName($payerids[0]);
622 // This computes the invoice's total original charges and adjustments,
623 // date of last activity, and determines if insurance has responded to
624 // all billing items.
625 $invlines = ar_get_invoice_summary($patient_id, $encounter_id, true);
627 // if ($encounter_id == 185) { // debugging
629 // print_r($invlines);
634 $row['adjustments'] = 0;
636 $ins_seems_done = true;
638 foreach ($invlines as $key => $value) {
639 $row['charges'] +
= $value['chg'] +
$value['adj'];
640 $row['adjustments'] +
= 0 - $value['adj'];
641 $row['paid'] +
= $value['chg'] - $value['bal'];
642 foreach ($value['dtl'] as $dkey => $dvalue) {
643 $dtldate = trim(substr($dkey, 0, 10));
644 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
646 $lckey = strtolower($key);
647 if ($lckey == 'co-pay' ||
$lckey == 'claim') continue;
648 if (count($value['dtl']) <= 1) $ins_seems_done = false;
651 // Simulating ar.amount in SQL-Ledger which is charges with adjustments:
652 $row['amount'] = $row['charges'] +
$row['adjustments'];
654 $row['billing_errmsg'] = '';
655 if ($is_due_ins && $last_level_closed < 1 && $ins_seems_done)
656 $row['billing_errmsg'] = 'Ins1 seems done';
657 else if ($last_level_closed >= 1 && !$ins_seems_done)
658 $row['billing_errmsg'] = 'Ins1 seems not done';
660 $row['ladate'] = $ladate;
662 // Compute number of days since last activity.
663 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
664 substr($ladate, 8, 2), substr($ladate, 0, 4));
665 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
667 // Look up insurance policy number if we need it.
668 if ($form_cb_policy) {
669 $instype = ($insposition == 2) ?
'secondary' : (($insposition == 3) ?
'tertiary' : 'primary');
670 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
671 "pid = '$patient_id' AND type = '$instype' AND date <= '$svcdate' " .
672 "ORDER BY date DESC LIMIT 1");
673 $row['policy'] = $insrow['policy_number'];
676 $ptname = $erow['lname'] . ", " . $erow['fname'];
677 if ($erow['mname']) $ptname .= " " . substr($erow['mname'], 0, 1);
679 if (!$is_due_ins ) $insname = '';
680 $rows[$insname . '|' . $ptname . '|' . $encounter_id] = $row;
682 } // end $INTEGRATED_AR
684 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
686 foreach ($_POST['form_cb'] as $key => $value) $where .= " OR ar.customer_id = $key";
691 if ($where) $where .= " AND ";
692 $date1 = substr($form_date, 0, 4) . substr($form_date, 5, 2) .
693 substr($form_date, 8, 2);
695 $date2 = substr($form_to_date, 0, 4) . substr($form_to_date, 5, 2) .
696 substr($form_to_date, 8, 2);
697 $where .= "((CAST (substring(ar.invnumber from position('.' in ar.invnumber) + 1 for 8) AS integer) " .
698 "BETWEEN '$date1' AND '$date2')";
699 $tmp = "date >= '$form_date' AND date <= '$form_to_date'";
702 // This catches old converted invoices where we have no encounters:
703 $where .= "(ar.invnumber LIKE '%.$date1'";
704 $tmp = "date = '$form_date'";
706 // Pick out the encounters from MySQL with the desired DOS:
707 $rez = sqlStatement("SELECT pid, encounter FROM form_encounter WHERE $tmp");
708 while ($row = sqlFetchArray($rez)) {
709 $where .= " OR ar.invnumber = '" . $row['pid'] . "." . $row['encounter'] . "'";
718 // Instead of the subselects in the following query, we will call
719 // get_invoice_summary() in order to get data at the procedure level and
720 // thus decide if insurance appears to be done with each invoice.
722 $query = "SELECT ar.id, ar.invnumber, ar.duedate, ar.amount, ar.paid, " .
723 "ar.intnotes, ar.notes, ar.shipvia, " .
724 "customer.id AS custid, customer.name, customer.address1, " .
725 "customer.city, customer.state, customer.zipcode, customer.phone " .
726 // ", (SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
727 // "invoice.trans_id = ar.id AND invoice.fxsellprice > 0) AS charges, " .
728 // "(SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
729 // "invoice.trans_id = ar.id AND invoice.fxsellprice < 0) AS adjustments " .
730 "FROM ar JOIN customer ON customer.id = ar.customer_id " .
732 if ($_POST['form_refresh'] && ! $is_all) {
733 $query .= "AND ar.amount != ar.paid ";
735 $query .= "ORDER BY ar.invnumber";
737 // echo "<!-- $query -->\n"; // debugging
739 $t_res = SLQuery($query);
740 if ($sl_err) die($sl_err);
741 $num_invoices = SLRowCount($t_res);
743 //////////////////////////////////////////////////////////////////
745 for ($irow = 0; $irow < $num_invoices; ++
$irow) {
746 $row = SLGetRow($t_res, $irow);
748 // If a facility was specified then skip invoices whose encounters
749 // do not indicate that facility.
750 if ($form_facility) {
751 list($patient_id, $encounter_id) = explode(".", $row['invnumber']);
752 $tmp = sqlQuery("SELECT count(*) AS count FROM form_encounter WHERE " .
753 "pid = '$patient_id' AND encounter = '$encounter_id' AND " .
754 "facility_id = '$form_facility'");
755 if (empty($tmp['count'])) continue;
758 $pt_balance = sprintf("%.2f",$row['amount']) - sprintf("%.2f",$row['paid']);
760 if ($_POST['form_category'] == 'Credits') {
761 if ($pt_balance > 0) continue;
764 // $duncount was originally supposed to be the number of times that
765 // the patient was sent a statement for this invoice.
767 $duncount = substr_count(strtolower($row['intnotes']), "statement sent");
769 // But if we have not yet billed the patient, then compute $duncount as a
770 // negative count of the number of insurance plans for which we have not
771 // yet closed out insurance. Here we also compute $insname as the name of
772 // the insurance plan from which we are awaiting payment, and its sequence
773 // number $insposition (1-3).
777 $inseobs = strtolower($row['shipvia']);
778 $insgot = strtolower($row['notes']);
780 foreach (array('ins1', 'ins2', 'ins3') as $value) {
781 $i = strpos($insgot, $value);
782 if ($i !== false && strpos($inseobs, $value) === false) {
784 if (!$insname && $is_due_ins) {
785 $j = strpos($insgot, "\n", $i);
786 if (!$j) $j = strlen($insgot);
787 $insname = trim(substr($row['notes'], $i +
5, $j - $i - 5));
788 $insposition = substr($value, 3); // 1, 2 or 3
793 $row['insname'] = $insname;
795 // Also get the primary insurance company name whenever there is one.
797 $i = strpos($insgot, 'ins1');
799 $j = strpos($insgot, "\n", $i);
800 if (!$j) $j = strlen($insgot);
801 $row['ins1'] = trim(substr($row['notes'], $i +
5, $j - $i - 5));
804 // An invoice is now due from the patient if money is owed and we are
805 // not waiting for insurance to pay. We no longer look at the due date
808 $isduept = ($duncount >= 0) ?
" checked" : "";
810 // Skip invoices not in the desired "Due..." category.
812 if ($is_due_ins && $duncount >= 0) continue;
813 if ($is_due_pt && $duncount < 0) continue;
815 $row['duncount'] = $duncount;
817 // Determine the date of service. An 8-digit encounter number is
818 // presumed to be a date of service imported during conversion.
819 // Otherwise look it up in the form_encounter table.
822 list($pid, $encounter) = explode(".", $row['invnumber']);
823 if (strlen($encounter) == 8) {
824 $svcdate = substr($encounter, 0, 4) . "-" . substr($encounter, 4, 2) .
825 "-" . substr($encounter, 6, 2);
827 else if ($encounter) {
828 $tmp = sqlQuery("SELECT date FROM form_encounter WHERE " .
829 "encounter = $encounter");
830 $svcdate = substr($tmp['date'], 0, 10);
833 $row['dos'] = $svcdate;
835 // This computes the invoice's total original charges and adjustments,
836 // date of last activity, and determines if insurance has responded to
837 // all billing items.
839 $invlines = get_invoice_summary($row['id'], true);
841 $row['adjustments'] = 0;
842 $ins_seems_done = true;
844 foreach ($invlines as $key => $value) {
845 $row['charges'] +
= $value['chg'] +
$value['adj'];
846 $row['adjustments'] +
= 0 - $value['adj'];
847 foreach ($value['dtl'] as $dkey => $dvalue) {
848 $dtldate = trim(substr($dkey, 0, 10));
849 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
851 $lckey = strtolower($key);
852 if ($lckey == 'co-pay' ||
$lckey == 'claim') continue;
853 if (count($value['dtl']) <= 1) $ins_seems_done = false;
855 $row['billing_errmsg'] = '';
856 if ($is_due_ins && strpos($inseobs, 'ins1') === false && $ins_seems_done)
857 $row['billing_errmsg'] = 'Ins1 seems done';
858 else if (strpos($inseobs, 'ins1') !== false && !$ins_seems_done)
859 $row['billing_errmsg'] = 'Ins1 seems not done';
861 $row['ladate'] = $ladate;
863 // Compute number of days since last activity.
864 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
865 substr($ladate, 8, 2), substr($ladate, 0, 4));
866 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
868 $pdrow = sqlQuery("SELECT pd.fname, pd.lname, pd.mname, pd.ss, " .
869 "pd.genericname2, pd.genericval2, pd.pid, pd.pubpid, pd.DOB, " .
870 "CONCAT(u.lname, ', ', u.fname) AS referrer FROM " .
871 "integration_mapping AS im, patient_data AS pd " .
872 "LEFT OUTER JOIN users AS u ON u.id = pd.providerID " .
873 "WHERE im.foreign_id = " . $row['custid'] . " AND " .
874 "im.foreign_table = 'customer' AND " .
875 "pd.id = im.local_id");
877 $row['ss'] = $pdrow['ss'];
878 $row['DOB'] = $pdrow['DOB'];
879 $row['pubpid'] = $pdrow['pubpid'];
880 $row['billnote'] = ($pdrow['genericname2'] == 'Billing') ?
$pdrow['genericval2'] : '';
881 $row['referrer'] = $pdrow['referrer'];
883 $ptname = $pdrow['lname'] . ", " . $pdrow['fname'];
884 if ($pdrow['mname']) $ptname .= " " . substr($pdrow['mname'], 0, 1);
886 // Look up insurance policy number if we need it.
887 if ($form_cb_policy) {
888 $patient_id = $pdrow['pid'];
889 $instype = ($insposition == 2) ?
'secondary' : (($insposition == 3) ?
'tertiary' : 'primary');
890 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
891 "pid = '$patient_id' AND type = '$instype' AND date <= '$svcdate' " .
892 "ORDER BY date DESC LIMIT 1");
893 $row['policy'] = $insrow['policy_number'];
896 $rows[$insname . '|' . $ptname . '|' . $encounter] = $row;
898 } // end not $INTEGRATED_AR
902 if ($_POST['form_export']) {
903 echo "<textarea rows='35' cols='100' readonly>";
905 else if ($_POST['form_csvexport']) {
918 echo '"LADate"' . "\n";
924 <div id
="report_results">
928 <?php
if ($is_due_ins) { ?
>
929 <th
> 
;<?php
xl('Insurance','e')?
></th
>
931 <?php
if (!$is_ins_summary) { ?
>
932 <th
> 
;<?php
xl('Name','e')?
></th
>
934 <?php
if ($form_cb_ssn) { ?
>
935 <th
> 
;<?php
xl('SSN','e')?
></th
>
937 <?php
if ($form_cb_dob) { ?
>
938 <th
> 
;<?php
xl('DOB','e')?
></th
>
940 <?php
if ($form_cb_pubpid) { ?
>
941 <th
> 
;<?php
xl('ID','e')?
></th
>
943 <?php
if ($form_cb_policy) { ?
>
944 <th
> 
;<?php
xl('Policy','e')?
></th
>
946 <?php
if ($form_cb_phone) { ?
>
947 <th
> 
;<?php
xl('Phone','e')?
></th
>
949 <?php
if ($form_cb_city) { ?
>
950 <th
> 
;<?php
xl('City','e')?
></th
>
952 <?php
if ($form_cb_ins1) { ?
>
953 <th
> 
;<?php
xl('Primary Ins','e')?
></th
>
955 <?php
if ($form_cb_referrer) { ?
>
956 <th
> 
;<?php
xl('Referrer','e')?
></th
>
958 <?php
if (!$is_ins_summary) { ?
>
959 <th
> 
;<?php
xl('Invoice','e') ?
></th
>
960 <th
> 
;<?php
xl('Svc Date','e') ?
></th
>
961 <?php
if ($form_cb_adate) { ?
>
962 <th
> 
;<?php
xl('Act Date','e')?
></th
>
965 <th align
="right"><?php
xl('Charge','e') ?
> 
;</th
>
966 <th align
="right"><?php
xl('Adjust','e') ?
> 
;</th
>
967 <th align
="right"><?php
xl('Paid','e') ?
> 
;</th
>
969 // Generate aging headers if appropriate, else balance header.
970 if ($form_age_cols) {
971 for ($c = 0; $c < $form_age_cols;) {
972 echo " <th class='dehead' align='right'>";
973 echo $form_age_inc * $c;
974 if (++
$c < $form_age_cols) {
975 echo "-" . ($form_age_inc * $c - 1);
984 <th align
="right"><?php
xl('Balance','e') ?
> 
;</th
>
988 <?php
if ($form_cb_idays) { ?
>
989 <th align
="right"><?php
xl('IDays','e')?
> 
;</th
>
991 <?php
if (!$is_ins_summary) { ?
>
992 <th align
="center"><?php
xl('Prv','e') ?
></th
>
993 <th align
="center"><?php
xl('Sel','e') ?
></th
>
995 <?php
if ($form_cb_err) { ?
>
996 <th
> 
;<?php
xl('Error','e')?
></th
>
1003 $ptrow = array('insname' => '', 'pid' => 0);
1006 foreach ($rows as $key => $row) {
1007 list($insname, $ptname, $trash) = explode('|', $key);
1008 list($pid, $encounter) = explode(".", $row['invnumber']);
1010 if ($is_ins_summary && $insname != $ptrow['insname']) {
1011 endInsurance($ptrow);
1012 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
1013 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1014 foreach ($row as $key => $value) $ptrow[$key] = $value;
1015 $ptrow['agedbal'] = array();
1017 else if (!$is_ins_summary && ($insname != $ptrow['insname'] ||
$pid != $ptrow['pid'])) {
1018 // For the report, this will write the patient totals. For the
1019 // collections export this writes everything for the patient:
1021 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
1022 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1023 foreach ($row as $key => $value) $ptrow[$key] = $value;
1024 $ptrow['agedbal'] = array();
1026 $ptrow['amount'] +
= $row['amount'];
1027 $ptrow['paid'] +
= $row['paid'];
1028 $ptrow['charges'] +
= $row['charges'];
1029 $ptrow['adjustments'] +
= $row['adjustments'];
1033 // Compute invoice balance and aging column number, and accumulate aging.
1034 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
1035 if ($form_age_cols) {
1036 $agedate = $is_ageby_lad ?
$row['ladate'] : $row['dos'];
1037 $agetime = mktime(0, 0, 0, substr($agedate, 5, 2),
1038 substr($agedate, 8, 2), substr($agedate, 0, 4));
1039 $days = floor((time() - $agetime) / (60 * 60 * 24));
1040 $agecolno = min($form_age_cols - 1, max(0, floor($days / $form_age_inc)));
1041 $ptrow['agedbal'][$agecolno] +
= $balance;
1044 if (!$is_ins_summary && !$_POST['form_export'] && !$_POST['form_csvexport']) {
1045 $in_collections = stristr($row['billnote'], 'IN COLLECTIONS') !== false;
1047 <tr bgcolor
='<?php echo $bgcolor ?>'>
1049 if ($ptrow['count'] == 1) {
1051 echo " <td class='detail'> $insname</td>\n";
1053 echo " <td class='detail'> $ptname</td>\n";
1055 echo " <td class='detail'> " . $row['ss'] . "</td>\n";
1058 echo " <td class='detail'> " . oeFormatShortDate($row['DOB']) . "</td>\n";
1060 if ($form_cb_pubpid) {
1061 echo " <td class='detail'> " . $row['pubpid'] . "</td>\n";
1063 if ($form_cb_policy) {
1064 echo " <td class='detail'> " . $row['policy'] . "</td>\n";
1066 if ($form_cb_phone) {
1067 echo " <td class='detail'> " . $row['phone'] . "</td>\n";
1069 if ($form_cb_city) {
1070 echo " <td class='detail'> " . $row['city'] . "</td>\n";
1072 if ($form_cb_ins1) {
1073 echo " <td class='detail'> " . $row['ins1'] . "</td>\n";
1075 if ($form_cb_referrer) {
1076 echo " <td class='detail'> " . $row['referrer'] . "</td>\n";
1079 echo " <td class='detail' colspan='$initial_colspan'>";
1080 echo " </td>\n";
1084  
;<a href
="../billing/sl_eob_invoice.php?id=<?php echo $row['id'] ?>"
1085 target
="_blank"><?php
echo empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']; ?
></a
>
1088  
;<?php
echo oeFormatShortDate($row['dos']); ?
>
1090 <?php
if ($form_cb_adate) { ?
>
1092  
;<?php
echo oeFormatShortDate($row['ladate']); ?
>
1095 <td
class="detail" align
="right">
1096 <?php
bucks($row['charges']) ?
> 
;
1098 <td
class="detail" align
="right">
1099 <?php
bucks($row['adjustments']) ?
> 
;
1101 <td
class="detail" align
="right">
1102 <?php
bucks($row['paid']) ?
> 
;
1105 if ($form_age_cols) {
1106 for ($c = 0; $c < $form_age_cols; ++
$c) {
1107 echo " <td class='detail' align='right'>";
1108 if ($c == $agecolno) {
1111 echo " </td>\n";
1116 <td
class="detail" align
="right"><?php
bucks($balance) ?
> 
;</td
>
1121 if ($form_cb_idays) {
1122 echo " <td class='detail' align='right'>";
1123 echo $row['inactive_days'] . " </td>\n";
1126 <td
class="detail" align
="center">
1127 <?php
echo $row['duncount'] ?
$row['duncount'] : " " ?
>
1129 <td
class="detail" align
="center">
1131 if ($ptrow['count'] == 1) {
1132 if ($in_collections) {
1133 echo " <b><font color='red'>IC</font></b>\n";
1135 echo " <input type='checkbox' name='form_cb[" . $row['custid'] . "]' />\n";
1144 echo " <td class='detail'> ";
1145 echo $row['billing_errmsg'] . "</td>\n";
1150 } // end not export and not insurance summary
1152 else if ($_POST['form_csvexport']) {
1153 // The CSV detail line is written here.
1154 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
1155 // echo '"' . $insname . '",';
1156 echo '"' . $row['ins1'] . '",';
1157 echo '"' . $ptname . '",';
1158 echo '"' . (empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']) . '",';
1159 echo '"' . oeFormatShortDate($row['dos']) . '",';
1160 echo '"' . $row['referrer'] . '",';
1161 echo '"' . oeFormatMoney($row['charges']) . '",';
1162 echo '"' . oeFormatMoney($row['adjustments']) . '",';
1163 echo '"' . oeFormatMoney($row['paid']) . '",';
1164 echo '"' . oeFormatMoney($balance) . '",';
1165 echo '"' . $row['inactive_days'] . '",';
1166 echo '"' . oeFormatShortDate($row['ladate']) . '"' . "\n";
1167 } // end $form_csvexport
1171 if ($is_ins_summary)
1172 endInsurance($ptrow);
1176 if ($_POST['form_export']) {
1177 echo "</textarea>\n";
1178 $alertmsg .= "$export_patient_count patients with total of " .
1179 oeFormatMoney($export_dollars) . " have been exported ";
1180 if ($_POST['form_without']) {
1181 $alertmsg .= "but NOT flagged as in collections.";
1183 $alertmsg .= "AND flagged as in collections.";
1186 else if ($_POST['form_csvexport']) {
1187 // echo "</textarea>\n";
1188 // $alertmsg .= "$export_patient_count patients representing $" .
1189 // sprintf("%.2f", $export_dollars) . " have been exported.";
1192 echo " <tr bgcolor='#ffffff'>\n";
1193 if ($is_ins_summary) {
1194 echo " <td class='dehead'> " . xl('Report Totals') . ":</td>\n";
1196 echo " <td class='detail' colspan='$initial_colspan'>\n";
1197 echo " </td>\n";
1198 echo " <td class='dehead' colspan='" . ($final_colspan - 3) .
1199 "'> " . xl('Report Totals') . ":</td>\n";
1201 echo " <td class='dehead' align='right'> " .
1202 oeFormatMoney($grand_total_charges) . " </td>\n";
1203 echo " <td class='dehead' align='right'> " .
1204 oeFormatMoney($grand_total_adjustments) . " </td>\n";
1205 echo " <td class='dehead' align='right'> " .
1206 oeFormatMoney($grand_total_paid) . " </td>\n";
1207 if ($form_age_cols) {
1208 for ($c = 0; $c < $form_age_cols; ++
$c) {
1209 echo " <td class='dehead' align='right'>" .
1210 oeFormatMoney($grand_total_agedbal[$c]) . " </td>\n";
1214 echo " <td class='dehead' align='right'>" .
1215 oeFormatMoney($grand_total_charges +
1216 $grand_total_adjustments - $grand_total_paid) . " </td>\n";
1218 if ($form_cb_idays) echo " <td class='detail'> </td>\n";
1219 if (!$is_ins_summary) echo " <td class='detail' colspan='2'> </td>\n";
1220 if ($form_cb_err) echo " <td class='detail'> </td>\n";
1225 } // end if form_refresh
1227 if (!$INTEGRATED_AR) SLClose();
1229 if (!$_POST['form_csvexport']) {
1230 if (!$_POST['form_export']) {
1233 <div style
='float;margin-top:5px'>
1235 <a href
='javascript:;' class='css_button' onclick
='checkAll(true)'><span
><?php
xl('Select All','e'); ?
></span
></a
>
1236 <a href
='javascript:;' class='css_button' onclick
='checkAll(false)'><span
><?php
xl('Clear All','e'); ?
></span
></a
>
1237 <a href
='javascript:;' class='css_button' onclick
='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
1238 <span
><?php
xl('Export Selected as CSV','e'); ?
></span
>
1240 <a href
='javascript:;' class='css_button' onclick
='$("#form_export").attr("value","true"); $("#theform").submit();'>
1241 <span
><?php
xl('Export Selected to Collections','e'); ?
></span
>
1245 <div style
='float:left'>
1246 <input type
='checkbox' name
='form_without' value
='1' /> <?php
xl('Without Update','e') ?
>
1254 <script language
="JavaScript">
1257 echo "alert('" . htmlentities($alertmsg) . "');\n";
1262 <!-- stuff
for the popup calendar
-->
1263 <style type
="text/css">@import
url(../../library
/dynarch_calendar
.css
);</style
>
1264 <script type
="text/javascript" src
="../../library/dynarch_calendar.js"></script
>
1265 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
1266 <script type
="text/javascript" src
="../../library/dynarch_calendar_setup.js"></script
>
1267 <script type
="text/javascript" src
="../../library/js/jquery.1.3.2.js"></script
>
1268 <script language
="Javascript">
1269 Calendar
.setup({inputField
:"form_date", ifFormat
:"%Y-%m-%d", button
:"img_from_date"});
1270 Calendar
.setup({inputField
:"form_to_date", ifFormat
:"%Y-%m-%d", button
:"img_to_date"});
1274 } // end not form_csvexport