2 // Copyright (C) 2005 Rod Roark <rod@sunsetsystems.com>
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";
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";
32 // Add a string to output with some basic sanitizing.
33 function Add($field) {
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);
44 function Sex($field) {
45 $sex = strtoupper(substr(trim($field), 0, 1));
46 if ($sex != "M" && $sex != "F") $sex = "U";
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";
64 // Error abort function that does not leave the system locked.
65 function mydie($msg) {
67 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
71 $alertmsg = ""; // anything here pops up in an alert box
73 // This mess gets all the info for the patient.
76 "p.pubpid, p.fname, p.mname, p.lname, p.DOB, p.providerID, " .
77 "p.ss, p.street, p.city, p.state, p.postal_code, p.phone_home, p.sex, " .
78 "i1.policy_number AS policy1, i1.group_number AS group1, i1.provider as provider1, " .
79 "i1.subscriber_fname AS fname1, i1.subscriber_mname AS mname1, i1.subscriber_lname AS lname1, " .
80 "i1.subscriber_street AS sstreet1, i1.subscriber_city AS scity1, i1.subscriber_state AS sstate1, " .
81 "i1.subscriber_postal_code AS szip1, i1.subscriber_relationship AS relationship1, " .
82 "c1.name AS name1, c1.freeb_type AS instype1, " .
83 "a1.line1 AS street11, a1.line2 AS street21, a1.city AS city1, a1.state AS state1, " .
84 "a1.zip AS zip1, a1.plus_four AS zip41, " .
85 "i2.policy_number AS policy2, i2.group_number AS group2, i2.provider as provider2, " .
86 "i2.subscriber_fname AS fname2, i2.subscriber_mname AS mname2, i2.subscriber_lname AS lname2, " .
87 "i2.subscriber_relationship AS relationship2, " .
88 "c2.name AS name2, c2.freeb_type AS instype2, " .
89 "a2.line1 AS street12, a2.line2 AS street22, a2.city AS city2, a2.state AS state2, " .
90 "a2.zip AS zip2, a2.plus_four AS zip42 " .
91 "FROM patient_data AS p " .
92 "LEFT OUTER JOIN insurance_data AS i1 ON i1.pid = p.pid AND i1.type = 'primary' " .
93 "LEFT OUTER JOIN insurance_data AS i2 ON i2.pid = p.pid AND i2.type = 'secondary' " .
94 "LEFT OUTER JOIN insurance_companies AS c1 ON c1.id = i1.provider " .
95 "LEFT OUTER JOIN insurance_companies AS c2 ON c2.id = i2.provider " .
96 "LEFT OUTER JOIN addresses AS a1 ON a1.foreign_id = c1.id " .
97 "LEFT OUTER JOIN addresses AS a2 ON a2.foreign_id = c2.id " .
98 "WHERE p.pid = '$pid' LIMIT 1";
100 $row = sqlFetchArray(sqlStatement($query));
102 // Get primary care doc info. If none was selected in the patient
103 // demographics then pick the #1 doctor in the clinic.
105 $query = "select id, fname, mname, lname from users where authorized = 1";
106 if ($row['providerID']) {
107 $query .= " AND id = " . $row['providerID'];
109 $query .= " ORDER BY id LIMIT 1";
111 $prow = sqlFetchArray(sqlStatement($query));
115 $out .= $pid; // patient id
116 Add($row['pubpid']); // chart number
117 Add($row['lname']); // last name
118 Add($row['fname']); // first name
119 Add(substr($row['mname'], 0, 1)); // middle initial
121 Add(Digits($row['ss'])); // ssn
122 Add(LWDate($row['DOB'])); // dob
123 Add(Sex($row['sex'])); // gender
125 Add($row['street']); // address 1
127 Add($row['city']); // city
128 Add($row['state']); // state
129 Add($row['postal_code']); // zip
130 Add(Digits($row['phone_home'])); // home phone
132 // Guarantor Section. OpenEMR does not have guarantors so we use the primary
133 // insurance subscriber if there is one, otherwise the patient.
135 if (trim($row['lname1'])) {
138 Add(substr($row['mname1'], 0, 1));
139 Add($row['sstreet1']);
142 Add($row['sstate1']);
147 Add(substr($row['mname'], 0, 1));
152 Add($row['postal_code']);
155 // Primary Insurance Section.
157 Add($row['provider1']);
159 Add($row['street11']);
160 Add($row['street21']);
165 Add(InsType($row['instype1']));
166 Add($row['fname1'] . " " . $row['lname1']);
167 Add(ucfirst($row['relationship1']));
169 Add($row['policy1']);
171 // Secondary Insurance Section.
173 Add($row['provider2']);
175 Add($row['street12']);
176 Add($row['street22']);
181 Add(InsType($row['instype2']));
182 Add($row['fname2'] . " " . $row['lname2']);
183 Add(ucfirst($row['relationship2']));
185 Add($row['policy2']);
187 // Primary Care Physician Section.
192 Add(substr($prow['mname'], 0, 1));
193 Add(""); // UPIN not available
198 // In case this is the very first time.
199 if (! file_exists($EXPORT_PATH)) {
201 @touch
("$EXPORT_PATH/unlocked");
204 // Serialize the following code; collisions would be very bad.
205 if (! rename("$EXPORT_PATH/unlocked", "$EXPORT_PATH/locked"))
206 die("Export seems to be in use by someone else; please try again.");
208 // Figure out what to use for the target filename.
209 $dh = opendir($EXPORT_PATH);
210 if (! $dh) mydie("Cannot read $EXPORT_PATH");
212 while (false !== ($filename = readdir($dh))) {
213 if (preg_match("/PMI(\d{8})\.DEM/", $filename, $matches)) {
214 $tmp = 1 +
$matches[1];
215 if ($tmp > $nextnumber) {
221 $fnprefix = sprintf("PMI%08.0f.", $nextnumber);
222 $initialname = $fnprefix . "creating";
223 $finalname = $fnprefix . "DEM";
224 $initialpath = "$EXPORT_PATH/$initialname";
225 $finalpath = "$EXPORT_PATH/$finalname";
227 // Write the file locally with a temporary version of the name.
228 @touch
($initialpath); // work around possible php bug
229 $fh = @fopen
($initialpath, "w");
230 if (! $fh) mydie("Unable to open $initialpath for writing");
234 // Rename the local file.
235 rename($initialpath, $finalpath);
237 // Delete old stuff to avoid uncontrolled growth.
238 if ($nextnumber > 5) {
239 @unlink
("$EXPORT_PATH/PMI%08.0f.DEM", $nextnumber - 5);
242 // End of serialized code.
243 rename("$EXPORT_PATH/locked", "$EXPORT_PATH/unlocked");
245 // If we have an ftp server, send it there and then rename it.
247 $ftpconn = ftp_connect($FTP_SERVER) or die("FTP connection failed");
248 ftp_login($ftpconn, $FTP_USER, $FTP_PASS) or die("FTP login failed");
249 if ($FTP_DIR) ftp_chdir($ftpconn, $FTP_DIR) or die("FTP chdir failed");
250 ftp_put($ftpconn, $initialname, $finalpath, FTP_BINARY
) or die("FTP put failed");
251 ftp_rename($ftpconn, $initialname, $finalname) or die("FTP rename failed");
257 <link rel
=stylesheet href
="<?echo $css_header;?>" type
="text/css">
258 <title
>Export Patient Demographics
</title
>
263 <p
>Demographics
for <?
echo $row['fname'] . " " . $row['lname'] ?
>
264 have been exported to LabWorks
.</p
>
267 <p
><input type
='button' value
='OK' onclick
='window.close()' /></p
>