bug fix in code selector to ensure selected code set is persistent
[openemr.git] / interface / reports / ippf_daily.php
blobbe85574044e673932ea582c90ced1a86ed61aab7
1 <?php
2 // This module creates the Barbados Daily Record.
4 include_once("../globals.php");
5 include_once("../../library/patient.inc");
6 include_once("../../library/acl.inc");
8 // Might want something different here.
9 //
10 if (! acl_check('acct', 'rep')) die("Unauthorized access.");
12 $from_date = fixDate($_POST['form_from_date']);
13 $form_facility = isset($_POST['form_facility']) ? $_POST['form_facility'] : '';
14 $form_output = isset($_POST['form_output']) ? 0 + $_POST['form_output'] : 1;
16 $report_title = xl('Clinic Daily Record');
17 $report_col_count = 12;
19 // This will become the array of reportable values.
20 $areport = array();
22 // This accumulates the bottom line totals.
23 $atotals = array();
25 $cellcount = 0;
27 function genStartRow($att) {
28 global $cellcount, $form_output;
29 if ($form_output != 3) echo " <tr $att>\n";
30 $cellcount = 0;
33 function genEndRow() {
34 global $form_output;
35 if ($form_output == 3) {
36 echo "\n";
38 else {
39 echo " </tr>\n";
43 // Usually this generates one cell, but allows for two or more.
45 function genAnyCell($data, $right=false, $class='') {
46 global $cellcount, $form_output;
47 if (!is_array($data)) {
48 $data = array(0 => $data);
50 foreach ($data as $datum) {
51 if ($form_output == 3) {
52 if ($cellcount) echo ',';
53 echo '"' . $datum . '"';
55 else {
56 echo " <td";
57 if ($class) echo " class='$class'";
58 if ($right) echo " align='right'";
59 echo ">$datum</td>\n";
61 ++$cellcount;
65 function genHeadCell($data, $right=false) {
66 genAnyCell($data, $right, 'dehead');
69 // Create an HTML table cell containing a numeric value, and track totals.
71 function genNumCell($num, $cnum) {
72 global $atotals, $form_output;
73 $atotals[$cnum] += $num;
74 if (empty($num) && $form_output != 3) $num = '&nbsp;';
75 genAnyCell($num, true, 'detail');
78 // If we are doing the CSV export then generate the needed HTTP headers.
79 // Otherwise generate HTML.
81 if ($form_output == 3) {
82 header("Pragma: public");
83 header("Expires: 0");
84 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
85 header("Content-Type: application/force-download");
86 header("Content-Disposition: attachment; filename=service_statistics_report.csv");
87 header("Content-Description: File Transfer");
89 else { // not export
91 <html>
92 <head>
93 <?php html_header_show(); ?>
94 <title><?php echo $report_title; ?></title>
95 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
96 <style type="text/css">
97 body { font-family:sans-serif; font-size:10pt; font-weight:normal }
98 .dehead { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:bold }
99 .detail { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:normal }
100 </style>
101 <script type="text/javascript" src="../../library/textformat.js"></script>
102 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
103 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
104 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
105 <script language="JavaScript">
106 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
107 </script>
108 </head>
110 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
112 <center>
114 <h2><?php echo $report_title; ?></h2>
116 <form name='theform' method='post'
117 action='ippf_daily.php?t=<?php echo $report_type ?>'>
119 <table border='0' cellspacing='5' cellpadding='1'>
120 <tr>
121 <td valign='top' class='detail' nowrap>
122 <?php xl('Facility','e'); ?>:
123 </td>
124 <td valign='top' class='detail'>
125 <?php
126 // Build a drop-down list of facilities.
128 $query = "SELECT id, name FROM facility ORDER BY name";
129 $fres = sqlStatement($query);
130 echo " <select name='form_facility'>\n";
131 echo " <option value=''>-- All Facilities --\n";
132 while ($frow = sqlFetchArray($fres)) {
133 $facid = $frow['id'];
134 echo " <option value='$facid'";
135 if ($facid == $_POST['form_facility']) echo " selected";
136 echo ">" . $frow['name'] . "\n";
138 echo " </select>\n";
140 </td>
141 <td colspan='2' class='detail' nowrap>
142 <?php xl('Date','e'); ?>
143 <input type='text' name='form_from_date' id='form_from_date' size='10' value='<?php echo $from_date ?>'
144 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='Report date yyyy-mm-dd' />
145 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
146 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
147 title='<?php xl('Click here to choose a date','e'); ?>' />
148 </td>
149 <td valign='top' class='dehead' nowrap>
150 <?php xl('To','e'); ?>:
151 </td>
152 <td colspan='3' valign='top' class='detail' nowrap>
153 <?php
154 foreach (array(1 => 'Screen', 2 => 'Printer', 3 => 'Export File') as $key => $value) {
155 echo " <input type='radio' name='form_output' value='$key'";
156 if ($key == $form_output) echo ' checked';
157 echo " />$value &nbsp;";
160 </td>
161 <td align='right' valign='top' class='detail' nowrap>
162 <input type='submit' name='form_submit' value='<?php xl('Submit','e'); ?>'
163 title='<?php xl('Click to generate the report','e'); ?>' />
164 </td>
165 </tr>
166 <tr>
167 <td colspan='5' height="1">
168 </td>
169 </tr>
170 </table>
171 <?php
172 } // end not export
174 if ($_POST['form_submit']) {
176 $lores = sqlStatement("SELECT option_id, title FROM list_options WHERE " .
177 "list_id = 'contrameth' ORDER BY title");
178 while ($lorow = sqlFetchArray($lores)) {
179 $areport[$lorow['option_id']] = array($lorow['title'],
180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
182 $areport['zzz'] = array('Unknown', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
184 // This gets us all MA codes, with encounter and patient
185 // info attached and grouped by patient and encounter.
186 $query = "SELECT " .
187 "fe.pid, fe.encounter, fe.date AS encdate, fe.pc_catid, " .
188 "pd.regdate, b.code_type, b.code " .
189 "FROM form_encounter AS fe " .
190 "JOIN patient_data AS pd ON pd.pid = fe.pid " .
191 "LEFT JOIN billing AS b ON " .
192 "b.pid = fe.pid AND b.encounter = fe.encounter AND b.activity = 1 " .
193 "AND b.code_type = 'MA' " .
194 "WHERE fe.date >= '$from_date 00:00:00' AND " .
195 "fe.date <= '$from_date 23:59:59' ";
197 if ($form_facility) {
198 $query .= "AND fe.facility_id = '$form_facility' ";
200 $query .= "ORDER BY fe.pid, fe.encounter, b.code";
201 $res = sqlStatement($query);
203 $last_pid = '0';
204 $last_contra_pid = '0';
205 $last_encounter = '0';
206 $method = '';
208 while ($row = sqlFetchArray($res)) {
209 if ($row['code_type'] === 'MA') {
211 // Logic for individual patients.
213 if ($row['pid'] != $last_pid) { // new patient
214 $last_pid = $row['pid'];
216 $crow = sqlQuery("SELECT lc.new_method " .
217 "FROM lists AS l, lists_ippf_con AS lc WHERE " .
218 "l.pid = '$last_pid' AND l.begdate <= '$from_date' AND " .
219 "( l.enddate IS NULL OR l.enddate > '$from_date' ) AND " .
220 "l.activity = 1 AND l.type = 'contraceptive' AND lc.id = l.id " .
221 "ORDER BY l.begdate DESC LIMIT 1");
222 $amethods = explode('|', empty($crow) ? 'zzz' : $crow['new_method']);
224 // TBD: We probably want to select the method with highest CYP here,
225 // but for now we'll settle for the first one that appears.
226 $method = $amethods[0];
228 if (empty($areport[$method])) {
229 // This should not happen.
230 $areport[$method] = array("Unlisted method '$method'",
231 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
234 // Count total clients.
235 ++$areport[$method][3];
237 // Count as new or old client.
238 if ($row['regdate'] == $from_date) {
239 ++$areport[$method][1];
240 } else {
241 ++$areport[$method][2];
244 /*************************************************************
245 // Maybe count as old Client First Visit this year.
246 $regyear = substr($row['regdate'], 0, 4);
247 $thisyear = substr($from_date, 0, 4);
248 if ($regyear && $regyear < $thisyear) {
249 $trow = sqlQuery("SELECT count(*) AS count FROM form_encounter " .
250 "WHERE date >= '$thisyear-01-01 00:00:00' AND " .
251 "date < '" . $row['encdate'] . " 00:00:00'");
252 if (empty($trow['count'])) ++$areport[$method][5];
254 *************************************************************/
256 } // end new patient
258 // Logic for visits.
260 if ($row['encounter'] != $last_encounter) { // new visit
261 $last_encounter = $row['encounter'];
263 // Count unique clients coming for supply or re-supply.
264 if ($row['pc_catid'] == '10' && $last_pid != $last_contra_pid) {
265 $last_contra_pid = $last_pid;
266 ++$areport[$method][4];
270 // Logic for specific services.
272 $code = 0 + $row['code'];
273 if ($code == 255004) ++$areport[$method][5]; // pap smear
274 if ($code == 256101) ++$areport[$method][6]; // preg test
275 if ($code == 375008) ++$areport[$method][7]; // dr's check
276 if ($code == 375015) ++$areport[$method][8]; // dr's visit (was 375014)
277 if ($code == 375011) ++$areport[$method][9]; // advice
278 if ($code == 019916) ++$areport[$method][10]; // couns by method
279 if ($code == 039916) ++$areport[$method][11]; // infert couns
280 if ($code == 019911) ++$areport[$method][12]; // std/aids couns
282 } // end while
284 if ($form_output != 3) {
285 echo "<table border='0' cellpadding='1' cellspacing='2' width='98%'>\n";
286 } // end not csv export
288 // Generate headings.
289 genStartRow("bgcolor='#dddddd'");
290 genHeadCell(xl('Method' ));
291 genHeadCell(xl('New Clients' ), true);
292 genHeadCell(xl('Old Clients' ), true);
293 genHeadCell(xl('Total Clients' ), true);
294 genHeadCell(xl('Contra Clients' ), true);
295 // genHeadCell(xl('O.A.F.V.' ), true);
296 genHeadCell(xl('Pap Smear' ), true);
297 genHeadCell(xl('Preg Test' ), true);
298 genHeadCell(xl('Dr Check' ), true);
299 genHeadCell(xl('Dr Visit' ), true);
300 genHeadCell(xl('Advice' ), true);
301 genHeadCell(xl('Couns by Method'), true);
302 genHeadCell(xl('Infert Couns' ), true);
303 genHeadCell(xl('STD/AIDS Couns' ), true);
304 genEndRow();
306 $encount = 0;
308 foreach ($areport as $key => $varr) {
309 $bgcolor = (++$encount & 1) ? "#ddddff" : "#ffdddd";
310 genStartRow("bgcolor='$bgcolor'");
311 genAnyCell($varr[0], false, 'detail');
312 // Generate data and accumulate totals for this row.
313 for ($cnum = 0; $cnum < $report_col_count; ++$cnum) {
314 genNumCell($varr[$cnum + 1], $cnum);
316 genEndRow();
317 } // end foreach
319 if ($form_output != 3) {
320 // Generate the line of totals.
321 genStartRow("bgcolor='#dddddd'");
322 genHeadCell(xl('Totals'));
323 for ($cnum = 0; $cnum < $report_col_count; ++$cnum) {
324 genHeadCell($atotals[$cnum], true);
326 genEndRow();
327 // End of table.
328 echo "</table>\n";
331 } // end if submit
333 if ($form_output != 3) {
335 </form>
336 </center>
338 <script language='JavaScript'>
339 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
340 <?php if ($form_output == 2) { ?>
341 window.print();
342 <?php } ?>
343 </script>
345 </body>
346 </html>
347 <?php
348 } // end not export