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 "clinical_hx = '" . formData('form_clinical_hx') . "', " .
79 "patient_instructions = '" . formData('form_patient_instructions') . "', " .
80 "patient_id = '" . $pid . "', " .
81 "encounter_id = '" . $encounter . "'";
83 // If updating an existing form...
86 $query = "UPDATE procedure_order SET $sets " .
87 "WHERE procedure_order_id = '$formid'";
91 // If adding a new form...
94 $query = "INSERT INTO procedure_order SET $sets";
95 $formid = sqlInsert($query);
96 addForm($encounter, "Procedure Order", $formid, "procedure_order", $pid, $userauthorized);
99 // Remove any existing procedures and their answers for this order and
100 // replace them from the form.
102 sqlStatement("DELETE FROM procedure_answers WHERE procedure_order_id = ?",
104 sqlStatement("DELETE FROM procedure_order_code WHERE procedure_order_id = ?",
107 for ($i = 0; isset($_POST['form_proc_type'][$i]); ++
$i) {
108 $ptid = $_POST['form_proc_type'][$i] +
0;
109 if ($ptid <= 0) continue;
111 $prefix = "ans$i" . "_";
113 $poseq = sqlInsert("INSERT INTO procedure_order_code SET ".
114 "procedure_order_id = ?, " .
116 "procedure_code = (SELECT procedure_code FROM procedure_type WHERE procedure_type_id = ?), " .
117 "procedure_name = (SELECT name FROM procedure_type WHERE procedure_type_id = ?)",
118 array($formid, strip_escape_custom($_POST['form_proc_type_diag'][$i]), $ptid, $ptid));
120 $qres = sqlStatement("SELECT " .
121 "q.procedure_code, q.question_code, q.options, q.fldtype " .
122 "FROM procedure_type AS t " .
123 "JOIN procedure_questions AS q ON q.lab_id = t.lab_id " .
124 "AND q.procedure_code = t.procedure_code AND q.activity = 1 " .
125 "WHERE t.procedure_type_id = ? " .
126 "ORDER BY q.seq, q.question_text", array($ptid));
128 while ($qrow = sqlFetchArray($qres)) {
129 $options = trim($qrow['options']);
130 $qcode = trim($qrow['question_code']);
131 $fldtype = $qrow['fldtype'];
133 if ($fldtype == 'G') {
134 if ($_POST["G1_$prefix$qcode"]) {
135 $data = $_POST["G1_$prefix$qcode"] * 7 +
$_POST["G2_$prefix$qcode"];
139 $data = $_POST["$prefix$qcode"];
141 if (!isset($data) ||
$data === '') continue;
142 if (!is_array($data)) $data = array($data);
143 foreach ($data as $datum) {
144 // Note this will auto-assign the seq value.
145 sqlStatement("INSERT INTO procedure_answers SET ".
146 "procedure_order_id = ?, " .
147 "procedure_order_seq = ?, " .
148 "question_code = ?, " .
150 array($formid, $poseq, $qcode, strip_escape_custom($datum)));
156 if ($_POST['bn_xmit']) {
158 $alertmsg = gen_hl7_order($formid, $hl7);
159 if (empty($alertmsg)) {
160 $alertmsg = send_hl7_order($ppid, $hl7);
162 if (empty($alertmsg)) {
163 sqlStatement("UPDATE procedure_order SET date_transmitted = NOW() WHERE " .
164 "procedure_order_id = ?", array($formid));
168 formHeader("Redirecting....");
170 echo "\n<script language='Javascript'>alert('";
171 echo addslashes(xl('Transmit failed') . ': ' . $alertmsg);
172 echo "')</script>\n";
180 $row = sqlQuery ("SELECT * FROM procedure_order WHERE " .
181 "procedure_order_id = ?",
185 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date FROM " .
186 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
187 "p.pid = ? AND f.pid = p.pid AND f.encounter = ? AND " .
188 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
189 "fe.id = f.form_id LIMIT 1",
190 array($pid, $encounter));
194 <?php
html_header_show(); ?
>
195 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css" />
210 <style type
="text/css">@import
url(<?php
echo $GLOBALS['webroot'] ?
>/library
/dynarch_calendar
.css
);</style
>
211 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar.js"></script
>
212 <?php
include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?
>
213 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot'] ?>/library/dynarch_calendar_setup.js"></script
>
215 <script type
="text/javascript" src
="../../../library/dialog.js"></script
>
216 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot'] ?>/library/textformat.js"></script
>
218 <script language
='JavaScript'>
220 // This invokes the find-procedure-type popup.
221 // formseq = 0-relative index in the form.
223 function sel_proc_type(formseq
) {
224 var f
= document
.forms
[0];
225 // if (!f.form_lab_id.value) {
226 // alert('<?php echo xls('Please select a procedure provider'); ?>');
229 gbl_formseq
= formseq
;
230 var ptvarname
= 'form_proc_type[' + formseq +
']';
231 /********************************************************************
232 dlgopen('../../orders/types.php?popup=1' +
233 '&labid=' + f.form_lab_id.value +
234 '&order=' + f[ptvarname].value +
235 '&formid=<?php echo $formid; ?>' +
236 '&formseq=' + formseq,
238 ********************************************************************/
239 // This replaces the above for an easier/faster order picker tool.
240 dlgopen('../../orders/find_order_popup.php' +
241 '?labid=' + f
.form_lab_id
.value +
242 '&order=' + f
[ptvarname
].value +
243 '&formid=<?php echo $formid; ?>' +
244 '&formseq=' + formseq
,
248 // This is for callback by the find-procedure-type popup.
249 // Sets both the selected type ID and its descriptive name.
250 function set_proc_type(typeid
, typename
) {
251 var f
= document
.forms
[0];
252 var ptvarname
= 'form_proc_type[' + gbl_formseq +
']';
253 var ptdescname
= 'form_proc_type_desc[' + gbl_formseq +
']';
254 f
[ptvarname
].value
= typeid
;
255 f
[ptdescname
].value
= typename
;
258 // This is also for callback by the find-procedure-type popup.
259 // Sets the contents of the table containing the form fields for questions.
260 function set_proc_html(s
, js
) {
261 document
.getElementById('qoetable[' + gbl_formseq +
']').innerHTML
= s
;
265 // New lab selected so clear all procedures and questions from the form.
266 function lab_id_changed() {
267 var f
= document
.forms
[0];
268 for (var i
= 0; true; ++i
) {
269 var ix
= '[' + i +
']';
270 if (!f
['form_proc_type' + ix
]) break;
271 f
['form_proc_type' + ix
].value
= '-1';
272 f
['form_proc_type_desc' + ix
].value
= '';
273 document
.getElementById('qoetable' + ix
).innerHTML
= '';
277 // Add a line for entry of another procedure.
278 function addProcLine() {
279 var f
= document
.forms
[0];
280 var table
= document
.getElementById('proctable');
281 // Compute i = next procedure index.
283 for (; f
['form_proc_type[' + i +
']']; ++i
);
284 var row
= table
.insertRow(table
.rows
.length
);
285 var cell
= row
.insertCell(0);
287 cell
.innerHTML
= "<b><?php echo xls('Procedure'); ?> " +
(i +
1) +
":</b>";
288 var cell
= row
.insertCell(1);
291 "<input type='text' size='50' name='form_proc_type_desc[" + i +
"]'" +
292 " onclick='sel_proc_type(" + i +
")'" +
293 " onfocus='this.blur()'" +
294 " title='<?php echo xla('Click to select the desired procedure'); ?>'" +
295 " style='width:100%;cursor:pointer;cursor:hand' readonly />" +
296 " <input type='hidden' name='form_proc_type[" + i +
"]' value='-1' />" +
297 "<br /><b><?php echo xla('Diagnoses'); ?>: </b>" +
298 "<input type='text' size='50' name='form_proc_type_diag[" + i +
"]'" +
299 " onclick='sel_related(this.name)'" +
300 " title='<?php echo xla('Click to add a diagnosis'); ?>'" +
301 " onfocus='this.blur()'" +
302 " style='cursor:pointer;cursor:hand' readonly />" +
303 " <div style='width:95%;' id='qoetable[" + i +
"]'></div>";
308 // The name of the form field for find-code popup results.
311 // This is for callback by the find-code popup.
312 // Appends to or erases the current list of related codes.
313 function set_related(codetype
, code
, selector
, codedesc
) {
314 var f
= document
.forms
[0];
315 var s
= f
[rcvarname
].value
;
317 if (s
.length
> 0) s +
= ';';
318 s +
= codetype +
':' + code
;
322 f
[rcvarname
].value
= s
;
325 // This invokes the find-code popup.
326 function sel_related(varname
) {
328 // codetype is just to make things easier and avoid mistakes.
329 // Might be nice to have a lab parameter for acceptable code types.
330 // Also note the controlling script here runs from interface/patient_file/encounter/.
331 dlgopen('find_code_popup.php?codetype=<?php echo attr(collect_codetypes("diagnosis","csv")) ?>', '_blank', 500, 400);
334 var transmitting
= false;
336 // Issue a Cancel/OK warning if a previously transmitted order is being transmitted again.
337 function validate(f
) {
338 <?php
if (!empty($row['date_transmitted'])) { ?
>
340 if (!confirm('<?php echo xls('This order was already transmitted on
') . ' ' .
341 addslashes($row['date_transmitted
']) . '. ' .
342 xls('Are you sure you want to transmit it again?
'); ?>')) {
347 top
.restoreSession();
355 <body
class="body_top">
357 <form method
="post" action
="<?php echo $rootdir ?>/forms/procedure_order/new.php?id=<?php echo $formid ?>"
358 onsubmit
="return validate(this)">
360 <p
class='title' style
='margin-top:8px;margin-bottom:8px;text-align:center'>
362 echo xl('Procedure Order for') . ' ';
363 echo $enrow['fname'] . ' ' . $enrow['mname'] . ' ' . $enrow['lname'];
364 echo ' ' . xl('on') . ' ' . oeFormatShortDate(substr($enrow['date'], 0, 10));
371 <table border
='1' width
='95%' id
='proctable'>
374 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Ordering Provider','e'); ?
>:</b
></td
>
377 generate_form_field(array('data_type'=>10,'field_id'=>'provider_id'),
378 $row['provider_id']);
384 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Sending To','e'); ?
>:</b
></td
>
386 <select name
='form_lab_id' onchange
='lab_id_changed()'>
388 $ppres = sqlStatement("SELECT ppid, name FROM procedure_providers " .
389 "ORDER BY name, ppid");
390 while ($pprow = sqlFetchArray($ppres)) {
391 echo "<option value='" . attr($pprow['ppid']) . "'";
392 if ($pprow['ppid'] == $row['lab_id']) echo " selected";
393 echo ">" . text($pprow['name']) . "</option>";
401 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Order Date','e'); ?
>:</b
></td
>
404 echo "<input type='text' size='10' name='form_date_ordered' id='form_date_ordered'" .
405 " value='" . $row['date_ordered'] . "'" .
406 " title='" . xl('Date of this order') . "'" .
407 " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'" .
409 "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
410 " id='img_date_ordered' border='0' alt='[?]' style='cursor:pointer'" .
411 " title='" . xl('Click here to choose a date') . "' />";
417 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Internal Time Collected','e'); ?
>:</b
></td
>
420 echo "<input type='text' size='16' name='form_date_collected' id='form_date_collected'" .
421 " value='" . substr($row['date_collected'], 0, 16) . "'" .
422 " title='" . xl('Date and time that the sample was collected') . "'" .
423 // " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)'" .
425 "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
426 " id='img_date_collected' border='0' alt='[?]' style='cursor:pointer'" .
427 " title='" . xl('Click here to choose a date and time') . "' />";
433 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Priority','e'); ?
>:</b
></td
>
436 generate_form_field(array('data_type'=>1,'field_id'=>'order_priority',
437 'list_id'=>'ord_priority'), $row['order_priority']);
443 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Status','e'); ?
>:</b
></td
>
446 generate_form_field(array('data_type'=>1,'field_id'=>'order_status',
447 'list_id'=>'ord_status'), $row['order_status']);
453 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Clinical History','e'); ?
>:</b
></td
>
455 <input type
='text' maxlength
='255' name
='form_clinical_hx' style
='width:100%'
456 class='inputtext' value
='<?php echo attr($row['clinical_hx
']); ?>' />
460 <!-- Will enable this later
, nothing uses it yet
. -->
461 <tr style
='display:none'>
462 <td width
='1%' valign
='top' nowrap
><b
><?php
xl('Patient Instructions','e'); ?
>:</b
></td
>
464 <textarea rows
='3' cols
='40' name
='form_patient_instructions' style
='width:100%'
465 wrap
='virtual' class='inputtext' /><?php
echo $row['patient_instructions'] ?
></textarea
>
471 // This section merits some explanation. :)
473 // If any procedures have already been saved for this form, then a top-level table row is
474 // created for each of them, and includes the relevant questions and any existing answers.
475 // Otherwise a single empty table row is created for entering the first or only procedure.
477 // If a new procedure is selected or changed, the questions for it are (re)generated from
478 // the dialog window from which the procedure is selected, via JavaScript. The sel_proc_type
479 // function and the types.php script that it invokes collaborate to support this feature.
481 // The generate_qoe_html function in qoe.inc.php contains logic to generate the HTML for
482 // the questions, and can be invoked either from this script or from types.php.
484 // The $i counter that you see below is to resolve the need for unique names for form fields
485 // that may occur for each of the multiple procedure requests within the same order.
486 // procedure_order_seq serves a similar need for uniqueness at the database level.
490 $opres = sqlStatement("SELECT " .
491 "pc.procedure_order_seq, pc.procedure_code, pc.procedure_name, " .
492 "pc.diagnoses, pt.procedure_type_id " .
493 "FROM procedure_order_code AS pc " .
494 "LEFT JOIN procedure_type AS pt ON pt.lab_id = ? AND " .
495 "pt.procedure_code = pc.procedure_code " .
496 "WHERE pc.procedure_order_id = ? " .
497 "ORDER BY pc.procedure_order_seq",
498 array($row['lab_id'], $formid));
499 while ($oprow = sqlFetchArray($opres)) {
503 if (empty($oparr)) $oparr[] = array('procedure_name' => '');
506 foreach ($oparr as $oprow) {
507 $ptid = -1; // -1 means no procedure is selected yet
508 if (!empty($oprow['procedure_type_id'])) {
509 $ptid = $oprow['procedure_type_id'];
513 <td width
='1%' valign
='top'><b
><?php
echo xl('Procedure') . ' ' . ($i +
1); ?
>:</b
></td
>
515 <input type
='text' size
='50' name
='form_proc_type_desc[<?php echo $i; ?>]'
516 value
='<?php echo attr($oprow['procedure_name
']) ?>'
517 onclick
="sel_proc_type(<?php echo $i; ?>)"
518 onfocus
='this.blur()'
519 title
='<?php xla('Click to select the desired procedure
','e
'); ?>'
520 style
='width:100%;cursor:pointer;cursor:hand' readonly
/>
521 <input type
='hidden' name
='form_proc_type[<?php echo $i; ?>]' value
='<?php echo $ptid ?>' />
522 <br
/><b
><?php
echo xlt('Diagnoses'); ?
>:</b
>
523 <input type
='text' size
='50' name
='form_proc_type_diag[<?php echo $i; ?>]'
524 value
='<?php echo attr($oprow['diagnoses
']) ?>' onclick
='sel_related(this.name)'
525 title
='<?php echo xla('Click to add a diagnosis
'); ?>'
526 onfocus
='this.blur()'
527 style
='cursor:pointer;cursor:hand' readonly
/>
528 <!-- MSIE innerHTML property
for a TABLE element is read
-only
, so using a DIV here
. -->
529 <div style
='width:95%;' id
='qoetable[<?php echo $i; ?>]'>
531 $qoe_init_javascript = '';
532 echo generate_qoe_html($ptid, $formid, $oprow['procedure_order_seq'], $i);
533 if ($qoe_init_javascript)
534 echo "<script language='JavaScript'>$qoe_init_javascript</script>";
547 <input type
='button' value
='<?php echo xla('Add Procedure
'); ?>' onclick
="addProcLine()" />
549 <input type
='submit' name
='bn_save' value
='<?php echo xla('Save
'); ?>' onclick
='transmitting = false;' />
551 <input type
='submit' name
='bn_xmit' value
='<?php echo xla('Save
and Transmit
'); ?>' onclick
='transmitting = true;' />
553 <input type
='button' value
='<?php echo xla('Cancel
'); ?>' onclick
="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
558 <script language
='JavaScript'>
559 Calendar
.setup({inputField
:'form_date_ordered', ifFormat
:'%Y-%m-%d',
560 button
:'img_date_ordered'});
561 Calendar
.setup({inputField
:'form_date_collected', ifFormat
:'%Y-%m-%d %H:%M',
562 button
:'img_date_collected', showsTime
:true});