Indigent Patients Report Cleanup
[openemr.git] / interface / billing / indigent_patients_report.php
blob588b4664d68bffaf129143e171b4abfbe6971d46
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 use OpenEMR\Core\Header;
18 require_once("../globals.php");
19 require_once("$srcdir/patient.inc");
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()'>
95 <div id="report_parameters">
97 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
99 <table>
100 <tr>
101 <td width='410px'>
102 <div style='float:left'>
104 <table class='text'>
105 <tr>
106 <td class='control-label'>
107 <?php echo xlt('Visits From'); ?>:
108 </td>
109 <td>
110 <input type='text' class='datepicker form-control' name='form_start_date' id="form_start_date" size='10' value='<?php echo oeFormatShortDate($form_start_date); ?>'>
111 </td>
112 <td class='control-label'>
113 <?php xl('To', 'e'); ?>:
114 </td>
115 <td>
116 <input type='text' class='datepicker form-control' name='form_end_date' id="form_end_date" size='10' value='<?php echo oeFormatShortDate($form_end_date); ?>'>
117 </td>
118 </tr>
119 </table>
121 </div>
123 </td>
124 <td align='left' valign='middle' height="100%">
125 <table style='border-left:1px solid; width:100%; height:100%' >
126 <tr>
127 <td>
128 <div class="text-center">
129 <div class="btn-group" role="group">
130 <a href='#' class='btn btn-default btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
131 <?php echo xlt('Submit'); ?>
132 </a>
133 <?php if ($_POST['form_refresh']) { ?>
134 <a href='#' class='btn btn-default btn-print' id='printbutton'>
135 <?php echo xlt('Print'); ?>
136 </a>
137 <?php } ?>
138 </div>
139 </div>
140 </td>
141 </tr>
142 </table>
143 </td>
144 </tr>
145 </table>
146 </div> <!-- end of parameters -->
148 <div id="report_results">
149 <table>
151 <thead bgcolor="#dddddd">
152 <th>
153 &nbsp;<?php echo xlt('Patient'); ?>
154 </th>
155 <th>
156 &nbsp;<?php echo xlt('SSN'); ?>
157 </th>
158 <th>
159 &nbsp;<?php echo xlt('Invoice'); ?>
160 </th>
161 <th>
162 &nbsp;<?php echo xlt('Svc Date'); ?>
163 </th>
164 <th>
165 &nbsp;<?php echo xlt('Due Date'); ?>
166 </th>
167 <th align="right">
168 <?php echo xlt('Amount'); ?>&nbsp;
169 </th>
170 <th align="right">
171 <?php echo xlt('Paid'); ?>&nbsp;
172 </th>
173 <th align="right">
174 <?php echo xlt('Balance'); ?>&nbsp;
175 </th>
176 </thead>
178 <?php
179 if ($_POST['form_refresh']) {
180 $where = "";
181 $sqlBindArray = array();
183 if ($form_start_date) {
184 $where .= " AND e.date >= ?";
185 array_push($sqlBindArray, $form_start_date);
188 if ($form_end_date) {
189 $where .= " AND e.date <= ?";
190 array_push($sqlBindArray, $form_end_date);
193 $rez = sqlStatement("SELECT " .
194 "e.date, e.encounter, p.pid, p.lname, p.fname, p.mname, p.ss " .
195 "FROM form_encounter AS e, patient_data AS p, insurance_data AS i " .
196 "WHERE p.pid = e.pid AND i.pid = e.pid AND i.type = 'primary' " .
197 "AND i.provider = ''$where " .
198 "ORDER BY p.lname, p.fname, p.mname, p.pid, e.date", $sqlBindArray);
200 $total_amount = 0;
201 $total_paid = 0;
203 for ($irow = 0; $row = sqlFetchArray($rez); ++$irow) {
204 $patient_id = $row['pid'];
205 $encounter_id = $row['encounter'];
206 $invnumber = $row['pid'] . "." . $row['encounter'];
207 $inv_duedate = '';
208 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM drug_sales WHERE " .
209 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
210 $inv_amount = $arow['amount'];
211 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
212 "pid = ? AND encounter = ? AND " .
213 "activity = 1 AND code_type != 'COPAY'", array($patient_id, $encounter_id));
214 $inv_amount += $arow['amount'];
215 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
216 "pid = ? AND encounter = ? AND " .
217 "activity = 1 AND code_type = 'COPAY'", array($patient_id, $encounter_id));
218 $inv_paid = 0 - $arow['amount'];
219 $arow = sqlQuery("SELECT SUM(pay_amount) AS pay, " .
220 "sum(adj_amount) AS adj FROM ar_activity WHERE " .
221 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
222 $inv_paid += $arow['pay'];
223 $inv_amount -= $arow['adj'];
224 $total_amount += bucks($inv_amount);
225 $total_paid += bucks($inv_paid);
227 $bgcolor = (($irow & 1) ? "#ffdddd" : "#ddddff");
229 <tr bgcolor='<?php echo $bgcolor ?>'>
230 <td class="detail">
231 &nbsp;<?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>
232 </td>
233 <td class="detail">
234 &nbsp;<?php echo text($row['ss']); ?>
235 </td>
236 <td class="detail">
237 &nbsp;<?php echo text($invnumber); ?></a>
238 </td>
239 <td class="detail">
240 &nbsp;<?php echo oeFormatShortDate(substr($row['date'], 0, 10)); ?>
241 </td>
242 <td class="detail">
243 &nbsp;<?php echo oeFormatShortDate($inv_duedate); ?>
244 </td>
245 <td class="detail" align="right">
246 <?php echo bucks($inv_amount); ?>&nbsp;
247 </td>
248 <td class="detail" align="right">
249 <?php echo bucks($inv_paid); ?>&nbsp;
250 </td>
251 <td class="detail" align="right">
252 <?php echo bucks($inv_amount - $inv_paid); ?>&nbsp;
253 </td>
254 </tr>
255 <?php
258 <tr bgcolor='#dddddd'>
259 <td class="detail">
260 &nbsp;<?php echo xlt('Totals'); ?>
261 </td>
262 <td class="detail">
263 &nbsp;
264 </td>
265 <td class="detail">
266 &nbsp;
267 </td>
268 <td class="detail">
269 &nbsp;
270 </td>
271 <td class="detail">
272 &nbsp;
273 </td>
274 <td class="detail" align="right">
275 <?php echo bucks($total_amount); ?>&nbsp;
276 </td>
277 <td class="detail" align="right">
278 <?php echo bucks($total_paid); ?>&nbsp;
279 </td>
280 <td class="detail" align="right">
281 <?php echo bucks($total_amount - $total_paid); ?>&nbsp;
282 </td>
283 </tr>
284 <?php
288 </table>
289 </div>
291 </form>
292 <script>
293 <?php
294 if ($alertmsg) {
295 echo "alert('" . addslashes($alertmsg) . "');\n";
298 </script>
299 </body>
301 </html>