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>;.
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;
31 $sanitize_all_escapes = true;
33 require_once("../globals.php");
34 require_once "$srcdir/options.inc.php";
35 require_once "$srcdir/appointments.inc.php";
38 $selectedFromDate = isset($_POST['form_from_date']) ?
$_POST['form_from_date'] : date('Y-m-d'); // From date filter
39 $selectedToDate = isset($_POST['form_to_date']) ?
$_POST['form_to_date'] : date('Y-m-d'); // To date filter
40 $selectedFacility = isset($_POST['form_facility']) ?
$_POST['form_facility'] : ""; // facility filter
41 $selectedProvider = isset($_POST['form_provider']) ?
$_POST['form_provider'] : ""; // provider filter
43 $from_date = fixDate($selectedFromDate, date('Y-m-d'));
44 $to_date = fixDate($selectedToDate, date('Y-m-d'));
49 <?php
html_header_show(); ?
>
50 <link rel
="stylesheet" href
="<?php echo $css_header; ?>" type
="text/css">
51 <link rel
="stylesheet" href
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
53 <script type
="text/javascript" src
="../../library/textformat.js?v=<?php echo $v_js_includes; ?>"></script
>
54 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script
>
55 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script
>
56 <script type
="text/javascript">
59 var mypcc
= '<?php echo $GLOBALS['phone_country_code
'] ?>';
61 function submitForm() {
62 var fromDate
= $
("#form_from_date").val();
63 var toDate
= $
("#form_to_date").val();
65 if (fromDate
=== '') {
66 alert("<?php echo xls('Please select From date'); ?>");
70 alert("<?php echo xls('Please select To date'); ?>");
73 if (Date
.parse(fromDate
) > Date
.parse(toDate
)) {
74 alert("<?php echo xls('From date should be less than To date'); ?>");
78 $
("#form_refresh").attr("value", "true");
79 $
("#report_form").submit();
83 $
( document
).ready(function(){
84 $
('.datepicker').datetimepicker({
85 <?php
$datetimepicker_timepicker = 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 ?>
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">
105 <div style
='float: left'>
108 <td
class='label'><?php
echo xlt('Facility'); ?
>:</td
>
109 <td
><?php
dropdown_facility($selectedFacility, 'form_facility', false); ?
></td
>
110 <td
class='label'><?php
echo xlt('From'); ?
>:</td
>
112 <input type
='text' name
='form_from_date' id
="form_from_date"
114 size
='10' value
='<?php echo attr($from_date) ?>'
117 <td
class='label'><?php
echo xlt('To'); ?
>:</td
>
119 <input type
='text' name
='form_to_date' id
="form_to_date"
121 size
='10' value
='<?php echo attr($to_date) ?>'
127 <td
class='label'><?php
echo xlt('Provider'); ?
>:</td
>
130 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
131 'empty_title' => '-- All Providers --'), $selectedProvider);
134 <td align
='left' valign
='middle' height
="100%">
135 <table style
='border-left: 1px solid; width: 100%; height: 100%'>
138 <div style
='margin-left: 15px'>
139 <a href
='#' class='css_button' onclick
='return submitForm();'>
140 <span
> <?php
echo xlt('Submit'); ?
> </span
>
142 <a href
='' class="css_button" id
='new0' onClick
=" return top.window.parent.left_nav.loadFrame2('new0', 'RTop', 'reports/daily_summary_report.php')">
143 <span
><?php
echo xlt('Reset'); ?
></span
>
152 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
='' />
155 <!-- end of search parameters
-->
158 $dateSet = $facilitySet = 0;
159 if (!empty($selectedFromDate) && !empty($selectedToDate)) {
162 if (isset($selectedFacility) && !empty($selectedFacility)) {
166 // define all the variables as initial blank array
167 $facilities = $totalAppointment = $totalNewPatient = $totalVisit = $totalPayment = $dailySummaryReport = $totalPaid = array();
169 // define all the where condition variable as initial value set 1=1
170 $whereTotalVisitConditions = $whereTotalPaymentConditions = $wherePaidConditions = $whereNewPatientConditions = '1 = 1 ';
172 // fetch all facility from the table
173 $facilityReacords = sqlStatement("SELECT `id`,`name` from facility");
174 while ($facilityList = sqlFetchArray($facilityReacords)) {
175 if (1 === $facilitySet && $facilityList['id'] == $selectedFacility) {
176 $facilities[$facilityList['id']] = $facilityList['name'];
178 if (empty($selectedFacility)) {
179 $facilities[$facilityList['id']] = $facilityList['name'];
183 // define provider and facility as null
184 $providerID = $facilityID = NULL;
185 // define all the bindarray variables as initial blank array
186 $sqlBindArrayAppointment = $sqlBindArrayTotalVisit = $sqlBindArrayTotalPayment = $sqlBindArrayPaid = $sqlBindArrayNewPatient = array();
188 // make all condition on by default today's date
189 if ($dateSet != 1 && $facilitySet != 1) {
190 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) = ? ';
191 array_push($sqlBindArrayNewPatient, date("Y-m-d"));
192 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) = ? ';
193 array_push($sqlBindArrayTotalVisit, date("Y-m-d"));
194 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) = ? ';
195 array_push($sqlBindArrayTotalPayment, date("Y-m-d"));
196 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) = ? ';
197 array_push($sqlBindArrayPaid, date("Y-m-d"));
200 // if search based on facility then append condition for facility search
201 if (1 === $facilitySet) {
202 $facilityID = $selectedFacility;
203 $whereNewPatientConditions .= ' AND `f`.`id` = ?';
204 array_push($sqlBindArrayNewPatient, $selectedFacility);
205 $whereTotalVisitConditions .= ' AND `f`.`id` = ?';
206 array_push($sqlBindArrayTotalVisit, $selectedFacility);
207 $whereTotalPaymentConditions .= ' AND `f`.`id` = ?';
208 array_push($sqlBindArrayTotalPayment, $selectedFacility);
209 $wherePaidConditions .= ' AND `f`.`id` = ?';
210 array_push($sqlBindArrayPaid, $selectedFacility);
213 // if date range wise search then append condition for date search
214 if (1 === $dateSet) {
215 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) BETWEEN ? AND ?';
216 array_push($sqlBindArrayNewPatient, $selectedFromDate, $selectedToDate);
217 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) BETWEEN ? AND ?';
218 array_push($sqlBindArrayTotalVisit, $selectedFromDate, $selectedToDate);
219 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) BETWEEN ? AND ?';
220 array_push($sqlBindArrayTotalPayment, $selectedFromDate, $selectedToDate);
221 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) BETWEEN ? AND ?';
222 array_push($sqlBindArrayPaid, $selectedFromDate, $selectedToDate);
225 // if provider selected then append condition for provider
226 if (isset($selectedProvider) && !empty($selectedProvider)) {
227 $providerID = $selectedProvider;
228 $whereNewPatientConditions .= ' AND `OPE`.`pc_aid` = ?';
229 array_push($sqlBindArrayNewPatient, $selectedProvider);
230 $whereTotalVisitConditions .= ' AND `fc`.`provider_id` = ?';
231 array_push($sqlBindArrayTotalVisit, $selectedProvider);
232 $whereTotalPaymentConditions .= ' AND `fe`.`provider_id` = ?';
233 array_push($sqlBindArrayTotalPayment, $selectedProvider);
234 $wherePaidConditions .= ' AND `fe`.`provider_id` = ?';
235 array_push($sqlBindArrayPaid, $selectedProvider);
238 // pass last parameter as Boolean, which is getting the facility name in the resulted array
239 $totalAppointmentSql = fetchAppointments($from_date, $to_date, null, $providerID, $facilityID);
240 if (count($totalAppointmentSql) > 0) { // check if $totalAppointmentSql array has value
241 foreach ($totalAppointmentSql as $appointment) {
243 $eventDate = $appointment['pc_eventDate'];
244 $facility = $appointment['name'];
245 $providerName = $appointment['ufname'] . ' ' . $appointment['ulname'];
247 // initialize each level of the data structure if it doesn't already exist
248 if (!isset($totalAppointment[$eventDate])) {
249 $totalAppointment[$eventDate] = [];
251 if (!isset($totalAppointment[$eventDate][$facility])) {
252 $totalAppointment[$eventDate][$facility] = [];
254 if (!isset($totalAppointment[$eventDate][$facility][$providerName])) {
255 $totalAppointment[$eventDate][$facility][$providerName] = [];
257 // initialize the number of appointment to 0
258 if (!isset($totalAppointment[$eventDate][$facility][$providerName]['appointments'])) {
259 $totalAppointment[$eventDate][$facility][$providerName]['appointments'] = 0;
261 // increment the number of appointments
262 $totalAppointment[$eventDate][$facility][$providerName]['appointments']++
;
266 //Count Total New Patient
267 $newPatientSql = sqlStatement("SELECT `OPE`.`pc_eventDate` , `f`.`name` AS facility_Name , count( * ) AS totalNewPatient, `PD`.`providerID`, CONCAT( `u`.`fname`, ' ', `u`.`lname` ) AS provider_name
268 FROM `patient_data` AS PD
269 LEFT JOIN `openemr_postcalendar_events` AS OPE ON ( `OPE`.`pc_pid` = `PD`.`pid` )
270 LEFT JOIN `facility` AS f ON ( `OPE`.`pc_facility` = `f`.`id` )
271 LEFT JOIN `users` AS u ON ( `OPE`.`pc_aid` = `u`.`id` )
272 WHERE `OPE`.`pc_title` = 'New Patient'
273 AND $whereNewPatientConditions
274 GROUP BY `f`.`id` , `OPE`.`pc_eventDate`,provider_name
275 ORDER BY `OPE`.`pc_eventDate` ASC", $sqlBindArrayNewPatient);
279 while ($totalNewPatientRecord = sqlFetchArray($newPatientSql)) {
280 $totalNewPatient[$totalNewPatientRecord['pc_eventDate']][$totalNewPatientRecord['facility_Name']][$totalNewPatientRecord['provider_name']]['newPatient'] = $totalNewPatientRecord['totalNewPatient'];
284 $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
285 FROM `form_encounter` AS fc
286 LEFT JOIN `facility` AS f ON ( `fc`.`facility_id` = `f`.`id` )
287 LEFT JOIN `users` AS u ON ( `fc`.`provider_id` = `u`.`id` )
288 WHERE $whereTotalVisitConditions
289 GROUP BY `fc`.`facility_id`, DATE( `fc`.`date` ),provider_name ORDER BY DATE( `fc`.`date` ) ASC", $sqlBindArrayTotalVisit);
291 while ($totalVisitRecord = sqlFetchArray($totalVisitSql)) {
292 $totalVisit[$totalVisitRecord['Date']][$totalVisitRecord['facility_Name']][$totalVisitRecord['provider_name']]['visits'] = $totalVisitRecord['totalVisit'];
295 //Count Total Payments for only active records i.e. activity = 1
296 $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
298 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
299 LEFT JOIN `billing` AS b ON ( `fe`.`encounter` = `b`.`encounter` )
300 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
301 WHERE `b`.`activity` =1 AND
302 $whereTotalPaymentConditions
303 GROUP BY `b`.`encounter`,Date,provider_name ORDER BY Date ASC", $sqlBindArrayTotalPayment);
305 while ($totalPaymentRecord = sqlFetchArray($totalPaymetsSql)) {
306 $totalPayment[$totalPaymentRecord['Date']][$totalPaymentRecord['facilityName']][$totalPaymentRecord['provider_name']]['payments'] +
= $totalPaymentRecord['totalpayment'];
310 $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
312 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
313 LEFT JOIN `payments` AS p ON ( `fe`.`encounter` = `p`.`encounter` )
314 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
315 WHERE $wherePaidConditions
316 GROUP BY `p`.`encounter`, Date,provider_name ORDER BY Date ASC", $sqlBindArrayPaid);
319 while ($totalPaidRecord = sqlFetchArray($totalPaidAmountSql)) {
320 $totalPaid[$totalPaidRecord['Date']][$totalPaidRecord['facilityName']][$totalPaidRecord['provider_name']]['paidAmount'] +
= $totalPaidRecord['totalPaidAmount'];
323 // merge all array recursive in to one array
324 $dailySummaryReport = array_merge_recursive($totalAppointment, $totalNewPatient, $totalVisit, $totalPayment, $totalPaid);
327 <div id
="report_results" style
="font-size: 12px">
328 <?php
echo '<b>' . xlt('From') . '</b> ' . $from_date . ' <b>' . xlt('To') . '</b> ' . $to_date; ?
>
330 <table
class="flowboard" cellpadding
='5' cellspacing
='2' id
="ds_report">
333 <td
><?php
echo xlt('Date'); ?
></td
>
334 <td
><?php
echo xlt('Facility'); ?
></td
>
335 <td
><?php
echo xlt('Provider'); ?
></td
>
336 <td
><?php
echo xlt('Appointments'); ?
></td
>
337 <td
><?php
echo xlt('New Patients'); ?
></td
>
338 <td
><?php
echo xlt('Visited Patients'); ?
></td
>
339 <td
><?php
echo xlt('Total Charges'); ?
></td
>
340 <td
><?php
echo xlt('Total Co-Pay'); ?
></td
>
341 <td
><?php
echo xlt('Balance Payment'); ?
></td
>
344 if (count($dailySummaryReport) > 0) { // check if daily summary array has value
345 foreach ($dailySummaryReport as $date => $dataValue) { // daily summary array which consists different/dynamic values
346 foreach ($facilities as $facility) { // facility array
347 if (isset($dataValue[$facility])) {
348 foreach ($dataValue[$facility] as $provider => $information) { // array which consists different/dynamic values
351 <td
><?php
echo text($date) ?
></td
>
352 <td
><?php
echo text($facility); ?
></td
>
353 <td
><?php
echo text($provider); ?
></td
>
354 <td
><?php
echo isset($information['appointments']) ?
text($information['appointments']) : 0; ?
></td
>
355 <td
><?php
echo isset($information['newPatient']) ?
text($information['newPatient']) : 0; ?
></td
>
356 <td
><?php
echo isset($information['visits']) ?
text($information['visits']) : 0; ?
></td
>
357 <td align
="right"><?php
echo isset($information['payments']) ?
text(number_format($information['payments'], 2)) : number_format(0, 2); ?
></td
>
358 <td align
="right"><?php
echo isset($information['paidAmount']) ?
text(number_format($information['paidAmount'], 2)) : number_format(0, 2); ?
></td
>
361 if (isset($information['payments']) ||
isset($information['paidAmount'])) {
362 $dueAmount = number_format(str_replace(",", "", $information['payments']) - str_replace(",", "", $information['paidAmount']), 2);
364 $dueAmount = number_format(0, 2);
366 echo text($dueAmount);
371 if (count($dailySummaryReport) > 0) { // calculate the total count of the appointments, new patient,visits, payments, paid amount and due amount
372 $totalAppointments +
= $information['appointments'];
373 $totalNewRegisterPatient +
= $information['newPatient'];
374 $totalVisits +
= $information['visits'];
375 $totalPayments +
= str_replace(",", "", $information['payments']);
376 $totalPaidAmount +
= str_replace(",", "", $information['paidAmount']);
377 $totalDueAmount +
= $dueAmount;
384 <!--display total count
-->
385 <tr
class="totalrow">
386 <td
><?php
echo xlt("Total"); ?
></td
>
389 <td
><?php
echo text($totalAppointments); ?
></td
>
390 <td
><?php
echo text($totalNewRegisterPatient); ?
></td
>
391 <td
><?php
echo text($totalVisits); ?
></td
>
392 <td align
="right"><?php
echo text(number_format($totalPayments, 2)); ?
></td
>
393 <td align
="right"><?php
echo text(number_format($totalPaidAmount, 2)); ?
></td
>
394 <td align
="right"><?php
echo text(number_format($totalDueAmount, 2)); ?
></td
>
397 } else { // if there are no records then display message
400 <td colspan
="9" style
="text-align:center;font-weight:bold;"> <?php
echo xlt("There are no record(s) found."); ?
></td
>