added dob, ssn, policy to the CSV export
[openemr.git] / interface / reports / collections_report.php
bloba5bc56c37da0c6e5de00651c066bc985ab233f2a
1 <?php
2 // Copyright (C) 2006-2008 Rod Roark <rod@sunsetsystems.com>
3 //
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 include_once("../globals.php");
10 include_once("../../library/patient.inc");
11 include_once("../../library/sql-ledger.inc");
12 include_once("../../library/invoice_summary.inc.php");
13 // include_once("../../custom/statement.inc.php");
14 // include_once("../../library/sl_eob.inc.php");
16 $alertmsg = '';
17 $bgcolor = "#aaaaaa";
18 $export_patient_count = 0;
19 $export_dollars = 0;
21 $today = date("Y-m-d");
23 $form_date = fixDate($_POST['form_date'], "");
24 $form_to_date = fixDate($_POST['form_to_date'], "");
25 $is_due_ins = $_POST['form_category'] == xl('Due Ins');
26 $is_due_pt = $_POST['form_category'] == xl('Due Pt');
27 $is_all = $_POST['form_category'] == xl('All');
29 if ($_POST['form_search'] || $_POST['form_export'] || $_POST['form_csvexport']) {
30 $form_cb_ssn = $_POST['form_cb_ssn'] ? true : false;
31 $form_cb_dob = $_POST['form_cb_dob'] ? true : false;
32 $form_cb_policy = $_POST['form_cb_policy'] ? true : false;
33 $form_cb_phone = $_POST['form_cb_phone'] ? true : false;
34 $form_cb_city = $_POST['form_cb_city'] ? true : false;
35 $form_cb_ins1 = $_POST['form_cb_ins1'] ? true : false;
36 $form_cb_referrer = $_POST['form_cb_referrer'] ? true : false;
37 $form_cb_idays = $_POST['form_cb_idays'] ? true : false;
38 $form_cb_err = $_POST['form_cb_err'] ? true : false;
39 } else {
40 $form_cb_ssn = true;
41 $form_cb_dob = false;
42 $form_cb_policy = false;
43 $form_cb_phone = true;
44 $form_cb_city = false;
45 $form_cb_ins1 = false;
46 $form_cb_referrer = false;
47 $form_cb_idays = false;
48 $form_cb_err = false;
50 $form_age_cols = (int) $_POST['form_age_cols'];
51 $form_age_inc = (int) $_POST['form_age_inc'];
52 if ($form_age_cols > 0 && $form_age_cols < 50) {
53 if ($form_age_inc <= 0) $form_age_inc = 30;
54 } else {
55 $form_age_cols = 0;
56 $form_age_inc = 0;
59 $initial_colspan = 1;
60 if ($is_due_ins ) ++$initial_colspan;
61 if ($form_cb_ssn ) ++$initial_colspan;
62 if ($form_cb_dob ) ++$initial_colspan;
63 if ($form_cb_policy ) ++$initial_colspan;
64 if ($form_cb_phone ) ++$initial_colspan;
65 if ($form_cb_city ) ++$initial_colspan;
66 if ($form_cb_ins1 ) ++$initial_colspan;
67 if ($form_cb_referrer) ++$initial_colspan;
69 $grand_total_charges = 0;
70 $grand_total_adjustments = 0;
71 $grand_total_paid = 0;
72 $grand_total_agedbal = array();
73 for ($c = 0; $c < $form_age_cols; ++$c) $grand_total_agedbal[$c] = 0;
75 SLConnect();
77 function bucks($amount) {
78 if ($amount)
79 printf("%.2f", $amount);
82 function endPatient($ptrow) {
83 global $export_patient_count, $export_dollars, $bgcolor;
84 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
85 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
86 global $initial_colspan, $form_cb_idays, $form_cb_err;
88 if (!$ptrow['pid']) return;
90 $pt_balance = $ptrow['amount'] - $ptrow['paid'];
92 if ($_POST['form_export']) {
93 // This is a fixed-length format used by Transworld Systems. Your
94 // needs will surely be different, so consider this just an example.
96 echo "1896H"; // client number goes here
97 echo "000"; // filler
98 echo sprintf("%-30s", substr($ptrow['ptname'], 0, 30));
99 echo sprintf("%-30s", " ");
100 echo sprintf("%-30s", substr($ptrow['address1'], 0, 30));
101 echo sprintf("%-15s", substr($ptrow['city'], 0, 15));
102 echo sprintf("%-2s", substr($ptrow['state'], 0, 2));
103 echo sprintf("%-5s", $ptrow['zipcode'] ? substr($ptrow['zipcode'], 0, 5) : '00000');
104 echo "1"; // service code
105 echo sprintf("%010.0f", $ptrow['pid']); // transmittal number = patient id
106 echo " "; // filler
107 echo sprintf("%-15s", substr($ptrow['ss'], 0, 15));
108 echo substr($ptrow['dos'], 5, 2) . substr($ptrow['dos'], 8, 2) . substr($ptrow['dos'], 2, 2);
109 echo sprintf("%08.0f", $pt_balance * 100);
110 echo sprintf("%-9s\n", " ");
112 if (!$_POST['form_without']) {
113 sqlStatement("UPDATE patient_data SET " .
114 "genericname2 = 'Billing', " .
115 "genericval2 = CONCAT('IN COLLECTIONS " . date("Y-m-d") . "', genericval2) " .
116 "WHERE pid = '" . $ptrow['pid'] . "'");
118 $export_patient_count += 1;
119 $export_dollars += $pt_balance;
121 else if ($_POST['form_csvexport']) {
122 $export_patient_count += 1;
123 $export_dollars += $pt_balance;
125 else {
126 if ($ptrow['count'] > 1) {
127 echo " <tr bgcolor='$bgcolor'>\n";
128 echo " <td class='detail' colspan='$initial_colspan'>";
129 echo "&nbsp;</td>\n";
130 echo " <td class='detotal' colspan='5'>&nbsp;Total Patient Balance:</td>\n";
131 if ($form_age_cols) {
132 for ($c = 0; $c < $form_age_cols; ++$c) {
133 echo " <td class='detotal' align='right'>&nbsp;" .
134 sprintf("%.2f", $ptrow['agedbal'][$c]) . "&nbsp;</td>\n";
137 else {
138 echo " <td class='detotal' align='right'>&nbsp;" .
139 sprintf("%.2f", $pt_balance) . "&nbsp;</td>\n";
141 if ($form_cb_idays) echo " <td class='detail'>&nbsp;</td>\n";
142 echo " <td class='detail' colspan='2'>&nbsp;</td>\n";
143 if ($form_cb_err) echo " <td class='detail'>&nbsp;</td>\n";
144 echo " </tr>\n";
147 $grand_total_charges += $ptrow['charges'];
148 $grand_total_adjustments += $ptrow['adjustments'];
149 $grand_total_paid += $ptrow['paid'];
150 for ($c = 0; $c < $form_age_cols; ++$c) {
151 $grand_total_agedbal[$c] += $ptrow['agedbal'][$c];
155 // In the case of CSV export only, a download will be forced.
156 if ($_POST['form_csvexport']) {
157 header("Pragma: public");
158 header("Expires: 0");
159 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
160 header("Content-Type: application/force-download");
161 header("Content-Disposition: attachment; filename=collections_report.csv");
162 header("Content-Description: File Transfer");
164 else {
166 <html>
167 <head>
168 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
169 <title><?xl('Collections Report','e')?></title>
170 <style type="text/css">
171 body { font-family:sans-serif; font-size:10pt; font-weight:normal }
172 .dehead { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:bold }
173 .detail { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:normal }
174 .detotal { color:#996600; font-family:sans-serif; font-size:10pt; font-weight:normal }
175 </style>
177 <script language="JavaScript">
179 function checkAll(checked) {
180 var f = document.forms[0];
181 for (var i = 0; i < f.elements.length; ++i) {
182 var ename = f.elements[i].name;
183 if (ename.indexOf('form_cb[') == 0)
184 f.elements[i].checked = checked;
188 </script>
190 </head>
192 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
193 <center>
195 <form method='post' action='collections_report.php' enctype='multipart/form-data'>
197 <table border='0' cellpadding='5' cellspacing='0' width='98%'>
199 <tr>
200 <td height="1">
201 </td>
202 </tr>
204 <tr bgcolor='#ddddff'>
205 <td align='center'>
206 <input type='checkbox' name='form_cb_ssn'<?php if ($form_cb_ssn) echo ' checked'; ?>>
207 <?php xl('SSN','e') ?>&nbsp;
208 <input type='checkbox' name='form_cb_dob'<?php if ($form_cb_dob) echo ' checked'; ?>>
209 <?php xl('DOB','e') ?>&nbsp;
210 <input type='checkbox' name='form_cb_policy'<?php if ($form_cb_policy) echo ' checked'; ?>>
211 <?php xl('Policy','e') ?>&nbsp;
212 <input type='checkbox' name='form_cb_phone'<?php if ($form_cb_phone) echo ' checked'; ?>>
213 <?php xl('Phone','e') ?>&nbsp;
214 <input type='checkbox' name='form_cb_city'<?php if ($form_cb_city) echo ' checked'; ?>>
215 <?php xl('City','e') ?>&nbsp;
216 <input type='checkbox' name='form_cb_ins1'<?php if ($form_cb_ins1) echo ' checked'; ?>>
217 <?php xl('Primary Ins','e') ?>&nbsp;
218 <input type='checkbox' name='form_cb_referrer'<?php if ($form_cb_referrer) echo ' checked'; ?>>
219 <?php xl('Referrer','e') ?>&nbsp;
220 <input type='checkbox' name='form_cb_idays'<?php if ($form_cb_idays) echo ' checked'; ?>>
221 <?php xl('Inactive Days','e') ?>&nbsp;
222 <input type='checkbox' name='form_cb_err'<?php if ($form_cb_err) echo ' checked'; ?>>
223 <?php xl('Errors','e') ?>
224 </td>
225 </tr>
227 <tr bgcolor='#ddddff'>
228 <td align='center'>
229 <?php xl('Age Cols:','e') ?>
230 <input type='text' name='form_age_cols' size='2' value='<?php echo $form_age_cols; ?>'>
231 &nbsp;
232 <?php xl('Age Increment:','e') ?>
233 <input type='text' name='form_age_inc' size='3' value='<?php echo $form_age_inc; ?>'>
234 &nbsp;
235 <?xl('Svc Date:','e')?>
236 <input type='text' name='form_date' size='10' value='<?php echo $_POST['form_date']; ?>'
237 title='<?xl("Date of service mm/dd/yyyy","e")?>'>
238 &nbsp;
239 <?xl('To:','e')?>
240 <input type='text' name='form_to_date' size='10' value='<?php echo $_POST['form_to_date']; ?>'
241 title='<?xl("Ending DOS mm/dd/yyyy if you wish to enter a range","e")?>'>
242 &nbsp;
243 <select name='form_category'>
244 <?php
245 foreach (array(xl('Open'), xl('Due Pt'), xl('Due Ins'), xl('Credits'), xl('All')) as $value) {
246 echo " <option value='$value'";
247 if ($_POST['form_category'] == $value) echo " selected";
248 echo ">$value</option>\n";
251 </select>
252 &nbsp;
253 <input type='submit' name='form_search' value='<?xl("Search","e")?>'>
254 </td>
255 </tr>
257 <tr>
258 <td height="1">
259 </td>
260 </tr>
262 </table>
264 <?php
266 } // end not form_csvexport
268 if ($_POST['form_search'] || $_POST['form_export'] || $_POST['form_csvexport']) {
269 $where = "";
271 if ($_POST['form_export'] || $_POST['form_csvexport']) {
272 $where = "( 1 = 2";
273 foreach ($_POST['form_cb'] as $key => $value) $where .= " OR ar.customer_id = $key";
274 $where .= ' )';
277 if ($form_date) {
278 if ($where) $where .= " AND ";
279 $date1 = substr($form_date, 0, 4) . substr($form_date, 5, 2) .
280 substr($form_date, 8, 2);
281 if ($form_to_date) {
282 $date2 = substr($form_to_date, 0, 4) . substr($form_to_date, 5, 2) .
283 substr($form_to_date, 8, 2);
284 $where .= "((CAST (substring(ar.invnumber from position('.' in ar.invnumber) + 1 for 8) AS integer) " .
285 "BETWEEN '$date1' AND '$date2')";
286 $tmp = "date >= '$form_date' AND date <= '$form_to_date'";
288 else {
289 // This catches old converted invoices where we have no encounters:
290 $where .= "(ar.invnumber LIKE '%.$date1'";
291 $tmp = "date = '$form_date'";
293 // Pick out the encounters from MySQL with the desired DOS:
294 $rez = sqlStatement("SELECT pid, encounter FROM form_encounter WHERE $tmp");
295 while ($row = sqlFetchArray($rez)) {
296 $where .= " OR ar.invnumber = '" . $row['pid'] . "." . $row['encounter'] . "'";
298 $where .= ")";
301 if (! $where) {
302 $where = "1 = 1";
305 // TBD: Instead of the subselects in the following query, we will call
306 // get_invoice_summary() in order to get data at the procedure level and
307 // thus decide if insurance appears to be done with each invoice.
309 $query = "SELECT ar.id, ar.invnumber, ar.duedate, ar.amount, ar.paid, " .
310 "ar.intnotes, ar.notes, ar.shipvia, " .
311 "customer.id AS custid, customer.name, customer.address1, " .
312 "customer.city, customer.state, customer.zipcode, customer.phone " .
313 // ", (SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
314 // "invoice.trans_id = ar.id AND invoice.fxsellprice > 0) AS charges, " .
315 // "(SELECT SUM(invoice.fxsellprice) FROM invoice WHERE " .
316 // "invoice.trans_id = ar.id AND invoice.fxsellprice < 0) AS adjustments " .
317 "FROM ar JOIN customer ON customer.id = ar.customer_id " .
318 "WHERE ( $where ) ";
319 if ($_POST['form_search'] && ! $is_all) {
320 $query .= "AND ar.amount != ar.paid ";
322 $query .= "ORDER BY ar.invnumber";
324 // echo "<!-- $query -->\n"; // debugging
326 $t_res = SLQuery($query);
327 if ($sl_err) die($sl_err);
328 $num_invoices = SLRowCount($t_res);
330 //////////////////////////////////////////////////////////////////
332 $rows = array();
333 for ($irow = 0; $irow < $num_invoices; ++$irow) {
334 $row = SLGetRow($t_res, $irow);
335 $pt_balance = sprintf("%.2f",$row['amount']) - sprintf("%.2f",$row['paid']);
337 if ($_POST['form_category'] == 'Credits') {
338 if ($pt_balance > 0) continue;
341 // $duncount was originally supposed to be the number of times that
342 // the patient was sent a statement for this invoice.
344 $duncount = substr_count(strtolower($row['intnotes']), "statement sent");
346 // But if we have not yet billed the patient, then compute $duncount as a
347 // negative count of the number of insurance plans for which we have not
348 // yet closed out insurance. Here we also compute $insname as the name of
349 // the insurance plan from which we are awaiting payment, and its sequence
350 // number $insposition (1-3).
352 $insname = '';
353 $insposition = 0;
354 $inseobs = strtolower($row['shipvia']);
355 $insgot = strtolower($row['notes']);
356 if (! $duncount) {
357 foreach (array('ins1', 'ins2', 'ins3') as $value) {
358 $i = strpos($insgot, $value);
359 if ($i !== false && strpos($inseobs, $value) === false) {
360 --$duncount;
361 if (!$insname && $is_due_ins) {
362 $j = strpos($insgot, "\n", $i);
363 if (!$j) $j = strlen($insgot);
364 $insname = trim(substr($row['notes'], $i + 5, $j - $i - 5));
365 $insposition = substr($value, 3); // 1, 2 or 3
371 // Also get the primary insurance company name whenever there is one.
372 $row['ins1'] = '';
373 $i = strpos($insgot, 'ins1');
374 if ($i !== false) {
375 $j = strpos($insgot, "\n", $i);
376 if (!$j) $j = strlen($insgot);
377 $row['ins1'] = trim(substr($row['notes'], $i + 5, $j - $i - 5));
380 // An invoice is now due from the patient if money is owed and we are
381 // not waiting for insurance to pay. We no longer look at the due date
382 // for this.
384 $isduept = ($duncount >= 0) ? " checked" : "";
386 // Skip invoices not in the desired "Due..." category.
388 if ($is_due_ins && $duncount >= 0) continue;
389 if ($is_due_pt && $duncount < 0) continue;
391 $row['duncount'] = $duncount;
393 // Determine the date of service. An 8-digit encounter number is
394 // presumed to be a date of service imported during conversion.
395 // Otherwise look it up in the form_encounter table.
397 $svcdate = "";
398 list($pid, $encounter) = explode(".", $row['invnumber']);
399 if (strlen($encounter) == 8) {
400 $svcdate = substr($encounter, 0, 4) . "-" . substr($encounter, 4, 2) .
401 "-" . substr($encounter, 6, 2);
403 else if ($encounter) {
404 $tmp = sqlQuery("SELECT date FROM form_encounter WHERE " .
405 "encounter = $encounter");
406 $svcdate = substr($tmp['date'], 0, 10);
409 $row['dos'] = $svcdate;
411 // This computes the invoice's total original charges and adjustments,
412 // date of last activity, and determines if insurance has responded to
413 // all billing items.
415 $invlines = get_invoice_summary($row['id'], true);
416 $row['charges'] = 0;
417 $row['adjustments'] = 0;
418 $ins_seems_done = true;
419 $ladate = $svcdate;
420 // echo "\n<!-- $ladate * -->\n"; // debugging
421 foreach ($invlines as $key => $value) {
422 $row['charges'] += $value['chg'] + $value['adj'];
423 $row['adjustments'] += 0 - $value['adj'];
424 foreach ($value['dtl'] as $dkey => $dvalue) {
425 $dtldate = trim(substr($dkey, 0, 10));
426 // echo "\n<!-- $dtldate -->\n"; // debugging
427 if ($dtldate && $dtldate > $ladate) $ladate = $dtldate;
429 $lckey = strtolower($key);
430 if ($lckey == 'co-pay' || $lckey == 'claim') continue;
431 if (count($value['dtl']) <= 1) $ins_seems_done = false;
433 $row['billing_errmsg'] = '';
434 if ($is_due_ins && strpos($inseobs, 'ins1') === false && $ins_seems_done)
435 $row['billing_errmsg'] = 'Ins1 seems done';
436 else if (strpos($inseobs, 'ins1') !== false && !$ins_seems_done)
437 $row['billing_errmsg'] = 'Ins1 seems not done';
439 // Compute number of days since last activity.
440 $latime = mktime(0, 0, 0, substr($ladate, 5, 2),
441 substr($ladate, 8, 2), substr($ladate, 0, 4));
442 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
444 $pdrow = sqlQuery("SELECT pd.fname, pd.lname, pd.mname, pd.ss, " .
445 "pd.genericname2, pd.genericval2, pd.pid, pd.DOB, " .
446 "CONCAT(u.lname, ', ', u.fname) AS referrer FROM " .
447 "integration_mapping AS im, patient_data AS pd " .
448 "LEFT OUTER JOIN users AS u ON u.id = pd.providerID " .
449 "WHERE im.foreign_id = " . $row['custid'] . " AND " .
450 "im.foreign_table = 'customer' AND " .
451 "pd.id = im.local_id");
453 $row['ss'] = $pdrow['ss'];
454 $row['DOB'] = $pdrow['DOB'];
455 $row['billnote'] = ($pdrow['genericname2'] == 'Billing') ? $pdrow['genericval2'] : '';
456 $row['referrer'] = $pdrow['referrer'];
458 $ptname = $pdrow['lname'] . ", " . $pdrow['fname'];
459 if ($pdrow['mname']) $ptname .= " " . substr($pdrow['mname'], 0, 1);
461 // Look up insurance policy number if we need it.
462 if ($form_cb_policy || $_POST['form_csvexport']) {
463 $patient_id = $pdrow['pid'];
464 $instype = ($insposition == 2) ? 'secondary' : (($insposition == 3) ? 'tertiary' : 'primary');
465 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
466 "pid = '$patient_id' AND type = '$instype' AND date <= '$svcdate' " .
467 "ORDER BY date DESC LIMIT 1");
468 $row['policy'] = $insrow['policy_number'];
471 // $rows[$ptname] = $row;
472 $rows[$insname . '|' . $ptname . '|' . $encounter] = $row;
475 ksort($rows);
477 if ($_POST['form_export']) {
478 echo "<textarea rows='35' cols='100' readonly>";
480 else if ($_POST['form_csvexport']) {
481 // CSV headers:
482 if (true) {
483 echo '"Insurance",';
484 echo '"Name",';
485 echo '"DOB",';
486 echo '"SSN",';
487 echo '"Policy",';
488 echo '"Invoice",';
489 echo '"DOS",';
490 echo '"Referrer",';
491 echo '"Charge",';
492 echo '"Adjust",';
493 echo '"Paid",';
494 echo '"Balance",';
495 echo '"IDays"' . "\n";
498 else {
501 <table border='0' cellpadding='1' cellspacing='2' width='98%'>
503 <tr bgcolor="#dddddd">
504 <?php if ($is_due_ins) { ?>
505 <td class="dehead">&nbsp;<?php xl('Insurance','e')?></td>
506 <?php } ?>
507 <td class="dehead">&nbsp;<?php xl('Name','e')?></td>
508 <?php if ($form_cb_ssn) { ?>
509 <td class="dehead">&nbsp;<?php xl('SSN','e')?></td>
510 <?php } ?>
511 <?php if ($form_cb_dob) { ?>
512 <td class="dehead">&nbsp;<?php xl('DOB','e')?></td>
513 <?php } ?>
514 <?php if ($form_cb_policy) { ?>
515 <td class="dehead">&nbsp;<?php xl('Policy','e')?></td>
516 <?php } ?>
517 <?php if ($form_cb_phone) { ?>
518 <td class="dehead">&nbsp;<?php xl('Phone','e')?></td>
519 <?php } ?>
520 <?php if ($form_cb_city) { ?>
521 <td class="dehead">&nbsp;<?php xl('City','e')?></td>
522 <?php } ?>
523 <?php if ($form_cb_ins1) { ?>
524 <td class="dehead">&nbsp;<?php xl('Primary Ins','e')?></td>
525 <?php } ?>
526 <?php if ($form_cb_referrer) { ?>
527 <td class="dehead">&nbsp;<?php xl('Referrer','e')?></td>
528 <?php } ?>
529 <td class="dehead">&nbsp;<?php xl('Invoice','e') ?></td>
530 <td class="dehead">&nbsp;<?php xl('Svc Date','e') ?></td>
531 <td class="dehead" align="right"><?php xl('Charge','e') ?>&nbsp;</td>
532 <td class="dehead" align="right"><?php xl('Adjust','e') ?>&nbsp;</td>
533 <td class="dehead" align="right"><?php xl('Paid','e') ?>&nbsp;</td>
534 <?php
535 // Generate aging headers if appropriate, else balance header.
536 if ($form_age_cols) {
537 for ($c = 0; $c < $form_age_cols;) {
538 echo " <td class='dehead' align='right'>";
539 echo $form_age_inc * $c;
540 if (++$c < $form_age_cols) {
541 echo "-" . ($form_age_inc * $c - 1);
542 } else {
543 echo "+";
545 echo "</td>\n";
548 else {
550 <td class="dehead" align="right"><?php xl('Balance','e') ?>&nbsp;</td>
551 <?php
554 <?php if ($form_cb_idays) { ?>
555 <td class="dehead" align="right"><?php xl('IDays','e')?>&nbsp;</td>
556 <?php } ?>
557 <td class="dehead" align="center"><?php xl('Prv','e') ?></td>
558 <td class="dehead" align="center"><?php xl('Sel','e') ?></td>
559 <?php if ($form_cb_err) { ?>
560 <td class="dehead">&nbsp;<?php xl('Error','e')?></td>
561 <?php } ?>
562 </tr>
564 <?php
565 } // end not export
567 $ptrow = array('insname' => '', 'pid' => 0);
568 $orow = -1;
570 foreach ($rows as $key => $row) {
571 list($insname, $ptname, $trash) = explode('|', $key);
572 list($pid, $encounter) = explode(".", $row['invnumber']);
574 if ($insname != $ptrow['insname'] || $pid != $ptrow['pid']) {
575 // For the report, this will write the patient totals. For the
576 // collections export this writes everything for the patient:
577 endPatient($ptrow);
578 $bgcolor = ((++$orow & 1) ? "#ffdddd" : "#ddddff");
579 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
580 foreach ($row as $key => $value) $ptrow[$key] = $value;
581 $ptrow['agedbal'] = array();
582 } else {
583 $ptrow['amount'] += $row['amount'];
584 $ptrow['paid'] += $row['paid'];
585 $ptrow['charges'] += $row['charges'];
586 $ptrow['adjustments'] += $row['adjustments'];
587 ++$ptrow['count'];
590 if (!$_POST['form_export'] && !$_POST['form_csvexport']) {
591 $in_collections = stristr($row['billnote'], 'IN COLLECTIONS') !== false;
593 <tr bgcolor='<?php echo $bgcolor ?>'>
594 <?php
595 if ($ptrow['count'] == 1) {
596 if ($is_due_ins) {
597 echo " <td class='detail'>&nbsp;$insname</td>\n";
599 echo " <td class='detail'>&nbsp;$ptname</td>\n";
600 if ($form_cb_ssn) {
601 echo " <td class='detail'>&nbsp;" . $row['ss'] . "</td>\n";
603 if ($form_cb_dob) {
604 echo " <td class='detail'>&nbsp;" . $row['DOB'] . "</td>\n";
606 if ($form_cb_policy) {
607 echo " <td class='detail'>&nbsp;" . $row['policy'] . "</td>\n";
609 if ($form_cb_phone) {
610 echo " <td class='detail'>&nbsp;" . $row['phone'] . "</td>\n";
612 if ($form_cb_city) {
613 echo " <td class='detail'>&nbsp;" . $row['city'] . "</td>\n";
615 if ($form_cb_ins1) {
616 echo " <td class='detail'>&nbsp;" . $row['ins1'] . "</td>\n";
618 if ($form_cb_referrer) {
619 echo " <td class='detail'>&nbsp;" . $row['referrer'] . "</td>\n";
621 } else {
622 echo " <td class='detail' colspan='$initial_colspan'>";
623 echo "&nbsp;</td>\n";
626 <td class="detail">
627 &nbsp;<a href="../billing/sl_eob_invoice.php?id=<?php echo $row['id'] ?>"
628 target="_blank"><?php echo $row['invnumber'] ?></a>
629 </td>
630 <td class="detail">
631 &nbsp;<?php echo $row['dos']; ?>
632 </td>
633 <td class="detail" align="right">
634 <?php bucks($row['charges']) ?>&nbsp;
635 </td>
636 <td class="detail" align="right">
637 <?php bucks($row['adjustments']) ?>&nbsp;
638 </td>
639 <td class="detail" align="right">
640 <?php bucks($row['paid']) ?>&nbsp;
641 </td>
643 <?php
644 $balance = $row['charges'] + $row['adjustments'] - $row['paid'];
645 if ($form_age_cols) {
646 $dostime = mktime(0, 0, 0, substr($row['dos'], 5, 2),
647 substr($row['dos'], 8, 2), substr($row['dos'], 0, 4));
648 $days = floor((time() - $dostime) / (60 * 60 * 24));
649 $colno = min($form_age_cols - 1, max(0, floor($days / $form_age_inc)));
650 $ptrow['agedbal'][$colno] += $balance;
651 for ($c = 0; $c < $form_age_cols; ++$c) {
652 echo " <td class='detail' align='right'>";
653 if ($c == $colno) {
654 bucks($balance);
656 echo "&nbsp;</td>\n";
659 else {
661 <td class="detail" align="right"><?php bucks($balance) ?>&nbsp;</td>
662 <?php
663 } // end else
665 <?php
666 if ($form_cb_idays) {
667 echo " <td class='detail' align='right'>";
668 echo $row['inactive_days'] . "&nbsp;</td>\n";
671 <td class="detail" align="center">
672 <?php echo $row['duncount'] ? $row['duncount'] : "&nbsp;" ?>
673 </td>
674 <td class="detail" align="center">
675 <?php
676 if ($ptrow['count'] == 1) {
677 if ($in_collections) {
678 echo " <b><font color='red'>IC</font></b>\n";
679 } else {
680 echo " <input type='checkbox' name='form_cb[" . $row['custid'] . "]' />\n";
682 } else {
683 echo " &nbsp;\n";
686 </td>
687 <?php
688 if ($form_cb_err) {
689 echo " <td class='detail'>&nbsp;";
690 echo $row['billing_errmsg'] . "</td>\n";
693 </tr>
695 } // end not export
697 else if ($_POST['form_csvexport']) {
698 // The CSV detail line is written here.
699 $balance = $row['charges'] + $row['adjustments'] - $row['paid'];
700 // echo '"' . $insname . '",';
701 echo '"' . $row['ins1'] . '",';
702 echo '"' . $ptname . '",';
703 echo '"' . $row['DOB'] . '",';
704 echo '"' . $row['ss'] . '",';
705 echo '"' . $row['policy'] . '",';
706 echo '"' . $row['invnumber'] . '",';
707 echo '"' . $row['dos'] . '",';
708 echo '"' . $row['referrer'] . '",';
709 echo '"' . sprintf('%.2f', $row['charges']) . '",';
710 echo '"' . sprintf('%.2f', $row['adjustments']) . '",';
711 echo '"' . sprintf('%.2f', $row['paid']) . '",';
712 echo '"' . sprintf('%.2f', $balance) . '",';
713 echo '"' . $row['inactive_days'] . '"' . "\n";
714 } // end $form_csvexport
716 } // end loop
718 endPatient($ptrow);
720 if ($_POST['form_export']) {
721 echo "</textarea>\n";
722 $alertmsg .= "$export_patient_count patients representing $" .
723 sprintf("%.2f", $export_dollars) . " have been exported ";
724 if ($_POST['form_without']) {
725 $alertmsg .= "but NOT flagged as in collections.";
726 } else {
727 $alertmsg .= "AND flagged as in collections.";
730 else if ($_POST['form_csvexport']) {
731 // echo "</textarea>\n";
732 // $alertmsg .= "$export_patient_count patients representing $" .
733 // sprintf("%.2f", $export_dollars) . " have been exported.";
735 else {
736 echo " <tr bgcolor='#ffffff'>\n";
737 echo " <td class='detail' colspan='$initial_colspan'>\n";
738 echo " &nbsp;</td>\n";
739 echo " <td class='dehead' colspan='2'>&nbsp;Report Totals:</td>\n";
740 echo " <td class='dehead' align='right'>&nbsp;" .
741 sprintf("%.2f", $grand_total_charges) . "&nbsp;</td>\n";
742 echo " <td class='dehead' align='right'>&nbsp;" .
743 sprintf("%.2f", $grand_total_adjustments) . "&nbsp;</td>\n";
744 echo " <td class='dehead' align='right'>&nbsp;" .
745 sprintf("%.2f", $grand_total_paid) . "&nbsp;</td>\n";
746 if ($form_age_cols) {
747 for ($c = 0; $c < $form_age_cols; ++$c) {
748 echo " <td class='dehead' align='right'>" .
749 sprintf("%.2f", $grand_total_agedbal[$c]) . "&nbsp;</td>\n";
752 else {
753 echo " <td class='dehead' align='right'>" .
754 sprintf("%.2f", $grand_total_charges +
755 $grand_total_adjustments - $grand_total_paid) . "&nbsp;</td>\n";
757 if ($form_cb_idays) echo " <td class='detail'>&nbsp;</td>\n";
758 echo " <td class='detail' colspan='2'>&nbsp;</td>\n";
759 echo " </tr>\n";
760 if ($form_cb_err) echo " <td class='detail'>&nbsp;</td>\n";
761 echo "</table>\n";
763 } // end if form_search
764 SLClose();
766 if (!$_POST['form_csvexport']) {
767 if (!$_POST['form_export']) {
770 <input type='button' value='Select All' onclick='checkAll(true)' /> &nbsp;
771 <input type='button' value='Clear All' onclick='checkAll(false)' /> &nbsp;
772 <input type='submit' name='form_csvexport' value='Export Selected as CSV' /> &nbsp; &nbsp;
773 <input type='submit' name='form_export' value='Export Selected to Collections' /> &nbsp;
774 <input type='checkbox' name='form_without' value='1' /> <?php xl('Without Update','e') ?>
775 </p>
776 <?php
777 } // end not export
779 </form>
780 </center>
781 <script language="JavaScript">
782 <?php
783 if ($alertmsg) {
784 echo "alert('" . htmlentities($alertmsg) . "');\n";
787 </script>
788 </body>
789 </html>
790 <?php
791 } // end not form_csvexport