3 * Encounter form for entering procedure orders.
5 * Copyright (C) 2010-2013 Rod Roark <rod@sunsetsystems.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
19 * @author Rod Roark <rod@sunsetsystems.com>
22 require_once("../../globals.php");
23 require_once("$srcdir/api.inc");
24 require_once("$srcdir/forms.inc");
25 require_once("$srcdir/options.inc.php");
26 require_once("$srcdir/formdata.inc.php");
27 require_once("$srcdir/formatting.inc.php");
28 require_once("../../orders/qoe.inc.php");
29 require_once("../../orders/gen_hl7_order.inc.php");
30 require_once("../../../custom/code_types.inc.php");
32 // Defaults for new orders.
34 'provider_id' => $_SESSION['authUserID'],
35 'date_ordered' => date('Y-m-d'),
36 'date_collected' => date('Y-m-d H:i'),
39 if (! $encounter) { // comes from globals.php
40 die("Internal error: we do not seem to be in an encounter!");
43 function cbvalue($cbname) {
44 return $_POST[$cbname] ?
'1' : '0';
47 function cbinput($name, $colname) {
49 $ret = "<input type='checkbox' name='$name' value='1'";
50 if ($row[$colname]) $ret .= " checked";
55 function cbcell($name, $desc, $colname) {
56 return "<td width='25%' nowrap>" . cbinput($name, $colname) . "$desc</td>\n";
59 function QuotedOrNull($fld) {
60 if (empty($fld)) return "NULL";
64 $formid = formData('id', 'G') +
0;
66 // If Save or Transmit was clicked, save the info.
68 if ($_POST['bn_save'] ||
$_POST['bn_xmit']) {
69 $ppid = formData('form_lab_id') +
0;
72 "date_ordered = " . QuotedOrNull(formData('form_date_ordered')) . ", " .
73 "provider_id = " . (formData('form_provider_id') +
0) . ", " .
74 "lab_id = " . $ppid . ", " .
75 "date_collected = " . QuotedOrNull(formData('form_date_collected')) . ", " .
76 "order_priority = '" . formData('form_order_priority') . "', " .
77 "order_status = '" . formData('form_order_status') . "', " .
78 "patient_instructions = '" . formData('form_patient_instructions') . "', " .
79 "patient_id = '" . $pid . "', " .
80 "encounter_id = '" . $encounter . "'";
82 // If updating an existing form...
85 $query = "UPDATE procedure_order SET $sets " .
86 "WHERE procedure_order_id = '$formid'";
90 // If adding a new form...
93 $query = "INSERT INTO procedure_order SET $sets";
94 $formid = sqlInsert($query);
95 addForm($encounter, "Procedure Order", $formid, "procedure_order", $pid, $userauthorized);
98 // Remove any existing procedures and their answers for this order and
99 // replace them from the form.
101 sqlStatement("DELETE FROM procedure_answers WHERE procedure_order_id = ?",
103 sqlStatement("DELETE FROM procedure_order_code WHERE procedure_order_id = ?",
106 for ($i = 0; isset($_POST['form_proc_type'][$i]); ++
$i) {
107 $ptid = $_POST['form_proc_type'][$i] +
0;
108 if ($ptid <= 0) continue;
110 $prefix = "ans$i" . "_";
112 $poseq = sqlInsert("INSERT INTO procedure_order_code SET ".
113 "procedure_order_id = ?, " .
115 "procedure_code = (SELECT procedure_code FROM procedure_type WHERE procedure_type_id = ?), " .
116 "procedure_name = (SELECT name FROM procedure_type WHERE procedure_type_id = ?)",
117 array($formid, strip_escape_custom($_POST['form_proc_type_diag'][$i]), $ptid, $ptid));
119 $qres = sqlStatement("SELECT " .
120 "q.procedure_code, q.question_code, q.options, q.fldtype " .
121 "FROM procedure_type AS t " .
122 "JOIN procedure_questions AS q ON q.lab_id = t.lab_id " .
123 "AND q.procedure_code = t.procedure_code AND q.activity = 1 " .
124 "WHERE t.procedure_type_id = ? " .
125 "ORDER BY q.seq, q.question_text", array($ptid));
127 while ($qrow = sqlFetchArray($qres)) {
128 $options = trim($qrow['options']);
129 $qcode = trim($qrow['question_code']);
130 $fldtype = $qrow['fldtype'];
132 if ($fldtype == 'G') {
133 if ($_POST["G1_$prefix$qcode"]) {
134 $data = $_POST["G1_$prefix$qcode"] * 7 +
$_POST["G2_$prefix$qcode"];
138 $data = $_POST["$prefix$qcode"];
140 if (!isset($data) ||
$data === '') continue;
141 if (!is_array($data)) $data = array($data);
142 foreach ($data as $datum) {
143 // Note this will auto-assign the seq value.
144 sqlStatement("INSERT INTO procedure_answers SET ".
145 "procedure_order_id = ?, " .
146 "procedure_order_seq = ?, " .
147 "question_code = ?, " .
149 array($formid, $poseq, $qcode, strip_escape_custom($datum)));
155 if ($_POST['bn_xmit']) {
157 $alertmsg = gen_hl7_order($formid, $hl7);
158 if (empty($alertmsg)) {
159 $alertmsg = send_hl7_order($ppid, $hl7);
163 // TBD: Implement and set a transmit date in the order.
164 // Add code elsewhere to show a warning if a previously transmitted order is opened.
166 formHeader("Redirecting....");
168 echo "\n<script language='Javascript'>alert('";
169 echo addslashes(xl('Transmit failed') . ': ' . $alertmsg);
170 echo "')</script>\n";
178 $row = sqlQuery ("SELECT * FROM procedure_order WHERE " .
179 "procedure_order_id = ?",
183 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date FROM " .
184 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
185 "p.pid = ? AND f.pid = p.pid AND f.encounter = ? AND " .
186 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
187 "fe.id = f.form_id LIMIT 1",
188 array($pid, $encounter));
192 <?php
html_header_show(); ?
>
193 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css" />
208 <style type
="text/css">@import
url(<?php
echo $GLOBALS['webroot'] ?
>/library
/dynarch_calendar
.css
);</style
>
209 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script
>
210 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
211 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script
>
213 <script type
="text/javascript" src
="../../../library/dialog.js"></script
>
214 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot'] ?>/library/textformat.js"></script
>
216 <script language
='JavaScript'>
218 // This invokes the find-procedure-type popup.
219 // formseq = 0-relative index in the form.
221 function sel_proc_type(formseq
) {
222 var f
= document
.forms
[0];
223 // if (!f.form_lab_id.value) {
224 // alert('<?php echo xls('Please select a procedure provider'); ?>');
227 gbl_formseq
= formseq
;
228 var ptvarname
= 'form_proc_type[' + formseq +
']';
229 /********************************************************************
230 dlgopen('../../orders/types.php?popup=1' +
231 '&labid=' + f.form_lab_id.value +
232 '&order=' + f[ptvarname].value +
233 '&formid=<?php echo $formid; ?>' +
234 '&formseq=' + formseq,
236 ********************************************************************/
237 // This replaces the above for an easier/faster order picker tool.
238 dlgopen('../../orders/find_order_popup.php' +
239 '?labid=' + f
.form_lab_id
.value +
240 '&order=' + f
[ptvarname
].value +
241 '&formid=<?php echo $formid; ?>' +
242 '&formseq=' + formseq
,
246 // This is for callback by the find-procedure-type popup.
247 // Sets both the selected type ID and its descriptive name.
248 function set_proc_type(typeid
, typename
) {
249 var f
= document
.forms
[0];
250 var ptvarname
= 'form_proc_type[' + gbl_formseq +
']';
251 var ptdescname
= 'form_proc_type_desc[' + gbl_formseq +
']';
252 f
[ptvarname
].value
= typeid
;
253 f
[ptdescname
].value
= typename
;
256 // This is also for callback by the find-procedure-type popup.
257 // Sets the contents of the table containing the form fields for questions.
258 function set_proc_html(s
, js
) {
259 document
.getElementById('qoetable[' + gbl_formseq +
']').innerHTML
= s
;
263 // New lab selected so clear all procedures and questions from the form.
264 function lab_id_changed() {
265 var f
= document
.forms
[0];
266 for (var i
= 0; true; ++i
) {
267 var ix
= '[' + i +
']';
268 if (!f
['form_proc_type' + ix
]) break;
269 f
['form_proc_type' + ix
].value
= '-1';
270 f
['form_proc_type_desc' + ix
].value
= '';
271 document
.getElementById('qoetable' + ix
).innerHTML
= '';
275 // Add a line for entry of another procedure.
276 function addProcLine() {
277 var f
= document
.forms
[0];
278 var table
= document
.getElementById('proctable');
279 // Compute i = next procedure index.
281 for (; f
['form_proc_type[' + i +
']']; ++i
);
282 var row
= table
.insertRow(table
.rows
.length
);
283 var cell
= row
.insertCell(0);
285 cell
.innerHTML
= "<b><?php echo xls('Procedure'); ?> " +
(i +
1) +
":</b>";
286 var cell
= row
.insertCell(1);
289 "<input type='text' size='50' name='form_proc_type_desc[" + i +
"]'" +
290 " onclick='sel_proc_type(" + i +
")'" +
291 " onfocus='this.blur()'" +
292 " title='<?php echo xla('Click to select the desired procedure'); ?>'" +
293 " style='width:100%;cursor:pointer;cursor:hand' readonly />" +
294 " <input type='hidden' name='form_proc_type[" + i +
"]' value='-1' />" +
295 "<br /><b><?php echo xla('Diagnoses'); ?>: </b>" +
296 "<input type='text' size='50' name='form_proc_type_diag[" + i +
"]'" +
297 " onclick='sel_related(this.name)'" +
298 " title='<?php echo xla('Click to add a diagnosis'); ?>'" +
299 " onfocus='this.blur()'" +
300 " style='cursor:pointer;cursor:hand' readonly />" +
301 " <div style='width:95%;' id='qoetable[" + i +
"]'></div>";
306 // The name of the form field for find-code popup results.
309 // This is for callback by the find-code popup.
310 // Appends to or erases the current list of related codes.
311 function set_related(codetype
, code
, selector
, codedesc
) {
312 var f
= document
.forms
[0];
313 var s
= f
[rcvarname
].value
;
315 if (s
.length
> 0) s +
= ';';
316 s +
= codetype +
':' + code
;
320 f
[rcvarname
].value
= s
;
323 // This invokes the find-code popup.
324 function sel_related(varname
) {
326 // codetype is just to make things easier and avoid mistakes.
327 // Might be nice to have a lab parameter for acceptable code types.
328 // Also note the controlling script here runs from interface/patient_file/encounter/.
329 dlgopen('find_code_popup.php?codetype=<?php echo attr(collect_codetypes("diagnosis","csv")) ?>', '_blank', 500, 400);
336 <body
class="body_top">
338 <form method
="post" action
="<?php echo $rootdir ?>/forms/procedure_order/new.php?id=<?php echo $formid ?>" onsubmit
="return top.restoreSession()">
340 <p
class='title' style
='margin-top:8px;margin-bottom:8px;text-align:center'>
342 echo xl('Procedure Order for') . ' ';
343 echo $enrow['fname'] . ' ' . $enrow['mname'] . ' ' . $enrow['lname'];
344 echo ' ' . xl('on') . ' ' . oeFormatShortDate(substr($enrow['date'], 0, 10));
351 <table border
='1' width
='95%' id
='proctable'>
354 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Ordering Provider','e'); ?
>:</b
></td
>
357 generate_form_field(array('data_type'=>10,'field_id'=>'provider_id'),
358 $row['provider_id']);
364 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Sending To','e'); ?
>:</b
></td
>
366 <select name
='form_lab_id' onchange
='lab_id_changed()'>
368 $ppres = sqlStatement("SELECT ppid, name FROM procedure_providers " .
369 "ORDER BY name, ppid");
370 while ($pprow = sqlFetchArray($ppres)) {
371 echo "<option value='" . attr($pprow['ppid']) . "'";
372 if ($pprow['ppid'] == $row['lab_id']) echo " selected";
373 echo ">" . text($pprow['name']) . "</option>";
381 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Order Date','e'); ?
>:</b
></td
>
384 echo "<input type='text' size='10' name='form_date_ordered' id='form_date_ordered'" .
385 " value='" . $row['date_ordered'] . "'" .
386 " title='" . xl('Date of this order') . "'" .
387 " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'" .
389 "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
390 " id='img_date_ordered' border='0' alt='[?]' style='cursor:pointer'" .
391 " title='" . xl('Click here to choose a date') . "' />";
397 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Internal Time Collected','e'); ?
>:</b
></td
>
400 echo "<input type='text' size='16' name='form_date_collected' id='form_date_collected'" .
401 " value='" . substr($row['date_collected'], 0, 16) . "'" .
402 " title='" . xl('Date and time that the sample was collected') . "'" .
403 // " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'" .
405 "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
406 " id='img_date_collected' border='0' alt='[?]' style='cursor:pointer'" .
407 " title='" . xl('Click here to choose a date and time') . "' />";
413 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Priority','e'); ?
>:</b
></td
>
416 generate_form_field(array('data_type'=>1,'field_id'=>'order_priority',
417 'list_id'=>'ord_priority'), $row['order_priority']);
423 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Status','e'); ?
>:</b
></td
>
426 generate_form_field(array('data_type'=>1,'field_id'=>'order_status',
427 'list_id'=>'ord_status'), $row['order_status']);
433 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Patient Instructions','e'); ?
>:</b
></td
>
435 <textarea rows
='3' cols
='40' name
='form_patient_instructions' style
='width:100%'
436 wrap
='virtual' class='inputtext' /><?php
echo $row['patient_instructions'] ?
></textarea
>
442 // This section merits some explanation. :)
444 // If any procedures have already been saved for this form, then a top-level table row is
445 // created for each of them, and includes the relevant questions and any existing answers.
446 // Otherwise a single empty table row is created for entering the first or only procedure.
448 // If a new procedure is selected or changed, the questions for it are (re)generated from
449 // the dialog window from which the procedure is selected, via JavaScript. The sel_proc_type
450 // function and the types.php script that it invokes collaborate to support this feature.
452 // The generate_qoe_html function in qoe.inc.php contains logic to generate the HTML for
453 // the questions, and can be invoked either from this script or from types.php.
455 // The $i counter that you see below is to resolve the need for unique names for form fields
456 // that may occur for each of the multiple procedure requests within the same order.
457 // procedure_order_seq serves a similar need for uniqueness at the database level.
461 $opres = sqlStatement("SELECT " .
462 "pc.procedure_order_seq, pc.procedure_code, pc.procedure_name, " .
463 "pc.diagnoses, pt.procedure_type_id " .
464 "FROM procedure_order_code AS pc " .
465 "LEFT JOIN procedure_type AS pt ON pt.lab_id = ? AND " .
466 "pt.procedure_code = pc.procedure_code " .
467 "WHERE pc.procedure_order_id = ? " .
468 "ORDER BY pc.procedure_order_seq",
469 array($row['lab_id'], $formid));
470 while ($oprow = sqlFetchArray($opres)) {
474 if (empty($oparr)) $oparr[] = array('procedure_name' => '');
477 foreach ($oparr as $oprow) {
478 $ptid = -1; // -1 means no procedure is selected yet
479 if (!empty($oprow['procedure_type_id'])) {
480 $ptid = $oprow['procedure_type_id'];
484 <td width
='1%' valign
='top'><b
><?php
echo xl('Procedure') . ' ' . ($i +
1); ?
>:</b
></td
>
486 <input type
='text' size
='50' name
='form_proc_type_desc[<?php echo $i; ?>]'
487 value
='<?php echo attr($oprow['procedure_name
']) ?>'
488 onclick
="sel_proc_type(<?php echo $i; ?>)"
489 onfocus
='this.blur()'
490 title
='<?php xla('Click to select the desired procedure
','e
'); ?>'
491 style
='width:100%;cursor:pointer;cursor:hand' readonly
/>
492 <input type
='hidden' name
='form_proc_type[<?php echo $i; ?>]' value
='<?php echo $ptid ?>' />
493 <br
/><b
><?php
echo xlt('Diagnoses'); ?
>:</b
>
494 <input type
='text' size
='50' name
='form_proc_type_diag[<?php echo $i; ?>]'
495 value
='<?php echo attr($oprow['diagnoses
']) ?>' onclick
='sel_related(this.name)'
496 title
='<?php echo xla('Click to add a diagnosis
'); ?>'
497 onfocus
='this.blur()'
498 style
='cursor:pointer;cursor:hand' readonly
/>
499 <!-- MSIE innerHTML property
for a TABLE element is read
-only
, so using a DIV here
. -->
500 <div style
='width:95%;' id
='qoetable[<?php echo $i; ?>]'>
502 $qoe_init_javascript = '';
503 echo generate_qoe_html($ptid, $formid, $oprow['procedure_order_seq'], $i);
504 if ($qoe_init_javascript)
505 echo "<script language='JavaScript'>$qoe_init_javascript</script>";
518 <input type
='button' value
='<?php echo xla('Add Procedure
'); ?>' onclick
="addProcLine()" />
520 <input type
='submit' name
='bn_save' value
='<?php echo xla('Save
'); ?>' />
522 <input type
='submit' name
='bn_xmit' value
='<?php echo xla('Save
and Transmit
'); ?>' />
524 <input type
='button' value
='<?php echo xla('Cancel
'); ?>' onclick
="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
529 <script language
='JavaScript'>
530 Calendar
.setup({inputField
:'form_date_ordered', ifFormat
:'%Y-%m-%d',
531 button
:'img_date_ordered'});
532 Calendar
.setup({inputField
:'form_date_collected', ifFormat
:'%Y-%m-%d %H:%M',
533 button
:'img_date_collected', showsTime
:true});