Highway to PSR2
[openemr.git] / interface / forms / CAMOS / notegen.php
blob740973178e26566eabf19eb9095ff8a931add7e2
1 <?php
2 /**
3 * CAMOS note generator.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Mark Leeds <drleeds@gmail.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2006-2009 Mark Leeds <drleeds@gmail.com>
10 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 $depth = '../../../';
16 require_once($depth.'interface/globals.php');
17 require_once("content_parser.php");
19 <?php
20 if (!($_POST['submit_pdf'] || $_POST['submit_html']) && ($_GET['pid'] && $_GET['encounter'])) {
22 <html>
23 <head>
24 <title>
25 <?php xl('Print Notes', 'e'); ?>
26 </title>
27 </head>
28 <body>
29 <?php xl('Choose print format for this encounter report.', 'e'); ?><br><br>
30 <form method=post name=choose_patients>
31 <input type='submit' name='submit_pdf' value='<?php xl('Print (PDF)', 'e'); ?>'>
32 <input type='submit' name='submit_html' value='<?php xl('Print (HTML)', 'e'); ?>'>
33 </form>
34 </body>
35 </html>
36 <?php
37 exit;
40 if (!$_POST['submit_pdf'] && !$_POST['submit_html'] && !($_GET['pid'] && $_GET['encounter'])) {
42 <html>
43 <head>
45 <title>
46 <?php xl('Print Notes', 'e'); ?>
47 </title>
49 <?php html_header_show();?>
50 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
51 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
53 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
54 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
55 <script type="text/javascript" src="<?php echo $depth ?>library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
56 <script type="text/javascript" src="<?php echo $depth ?>library/textformat.js?v=<?php echo $v_js_includes; ?>"></script>
58 <script type="text/javascript">
59 $(document).ready(function(){
60 $('.datepicker').datetimepicker({
61 <?php $datetimepicker_timepicker = false; ?>
62 <?php $datetimepicker_showseconds = false; ?>
63 <?php $datetimepicker_formatInput = false; ?>
64 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
65 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
66 });
67 });
68 </script>
70 </head>
72 <body>
73 <script language='JavaScript'> var mypcc = '1'; </script>
75 <form method=post name=choose_patients>
77 <table>
78 <tr><td>
79 <span class='text'><?php xl('Start (yyyy-mm-dd): ', 'e') ?></span>
80 </td><td>
81 <input type='text' size='10' name='start' id='start' value='<?php echo $_POST['end'] ? $_POST['end'] : date('Y-m-d') ?>'
82 class='datepicker'
83 title='<?php xl('yyyy-mm-dd last date of this event', 'e'); ?>' />
84 </td></tr>
85 <tr><td>
86 <span class='text'><?php xl('End (yyyy-mm-dd): ', 'e') ?></span>
87 </td><td>
88 <input type='text' size='10' name='end' id='end' value ='<?php echo $_POST['end'] ? $_POST['end'] : date('Y-m-d') ?>'
89 class='datepicker'
90 title='<?php xl('yyyy-mm-dd last date of this event', 'e'); ?>' />
91 </td></tr>
92 <tr><td></td><td></td></tr>
93 <tr><td><?php xl('Last Name', 'e'); ?>: </td><td>
94 <input type='text' name='lname'/>
95 </td></tr>
96 <tr><td><?php xl('First Name', 'e'); ?>: </td><td>
97 <input type='text' name='fname'/>
98 </td></tr>
99 <tr><td>
100 <input type='submit' name='submit_pdf' value='<?php xl('Print (PDF)', 'e'); ?>'>
101 <input type='submit' name='submit_html' value='<?php xl('Print (HTML)', 'e'); ?>'>
102 </td><td>
103 </td></tr>
104 </table>
105 </form>
106 </body>
107 </html>
108 <?php
111 if ($_POST['submit_pdf'] || $_POST['submit_html'] || ($_GET['pid'] && $_GET['encounter'])) {
112 // note we are cleaning input, trimming, and preparing the variables for database insert
113 // before sending through this function
114 $output = getFormData(formData("start", "P", true), formData("end", "P", true), formData("lname", "P", true), formData("fname", "P", true));
115 ksort($output);
116 if ($_POST['submit_html']) { //print as html
118 <html>
119 <head>
120 <style>
121 body {
122 font-family: sans-serif;
123 font-weight: normal;
124 font-size: 8pt;
125 background: white;
126 color: black;
128 .paddingdiv {
129 width: 524pt;
130 padding: 0pt;
132 .navigate {
133 margin-top: 2.5em;
135 @media print {
136 .navigate {
137 display: none;
140 div.page {
141 page-break-after: always;
142 padding: 0pt;
143 margin-top: 50pt;
145 span.heading {
146 font-weight: bold;
147 font-size: 130%;
149 </style>
150 <title><?php xl('Patient Notes', 'e'); ?></title>
151 </head>
152 <body>
153 <div class='paddingdiv'>
154 <?php
155 foreach ($output as $datekey => $dailynote) {
156 foreach ($dailynote as $note_id => $notecontents) {
157 preg_match('/(\d+)_(\d+)/', $note_id, $matches); //the unique note id contains the pid and encounter
158 $pid = $matches[1];
159 $enc = $matches[2];
161 //new page code here
162 print "<DIV class='page'>";
164 print xl("Date").": ".$notecontents['date'] . "<br/>";
165 print xl("Name").": ".$notecontents['name'] . "<br/>";
166 print xl("DOB").": ".$notecontents['dob'] . "<br/>";
167 print xl("Claim")."# ".$notecontents['pubpid'] . "<br/>";
169 print "<br/>";
170 print xl("Chief Complaint").": ".$notecontents['reason'] . "<br/>";
171 if ($notecontents['vitals']) {
172 print "<br/>";
173 print $notecontents['vitals'] . "<br/>";
176 if (count($notecontents['exam']) > 0) {
177 print "<br/>";
178 print "<span class='heading'>" . xl("Progress Notes") . "</span><br/>";
179 print "<br/>";
180 foreach ($notecontents['exam'] as $examnote) {
181 print nl2br(replace($pid, $enc, $examnote)) . "<br/>";
185 if (count($notecontents['prescriptions']) > 0) {
186 print "<br/>";
187 print "<span class='heading'>" . xl("Prescriptions") . "</span><br/>";
188 print "<br/>";
189 foreach ($notecontents['prescriptions'] as $rx) {
190 print nl2br(replace($pid, $enc, $rx)) . "<br/>";
194 if (count($notecontents['other']) > 0) {
195 print "<br/>";
196 print "<span class='heading'>" . xl("Other") . "</span><br/>";
197 print "<br/>";
198 foreach ($notecontents['other'] as $other => $othercat) {
199 print nl2br($other) . "<br/>";
200 foreach ($othercat as $items) {
201 print nl2br(replace($pid, $enc, $items)) . "<br/>";
206 if (count($notecontents['billing']) > 0) {
207 $tmp = array();
208 foreach ($notecontents['billing'] as $code) {
209 $tmp[$code]++;
212 if (count($tmp) > 0) {
213 print "<br/>";
214 print "<span class='heading'>" . xl("Coding") . "</span><br/>";
215 print "<br/>";
216 foreach ($tmp as $code => $val) {
217 print nl2br($code) . "<br/>";
222 if (count($notecontents['calories']) > 0) {
223 $sum = 0;
224 print "<br/>";
225 print "<span class='heading'>" . xl("Calories") . "</span><br/>";
226 print "<br/>";
227 foreach ($notecontents['calories'] as $calories => $value) {
228 print $value['content'].' - '.$value['item'].' - '.$value['date'] . "<br/>";
229 $sum += $value['content'];
232 print "--------" . "<br/>";
233 print $sum . "<br/>";
236 print "<br/>";
237 print "<br/>";
238 print "<span class='heading'>" . xl("Digitally Signed") . "</span><br/>";
240 $query = sqlStatement("select t2.id, t2.fname, t2.lname, t2.title from forms as t1 join users as t2 on " .
241 "(t1.user like t2.username) where t1.pid=$pid and t1.encounter=$encounter");
242 if ($results = sqlFetchArray($query)) {
243 $name = $results['fname']." ".$results['lname'].", ".$results['title'];
244 $user_id = $results['id'];
247 $path = $GLOBALS['fileroot']."/interface/forms/CAMOS";
248 if (file_exists($path."/sig".$user_id.".jpg")) {
249 //show the image here
252 print "<span class='heading'>" . $name . "</span><br/>";
253 print "</DIV>"; //end of last page
257 <script language='JavaScript'>
258 var win = top.printLogPrint ? top : opener.top;
259 win.printLogPrint(window);
260 </script>
261 </div>
262 </body>
263 </html>
264 <?php
265 exit;
266 } else { // print as pdf
267 $pdf = new Cezpdf();
268 $pdf->selectFont('Helvetica');
269 $pdf->ezSetCmMargins(3, 1, 1, 1);
270 $first = 1;
271 foreach ($output as $datekey => $dailynote) {
272 foreach ($dailynote as $note_id => $notecontents) {
273 preg_match('/(\d+)_(\d+)/', $note_id, $matches); //the unique note id contains the pid and encounter
274 $pid = $matches[1];
275 $enc = $matches[2];
276 if (!$first) { //generate a new page each time except first iteration when nothing has been printed yet
277 $pdf->ezNewPage();
278 } else {
279 $first = 0;
282 $pdf->ezText(xl("Date").": ".$notecontents['date'], 8);
283 $pdf->ezText(xl("Name").": ".$notecontents['name'], 8);
284 $pdf->ezText(xl("DOB").": ".$notecontents['dob'], 8);
285 $pdf->ezText(xl("Claim")."# ".$notecontents['pubpid'], 8);
287 $pdf->ezText("", 8);
288 $pdf->ezText(xl("Chief Complaint").": ".$notecontents['reason'], 8);
289 if ($notecontents['vitals']) {
290 $pdf->ezText("", 8);
291 $pdf->ezText($notecontents['vitals'], 8);
294 if (count($notecontents['exam']) > 0) {
295 $pdf->ezText("", 8);
296 $pdf->ezText(xl("Progress Notes"), 12);
297 $pdf->ezText("", 8);
298 foreach ($notecontents['exam'] as $examnote) {
299 $pdf->ezText(replace($pid, $enc, $examnote));
303 if (count($notecontents['prescriptions']) > 0) {
304 $pdf->ezText("", 8);
305 $pdf->ezText(xl("Prescriptions"), 12);
306 $pdf->ezText("", 8);
307 foreach ($notecontents['prescriptions'] as $rx) {
308 $pdf->ezText(replace($pid, $enc, $rx));
312 if (count($notecontents['other']) > 0) {
313 $pdf->ezText("", 8);
314 $pdf->ezText(xl("Other"), 12);
315 $pdf->ezText("", 8);
316 foreach ($notecontents['other'] as $other => $othercat) {
317 $pdf->ezText($other, 8);
318 foreach ($othercat as $items) {
319 $pdf->ezText(replace($pid, $enc, $items), 8);
324 if (count($notecontents['billing']) > 0) {
325 $tmp = array();
326 foreach ($notecontents['billing'] as $code) {
327 $tmp[$code]++;
330 if (count($tmp) > 0) {
331 $pdf->ezText("", 8);
332 $pdf->ezText(xl("Coding"), 12);
333 $pdf->ezText("", 8);
334 foreach ($tmp as $code => $val) {
335 $pdf->ezText($code, 8);
340 if (count($notecontents['calories']) > 0) {
341 $sum = 0;
342 $pdf->ezText("", 8);
343 $pdf->ezText(xl("Calories"), 12);
344 $pdf->ezText("", 8);
345 foreach ($notecontents['calories'] as $calories => $value) {
346 $pdf->ezText($value['content'].' - '.$value['item'].' - '.$value['date'], 8);
347 $sum += $value['content'];
350 $pdf->ezText("--------", 8);
351 $pdf->ezText($sum, 8);
354 $pdf->ezText("", 12);
355 $pdf->ezText("", 12);
356 $pdf->ezText(xl("Digitally Signed"), 12);
358 $query = sqlStatement("select t2.id, t2.fname, t2.lname, t2.title from forms as t1 join users as t2 on " .
359 "(t1.user like t2.username) where t1.pid=$pid and t1.encounter=$encounter");
360 if ($results = sqlFetchArray($query)) {
361 $name = $results['fname']." ".$results['lname'].", ".$results['title'];
362 $user_id = $results['id'];
365 $path = $GLOBALS['fileroot']."/interface/forms/CAMOS";
366 if (file_exists($path."/sig".$user_id.".jpg")) {
367 $pdf->ezImage($path."/sig".$user_id.".jpg", '', '72', '', 'left', '');
370 $pdf->ezText($name, 12);
374 $pdf->ezStream();
378 function getFormData($start_date, $end_date, $lname, $fname)
380 //dates in sql format
382 // All 4 parameters have previously been trimmed, globally validated,
383 // and prepared for database insert
385 $name_clause = '';
386 $date_clause = "date(t2.date) >= '".$start_date."' and date(t2.date) <= '".$end_date."' ";
387 if ($lname || $fname) {
388 $name_clause = "and t3.lname like '%".$lname."%' and t3.fname like '%".$fname."%' ";
391 $dates = array();
392 if ($_GET['pid'] && $_GET['encounter']) {
393 $date_clause = '';
394 $name_clause = "t2.pid=".$_GET['pid']." and t2.encounter=".$_GET['encounter']." ";
397 $query1 = sqlStatement(
398 "select t1.form_id, t1.form_name, t1.pid, date_format(t2.date,'%m-%d-%Y') as date, " .
399 "date_format(t2.date,'%Y%m%d') as datekey, " .
400 "t3.lname, t3.fname, t3.pubpid, date_format(t3.DOB,'%m-%d-%Y') as dob, " .
401 "t2.encounter as enc, " .
402 "t2.reason from " .
403 "forms as t1 join " .
404 "form_encounter as t2 on " .
405 "(t1.pid = t2.pid and t1.encounter = t2.encounter) " .
406 "join patient_data as t3 on " .
407 "(t1.pid = t3.pid) where " .
408 $date_clause .
409 $name_clause .
410 "order by date,pid"
412 while ($results1 = sqlFetchArray($query1)) {
413 if (!$dates[$results1['datekey']]) {
414 $dates[$results1['datekey']] = array();
417 if (!$dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]) {
418 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']] = array();
419 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['name'] = $results1['fname'].' '.$results1['lname'];
420 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['date'] = $results1['date'];
421 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['pubpid'] = $results1['pubpid'];
422 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['dob'] = $results1['dob'];
423 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['vitals'] = '';
424 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['reason'] = $results1['reason'];
425 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['exam'] = array();
426 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['prescriptions'] = array();
427 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'] = array();
428 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['billing'] = array();
429 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['calories'] = array();
432 // get icd9 codes for this encounter
433 $query2 = sqlStatement("select * from billing where encounter = ".
434 $results1['enc']." and pid = ".$results1['pid']." and code_type like 'ICD9' and activity=1");
435 while ($results2 = sqlFetchArray($query2)) {
436 array_push(
437 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['billing'],
438 $results2['code'].' '.$results2['code_text']
442 if (strtolower($results1['form_name']) == 'vitals') { // deal with Vitals
443 $query2 = sqlStatement("select * from form_vitals where id = " .
444 $results1['form_id']);
445 if ($results2 = sqlFetchArray($query2)) {
446 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['vitals'] = formatVitals($results2);
450 if (substr(strtolower($results1['form_name']), 0, 5) == 'camos') { // deal with camos
451 $query2 = sqlStatement("select category,subcategory,item,content,date_format(date,'%h:%i %p') as date from ".mitigateSqlTableUpperCase("form_CAMOS")." where id = " .
452 $results1['form_id']);
453 if ($results2 = sqlFetchArray($query2)) {
454 if ($results2['category'] == 'exam') {
455 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['exam'], $results2['content']);
456 } elseif ($results2['category'] == 'prescriptions') {
457 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['prescriptions'], preg_replace("/\n+/", ' ', $results2['content']));
458 } elseif ($results2['category'] == 'communications') {
459 //do nothing
460 } elseif ($results2['category'] == 'calorie intake') {
461 $values = array('subcategory' => $results2['subcategory'],
462 'item' => $results2['item'],
463 'content' => $results2['content'],
464 'date' => $results2['date']);
465 array_push($dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['calories'], $values);
466 } else {
467 if (!$dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'][$results2['category']]) {
468 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'][$results2['category']] = array();
471 array_push(
472 $dates[$results1['datekey']][$results1['pid'].'_'.$results1['enc']]['other'][$results2['category']],
473 preg_replace(array("/\n+/","/patientname/i"), array(' ',$results1['fname'].' '.$results1['lname']), $results2['content'])
480 return $dates;
482 function formatVitals($raw)
484 //pass raw vitals array, format and return as string
485 $height = '';
486 $weight = '';
487 $bmi = '';
488 $temp= '';
489 $bp = '';
490 $pulse = '';
491 $respiration = '';
492 $oxygen_saturation = '';
493 if ($raw['height'] && $raw['height'] > 0) {
494 $height = xl("HT").": ".$raw['height']." ";
497 if ($raw['weight'] && $raw['weight'] > 0) {
498 $weight = xl("WT").": ".$raw['weight']." ";
501 if ($raw['BMI'] && $raw['BMI'] > 0) {
502 $bmi = xl("BMI").": ".$raw['BMI']." ";
505 if ($raw['temperature'] && $raw['temperature'] > 0) {
506 $temp = xl("Temp").": ".$raw['temperature']." ";
509 if ($raw['bps'] && $raw['bpd'] && $raw['bps'] > 0 && $raw['bpd'] > 0) {
510 $bp = xl("BP").": ".$raw['bps']."/".$raw['bpd']." ";
513 if ($raw['pulse'] && $raw['pulse'] > 0) {
514 $pulse = xl("Pulse").": ".$raw['pulse']." ";
517 if ($raw['respiration'] && $raw['respiration'] > 0) {
518 $respiration = xl("Respiration").": ".$raw['respiration']." ";
521 if ($raw['oxygen_saturation'] && $raw['oxygen_saturation'] > 0) {
522 $oxygen_saturation = xl("O2 Sat").": ".$raw['oxygen_saturation']."% ";
525 $ret = $height.$weight.$bmi.$temp.$bp.
526 $pulse.$respiration.$oxygen_saturation;
527 if ($ret != '') {
528 $ret = xl("Vital Signs").": ".$ret;
531 return $ret;