Feat openemr fix 7480 7494 email prescription (#7495)
[openemr.git] / interface / reports / ippf_daily.php
blob3832f09260f12bd0daccee37d1467dd34e9d9ab5
1 <?php
3 /**
4 * This module creates the Barbados Daily Record.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2009 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017-2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../globals.php");
16 require_once("../../library/patient.inc.php");
18 use OpenEMR\Common\Acl\AclMain;
19 use OpenEMR\Common\Csrf\CsrfUtils;
20 use OpenEMR\Common\Twig\TwigContainer;
21 use OpenEMR\Core\Header;
22 use OpenEMR\Services\FacilityService;
24 if (!empty($_POST)) {
25 if (!CsrfUtils::verifyCsrfToken($_POST["csrf_token_form"])) {
26 CsrfUtils::csrfNotVerified();
30 // Might want something different here.
32 if (! AclMain::aclCheckCore('acct', 'rep')) {
33 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Clinic Daily Record")]);
34 exit;
37 $facilityService = new FacilityService();
39 $from_date = (isset($_POST['form_from_date'])) ? DateToYYYYMMDD($_POST['form_from_date']) : date('Y-m-d');
41 $form_facility = isset($_POST['form_facility']) ? $_POST['form_facility'] : '';
42 $form_output = isset($_POST['form_output']) ? 0 + $_POST['form_output'] : 1;
44 $report_title = xl('Clinic Daily Record');
45 $report_col_count = 12;
47 // This will become the array of reportable values.
48 $areport = array();
50 // This accumulates the bottom line totals.
51 $atotals = array();
53 $cellcount = 0;
55 function genStartRow($att)
57 global $cellcount, $form_output;
58 if ($form_output != 3) {
59 echo " <tr $att>\n";
62 $cellcount = 0;
65 function genEndRow()
67 global $form_output;
68 if ($form_output == 3) {
69 echo "\n";
70 } else {
71 echo " </tr>\n";
75 // Usually this generates one cell, but allows for two or more.
77 function genAnyCell($data, $right = false, $class = '')
79 global $cellcount, $form_output;
80 if (!is_array($data)) {
81 $data = array(0 => $data);
84 foreach ($data as $datum) {
85 if ($form_output == 3) {
86 if ($cellcount) {
87 echo ',';
90 echo '"' . $datum . '"';
91 } else {
92 echo " <td";
93 if ($class) {
94 echo " class='" . attr($class) . "'";
97 if ($right) {
98 echo " align='right'";
101 echo ">" . text($datum) . "</td>\n";
104 ++$cellcount;
108 function genHeadCell($data, $right = false)
110 genAnyCell($data, $right, 'dehead');
113 // Create an HTML table cell containing a numeric value, and track totals.
115 function genNumCell($num, $cnum)
117 global $atotals, $form_output;
118 $atotals[$cnum] += $num;
119 if (empty($num) && $form_output != 3) {
120 $num = '&nbsp;';
123 genAnyCell($num, true, 'detail');
126 // If we are doing the CSV export then generate the needed HTTP headers.
127 // Otherwise generate HTML.
129 if ($form_output == 3) {
130 header("Pragma: public");
131 header("Expires: 0");
132 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
133 header("Content-Type: application/force-download");
134 header("Content-Disposition: attachment; filename=service_statistics_report.csv");
135 header("Content-Description: File Transfer");
136 } else { // not export
138 <html>
139 <head>
140 <title><?php echo text($report_title); ?></title>
142 <?php Header::setupHeader('datetime-picker'); ?>
144 <style>
145 body { font-family:sans-serif; font-size:10pt; font-weight:normal }
146 .dehead { color:var(--black); font-family:sans-serif; font-size:10pt; font-weight:bold }
147 .detail { color:var(--black); font-family:sans-serif; font-size:10pt; font-weight:normal }
148 </style>
150 <script>
151 $(function () {
152 $('.datepicker').datetimepicker({
153 <?php $datetimepicker_timepicker = false; ?>
154 <?php $datetimepicker_showseconds = false; ?>
155 <?php $datetimepicker_formatInput = true; ?>
156 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
157 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
160 </script>
161 </head>
163 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
165 <center>
167 <h2><?php echo text($report_title); ?></h2>
169 <form name='theform' method='post' action='ippf_daily.php?t=<?php echo attr_url($report_type); ?>' onsubmit='return top.restoreSession()'>
170 <input type="hidden" name="csrf_token_form" value="<?php echo attr(CsrfUtils::collectCsrfToken()); ?>" />
172 <table border='0' cellspacing='5' cellpadding='1'>
173 <tr>
174 <td valign='top' class='detail' nowrap>
175 <?php echo xlt('Facility'); ?>:
176 </td>
177 <td valign='top' class='detail'>
178 <?php
179 // Build a drop-down list of facilities.
181 $fres = $facilityService->getAllFacility();
182 echo " <select name='form_facility'>\n";
183 echo " <option value=''>-- All Facilities --\n";
184 foreach ($fres as $frow) {
185 $facid = $frow['id'];
186 echo " <option value='" . attr($facid) . "'";
187 if ($facid == $_POST['form_facility']) {
188 echo " selected";
191 echo ">" . text($frow['name']) . "\n";
194 echo " </select>\n";
196 </td>
197 <td colspan='2' class='detail' nowrap>
198 <?php echo xlt('Date'); ?>
199 <input type='text' class='datepicker' name='form_from_date' id='form_from_date' size='10' value='<?php echo attr(oeFormatShortDate($from_date)); ?>' />
200 </td>
201 <td valign='top' class='dehead' nowrap>
202 <?php echo xlt('To{{Destination}}'); ?>:
203 </td>
204 <td colspan='3' valign='top' class='detail' nowrap>
205 <?php
206 foreach (array(1 => 'Screen', 2 => 'Printer', 3 => 'Export File') as $key => $value) {
207 echo " <input type='radio' name='form_output' value='" . attr($key) . "'";
208 if ($key == $form_output) {
209 echo ' checked';
212 echo " />" . text($value) . " &nbsp;";
215 </td>
216 <td align='right' valign='top' class='detail' nowrap>
217 <input type='submit' name='form_submit' value='<?php echo xla('Submit'); ?>'
218 title='<?php echo xla('Click to generate the report'); ?>' />
219 </td>
220 </tr>
221 <tr>
222 <td colspan='5' height="1">
223 </td>
224 </tr>
225 </table>
226 <?php
227 } // end not export
229 if ($_POST['form_submit']) {
230 $lores = sqlStatement("SELECT option_id, title FROM list_options WHERE " .
231 "list_id = 'contrameth' AND activity = 1 ORDER BY title");
232 while ($lorow = sqlFetchArray($lores)) {
233 $areport[$lorow['option_id']] = array($lorow['title'],
234 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
237 $areport['zzz'] = array('Unknown', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
239 $sqlBindArray = array();
241 // This gets us all MA codes, with encounter and patient
242 // info attached and grouped by patient and encounter.
243 $query = "SELECT " .
244 "fe.pid, fe.encounter, fe.date AS encdate, fe.pc_catid, " .
245 "pd.regdate, b.code_type, b.code " .
246 "FROM form_encounter AS fe " .
247 "JOIN patient_data AS pd ON pd.pid = fe.pid " .
248 "LEFT JOIN billing AS b ON " .
249 "b.pid = fe.pid AND b.encounter = fe.encounter AND b.activity = 1 " .
250 "AND b.code_type = 'MA' " .
251 "WHERE fe.date >= ? AND " .
252 "fe.date <= ? ";
253 array_push($sqlBindArray, $from_date . ' 00:00:00', $from_date . ' 23:59:59');
255 if ($form_facility) {
256 $query .= "AND fe.facility_id = ? ";
257 array_push($sqlBindArray, $form_facility);
260 $query .= "ORDER BY fe.pid, fe.encounter, b.code";
261 $res = sqlStatement($query, $sqlBindArray);
263 $last_pid = '0';
264 $last_contra_pid = '0';
265 $last_encounter = '0';
266 $method = '';
268 while ($row = sqlFetchArray($res)) {
269 if ($row['code_type'] === 'MA') {
270 // Logic for individual patients.
272 if ($row['pid'] != $last_pid) { // new patient
273 $last_pid = $row['pid'];
275 $crow = sqlQuery("SELECT lc.new_method " .
276 "FROM lists AS l, lists_ippf_con AS lc WHERE " .
277 "l.pid = ? AND l.begdate <= ? AND " .
278 "( l.enddate IS NULL OR l.enddate > ? ) AND " .
279 "l.activity = 1 AND l.type = 'contraceptive' AND lc.id = l.id " .
280 "ORDER BY l.begdate DESC LIMIT 1", array($last_pid, $from_date, $from_date));
281 $amethods = explode('|', empty($crow) ? 'zzz' : $crow['new_method']);
283 // TBD: We probably want to select the method with highest CYP here,
284 // but for now we'll settle for the first one that appears.
285 $method = $amethods[0];
287 if (empty($areport[$method])) {
288 // This should not happen.
289 $areport[$method] = array("Unlisted method '$method'",
290 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
293 // Count total clients.
294 ++$areport[$method][3];
296 // Count as new or old client.
297 if ($row['regdate'] == $from_date) {
298 ++$areport[$method][1];
299 } else {
300 ++$areport[$method][2];
303 /*************************************************************
304 // Maybe count as old Client First Visit this year.
305 $regyear = substr($row['regdate'], 0, 4);
306 $thisyear = substr($from_date, 0, 4);
307 if ($regyear && $regyear < $thisyear) {
308 $trow = sqlQuery("SELECT count(*) AS count FROM form_encounter " .
309 "WHERE date >= '$thisyear-01-01 00:00:00' AND " .
310 "date < '" . $row['encdate'] . " 00:00:00'");
311 if (empty($trow['count'])) ++$areport[$method][5];
313 *************************************************************/
314 } // end new patient
316 // Logic for visits.
318 if ($row['encounter'] != $last_encounter) { // new visit
319 $last_encounter = $row['encounter'];
321 // Count unique clients coming for supply or re-supply.
322 if ($row['pc_catid'] == '10' && $last_pid != $last_contra_pid) {
323 $last_contra_pid = $last_pid;
324 ++$areport[$method][4];
328 // Logic for specific services.
330 $code = 0 + $row['code'];
331 if ($code == 255004) {
332 ++$areport[$method][5]; // pap smear
335 if ($code == 256101) {
336 ++$areport[$method][6]; // preg test
339 if ($code == 375008) {
340 ++$areport[$method][7]; // dr's check
343 if ($code == 375015) {
344 ++$areport[$method][8]; // dr's visit (was 375014)
347 if ($code == 375011) {
348 ++$areport[$method][9]; // advice
351 if ($code == 19916) {
352 ++$areport[$method][10]; // couns by method
355 if ($code == 39916) {
356 ++$areport[$method][11]; // infert couns
359 if ($code == 19911) {
360 ++$areport[$method][12]; // std/aids couns
363 } // end while
365 if ($form_output != 3) {
366 echo "<table border='0' cellpadding='1' cellspacing='2' width='98%'>\n";
367 } // end not csv export
369 // Generate headings.
370 genStartRow("bgcolor='#dddddd'");
371 genHeadCell(xl('Method'));
372 genHeadCell(xl('New Clients'), true);
373 genHeadCell(xl('Old Clients'), true);
374 genHeadCell(xl('Total Clients'), true);
375 genHeadCell(xl('Contra Clients'), true);
376 // genHeadCell(xl('O.A.F.V.' ), true);
377 genHeadCell(xl('Pap Smear'), true);
378 genHeadCell(xl('Preg Test'), true);
379 genHeadCell(xl('Dr Check'), true);
380 genHeadCell(xl('Dr Visit'), true);
381 genHeadCell(xl('Advice'), true);
382 genHeadCell(xl('Couns by Method'), true);
383 genHeadCell(xl('Infert Couns'), true);
384 genHeadCell(xl('STD/AIDS Couns'), true);
385 genEndRow();
387 $encount = 0;
389 foreach ($areport as $key => $varr) {
390 $bgcolor = (++$encount & 1) ? "#ddddff" : "#ffdddd";
391 genStartRow("bgcolor='" . attr($bgcolor) . "'");
392 genAnyCell($varr[0], false, 'detail');
393 // Generate data and accumulate totals for this row.
394 for ($cnum = 0; $cnum < $report_col_count; ++$cnum) {
395 genNumCell($varr[$cnum + 1], $cnum);
398 genEndRow();
399 } // end foreach
401 if ($form_output != 3) {
402 // Generate the line of totals.
403 genStartRow("bgcolor='#dddddd'");
404 genHeadCell(xl('Totals'));
405 for ($cnum = 0; $cnum < $report_col_count; ++$cnum) {
406 genHeadCell($atotals[$cnum], true);
409 genEndRow();
410 // End of table.
411 echo "</table>\n";
413 } // end if submit
415 if ($form_output != 3) {
417 </form>
418 </center>
420 <script>
421 <?php if ($form_output == 2) { ?>
422 var win = top.printLogPrint ? top : opener.top;
423 win.printLogPrint(window);
424 <?php } ?>
425 </script>
427 </body>
428 </html>
429 <?php
430 } // end not export