Links to rx_print changed to make use of GET variable as now required by rx_print...
[openemr.git] / custom / export_labworks.php
blobc21442b5a5f2c593ec012f930cfd1d13de0780a1
1 <?
2 // Copyright (C) 2005 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 /////////////////////////////////////////////////////////////////////
10 // This program exports patient demographics on demand and sends
11 // them to an Atlas LabWorks server to facilitate lab requisitions.
12 /////////////////////////////////////////////////////////////////////
14 include_once("../interface/globals.php");
15 include_once("../library/patient.inc");
17 // FTP parameters that you must customize. If you are not sending
18 // then set $FTP_SERVER to an empty string.
20 $FTP_SERVER = "192.168.0.30";
21 $FTP_USER = "openemr";
22 $FTP_PASS = "secret";
23 $FTP_DIR = "";
25 // This is the destination directory on the local machine for the
26 // exported data. Required even if FTP is used.
28 $EXPORT_PATH = "/tmp/labworks";
30 $out = "";
32 // Add a string to output with some basic sanitizing.
33 function Add($field) {
34 global $out;
35 $out .= "^" . trim(str_replace(array("\r", "\n", "\t"), " ", $field));
38 // Remove all non-digits from a string.
39 function Digits($field) {
40 return preg_replace("/\D/", "", $field);
43 // Translate sex.
44 function Sex($field) {
45 $sex = strtoupper(substr(trim($field), 0, 1));
46 if ($sex != "M" && $sex != "F") $sex = "U";
47 return $sex;
50 // Translate a date.
51 function LWDate($field) {
52 $tmp = fixDate($field);
53 return substr($tmp, 5, 2) . substr($tmp, 8, 2) . substr($tmp, 0, 4);
56 // Translate insurance type.
57 function InsType($field) {
58 if (! $field) return "";
59 if ($field == 2) return "Medicare";
60 if ($field == 3) return "Medicaid";
61 return "Other";
64 // Error abort function that does not leave the system locked.
65 function mydie($msg) {
66 global $EXPORT_PATH;
67 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
68 die($msg);
71 $alertmsg = ""; // anything here pops up in an alert box
73 // This mess gets all the info for the patient.
75 $insrow = array();
76 foreach (array('primary','secondary') as $value) {
77 $insrow[] = sqlQuery("SELECT id FROM insurance_data WHERE " .
78 "pid = '$pid' AND type = '$value' ORDER BY date DESC LIMIT 1");
80 $query = "SELECT " .
81 "p.pubpid, p.fname, p.mname, p.lname, p.DOB, p.providerID, " .
82 "p.ss, p.street, p.city, p.state, p.postal_code, p.phone_home, p.sex, " .
83 "i1.policy_number AS policy1, i1.group_number AS group1, i1.provider as provider1, " .
84 "i1.subscriber_fname AS fname1, i1.subscriber_mname AS mname1, i1.subscriber_lname AS lname1, " .
85 "i1.subscriber_street AS sstreet1, i1.subscriber_city AS scity1, i1.subscriber_state AS sstate1, " .
86 "i1.subscriber_postal_code AS szip1, i1.subscriber_relationship AS relationship1, " .
87 "c1.name AS name1, c1.freeb_type AS instype1, " .
88 "a1.line1 AS street11, a1.line2 AS street21, a1.city AS city1, a1.state AS state1, " .
89 "a1.zip AS zip1, a1.plus_four AS zip41, " .
90 "i2.policy_number AS policy2, i2.group_number AS group2, i2.provider as provider2, " .
91 "i2.subscriber_fname AS fname2, i2.subscriber_mname AS mname2, i2.subscriber_lname AS lname2, " .
92 "i2.subscriber_relationship AS relationship2, " .
93 "c2.name AS name2, c2.freeb_type AS instype2, " .
94 "a2.line1 AS street12, a2.line2 AS street22, a2.city AS city2, a2.state AS state2, " .
95 "a2.zip AS zip2, a2.plus_four AS zip42 " .
96 "FROM patient_data AS p " .
97 // "LEFT OUTER JOIN insurance_data AS i1 ON i1.pid = p.pid AND i1.type = 'primary' " .
98 // "LEFT OUTER JOIN insurance_data AS i2 ON i2.pid = p.pid AND i2.type = 'secondary' " .
99 "LEFT OUTER JOIN insurance_data AS i1 ON i1.id = '" . $insrow[0]['id'] . "' " .
100 "LEFT OUTER JOIN insurance_data AS i2 ON i2.id = '" . $insrow[1]['id'] . "' " .
102 "LEFT OUTER JOIN insurance_companies AS c1 ON c1.id = i1.provider " .
103 "LEFT OUTER JOIN insurance_companies AS c2 ON c2.id = i2.provider " .
104 "LEFT OUTER JOIN addresses AS a1 ON a1.foreign_id = c1.id " .
105 "LEFT OUTER JOIN addresses AS a2 ON a2.foreign_id = c2.id " .
106 "WHERE p.pid = '$pid' LIMIT 1";
108 $row = sqlFetchArray(sqlStatement($query));
110 // Get primary care doc info. If none was selected in the patient
111 // demographics then pick the #1 doctor in the clinic.
113 $query = "select id, fname, mname, lname from users where authorized = 1";
114 if ($row['providerID']) {
115 $query .= " AND id = " . $row['providerID'];
116 } else {
117 $query .= " ORDER BY id LIMIT 1";
119 $prow = sqlFetchArray(sqlStatement($query));
121 // Patient Section.
123 $out .= $pid; // patient id
124 Add($row['pubpid']); // chart number
125 Add($row['lname']); // last name
126 Add($row['fname']); // first name
127 Add(substr($row['mname'], 0, 1)); // middle initial
128 Add(""); // alias
129 Add(Digits($row['ss'])); // ssn
130 Add(LWDate($row['DOB'])); // dob
131 Add(Sex($row['sex'])); // gender
132 Add(""); // notes
133 Add($row['street']); // address 1
134 Add(""); // address2
135 Add($row['city']); // city
136 Add($row['state']); // state
137 Add($row['postal_code']); // zip
138 Add(Digits($row['phone_home'])); // home phone
140 // Guarantor Section. OpenEMR does not have guarantors so we use the primary
141 // insurance subscriber if there is one, otherwise the patient.
143 if (trim($row['lname1'])) {
144 Add($row['lname1']);
145 Add($row['fname1']);
146 Add(substr($row['mname1'], 0, 1));
147 Add($row['sstreet1']);
148 Add("");
149 Add($row['scity1']);
150 Add($row['sstate1']);
151 Add($row['szip1']);
152 } else {
153 Add($row['lname']);
154 Add($row['fname']);
155 Add(substr($row['mname'], 0, 1));
156 Add($row['street']);
157 Add("");
158 Add($row['city']);
159 Add($row['state']);
160 Add($row['postal_code']);
163 // Primary Insurance Section.
165 Add($row['provider1']);
166 Add($row['name1']);
167 Add($row['street11']);
168 Add($row['street21']);
169 Add($row['city1']);
170 Add($row['state1']);
171 Add($row['zip1']);
172 Add("");
173 Add(InsType($row['instype1']));
174 Add($row['fname1'] . " " . $row['lname1']);
175 Add(ucfirst($row['relationship1']));
176 Add($row['group1']);
177 Add($row['policy1']);
179 // Secondary Insurance Section.
181 Add($row['provider2']);
182 Add($row['name2']);
183 Add($row['street12']);
184 Add($row['street22']);
185 Add($row['city2']);
186 Add($row['state2']);
187 Add($row['zip2']);
188 Add("");
189 Add(InsType($row['instype2']));
190 Add($row['fname2'] . " " . $row['lname2']);
191 Add(ucfirst($row['relationship2']));
192 Add($row['group2']);
193 Add($row['policy2']);
195 // Primary Care Physician Section.
197 Add($prow['id']);
198 Add($prow['lname']);
199 Add($prow['fname']);
200 Add(substr($prow['mname'], 0, 1));
201 Add(""); // UPIN not available
203 // All done.
204 $out .= "\rEND";
206 // In case this is the very first time.
207 if (! file_exists($EXPORT_PATH)) {
208 mkdir($EXPORT_PATH);
209 @touch("$EXPORT_PATH/unlocked");
212 // Serialize the following code; collisions would be very bad.
213 if (! rename("$EXPORT_PATH/unlocked", "$EXPORT_PATH/locked"))
214 die("Export seems to be in use by someone else; please try again.");
216 // Figure out what to use for the target filename.
217 $dh = opendir($EXPORT_PATH);
218 if (! $dh) mydie("Cannot read $EXPORT_PATH");
219 $nextnumber = 1;
220 while (false !== ($filename = readdir($dh))) {
221 if (preg_match("/PMI(\d{8})\.DEM/", $filename, $matches)) {
222 $tmp = 1 + $matches[1];
223 if ($tmp > $nextnumber) {
224 $nextnumber = $tmp;
228 closedir($dh);
229 $fnprefix = sprintf("PMI%08.0f.", $nextnumber);
230 $initialname = $fnprefix . "creating";
231 $finalname = $fnprefix . "DEM";
232 $initialpath = "$EXPORT_PATH/$initialname";
233 $finalpath = "$EXPORT_PATH/$finalname";
235 // Write the file locally with a temporary version of the name.
236 @touch($initialpath); // work around possible php bug
237 $fh = @fopen($initialpath, "w");
238 if (! $fh) mydie("Unable to open $initialpath for writing");
239 fwrite($fh, $out);
240 fclose($fh);
242 // Rename the local file.
243 rename($initialpath, $finalpath);
245 // Delete old stuff to avoid uncontrolled growth.
246 if ($nextnumber > 5) {
247 @unlink("$EXPORT_PATH/PMI%08.0f.DEM", $nextnumber - 5);
250 // End of serialized code.
251 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
253 // If we have an ftp server, send it there and then rename it.
254 if ($FTP_SERVER) {
255 $ftpconn = ftp_connect($FTP_SERVER) or die("FTP connection failed");
256 ftp_login($ftpconn, $FTP_USER, $FTP_PASS) or die("FTP login failed");
257 if ($FTP_DIR) ftp_chdir($ftpconn, $FTP_DIR) or die("FTP chdir failed");
258 ftp_put($ftpconn, $initialname, $finalpath, FTP_BINARY) or die("FTP put failed");
259 ftp_rename($ftpconn, $initialname, $finalname) or die("FTP rename failed");
260 ftp_close($ftpconn);
263 <html>
264 <head>
265 <? html_header_show();?>
266 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
267 <title>Export Patient Demographics</title>
268 </head>
269 <body>
270 <center>
271 <p>&nbsp;</p>
272 <p>Demographics for <? echo $row['fname'] . " " . $row['lname'] ?>
273 have been exported to LabWorks.</p>
274 <p>&nbsp;</p>
275 <form>
276 <p><input type='button' value='OK' onclick='window.close()' /></p>
277 </form>
278 </center>
279 </body>
280 </html>