4 * 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.
10 * @link http://www.open-emr.org
11 * @author Rishabh Software
12 * @author Brady Miller <brady.g.miller@gmail.com>
13 * @copyright Copyright (c) 2016 Rishabh Software
14 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
15 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
18 require_once("../globals.php");
19 require_once "$srcdir/options.inc.php";
20 require_once "$srcdir/appointments.inc.php";
22 use OpenEMR\Common\Csrf\CsrfUtils
;
23 use OpenEMR\Core\Header
;
24 use OpenEMR\Services\FacilityService
;
27 if (!CsrfUtils
::verifyCsrfToken($_POST["csrf_token_form"])) {
28 CsrfUtils
::csrfNotVerified();
32 $facilityService = new FacilityService();
34 $from_date = isset($_POST['form_from_date']) ?
DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d'); // From date filter
35 $to_date = isset($_POST['form_to_date']) ?
DateToYYYYMMDD($_POST['form_to_date']) : date('Y-m-d'); // To date filter
36 $selectedFacility = isset($_POST['form_facility']) ?
$_POST['form_facility'] : ""; // facility filter
37 $selectedProvider = isset($_POST['form_provider']) ?
$_POST['form_provider'] : ""; // provider filter
43 <title
><?php
echo xlt('Daily Summary Report'); ?
></title
>
45 <?php Header
::setupHeader(['datetime-picker', 'report-helper']); ?
>
48 function submitForm() {
49 var fromDate
= $
("#form_from_date").val();
50 var toDate
= $
("#form_to_date").val();
52 if (fromDate
=== '') {
53 alert(<?php
echo xlj('Please select From date'); ?
>);
57 alert(<?php
echo xlj('Please select To date'); ?
>);
60 if (Date
.parse(fromDate
) > Date
.parse(toDate
)) {
61 alert(<?php
echo xlj('From date should be less than To date'); ?
>);
65 $
("#form_refresh").attr("value", "true");
66 $
("#report_form").submit();
71 $
('.datepicker').datetimepicker({
72 <?php
$datetimepicker_timepicker = false; ?
>
73 <?php
$datetimepicker_showseconds = false; ?
>
74 <?php
$datetimepicker_formatInput = true; ?
>
75 <?php
require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?
>
76 <?php
// can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
84 <body
class="body_top">
86 <span
class='title'><?php
echo xlt('Daily Summary Report'); ?
></span
>
87 <!-- start of search parameters
-->
88 <form method
='post' name
='report_form' id
='report_form' action
='' onsubmit
='return top.restoreSession()'>
89 <input type
="hidden" name
="csrf_token_form" value
="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
90 <div id
="report_parameters">
91 <table
class="tableonly">
94 <div style
='float: left'>
97 <td
class='col-form-label'><?php
echo xlt('Facility'); ?
>:</td
>
98 <td
><?php
dropdown_facility($selectedFacility, 'form_facility', false); ?
></td
>
99 <td
class='col-form-label'><?php
echo xlt('From'); ?
>:</td
>
101 <input type
='text' name
='form_from_date' id
="form_from_date"
102 class='datepicker form-control'
103 size
='10' value
='<?php echo attr(oeFormatShortDate($from_date)); ?>'>
105 <td
class='col-form-label'><?php
echo xlt('To{{Range}}'); ?
>:</td
>
107 <input type
='text' name
='form_to_date' id
="form_to_date"
108 class='datepicker form-control'
109 size
='10' value
='<?php echo attr(oeFormatShortDate($to_date)); ?>'>
111 <td
class='col-form-label'><?php
echo xlt('Provider'); ?
>:</td
>
114 generate_form_field(array('data_type' => 10, 'field_id' => 'provider',
115 'empty_title' => '-- All Providers --'), $selectedProvider);
121 <td
class='h-100' align
='left' valign
='middle'>
122 <table
class='w-100 h-100' style
='border-left: 1px solid;'>
125 <div
class="text-center">
126 <div
class="btn-group" role
="group">
127 <a href
='#' class='btn btn-secondary btn-save' onclick
='return submitForm();'>
128 <?php
echo xlt('Submit'); ?
>
130 <a href
='' class="btn btn-secondary btn-refresh" id
='new0' onClick
=" top.restoreSession(); window.location = window.location.href;">
131 <?php
echo xlt('Reset'); ?
>
141 <input type
='hidden' name
='form_refresh' id
='form_refresh' value
='' />
144 <!-- end of search parameters
-->
147 $dateSet = $facilitySet = 0;
148 if (!empty($from_date) && !empty($to_date)) {
152 if (isset($selectedFacility) && !empty($selectedFacility)) {
156 // define all the variables as initial blank array
157 $facilities = $totalAppointment = $totalNewPatient = $totalVisit = $totalPayment = $dailySummaryReport = $totalPaid = array();
159 // define all the where condition variable as initial value set 1=1
160 $whereTotalVisitConditions = $whereTotalPaymentConditions = $wherePaidConditions = $whereNewPatientConditions = '1 = 1 ';
162 // fetch all facility from the table
163 $facilityRecords = $facilityService->getAllFacility();
164 foreach ($facilityRecords as $facilityList) {
165 if (1 === $facilitySet && $facilityList['id'] == $selectedFacility) {
166 $facilities[$facilityList['id']] = $facilityList['name'];
169 if (empty($selectedFacility)) {
170 $facilities[$facilityList['id']] = $facilityList['name'];
174 // define provider and facility as null
175 $providerID = $facilityID = null;
176 // define all the bindarray variables as initial blank array
177 $sqlBindArrayAppointment = $sqlBindArrayTotalVisit = $sqlBindArrayTotalPayment = $sqlBindArrayPaid = $sqlBindArrayNewPatient = array();
179 // make all condition on by default today's date
180 if ($dateSet != 1 && $facilitySet != 1) {
181 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) = ? ';
182 array_push($sqlBindArrayNewPatient, date("Y-m-d"));
183 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) = ? ';
184 array_push($sqlBindArrayTotalVisit, date("Y-m-d"));
185 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) = ? ';
186 array_push($sqlBindArrayTotalPayment, date("Y-m-d"));
187 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) = ? ';
188 array_push($sqlBindArrayPaid, date("Y-m-d"));
191 // if search based on facility then append condition for facility search
192 if (1 === $facilitySet) {
193 $facilityID = $selectedFacility;
194 $whereNewPatientConditions .= ' AND `f`.`id` = ?';
195 array_push($sqlBindArrayNewPatient, $selectedFacility);
196 $whereTotalVisitConditions .= ' AND `f`.`id` = ?';
197 array_push($sqlBindArrayTotalVisit, $selectedFacility);
198 $whereTotalPaymentConditions .= ' AND `f`.`id` = ?';
199 array_push($sqlBindArrayTotalPayment, $selectedFacility);
200 $wherePaidConditions .= ' AND `f`.`id` = ?';
201 array_push($sqlBindArrayPaid, $selectedFacility);
204 // if date range wise search then append condition for date search
205 if (1 === $dateSet) {
206 $whereNewPatientConditions .= ' AND DATE(`OPE`.`pc_eventDate`) BETWEEN ? AND ?';
207 array_push($sqlBindArrayNewPatient, $from_date, $to_date);
208 $whereTotalVisitConditions .= ' AND DATE(`fc`.`date`) BETWEEN ? AND ?';
209 array_push($sqlBindArrayTotalVisit, $from_date, $to_date);
210 $whereTotalPaymentConditions .= ' AND DATE(`b`.`date`) BETWEEN ? AND ?';
211 array_push($sqlBindArrayTotalPayment, $from_date, $to_date);
212 $wherePaidConditions .= ' AND DATE(`p`.`dtime`) BETWEEN ? AND ?';
213 array_push($sqlBindArrayPaid, $from_date, $to_date);
216 // if provider selected then append condition for provider
217 if (isset($selectedProvider) && !empty($selectedProvider)) {
218 $providerID = $selectedProvider;
219 $whereNewPatientConditions .= ' AND `OPE`.`pc_aid` = ?';
220 array_push($sqlBindArrayNewPatient, $selectedProvider);
221 $whereTotalVisitConditions .= ' AND `fc`.`provider_id` = ?';
222 array_push($sqlBindArrayTotalVisit, $selectedProvider);
223 $whereTotalPaymentConditions .= ' AND `fe`.`provider_id` = ?';
224 array_push($sqlBindArrayTotalPayment, $selectedProvider);
225 $wherePaidConditions .= ' AND `fe`.`provider_id` = ?';
226 array_push($sqlBindArrayPaid, $selectedProvider);
229 // pass last parameter as Boolean, which is getting the facility name in the resulted array
230 $totalAppointmentSql = fetchAppointments($from_date, $to_date, null, $providerID, $facilityID);
231 if (count($totalAppointmentSql) > 0) { // check if $totalAppointmentSql array has value
232 foreach ($totalAppointmentSql as $appointment) {
233 $eventDate = $appointment['pc_eventDate'];
234 $facility = $appointment['name'];
235 $providerName = $appointment['ufname'] . ' ' . $appointment['ulname'];
237 // initialize each level of the data structure if it doesn't already exist
238 if (!isset($totalAppointment[$eventDate])) {
239 $totalAppointment[$eventDate] = [];
242 if (!isset($totalAppointment[$eventDate][$facility])) {
243 $totalAppointment[$eventDate][$facility] = [];
246 if (!isset($totalAppointment[$eventDate][$facility][$providerName])) {
247 $totalAppointment[$eventDate][$facility][$providerName] = [];
250 // initialize the number of appointment to 0
251 if (!isset($totalAppointment[$eventDate][$facility][$providerName]['appointments'])) {
252 $totalAppointment[$eventDate][$facility][$providerName]['appointments'] = 0;
255 // increment the number of appointments
256 $totalAppointment[$eventDate][$facility][$providerName]['appointments']++
;
260 //Count Total New Patient
261 $newPatientSql = sqlStatement("SELECT `OPE`.`pc_eventDate` , `f`.`name` AS facility_Name , count( * ) AS totalNewPatient, `PD`.`providerID`, CONCAT( `u`.`fname`, ' ', `u`.`lname` ) AS provider_name
262 FROM `patient_data` AS PD
263 LEFT JOIN `openemr_postcalendar_events` AS OPE ON ( `OPE`.`pc_pid` = `PD`.`pid` )
264 LEFT JOIN `facility` AS f ON ( `OPE`.`pc_facility` = `f`.`id` )
265 LEFT JOIN `users` AS u ON ( `OPE`.`pc_aid` = `u`.`id` )
266 WHERE `OPE`.`pc_title` = 'New Patient'
267 AND $whereNewPatientConditions
268 GROUP BY `f`.`id` , `OPE`.`pc_eventDate`,provider_name
269 ORDER BY `OPE`.`pc_eventDate` ASC", $sqlBindArrayNewPatient);
273 while ($totalNewPatientRecord = sqlFetchArray($newPatientSql)) {
274 $totalNewPatient[$totalNewPatientRecord['pc_eventDate']][$totalNewPatientRecord['facility_Name']][$totalNewPatientRecord['provider_name']]['newPatient'] = $totalNewPatientRecord['totalNewPatient'];
278 $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
279 FROM `form_encounter` AS fc
280 LEFT JOIN `facility` AS f ON ( `fc`.`facility_id` = `f`.`id` )
281 LEFT JOIN `users` AS u ON ( `fc`.`provider_id` = `u`.`id` )
282 WHERE $whereTotalVisitConditions
283 GROUP BY `fc`.`facility_id`, DATE( `fc`.`date` ),provider_name ORDER BY DATE( `fc`.`date` ) ASC", $sqlBindArrayTotalVisit);
285 while ($totalVisitRecord = sqlFetchArray($totalVisitSql)) {
286 $totalVisit[$totalVisitRecord['Date']][$totalVisitRecord['facility_Name']][$totalVisitRecord['provider_name']]['visits'] = $totalVisitRecord['totalVisit'];
289 //Count Total Payments for only active records i.e. activity = 1
290 $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
292 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
293 LEFT JOIN `billing` AS b ON ( `fe`.`encounter` = `b`.`encounter` )
294 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
295 WHERE `b`.`activity` =1 AND
296 $whereTotalPaymentConditions
297 GROUP BY `b`.`encounter`,Date,provider_name ORDER BY Date ASC", $sqlBindArrayTotalPayment);
299 while ($totalPaymentRecord = sqlFetchArray($totalPaymetsSql)) {
300 $totalPayment[$totalPaymentRecord['Date']][$totalPaymentRecord['facilityName']][$totalPaymentRecord['provider_name']]['payments'] = $totalPayment[$totalPaymentRecord['Date']][$totalPaymentRecord['facilityName']][$totalPaymentRecord['provider_name']]['payments'] ??
null;
301 $totalPayment[$totalPaymentRecord['Date']][$totalPaymentRecord['facilityName']][$totalPaymentRecord['provider_name']]['payments'] +
= $totalPaymentRecord['totalpayment'];
305 $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
307 LEFT JOIN `form_encounter` AS fe ON ( `fe`.`facility_id` = `f`.`id` )
308 LEFT JOIN `payments` AS p ON ( `fe`.`encounter` = `p`.`encounter` )
309 LEFT JOIN `users` AS u ON ( `fe`.`provider_id` = `u`.`id` )
310 WHERE $wherePaidConditions
311 GROUP BY `p`.`encounter`, Date,provider_name ORDER BY Date ASC", $sqlBindArrayPaid);
314 while ($totalPaidRecord = sqlFetchArray($totalPaidAmountSql)) {
315 $totalPaid[$totalPaidRecord['Date']][$totalPaidRecord['facilityName']][$totalPaidRecord['provider_name']]['paidAmount'] +
= $totalPaidRecord['totalPaidAmount'];
318 // merge all array recursive in to one array
319 $dailySummaryReport = array_merge_recursive($totalAppointment, $totalNewPatient, $totalVisit, $totalPayment, $totalPaid);
322 <div id
="report_results" style
="font-size: 12px">
323 <?php
echo '<strong>' . xlt('From') . '</strong> ' . text(oeFormatShortDate($from_date)) . ' <strong>' . xlt('To{{Range}}') . '</strong> ' . text(oeFormatShortDate($to_date)); ?
>
325 <table
class="table flowboard" cellpadding
='5' cellspacing
='2' id
="ds_report">
326 <tr
class="head thead-light">
328 <td
><?php
echo xlt('Date'); ?
></td
>
329 <td
><?php
echo xlt('Facility'); ?
></td
>
330 <td
><?php
echo xlt('Provider'); ?
></td
>
331 <td
><?php
echo xlt('Appointments'); ?
></td
>
332 <td
><?php
echo xlt('New Patients'); ?
></td
>
333 <td
><?php
echo xlt('Visited Patients'); ?
></td
>
334 <td
><?php
echo xlt('Total Charges'); ?
></td
>
335 <td
><?php
echo xlt('Total Co-Pay'); ?
></td
>
336 <td
><?php
echo xlt('Balance Payment'); ?
></td
>
339 if (count($dailySummaryReport) > 0) { // check if daily summary array has value
340 foreach ($dailySummaryReport as $date => $dataValue) { // daily summary array which consists different/dynamic values
341 foreach ($facilities as $facility) { // facility array
342 if (isset($dataValue[$facility])) {
343 foreach ($dataValue[$facility] as $provider => $information) { // array which consists different/dynamic values
346 <td
><?php
echo text(oeFormatShortDate($date)); ?
></td
>
347 <td
><?php
echo text($facility); ?
></td
>
348 <td
><?php
echo text($provider); ?
></td
>
349 <td
><?php
echo isset($information['appointments']) ?
text($information['appointments']) : 0; ?
></td
>
350 <td
><?php
echo isset($information['newPatient']) ?
text($information['newPatient']) : 0; ?
></td
>
351 <td
><?php
echo isset($information['visits']) ?
text($information['visits']) : 0; ?
></td
>
352 <td align
="right"><?php
echo isset($information['payments']) ?
text(number_format($information['payments'], 2)) : number_format(0, 2); ?
></td
>
353 <td align
="right"><?php
echo isset($information['paidAmount']) ?
text(number_format($information['paidAmount'], 2)) : number_format(0, 2); ?
></td
>
356 if (isset($information['payments']) ||
isset($information['paidAmount'])) {
357 $dueAmount = number_format(floatval(str_replace(",", "", $information['payments'])) - floatval(str_replace(",", "", ($information['paidAmount'] ??
null))), 2);
359 $dueAmount = number_format(0, 2);
362 echo text($dueAmount);
367 if (count($dailySummaryReport) > 0) { // calculate the total count of the appointments, new patient,visits, payments, paid amount and due amount
368 $totalAppointments = $totalAppointments ??
null;
369 $totalAppointments +
= $information['appointments'];
371 $totalNewRegisterPatient = $totalNewRegisterPatient ??
null;
372 $totalNewRegisterPatient +
= ($information['newPatient'] ??
null);
374 $totalVisits = $totalVisits ??
null;
375 $totalVisits +
= ($information['visits'] ??
null);
377 $totalPayments = $totalPayments ??
null;
378 $totalPayments +
= floatval(str_replace(",", "", ($information['payments'] ??
null)));
380 $totalPaidAmount = $totalPaidAmount ??
null;
381 $totalPaidAmount +
= floatval(str_replace(",", "", ($information['paidAmount'] ??
null)));
383 $totalDueAmount = $totalDueAmount ??
null;
384 $totalDueAmount +
= $dueAmount;
391 <!--display total count
-->
392 <tr
class="totalrow">
393 <td
><?php
echo xlt("Total"); ?
></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
>
404 } else { // if there are no records then display message
407 <td colspan
="9" style
="text-align:center;font-weight:bold;"> <?php
echo xlt("There are no record(s) found."); ?
></td
>