fix bug in use of end date for deactivation
[openemr.git] / interface / patient_file / summary / add_edit_issue.php
blobb602552340ff73ca9ddf819b6cbf96373e3f5abf
1 <?php
2 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 include_once("../../globals.php");
10 include_once("$srcdir/lists.inc");
11 include_once("$srcdir/patient.inc");
12 include_once("$srcdir/acl.inc");
14 $issue = $_REQUEST['issue'];
15 $info_msg = "";
17 $thisauth = acl_check('patients', 'med');
18 if ($issue && $thisauth != 'write') die("Edit is not authorized!");
19 if ($thisauth != 'write' && $thisauth != 'addonly') die("Add is not authorized!");
21 $tmp = getPatientData($pid, "squad");
22 if ($tmp['squad'] && ! acl_check('squads', $tmp['squad']))
23 die("Not authorized for this squad!");
25 $arroccur = array(
26 0 => 'Unknown or N/A',
27 1 => 'First',
28 2 => 'Second',
29 3 => 'Third',
30 4 => 'Chronic/Recurrent',
31 5 => 'Acute on Chronic'
34 function QuotedOrNull($fld) {
35 if ($fld) return "'$fld'";
36 return "NULL";
39 function rbinput($name, $value, $desc, $colname) {
40 global $irow;
41 $ret = "<input type='radio' name='$name' value='$value'";
42 if ($irow[$colname] == $value) $ret .= " checked";
43 $ret .= " />$desc";
44 return $ret;
47 function rbvalue($rbname) {
48 $tmp = $_POST[$rbname];
49 if (! $tmp) $tmp = '0';
50 return "'$tmp'";
54 <html>
55 <head>
56 <title><? echo $issue ? "Edit" : "Add New" ?> Issue</title>
57 <link rel=stylesheet href='<? echo $css_header ?>' type='text/css'>
59 <style>
60 td { font-size:10pt; }
61 </style>
63 <script type="text/javascript" src="../../../library/overlib_mini.js"></script>
64 <script type="text/javascript" src="../../../library/calendar.js"></script>
65 <script type="text/javascript" src="../../../library/textformat.js"></script>
66 <script type="text/javascript" src="../../../library/dialog.js"></script>
68 <script language="JavaScript">
70 var mypcc = '<? echo $GLOBALS['phone_country_code'] ?>';
72 var aitypes = new Array(); // issue type attributes
73 var aopts = new Array(); // Option objects
74 <?php
75 // "Clickoptions" is a feature by Mark Leeds that provides for one-click
76 // access to preselected lists of issues in each category. Here we get
77 // the issue titles from the user-customizable file and write JavaScript
78 // statements that will build an array of arrays of Option objects.
80 $clickoptions = array();
81 if (is_file("../../../custom/clickoptions.txt"))
82 $clickoptions = file("../../../custom/clickoptions.txt");
83 $i = 0;
84 foreach ($ISSUE_TYPES as $key => $value) {
85 echo " aitypes[$i] = " . $value[3] . ";\n";
86 echo " aopts[$i] = new Array();\n";
87 foreach($clickoptions as $line) {
88 $line = trim($line);
89 if (substr($line, 0, 1) != "#") {
90 if (strpos($line, $key) !== false) {
91 $text = addslashes(substr($line, strpos($line, "::") + 2));
92 echo " aopts[$i][aopts[$i].length] = new Option('$text', '$text', false, false);\n";
96 ++$i;
100 // React to selection of an issue type. This loads the associated
101 // shortcuts into the selection list of titles, and determines which
102 // rows are displayed or hidden.
103 function newtype(index) {
104 var f = document.forms[0];
105 var theopts = f.form_titles.options;
106 theopts.length = 0;
107 for (i = 0; i < aopts[index].length; ++i) {
108 theopts[i] = aopts[index][i];
110 // Show or hide various rows depending on issue type, except do not
111 // hide the comments or referred-by fields if they have data.
112 var comdisp = (aitypes[index] == 1) ? 'none' : '';
113 var revdisp = (aitypes[index] == 1) ? '' : 'none';
114 document.getElementById('row_enddate' ).style.display = comdisp;
115 document.getElementById('row_active' ).style.display = revdisp;
116 document.getElementById('row_diagnosis' ).style.display = comdisp;
117 document.getElementById('row_occurrence').style.display = comdisp;
118 document.getElementById('row_referredby').style.display = (f.form_referredby.value) ? '' : comdisp;
119 document.getElementById('row_comments' ).style.display = (f.form_comments.value) ? '' : revdisp;
120 <?php if ($GLOBALS['athletic_team']) { ?>
121 document.getElementById('row_returndate').style.display = comdisp;
122 document.getElementById('row_missed' ).style.display = comdisp;
123 <?php } ?>
126 // If a clickoption title is selected, copy it to the title field.
127 function set_text() {
128 var f = document.forms[0];
129 f.form_title.value = f.form_titles.options[f.form_titles.selectedIndex].text;
130 f.form_titles.selectedIndex = -1;
133 // Process click on Delete link.
134 function deleteme() {
135 dlgopen('../deleter.php?issue=<?php echo $issue ?>', '_blank', 500, 450);
136 return false;
139 // Called by the deleteme.php window on a successful delete.
140 function imdeleted() {
141 window.close();
144 // Called when the Active checkbox is clicked. For consistency we
145 // use the existence of an end date to indicate inactivity, even
146 // though the simple verion of the form does not show an end date.
147 function activeClicked(cb) {
148 var f = document.forms[0];
149 if (cb.checked) {
150 f.form_end.value = '';
151 } else {
152 var today = new Date();
153 f.form_end.value = '' + (today.getYear() + 1900) + '-' +
154 (today.getMonth() + 1) + '-' + today.getDate();
158 </script>
160 </head>
162 <body <?echo $top_bg_line;?>>
163 <?php
164 // If we are saving, then save and close the window.
166 if ($_POST['form_save']) {
168 $i = 0;
169 $text_type = "unknown";
170 foreach ($ISSUE_TYPES as $key => $value) {
171 if ($i++ == $_POST['form_type']) $text_type = $key;
174 $form_begin = fixDate($_POST['form_begin'], '');
175 $form_end = fixDate($_POST['form_end'], '');
177 if ($issue) {
179 $query = "UPDATE lists SET " .
180 "type = '" . $text_type . "', " .
181 "title = '" . $_POST['form_title'] . "', " .
182 "comments = '" . $_POST['form_comments'] . "', " .
183 "begdate = " . QuotedOrNull($form_begin) . ", " .
184 "enddate = " . QuotedOrNull($form_end) . ", " .
185 "returndate = " . QuotedOrNull($form_return) . ", " .
186 "diagnosis = '" . $_POST['form_diagnosis'] . "', " .
187 "occurrence = '" . $_POST['form_occur'] . "', " .
188 "referredby = '" . $_POST['form_referredby'] . "', " .
189 "extrainfo = '" . $_POST['form_missed'] . "', " .
190 "outcome = " . rbvalue('form_outcome') . ", " .
191 // "destination = " . rbvalue('form_destination') . " " . // radio button version
192 "destination = '" . $_POST['form_destination'] . "' " .
193 "WHERE id = '$issue'";
194 sqlStatement($query);
195 if ($text_type == "medication" && enddate != '') {
196 sqlStatement('UPDATE prescriptions SET '
197 . 'medication = 0 where patient_id = ' . $pid
198 . " and upper(trim(drug)) = '" . strtoupper($_POST['form_title']) . "' "
199 . ' and medication = 1' );
202 } else {
204 $issue = sqlInsert("INSERT INTO lists ( " .
205 "date, pid, type, title, activity, comments, begdate, enddate, returndate, " .
206 "diagnosis, occurrence, referredby, extrainfo, user, groupname, " .
207 "outcome, destination " .
208 ") VALUES ( " .
209 "NOW(), " .
210 "'$pid', " .
211 "'" . $text_type . "', " .
212 "'" . $_POST['form_title'] . "', " .
213 "1, " .
214 "'" . $_POST['form_comments'] . "', " .
215 QuotedOrNull($form_begin) . ", " .
216 QuotedOrNull($form_end) . ", " .
217 QuotedOrNull($form_return) . ", " .
218 "'" . $_POST['form_diagnosis'] . "', " .
219 "'" . $_POST['form_occur'] . "', " .
220 "'" . $_POST['form_referredby'] . "', " .
221 "'" . $_POST['form_missed'] . "', " .
222 "'" . $$_SESSION['authUser'] . "', " .
223 "'" . $$_SESSION['authProvider'] . "', " .
224 rbvalue('form_outcome') . ", " .
225 // rbvalue('form_destination') . " " . // radio button version
226 "'" . $_POST['form_destination'] . "' " .
227 ")");
231 $tmp_title = $ISSUE_TYPES[$text_type][2] . ": $form_begin " .
232 substr($_POST['form_title'], 0, 40);
234 // Close this window and redisplay the updated list of issues.
236 echo "<script language='JavaScript'>\n";
237 if ($info_msg) echo " alert('$info_msg');\n";
238 echo " window.close();\n";
239 // echo " opener.location.reload();\n";
240 echo " if (opener.refreshIssue) opener.refreshIssue($issue,'$tmp_title');\n";
241 echo "</script></body></html>\n";
242 exit();
245 $irow = array();
246 $type_index = 0;
248 if ($issue) {
249 $irow = sqlQuery("SELECT * FROM lists WHERE id = $issue");
250 foreach ($ISSUE_TYPES as $key => $value) {
251 if ($key == $irow['type']) break;
252 ++$type_index;
254 // Get all of the eligible diagnoses.
255 // We include the pid in this search for better performance,
256 // because it's part of the primary key:
257 $bres = sqlStatement(
258 "SELECT DISTINCT billing.code, billing.code_text " .
259 "FROM issue_encounter, billing WHERE " .
260 "issue_encounter.pid = '$pid' AND " .
261 "issue_encounter.list_id = '$issue' AND " .
262 "billing.encounter = issue_encounter.encounter AND " .
263 "( billing.code_type LIKE 'ICD%' OR " .
264 "billing.code_type LIKE 'OSICS' OR " .
265 "billing.code_type LIKE 'UCSMC' )"
269 <!-- Required for the popup date selectors -->
270 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
272 <form method='post' name='theform' action='add_edit_issue.php?issue=<? echo $issue ?>'>
273 <center>
275 <table border='0' width='100%'>
277 <tr>
278 <td valign='top' width='1%' nowrap><b><? xl('Type','e'); ?>:</b></td>
279 <td>
280 <?php
281 $index = 0;
282 foreach ($ISSUE_TYPES as $value) {
283 echo " <input type='radio' name='form_type' value='$index' onclick='newtype($index)'";
284 if ($index == $type_index) echo " checked";
285 echo " />" . $value[1] . "&nbsp;\n";
286 ++$index;
289 </td>
290 </tr>
292 <tr>
293 <td valign='top' nowrap><b><? xl('Title','e'); ?>:</b></td>
294 <td>
295 <select name='form_titles' size='4' onchange='set_text()' style='width:100%'>
296 </select><br>
297 <input type='text' size='40' name='form_title' value='<? echo $irow['title'] ?>' style='width:100%' />
298 </td>
299 </tr>
301 <tr>
302 <td valign='top' nowrap><b><? xl('Begin Date','e'); ?>:</b></td>
303 <td>
304 <input type='text' size='10' name='form_begin' value='<? echo $irow['begdate'] ?>'
305 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
306 title='yyyy-mm-dd date of onset, surgery or start of medication' />
307 <a href="javascript:show_calendar('theform.form_begin')"
308 title="Click here to choose a date"
309 ><img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22' border='0'></a>
310 </td>
311 </tr>
313 <tr id='row_enddate'>
314 <td valign='top' nowrap><b><? xl('End Date','e'); ?>:</b></td>
315 <td>
316 <input type='text' size='10' name='form_end' value='<? echo $irow['enddate'] ?>'
317 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
318 title='yyyy-mm-dd date of recovery or end of medication' />
319 <a href="javascript:show_calendar('theform.form_end')"
320 title="Click here to choose a date"
321 ><img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22' border='0'></a>
322 &nbsp;(<? xl('leave blank if still active','e'); ?>)
323 </td>
324 </tr>
326 <tr id='row_active'>
327 <td valign='top' nowrap><b><? xl('Active','e'); ?>:</b></td>
328 <td>
329 <input type='checkbox' name='form_active' value='1' <? echo $irow['enddate'] ? "" : "checked"; ?>
330 onclick='activeClicked(this);'
331 title='Indicates if this issue is currently active' />
332 </td>
333 </tr>
335 <tr<?php if (! $GLOBALS['athletic_team']) echo " style='display:none;'"; ?> id='row_returndate'>
336 <td valign='top' nowrap><b><? xl('Returned to Play','e'); ?>:</b></td>
337 <td>
338 <input type='text' size='10' name='form_return' value='<? echo $irow['returndate'] ?>'
339 onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'
340 title='yyyy-mm-dd date returned to play' />
341 <a href="javascript:show_calendar('theform.form_return')"
342 title="Click here to choose a date"
343 ><img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22' border='0'></a>
344 &nbsp;(<? xl('leave blank if still active','e'); ?>)
345 </td>
346 </tr>
348 <tr id='row_diagnosis'>
349 <td valign='top' nowrap><b><? xl('Diagnosis','e'); ?>:</b></td>
350 <td>
351 <select name='form_diagnosis' title='Diagnosis must be coded into a linked encounter'>
352 <option value=""><? xl('Unknown or N/A','e'); ?></option>
353 <?php
354 while ($brow = sqlFetchArray($bres)) {
355 echo " <option value='" . $brow['code'] . "'";
356 if ($brow['code'] == $irow['diagnosis']) echo " selected";
357 echo ">" . $brow['code'] . " " . substr($brow['code_text'], 0, 40) . "</option>\n";
360 </select>
361 </td>
362 </tr>
364 <tr id='row_occurrence'>
365 <td valign='top' nowrap><b><? xl('Occurrence','e'); ?>:</b></td>
366 <td>
367 <select name='form_occur'>
368 <?php
369 foreach ($arroccur as $key => $value) {
370 echo " <option value='$key'";
371 if ($key == $irow['occurrence']) echo " selected";
372 echo ">$value\n";
375 </select>
376 </td>
377 </tr>
379 <tr<?php if (! $GLOBALS['athletic_team']) echo " style='display:none;'"; ?> id='row_missed'>
380 <td valign='top' nowrap><b><? xl('Missed','e'); ?>:</b></td>
381 <td>
382 <input type='text' size='3' name='form_missed' value='<? echo $irow['extrainfo'] ?>'
383 title='Number of games or events missed, if any' />
384 &nbsp;<? xl('games/events','e'); ?>
385 </td>
386 </tr>
388 <tr<?php if ($GLOBALS['athletic_team']) echo " style='display:none;'"; ?> id='row_referredby'>
389 <td valign='top' nowrap><b><? xl('Referred by','e'); ?>:</b></td>
390 <td>
391 <input type='text' size='40' name='form_referredby' value='<? echo $irow['referredby'] ?>'
392 style='width:100%' title='Referring physician and practice' />
393 </td>
394 </tr>
396 <tr id='row_comments'>
397 <td valign='top' nowrap><b><? xl('Comments','e'); ?>:</b></td>
398 <td>
399 <textarea name='form_comments' rows='4' cols='40' wrap='virtual' style='width:100%'><? echo $irow['comments'] ?></textarea>
400 </td>
401 </tr>
403 <tr<?php if ($GLOBALS['athletic_team']) echo " style='display:none;'"; ?>>
404 <td valign='top' nowrap><b><? xl('Outcome','e'); ?>:</b></td>
405 <td>
406 <? echo rbinput('form_outcome', '1', 'Resolved' , 'outcome') ?>&nbsp;
407 <? echo rbinput('form_outcome', '2', 'Improved' , 'outcome') ?>&nbsp;
408 <? echo rbinput('form_outcome', '3', 'Status quo' , 'outcome') ?>&nbsp;
409 <? echo rbinput('form_outcome', '4', 'Worse' , 'outcome') ?>&nbsp;
410 <? echo rbinput('form_outcome', '5', 'Pending followup', 'outcome') ?>
411 </td>
412 </tr>
414 <tr<?php if ($GLOBALS['athletic_team']) echo " style='display:none;'"; ?>>
415 <td valign='top' nowrap><b><? xl('Destination','e'); ?>:</b></td>
416 <td>
417 <?php if (true) { ?>
418 <input type='text' size='40' name='form_destination' value='<? echo $irow['destination'] ?>'
419 style='width:100%' title='GP, Secondary care specialist, etc.' />
420 <?php } else { // leave this here for now, please -- Rod ?>
421 <? echo rbinput('form_destination', '1', 'GP' , 'destination') ?>&nbsp;
422 <? echo rbinput('form_destination', '2', 'Secondary care spec', 'destination') ?>&nbsp;
423 <? echo rbinput('form_destination', '3', 'GP via physio' , 'destination') ?>&nbsp;
424 <? echo rbinput('form_destination', '4', 'GP via podiatry' , 'destination') ?>
425 <?php } ?>
426 </td>
427 </tr>
429 </table>
432 <input type='submit' name='form_save' value='Save' />
434 <?php if ($issue && acl_check('admin', 'super')) { ?>
435 &nbsp;
436 <input type='button' value='Delete' style='color:red' onclick='deleteme()' />
437 <?php } ?>
439 &nbsp;
440 <input type='button' value='Cancel' onclick='window.close()' />
441 </p>
443 </center>
444 </form>
445 <script language='JavaScript'>
446 newtype(<? echo $type_index ?>);
447 </script>
448 </body>
449 </html>