fixes: CDR engine fixes for duplicate use of actions across rules in addition to...
[openemr.git] / custom / export_labworks.php
blob37ce8f5c7a6e1102d46dc23c2eab1d4e7c05d701
1 <?php
3 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 /////////////////////////////////////////////////////////////////////
11 // This program exports patient demographics on demand and sends
12 // them to an Atlas LabWorks server to facilitate lab requisitions.
13 /////////////////////////////////////////////////////////////////////
15 require_once("../interface/globals.php");
16 require_once("../library/patient.inc.php");
18 use OpenEMR\Core\Header;
20 // FTP parameters that you must customize. If you are not sending
21 // then set $FTP_SERVER to an empty string.
23 $FTP_SERVER = "192.168.0.30";
24 $FTP_USER = "openemr";
25 $FTP_PASS = "secret";
26 $FTP_DIR = "";
28 // This is the destination directory on the local machine for the
29 // exported data. Required even if FTP is used.
31 $EXPORT_PATH = "/tmp/labworks";
33 $out = "";
35 // Add a string to output with some basic sanitizing.
36 function Add($field)
38 global $out;
39 $out .= "^" . trim(str_replace(array("\r", "\n", "\t"), " ", $field));
42 // Remove all non-digits from a string.
43 function Digits($field)
45 return preg_replace("/\D/", "", $field);
48 // Translate sex.
49 function Sex($field)
51 $sex = strtoupper(substr(trim($field), 0, 1));
52 if ($sex != "M" && $sex != "F") {
53 $sex = "U";
56 return $sex;
59 // Translate a date.
60 function LWDate($field)
62 $tmp = fixDate($field);
63 return substr($tmp, 5, 2) . substr($tmp, 8, 2) . substr($tmp, 0, 4);
66 // Translate insurance type.
67 function InsType($field)
69 if (! $field) {
70 return "";
73 if ($field == 2) {
74 return "Medicare";
77 if ($field == 3) {
78 return "Medicaid";
81 return "Other";
84 // Error abort function that does not leave the system locked.
85 function mydie($msg)
87 global $EXPORT_PATH;
88 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
89 die($msg);
92 $alertmsg = ""; // anything here pops up in an alert box
94 // This mess gets all the info for the patient.
96 $insrow = array();
97 foreach (array('primary','secondary') as $value) {
98 $insrow[] = sqlQuery("SELECT id FROM insurance_data WHERE " .
99 "pid = ? AND type = ? ORDER BY date DESC LIMIT 1", array($pid, $value));
102 $query = "SELECT " .
103 "p.pubpid, p.fname, p.mname, p.lname, p.DOB, p.providerID, " .
104 "p.ss, p.street, p.city, p.state, p.postal_code, p.phone_home, p.sex, " .
105 "i1.policy_number AS policy1, i1.group_number AS group1, i1.provider as provider1, " .
106 "i1.subscriber_fname AS fname1, i1.subscriber_mname AS mname1, i1.subscriber_lname AS lname1, " .
107 "i1.subscriber_street AS sstreet1, i1.subscriber_city AS scity1, i1.subscriber_state AS sstate1, " .
108 "i1.subscriber_postal_code AS szip1, i1.subscriber_relationship AS relationship1, " .
109 "c1.name AS name1, c1.ins_type_code AS instype1, " .
110 "a1.line1 AS street11, a1.line2 AS street21, a1.city AS city1, a1.state AS state1, " .
111 "a1.zip AS zip1, a1.plus_four AS zip41, " .
112 "i2.policy_number AS policy2, i2.group_number AS group2, i2.provider as provider2, " .
113 "i2.subscriber_fname AS fname2, i2.subscriber_mname AS mname2, i2.subscriber_lname AS lname2, " .
114 "i2.subscriber_relationship AS relationship2, " .
115 "c2.name AS name2, c2.ins_type_code AS instype2, " .
116 "a2.line1 AS street12, a2.line2 AS street22, a2.city AS city2, a2.state AS state2, " .
117 "a2.zip AS zip2, a2.plus_four AS zip42 " .
118 "FROM patient_data AS p " .
119 // "LEFT OUTER JOIN insurance_data AS i1 ON i1.pid = p.pid AND i1.type = 'primary' " .
120 // "LEFT OUTER JOIN insurance_data AS i2 ON i2.pid = p.pid AND i2.type = 'secondary' " .
121 "LEFT OUTER JOIN insurance_data AS i1 ON i1.id = ? " .
122 "LEFT OUTER JOIN insurance_data AS i2 ON i2.id = ? " .
124 "LEFT OUTER JOIN insurance_companies AS c1 ON c1.id = i1.provider " .
125 "LEFT OUTER JOIN insurance_companies AS c2 ON c2.id = i2.provider " .
126 "LEFT OUTER JOIN addresses AS a1 ON a1.foreign_id = c1.id " .
127 "LEFT OUTER JOIN addresses AS a2 ON a2.foreign_id = c2.id " .
128 "WHERE p.pid = ? LIMIT 1";
130 $row = sqlFetchArray(sqlStatement($query, array($insrow[0]['id'], $insrow[1]['id'], $pid)));
132 // Get primary care doc info. If none was selected in the patient
133 // demographics then pick the #1 doctor in the clinic.
135 $query = "select id, fname, mname, lname from users where authorized = 1";
136 $sqlBindArray = array();
137 if ($row['providerID']) {
138 $query .= " AND id = ?";
139 array_push($sqlBindArray, $row['providerID']);
140 } else {
141 $query .= " ORDER BY id LIMIT 1";
144 $prow = sqlFetchArray(sqlStatement($query, $sqlBindArray));
146 // Patient Section.
148 $out .= $pid; // patient id
149 Add($row['pubpid']); // chart number
150 Add($row['lname']); // last name
151 Add($row['fname']); // first name
152 Add(substr($row['mname'], 0, 1)); // middle initial
153 Add(""); // alias
154 Add(Digits($row['ss'])); // ssn
155 Add(LWDate($row['DOB'])); // dob
156 Add(Sex($row['sex'])); // gender
157 Add(""); // notes
158 Add($row['street']); // address 1
159 Add(""); // address2
160 Add($row['city']); // city
161 Add($row['state']); // state
162 Add($row['postal_code']); // zip
163 Add(Digits($row['phone_home'])); // home phone
165 // Guarantor Section. OpenEMR does not have guarantors so we use the primary
166 // insurance subscriber if there is one, otherwise the patient.
168 if (trim($row['lname1'])) {
169 Add($row['lname1']);
170 Add($row['fname1']);
171 Add(substr($row['mname1'], 0, 1));
172 Add($row['sstreet1']);
173 Add("");
174 Add($row['scity1']);
175 Add($row['sstate1']);
176 Add($row['szip1']);
177 } else {
178 Add($row['lname']);
179 Add($row['fname']);
180 Add(substr($row['mname'], 0, 1));
181 Add($row['street']);
182 Add("");
183 Add($row['city']);
184 Add($row['state']);
185 Add($row['postal_code']);
188 // Primary Insurance Section.
190 Add($row['provider1']);
191 Add($row['name1']);
192 Add($row['street11']);
193 Add($row['street21']);
194 Add($row['city1']);
195 Add($row['state1']);
196 Add($row['zip1']);
197 Add("");
198 Add(InsType($row['instype1']));
199 Add($row['fname1'] . " " . $row['lname1']);
200 Add(ucfirst($row['relationship1']));
201 Add($row['group1']);
202 Add($row['policy1']);
204 // Secondary Insurance Section.
206 Add($row['provider2']);
207 Add($row['name2']);
208 Add($row['street12']);
209 Add($row['street22']);
210 Add($row['city2']);
211 Add($row['state2']);
212 Add($row['zip2']);
213 Add("");
214 Add(InsType($row['instype2']));
215 Add($row['fname2'] . " " . $row['lname2']);
216 Add(ucfirst($row['relationship2']));
217 Add($row['group2']);
218 Add($row['policy2']);
220 // Primary Care Physician Section.
222 Add($prow['id']);
223 Add($prow['lname']);
224 Add($prow['fname']);
225 Add(substr($prow['mname'], 0, 1));
226 Add(""); // UPIN not available
228 // All done.
229 $out .= "\rEND";
231 // In case this is the very first time.
232 if (! file_exists($EXPORT_PATH)) {
233 mkdir($EXPORT_PATH);
234 @touch("$EXPORT_PATH/unlocked");
237 // Serialize the following code; collisions would be very bad.
238 if (! rename("$EXPORT_PATH/unlocked", "$EXPORT_PATH/locked")) {
239 die("Export seems to be in use by someone else; please try again.");
242 // Figure out what to use for the target filename.
243 $dh = opendir($EXPORT_PATH);
244 if (! $dh) {
245 mydie("Cannot read $EXPORT_PATH");
248 $nextnumber = 1;
249 while (false !== ($filename = readdir($dh))) {
250 if (preg_match("/PMI(\d{8})\.DEM/", $filename, $matches)) {
251 $tmp = 1 + $matches[1];
252 if ($tmp > $nextnumber) {
253 $nextnumber = $tmp;
258 closedir($dh);
259 $fnprefix = sprintf("PMI%08.0f.", $nextnumber);
260 $initialname = $fnprefix . "creating";
261 $finalname = $fnprefix . "DEM";
262 $initialpath = "$EXPORT_PATH/$initialname";
263 $finalpath = "$EXPORT_PATH/$finalname";
265 // Write the file locally with a temporary version of the name.
266 @touch($initialpath); // work around possible php bug
267 $fh = @fopen($initialpath, "w");
268 if (! $fh) {
269 mydie("Unable to open " . text($initialpath) . " for writing");
272 fwrite($fh, $out);
273 fclose($fh);
275 // Rename the local file.
276 rename($initialpath, $finalpath);
278 // Delete old stuff to avoid uncontrolled growth.
279 if ($nextnumber > 5) {
280 @unlink("$EXPORT_PATH/PMI%08.0f.DEM", $nextnumber - 5);
283 // End of serialized code.
284 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
286 // If we have an ftp server, send it there and then rename it.
287 if ($FTP_SERVER) {
288 $ftpconn = ftp_connect($FTP_SERVER) or die("FTP connection failed");
289 ftp_login($ftpconn, $FTP_USER, $FTP_PASS) or die("FTP login failed");
290 if ($FTP_DIR) {
291 ftp_chdir($ftpconn, $FTP_DIR) or die("FTP chdir failed");
294 ftp_put($ftpconn, $initialname, $finalpath, FTP_BINARY) or die("FTP put failed");
295 ftp_rename($ftpconn, $initialname, $finalname) or die("FTP rename failed");
296 ftp_close($ftpconn);
299 <html>
300 <head>
301 <?php Header::setupHeader(); ?>
302 <title>Export Patient Demographics</title>
303 </head>
304 <body>
305 <center>
306 <p>&nbsp;</p>
307 <p>Demographics for <?php echo text($row['fname']) . " " . text($row['lname']); ?>
308 have been exported to LabWorks.</p>
309 <p>&nbsp;</p>
310 <form>
311 <p><input type='button' value='OK' onclick='window.close()' /></p>
312 </form>
313 </center>
314 </body>
315 </html>