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 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");
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'];
117 $query .= " ORDER BY id LIMIT 1";
119 $prow = sqlFetchArray(sqlStatement($query));
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
129 Add(Digits($row['ss'])); // ssn
130 Add(LWDate($row['DOB'])); // dob
131 Add(Sex($row['sex'])); // gender
133 Add($row['street']); // address 1
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'])) {
146 Add(substr($row['mname1'], 0, 1));
147 Add($row['sstreet1']);
150 Add($row['sstate1']);
155 Add(substr($row['mname'], 0, 1));
160 Add($row['postal_code']);
163 // Primary Insurance Section.
165 Add($row['provider1']);
167 Add($row['street11']);
168 Add($row['street21']);
173 Add(InsType($row['instype1']));
174 Add($row['fname1'] . " " . $row['lname1']);
175 Add(ucfirst($row['relationship1']));
177 Add($row['policy1']);
179 // Secondary Insurance Section.
181 Add($row['provider2']);
183 Add($row['street12']);
184 Add($row['street22']);
189 Add(InsType($row['instype2']));
190 Add($row['fname2'] . " " . $row['lname2']);
191 Add(ucfirst($row['relationship2']));
193 Add($row['policy2']);
195 // Primary Care Physician Section.
200 Add(substr($prow['mname'], 0, 1));
201 Add(""); // UPIN not available
206 // In case this is the very first time.
207 if (! file_exists($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");
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) {
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");
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.
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");
265 <?
html_header_show();?
>
266 <link rel
=stylesheet href
="<?echo $css_header;?>" type
="text/css">
267 <title
>Export Patient Demographics
</title
>
272 <p
>Demographics
for <?
echo $row['fname'] . " " . $row['lname'] ?
>
273 have been exported to LabWorks
.</p
>
276 <p
><input type
='button' value
='OK' onclick
='window.close()' /></p
>