refactor: refactor bucks function with new class based on oeFormatMoney (#6932)
[openemr.git] / interface / billing / indigent_patients_report.php
blob6f9c8c9e671e848ac15720b4401bd8e0b1703f0e
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.php");
20 use OpenEMR\Common\Acl\AclMain;
21 use OpenEMR\Common\Csrf\CsrfUtils;
22 use OpenEMR\Common\Twig\TwigContainer;
23 use OpenEMR\Common\Utils\FormatMoney;
24 use OpenEMR\Core\Header;
26 if (!AclMain::aclCheckCore('acct', 'rep_a')) {
27 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Indigent Patients Report")]);
28 exit;
31 $alertmsg = '';
33 $form_start_date = (!empty($_POST['form_start_date'])) ? DateToYYYYMMDD($_POST['form_start_date']) : date('Y-01-01');
34 $form_end_date = (!empty($_POST['form_end_date'])) ? DateToYYYYMMDD($_POST['form_end_date']) : date('Y-m-d');
37 <html>
38 <head>
40 <style>
42 /* specifically include & exclude from printing */
43 @media print {
44 #report_parameters {
45 visibility: hidden;
46 display: none;
48 #report_parameters_daterange {
49 visibility: visible;
50 display: inline;
52 #report_results table {
53 margin-top: 0px;
57 /* specifically exclude some from the screen */
58 @media screen {
59 #report_parameters_daterange {
60 visibility: hidden;
61 display: none;
65 </style>
67 <?php Header::setupHeader('datetime-picker'); ?>
69 <title><?php echo xlt('Indigent Patients Report')?></title>
71 <script>
73 $(function () {
74 var win = top.printLogSetup ? top : opener.top;
75 win.printLogSetup(document.getElementById('printbutton'));
77 $('.datepicker').datetimepicker({
78 <?php $datetimepicker_timepicker = false; ?>
79 <?php $datetimepicker_showseconds = false; ?>
80 <?php $datetimepicker_formatInput = true; ?>
81 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
82 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
83 });
84 });
86 </script>
88 </head>
90 <body class="body_top">
92 <span class='title'><?php echo xlt('Report'); ?> - <?php echo xlt('Indigent Patients'); ?></span>
94 <form method='post' action='indigent_patients_report.php' id='theform' onsubmit='return top.restoreSession()'>
95 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
97 <div id="report_parameters">
99 <input type='hidden' name='form_refresh' id='form_refresh' value=''/>
101 <table>
102 <tr>
103 <td width='410px'>
104 <div style='float: left'>
106 <table class='text'>
107 <tr>
108 <td class='col-form-label'>
109 <?php echo xlt('Visits From'); ?>:
110 </td>
111 <td>
112 <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)); ?>'>
113 </td>
114 <td class='col-form-label'>
115 <?php echo xlt('To{{Range}}'); ?>:
116 </td>
117 <td>
118 <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)); ?>'>
119 </td>
120 </tr>
121 </table>
123 </div>
125 </td>
126 <td class='h-100' align='left' valign='middle'>
127 <table class='w-100 h-100' style='border-left: 1px solid;'>
128 <tr>
129 <td>
130 <div class="text-center">
131 <div class="btn-group" role="group">
132 <a href='#' class='btn btn-secondary btn-save' onclick='$("#form_refresh").attr("value","true"); $("#theform").submit();'>
133 <?php echo xlt('Submit'); ?>
134 </a>
135 <?php if (!empty($_POST['form_refresh'])) { ?>
136 <a href='#' class='btn btn-secondary btn-print' id='printbutton'>
137 <?php echo xlt('Print'); ?>
138 </a>
139 <?php } ?>
140 </div>
141 </div>
142 </td>
143 </tr>
144 </table>
145 </td>
146 </tr>
147 </table>
148 </div> <!-- end of parameters -->
150 <div id="report_results">
151 <table class='table'>
153 <thead class="thead-light">
154 <th>
155 &nbsp;<?php echo xlt('Patient'); ?>
156 </th>
157 <th>
158 &nbsp;<?php echo xlt('SSN'); ?>
159 </th>
160 <th>
161 &nbsp;<?php echo xlt('Invoice'); ?>
162 </th>
163 <th>
164 &nbsp;<?php echo xlt('Svc Date'); ?>
165 </th>
166 <th>
167 &nbsp;<?php echo xlt('Due Date'); ?>
168 </th>
169 <th align="right">
170 <?php echo xlt('Amount'); ?>&nbsp;
171 </th>
172 <th align="right">
173 <?php echo xlt('Paid'); ?>&nbsp;
174 </th>
175 <th align="right">
176 <?php echo xlt('Balance'); ?>&nbsp;
177 </th>
178 </thead>
180 <?php
181 if (!empty($_POST['form_refresh'])) {
182 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
183 CsrfUtils::csrfNotVerified();
186 $where = "";
187 $sqlBindArray = array();
189 if ($form_start_date) {
190 $where .= " AND e.date >= ?";
191 array_push($sqlBindArray, $form_start_date);
194 if ($form_end_date) {
195 $where .= " AND e.date <= ?";
196 array_push($sqlBindArray, $form_end_date);
199 $rez = sqlStatement("SELECT " .
200 "e.date, e.encounter, p.pid, p.lname, p.fname, p.mname, p.ss " .
201 "FROM form_encounter AS e, patient_data AS p, insurance_data AS i " .
202 "WHERE p.pid = e.pid AND i.pid = e.pid AND i.type = 'primary' " .
203 "AND i.provider = ''$where " .
204 "ORDER BY p.lname, p.fname, p.mname, p.pid, e.date", $sqlBindArray);
206 $total_amount = 0;
207 $total_paid = 0;
209 for ($irow = 0; $row = sqlFetchArray($rez); ++$irow) {
210 $patient_id = $row['pid'];
211 $encounter_id = $row['encounter'];
212 $invnumber = $row['pid'] . "." . $row['encounter'];
213 $inv_duedate = '';
214 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM drug_sales WHERE " .
215 "pid = ? AND encounter = ?", array($patient_id, $encounter_id));
216 $inv_amount = $arow['amount'];
217 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
218 "pid = ? AND encounter = ? AND " .
219 "activity = 1 AND code_type != 'COPAY'", array($patient_id, $encounter_id));
220 $inv_amount += $arow['amount'];
221 $arow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
222 "pid = ? AND encounter = ? AND " .
223 "activity = 1 AND code_type = 'COPAY'", array($patient_id, $encounter_id));
224 $inv_paid = 0 - $arow['amount'];
225 $arow = sqlQuery(
226 "SELECT SUM(pay_amount) AS pay, sum(adj_amount) AS adj " .
227 "FROM ar_activity WHERE pid = ? AND encounter = ? AND deleted IS NULL",
228 array($patient_id, $encounter_id)
230 $inv_paid += floatval($arow['pay']);
231 $inv_amount -= floatval($arow['adj']);
232 $total_amount += $inv_amount;
233 $total_paid += $inv_paid;
235 $bgcolor = (($irow & 1) ? "#ffdddd" : "#ddddff");
237 <tr bgcolor='<?php echo $bgcolor ?>'>
238 <td class="detail">
239 &nbsp;<?php echo text($row['lname'] . ', ' . $row['fname'] . ' ' . $row['mname']); ?>
240 </td>
241 <td class="detail">
242 &nbsp;<?php echo text($row['ss']); ?>
243 </td>
244 <td class="detail">
245 &nbsp;<?php echo text($invnumber); ?></a>
246 </td>
247 <td class="detail">
248 &nbsp;<?php echo text(oeFormatShortDate(substr($row['date'], 0, 10))); ?>
249 </td>
250 <td class="detail">
251 &nbsp;<?php echo text(oeFormatShortDate($inv_duedate)); ?>
252 </td>
253 <td class="detail" align="right">
254 <?php echo FormatMoney::getBucks($inv_amount); ?>&nbsp;
255 </td>
256 <td class="detail" align="right">
257 <?php echo FormatMoney::getBucks($inv_paid); ?>&nbsp;
258 </td>
259 <td class="detail" align="right">
260 <?php echo FormatMoney::getBucks($inv_amount - $inv_paid); ?>&nbsp;
261 </td>
262 </tr>
263 <?php
266 <tr class="table-light">
267 <td class="detail">
268 &nbsp;<?php echo xlt('Totals'); ?>
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">
280 &nbsp;
281 </td>
282 <td class="detail" align="right">
283 <?php echo FormatMoney::getBucks($total_amount); ?>&nbsp;
284 </td>
285 <td class="detail" align="right">
286 <?php echo FormatMoney::getBucks($total_paid); ?>&nbsp;
287 </td>
288 <td class="detail" align="right">
289 <?php echo FormatMoney::getBucks($total_amount - $total_paid); ?>&nbsp;
290 </td>
291 </tr>
292 <?php
296 </table>
297 </div>
299 </form>
300 <script>
301 <?php
302 if ($alertmsg) {
303 echo "alert(" . js_escape($alertmsg) . ");\n";
306 </script>
307 </body>
309 </html>