bug fix for mutliple reports, paginating html output, and some formData stuff
[openemr.git] / interface / forms / CAMOS / notegen.php
blobf0222396eeabbc1c9f1818d0fdfbad5c00f40d93
1 <?php
2 $depth = '../../../';
3 include_once ($depth.'interface/globals.php');
4 include_once($depth.'library/formdata.inc.php');
5 include_once ($depth.'library/classes/class.ezpdf.php');
6 ?>
7 <?php
8 if (!($_POST['submit_pdf'] || $_POST['submit_html']) && ($_GET['pid'] && $_GET['encounter'])) {
9 ?>
10 <html>
11 <head>
12 <title>
13 <?php xl('Print Notes','e'); ?>
14 </title>
15 </head>
16 <body>
17 <?php xl('Choose print format for this encounter report.','e'); ?><br><br>
18 <form method=post name=choose_patients>
19 <input type='submit' name='submit_pdf' value='<?php xl('Print (PDF)','e'); ?>'>
20 <input type='submit' name='submit_html' value='<?php xl('Print (HTML)','e'); ?>'>
21 </form>
22 </body>
23 </html>
24 <?php
25 exit;
27 if (!$_POST['submit_pdf'] && !$_POST['submit_html'] && !($_GET['pid'] && $_GET['encounter'])) {
29 <html>
30 <head>
31 <title>
32 <?php xl('Print Notes','e'); ?>
33 </title>
34 <style type="text/css">@import url('<?php echo $depth ?>library/dynarch_calendar.css');</style>
35 <script type="text/javascript" src="<?php echo $depth ?>library/dialog.js"></script>
36 <script type="text/javascript" src="<?php echo $depth ?>library/textformat.js"></script>
37 <script type="text/javascript" src="<?php echo $depth ?>library/dynarch_calendar.js"></script>
38 <script type="text/javascript" src="<?php echo $depth ?>library/dynarch_calendar_en.js"></script>
39 <script type="text/javascript" src="<?php echo $depth ?>library/dynarch_calendar_setup.js"></script>
40 </head>
42 <body>
43 <script language='JavaScript'> var mypcc = '1'; </script>
45 <form method=post name=choose_patients>
47 <table>
48 <tr><td>
49 <span class='text'><?php xl('Start (yyyy-mm-dd): ','e') ?></span>
50 </td><td>
51 <input type='text' size='10' name='start' id='start' value='<? echo $_POST['end'] ? $_POST['end'] : date('Y-m-d') ?>'
52 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
53 title='<?php xl('yyyy-mm-dd last date of this event','e'); ?>' />
54 <img src='<?php echo $depth ?>interface/pic/show_calendar.gif' align='absbottom' width='24' height='22'
55 id='img_start' border='0' alt='[?]' style='cursor:pointer'
56 title='<?php xl('Click here to choose a date','e'); ?>'>
57 <script>
58 Calendar.setup({inputField:'start', ifFormat:'%Y-%m-%d', button:'img_start'});
59 </script>
60 </td></tr>
62 <tr><td>
63 <span class='text'><?php xl('End (yyyy-mm-dd): ','e') ?></span>
64 </td><td>
65 <input type='text' size='10' name='end' id='end' value ='<? echo $_POST['end'] ? $_POST['end'] : date('Y-m-d') ?>'
66 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
67 title='<?php xl('yyyy-mm-dd last date of this event','e'); ?>' />
68 <img src='<?php echo $depth ?>interface/pic/show_calendar.gif' align='absbottom' width='24' height='22'
69 id='img_end' border='0' alt='[?]' style='cursor:pointer'
70 title='<?php xl('Click here to choose a date','e'); ?>'>
71 <script>
72 Calendar.setup({inputField:'end', ifFormat:'%Y-%m-%d', button:'img_end'});
73 </script>
74 </td></tr>
75 <tr><td></td><td></td></tr>
76 <tr><td><?php xl('Last Name','e'); ?>: </td><td>
77 <input type='text' name='lname'/>
78 </td></tr>
79 <tr><td><?php xl('First Name','e'); ?>: </td><td>
80 <input type='text' name='fname'/>
81 </td></tr>
82 <tr><td>
83 <input type='submit' name='submit_pdf' value='<?php xl('Print (PDF)','e'); ?>'>
84 <input type='submit' name='submit_html' value='<?php xl('Print (HTML)','e'); ?>'>
85 </td><td>
86 </td></tr>
87 </table>
88 </form>
89 </body>
90 </html>
91 <?php
93 if ($_POST['submit_pdf'] || $_POST['submit_html'] || ($_GET['pid'] && $_GET['encounter'])) {
94 // note we are cleaning input, trimming, and preparing the variables for database insert
95 // before sending through this function
96 $output = getFormData(formData("start","P",true),formData("end","P",true),formData("lname","P",true),formData("fname","P",true));
97 ksort($output);
98 if ($_POST['submit_html']) { //print as html
100 <html>
101 <head>
102 <style>
103 body {
104 font-family: sans-serif;
105 font-weight: normal;
106 font-size: 8pt;
107 background: white;
108 color: black;
110 .paddingdiv {
111 width: 524pt;
112 padding: 0pt;
114 .navigate {
115 margin-top: 2.5em;
117 @media print {
118 .navigate {
119 display: none;
122 div.page {
123 page-break-after: always;
124 padding: 0pt;
125 margin-top: 50pt;
127 span.heading {
128 font-weight: bold;
129 font-size: 130%;
131 </style>
132 <title><?php xl('Patient Notes','e'); ?></title>
133 </head>
134 <body>
135 <div class='paddingdiv'>
136 <?php
137 foreach ($output as $datekey => $dailynote) {
138 foreach ($dailynote as $note_id => $notecontents) {
139 preg_match('/(\d+)_(\d+)/', $note_id, $matches); //the unique note id contains the pid and encounter
140 $pid = $matches[1];
141 $enc = $matches[2];
143 //new page code here
144 print "<DIV class='page'>";
146 print xl("Date").": ".$notecontents['date'] . "<br/>";
147 print xl("Name").": ".$notecontents['name'] . "<br/>";
148 print xl("DOB").": ".$notecontents['dob'] . "<br/>";
149 print xl("Claim")."# ".$notecontents['pubpid'] . "<br/>";
151 print "<br/>";
152 print xl("Chief Complaint").": ".$notecontents['reason'] . "<br/>";
153 if ($notecontents['vitals']) {
154 print "<br/>";
155 print $notecontents['vitals'] . "<br/>";
157 if (count($notecontents['exam']) > 0) {
158 print "<br/>";
159 print "<span class='heading'>" . xl("Progress Notes") . "</span><br/>";
160 print "<br/>";
161 foreach($notecontents['exam'] as $examnote) {
162 print nl2br($examnote) . "<br/>";
165 if (count($notecontents['prescriptions']) > 0) {
166 print "<br/>";
167 print "<span class='heading'>" . xl("Prescriptions") . "</span><br/>";
168 print "<br/>";
169 foreach($notecontents['prescriptions'] as $rx) {
170 print nl2br($rx) . "<br/>";
173 if (count($notecontents['other']) > 0) {
174 print "<br/>";
175 print "<span class='heading'>" . xl("Other") . "</span><br/>";
176 print "<br/>";
177 foreach($notecontents['other'] as $other => $othercat) {
178 print nl2br($other) . "<br/>";
179 foreach($othercat as $items) {
180 print nl2br($items) . "<br/>";
184 if (count($notecontents['billing']) > 0) {
185 $tmp = array();
186 foreach($notecontents['billing'] as $code) {
187 $tmp[$code]++;
189 if (count($tmp) > 0) {
190 print "<br/>";
191 print "<span class='heading'>" . xl("Coding") . "</span><br/>";
192 print "<br/>";
193 foreach($tmp as $code => $val) {
194 print nl2br($code) . "<br/>";
198 if (count($notecontents['calories']) > 0) {
199 $sum = 0;
200 print "<br/>";
201 print "<span class='heading'>" . xl("Calories") . "</span><br/>";
202 print "<br/>";
203 foreach($notecontents['calories'] as $calories => $value) {
204 print $value['content'].' - '.$value['item'].' - '.$value['date'] . "<br/>";
205 $sum += $value['content'];
207 print "--------" . "<br/>";
208 print $sum . "<br/>";
210 print "<br/>";
211 print "<br/>";
212 print "<span class='heading'>" . xl("Digitally Signed") . "</span><br/>";
214 $query = sqlStatement("select t2.id, t2.fname, t2.lname, t2.title from forms as t1 join users as t2 on " .
215 "(t1.user like t2.username) where t1.pid=$pid and t1.encounter=$encounter");
216 if ($results = mysql_fetch_array($query, MYSQL_ASSOC)) {
217 $name = $results['fname']." ".$results['lname'].", ".$results['title'];
218 $user_id = $results['id'];
220 $path = $GLOBALS['fileroot']."/interface/forms/CAMOS";
221 if (file_exists($path."/sig".$user_id.".jpg")) {
222 //show the image here
224 print "<span class='heading'>" . $name . "</span><br/>";
225 print "</DIV>"; //end of last page
229 <script language='JavaScript'>
230 window.print();
231 </script>
232 </div>
233 </body>
234 </html>
235 <?php
236 exit;
238 else { // print as pdf
239 $pdf =& new Cezpdf();
240 $pdf->selectFont($depth.'library/fonts/Helvetica');
241 $pdf->ezSetCmMargins(3,1,1,1);
242 $first = 1;
243 foreach ($output as $datekey => $dailynote) {
244 foreach ($dailynote as $note_id => $notecontents) {
245 preg_match('/(\d+)_(\d+)/', $note_id, $matches); //the unique note id contains the pid and encounter
246 $pid = $matches[1];
247 $enc = $matches[2];
248 if (!$first) { //generate a new page each time except first iteration when nothing has been printed yet
249 $pdf->ezNewPage();
251 else {
252 $first = 0;
254 $pdf->ezText(xl("Date").": ".$notecontents['date'],8);
255 $pdf->ezText(xl("Name").": ".$notecontents['name'],8);
256 $pdf->ezText(xl("DOB").": ".$notecontents['dob'],8);
257 $pdf->ezText(xl("Claim")."# ".$notecontents['pubpid'],8);
259 $pdf->ezText("",8);
260 $pdf->ezText(xl("Chief Complaint").": ".$notecontents['reason'],8);
261 if ($notecontents['vitals']) {
262 $pdf->ezText("",8);
263 $pdf->ezText($notecontents['vitals'],8);
265 if (count($notecontents['exam']) > 0) {
266 $pdf->ezText("",8);
267 $pdf->ezText(xl("Progress Notes"),12);
268 $pdf->ezText("",8);
269 foreach($notecontents['exam'] as $examnote) {
270 $pdf->ezText("$examnote");
273 if (count($notecontents['prescriptions']) > 0) {
274 $pdf->ezText("",8);
275 $pdf->ezText(xl("Prescriptions"),12);
276 $pdf->ezText("",8);
277 foreach($notecontents['prescriptions'] as $rx) {
278 $pdf->ezText($rx);
281 if (count($notecontents['other']) > 0) {
282 $pdf->ezText("",8);
283 $pdf->ezText(xl("Other"),12);
284 $pdf->ezText("",8);
285 foreach($notecontents['other'] as $other => $othercat) {
286 $pdf->ezText($other,8);
287 foreach($othercat as $items) {
288 $pdf->ezText($items,8);
292 if (count($notecontents['billing']) > 0) {
293 $tmp = array();
294 foreach($notecontents['billing'] as $code) {
295 $tmp[$code]++;
297 if (count($tmp) > 0) {
298 $pdf->ezText("",8);
299 $pdf->ezText(xl("Coding"),12);
300 $pdf->ezText("",8);
301 foreach($tmp as $code => $val) {
302 $pdf->ezText($code,8);
306 if (count($notecontents['calories']) > 0) {
307 $sum = 0;
308 $pdf->ezText("",8);
309 $pdf->ezText(xl("Calories"),12);
310 $pdf->ezText("",8);
311 foreach($notecontents['calories'] as $calories => $value) {
312 $pdf->ezText($value['content'].' - '.$value['item'].' - '.$value['date'],8);
313 $sum += $value['content'];
315 $pdf->ezText("--------",8);
316 $pdf->ezText($sum,8);
318 $pdf->ezText("",12);
319 $pdf->ezText("",12);
320 $pdf->ezText(xl("Digitally Signed"),12);
322 $query = sqlStatement("select t2.id, t2.fname, t2.lname, t2.title from forms as t1 join users as t2 on " .
323 "(t1.user like t2.username) where t1.pid=$pid and t1.encounter=$encounter");
324 if ($results = mysql_fetch_array($query, MYSQL_ASSOC)) {
325 $name = $results['fname']." ".$results['lname'].", ".$results['title'];
326 $user_id = $results['id'];
328 $path = $GLOBALS['fileroot']."/interface/forms/CAMOS";
329 if (file_exists($path."/sig".$user_id.".jpg")) {
330 $pdf->ezImage($path."/sig".$user_id.".jpg",'','72','','left','');
332 $pdf->ezText($name,12);
335 $pdf->ezStream();
338 function getFormData($start_date,$end_date,$lname,$fname) { //dates in sql format
340 // All 4 parameters have previously been trimmed, globally validated,
341 // and prepared for database insert
343 $name_clause = '';
344 $date_clause = "date(t2.date) >= '".$start_date."' and date(t2.date) <= '".$end_date."' ";
345 if ($lname || $fname) {
346 $name_clause = "and t3.lname like '%".$lname."%' and t3.fname like '%".$fname."%' ";
348 $dates = array();
349 if ($_GET['pid'] && $_GET['encounter']) {
350 $date_clause = '';
351 $name_clause = "t2.pid=".$_GET['pid']." and t2.encounter=".$_GET['encounter']." ";
353 $query1 = sqlStatement(
354 "select t1.form_id, t1.form_name, t1.pid, date_format(t2.date,'%m-%d-%Y') as date, " .
355 "date_format(t2.date,'%Y%m%d') as datekey, " .
356 "t3.lname, t3.fname, t3.pubpid, date_format(t3.DOB,'%m-%d-%Y') as dob, " .
357 "t2.encounter as enc, " .
358 "t2.reason from " .
359 "forms as t1 join " .
360 "form_encounter as t2 on " .
361 "(t1.pid = t2.pid and t1.encounter = t2.encounter) " .
362 "join patient_data as t3 on " .
363 "(t1.pid = t3.pid) where " .
364 $date_clause .
365 $name_clause .
366 "order by date,pid");
367 while ($results1 = mysql_fetch_array($query1, MYSQL_ASSOC)) {
368 if (!$dates[$results1['datekey']]) {
369 $dates[$results1['datekey']] = array();
371 if (!$dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]) {
372 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']] = array();
373 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['name'] = $results1['fname'].' '.$results1['lname'];
374 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['date'] = $results1['date'];
375 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['pubpid'] = $results1['pubpid'];
376 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['dob'] = $results1['dob'];
377 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['vitals'] = '';
378 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['reason'] = $results1['reason'];
379 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['exam'] = array();
380 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['prescriptions'] = array();
381 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'] = array();
382 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['billing'] = array();
383 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['calories'] = array();
385 // get icd9 codes for this encounter
386 $query2 = sqlStatement("select * from billing where encounter = ".
387 $results1['enc']." and pid = ".$results1['pid']." and code_type like 'ICD9' and activity=1");
388 while ($results2 = mysql_fetch_array($query2, MYSQL_ASSOC)) {
389 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['billing'],
390 $results2['code'].' '.$results2['code_text']);
392 if (strtolower($results1['form_name']) == 'vitals') { // deal with Vitals
393 $query2 = sqlStatement("select * from form_vitals where id = " .
394 $results1['form_id']);
395 if ($results2 = mysql_fetch_array($query2, MYSQL_ASSOC)) {
396 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['vitals'] = formatVitals($results2);
399 if (substr(strtolower($results1['form_name']),0,5) == 'camos') { // deal with camos
400 $query2 = sqlStatement("select category,subcategory,item,content,date_format(date,'%h:%i %p') as date from form_CAMOS where id = " .
401 $results1['form_id']);
402 if ($results2 = mysql_fetch_array($query2, MYSQL_ASSOC)) {
403 if ($results2['category'] == 'exam') {
404 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['exam'],$results2['content']);
406 elseif ($results2['category'] == 'prescriptions') {
407 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['prescriptions'],preg_replace("/\n+/",' ',$results2['content']));
409 elseif ($results2['category'] == 'communications') {
410 //do nothing
412 elseif ($results2['category'] == 'calorie intake') {
413 $values = array('subcategory' => $results2['subcategory'],
414 'item' => $results2['item'],
415 'content' => $results2['content'],
416 'date' => $results2['date']);
417 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['calories'],$values);
420 else {
421 if (!$dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'][$results2['category']]) {
422 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'][$results2['category']] = array();
424 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'][$results2['category']],
425 preg_replace(array("/\n+/","/patientname/i"),array(' ',$results1['fname'].' '.$results1['lname']),$results2['content']));
430 return $dates;
432 function formatVitals($raw) { //pass raw vitals array, format and return as string
433 $height = '';
434 $weight = '';
435 $bmi = '';
436 $temp= '';
437 $bp = '';
438 $pulse = '';
439 $respiration = '';
440 $oxygen_saturation = '';
441 if ($raw['height'] && $raw['height'] > 0) {
442 $height = xl("HT").": ".$raw['height']." ";
444 if ($raw['weight'] && $raw['weight'] > 0) {
445 $weight = xl("WT").": ".$raw['weight']." ";
447 if ($raw['BMI'] && $raw['BMI'] > 0) {
448 $bmi = xl("BMI").": ".$raw['BMI']." ";
450 if ($raw['temperature'] && $raw['temperature'] > 0) {
451 $temp = xl("Temp").": ".$raw['temperature']." ";
453 if ($raw['bps'] && $raw['bpd'] && $raw['bps'] > 0 && $raw['bpd'] > 0) {
454 $bp = xl("BP").": ".$raw['bps']."/".$raw['bpd']." ";
456 if ($raw['pulse'] && $raw['pulse'] > 0) {
457 $pulse = xl("Pulse").": ".$raw['pulse']." ";
459 if ($raw['respiration'] && $raw['respiration'] > 0) {
460 $respiration = xl("Respiration").": ".$raw['respiration']." ";
462 if ($raw['oxygen_saturation'] && $raw['oxygen_saturation'] > 0) {
463 $oxygen_saturation = xl("O2 Sat").": ".$raw['oxygen_saturation']."% ";
465 $ret = $height.$weight.$bmi.$temp.$bp.
466 $pulse.$respiration.$oxygen_saturation;
467 if ($ret != '') {
468 $ret = xl("Vital Signs").": ".$ret;
470 return $ret;