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>
10 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
12 * LICENSE: This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 3
15 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
24 * @author Rod Roark <rod@sunsetsystems.com>
25 * @author Terry Hill <terry@lilysystems.com>
26 * @author Brady Miller <brady.g.miller@gmail.com>
27 * @link http://www.open-emr.org
32 use OpenEMR\Core\Header
;
34 require_once("../globals.php");
35 require_once("../../library/patient.inc");
36 require_once("../../library/invoice_summary.inc.php");
37 require_once("../../library/sl_eob.inc.php");
38 require_once "$srcdir/options.inc.php";
43 $export_patient_count = 0;
46 $today = date("Y-m-d");
48 $form_date = fixDate($_POST['form_date'], "");
49 $form_to_date = fixDate($_POST['form_to_date'], "");
50 $is_ins_summary = $_POST['form_category'] == 'Ins Summary';
51 $is_due_ins = ($_POST['form_category'] == 'Due Ins') ||
$is_ins_summary;
52 $is_due_pt = $_POST['form_category'] == 'Due Pt';
53 $is_all = $_POST['form_category'] == 'All';
54 $is_ageby_lad = strpos($_POST['form_ageby'], 'Last') !== false;
55 $form_facility = $_POST['form_facility'];
56 $form_provider = $_POST['form_provider'];
57 $form_payer_id = $_POST['form_payer_id'];
59 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
60 if ($is_ins_summary) {
63 $form_cb_pubpid = false;
64 $form_cb_adate = false;
65 $form_cb_policy = false;
66 $form_cb_phone = false;
67 $form_cb_city = false;
68 $form_cb_ins1 = false;
69 $form_cb_referrer = false;
70 $form_cb_idays = false;
73 $form_cb_ssn = $_POST['form_cb_ssn'] ?
true : false;
74 $form_cb_dob = $_POST['form_cb_dob'] ?
true : false;
75 $form_cb_pubpid = $_POST['form_cb_pubpid'] ?
true : false;
76 $form_cb_adate = $_POST['form_cb_adate'] ?
true : false;
77 $form_cb_policy = $_POST['form_cb_policy'] ?
true : false;
78 $form_cb_phone = $_POST['form_cb_phone'] ?
true : false;
79 $form_cb_city = $_POST['form_cb_city'] ?
true : false;
80 $form_cb_ins1 = $_POST['form_cb_ins1'] ?
true : false;
81 $form_cb_referrer = $_POST['form_cb_referrer'] ?
true : false;
82 $form_cb_idays = $_POST['form_cb_idays'] ?
true : false;
83 $form_cb_err = $_POST['form_cb_err'] ?
true : false;
88 $form_cb_pubpid = false;
89 $form_cb_adate = false;
90 $form_cb_policy = false;
91 $form_cb_phone = true;
92 $form_cb_city = false;
93 $form_cb_ins1 = false;
94 $form_cb_referrer = false;
95 $form_cb_idays = false;
99 $form_age_cols = (int) $_POST['form_age_cols'];
100 $form_age_inc = (int) $_POST['form_age_inc'];
101 if ($form_age_cols > 0 && $form_age_cols < 50) {
102 if ($form_age_inc <= 0) {
110 $initial_colspan = 1;
123 if ($form_cb_pubpid) {
127 if ($form_cb_policy) {
131 if ($form_cb_phone) {
143 if ($form_cb_referrer) {
147 if ($form_provider) {
151 if ($form_payer_id) {
155 $final_colspan = $form_cb_adate ?
6 : 5;
156 $form_cb_with_debt = $_POST['form_cb_with_debt'] ?
true : false;
157 $grand_total_charges = 0;
158 $grand_total_adjustments = 0;
159 $grand_total_paid = 0;
160 $grand_total_agedbal = array();
161 for ($c = 0; $c < $form_age_cols;
163 $grand_total_agedbal[$c] = 0;
167 function bucks($amount)
170 echo oeFormatMoney($amount); // was printf("%.2f", $amount);
174 function endPatient($ptrow)
176 global $export_patient_count, $export_dollars, $bgcolor;
177 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
178 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
179 global $initial_colspan, $final_colspan, $form_cb_idays, $form_cb_err;
181 if (!$ptrow['pid']) {
185 $pt_balance = $ptrow['amount'] - $ptrow['paid'];
187 if ($_POST['form_export']) {
188 // This is a fixed-length format used by Transworld Systems. Your
189 // needs will surely be different, so consider this just an example.
191 echo "1896H"; // client number goes here
192 echo "000"; // filler
193 echo sprintf("%-30s", substr($ptrow['ptname'], 0, 30));
194 echo sprintf("%-30s", " ");
195 echo sprintf("%-30s", substr($ptrow['address1'], 0, 30));
196 echo sprintf("%-15s", substr($ptrow['city'], 0, 15));
197 echo sprintf("%-2s", substr($ptrow['state'], 0, 2));
198 echo sprintf("%-5s", $ptrow['zipcode'] ?
substr($ptrow['zipcode'], 0, 5) : '00000');
199 echo "1"; // service code
200 echo sprintf("%010.0f", $ptrow['pid']); // transmittal number = patient id
202 echo sprintf("%-15s", substr($ptrow['ss'], 0, 15));
203 echo substr($ptrow['dos'], 5, 2) . substr($ptrow['dos'], 8, 2) . substr($ptrow['dos'], 2, 2);
204 echo sprintf("%08.0f", $pt_balance * 100);
205 echo sprintf("%-9s\n", " ");
207 if (!$_POST['form_without']) {
208 sqlStatement("UPDATE patient_data SET " .
209 "billing_note = CONCAT('IN COLLECTIONS " . date("Y-m-d") . "', billing_note) " .
210 "WHERE pid = ? ", array($ptrow['pid']));
213 $export_patient_count +
= 1;
214 $export_dollars +
= $pt_balance;
215 } else if ($_POST['form_csvexport']) {
216 $export_patient_count +
= 1;
217 $export_dollars +
= $pt_balance;
219 if ($ptrow['count'] > 1) {
220 echo " <tr bgcolor='$bgcolor'>\n";
221 /***************************************************************
222 echo " <td class='detail' colspan='$initial_colspan'>";
223 echo " </td>\n";
224 echo " <td class='detotal' colspan='$final_colspan'> Total Patient Balance:</td>\n";
225 ***************************************************************/
226 echo " <td class='detotal' colspan='" . ($initial_colspan +
$final_colspan) .
227 "'> " . xlt('Total Patient Balance') . ":</td>\n";
228 /**************************************************************/
229 if ($form_age_cols) {
230 for ($c = 0; $c < $form_age_cols; ++
$c) {
231 echo " <td class='detotal' align='right'> " .
232 oeFormatMoney($ptrow['agedbal'][$c]) . " </td>\n";
235 echo " <td class='detotal' align='right'> " .
236 oeFormatMoney($pt_balance) . " </td>\n";
239 if ($form_cb_idays) {
240 echo " <td class='detail'> </td>\n";
243 echo " <td class='detail' colspan='2'> </td>\n";
245 echo " <td class='detail'> </td>\n";
252 $grand_total_charges +
= $ptrow['charges'];
253 $grand_total_adjustments +
= $ptrow['adjustments'];
254 $grand_total_paid +
= $ptrow['paid'];
255 for ($c = 0; $c < $form_age_cols; ++
$c) {
256 $grand_total_agedbal[$c] +
= $ptrow['agedbal'][$c];
260 function endInsurance($insrow)
262 global $export_patient_count, $export_dollars, $bgcolor;
263 global $grand_total_charges, $grand_total_adjustments, $grand_total_paid;
264 global $grand_total_agedbal, $is_due_ins, $form_age_cols;
265 global $initial_colspan, $form_cb_idays, $form_cb_err;
266 if (!$insrow['pid']) {
270 $ins_balance = $insrow['amount'] - $insrow['paid'];
271 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
272 // No exporting of insurance summaries.
273 $export_patient_count +
= 1;
274 $export_dollars +
= $ins_balance;
276 echo " <tr bgcolor='$bgcolor'>\n";
277 echo " <td class='detail'>" . text($insrow['insname']) . "</td>\n";
278 echo " <td class='detotal' align='right'> " .
279 oeFormatMoney($insrow['charges']) . " </td>\n";
280 echo " <td class='detotal' align='right'> " .
281 oeFormatMoney($insrow['adjustments']) . " </td>\n";
282 echo " <td class='detotal' align='right'> " .
283 oeFormatMoney($insrow['paid']) . " </td>\n";
284 if ($form_age_cols) {
285 for ($c = 0; $c < $form_age_cols; ++
$c) {
286 echo " <td class='detotal' align='right'> " .
287 oeFormatMoney($insrow['agedbal'][$c]) . " </td>\n";
290 echo " <td class='detotal' align='right'> " .
291 oeFormatMoney($ins_balance) . " </td>\n";
297 $grand_total_charges +
= $insrow['charges'];
298 $grand_total_adjustments +
= $insrow['adjustments'];
299 $grand_total_paid +
= $insrow['paid'];
300 for ($c = 0; $c < $form_age_cols; ++
$c) {
301 $grand_total_agedbal[$c] +
= $insrow['agedbal'][$c];
305 function getInsName($payerid)
307 $tmp = sqlQuery("SELECT name FROM insurance_companies WHERE id = ? ", array($payerid));
311 // In the case of CSV export only, a download will be forced.
312 if ($_POST['form_csvexport']) {
313 header("Pragma: public");
314 header("Expires: 0");
315 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
316 header("Content-Type: application/force-download");
317 header("Content-Disposition: attachment; filename=collections_report.csv");
318 header("Content-Description: File Transfer");
324 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
326 <title
><?php
echo xlt('Collections Report')?
></title
>
327 <style type
="text/css">
334 #report_parameters_daterange {
343 /* specifically exclude some from the screen */
345 #report_parameters_daterange {
353 <script language
="JavaScript">
355 $
(document
).ready(function() {
356 oeFixedHeaderSetup(document
.getElementById('mymaintable'));
357 var win
= top
.printLogSetup ? top
: opener
.top
;
358 win
.printLogSetup(document
.getElementById('printbutton'));
360 $
('.datepicker').datetimepicker({
361 <?php
$datetimepicker_timepicker = false; ?
>
362 <?php
$datetimepicker_showseconds = false; ?
>
363 <?php
$datetimepicker_formatInput = false; ?
>
364 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
365 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
369 function checkAll(checked
) {
370 var f
= document
.forms
[0];
371 for (var i
= 0; i
< f
.elements
.length
; ++i
) {
372 var ename
= f
.elements
[i
].name
;
373 if (ename
.indexOf('form_cb[') == 0)
374 f
.elements
[i
].checked
= checked
;
382 <body
class="body_top">
384 <span
class='title'><?php
echo xlt('Report'); ?
> - <?php
echo xlt('Collections'); ?
></span
>
386 <form method
='post' action
='collections_report.php' enctype
='multipart/form-data' id
='theform'>
388 <div id
="report_parameters">
390 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
=''/>
391 <input type
='hidden' name
='form_export' id
='form_export' value
=''/>
392 <input type
='hidden' name
='form_csvexport' id
='form_csvexport' value
=''/>
397 <div style
='float:left'>
401 <td
class='control-label'>
404 <td
><?php
echo xlt('Displayed Columns') ?
>:</td
>
408 <label
><input type
='checkbox' name
='form_cb_ssn'<?php
if ($form_cb_ssn) {
411 <?php
echo xlt('SSN') ?
> 
;</label
>
414 <label
><input type
='checkbox' name
='form_cb_dob'<?php
if ($form_cb_dob) {
417 <?php
echo xlt('DOB') ?
> 
;</label
>
420 <label
><input type
='checkbox' name
='form_cb_pubpid'<?php
if ($form_cb_pubpid) {
423 <?php
echo xlt('ID') ?
> 
;</label
>
426 <label
><input type
='checkbox' name
='form_cb_policy'<?php
if ($form_cb_policy) {
429 <?php
echo xlt('Policy') ?
> 
;</label
>
432 <label
><input type
='checkbox' name
='form_cb_phone'<?php
if ($form_cb_phone) {
435 <?php
echo xlt('Phone') ?
> 
;</label
>
438 <label
><input type
='checkbox' name
='form_cb_city'<?php
if ($form_cb_city) {
441 <?php
echo xlt('City') ?
> 
;</label
>
446 <label
><input type
='checkbox' name
='form_cb_ins1'<?php
if ($form_cb_ins1) {
449 <?php
echo xlt('Primary Ins') ?
> 
;</label
>
452 <label
><input type
='checkbox' name
='form_cb_referrer'<?php
if ($form_cb_referrer) {
455 <?php
echo xlt('Referrer') ?
> 
;</label
>
458 <label
><input type
='checkbox' name
='form_cb_adate'<?php
if ($form_cb_adate) {
461 <?php
echo xlt('Act Date') ?
> 
;</label
>
464 <label
><input type
='checkbox' name
='form_cb_idays'<?php
if ($form_cb_idays) {
467 <?php
echo xlt('Inactive Days') ?
> 
;</label
>
470 <label
><input type
='checkbox' name
='form_cb_err'<?php
if ($form_cb_err) {
473 <?php
echo xlt('Errors') ?
></label
>
484 <td
class='control-label'>
485 <?php
echo xlt('Service Date'); ?
>:
488 <input type
='text' class='datepicker form-control' name
='form_date' id
="form_date" size
='10' value
='<?php echo attr($form_date) ?>'
491 <td
class='control-label'>
492 <?php
echo xlt('To'); ?
>:
495 <input type
='text' class='datepicker form-control' name
='form_to_date' id
="form_to_date" size
='10' value
='<?php echo attr($form_to_date) ?>'
499 <select name
='form_category' class='form-control'>
501 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) {
502 echo " <option value='" . attr($key) . "'";
503 if ($_POST['form_category'] == $key) {
507 echo ">" . text($value) . "</option>\n";
517 <td
class='control-label'>
518 <?php
echo xlt('Facility'); ?
>:
521 <?php
dropdown_facility($form_facility, 'form_facility', false); ?
>
524 <td
class='control-label'>
525 <?php
echo xlt('Payor'); ?
>:
528 <?php
# added dropdown for payors (TLH)
529 $insurancei = getInsuranceProviders();
530 echo " <select name='form_payer_id' class='form-control'>\n";
531 echo " <option value='0'>-- " . xlt('All') . " --</option>\n";
532 foreach ($insurancei as $iid => $iname) {
533 echo "<option value='" . attr($iid) . "'";
534 if ($iid == $_POST['form_payer_id']) {
538 echo ">" . text($iname) . "</option>\n";
539 if ($iid == $_POST['form_payer_id']) {
540 $ins_co_name = $iname;
550 <td
class='control-label'>
551 <?php
echo xlt('Age By') ?
>:
554 <select name
='form_ageby' class='form-control'>
556 foreach (array( 'Service Date'=>xl('Service Date'), 'Last Activity Date'=>xl('Last Activity Date')) as $key => $value) {
557 echo " <option value='" . attr($key) . "'";
558 if ($_POST['form_ageby'] == $value) {
562 echo ">" . text($value) . "</option>\n";
568 <td
class='control-label'>
569 <?php
echo xlt('Provider') ?
>:
572 <?php
# Build a drop-down list of providers.
575 $query = "SELECT id, lname, fname FROM users WHERE ".
576 "authorized = 1 ORDER BY lname, fname"; #(CHEMED) facility filter
578 $ures = sqlStatement($query);
580 echo " <select name='form_provider' class='form-control'>\n";
581 echo " <option value=''>-- " . xlt('All') . " --\n";
583 while ($urow = sqlFetchArray($ures)) {
584 $provid = $urow['id'];
585 echo " <option value='" . attr($provid) . "'";
586 if ($provid == $_POST['form_provider']) {
590 echo ">" . text($urow['lname']) . ", " . text($urow['fname']) . "\n";
591 if ($provid == $_POST['form_provider']) {
592 $provider_name = $urow['lname'] . ", " . $urow['fname'];
601 <td
class='control-label'>
602 <?php
echo xlt('Aging Columns') ?
>:
605 <input type
='text' name
='form_age_cols' class='form-control' size
='2' value
='<?php echo attr($form_age_cols); ?>' />
607 <td
class='control-label'>
608 <?php
echo xlt('Days/Col') ?
>:
611 <input type
='text' name
='form_age_inc' class='form-control' size
='3' value
='<?php echo attr($form_age_inc); ?>' />
614 <div
class="checkbox">
615 <label
><input type
='checkbox' name
='form_cb_with_debt'<?php
if ($form_cb_with_debt) {
618 <?php
echo xlt('Patients with debt') ?
></label
>
632 <td align
='left' valign
='middle' height
="100%">
633 <table style
='border-left:1px solid; width:100%; height:100%' >
636 <div
class="text-center">
637 <div
class="btn-group" role
="group">
638 <a href
='#' class='btn btn-default btn-save' onclick
='$("#form_refresh").attr("value","true"); $("#form_csvexport").val(""); $("#theform").submit();'>
639 <?php
echo xlt('Submit'); ?
>
641 <?php
if ($_POST['form_refresh']) { ?
>
642 <a href
='#' class='btn btn-default btn-print' onclick
='window.print()'>
643 <?php
echo xlt('Print'); ?
>
658 } // end not form_csvexport
660 if ($_POST['form_refresh'] ||
$_POST['form_export'] ||
$_POST['form_csvexport']) {
664 if ($_POST['form_export'] ||
$_POST['form_csvexport']) {
666 foreach ($_POST['form_cb'] as $key => $value) {
667 list($key_newval['pid'], $key_newval['encounter']) = explode(".", $key);
668 $newkey = $key_newval['pid'];
669 $newencounter = $key_newval['encounter'];
670 # added this condition to handle the downloading of individual invoices (TLH)
671 if ($_POST['form_individual'] ==1) {
672 $where .= " OR f.encounter = ? ";
673 array_push($sqlArray, $newencounter);
675 $where .= " OR f.pid = ? ";
676 array_push($sqlArray, $newkey);
689 $where .= "f.date >= ? AND f.date <= ? ";
690 array_push($sqlArray, $form_date.' 00:00:00', $form_to_date.' 23:59:59');
692 $where .= "f.date >= ? AND f.date <= ? ";
693 array_push($sqlArray, $form_date.' 00:00:00', $form_date.' 23:59:59');
697 if ($form_facility) {
702 $where .= "f.facility_id = ? ";
703 array_push($sqlArray, $form_facility);
706 # added for filtering by provider (TLH)
707 if ($form_provider) {
712 $where .= "f.provider_id = ? ";
713 array_push($sqlArray, $form_provider);
720 # added provider from encounter to the query (TLH)
721 $query = "SELECT f.id, f.date, f.pid, CONCAT(w.lname, ', ', w.fname) AS provider_id, f.encounter, f.last_level_billed, " .
722 "f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, " .
723 "p.fname, p.mname, p.lname, p.street, p.city, p.state, " .
724 "p.postal_code, p.phone_home, p.ss, p.billing_note, " .
725 "p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, " .
726 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
727 "b.pid = f.pid AND b.encounter = f.encounter AND " .
728 "b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, " .
729 "( SELECT SUM(b.fee) FROM billing AS b WHERE " .
730 "b.pid = f.pid AND b.encounter = f.encounter AND " .
731 "b.activity = 1 AND b.code_type = 'COPAY' ) AS copays, " .
732 "( SELECT SUM(s.fee) FROM drug_sales AS s WHERE " .
733 "s.pid = f.pid AND s.encounter = f.encounter ) AS sales, " .
734 "( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE " .
735 "a.pid = f.pid AND a.encounter = f.encounter ) AS payments, " .
736 "( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE " .
737 "a.pid = f.pid AND a.encounter = f.encounter ) AS adjustments " .
738 "FROM form_encounter AS f " .
739 "JOIN patient_data AS p ON p.pid = f.pid " .
740 "LEFT OUTER JOIN users AS u ON u.id = p.ref_providerID " .
741 "LEFT OUTER JOIN users AS w ON w.id = f.provider_id " .
743 "ORDER BY f.pid, f.encounter";
745 $eres = sqlStatement($query, $sqlArray);
747 while ($erow = sqlFetchArray($eres)) {
748 $patient_id = $erow['pid'];
749 $encounter_id = $erow['encounter'];
750 $pt_balance = $erow['charges'] +
$erow['sales'] +
$erow['copays'] - $erow['payments'] - $erow['adjustments'];
751 $pt_balance = 0 +
sprintf("%.2f", $pt_balance); // yes this seems to be necessary
752 $svcdate = substr($erow['date'], 0, 10);
754 if ($form_cb_with_debt && $pt_balance<=0) {
759 if ($_POST['form_refresh'] && ! $is_all) {
760 if ($pt_balance == 0) {
765 if ($_POST['form_category'] == 'Credits') {
766 if ($pt_balance > 0) {
771 // If we have not yet billed the patient, then compute $duncount as a
772 // negative count of the number of insurance plans for which we have not
773 // yet closed out insurance. Here we also compute $insname as the name of
774 // the insurance plan from which we are awaiting payment, and its sequence
775 // number $insposition (1-3).
776 $last_level_closed = $erow['last_level_closed'];
777 $duncount = $erow['stmt_count'];
782 for ($i = 1; $i <= 3; ++
$i) {
783 $tmp = arGetPayerID($patient_id, $svcdate, $i);
791 $duncount = $last_level_closed - count($payerids);
793 if (!empty($payerids[$last_level_closed])) {
794 $insname = getInsName($payerids[$last_level_closed]);
795 $insposition = $last_level_closed +
1;
800 // Skip invoices not in the desired "Due..." category.
802 if ($is_due_ins && $duncount >= 0) {
806 if ($is_due_pt && $duncount < 0) {
810 // echo "<!-- " . $erow['encounter'] . ': ' . $erow['charges'] . ' + ' . $erow['sales'] . ' + ' . $erow['copays'] . ' - ' . $erow['payments'] . ' - ' . $erow['adjustments'] . " -->\n"; // debugging
812 // An invoice is due from the patient if money is owed and we are
813 // not waiting for insurance to pay.
814 $isduept = ($duncount >= 0) ?
" checked" : "";
818 $row['id'] = $erow['id'];
819 $row['invnumber'] = "$patient_id.$encounter_id";
820 $row['custid'] = $patient_id;
821 $row['name'] = $erow['fname'] . ' ' . $erow['lname'];
822 $row['address1'] = $erow['street'];
823 $row['city'] = $erow['city'];
824 $row['state'] = $erow['state'];
825 $row['zipcode'] = $erow['postal_code'];
826 $row['phone'] = $erow['phone_home'];
827 $row['duncount'] = $duncount;
828 $row['dos'] = $svcdate;
829 $row['ss'] = $erow['ss'];
830 $row['DOB'] = $erow['DOB'];
831 $row['pubpid'] = $erow['pubpid'];
832 $row['billnote'] = $erow['billing_note'];
833 $row['referrer'] = $erow['referrer'];
834 $row['provider'] = $erow['provider_id'];
835 $row['irnumber'] = $erow['invoice_refno'];
837 // Also get the primary insurance company name whenever there is one.
839 if ($insposition == 1) {
840 $row['ins1'] = $insname;
842 if (empty($payerids)) {
843 $tmp = arGetPayerID($patient_id, $svcdate, 1);
849 if (!empty($payerids)) {
850 $row['ins1'] = getInsName($payerids[0]);
854 // This computes the invoice's total original charges and adjustments,
855 // date of last activity, and determines if insurance has responded to
856 // all billing items.
857 $invlines = ar_get_invoice_summary($patient_id, $encounter_id, true);
859 // if ($encounter_id == 185) { // debugging
861 // print_r($invlines);
866 $row['adjustments'] = 0;
868 $ins_seems_done = true;
870 foreach ($invlines as $key => $value) {
871 $row['charges'] +
= $value['chg'] +
$value['adj'];
872 $row['adjustments'] +
= 0 - $value['adj'];
873 $row['paid'] +
= $value['chg'] - $value['bal'];
874 foreach ($value['dtl'] as $dkey => $dvalue) {
875 $dtldate = trim(substr($dkey, 0, 10));
876 if ($dtldate && $dtldate > $ladate) {
881 $lckey = strtolower($key);
882 if ($lckey == 'co-pay' ||
$lckey == 'claim') {
886 if (count($value['dtl']) <= 1) {
887 $ins_seems_done = false;
891 // Simulating ar.amount in SQL-Ledger which is charges with adjustments:
892 $row['amount'] = $row['charges'] +
$row['adjustments'];
894 $row['billing_errmsg'] = '';
895 if ($is_due_ins && $last_level_closed < 1 && $ins_seems_done) {
896 $row['billing_errmsg'] = 'Ins1 seems done';
897 } else if ($last_level_closed >= 1 && !$ins_seems_done) {
898 $row['billing_errmsg'] = 'Ins1 seems not done';
901 $row['ladate'] = $ladate;
903 // Compute number of days since last activity.
908 substr($ladate, 5, 2),
909 substr($ladate, 8, 2),
910 substr($ladate, 0, 4)
912 $row['inactive_days'] = floor((time() - $latime) / (60 * 60 * 24));
914 // Look up insurance policy number if we need it.
915 if ($form_cb_policy) {
916 $instype = ($insposition == 2) ?
'secondary' : (($insposition == 3) ?
'tertiary' : 'primary');
917 $insrow = sqlQuery("SELECT policy_number FROM insurance_data WHERE " .
918 "pid = ? AND type = ? AND date <= ? " .
919 "ORDER BY date DESC LIMIT 1", array($patient_id, $instype, $svcdate));
920 $row['policy'] = $insrow['policy_number'];
923 $ptname = $erow['lname'] . ", " . $erow['fname'];
924 if ($erow['mname']) {
925 $ptname .= " " . substr($erow['mname'], 0, 1);
932 $rows[$insname . '|' . $ptname . '|' . $encounter_id] = $row;
938 if ($_POST['form_export']) {
939 echo "<textarea rows='35' cols='100' readonly>";
940 } else if ($_POST['form_csvexport']) {
941 # CSV headers added conditions if they are checked to display then export them (TLH)
943 echo '"' . xl('Insurance') . '",';
944 echo '"' . xl('Name') . '",';
946 echo '"' . xl('SSN') . '",';
950 echo '"' . xl('DOB') . '",';
953 if ($form_cb_pubid) {
954 echo '"' . xl('Pubid') . '",';
957 if ($form_cb_policy) {
958 echo '"' . xl('Policy') . '",';
961 if ($form_cb_phone) {
962 echo '"' . xl('Phone') . '",';
966 echo '"' . xl('City') . '",';
969 echo '"' . xl('Invoice') . '",';
970 echo '"' . xl('DOS') . '",';
971 echo '"' . xl('Referrer') . '",';
972 echo '"' . xl('Provider') . '",';
973 echo '"' . xl('Charge') . '",';
974 echo '"' . xl('Adjust') . '",';
975 echo '"' . xl('Paid') . '",';
976 echo '"' . xl('Balance') . '",';
977 echo '"' . xl('IDays') . '",';
979 echo '"' . xl('LADate') . '",';
980 echo '"' . xl('Error') . '"' . "\n";
982 echo '"' . xl('LADate') . '"' . "\n";
988 <div id
="report_results">
989 <table id
='mymaintable'>
992 <?php
if ($is_due_ins) { ?
>
993 <th
> 
;<?php
echo xlt('Insurance')?
></th
>
995 <?php
if (!$is_ins_summary) { ?
>
996 <th
> 
;<?php
echo xlt('Name')?
></th
>
998 <?php
if ($form_cb_ssn) { ?
>
999 <th
> 
;<?php
echo xlt('SSN')?
></th
>
1001 <?php
if ($form_cb_dob) { ?
>
1002 <th
> 
;<?php
echo xlt('DOB')?
></th
>
1004 <?php
if ($form_cb_pubpid) { ?
>
1005 <th
> 
;<?php
echo xlt('ID')?
></th
>
1007 <?php
if ($form_cb_policy) { ?
>
1008 <th
> 
;<?php
echo xlt('Policy')?
></th
>
1010 <?php
if ($form_cb_phone) { ?
>
1011 <th
> 
;<?php
echo xlt('Phone')?
></th
>
1013 <?php
if ($form_cb_city) { ?
>
1014 <th
> 
;<?php
echo xlt('City')?
></th
>
1016 <?php
if ($form_cb_ins1 ||
$form_payer_id) { ?
>
1017 <th
> 
;<?php
echo xlt('Primary Ins')?
></th
>
1019 <?php
if ($form_provider) { ?
>
1020 <th
> 
;<?php
echo xlt('Provider')?
></th
>
1022 <?php
if ($form_cb_referrer) { ?
>
1023 <th
> 
;<?php
echo xlt('Referrer')?
></th
>
1025 <?php
if (!$is_ins_summary) { ?
>
1026 <th
> 
;<?php
echo xlt('Invoice') ?
></th
>
1027 <th
> 
;<?php
echo xlt('Svc Date') ?
></th
>
1028 <?php
if ($form_cb_adate) { ?
>
1029 <th
> 
;<?php
echo xlt('Act Date')?
></th
>
1032 <th align
="right"><?php
echo xlt('Charge') ?
> 
;</th
>
1033 <th align
="right"><?php
echo xlt('Adjust') ?
> 
;</th
>
1034 <th align
="right"><?php
echo xlt('Paid') ?
> 
;</th
>
1036 // Generate aging headers if appropriate, else balance header.
1037 if ($form_age_cols) {
1038 for ($c = 0; $c < $form_age_cols;) {
1039 echo " <th class='dehead' align='right'>";
1040 echo $form_age_inc * $c;
1041 if (++
$c < $form_age_cols) {
1042 echo "-" . ($form_age_inc * $c - 1);
1051 <th align
="right"><?php
echo xlt('Balance') ?
> 
;</th
>
1055 <?php
if ($form_cb_idays) { ?
>
1056 <th align
="right"><?php
echo xlt('IDays')?
> 
;</th
>
1058 <?php
if (!$is_ins_summary) { ?
>
1059 <th align
="center"><?php
echo xlt('Prv') ?
></th
>
1060 <th align
="center"><?php
echo xlt('Sel') ?
></th
>
1062 <?php
if ($form_cb_err) { ?
>
1063 <th
> 
;<?php
echo xlt('Error')?
></th
>
1070 $ptrow = array('insname' => '', 'pid' => 0);
1073 foreach ($rows as $key => $row) {
1074 list($insname, $ptname, $trash) = explode('|', $key);
1075 list($pid, $encounter) = explode(".", $row['invnumber']);
1076 if ($form_payer_id) {
1077 if ($ins_co_name <> $row['ins1']) {
1082 if ($is_ins_summary && $insname != $ptrow['insname']) {
1083 endInsurance($ptrow);
1084 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
1085 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1086 foreach ($row as $key => $value) {
1087 $ptrow[$key] = $value;
1090 $ptrow['agedbal'] = array();
1091 } else if (!$is_ins_summary && ($insname != $ptrow['insname'] ||
$pid != $ptrow['pid'])) {
1092 // For the report, this will write the patient totals. For the
1093 // collections export this writes everything for the patient:
1095 $bgcolor = ((++
$orow & 1) ?
"#ffdddd" : "#ddddff");
1096 $ptrow = array('insname' => $insname, 'ptname' => $ptname, 'pid' => $pid, 'count' => 1);
1097 foreach ($row as $key => $value) {
1098 $ptrow[$key] = $value;
1101 $ptrow['agedbal'] = array();
1103 $ptrow['amount'] +
= $row['amount'];
1104 $ptrow['paid'] +
= $row['paid'];
1105 $ptrow['charges'] +
= $row['charges'];
1106 $ptrow['adjustments'] +
= $row['adjustments'];
1110 // Compute invoice balance and aging column number, and accumulate aging.
1111 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
1112 if ($form_age_cols) {
1113 $agedate = $is_ageby_lad ?
$row['ladate'] : $row['dos'];
1118 substr($agedate, 5, 2),
1119 substr($agedate, 8, 2),
1120 substr($agedate, 0, 4)
1122 $days = floor((time() - $agetime) / (60 * 60 * 24));
1123 $agecolno = min($form_age_cols - 1, max(0, floor($days / $form_age_inc)));
1124 $ptrow['agedbal'][$agecolno] +
= $balance;
1127 if (!$is_ins_summary && !$_POST['form_export'] && !$_POST['form_csvexport']) {
1128 $in_collections = stristr($row['billnote'], 'IN COLLECTIONS') !== false;
1130 <tr bgcolor
='<?php echo attr($bgcolor) ?>'>
1132 if ($ptrow['count'] == 1) {
1134 echo " <td class='detail'> " . attr($insname) ."</td>\n";
1137 echo " <td class='detail'> " . attr($ptname) ."</td>\n";
1139 echo " <td class='detail'> " . attr($row['ss']) . "</td>\n";
1143 echo " <td class='detail'> " . attr(oeFormatShortDate($row['DOB'])) . "</td>\n";
1146 if ($form_cb_pubpid) {
1147 echo " <td class='detail'> " . attr($row['pubpid']) . "</td>\n";
1150 if ($form_cb_policy) {
1151 echo " <td class='detail'> " . attr($row['policy']) . "</td>\n";
1154 if ($form_cb_phone) {
1155 echo " <td class='detail'> " . attr($row['phone']) . "</td>\n";
1158 if ($form_cb_city) {
1159 echo " <td class='detail'> " . attr($row['city']) . "</td>\n";
1162 if ($form_cb_ins1 ||
$form_payer_id) {
1163 echo " <td class='detail'> " . attr($row['ins1']) . "</td>\n";
1166 if ($form_provider) {
1167 echo " <td class='detail'> " . attr($provider_name) . "</td>\n";
1170 if ($form_cb_referrer) {
1171 echo " <td class='detail'> " . attr($row['referrer']) . "</td>\n";
1174 echo " <td class='detail' colspan='$initial_colspan'>";
1175 echo " </td>\n";
1179  
;<a href
="../billing/sl_eob_invoice.php?id=<?php echo attr($row['id']) ?>"
1180 target
="_blank"><?php
echo empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']; ?
></a
>
1183  
;<?php
echo attr(oeFormatShortDate($row['dos'])); ?
>
1185 <?php
if ($form_cb_adate) { ?
>
1187  
;<?php
echo attr(oeFormatShortDate($row['ladate'])); ?
>
1190 <td
class="detail" align
="right">
1191 <?php
attr(bucks($row['charges'])) ?
> 
;
1193 <td
class="detail" align
="right">
1194 <?php
attr(bucks($row['adjustments'])) ?
> 
;
1196 <td
class="detail" align
="right">
1197 <?php
attr(bucks($row['paid'])) ?
> 
;
1200 if ($form_age_cols) {
1201 for ($c = 0; $c < $form_age_cols; ++
$c) {
1202 echo " <td class='detail' align='right'>";
1203 if ($c == $agecolno) {
1207 echo " </td>\n";
1211 <td
class="detail" align
="right"><?php
bucks($balance) ?
> 
;</td
>
1216 if ($form_cb_idays) {
1217 echo " <td class='detail' align='right'>";
1218 echo attr($row['inactive_days']) . " </td>\n";
1221 <td
class="detail" align
="center">
1222 <?php
echo $row['duncount'] ?
$row['duncount'] : " " ?
>
1224 <td
class="detail" align
="center">
1226 if ($in_collections) {
1227 echo " <b><font color='red'>IC</font></b>\n";
1229 echo " <input type='checkbox' name='form_cb[" . attr($row['invnumber']) . "]' />\n";
1235 echo " <td class='detail'> ";
1236 echo text($row['billing_errmsg']) . "</td>\n";
1241 } // end not export and not insurance summary
1243 else if ($_POST['form_csvexport']) {
1244 # The CSV detail line is written here added conditions for checked items (TLH).
1245 $balance = $row['charges'] +
$row['adjustments'] - $row['paid'];
1247 // echo '"' . $insname . '",';
1248 echo '"' . $row['ins1'] . '",';
1249 echo '"' . $ptname . '",';
1251 echo '"' . $row['ss'] . '",';
1255 echo '"' . oeFormatShortDate($row['DOB']) . '",';
1258 if ($form_cb_pubid) {
1259 echo '"' . $row['pubpid'] . '",';
1262 if ($form_cb_policy) {
1263 echo '"' . $row['policy'] . '",';
1266 if ($form_cb_phone) {
1267 echo '"' . $row['phone'] . '",';
1270 if ($form_cb_city) {
1271 echo '"' . $row['city'] . '",';
1274 echo '"' . (empty($row['irnumber']) ?
$row['invnumber'] : $row['irnumber']) . '",';
1275 echo '"' . oeFormatShortDate($row['dos']) . '",';
1276 echo '"' . $row['referrer'] . '",';
1277 echo '"' . $row['provider'] . '",';
1278 echo '"' . oeFormatMoney($row['charges']) . '",';
1279 echo '"' . oeFormatMoney($row['adjustments']) . '",';
1280 echo '"' . oeFormatMoney($row['paid']) . '",';
1281 echo '"' . oeFormatMoney($balance) . '",';
1282 echo '"' . $row['inactive_days'] . '",';
1284 echo '"' . oeFormatShortDate($row['ladate']) . '",';
1285 echo '"' . $row['billing_errmsg'] . '"' . "\n";
1287 echo '"' . oeFormatShortDate($row['ladate']) . '"' . "\n";
1290 } // end $form_csvexport
1293 if ($is_ins_summary) {
1294 endInsurance($ptrow);
1299 if ($_POST['form_export']) {
1300 echo "</textarea>\n";
1301 $alertmsg .= "$export_patient_count patients with total of " .
1302 oeFormatMoney($export_dollars) . " have been exported ";
1303 if ($_POST['form_without']) {
1304 $alertmsg .= "but NOT flagged as in collections.";
1306 $alertmsg .= "AND flagged as in collections.";
1308 } else if ($_POST['form_csvexport']) {
1309 // echo "</textarea>\n";
1310 // $alertmsg .= "$export_patient_count patients representing $" .
1311 // sprintf("%.2f", $export_dollars) . " have been exported.";
1313 echo " <tr bgcolor='#ffffff'>\n";
1314 if ($is_ins_summary) {
1315 echo " <td class='dehead'> " . xlt('Report Totals') . ":</td>\n";
1317 echo " <td class='detail' colspan='" . attr($initial_colspan) . "'>\n";
1318 echo " </td>\n";
1319 echo " <td class='dehead' colspan='" . attr($final_colspan - 3) .
1320 "'> " . xlt('Report Totals') . ":</td>\n";
1323 echo " <td class='dehead' align='right'> " .
1324 oeFormatMoney($grand_total_charges) . " </td>\n";
1325 echo " <td class='dehead' align='right'> " .
1326 oeFormatMoney($grand_total_adjustments) . " </td>\n";
1327 echo " <td class='dehead' align='right'> " .
1328 oeFormatMoney($grand_total_paid) . " </td>\n";
1329 if ($form_age_cols) {
1330 for ($c = 0; $c < $form_age_cols; ++
$c) {
1331 echo " <td class='dehead' align='right'>" .
1332 oeFormatMoney($grand_total_agedbal[$c]) . " </td>\n";
1335 echo " <td class='dehead' align='right'>" .
1336 oeFormatMoney($grand_total_charges +
1337 $grand_total_adjustments - $grand_total_paid) . " </td>\n";
1340 if ($form_cb_idays) {
1341 echo " <td class='detail'> </td>\n";
1344 if (!$is_ins_summary) {
1345 echo " <td class='detail' colspan='2'> </td>\n";
1349 echo " <td class='detail'> </td>\n";
1356 } // end if form_refresh
1359 if (!$_POST['form_csvexport']) {
1360 if (!$_POST['form_export']) {
1363 <div style
='float;margin-top:5px'>
1364 <div
class="btn-group pull-left" role
="group">
1365 <a href
='javascript:;' class='btn btn-default btn-save' onclick
='checkAll(true)'><?php
echo xlt('Select All'); ?
></a
>
1366 <a href
='javascript:;' class='btn btn-default btn-cancel' onclick
='checkAll(false)'><?php
echo xlt('Clear All'); ?
></a
>
1367 <a href
='javascript:;' class='btn btn-default btn-transmit' onclick
='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
1368 <?php
echo xlt('Export Selected as CSV'); ?
>
1370 <a href
='javascript:;' class='btn btn-default btn-transmit' onclick
='$("#form_export").attr("value","true"); $("#form_csvexport").val(""); $("#theform").submit();'>
1371 <?php
echo xlt('Export Selected to Collections'); ?
>
1375 <div style
='float:left'>
1376 <label
><input type
='checkbox' name
='form_individual' value
='1' /> <?php
echo xlt('Export Individual Invoices') ?
> 
; 
;</label
>
1379 <div style
='float:left'>
1380 <label
><input type
='checkbox' name
='form_without' value
='1' /> <?php
echo xlt('Without Update') ?
></label
>
1389 <script language
="JavaScript">
1392 echo "alert('" . addslashes($alertmsg) . "');\n";
1399 } // end not form_csvexport