adding new buttons to reports (#738)
[openemr.git] / interface / reports / daily_summary_report.php
blob485bcf20ab991bd785c9011fdff3a705c7b7a66c
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
30 $fake_register_globals = false;
33 require_once("../globals.php");
34 require_once "$srcdir/options.inc.php";
35 require_once "$srcdir/appointments.inc.php";
37 $facilityService = new \services\FacilityService();
39 $selectedFromDate = isset($_POST['form_from_date']) ? $_POST['form_from_date'] : date('Y-m-d'); // From date filter
40 $selectedToDate = isset($_POST['form_to_date']) ? $_POST['form_to_date'] : date('Y-m-d'); // To date filter
41 $selectedFacility = isset($_POST['form_facility']) ? $_POST['form_facility'] : ""; // facility filter
42 $selectedProvider = isset($_POST['form_provider']) ? $_POST['form_provider'] : ""; // provider filter
44 $from_date = fixDate($selectedFromDate, date('Y-m-d'));
45 $to_date = fixDate($selectedToDate, date('Y-m-d'));
48 <html>
49 <head>
50 <?php html_header_show(); ?>
52 <?php $include_standard_style_js = array("datetimepicker","report_helper.js"); ?>
53 <?php require "{$GLOBALS['srcdir']}/templates/standard_header_template.php"; ?>
55 <script type="text/javascript">
58 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
60 function submitForm() {
61 var fromDate = $("#form_from_date").val();
62 var toDate = $("#form_to_date").val();
64 if (fromDate === '') {
65 alert("<?php echo xls('Please select From date'); ?>");
66 return false;
68 if (toDate === '') {
69 alert("<?php echo xls('Please select To date'); ?>");
70 return false;
72 if (Date.parse(fromDate) > Date.parse(toDate)) {
73 alert("<?php echo xls('From date should be less than To date'); ?>");
74 return false;
76 else {
77 $("#form_refresh").attr("value", "true");
78 $("#report_form").submit();
82 $( document ).ready(function(){
83 $('.datepicker').datetimepicker({
84 <?php $datetimepicker_timepicker = false; ?>
85 <?php $datetimepicker_showseconds = false; ?>
86 <?php $datetimepicker_formatInput = false; ?>
87 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
88 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
89 });
90 });
92 </script>
94 </head>
96 <body class="body_top">
98 <span class='title'><?php echo xlt('Daily Summary Report'); ?></span>
99 <!-- start of search parameters -->
100 <form method='post' name='report_form' id='report_form' action='' onsubmit='return top.restoreSession()'>
101 <div id="report_parameters">
102 <table class="tableonly">
103 <tr>
104 <td width='745px'>
105 <div style='float: left'>
106 <table class='text'>
107 <tr>
108 <td class='control-label'><?php echo xlt('Facility'); ?>:</td>
109 <td><?php dropdown_facility($selectedFacility, 'form_facility', false); ?></td>
110 <td class='control-label'><?php echo xlt('From'); ?>:</td>
111 <td>
112 <input type='text' name='form_from_date' id="form_from_date"
113 class='datepicker form-control'
114 size='10' value='<?php echo attr($from_date) ?>'
115 title='yyyy-mm-dd'>
116 </td>
117 <td class='control-label'><?php echo xlt('To'); ?>:</td>
118 <td>
119 <input type='text' name='form_to_date' id="form_to_date"
120 class='datepicker form-control'
121 size='10' value='<?php echo attr($to_date) ?>'
122 title='yyyy-mm-dd'>
123 </td>
124 <td class='control-label'><?php echo xlt('Provider'); ?>:</td>
125 <td>
126 <?php
127 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
128 'empty_title' => '-- All Providers --'), $selectedProvider);
130 </td>
131 </table>
132 </div>
133 </td>
134 <td align='left' valign='middle' height="100%">
135 <table style='border-left: 1px solid; width: 100%; height: 100%'>
136 <tr>
137 <td>
138 <div class="text-center">
139 <div class="btn-group" role="group">
140 <a href='#' class='btn btn-default btn-save' onclick='return submitForm();'>
141 <?php echo xlt('Submit'); ?>
142 </a>
143 <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')">
144 <?php echo xlt('Reset'); ?>
145 </a>
146 </div>
147 </div>
148 </td>
149 </tr>
150 </table>
151 </td>
152 </tr>
153 </table>
154 <input type='hidden' name='form_refresh' id='form_refresh' value='' />
155 </div>
156 </form>
157 <!-- end of search parameters -->
159 <?php
160 $dateSet = $facilitySet = 0;
161 if (!empty($selectedFromDate) && !empty($selectedToDate)) {
162 $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'];
180 if (empty($selectedFacility)) {
181 $facilities[$facilityList['id']] = $facilityList['name'];
185 // define provider and facility as null
186 $providerID = $facilityID = NULL;
187 // define all the bindarray variables as initial blank array
188 $sqlBindArrayAppointment = $sqlBindArrayTotalVisit = $sqlBindArrayTotalPayment = $sqlBindArrayPaid = $sqlBindArrayNewPatient = array();
190 // make all condition on by default today's date
191 if ($dateSet != 1 && $facilitySet != 1) {
192 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) = ? ';
193 array_push($sqlBindArrayNewPatient, date("Y-m-d"));
194 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) = ? ';
195 array_push($sqlBindArrayTotalVisit, date("Y-m-d"));
196 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) = ? ';
197 array_push($sqlBindArrayTotalPayment, date("Y-m-d"));
198 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) = ? ';
199 array_push($sqlBindArrayPaid, date("Y-m-d"));
202 // if search based on facility then append condition for facility search
203 if (1 === $facilitySet) {
204 $facilityID = $selectedFacility;
205 $whereNewPatientConditions .= ' AND `f`.`id` = ?';
206 array_push($sqlBindArrayNewPatient, $selectedFacility);
207 $whereTotalVisitConditions .= ' AND `f`.`id` = ?';
208 array_push($sqlBindArrayTotalVisit, $selectedFacility);
209 $whereTotalPaymentConditions .= ' AND `f`.`id` = ?';
210 array_push($sqlBindArrayTotalPayment, $selectedFacility);
211 $wherePaidConditions .= ' AND `f`.`id` = ?';
212 array_push($sqlBindArrayPaid, $selectedFacility);
215 // if date range wise search then append condition for date search
216 if (1 === $dateSet) {
217 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) BETWEEN ? AND ?';
218 array_push($sqlBindArrayNewPatient, $selectedFromDate, $selectedToDate);
219 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) BETWEEN ? AND ?';
220 array_push($sqlBindArrayTotalVisit, $selectedFromDate, $selectedToDate);
221 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) BETWEEN ? AND ?';
222 array_push($sqlBindArrayTotalPayment, $selectedFromDate, $selectedToDate);
223 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) BETWEEN ? AND ?';
224 array_push($sqlBindArrayPaid, $selectedFromDate, $selectedToDate);
227 // if provider selected then append condition for provider
228 if (isset($selectedProvider) && !empty($selectedProvider)) {
229 $providerID = $selectedProvider;
230 $whereNewPatientConditions .= ' AND `OPE`.`pc_aid` = ?';
231 array_push($sqlBindArrayNewPatient, $selectedProvider);
232 $whereTotalVisitConditions .= ' AND `fc`.`provider_id` = ?';
233 array_push($sqlBindArrayTotalVisit, $selectedProvider);
234 $whereTotalPaymentConditions .= ' AND `fe`.`provider_id` = ?';
235 array_push($sqlBindArrayTotalPayment, $selectedProvider);
236 $wherePaidConditions .= ' AND `fe`.`provider_id` = ?';
237 array_push($sqlBindArrayPaid, $selectedProvider);
240 // pass last parameter as Boolean, which is getting the facility name in the resulted array
241 $totalAppointmentSql = fetchAppointments($from_date, $to_date, null, $providerID, $facilityID);
242 if (count($totalAppointmentSql) > 0) { // check if $totalAppointmentSql array has value
243 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] = [];
253 if (!isset($totalAppointment[$eventDate][$facility])) {
254 $totalAppointment[$eventDate][$facility] = [];
256 if (!isset($totalAppointment[$eventDate][$facility][$providerName])) {
257 $totalAppointment[$eventDate][$facility][$providerName] = [];
259 // initialize the number of appointment to 0
260 if (!isset($totalAppointment[$eventDate][$facility][$providerName]['appointments'])) {
261 $totalAppointment[$eventDate][$facility][$providerName]['appointments'] = 0;
263 // increment the number of appointments
264 $totalAppointment[$eventDate][$facility][$providerName]['appointments']++;
268 //Count Total New Patient
269 $newPatientSql = sqlStatement("SELECT `OPE`.`pc_eventDate` , `f`.`name` AS facility_Name , count( * ) AS totalNewPatient, `PD`.`providerID`, CONCAT( `u`.`fname`, ' ', `u`.`lname` ) AS provider_name
270 FROM `patient_data` AS PD
271 LEFT JOIN `openemr_postcalendar_events` AS OPE ON ( `OPE`.`pc_pid` = `PD`.`pid` )
272 LEFT JOIN `facility` AS f ON ( `OPE`.`pc_facility` = `f`.`id` )
273 LEFT JOIN `users` AS u ON ( `OPE`.`pc_aid` = `u`.`id` )
274 WHERE `OPE`.`pc_title` = 'New Patient'
275 AND $whereNewPatientConditions
276 GROUP BY `f`.`id` , `OPE`.`pc_eventDate`,provider_name
277 ORDER BY `OPE`.`pc_eventDate` ASC", $sqlBindArrayNewPatient);
281 while ($totalNewPatientRecord = sqlFetchArray($newPatientSql)) {
282 $totalNewPatient[$totalNewPatientRecord['pc_eventDate']][$totalNewPatientRecord['facility_Name']][$totalNewPatientRecord['provider_name']]['newPatient'] = $totalNewPatientRecord['totalNewPatient'];
285 //Count Total Visit
286 $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
287 FROM `form_encounter` AS fc
288 LEFT JOIN `facility` AS f ON ( `fc`.`facility_id` = `f`.`id` )
289 LEFT JOIN `users` AS u ON ( `fc`.`provider_id` = `u`.`id` )
290 WHERE $whereTotalVisitConditions
291 GROUP BY `fc`.`facility_id`, DATE( `fc`.`date` ),provider_name ORDER BY DATE( `fc`.`date` ) ASC", $sqlBindArrayTotalVisit);
293 while ($totalVisitRecord = sqlFetchArray($totalVisitSql)) {
294 $totalVisit[$totalVisitRecord['Date']][$totalVisitRecord['facility_Name']][$totalVisitRecord['provider_name']]['visits'] = $totalVisitRecord['totalVisit'];
297 //Count Total Payments for only active records i.e. activity = 1
298 $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
299 FROM `facility` AS f
300 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
301 LEFT JOIN `billing` AS b ON ( `fe`.`encounter` = `b`.`encounter` )
302 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
303 WHERE `b`.`activity` =1 AND
304 $whereTotalPaymentConditions
305 GROUP BY `b`.`encounter`,Date,provider_name ORDER BY Date ASC", $sqlBindArrayTotalPayment);
307 while ($totalPaymentRecord = sqlFetchArray($totalPaymetsSql)) {
308 $totalPayment[$totalPaymentRecord['Date']][$totalPaymentRecord['facilityName']][$totalPaymentRecord['provider_name']]['payments'] += $totalPaymentRecord['totalpayment'];
311 // total paid amount
312 $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
313 FROM `facility` AS f
314 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
315 LEFT JOIN `payments` AS p ON ( `fe`.`encounter` = `p`.`encounter` )
316 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
317 WHERE $wherePaidConditions
318 GROUP BY `p`.`encounter`, Date,provider_name ORDER BY Date ASC", $sqlBindArrayPaid);
321 while ($totalPaidRecord = sqlFetchArray($totalPaidAmountSql)) {
322 $totalPaid[$totalPaidRecord['Date']][$totalPaidRecord['facilityName']][$totalPaidRecord['provider_name']]['paidAmount'] += $totalPaidRecord['totalPaidAmount'];
325 // merge all array recursive in to one array
326 $dailySummaryReport = array_merge_recursive($totalAppointment, $totalNewPatient, $totalVisit, $totalPayment, $totalPaid);
329 <div id="report_results" style="font-size: 12px">
330 <?php echo '<b>' . xlt('From') . '</b> ' . $from_date . ' <b>' . xlt('To') . '</b> ' . $to_date; ?>
332 <table class="flowboard" cellpadding='5' cellspacing='2' id="ds_report">
333 <tr class="head">
335 <td><?php echo xlt('Date'); ?></td>
336 <td><?php echo xlt('Facility'); ?></td>
337 <td><?php echo xlt('Provider'); ?></td>
338 <td><?php echo xlt('Appointments'); ?></td>
339 <td><?php echo xlt('New Patients'); ?></td>
340 <td><?php echo xlt('Visited Patients'); ?></td>
341 <td><?php echo xlt('Total Charges'); ?></td>
342 <td><?php echo xlt('Total Co-Pay'); ?></td>
343 <td><?php echo xlt('Balance Payment'); ?></td>
344 </tr>
345 <?php
346 if (count($dailySummaryReport) > 0) { // check if daily summary array has value
347 foreach ($dailySummaryReport as $date => $dataValue) { // daily summary array which consists different/dynamic values
348 foreach ($facilities as $facility) { // facility array
349 if (isset($dataValue[$facility])) {
350 foreach ($dataValue[$facility] as $provider => $information) { // array which consists different/dynamic values
352 <tr>
353 <td><?php echo text($date) ?></td>
354 <td><?php echo text($facility); ?></td>
355 <td><?php echo text($provider); ?></td>
356 <td><?php echo isset($information['appointments']) ? text($information['appointments']) : 0; ?></td>
357 <td><?php echo isset($information['newPatient']) ? text($information['newPatient']) : 0; ?></td>
358 <td><?php echo isset($information['visits']) ? text($information['visits']) : 0; ?></td>
359 <td align="right"><?php echo isset($information['payments']) ? text(number_format($information['payments'], 2)) : number_format(0, 2); ?></td>
360 <td align="right"><?php echo isset($information['paidAmount']) ? text(number_format($information['paidAmount'], 2)) : number_format(0, 2); ?></td>
361 <td align="right">
362 <?php
363 if (isset($information['payments']) || isset($information['paidAmount'])) {
364 $dueAmount = number_format(str_replace(",", "", $information['payments']) - str_replace(",", "", $information['paidAmount']), 2);
365 } else {
366 $dueAmount = number_format(0, 2);
368 echo text($dueAmount);
370 </td>
371 </tr>
372 <?php
373 if (count($dailySummaryReport) > 0) { // calculate the total count of the appointments, new patient,visits, payments, paid amount and due amount
374 $totalAppointments += $information['appointments'];
375 $totalNewRegisterPatient += $information['newPatient'];
376 $totalVisits += $information['visits'];
377 $totalPayments += str_replace(",", "", $information['payments']);
378 $totalPaidAmount += str_replace(",", "", $information['paidAmount']);
379 $totalDueAmount += $dueAmount;
386 <!--display total count-->
387 <tr class="totalrow">
388 <td><?php echo xlt("Total"); ?></td>
389 <td>-</td>
390 <td>-</td>
391 <td><?php echo text($totalAppointments); ?></td>
392 <td><?php echo text($totalNewRegisterPatient); ?></td>
393 <td><?php echo text($totalVisits); ?></td>
394 <td align="right"><?php echo text(number_format($totalPayments, 2)); ?></td>
395 <td align="right"><?php echo text(number_format($totalPaidAmount, 2)); ?></td>
396 <td align="right"><?php echo text(number_format($totalDueAmount, 2)); ?></td>
397 </tr>
398 <?php
399 } else { // if there are no records then display message
401 <tr>
402 <td colspan="9" style="text-align:center;font-weight:bold;"> <?php echo xlt("There are no record(s) found."); ?></td>
403 </tr>
404 <?php } ?>
406 </table>
407 </div>
408 </body>
410 </html>