* @author Brady Miller * @copyright Copyright (c) 2010 Rod Roark * @copyright Copyright (c) 2017-2019 Brady Miller * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3 */ require_once("../globals.php"); require_once("$srcdir/options.inc.php"); require_once("$srcdir/lab.inc"); use OpenEMR\Common\Acl\AclMain; use OpenEMR\Common\Twig\TwigContainer; use OpenEMR\Core\Header; // Indicates if we are entering in batch mode. $form_batch = empty($_GET['batch']) ? 0 : 1; // Indicates if we are entering in review mode. $form_review = empty($_GET['review']) ? 0 : 1; // Check authorization. $thisauth = AclMain::aclCheckCore('patients', 'lab'); if (!$thisauth) { echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Procedure Results")]); exit; } // Check authorization for pending review. $reviewauth = AclMain::aclCheckCore('patients', 'sign'); if ($form_review and !$reviewauth and !$thisauth) { echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Procedure Results")]); exit; } // Set pid for pending review. if (!empty($_GET['set_pid']) && $form_review) { require_once("$srcdir/pid.inc"); require_once("$srcdir/patient.inc"); setpid($_GET['set_pid']); $result = getPatientData($pid, "*, DATE_FORMAT(DOB,'%Y-%m-%d') as DOB_YMD"); ?> $line_value) { list($order_id, $order_seq, $report_id, $result_id) = explode(':', $line_value); // Not using xl() here because this is for debugging only. if (empty($order_id)) { die("Order ID is missing from line " . text($lino) . "."); } // If report data exists for this line, save it. $date_report = oresData("form_date_report", $lino); if (!empty($date_report)) { $sets = "procedure_order_id = '" . add_escape_custom($order_id) . "', " . "procedure_order_seq = '" . add_escape_custom($order_seq) . "', " . "date_report = '" . add_escape_custom($date_report) . "', " . "date_collected = " . QuotedOrNull(oresData("form_date_collected", $lino)) . ", " . "specimen_num = '" . oresData("form_specimen_num", $lino) . "', " . "report_status = '" . oresData("form_report_status", $lino) . "'"; // Set the review status to reviewed. if ($form_review) { $sets .= ", review_status = 'reviewed'"; } if ($report_id) { // Report already exists. sqlStatement("UPDATE procedure_report SET $sets " . "WHERE procedure_report_id = '" . add_escape_custom($report_id) . "'"); } else { // Add new report. $report_id = sqlInsert("INSERT INTO procedure_report SET $sets"); } } // If this line had report data entry fields, filled or not, set the // "current report ID" which the following result data will link to. if (isset($_POST["form_date_report"][$lino])) { $current_report_id = $report_id; } // If there's a report, save corresponding results. if ($current_report_id) { // Comments and notes will be combined into one comments field. $form_comments = oresRawData("form_comments", $lino); $form_comments = str_replace("\n", '~', $form_comments); $form_comments = str_replace("\r", '', $form_comments); $form_notes = oresRawData("form_notes", $lino); if ($form_notes !== '') { $form_comments .= "\n" . $form_notes; } $sets = "procedure_report_id = '" . add_escape_custom($current_report_id) . "', " . "result_code = '" . oresData("form_result_code", $lino) . "', " . "result_text = '" . oresData("form_result_text", $lino) . "', " . "`date` = " . QuotedOrNull(oresData("form_result_date", $lino)) . ", " . "abnormal = '" . oresData("form_result_abnormal", $lino) . "', " . "result = '" . oresData("form_result_result", $lino) . "', " . "`range` = '" . oresData("form_result_range", $lino) . "', " . "units = '" . oresData("form_result_units", $lino) . "', " . "facility = '" . oresData("form_facility", $lino) . "', " . "comments = '" . $form_comments . "', " . "result_status = '" . oresData("form_result_status", $lino) . "', " . "`date_end` = " . QuotedOrNull(oresData("form_result_date_end", $lino)); if ($result_id) { // result already exists sqlStatement("UPDATE procedure_result SET $sets " . "WHERE procedure_result_id = '" . add_escape_custom($result_id) . "'"); } else { // Add new result. $result_id = sqlInsert("INSERT INTO procedure_result SET $sets"); } } } // end foreach } ?> <?php echo xlt('Procedure Results'); ?>
0) { $ptrow = sqlQuery("SELECT name FROM procedure_type WHERE " . "procedure_type_id = ?", [$form_proc_type]); $form_proc_type_desc = $ptrow['name']; } ?> : ' style='cursor:pointer;cursor:hand' readonly />  : ' />  : ' />  
0 && $form_from_date)) { ?> = ? AND po.date_ordered <= ? " . "AND $where " . "ORDER BY pd.lname, pd.fname, pd.mname, po.patient_id, $orderby"; array_push($sqlBindArray, $form_proc_type, $form_from_date, $form_to_date); } else { $query = "SELECT $selects " . "FROM procedure_order AS po " . "$joins " . "WHERE po.patient_id = ? AND $where " . "ORDER BY $orderby"; array_push($sqlBindArray, $pid); } $res = sqlStatement($query, $sqlBindArray); $lastpoid = -1; $lastpcid = -1; $lastprid = -1; $encount = 0; $lino = 0; $extra_html = ''; $lastrcn = ''; $facilities = array(); while ($row = sqlFetchArray($res)) { $order_type_id = empty($row['order_type_id']) ? 0 : ($row['order_type_id'] + 0); $order_id = empty($row['procedure_order_id']) ? 0 : ($row['procedure_order_id'] + 0); $order_seq = empty($row['procedure_order_seq']) ? 0 : ($row['procedure_order_seq'] + 0); $report_id = empty($row['procedure_report_id']) ? 0 : ($row['procedure_report_id'] + 0); $date_report = empty($row['date_report']) ? '' : substr($row['date_report'], 0, 16); $date_collected = empty($row['date_collected']) ? '' : substr($row['date_collected'], 0, 16); $specimen_num = empty($row['specimen_num']) ? '' : $row['specimen_num']; $report_status = empty($row['report_status']) ? '' : $row['report_status']; $review_status = empty($row['review_status']) ? 'received' : $row['review_status']; // skip report_status = receive to make sure do not show the report before it reviewed and sign off by Physicians if ($form_review) { if ($review_status == "reviewed") { continue; } } else { if ($review_status == "received") { continue; } } $query_test = sqlFetchArray(sqlStatement("select deleted from forms where form_id=? and formdir='procedure_order'", array($order_id))); // skip the procedure that has been deleted from the encounter form if ($query_test['deleted'] == 1) { continue; } $selects = "pt2.procedure_type, pt2.procedure_code, ll.title AS pt2_units, " . "pt2.range AS pt2_range, pt2.procedure_type_id AS procedure_type_id, " . "pt2.name AS name, pt2.description, pt2.seq AS seq, " . "ps.procedure_result_id, ps.result_code AS result_code, ps.result_text, ps.date, ps.date_end, ps.abnormal, ps.result, " . "ps.range, ps.result_status, ps.facility, ps.comments, ps.units, ps.comments"; // procedure_type_id for order: $pt2cond = "pt2.parent = '" . add_escape_custom($order_type_id) . "' AND " . "(pt2.procedure_type LIKE 'res%' OR pt2.procedure_type LIKE 'rec%')"; // pr.procedure_report_id or 0 if none: $pscond = "ps.procedure_report_id = '" . add_escape_custom($report_id) . "'"; $joincond = "ps.result_code = pt2.procedure_code"; // This union emulates a full outer join. The idea is to pick up all // result types defined for this order type, as well as any actual // results that do not have a matching result type. $query = "(SELECT $selects FROM procedure_type AS pt2 " . "LEFT JOIN procedure_result AS ps ON $pscond AND $joincond " . "LEFT JOIN list_options AS ll ON ll.list_id = 'proc_unit' AND ll.option_id = pt2.units " . "WHERE $pt2cond" . ") UNION (" . "SELECT $selects FROM procedure_result AS ps " . "LEFT JOIN procedure_type AS pt2 ON $pt2cond AND $joincond " . "LEFT JOIN list_options AS ll ON ll.list_id = 'proc_unit' AND ll.option_id = pt2.units " . "WHERE $pscond) " . "ORDER BY seq, name, procedure_type_id, result_code"; $rres = sqlStatement($query); while ($rrow = sqlFetchArray($rres)) { $restyp_code = empty($rrow['procedure_code']) ? '' : $rrow['procedure_code']; $restyp_type = empty($rrow['procedure_type']) ? '' : $rrow['procedure_type']; $restyp_name = empty($rrow['name']) ? '' : $rrow['name']; $restyp_units = empty($rrow['pt2_units']) ? '' : $rrow['pt2_units']; $restyp_range = empty($rrow['pt2_range']) ? '' : $rrow['pt2_range']; $result_id = empty($rrow['procedure_result_id']) ? 0 : ($rrow['procedure_result_id'] + 0); $result_code = empty($rrow['result_code']) ? $restyp_code : $rrow['result_code']; $result_text = empty($rrow['result_text']) ? $restyp_name : $rrow['result_text']; $result_date = empty($rrow['date']) ? '' : $rrow['date']; $result_date_end = empty($rrow['date_end']) ? '' : $rrow['date_end']; $result_abnormal = empty($rrow['abnormal']) ? '' : $rrow['abnormal']; $result_result = empty($rrow['result']) ? '' : $rrow['result']; $result_units = empty($rrow['units']) ? $restyp_units : $rrow['units']; $result_facility = empty($rrow['facility']) ? '' : $rrow['facility']; $result_comments = empty($rrow['comments']) ? '' : $rrow['comments']; $result_range = empty($rrow['range']) ? $restyp_range : $rrow['range']; $result_status = empty($rrow['result_status']) ? '' : $rrow['result_status']; // If there is more than one line of comments, everything after that is "notes". $result_notes = ''; $i = strpos($result_comments, "\n"); if ($i !== false) { $result_notes = trim(substr($result_comments, $i + 1)); $result_comments = substr($result_comments, 0, $i); } $result_comments = trim($result_comments); if ($result_facility <> "" && !in_array($result_facility, $facilities)) { $facilities[] = $result_facility; } if ($lastpoid != $order_id || $lastpcid != $order_seq) { ++$encount; $lastrcn = ''; } echo " \n"; // Generate first 2 columns. if ($lastpoid != $order_id || $lastpcid != $order_seq) { $lastprid = -1; // force report fields on first line of each procedure if ($form_batch) { if ($lastpoid != $order_id) { $tmp = $row['lname']; if ($row['fname'] || $row['mname']) { $tmp .= ', ' . $row['fname'] . ' ' . $row['mname']; } echo " \n"; echo " \n"; } else { echo " "; } } else { if ($lastpoid != $order_id) { echo " \n"; } else { echo " "; } echo " \n"; } } else { echo " "; } // If this starts a new report or a new order, generate the report form // fields. In the case of a new order with no report yet, the fields will // have their blank/default values, and form_line (above) will indicate a // report ID of 0. // // TBD: Also generate default report fields and another set of results if // the previous report is marked "Preliminary". // if ($report_id != $lastprid) { ?> \n" . "
" . text($tmp) . "" . text($row['pubpid']) . " " . text($row['date_ordered']) . " " . text($row['procedure_name']) . "  ' /> ' /> ' />   ' /> ' /> ' /> ' /> '>  ? 
\n" . "\n" . "" . "\n" . "" . // Ensoftek: Changed Facility to Text Area as the field procedure_result-->facility is now multi-line "\n" . "" . "\n" . "" . "\n" . "
" . text($result_text) . "
" . xlt('Status') . ": " . generate_select_list( "form_result_status[$lino]", 'proc_res_status', $result_status, xl('Result Status'), '' ) . "
" . xlt('Facility') . ":
" . xlt('Comments') . ":
" . xlt('Notes') . ":
\n" . "

\n" . "
"; $lastpoid = $order_id; $lastpcid = $order_seq; $lastprid = $report_id; ++$lino; } } if (!empty($facilities)) { // display facility information $extra_html .= ""; $extra_html .= ""; foreach ($facilities as $facilityID) { foreach (explode(":", $facilityID) as $lab_facility) { $facility_array = getFacilityInfo($lab_facility); if ($facility_array) { $extra_html .= "" . "" . "" . "" . ""; } } } $extra_html .= "
" . xlt('Performing Laboratory Facility') . "

" . text($facility_array['fname']) . " " . text($facility_array['lname']) . ", " . text($facility_array['title']) . "
" . text($facility_array['organization']) . "
" . text($facility_array['street']) . " " . text($facility_array['city']) . " " . text($facility_array['state']) . "
" . text(formatPhone($facility_array['phone'])) . "
\n"; } ?>