Added ar_activity.deleted to track voids of payments and adjustments. (#3743)
[openemr.git] / interface / billing / indigent_patients_report.php
blob23d919cf5d5de88094bd568755304c091b1e0baf
1 <?php
3 /**
4 * This is the Indigent Patients Report. It displays a summary of
5 * encounters within the specified time period for patients without
6 * insurance.
8 * @package OpenEMR
9 * @link http://www.open-emr.org
10 * @author Rod Roark <rod@sunsetsystems.com>
11 * @author Brady Miller <brady.g.miller@gmail.com>
12 * @copyright Copyright (c) 2005-2015, 2020 Rod Roark <rod@sunsetsystems.com>
13 * @copyright Copyright (c) 2017-2020 Brady Miller <brady.g.miller@gmail.com>
14 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
17 require_once("../globals.php");
18 require_once("$srcdir/patient.inc");
20 use OpenEMR\Common\Csrf\CsrfUtils;
21 use OpenEMR\Core\Header;
23 $alertmsg = '';
25 function bucks($amount)
27 if ($amount) {
28 return oeFormatMoney($amount);
31 return "";
34 $form_start_date = (!empty($_POST['form_start_date'])) ? DateToYYYYMMDD($_POST['form_start_date']) : date('Y-01-01');
35 $form_end_date = (!empty($_POST['form_end_date'])) ? DateToYYYYMMDD($_POST['form_end_date']) : date('Y-m-d');
38 <html>
39 <head>
41 <style>
43 /* specifically include & exclude from printing */
44 @media print {
45 #report_parameters {
46 visibility: hidden;
47 display: none;
49 #report_parameters_daterange {
50 visibility: visible;
51 display: inline;
53 #report_results table {
54 margin-top: 0px;
58 /* specifically exclude some from the screen */
59 @media screen {
60 #report_parameters_daterange {
61 visibility: hidden;
62 display: none;
66 </style>
68 <?php Header::setupHeader('datetime-picker'); ?>
70 <title><?php echo xlt('Indigent Patients Report')?></title>
72 <script>
74 $(function () {
75 var win = top.printLogSetup ? top : opener.top;
76 win.printLogSetup(document.getElementById('printbutton'));
78 $('.datepicker').datetimepicker({
79 <?php $datetimepicker_timepicker = false; ?>
80 <?php $datetimepicker_showseconds = false; ?>
81 <?php $datetimepicker_formatInput = true; ?>
82 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
83 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
84 });
85 });
87 </script>
89 </head>
91 <body class="body_top">
93 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Indigent Patients'); ?></span>
95 <form method='post' action='indigent_patients_report.php' id='theform' onsubmit='return top.restoreSession()'>
96 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
98 <div id="report_parameters">
100 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
102 <table>
103 <tr>
104 <td width='410px'>
105 <div style='float: left'>
107 <table class='text'>
108 <tr>
109 <td class='col-form-label'>
110 <?php echo xlt('Visits From'); ?>:
111 </td>
112 <td>
113 <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)); ?>'>
114 </td>
115 <td class='col-form-label'>
116 <?php echo xlt('To{{Range}}'); ?>:
117 </td>
118 <td>
119 <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)); ?>'>
120 </td>
121 </tr>
122 </table>
124 </div>
126 </td>
127 <td class='h-100' align='left' valign='middle'>
128 <table class='w-100 h-100' style='border-left: 1px solid;'>
129 <tr>
130 <td>
131 <div class="text-center">
132 <div class="btn-group" role="group">
133 <a href='#' class='btn btn-secondary btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
134 <?php echo xlt('Submit'); ?>
135 </a>
136 <?php if ($_POST['form_refresh']) { ?>
137 <a href='#' class='btn btn-secondary btn-print' id='printbutton'>
138 <?php echo xlt('Print'); ?>
139 </a>
140 <?php } ?>
141 </div>
142 </div>
143 </td>
144 </tr>
145 </table>
146 </td>
147 </tr>
148 </table>
149 </div> <!-- end of parameters -->
151 <div id="report_results">
152 <table class='table'>
154 <thead class="thead-light">
155 <th>
156 &nbsp;<?php echo xlt('Patient'); ?>
157 </th>
158 <th>
159 &nbsp;<?php echo xlt('SSN'); ?>
160 </th>
161 <th>
162 &nbsp;<?php echo xlt('Invoice'); ?>
163 </th>
164 <th>
165 &nbsp;<?php echo xlt('Svc Date'); ?>
166 </th>
167 <th>
168 &nbsp;<?php echo xlt('Due Date'); ?>
169 </th>
170 <th align="right">
171 <?php echo xlt('Amount'); ?>&nbsp;
172 </th>
173 <th align="right">
174 <?php echo xlt('Paid'); ?>&nbsp;
175 </th>
176 <th align="right">
177 <?php echo xlt('Balance'); ?>&nbsp;
178 </th>
179 </thead>
181 <?php
182 if ($_POST['form_refresh']) {
183 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
184 CsrfUtils::csrfNotVerified();
187 $where = "";
188 $sqlBindArray = array();
190 if ($form_start_date) {
191 $where .= " AND e.date >= ?";
192 array_push($sqlBindArray, $form_start_date);
195 if ($form_end_date) {
196 $where .= " AND e.date <= ?";
197 array_push($sqlBindArray, $form_end_date);
200 $rez = sqlStatement("SELECT " .
201 "e.date, e.encounter, p.pid, p.lname, p.fname, p.mname, p.ss " .
202 "FROM form_encounter AS e, patient_data AS p, insurance_data AS i " .
203 "WHERE p.pid = e.pid AND i.pid = e.pid AND i.type = 'primary' " .
204 "AND i.provider = ''$where " .
205 "ORDER BY p.lname, p.fname, p.mname, p.pid, e.date", $sqlBindArray);
207 $total_amount = 0;
208 $total_paid = 0;
210 for ($irow = 0; $row = sqlFetchArray($rez); ++$irow) {
211 $patient_id = $row['pid'];
212 $encounter_id = $row['encounter'];
213 $invnumber = $row['pid'] . "." . $row['encounter'];
214 $inv_duedate = '';
215 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM drug_sales WHERE " .
216 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
217 $inv_amount = $arow['amount'];
218 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
219 "pid = ? AND encounter = ? AND " .
220 "activity = 1 AND code_type != 'COPAY'", array($patient_id, $encounter_id));
221 $inv_amount += $arow['amount'];
222 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
223 "pid = ? AND encounter = ? AND " .
224 "activity = 1 AND code_type = 'COPAY'", array($patient_id, $encounter_id));
225 $inv_paid = 0 - $arow['amount'];
226 $arow = sqlQuery(
227 "SELECT SUM(pay_amount) AS pay, sum(adj_amount) AS adj " .
228 "FROM ar_activity WHERE pid = ? AND encounter = ? AND deleted IS NULL",
229 array($patient_id, $encounter_id)
231 $inv_paid += floatval($arow['pay']);
232 $inv_amount -= floatval($arow['adj']);
233 $total_amount += $inv_amount;
234 $total_paid += $inv_paid;
236 $bgcolor = (($irow & 1) ? "#ffdddd" : "#ddddff");
238 <tr bgcolor='<?php echo $bgcolor ?>'>
239 <td class="detail">
240 &nbsp;<?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>
241 </td>
242 <td class="detail">
243 &nbsp;<?php echo text($row['ss']); ?>
244 </td>
245 <td class="detail">
246 &nbsp;<?php echo text($invnumber); ?></a>
247 </td>
248 <td class="detail">
249 &nbsp;<?php echo text(oeFormatShortDate(substr($row['date'], 0, 10))); ?>
250 </td>
251 <td class="detail">
252 &nbsp;<?php echo text(oeFormatShortDate($inv_duedate)); ?>
253 </td>
254 <td class="detail" align="right">
255 <?php echo bucks($inv_amount); ?>&nbsp;
256 </td>
257 <td class="detail" align="right">
258 <?php echo bucks($inv_paid); ?>&nbsp;
259 </td>
260 <td class="detail" align="right">
261 <?php echo bucks($inv_amount - $inv_paid); ?>&nbsp;
262 </td>
263 </tr>
264 <?php
267 <tr class="table-light">
268 <td class="detail">
269 &nbsp;<?php echo xlt('Totals'); ?>
270 </td>
271 <td class="detail">
272 &nbsp;
273 </td>
274 <td class="detail">
275 &nbsp;
276 </td>
277 <td class="detail">
278 &nbsp;
279 </td>
280 <td class="detail">
281 &nbsp;
282 </td>
283 <td class="detail" align="right">
284 <?php echo bucks($total_amount); ?>&nbsp;
285 </td>
286 <td class="detail" align="right">
287 <?php echo bucks($total_paid); ?>&nbsp;
288 </td>
289 <td class="detail" align="right">
290 <?php echo bucks($total_amount - $total_paid); ?>&nbsp;
291 </td>
292 </tr>
293 <?php
297 </table>
298 </div>
300 </form>
301 <script>
302 <?php
303 if ($alertmsg) {
304 echo "alert(" . js_escape($alertmsg) . ");\n";
307 </script>
308 </body>
310 </html>