Focus the search term on load
[openemr.git] / interface / reports / insurance_allocation_report.php
blob7a429f7fad08e8d9851d6a03dafe11757a835f82
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 // This module shows relative insurance usage by unique patients
8 // that are seen within a given time period. Each patient that had
9 // a visit is counted only once, regardless of how many visits.
11 include_once("../globals.php");
12 include_once("../../library/patient.inc");
13 include_once("../../library/acl.inc");
14 require_once("../../library/formatting.inc.php");
16 // Might want something different here.
18 // if (! acl_check('acct', 'rep')) die("Unauthorized access.");
20 $from_date = fixDate($_POST['form_from_date']);
21 $to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
23 if ($_POST['form_csvexport']) {
24 header("Pragma: public");
25 header("Expires: 0");
26 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
27 header("Content-Type: application/force-download");
28 header("Content-Disposition: attachment; filename=insurance_distribution.csv");
29 header("Content-Description: File Transfer");
30 // CSV headers:
31 if (true) {
32 echo '"Insurance",';
33 echo '"Charges",';
34 echo '"Visits",';
35 echo '"Patients",';
36 echo '"Pt Pct"' . "\n";
39 else {
41 <html>
42 <head>
43 <?php html_header_show();?>
44 <title><?php xl('Patient Insurance Distribution','e'); ?></title>
45 <script type="text/javascript" src="../../library/overlib_mini.js"></script>
46 <script type="text/javascript" src="../../library/calendar.js"></script>
47 <script type="text/javascript" src="../../library/textformat.js"></script>
48 <script type="text/javascript" src="../../library/js/jquery.1.3.2.js"></script>
49 <script language="JavaScript">
50 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
51 </script>
53 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
54 <style type="text/css">
56 /* specifically include & exclude from printing */
57 @media print {
58 #report_parameters {
59 visibility: hidden;
60 display: none;
62 #report_parameters_daterange {
63 visibility: visible;
64 display: inline;
66 #report_results table {
67 margin-top: 0px;
71 /* specifically exclude some from the screen */
72 @media screen {
73 #report_parameters_daterange {
74 visibility: hidden;
75 display: none;
79 </style>
80 </head>
82 <body class="body_top">
84 <!-- Required for the popup date selectors -->
85 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
87 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Patient Insurance Distribution','e'); ?></span>
89 <div id="report_parameters_daterange">
90 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
91 </div>
93 <form name='theform' method='post' action='insurance_allocation_report.php' id='theform'>
95 <div id="report_parameters">
96 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
97 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
99 <table>
100 <tr>
101 <td width='410px'>
102 <div style='float:left'>
104 <table class='text'>
105 <tr>
106 <td class='label'>
107 <?php xl('From','e'); ?>:
108 </td>
109 <td>
110 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
111 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
112 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
113 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
114 title='<?php xl('Click here to choose a date','e'); ?>'>
115 </td>
116 <td class='label'>
117 <?php xl('To','e'); ?>:
118 </td>
119 <td>
120 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
121 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
122 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
123 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
124 title='<?php xl('Click here to choose a date','e'); ?>'>
125 </td>
126 </tr>
127 </table>
129 </div>
131 </td>
132 <td align='left' valign='middle' height="100%">
133 <table style='border-left:1px solid; width:100%; height:100%' >
134 <tr>
135 <td>
136 <div style='margin-left:15px'>
137 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
138 <span>
139 <?php xl('Submit','e'); ?>
140 </span>
141 </a>
143 <?php if ($_POST['form_refresh']) { ?>
144 <a href='#' class='css_button' onclick='window.print()'>
145 <span>
146 <?php xl('Print','e'); ?>
147 </span>
148 </a>
149 <a href='#' class='css_button' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
150 <span>
151 <?php xl('Export to CSV','e'); ?>
152 </span>
153 </a>
154 <?php } ?>
155 </div>
156 </td>
157 </tr>
158 </table>
159 </td>
160 </tr>
161 </table>
163 </form>
164 </div> <!-- end parameters -->
166 <div id="report_results">
167 <table>
169 <thead>
170 <th align='left'> <?php xl('Primary Insurance','e'); ?> </th>
171 <th align='right'> <?php xl('Charges','e'); ?> </th>
172 <th align='right'> <?php xl('Visits','e'); ?> </th>
173 <th align='right'> <?php xl('Patients','e'); ?> </th>
174 <th align='right'> <?php xl('Pt %','e'); ?> </th>
175 </thead>
176 <tbody>
177 <?php
178 } // end not export
179 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
181 $from_date = fixDate($_POST['form_from_date']);
182 $to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
184 $query = "SELECT b.pid, b.encounter, SUM(b.fee) AS charges, " .
185 "MAX(fe.date) AS date " .
186 "FROM form_encounter AS fe, billing AS b " .
187 "WHERE fe.date >= '$from_date' AND fe.date <= '$to_date' " .
188 "AND b.pid = fe.pid AND b.encounter = fe.encounter " .
189 "AND b.code_type != 'COPAY' AND b.activity > 0 AND b.fee != 0 " .
190 "GROUP BY b.pid, b.encounter ORDER BY b.pid, b.encounter";
192 $res = sqlStatement($query);
193 $insarr = array();
194 $prev_pid = 0;
195 $patcount = 0;
197 while ($row = sqlFetchArray($res)) {
198 $patient_id = $row['pid'];
199 $encounter_date = $row['date'];
200 $irow = sqlQuery("SELECT insurance_companies.name " .
201 "FROM insurance_data, insurance_companies WHERE " .
202 "insurance_data.pid = $patient_id AND " .
203 "insurance_data.type = 'primary' AND " .
204 "insurance_data.date <= '$encounter_date' AND " .
205 "insurance_companies.id = insurance_data.provider " .
206 "ORDER BY insurance_data.date DESC LIMIT 1");
207 $plan = $irow['name'] ? $irow['name'] : '-- No Insurance --';
208 $insarr[$plan]['visits'] += 1;
209 $insarr[$plan]['charges'] += sprintf('%0.2f', $row['charges']);
210 if ($patient_id != $prev_pid) {
211 ++$patcount;
212 $insarr[$plan]['patients'] += 1;
213 $prev_pid = $patient_id;
217 ksort($insarr);
219 while (list($key, $val) = each($insarr)) {
220 if ($_POST['form_csvexport']) {
221 echo '"' . $key . '",';
222 echo '"' . oeFormatMoney($val['charges']) . '",';
223 echo '"' . $val['visits'] . '",';
224 echo '"' . $val['patients'] . '",';
225 echo '"' . sprintf("%.1f", $val['patients'] * 100 / $patcount) . '"' . "\n";
227 else {
229 <tr>
230 <td>
231 <?php echo $key ?>
232 </td>
233 <td align='right'>
234 <?php echo oeFormatMoney($val['charges']) ?>
235 </td>
236 <td align='right'>
237 <?php echo $val['visits'] ?>
238 </td>
239 <td align='right'>
240 <?php echo $val['patients'] ?>
241 </td>
242 <td align='right'>
243 <?php printf("%.1f", $val['patients'] * 100 / $patcount) ?>
244 </td>
245 </tr>
246 <?php
247 } // end not export
248 } // end while
249 } // end if
251 if (! $_POST['form_csvexport']) {
254 </tbody>
255 </table>
256 </div> <!-- end of results -->
258 </body>
260 <!-- stuff for the popup calendar -->
261 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
262 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
263 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
264 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
265 <script language="Javascript">
266 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
267 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
268 </script>
269 </html>
270 <?php
271 } // end not export