continued bug fixes (#1876)
[openemr.git] / interface / billing / indigent_patients_report.php
blob3de24b81251fd59aff57aac82ecf75f892613436
1 <?php
2 /**
3 * This is the Indigent Patients Report. It displays a summary of
4 * encounters within the specified time period for patients without
5 * insurance.
7 * @package OpenEMR
8 * @link http://www.open-emr.org
9 * @author Rod Roark <rod@sunsetsystems.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2005-2015 Rod Roark <rod@sunsetsystems.com>
12 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
13 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once("../globals.php");
17 require_once("$srcdir/patient.inc");
19 use OpenEMR\Core\Header;
21 $alertmsg = '';
23 function bucks($amount)
25 if ($amount) {
26 return oeFormatMoney($amount);
29 return "";
32 $form_start_date = (!empty($_POST['form_start_date'])) ? DateToYYYYMMDD($_POST['form_start_date']) : date('Y-01-01');
33 $form_end_date = (!empty($_POST['form_end_date'])) ? DateToYYYYMMDD($_POST['form_end_date']) : date('Y-m-d');
36 <html>
37 <head>
39 <style type="text/css">
41 /* specifically include & exclude from printing */
42 @media print {
43 #report_parameters {
44 visibility: hidden;
45 display: none;
47 #report_parameters_daterange {
48 visibility: visible;
49 display: inline;
51 #report_results table {
52 margin-top: 0px;
56 /* specifically exclude some from the screen */
57 @media screen {
58 #report_parameters_daterange {
59 visibility: hidden;
60 display: none;
64 </style>
66 <?php Header::setupHeader('datetime-picker'); ?>
68 <title><?php echo xlt('Indigent Patients Report')?></title>
70 <script language="JavaScript">
72 $(document).ready(function() {
73 var win = top.printLogSetup ? top : opener.top;
74 win.printLogSetup(document.getElementById('printbutton'));
76 $('.datepicker').datetimepicker({
77 <?php $datetimepicker_timepicker = false; ?>
78 <?php $datetimepicker_showseconds = false; ?>
79 <?php $datetimepicker_formatInput = true; ?>
80 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
81 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
82 });
83 });
85 </script>
87 </head>
89 <body class="body_top">
91 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Indigent Patients'); ?></span>
93 <form method='post' action='indigent_patients_report.php' id='theform' onsubmit='return top.restoreSession()'>
94 <input type="hidden" name="csrf_token_form" value="<?php echo attr(collectCsrfToken()); ?>" />
96 <div id="report_parameters">
98 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
100 <table>
101 <tr>
102 <td width='410px'>
103 <div style='float:left'>
105 <table class='text'>
106 <tr>
107 <td class='control-label'>
108 <?php echo xlt('Visits From'); ?>:
109 </td>
110 <td>
111 <input type='text' class='datepicker form-control' name='form_start_date' id="form_start_date" size='10' value='<?php echo attr(oeFormatShortDate($form_start_date)); ?>'>
112 </td>
113 <td class='control-label'>
114 <?php xl('To', 'e'); ?>:
115 </td>
116 <td>
117 <input type='text' class='datepicker form-control' name='form_end_date' id="form_end_date" size='10' value='<?php echo attr(oeFormatShortDate($form_end_date)); ?>'>
118 </td>
119 </tr>
120 </table>
122 </div>
124 </td>
125 <td align='left' valign='middle' height="100%">
126 <table style='border-left:1px solid; width:100%; height:100%' >
127 <tr>
128 <td>
129 <div class="text-center">
130 <div class="btn-group" role="group">
131 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
132 <?php echo xlt('Submit'); ?>
133 </a>
134 <?php if ($_POST['form_refresh']) { ?>
135 <a href='#' class='btn btn-default btn-print' id='printbutton'>
136 <?php echo xlt('Print'); ?>
137 </a>
138 <?php } ?>
139 </div>
140 </div>
141 </td>
142 </tr>
143 </table>
144 </td>
145 </tr>
146 </table>
147 </div> <!-- end of parameters -->
149 <div id="report_results">
150 <table>
152 <thead bgcolor="#dddddd">
153 <th>
154 &nbsp;<?php echo xlt('Patient'); ?>
155 </th>
156 <th>
157 &nbsp;<?php echo xlt('SSN'); ?>
158 </th>
159 <th>
160 &nbsp;<?php echo xlt('Invoice'); ?>
161 </th>
162 <th>
163 &nbsp;<?php echo xlt('Svc Date'); ?>
164 </th>
165 <th>
166 &nbsp;<?php echo xlt('Due Date'); ?>
167 </th>
168 <th align="right">
169 <?php echo xlt('Amount'); ?>&nbsp;
170 </th>
171 <th align="right">
172 <?php echo xlt('Paid'); ?>&nbsp;
173 </th>
174 <th align="right">
175 <?php echo xlt('Balance'); ?>&nbsp;
176 </th>
177 </thead>
179 <?php
180 if ($_POST['form_refresh']) {
181 if (!verifyCsrfToken($_POST["csrf_token_form"])) {
182 die(xlt('Authentication Error'));
185 $where = "";
186 $sqlBindArray = array();
188 if ($form_start_date) {
189 $where .= " AND e.date >= ?";
190 array_push($sqlBindArray, $form_start_date);
193 if ($form_end_date) {
194 $where .= " AND e.date <= ?";
195 array_push($sqlBindArray, $form_end_date);
198 $rez = sqlStatement("SELECT " .
199 "e.date, e.encounter, p.pid, p.lname, p.fname, p.mname, p.ss " .
200 "FROM form_encounter AS e, patient_data AS p, insurance_data AS i " .
201 "WHERE p.pid = e.pid AND i.pid = e.pid AND i.type = 'primary' " .
202 "AND i.provider = ''$where " .
203 "ORDER BY p.lname, p.fname, p.mname, p.pid, e.date", $sqlBindArray);
205 $total_amount = 0;
206 $total_paid = 0;
208 for ($irow = 0; $row = sqlFetchArray($rez); ++$irow) {
209 $patient_id = $row['pid'];
210 $encounter_id = $row['encounter'];
211 $invnumber = $row['pid'] . "." . $row['encounter'];
212 $inv_duedate = '';
213 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM drug_sales WHERE " .
214 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
215 $inv_amount = $arow['amount'];
216 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
217 "pid = ? AND encounter = ? AND " .
218 "activity = 1 AND code_type != 'COPAY'", array($patient_id, $encounter_id));
219 $inv_amount += $arow['amount'];
220 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
221 "pid = ? AND encounter = ? AND " .
222 "activity = 1 AND code_type = 'COPAY'", array($patient_id, $encounter_id));
223 $inv_paid = 0 - $arow['amount'];
224 $arow = sqlQuery("SELECT SUM(pay_amount) AS pay, " .
225 "sum(adj_amount) AS adj FROM ar_activity WHERE " .
226 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
227 $inv_paid += floatval($arow['pay']);
228 $inv_amount -= floatval($arow['adj']);
229 $total_amount += $inv_amount;
230 $total_paid += $inv_paid;
232 $bgcolor = (($irow & 1) ? "#ffdddd" : "#ddddff");
234 <tr bgcolor='<?php echo $bgcolor ?>'>
235 <td class="detail">
236 &nbsp;<?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>
237 </td>
238 <td class="detail">
239 &nbsp;<?php echo text($row['ss']); ?>
240 </td>
241 <td class="detail">
242 &nbsp;<?php echo text($invnumber); ?></a>
243 </td>
244 <td class="detail">
245 &nbsp;<?php echo text(oeFormatShortDate(substr($row['date'], 0, 10))); ?>
246 </td>
247 <td class="detail">
248 &nbsp;<?php echo text(oeFormatShortDate($inv_duedate)); ?>
249 </td>
250 <td class="detail" align="right">
251 <?php echo bucks($inv_amount); ?>&nbsp;
252 </td>
253 <td class="detail" align="right">
254 <?php echo bucks($inv_paid); ?>&nbsp;
255 </td>
256 <td class="detail" align="right">
257 <?php echo bucks($inv_amount - $inv_paid); ?>&nbsp;
258 </td>
259 </tr>
260 <?php
263 <tr bgcolor='#dddddd'>
264 <td class="detail">
265 &nbsp;<?php echo xlt('Totals'); ?>
266 </td>
267 <td class="detail">
268 &nbsp;
269 </td>
270 <td class="detail">
271 &nbsp;
272 </td>
273 <td class="detail">
274 &nbsp;
275 </td>
276 <td class="detail">
277 &nbsp;
278 </td>
279 <td class="detail" align="right">
280 <?php echo bucks($total_amount); ?>&nbsp;
281 </td>
282 <td class="detail" align="right">
283 <?php echo bucks($total_paid); ?>&nbsp;
284 </td>
285 <td class="detail" align="right">
286 <?php echo bucks($total_amount - $total_paid); ?>&nbsp;
287 </td>
288 </tr>
289 <?php
293 </table>
294 </div>
296 </form>
297 <script>
298 <?php
299 if ($alertmsg) {
300 echo "alert('" . addslashes($alertmsg) . "');\n";
303 </script>
304 </body>
306 </html>