full tab fixes to stop scroll of tab and not show bottom border of active tab (#407)
[openemr.git] / interface / reports / insurance_allocation_report.php
blobd4a76a3cdfc2705a62a9d9a752a94a5bed603561
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="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-3-2/index.js"></script>
49 <script language="JavaScript">
50 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
52 $(document).ready(function() {
53 var win = top.printLogSetup ? top : opener.top;
54 win.printLogSetup(document.getElementById('printbutton'));
55 });
57 </script>
59 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
60 <style type="text/css">
62 /* specifically include & exclude from printing */
63 @media print {
64 #report_parameters {
65 visibility: hidden;
66 display: none;
68 #report_parameters_daterange {
69 visibility: visible;
70 display: inline;
72 #report_results table {
73 margin-top: 0px;
77 /* specifically exclude some from the screen */
78 @media screen {
79 #report_parameters_daterange {
80 visibility: hidden;
81 display: none;
85 </style>
86 </head>
88 <body class="body_top">
90 <!-- Required for the popup date selectors -->
91 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
93 <span class='title'><?php xl('Report','e'); ?> - <?php xl('Patient Insurance Distribution','e'); ?></span>
95 <div id="report_parameters_daterange">
96 <?php echo date("d F Y", strtotime($form_from_date)) ." &nbsp; to &nbsp; ". date("d F Y", strtotime($form_to_date)); ?>
97 </div>
99 <form name='theform' method='post' action='insurance_allocation_report.php' id='theform'>
101 <div id="report_parameters">
102 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
103 <input type='hidden' name='form_csvexport' id='form_csvexport' value=''/>
105 <table>
106 <tr>
107 <td width='410px'>
108 <div style='float:left'>
110 <table class='text'>
111 <tr>
112 <td class='label'>
113 <?php xl('From','e'); ?>:
114 </td>
115 <td>
116 <input type='text' name='form_from_date' id="form_from_date" size='10' value='<?php echo $form_from_date ?>'
117 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
118 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
119 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
120 title='<?php xl('Click here to choose a date','e'); ?>'>
121 </td>
122 <td class='label'>
123 <?php xl('To','e'); ?>:
124 </td>
125 <td>
126 <input type='text' name='form_to_date' id="form_to_date" size='10' value='<?php echo $form_to_date ?>'
127 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='yyyy-mm-dd'>
128 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
129 id='img_to_date' border='0' alt='[?]' style='cursor:pointer'
130 title='<?php xl('Click here to choose a date','e'); ?>'>
131 </td>
132 </tr>
133 </table>
135 </div>
137 </td>
138 <td align='left' valign='middle' height="100%">
139 <table style='border-left:1px solid; width:100%; height:100%' >
140 <tr>
141 <td>
142 <div style='margin-left:15px'>
143 <a href='#' class='css_button' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
144 <span>
145 <?php xl('Submit','e'); ?>
146 </span>
147 </a>
149 <?php if ($_POST['form_refresh']) { ?>
150 <a href='#' class='css_button' id='printbutton'>
151 <span>
152 <?php xl('Print','e'); ?>
153 </span>
154 </a>
155 <a href='#' class='css_button' onclick='$("#form_csvexport").attr("value","true"); $("#theform").submit();'>
156 <span>
157 <?php xl('Export to CSV','e'); ?>
158 </span>
159 </a>
160 <?php } ?>
161 </div>
162 </td>
163 </tr>
164 </table>
165 </td>
166 </tr>
167 </table>
169 </form>
170 </div> <!-- end parameters -->
172 <div id="report_results">
173 <table>
175 <thead>
176 <th align='left'> <?php xl('Primary Insurance','e'); ?> </th>
177 <th align='right'> <?php xl('Charges','e'); ?> </th>
178 <th align='right'> <?php xl('Visits','e'); ?> </th>
179 <th align='right'> <?php xl('Patients','e'); ?> </th>
180 <th align='right'> <?php xl('Pt %','e'); ?> </th>
181 </thead>
182 <tbody>
183 <?php
184 } // end not export
185 if ($_POST['form_refresh'] || $_POST['form_csvexport']) {
187 $from_date = fixDate($_POST['form_from_date']);
188 $to_date = fixDate($_POST['form_to_date'], date('Y-m-d'));
190 $query = "SELECT b.pid, b.encounter, SUM(b.fee) AS charges, " .
191 "MAX(fe.date) AS date " .
192 "FROM form_encounter AS fe, billing AS b " .
193 "WHERE fe.date >= '$from_date' AND fe.date <= '$to_date' " .
194 "AND b.pid = fe.pid AND b.encounter = fe.encounter " .
195 "AND b.code_type != 'COPAY' AND b.activity > 0 AND b.fee != 0 " .
196 "GROUP BY b.pid, b.encounter ORDER BY b.pid, b.encounter";
198 $res = sqlStatement($query);
199 $insarr = array();
200 $prev_pid = 0;
201 $patcount = 0;
203 while ($row = sqlFetchArray($res)) {
204 $patient_id = $row['pid'];
205 $encounter_date = $row['date'];
206 $irow = sqlQuery("SELECT insurance_companies.name " .
207 "FROM insurance_data, insurance_companies WHERE " .
208 "insurance_data.pid = $patient_id AND " .
209 "insurance_data.type = 'primary' AND " .
210 "insurance_data.date <= '$encounter_date' AND " .
211 "insurance_companies.id = insurance_data.provider " .
212 "ORDER BY insurance_data.date DESC LIMIT 1");
213 $plan = $irow['name'] ? $irow['name'] : '-- No Insurance --';
214 $insarr[$plan]['visits'] += 1;
215 $insarr[$plan]['charges'] += sprintf('%0.2f', $row['charges']);
216 if ($patient_id != $prev_pid) {
217 ++$patcount;
218 $insarr[$plan]['patients'] += 1;
219 $prev_pid = $patient_id;
223 ksort($insarr);
225 while (list($key, $val) = each($insarr)) {
226 if ($_POST['form_csvexport']) {
227 echo '"' . $key . '",';
228 echo '"' . oeFormatMoney($val['charges']) . '",';
229 echo '"' . $val['visits'] . '",';
230 echo '"' . $val['patients'] . '",';
231 echo '"' . sprintf("%.1f", $val['patients'] * 100 / $patcount) . '"' . "\n";
233 else {
235 <tr>
236 <td>
237 <?php echo $key ?>
238 </td>
239 <td align='right'>
240 <?php echo oeFormatMoney($val['charges']) ?>
241 </td>
242 <td align='right'>
243 <?php echo $val['visits'] ?>
244 </td>
245 <td align='right'>
246 <?php echo $val['patients'] ?>
247 </td>
248 <td align='right'>
249 <?php printf("%.1f", $val['patients'] * 100 / $patcount) ?>
250 </td>
251 </tr>
252 <?php
253 } // end not export
254 } // end while
255 } // end if
257 if (! $_POST['form_csvexport']) {
260 </tbody>
261 </table>
262 </div> <!-- end of results -->
264 </body>
266 <!-- stuff for the popup calendar -->
267 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
268 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
269 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
270 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
271 <script language="Javascript">
272 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
273 Calendar.setup({inputField:"form_to_date", ifFormat:"%Y-%m-%d", button:"img_to_date"});
274 </script>
275 </html>
276 <?php
277 } // end not export