Highway to PSR2
[openemr.git] / interface / patient_file / transaction / print_referral.php
blob976631dbf60897c6df1af7c61cdd049c9ec71b78
1 <?php
2 // Copyright (C) 2008-2015 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.
11 include_once("../../globals.php");
12 require_once("$srcdir/transactions.inc");
13 require_once("$srcdir/options.inc.php");
14 include_once("$srcdir/patient.inc");
16 $template_file = $GLOBALS['OE_SITE_DIR'] . "/referral_template.html";
18 $TEMPLATE_LABELS = array(
19 'label_clinic_id' => htmlspecialchars(xl('Clinic ID')),
20 'label_control_no' => htmlspecialchars(xl('Control No.')),
21 'label_date' => htmlspecialchars(xl('Date')),
22 'label_webpage_title' => htmlspecialchars(xl('Referral Form')),
23 'label_form1_title' => htmlspecialchars(xl('REFERRAL FORM')),
24 'label_name' => htmlspecialchars(xl('Name')),
25 'label_age' => htmlspecialchars(xl('Age')),
26 'label_gender' => htmlspecialchars(xl('Gender')),
27 'label_address' => htmlspecialchars(xl('Address')),
28 'label_postal' => htmlspecialchars(xl('Postal')),
29 'label_phone' => htmlspecialchars(xl('Phone')),
30 'label_ref_reason' => htmlspecialchars(xl('Reference Reason')),
31 'label_diagnosis' => htmlspecialchars(xl('Diagnosis')),
32 'label_ref_class' => htmlspecialchars(xl('Reference classification (risk level)')),
33 'label_dr_name_sig' => htmlspecialchars(xl('Doctor\'s name and signature')),
34 'label_refer_to' => htmlspecialchars(xl('Referred to')),
35 'label_clinic' => htmlspecialchars(xl('Health centre/clinic')),
36 'label_history_summary' => htmlspecialchars(xl('Client medical history summary')),
37 'label_bp' => htmlspecialchars(xl('Blood pressure')),
38 'label_ht' => htmlspecialchars(xl('Height')),
39 'label_wt' => htmlspecialchars(xl('Weight')),
40 'label_ref_name_sig' => htmlspecialchars(xl('Referer name and signature')),
41 'label_special_name_sig' => htmlspecialchars(xl('Specialist name and signature')),
42 'label_form2_title' => htmlspecialchars(xl('COUNTER REFERRAL FORM')),
43 'label_findings' => htmlspecialchars(xl('Findings')),
44 'label_final_diagnosis' => htmlspecialchars(xl('Final Diagnosis')),
45 'label_services_provided' => htmlspecialchars(xl('Services provided')),
46 'label_recommendations' => htmlspecialchars(xl('Recommendations and treatment')),
47 'label_scripts_and_referrals' => htmlspecialchars(xl('Prescriptions and other referrals')),
48 'label_subhead_clinic' => htmlspecialchars(xl('Clinic Copy')),
49 'label_subhead_patient' => htmlspecialchars(xl('Client Copy')),
50 'label_subhead_referred' => htmlspecialchars(xl('For Referred Organization/Practitioner'))
53 if (!is_file($template_file)) {
54 die("$template_file does not exist!");
57 $transid = empty($_REQUEST['transid']) ? 0 : $_REQUEST['transid'] + 0;
59 // if (!$transid) die("Transaction ID is missing!");
61 if ($transid) {
62 $trow = getTransById($transid);
63 $patient_id = $trow['pid'];
64 $refer_date = empty($trow['refer_date']) ? date('Y-m-d') : $trow['refer_date'];
65 } else {
66 if (empty($_REQUEST['patient_id'])) {
67 // If no transaction ID or patient ID, this will be a totally blank form.
68 $patient_id = 0;
69 $refer_date = '';
70 } else {
71 $patient_id = $_REQUEST['patient_id'] + 0;
72 $refer_date = date('Y-m-d');
75 $trow = array('id' => '', 'pid' => $patient_id, 'refer_date' => $refer_date);
78 if ($patient_id) {
79 $patdata = getPatientData($patient_id);
80 $patient_age = getPatientAge(str_replace('-', '', $patdata['DOB']));
81 } else {
82 $patdata = array('DOB' => '');
83 $patient_age = '';
86 if (empty($trow['refer_from'])) {
87 $trow['refer_from'] = 0;
90 if (empty($trow['refer_to' ])) {
91 $trow['refer_to' ] = 0;
94 $frrow = sqlQuery("SELECT * FROM users WHERE id = ?", array($trow['refer_from']));
95 if (empty($frrow)) {
96 $frrow = array();
99 $torow = sqlQuery("SELECT * FROM users WHERE id = ?", array($trow['refer_to']));
100 if (empty($torow)) {
101 $torow = array(
102 'organization' => '',
103 'street' => '',
104 'city' => '',
105 'state' => '',
106 'zip' => '',
107 'phone' => '',
111 $vrow = sqlQuery("SELECT * FROM form_vitals WHERE " .
112 "pid = ? AND date <= ? " .
113 "ORDER BY date DESC LIMIT 1", array($patient_id, $refer_date." 23:59:59"));
114 if (empty($vrow)) {
115 $vrow = array(
116 'bps' => '',
117 'bpd' => '',
118 'weight' => '',
119 'height' => '',
123 // $facrow = sqlQuery("SELECT name, facility_npi FROM facility ORDER BY " .
124 // "service_location DESC, billing_location DESC, id ASC LIMIT 1");
125 $facrow = getFacility(-1);
127 // Make some items HTML-friendly if they are empty.
128 if (empty($trow['id'])) {
129 $trow['id'] = '&nbsp;';
132 if (empty($patient_id)) {
133 $patient_id = '&nbsp;';
136 if (empty($facrow['facility_npi'])) {
137 $facrow['facility_npi'] = '&nbsp;';
140 $s = '';
141 $fh = fopen($template_file, 'r');
142 while (!feof($fh)) {
143 $s .= fread($fh, 8192);
146 fclose($fh);
148 $s = str_replace("{header1}", genFacilityTitle($TEMPLATE_LABELS['label_form1_title'], -1), $s);
149 $s = str_replace("{header2}", genFacilityTitle($TEMPLATE_LABELS['label_form2_title'], -1), $s);
151 $s = str_replace("{fac_name}", $facrow['name'], $s);
152 $s = str_replace("{fac_facility_npi}", $facrow['facility_npi'], $s);
153 $s = str_replace("{ref_id}", $trow['id'], $s);
154 $s = str_replace("{ref_pid}", $patient_id, $s);
155 $s = str_replace("{pt_age}", $patient_age, $s);
157 $fres = sqlStatement("SELECT * FROM layout_options " .
158 "WHERE form_id = 'LBTref' ORDER BY group_name, seq");
159 while ($frow = sqlFetchArray($fres)) {
160 $data_type = $frow['data_type'];
161 $field_id = $frow['field_id'];
162 $currvalue = '';
163 if (isset($trow[$field_id])) {
164 $currvalue = $trow[$field_id];
167 $s = str_replace(
168 "{ref_$field_id}",
169 generate_display_field($frow, $currvalue),
174 foreach ($patdata as $key => $value) {
175 if ($key == "sex") {
176 $s = str_replace("{pt_$key}", generate_display_field(array('data_type'=>'1','list_id'=>'sex'), $value), $s);
177 } else {
178 $s = str_replace("{pt_$key}", $value, $s);
182 foreach ($frrow as $key => $value) {
183 $s = str_replace("{from_$key}", $value, $s);
186 foreach ($torow as $key => $value) {
187 $s = str_replace("{to_$key}", $value, $s);
190 foreach ($vrow as $key => $value) {
191 $s = str_replace("{v_$key}", $value, $s);
194 foreach ($TEMPLATE_LABELS as $key => $value) {
195 $s = str_replace("{".$key."}", $value, $s);
198 // A final pass to clear any unmatched variables:
199 $s = preg_replace('/\{\S+\}/', '', $s);
201 echo $s;