Fixing critical issues with Fees > Batch Payments (#2656)
[openemr.git] / custom / export_labworks.php
blob13a619415d8b5f235b2569c2d7bd468e38d5c07c
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");
18 // FTP parameters that you must customize. If you are not sending
19 // then set $FTP_SERVER to an empty string.
21 $FTP_SERVER = "192.168.0.30";
22 $FTP_USER = "openemr";
23 $FTP_PASS = "secret";
24 $FTP_DIR = "";
26 // This is the destination directory on the local machine for the
27 // exported data. Required even if FTP is used.
29 $EXPORT_PATH = "/tmp/labworks";
31 $out = "";
33 // Add a string to output with some basic sanitizing.
34 function Add($field)
36 global $out;
37 $out .= "^" . trim(str_replace(array("\r", "\n", "\t"), " ", $field));
40 // Remove all non-digits from a string.
41 function Digits($field)
43 return preg_replace("/\D/", "", $field);
46 // Translate sex.
47 function Sex($field)
49 $sex = strtoupper(substr(trim($field), 0, 1));
50 if ($sex != "M" && $sex != "F") {
51 $sex = "U";
54 return $sex;
57 // Translate a date.
58 function LWDate($field)
60 $tmp = fixDate($field);
61 return substr($tmp, 5, 2) . substr($tmp, 8, 2) . substr($tmp, 0, 4);
64 // Translate insurance type.
65 function InsType($field)
67 if (! $field) {
68 return "";
71 if ($field == 2) {
72 return "Medicare";
75 if ($field == 3) {
76 return "Medicaid";
79 return "Other";
82 // Error abort function that does not leave the system locked.
83 function mydie($msg)
85 global $EXPORT_PATH;
86 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
87 die($msg);
90 $alertmsg = ""; // anything here pops up in an alert box
92 // This mess gets all the info for the patient.
94 $insrow = array();
95 foreach (array('primary','secondary') as $value) {
96 $insrow[] = sqlQuery("SELECT id FROM insurance_data WHERE " .
97 "pid = ? AND type = ? ORDER BY date DESC LIMIT 1", array($pid, $value));
100 $query = "SELECT " .
101 "p.pubpid, p.fname, p.mname, p.lname, p.DOB, p.providerID, " .
102 "p.ss, p.street, p.city, p.state, p.postal_code, p.phone_home, p.sex, " .
103 "i1.policy_number AS policy1, i1.group_number AS group1, i1.provider as provider1, " .
104 "i1.subscriber_fname AS fname1, i1.subscriber_mname AS mname1, i1.subscriber_lname AS lname1, " .
105 "i1.subscriber_street AS sstreet1, i1.subscriber_city AS scity1, i1.subscriber_state AS sstate1, " .
106 "i1.subscriber_postal_code AS szip1, i1.subscriber_relationship AS relationship1, " .
107 "c1.name AS name1, c1.ins_type_code AS instype1, " .
108 "a1.line1 AS street11, a1.line2 AS street21, a1.city AS city1, a1.state AS state1, " .
109 "a1.zip AS zip1, a1.plus_four AS zip41, " .
110 "i2.policy_number AS policy2, i2.group_number AS group2, i2.provider as provider2, " .
111 "i2.subscriber_fname AS fname2, i2.subscriber_mname AS mname2, i2.subscriber_lname AS lname2, " .
112 "i2.subscriber_relationship AS relationship2, " .
113 "c2.name AS name2, c2.ins_type_code AS instype2, " .
114 "a2.line1 AS street12, a2.line2 AS street22, a2.city AS city2, a2.state AS state2, " .
115 "a2.zip AS zip2, a2.plus_four AS zip42 " .
116 "FROM patient_data AS p " .
117 // "LEFT OUTER JOIN insurance_data AS i1 ON i1.pid = p.pid AND i1.type = 'primary' " .
118 // "LEFT OUTER JOIN insurance_data AS i2 ON i2.pid = p.pid AND i2.type = 'secondary' " .
119 "LEFT OUTER JOIN insurance_data AS i1 ON i1.id = ? " .
120 "LEFT OUTER JOIN insurance_data AS i2 ON i2.id = ? " .
122 "LEFT OUTER JOIN insurance_companies AS c1 ON c1.id = i1.provider " .
123 "LEFT OUTER JOIN insurance_companies AS c2 ON c2.id = i2.provider " .
124 "LEFT OUTER JOIN addresses AS a1 ON a1.foreign_id = c1.id " .
125 "LEFT OUTER JOIN addresses AS a2 ON a2.foreign_id = c2.id " .
126 "WHERE p.pid = ? LIMIT 1";
128 $row = sqlFetchArray(sqlStatement($query, array($insrow[0]['id'], $insrow[1]['id'], $pid)));
130 // Get primary care doc info. If none was selected in the patient
131 // demographics then pick the #1 doctor in the clinic.
133 $query = "select id, fname, mname, lname from users where authorized = 1";
134 $sqlBindArray = array();
135 if ($row['providerID']) {
136 $query .= " AND id = ?";
137 array_push($sqlBindArray, $row['providerID']);
138 } else {
139 $query .= " ORDER BY id LIMIT 1";
142 $prow = sqlFetchArray(sqlStatement($query, $sqlBindArray));
144 // Patient Section.
146 $out .= $pid; // patient id
147 Add($row['pubpid']); // chart number
148 Add($row['lname']); // last name
149 Add($row['fname']); // first name
150 Add(substr($row['mname'], 0, 1)); // middle initial
151 Add(""); // alias
152 Add(Digits($row['ss'])); // ssn
153 Add(LWDate($row['DOB'])); // dob
154 Add(Sex($row['sex'])); // gender
155 Add(""); // notes
156 Add($row['street']); // address 1
157 Add(""); // address2
158 Add($row['city']); // city
159 Add($row['state']); // state
160 Add($row['postal_code']); // zip
161 Add(Digits($row['phone_home'])); // home phone
163 // Guarantor Section. OpenEMR does not have guarantors so we use the primary
164 // insurance subscriber if there is one, otherwise the patient.
166 if (trim($row['lname1'])) {
167 Add($row['lname1']);
168 Add($row['fname1']);
169 Add(substr($row['mname1'], 0, 1));
170 Add($row['sstreet1']);
171 Add("");
172 Add($row['scity1']);
173 Add($row['sstate1']);
174 Add($row['szip1']);
175 } else {
176 Add($row['lname']);
177 Add($row['fname']);
178 Add(substr($row['mname'], 0, 1));
179 Add($row['street']);
180 Add("");
181 Add($row['city']);
182 Add($row['state']);
183 Add($row['postal_code']);
186 // Primary Insurance Section.
188 Add($row['provider1']);
189 Add($row['name1']);
190 Add($row['street11']);
191 Add($row['street21']);
192 Add($row['city1']);
193 Add($row['state1']);
194 Add($row['zip1']);
195 Add("");
196 Add(InsType($row['instype1']));
197 Add($row['fname1'] . " " . $row['lname1']);
198 Add(ucfirst($row['relationship1']));
199 Add($row['group1']);
200 Add($row['policy1']);
202 // Secondary Insurance Section.
204 Add($row['provider2']);
205 Add($row['name2']);
206 Add($row['street12']);
207 Add($row['street22']);
208 Add($row['city2']);
209 Add($row['state2']);
210 Add($row['zip2']);
211 Add("");
212 Add(InsType($row['instype2']));
213 Add($row['fname2'] . " " . $row['lname2']);
214 Add(ucfirst($row['relationship2']));
215 Add($row['group2']);
216 Add($row['policy2']);
218 // Primary Care Physician Section.
220 Add($prow['id']);
221 Add($prow['lname']);
222 Add($prow['fname']);
223 Add(substr($prow['mname'], 0, 1));
224 Add(""); // UPIN not available
226 // All done.
227 $out .= "\rEND";
229 // In case this is the very first time.
230 if (! file_exists($EXPORT_PATH)) {
231 mkdir($EXPORT_PATH);
232 @touch("$EXPORT_PATH/unlocked");
235 // Serialize the following code; collisions would be very bad.
236 if (! rename("$EXPORT_PATH/unlocked", "$EXPORT_PATH/locked")) {
237 die("Export seems to be in use by someone else; please try again.");
240 // Figure out what to use for the target filename.
241 $dh = opendir($EXPORT_PATH);
242 if (! $dh) {
243 mydie("Cannot read $EXPORT_PATH");
246 $nextnumber = 1;
247 while (false !== ($filename = readdir($dh))) {
248 if (preg_match("/PMI(\d{8})\.DEM/", $filename, $matches)) {
249 $tmp = 1 + $matches[1];
250 if ($tmp > $nextnumber) {
251 $nextnumber = $tmp;
256 closedir($dh);
257 $fnprefix = sprintf("PMI%08.0f.", $nextnumber);
258 $initialname = $fnprefix . "creating";
259 $finalname = $fnprefix . "DEM";
260 $initialpath = "$EXPORT_PATH/$initialname";
261 $finalpath = "$EXPORT_PATH/$finalname";
263 // Write the file locally with a temporary version of the name.
264 @touch($initialpath); // work around possible php bug
265 $fh = @fopen($initialpath, "w");
266 if (! $fh) {
267 mydie("Unable to open " . text($initialpath) . " for writing");
270 fwrite($fh, $out);
271 fclose($fh);
273 // Rename the local file.
274 rename($initialpath, $finalpath);
276 // Delete old stuff to avoid uncontrolled growth.
277 if ($nextnumber > 5) {
278 @unlink("$EXPORT_PATH/PMI%08.0f.DEM", $nextnumber - 5);
281 // End of serialized code.
282 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
284 // If we have an ftp server, send it there and then rename it.
285 if ($FTP_SERVER) {
286 $ftpconn = ftp_connect($FTP_SERVER) or die("FTP connection failed");
287 ftp_login($ftpconn, $FTP_USER, $FTP_PASS) or die("FTP login failed");
288 if ($FTP_DIR) {
289 ftp_chdir($ftpconn, $FTP_DIR) or die("FTP chdir failed");
292 ftp_put($ftpconn, $initialname, $finalpath, FTP_BINARY) or die("FTP put failed");
293 ftp_rename($ftpconn, $initialname, $finalname) or die("FTP rename failed");
294 ftp_close($ftpconn);
297 <html>
298 <head>
299 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
300 <title>Export Patient Demographics</title>
301 </head>
302 <body>
303 <center>
304 <p>&nbsp;</p>
305 <p>Demographics for <?php echo text($row['fname']) . " " . text($row['lname']); ?>
306 have been exported to LabWorks.</p>
307 <p>&nbsp;</p>
308 <form>
309 <p><input type='button' value='OK' onclick='window.close()' /></p>
310 </form>
311 </center>
312 </body>
313 </html>