changing some service codes
[openemr.git] / interface / reports / ippf_daily.php
blob7e434f2a2aa044dbccbcae6e5f9a59dff64c4c04
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');
18 // This will become the array of reportable values.
19 $areport = array();
21 // This accumulates the bottom line totals.
22 $atotals = array();
24 $cellcount = 0;
26 function genStartRow($att) {
27 global $cellcount, $form_output;
28 if ($form_output != 3) echo " <tr $att>\n";
29 $cellcount = 0;
32 function genEndRow() {
33 global $form_output;
34 if ($form_output == 3) {
35 echo "\n";
37 else {
38 echo " </tr>\n";
42 // Usually this generates one cell, but allows for two or more.
44 function genAnyCell($data, $right=false, $class='') {
45 global $cellcount, $form_output;
46 if (!is_array($data)) {
47 $data = array(0 => $data);
49 foreach ($data as $datum) {
50 if ($form_output == 3) {
51 if ($cellcount) echo ',';
52 echo '"' . $datum . '"';
54 else {
55 echo " <td";
56 if ($class) echo " class='$class'";
57 if ($right) echo " align='right'";
58 echo ">$datum</td>\n";
60 ++$cellcount;
64 function genHeadCell($data, $right=false) {
65 genAnyCell($data, $right, 'dehead');
68 // Create an HTML table cell containing a numeric value, and track totals.
70 function genNumCell($num, $cnum) {
71 global $atotals, $form_output;
72 $atotals[$cnum] += $num;
73 if (empty($num) && $form_output != 3) $num = '&nbsp;';
74 genAnyCell($num, true, 'detail');
77 // If we are doing the CSV export then generate the needed HTTP headers.
78 // Otherwise generate HTML.
80 if ($form_output == 3) {
81 header("Pragma: public");
82 header("Expires: 0");
83 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
84 header("Content-Type: application/force-download");
85 header("Content-Disposition: attachment; filename=service_statistics_report.csv");
86 header("Content-Description: File Transfer");
88 else { // not export
90 <html>
91 <head>
92 <?php html_header_show(); ?>
93 <title><?php echo $report_title; ?></title>
94 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
95 <style type="text/css">
96 body { font-family:sans-serif; font-size:10pt; font-weight:normal }
97 .dehead { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:bold }
98 .detail { color:#000000; font-family:sans-serif; font-size:10pt; font-weight:normal }
99 </style>
100 <script type="text/javascript" src="../../library/textformat.js"></script>
101 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
102 <script type="text/javascript" src="../../library/dynarch_calendar_en.js"></script>
103 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
104 <script language="JavaScript">
105 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
106 </script>
107 </head>
109 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
111 <center>
113 <h2><?php echo $report_title; ?></h2>
115 <form name='theform' method='post'
116 action='ippf_daily.php?t=<?php echo $report_type ?>'>
118 <table border='0' cellspacing='5' cellpadding='1'>
119 <tr>
120 <td valign='top' class='detail' nowrap>
121 <?php xl('Facility','e'); ?>:
122 </td>
123 <td valign='top' class='detail'>
124 <?php
125 // Build a drop-down list of facilities.
127 $query = "SELECT id, name FROM facility ORDER BY name";
128 $fres = sqlStatement($query);
129 echo " <select name='form_facility'>\n";
130 echo " <option value=''>-- All Facilities --\n";
131 while ($frow = sqlFetchArray($fres)) {
132 $facid = $frow['id'];
133 echo " <option value='$facid'";
134 if ($facid == $_POST['form_facility']) echo " selected";
135 echo ">" . $frow['name'] . "\n";
137 echo " </select>\n";
139 </td>
140 <td colspan='2' class='detail' nowrap>
141 <?php xl('Date','e'); ?>
142 <input type='text' name='form_from_date' id='form_from_date' size='10' value='<?php echo $from_date ?>'
143 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' title='Report date yyyy-mm-dd' />
144 <img src='../pic/show_calendar.gif' align='absbottom' width='24' height='22'
145 id='img_from_date' border='0' alt='[?]' style='cursor:pointer'
146 title='<?php xl('Click here to choose a date','e'); ?>' />
147 </td>
148 <td valign='top' class='dehead' nowrap>
149 <?php xl('To','e'); ?>:
150 </td>
151 <td colspan='3' valign='top' class='detail' nowrap>
152 <?php
153 foreach (array(1 => 'Screen', 2 => 'Printer', 3 => 'Export File') as $key => $value) {
154 echo " <input type='radio' name='form_output' value='$key'";
155 if ($key == $form_output) echo ' checked';
156 echo " />$value &nbsp;";
159 </td>
160 <td align='right' valign='top' class='detail' nowrap>
161 <input type='submit' name='form_submit' value='<?php xl('Submit','e'); ?>'
162 title='<?php xl('Click to generate the report','e'); ?>' />
163 </td>
164 </tr>
165 <tr>
166 <td colspan='5' height="1">
167 </td>
168 </tr>
169 </table>
170 <?php
171 } // end not export
173 if ($_POST['form_submit']) {
175 $lores = sqlStatement("SELECT option_id, title FROM list_options WHERE " .
176 "list_id = 'contrameth' ORDER BY title");
177 while ($lorow = sqlFetchArray($lores)) {
178 $areport[$lorow['option_id']] = array($lorow['title'],
179 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
181 $areport['zzz'] = array('Unknown', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
183 // This gets us all MA codes, with encounter and patient
184 // info attached and grouped by patient and encounter.
185 $query = "SELECT " .
186 "fe.pid, fe.encounter, fe.date AS encdate, fe.pc_catid, " .
187 "pd.regdate, b.code_type, b.code " .
188 "FROM form_encounter AS fe " .
189 "JOIN patient_data AS pd ON pd.pid = fe.pid " .
190 "LEFT JOIN billing AS b ON " .
191 "b.pid = fe.pid AND b.encounter = fe.encounter AND b.activity = 1 " .
192 "AND b.code_type = 'MA' " .
193 "WHERE fe.date >= '$from_date 00:00:00' AND " .
194 "fe.date <= '$from_date 23:59:59' ";
196 if ($form_facility) {
197 $query .= "AND fe.facility_id = '$form_facility' ";
199 $query .= "ORDER BY fe.pid, fe.encounter, b.code";
200 $res = sqlStatement($query);
202 $last_pid = '0';
203 $last_contra_pid = '0';
204 $last_encounter = '0';
205 $method = '';
207 while ($row = sqlFetchArray($res)) {
208 if ($row['code_type'] === 'MA') {
210 // Logic for individual patients.
212 if ($row['pid'] != $last_pid) { // new patient
213 $last_pid = $row['pid'];
215 $crow = sqlQuery("SELECT lc.new_method " .
216 "FROM lists AS l, lists_ippf_con AS lc WHERE " .
217 "l.pid = '$last_pid' AND l.begdate <= '$from_date' AND " .
218 "( l.enddate IS NULL OR l.enddate > '$from_date' ) AND " .
219 "l.activity = 1 AND l.type = 'contraceptive' AND lc.id = l.id " .
220 "ORDER BY l.begdate DESC LIMIT 1");
221 $amethods = explode('|', empty($crow) ? 'zzz' : $crow['new_method']);
223 // TBD: We probably want to select the method with highest CYP here,
224 // but for now we'll settle for the first one that appears.
225 $method = $amethods[0];
227 if (empty($areport[$method])) {
228 // This should not happen.
229 $areport[$method] = array("Unlisted method '$method'",
230 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
233 // Count total clients.
234 ++$areport[$method][3];
236 // Count as new or old client.
237 if ($row['regdate'] == $from_date) {
238 ++$areport[$method][1];
239 } else {
240 ++$areport[$method][2];
243 // Maybe count as old Client First Visit this year.
244 $regyear = substr($row['regdate'], 0, 4);
245 $thisyear = substr($from_date, 0, 4);
246 if ($regyear && $regyear < $thisyear) {
247 $trow = sqlQuery("SELECT count(*) AS count FROM form_encounter " .
248 "WHERE date >= '$thisyear-01-01 00:00:00' AND " .
249 "date < '" . $row['encdate'] . " 00:00:00'");
250 if (empty($trow['count'])) ++$areport[$method][5];
252 } // end new patient
254 // Logic for visits.
256 if ($row['encounter'] != $last_encounter) { // new visit
257 $last_encounter = $row['encounter'];
259 // Count unique clients coming for supply or re-supply.
260 if ($row['pc_catid'] == '10' && $last_pid != $last_contra_pid) {
261 $last_contra_pid = $last_pid;
262 ++$areport[$method][4];
266 // Logic for specific services.
268 $code = 0 + $row['code'];
269 if ($code == 255004) ++$areport[$method][6]; // pap smear
270 if ($code == 256101) ++$areport[$method][7]; // preg test
271 if ($code == 375008) ++$areport[$method][8]; // dr's check
272 if ($code == 375014) ++$areport[$method][9]; // dr's visit (was 375013)
273 if ($code == 375011) ++$areport[$method][10]; // advice (was 009903)
274 if ($code == 019916) ++$areport[$method][11]; // couns by method
275 if ($code == 039916) ++$areport[$method][12]; // infert couns
276 if ($code == 019911) ++$areport[$method][13]; // std/aids couns
278 } // end while
280 if ($form_output != 3) {
281 echo "<table border='0' cellpadding='1' cellspacing='2' width='98%'>\n";
282 } // end not csv export
284 // Generate headings.
285 genStartRow("bgcolor='#dddddd'");
286 genHeadCell(xl('Method' ));
287 genHeadCell(xl('New Clients' ), true);
288 genHeadCell(xl('Old Clients' ), true);
289 genHeadCell(xl('Total Clients' ), true);
290 genHeadCell(xl('Contra Clients' ), true);
291 genHeadCell(xl('O.A.F.V.' ), true);
292 genHeadCell(xl('Pap Smear' ), true);
293 genHeadCell(xl('Preg Test' ), true);
294 genHeadCell(xl('Dr Check' ), true);
295 genHeadCell(xl('Dr Visit' ), true);
296 genHeadCell(xl('Advice' ), true);
297 genHeadCell(xl('Couns by Method'), true);
298 genHeadCell(xl('Infert Couns' ), true);
299 genHeadCell(xl('STD/AIDS Couns' ), true);
300 genEndRow();
302 $encount = 0;
304 foreach ($areport as $key => $varr) {
305 $bgcolor = (++$encount & 1) ? "#ddddff" : "#ffdddd";
306 genStartRow("bgcolor='$bgcolor'");
307 genAnyCell($varr[0], false, 'detail');
308 // Generate data and accumulate totals for this row.
309 for ($cnum = 0; $cnum < 13; ++$cnum) {
310 genNumCell($varr[$cnum + 1], $cnum);
312 genEndRow();
313 } // end foreach
315 if ($form_output != 3) {
316 // Generate the line of totals.
317 genStartRow("bgcolor='#dddddd'");
318 genHeadCell(xl('Totals'));
319 for ($cnum = 0; $cnum < 13; ++$cnum) {
320 genHeadCell($atotals[$cnum], true);
322 genEndRow();
323 // End of table.
324 echo "</table>\n";
327 } // end if submit
329 if ($form_output != 3) {
331 </form>
332 </center>
334 <script language='JavaScript'>
335 Calendar.setup({inputField:"form_from_date", ifFormat:"%Y-%m-%d", button:"img_from_date"});
336 <?php if ($form_output == 2) { ?>
337 window.print();
338 <?php } ?>
339 </script>
341 </body>
342 </html>
343 <?php
344 } // end not export