Code type module improvements:
[openemr.git] / interface / patient_file / transaction / print_referral.php
blobe55064d995291d057fdb8b00a06bd72ec1c8e721
1 <?php
2 // Copyright (C) 2008-2010 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 //SANITIZE ALL ESCAPES
10 $sanitize_all_escapes=true;
13 //STOP FAKE REGISTER GLOBALS
14 $fake_register_globals=false;
17 include_once("../../globals.php");
18 require_once("$srcdir/transactions.inc");
19 require_once("$srcdir/options.inc.php");
20 include_once("$srcdir/patient.inc");
22 $template_file = $GLOBALS['OE_SITE_DIR'] . "/referral_template.html";
24 $TEMPLATE_LABELS = array(
25 'label_clinic_id' => htmlspecialchars( xl('Clinic ID')),
26 'label_control_no' => htmlspecialchars( xl('Control No.')),
27 'label_date' => htmlspecialchars( xl('Date')),
28 'label_webpage_title' => htmlspecialchars( xl('Referral Form')),
29 'label_form1_title' => htmlspecialchars( xl('REFERRAL FORM')),
30 'label_name' => htmlspecialchars( xl('Name')),
31 'label_age' => htmlspecialchars( xl('Age')),
32 'label_gender' => htmlspecialchars( xl('Gender')),
33 'label_address' => htmlspecialchars( xl('Address')),
34 'label_postal' => htmlspecialchars( xl('Postal')),
35 'label_phone' => htmlspecialchars( xl('Phone')),
36 'label_ref_reason' => htmlspecialchars( xl('Reference Reason')),
37 'label_diagnosis' => htmlspecialchars( xl('Diagnosis')),
38 'label_ref_class' => htmlspecialchars( xl('Reference classification (risk level)')),
39 'label_dr_name_sig' => htmlspecialchars( xl('Doctor\'s name and signature')),
40 'label_refer_to' => htmlspecialchars( xl('Referred to')),
41 'label_clinic' => htmlspecialchars( xl('Health centre/clinic')),
42 'label_history_summary' => htmlspecialchars( xl('Client medical history summary')),
43 'label_bp' => htmlspecialchars( xl('Blood pressure')),
44 'label_ht' => htmlspecialchars( xl('Height')),
45 'label_wt' => htmlspecialchars( xl('Weight')),
46 'label_ref_name_sig' => htmlspecialchars( xl('Referer name and signature')),
47 'label_special_name_sig' => htmlspecialchars( xl('Specialist name and signature')),
48 'label_form2_title' => htmlspecialchars( xl('COUNTER REFERRAL FORM')),
49 'label_findings' => htmlspecialchars( xl('Findings')),
50 'label_final_diagnosis' => htmlspecialchars( xl('Final Diagnosis')),
51 'label_services_provided' => htmlspecialchars( xl('Services provided')),
52 'label_recommendations' => htmlspecialchars( xl('Recommendations and treatment')),
53 'label_scripts_and_referrals' => htmlspecialchars( xl('Prescriptions and other referrals')),
54 'label_subhead_clinic' => htmlspecialchars( xl('Clinic Copy')),
55 'label_subhead_patient' => htmlspecialchars( xl('Client Copy')),
56 'label_subhead_referred' => htmlspecialchars( xl('For Referred Organization/Practitioner'))
59 if (!is_file($template_file)) die("$template_file does not exist!");
61 $transid = empty($_REQUEST['transid']) ? 0 : $_REQUEST['transid'] + 0;
63 // if (!$transid) die("Transaction ID is missing!");
65 if ($transid) {
66 $trow = getTransById($transid);
67 $patient_id = $trow['pid'];
68 $refer_date = empty($trow['refer_date']) ? date('Y-m-d') : $trow['refer_date'];
70 else {
71 if (empty($_REQUEST['patient_id'])) {
72 // If no transaction ID or patient ID, this will be a totally blank form.
73 $patient_id = 0;
74 $refer_date = '';
75 } else {
76 $patient_id = $_REQUEST['patient_id'] + 0;
77 $refer_date = date('Y-m-d');
79 $trow = array('id' => '', 'pid' => $patient_id, 'refer_date' => $refer_date);
82 if ($patient_id) {
83 $patdata = getPatientData($patient_id);
84 $patient_age = getPatientAge(str_replace('-', '', $patdata['DOB']));
85 } else {
86 $patdata = array('DOB' => '');
87 $patient_age = '';
90 $frrow = sqlQuery("SELECT * FROM users WHERE id = ?", array($trow['refer_from']) );
91 if (empty($frrow)) $frrow = array();
93 $torow = sqlQuery("SELECT * FROM users WHERE id = ?", array($trow['refer_to']) );
94 if (empty($torow)) $torow = array(
95 'organization' => '',
96 'street' => '',
97 'city' => '',
98 'state' => '',
99 'zip' => '',
100 'phone' => '',
103 $vrow = sqlQuery("SELECT * FROM form_vitals WHERE " .
104 "pid = ? AND date <= ? " .
105 "ORDER BY date DESC LIMIT 1", array($patient_id, $refer_date." 23:59:59") );
106 if (empty($vrow)) $vrow = array(
107 'bps' => '',
108 'bpd' => '',
109 'weight' => '',
110 'height' => '',
113 // $facrow = sqlQuery("SELECT name, facility_npi FROM facility ORDER BY " .
114 // "service_location DESC, billing_location DESC, id ASC LIMIT 1");
115 $facrow = getFacility(-1);
117 // Make some items HTML-friendly if they are empty.
118 if (empty($trow['id'])) $trow['id'] = '&nbsp;';
119 if (empty($patient_id)) $patient_id = '&nbsp;';
120 if (empty($facrow['facility_npi'])) $facrow['facility_npi'] = '&nbsp;';
122 $s = '';
123 $fh = fopen($template_file, 'r');
124 while (!feof($fh)) $s .= fread($fh, 8192);
125 fclose($fh);
127 $s = str_replace("{header1}", genFacilityTitle($TEMPLATE_LABELS['label_form1_title'], -1), $s);
128 $s = str_replace("{header2}", genFacilityTitle($TEMPLATE_LABELS['label_form2_title'], -1), $s);
130 $s = str_replace("{fac_name}" , $facrow['name'] , $s);
131 $s = str_replace("{fac_facility_npi}", $facrow['facility_npi'], $s);
132 $s = str_replace("{ref_id}" , $trow['id'] , $s);
133 $s = str_replace("{ref_pid}" , $patient_id , $s);
134 $s = str_replace("{pt_age}" , $patient_age , $s);
136 $fres = sqlStatement("SELECT * FROM layout_options " .
137 "WHERE form_id = 'REF' ORDER BY group_name, seq");
138 while ($frow = sqlFetchArray($fres)) {
139 $data_type = $frow['data_type'];
140 $field_id = $frow['field_id'];
141 $currvalue = '';
142 if (isset($trow[$field_id])) $currvalue = $trow[$field_id];
143 $s = str_replace("{ref_$field_id}",
144 generate_display_field($frow, $currvalue), $s);
147 foreach ($patdata as $key => $value) {
148 if ($key == "sex") {
149 $s = str_replace("{pt_$key}", generate_display_field(array('data_type'=>'1','list_id'=>'sex'), $value), $s);
151 else {
152 $s = str_replace("{pt_$key}", $value, $s);
156 foreach ($frrow as $key => $value) {
157 $s = str_replace("{from_$key}", $value, $s);
160 foreach ($torow as $key => $value) {
161 $s = str_replace("{to_$key}", $value, $s);
164 foreach ($vrow as $key => $value) {
165 $s = str_replace("{v_$key}", $value, $s);
168 foreach ($TEMPLATE_LABELS as $key => $value) {
169 $s = str_replace("{".$key."}", $value, $s);
172 // A final pass to clear any unmatched variables:
173 $s = preg_replace('/\{\S+\}/', '', $s);
175 echo $s;