Moved recent v5.0.0 statement fix to current master.
[openemr.git] / interface / reports / daily_summary_report.php
blob9711363fe04ed9e68710c9fbffc5859b60b24ee3
1 <?php
2 /*
3 * Daily Summary Report. (/interface/reports/daily_summary_report.php)
6 * This report shows date wise numbers of the Appointments Scheduled,
7 * New Patients, Visited patients, Total Charges, Total Co-pay and Balance amount for the selected facility & providers wise.
9 * Copyright (C) 2016 Rishabh Software
10 * Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
12 * LICENSE: This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 3
15 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
23 * @package OpenEMR
24 * @author Rishabh Software
25 * @author Brady Miller <brady.g.miller@gmail.com>
26 * @link http://www.open-emr.org
32 use OpenEMR\Core\Header;
34 require_once("../globals.php");
35 require_once "$srcdir/options.inc.php";
36 require_once "$srcdir/appointments.inc.php";
38 $facilityService = new \services\FacilityService();
40 $selectedFromDate = isset($_POST['form_from_date']) ? $_POST['form_from_date'] : date('Y-m-d'); // From date filter
41 $selectedToDate = isset($_POST['form_to_date']) ? $_POST['form_to_date'] : date('Y-m-d'); // To date filter
42 $selectedFacility = isset($_POST['form_facility']) ? $_POST['form_facility'] : ""; // facility filter
43 $selectedProvider = isset($_POST['form_provider']) ? $_POST['form_provider'] : ""; // provider filter
45 $from_date = fixDate($selectedFromDate, date('Y-m-d'));
46 $to_date = fixDate($selectedToDate, date('Y-m-d'));
49 <html>
50 <head>
52 <?php Header::setupHeader(['datetime-picker', 'report-helper']); ?>
54 <script type="text/javascript">
57 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
59 function submitForm() {
60 var fromDate = $("#form_from_date").val();
61 var toDate = $("#form_to_date").val();
63 if (fromDate === '') {
64 alert("<?php echo xls('Please select From date'); ?>");
65 return false;
67 if (toDate === '') {
68 alert("<?php echo xls('Please select To date'); ?>");
69 return false;
71 if (Date.parse(fromDate) > Date.parse(toDate)) {
72 alert("<?php echo xls('From date should be less than To date'); ?>");
73 return false;
75 else {
76 $("#form_refresh").attr("value", "true");
77 $("#report_form").submit();
81 $( document ).ready(function(){
82 $('.datepicker').datetimepicker({
83 <?php $datetimepicker_timepicker = false; ?>
84 <?php $datetimepicker_showseconds = false; ?>
85 <?php $datetimepicker_formatInput = false; ?>
86 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
87 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
88 });
89 });
91 </script>
93 </head>
95 <body class="body_top">
97 <span class='title'><?php echo xlt('Daily Summary Report'); ?></span>
98 <!-- start of search parameters -->
99 <form method='post' name='report_form' id='report_form' action='' onsubmit='return top.restoreSession()'>
100 <div id="report_parameters">
101 <table class="tableonly">
102 <tr>
103 <td width='745px'>
104 <div style='float: left'>
105 <table class='text'>
106 <tr>
107 <td class='control-label'><?php echo xlt('Facility'); ?>:</td>
108 <td><?php dropdown_facility($selectedFacility, 'form_facility', false); ?></td>
109 <td class='control-label'><?php echo xlt('From'); ?>:</td>
110 <td>
111 <input type='text' name='form_from_date' id="form_from_date"
112 class='datepicker form-control'
113 size='10' value='<?php echo attr($from_date) ?>'
114 title='yyyy-mm-dd'>
115 </td>
116 <td class='control-label'><?php echo xlt('To'); ?>:</td>
117 <td>
118 <input type='text' name='form_to_date' id="form_to_date"
119 class='datepicker form-control'
120 size='10' value='<?php echo attr($to_date) ?>'
121 title='yyyy-mm-dd'>
122 </td>
123 <td class='control-label'><?php echo xlt('Provider'); ?>:</td>
124 <td>
125 <?php
126 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
127 'empty_title' => '-- All Providers --'), $selectedProvider);
129 </td>
130 </table>
131 </div>
132 </td>
133 <td align='left' valign='middle' height="100%">
134 <table style='border-left: 1px solid; width: 100%; height: 100%'>
135 <tr>
136 <td>
137 <div class="text-center">
138 <div class="btn-group" role="group">
139 <a href='#' class='btn btn-default btn-save' onclick='return submitForm();'>
140 <?php echo xlt('Submit'); ?>
141 </a>
142 <a href='' class="btn btn-default btn-refresh" id='new0' onClick=" return top.window.parent.left_nav.loadFrame2('new0', 'RTop', 'reports/daily_summary_report.php')">
143 <?php echo xlt('Reset'); ?>
144 </a>
145 </div>
146 </div>
147 </td>
148 </tr>
149 </table>
150 </td>
151 </tr>
152 </table>
153 <input type='hidden' name='form_refresh' id='form_refresh' value='' />
154 </div>
155 </form>
156 <!-- end of search parameters -->
158 <?php
159 $dateSet = $facilitySet = 0;
160 if (!empty($selectedFromDate) && !empty($selectedToDate)) {
161 $dateSet = 1;
164 if (isset($selectedFacility) && !empty($selectedFacility)) {
165 $facilitySet = 1;
168 // define all the variables as initial blank array
169 $facilities = $totalAppointment = $totalNewPatient = $totalVisit = $totalPayment = $dailySummaryReport = $totalPaid = array();
171 // define all the where condition variable as initial value set 1=1
172 $whereTotalVisitConditions = $whereTotalPaymentConditions = $wherePaidConditions = $whereNewPatientConditions = '1 = 1 ';
174 // fetch all facility from the table
175 $facilityRecords = $facilityService->getAll();
176 foreach ($facilityRecords as $facilityList) {
177 if (1 === $facilitySet && $facilityList['id'] == $selectedFacility) {
178 $facilities[$facilityList['id']] = $facilityList['name'];
181 if (empty($selectedFacility)) {
182 $facilities[$facilityList['id']] = $facilityList['name'];
186 // define provider and facility as null
187 $providerID = $facilityID = null;
188 // define all the bindarray variables as initial blank array
189 $sqlBindArrayAppointment = $sqlBindArrayTotalVisit = $sqlBindArrayTotalPayment = $sqlBindArrayPaid = $sqlBindArrayNewPatient = array();
191 // make all condition on by default today's date
192 if ($dateSet != 1 && $facilitySet != 1) {
193 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) = ? ';
194 array_push($sqlBindArrayNewPatient, date("Y-m-d"));
195 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) = ? ';
196 array_push($sqlBindArrayTotalVisit, date("Y-m-d"));
197 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) = ? ';
198 array_push($sqlBindArrayTotalPayment, date("Y-m-d"));
199 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) = ? ';
200 array_push($sqlBindArrayPaid, date("Y-m-d"));
203 // if search based on facility then append condition for facility search
204 if (1 === $facilitySet) {
205 $facilityID = $selectedFacility;
206 $whereNewPatientConditions .= ' AND `f`.`id` = ?';
207 array_push($sqlBindArrayNewPatient, $selectedFacility);
208 $whereTotalVisitConditions .= ' AND `f`.`id` = ?';
209 array_push($sqlBindArrayTotalVisit, $selectedFacility);
210 $whereTotalPaymentConditions .= ' AND `f`.`id` = ?';
211 array_push($sqlBindArrayTotalPayment, $selectedFacility);
212 $wherePaidConditions .= ' AND `f`.`id` = ?';
213 array_push($sqlBindArrayPaid, $selectedFacility);
216 // if date range wise search then append condition for date search
217 if (1 === $dateSet) {
218 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) BETWEEN ? AND ?';
219 array_push($sqlBindArrayNewPatient, $selectedFromDate, $selectedToDate);
220 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) BETWEEN ? AND ?';
221 array_push($sqlBindArrayTotalVisit, $selectedFromDate, $selectedToDate);
222 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) BETWEEN ? AND ?';
223 array_push($sqlBindArrayTotalPayment, $selectedFromDate, $selectedToDate);
224 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) BETWEEN ? AND ?';
225 array_push($sqlBindArrayPaid, $selectedFromDate, $selectedToDate);
228 // if provider selected then append condition for provider
229 if (isset($selectedProvider) && !empty($selectedProvider)) {
230 $providerID = $selectedProvider;
231 $whereNewPatientConditions .= ' AND `OPE`.`pc_aid` = ?';
232 array_push($sqlBindArrayNewPatient, $selectedProvider);
233 $whereTotalVisitConditions .= ' AND `fc`.`provider_id` = ?';
234 array_push($sqlBindArrayTotalVisit, $selectedProvider);
235 $whereTotalPaymentConditions .= ' AND `fe`.`provider_id` = ?';
236 array_push($sqlBindArrayTotalPayment, $selectedProvider);
237 $wherePaidConditions .= ' AND `fe`.`provider_id` = ?';
238 array_push($sqlBindArrayPaid, $selectedProvider);
241 // pass last parameter as Boolean, which is getting the facility name in the resulted array
242 $totalAppointmentSql = fetchAppointments($from_date, $to_date, null, $providerID, $facilityID);
243 if (count($totalAppointmentSql) > 0) { // check if $totalAppointmentSql array has value
244 foreach ($totalAppointmentSql as $appointment) {
245 $eventDate = $appointment['pc_eventDate'];
246 $facility = $appointment['name'];
247 $providerName = $appointment['ufname'] . ' ' . $appointment['ulname'];
249 // initialize each level of the data structure if it doesn't already exist
250 if (!isset($totalAppointment[$eventDate])) {
251 $totalAppointment[$eventDate] = [];
254 if (!isset($totalAppointment[$eventDate][$facility])) {
255 $totalAppointment[$eventDate][$facility] = [];
258 if (!isset($totalAppointment[$eventDate][$facility][$providerName])) {
259 $totalAppointment[$eventDate][$facility][$providerName] = [];
262 // initialize the number of appointment to 0
263 if (!isset($totalAppointment[$eventDate][$facility][$providerName]['appointments'])) {
264 $totalAppointment[$eventDate][$facility][$providerName]['appointments'] = 0;
267 // increment the number of appointments
268 $totalAppointment[$eventDate][$facility][$providerName]['appointments']++;
272 //Count Total New Patient
273 $newPatientSql = sqlStatement("SELECT `OPE`.`pc_eventDate` , `f`.`name` AS facility_Name , count( * ) AS totalNewPatient, `PD`.`providerID`, CONCAT( `u`.`fname`, ' ', `u`.`lname` ) AS provider_name
274 FROM `patient_data` AS PD
275 LEFT JOIN `openemr_postcalendar_events` AS OPE ON ( `OPE`.`pc_pid` = `PD`.`pid` )
276 LEFT JOIN `facility` AS f ON ( `OPE`.`pc_facility` = `f`.`id` )
277 LEFT JOIN `users` AS u ON ( `OPE`.`pc_aid` = `u`.`id` )
278 WHERE `OPE`.`pc_title` = 'New Patient'
279 AND $whereNewPatientConditions
280 GROUP BY `f`.`id` , `OPE`.`pc_eventDate`,provider_name
281 ORDER BY `OPE`.`pc_eventDate` ASC", $sqlBindArrayNewPatient);
285 while ($totalNewPatientRecord = sqlFetchArray($newPatientSql)) {
286 $totalNewPatient[$totalNewPatientRecord['pc_eventDate']][$totalNewPatientRecord['facility_Name']][$totalNewPatientRecord['provider_name']]['newPatient'] = $totalNewPatientRecord['totalNewPatient'];
289 //Count Total Visit
290 $totalVisitSql = sqlStatement("SELECT DATE( `fc`.`date` ) AS Date,`f`.`name` AS facility_Name, count( * ) AS totalVisit, `fc`.`provider_id`, CONCAT( `u`.`fname`, ' ', `u`.`lname` ) AS provider_name
291 FROM `form_encounter` AS fc
292 LEFT JOIN `facility` AS f ON ( `fc`.`facility_id` = `f`.`id` )
293 LEFT JOIN `users` AS u ON ( `fc`.`provider_id` = `u`.`id` )
294 WHERE $whereTotalVisitConditions
295 GROUP BY `fc`.`facility_id`, DATE( `fc`.`date` ),provider_name ORDER BY DATE( `fc`.`date` ) ASC", $sqlBindArrayTotalVisit);
297 while ($totalVisitRecord = sqlFetchArray($totalVisitSql)) {
298 $totalVisit[$totalVisitRecord['Date']][$totalVisitRecord['facility_Name']][$totalVisitRecord['provider_name']]['visits'] = $totalVisitRecord['totalVisit'];
301 //Count Total Payments for only active records i.e. activity = 1
302 $totalPaymetsSql = sqlStatement("SELECT DATE( `b`.`date` ) AS Date, `f`.`name` AS facilityName, SUM( `b`.`fee` ) AS totalpayment, `fe`.`provider_id`, CONCAT( `u`.`fname`, ' ', `u`.`lname` ) AS provider_name
303 FROM `facility` AS f
304 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
305 LEFT JOIN `billing` AS b ON ( `fe`.`encounter` = `b`.`encounter` )
306 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
307 WHERE `b`.`activity` =1 AND
308 $whereTotalPaymentConditions
309 GROUP BY `b`.`encounter`,Date,provider_name ORDER BY Date ASC", $sqlBindArrayTotalPayment);
311 while ($totalPaymentRecord = sqlFetchArray($totalPaymetsSql)) {
312 $totalPayment[$totalPaymentRecord['Date']][$totalPaymentRecord['facilityName']][$totalPaymentRecord['provider_name']]['payments'] += $totalPaymentRecord['totalpayment'];
315 // total paid amount
316 $totalPaidAmountSql = sqlStatement("SELECT DATE( `p`.`dtime` ) AS Date,`f`.`name` AS facilityName, SUM( `p`.`amount1` ) AS totalPaidAmount, `fe`.`provider_id`, CONCAT( `u`.`fname`, ' ', `u`.`lname` ) AS provider_name
317 FROM `facility` AS f
318 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
319 LEFT JOIN `payments` AS p ON ( `fe`.`encounter` = `p`.`encounter` )
320 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
321 WHERE $wherePaidConditions
322 GROUP BY `p`.`encounter`, Date,provider_name ORDER BY Date ASC", $sqlBindArrayPaid);
325 while ($totalPaidRecord = sqlFetchArray($totalPaidAmountSql)) {
326 $totalPaid[$totalPaidRecord['Date']][$totalPaidRecord['facilityName']][$totalPaidRecord['provider_name']]['paidAmount'] += $totalPaidRecord['totalPaidAmount'];
329 // merge all array recursive in to one array
330 $dailySummaryReport = array_merge_recursive($totalAppointment, $totalNewPatient, $totalVisit, $totalPayment, $totalPaid);
333 <div id="report_results" style="font-size: 12px">
334 <?php echo '<b>' . xlt('From') . '</b> ' . $from_date . ' <b>' . xlt('To') . '</b> ' . $to_date; ?>
336 <table class="flowboard" cellpadding='5' cellspacing='2' id="ds_report">
337 <tr class="head">
339 <td><?php echo xlt('Date'); ?></td>
340 <td><?php echo xlt('Facility'); ?></td>
341 <td><?php echo xlt('Provider'); ?></td>
342 <td><?php echo xlt('Appointments'); ?></td>
343 <td><?php echo xlt('New Patients'); ?></td>
344 <td><?php echo xlt('Visited Patients'); ?></td>
345 <td><?php echo xlt('Total Charges'); ?></td>
346 <td><?php echo xlt('Total Co-Pay'); ?></td>
347 <td><?php echo xlt('Balance Payment'); ?></td>
348 </tr>
349 <?php
350 if (count($dailySummaryReport) > 0) { // check if daily summary array has value
351 foreach ($dailySummaryReport as $date => $dataValue) { // daily summary array which consists different/dynamic values
352 foreach ($facilities as $facility) { // facility array
353 if (isset($dataValue[$facility])) {
354 foreach ($dataValue[$facility] as $provider => $information) { // array which consists different/dynamic values
356 <tr>
357 <td><?php echo text($date) ?></td>
358 <td><?php echo text($facility); ?></td>
359 <td><?php echo text($provider); ?></td>
360 <td><?php echo isset($information['appointments']) ? text($information['appointments']) : 0; ?></td>
361 <td><?php echo isset($information['newPatient']) ? text($information['newPatient']) : 0; ?></td>
362 <td><?php echo isset($information['visits']) ? text($information['visits']) : 0; ?></td>
363 <td align="right"><?php echo isset($information['payments']) ? text(number_format($information['payments'], 2)) : number_format(0, 2); ?></td>
364 <td align="right"><?php echo isset($information['paidAmount']) ? text(number_format($information['paidAmount'], 2)) : number_format(0, 2); ?></td>
365 <td align="right">
366 <?php
367 if (isset($information['payments']) || isset($information['paidAmount'])) {
368 $dueAmount = number_format(str_replace(",", "", $information['payments']) - str_replace(",", "", $information['paidAmount']), 2);
369 } else {
370 $dueAmount = number_format(0, 2);
373 echo text($dueAmount);
375 </td>
376 </tr>
377 <?php
378 if (count($dailySummaryReport) > 0) { // calculate the total count of the appointments, new patient,visits, payments, paid amount and due amount
379 $totalAppointments += $information['appointments'];
380 $totalNewRegisterPatient += $information['newPatient'];
381 $totalVisits += $information['visits'];
382 $totalPayments += str_replace(",", "", $information['payments']);
383 $totalPaidAmount += str_replace(",", "", $information['paidAmount']);
384 $totalDueAmount += $dueAmount;
391 <!--display total count-->
392 <tr class="totalrow">
393 <td><?php echo xlt("Total"); ?></td>
394 <td>-</td>
395 <td>-</td>
396 <td><?php echo text($totalAppointments); ?></td>
397 <td><?php echo text($totalNewRegisterPatient); ?></td>
398 <td><?php echo text($totalVisits); ?></td>
399 <td align="right"><?php echo text(number_format($totalPayments, 2)); ?></td>
400 <td align="right"><?php echo text(number_format($totalPaidAmount, 2)); ?></td>
401 <td align="right"><?php echo text(number_format($totalDueAmount, 2)); ?></td>
402 </tr>
403 <?php
404 } else { // if there are no records then display message
406 <tr>
407 <td colspan="9" style="text-align:center;font-weight:bold;"> <?php echo xlt("There are no record(s) found."); ?></td>
408 </tr><?php
409 } ?>
411 </table>
412 </div>
413 </body>
415 </html>