added ending dates of service
[openemr.git] / custom / export_xml.php
blobfd95c9b4483c1073c0b19a0cfa49a3bde4383d27
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 to a custom XML format.
11 /////////////////////////////////////////////////////////////////////
13 include_once("../interface/globals.php");
14 include_once("../library/patient.inc");
16 $out = "";
17 $indent = 0;
19 // Add a string to output with some basic sanitizing.
20 function Add($tag, $text) {
21 global $out, $indent;
22 $text = trim(str_replace(array("\r", "\n", "\t"), " ", $text));
23 if ($text) {
24 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
25 $out .= "<$tag>$text</$tag>\n";
29 function OpenTag($tag) {
30 global $out, $indent;
31 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
32 ++$indent;
33 $out .= "<$tag>\n";
36 function CloseTag($tag) {
37 global $out, $indent;
38 --$indent;
39 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
40 $out .= "</$tag>\n";
43 // Remove all non-digits from a string.
44 function Digits($field) {
45 return preg_replace("/\D/", "", $field);
48 // Translate sex.
49 function Sex($field) {
50 $sex = strtoupper(substr(trim($field), 0, 1));
51 if ($sex != "M" && $sex != "F") $sex = "U";
52 return $sex;
55 // Translate a date.
56 function LWDate($field) {
57 return fixDate($field);
60 // Add an insurance section.
61 function addInsurance($row, $seq) {
62 if ($row["name$seq"]) {
63 OpenTag("insurance");
64 Add("priority" , $seq);
65 Add("group" , $row["group$seq"]);
66 Add("policy" , $row["policy$seq"]);
67 Add("provider" , $row["provider$seq"]);
68 Add("name" , $row["name$seq"]);
69 Add("street1" , $row["street1$seq"]);
70 Add("street2" , $row["street2$seq"]);
71 Add("city" , $row["city$seq"]);
72 Add("state" , $row["state$seq"]);
73 Add("zip" , $row["zip$seq"]);
74 Add("country" , $row["country$seq"]);
75 Add("type" , $row["instype$seq"]);
76 Add("copay" , $row["copay$seq"]);
77 OpenTag("subscriber");
78 Add("relationship" , $row["relationship$seq"]);
79 Add("lname" , $row["lname$seq"]);
80 Add("fname" , $row["fname$seq"]);
81 Add("mname" , $row["mname$seq"]);
82 Add("street" , $row["sstreet$seq"]);
83 Add("city" , $row["scity$seq"]);
84 Add("state" , $row["sstate$seq"]);
85 Add("zip" , $row["szip$seq"]);
86 Add("country" , $row["scountry$seq"]);
87 Add("dob" , $row["sdob$seq"]);
88 Add("ss" , $row["sss$seq"]);
89 Add("phone" , $row["sphone$seq"]);
90 Add("employer" , $row["semployer$seq"]);
91 Add("sex" , $row["ssex$seq"]);
92 Add("employer_street" , $row["semployer_street$seq"]);
93 Add("employer_city" , $row["semployer_city$seq"]);
94 Add("employer_state" , $row["semployer_state$seq"]);
95 Add("employer_zip" , $row["semployer_zip$seq"]);
96 Add("employer_country" , $row["semployer_country$seq"]);
97 CloseTag("subscriber");
98 CloseTag("insurance");
102 // This mess gets all the info for the patient.
104 $insrow = array();
105 foreach (array('primary','secondary','tertiary') as $value) {
106 $insrow[] = sqlQuery("SELECT id FROM insurance_data WHERE " .
107 "pid = '$pid' AND type = '$value' ORDER BY date DESC LIMIT 1");
109 $query = "SELECT " .
110 "p.pubpid, p.fname, p.mname, p.lname, p.DOB, p.providerID, " .
111 "p.ss, p.street, p.city, p.state, p.postal_code, p.phone_home, p.sex, " .
112 "p.title, p.country_code, p.occupation, p.phone_biz, p.phone_contact, p.phone_cell, " .
113 "p.status, p.contact_relationship, p.referrer, p.referrerID, p.email, " .
114 "p.language, p.ethnoracial, p.interpretter, p.migrantseasonal, p.family_size, " .
115 "p.monthly_income, p.homeless, p.financial_review, p.hipaa_mail, p.hipaa_voice, " .
116 "p.genericname1, p.genericval1, p.genericname2, p.genericval2, " .
117 "i1.policy_number AS policy1, i1.group_number AS group1, i1.provider as provider1, " .
118 "i1.subscriber_fname AS fname1, i1.subscriber_mname AS mname1, i1.subscriber_lname AS lname1, " .
119 "i1.subscriber_street AS sstreet1, i1.subscriber_city AS scity1, i1.subscriber_state AS sstate1, " .
120 "i1.subscriber_postal_code AS szip1, i1.subscriber_relationship AS relationship1, " .
121 "i1.subscriber_DOB AS sdob1, i1.subscriber_ss AS sss1, i1.subscriber_phone AS sphone1, " .
122 "i1.subscriber_sex AS ssex1, i1.subscriber_country AS scountry1, " .
123 "i1.subscriber_employer AS semployer1, i1.subscriber_employer_street AS semployer_street1, " .
124 "i1.subscriber_employer_city AS semployer_city1, i1.subscriber_employer_state AS semployer_state1, " .
125 "i1.subscriber_employer_postal_code AS semployer_zip1, " .
126 "i1.subscriber_employer_country AS semployer_country1, i1.copay AS copay1, " .
127 "c1.name AS name1, c1.freeb_type AS instype1, " .
128 "a1.line1 AS street11, a1.line2 AS street21, a1.city AS city1, a1.state AS state1, " .
129 "a1.zip AS zip1, a1.plus_four AS zip41, a1.country AS country1, " .
130 "i2.policy_number AS policy2, i2.group_number AS group2, i2.provider as provider2, " .
131 "i2.subscriber_fname AS fname2, i2.subscriber_mname AS mname2, i2.subscriber_lname AS lname2, " .
132 "i2.subscriber_postal_code AS szip2, i2.subscriber_relationship AS relationship2, " .
133 "i2.subscriber_DOB AS sdob2, i2.subscriber_ss AS sss2, i2.subscriber_phone AS sphone2, " .
134 "i2.subscriber_sex AS ssex2, i2.subscriber_country AS scountry2, " .
135 "i2.subscriber_employer AS semployer2, i2.subscriber_employer_street AS semployer_street2, " .
136 "i2.subscriber_employer_city AS semployer_city2, i2.subscriber_employer_state AS semployer_state2, " .
137 "i2.subscriber_employer_postal_code AS semployer_zip2, " .
138 "i2.subscriber_employer_country AS semployer_country2, i2.copay AS copay2, " .
139 "c2.name AS name2, c2.freeb_type AS instype2, " .
140 "a2.line1 AS street12, a2.line2 AS street22, a2.city AS city2, a2.state AS state2, " .
141 "a2.zip AS zip2, a2.plus_four AS zip42, a2.country AS country2, " .
142 "i3.policy_number AS policy3, i3.group_number AS group3, i3.provider as provider3, " .
143 "i3.subscriber_fname AS fname3, i3.subscriber_mname AS mname3, i3.subscriber_lname AS lname3, " .
144 "i3.subscriber_postal_code AS szip3, i3.subscriber_relationship AS relationship3, " .
145 "i3.subscriber_DOB AS sdob3, i3.subscriber_ss AS sss3, i3.subscriber_phone AS sphone3, " .
146 "i3.subscriber_sex AS ssex3, i3.subscriber_country AS scountry3, " .
147 "i3.subscriber_employer AS semployer3, i3.subscriber_employer_street AS semployer_street3, " .
148 "i3.subscriber_employer_city AS semployer_city3, i3.subscriber_employer_state AS semployer_state3, " .
149 "i3.subscriber_employer_postal_code AS semployer_zip3, " .
150 "i3.subscriber_employer_country AS semployer_country3, i3.copay AS copay3, " .
151 "c3.name AS name3, c3.freeb_type AS instype3, " .
152 "a3.line1 AS street13, a3.line2 AS street23, a3.city AS city3, a3.state AS state3, " .
153 "a3.zip AS zip3, a3.plus_four AS zip43, a3.country AS country3 " .
154 "FROM patient_data AS p " .
155 // "LEFT OUTER JOIN insurance_data AS i1 ON i1.pid = p.pid AND i1.type = 'primary' " .
156 // "LEFT OUTER JOIN insurance_data AS i2 ON i2.pid = p.pid AND i2.type = 'secondary' " .
157 // "LEFT OUTER JOIN insurance_data AS i3 ON i3.pid = p.pid AND i3.type = 'tertiary' " .
158 "LEFT OUTER JOIN insurance_data AS i1 ON i1.id = '" . $insrow[0]['id'] . "' " .
159 "LEFT OUTER JOIN insurance_data AS i2 ON i2.id = '" . $insrow[1]['id'] . "' " .
160 "LEFT OUTER JOIN insurance_data AS i3 ON i3.id = '" . $insrow[2]['id'] . "' " .
162 "LEFT OUTER JOIN insurance_companies AS c1 ON c1.id = i1.provider " .
163 "LEFT OUTER JOIN insurance_companies AS c2 ON c2.id = i2.provider " .
164 "LEFT OUTER JOIN insurance_companies AS c3 ON c3.id = i3.provider " .
165 "LEFT OUTER JOIN addresses AS a1 ON a1.foreign_id = c1.id " .
166 "LEFT OUTER JOIN addresses AS a2 ON a2.foreign_id = c2.id " .
167 "LEFT OUTER JOIN addresses AS a3 ON a3.foreign_id = c3.id " .
168 "WHERE p.pid = '$pid' LIMIT 1";
170 $row = sqlFetchArray(sqlStatement($query));
172 $rowed = getEmployerData($pid);
174 OpenTag("patient");
176 // Patient Section.
178 Add("pid" , $pid);
179 Add("pubpid" , $row['pubpid']);
180 Add("lname" , $row['lname']);
181 Add("fname" , $row['fname']);
182 Add("mname" , $row['mname']);
183 Add("title" , $row['title']);
184 Add("ss" , Digits($row['ss']));
185 Add("dob" , LWDate($row['DOB']));
186 Add("sex" , Sex($row['sex']));
187 Add("street" , $row['street']);
188 Add("city" , $row['city']);
189 Add("state" , $row['state']);
190 Add("zip" , $row['postal_code']);
191 Add("country" , $row['country_code']);
192 Add("phone_home" , Digits($row['phone_home']));
193 Add("phone_biz" , Digits($row['phone_biz']));
194 Add("phone_contact" , Digits($row['phone_contact']));
195 Add("phone_cell" , Digits($row['phone_cell']));
196 Add("occupation" , $row['occupation']);
197 Add("status" , $row['status']);
198 Add("contact_relationship", $row['contact_relationship']);
199 Add("referrer" , $row['referrer']);
200 Add("referrerID" , $row['referrerID']);
201 Add("email" , $row['email']);
202 Add("language" , $row['language']);
203 Add("ethnoracial" , $row['ethnoracial']);
204 Add("interpreter" , $row['interpretter']);
205 Add("migrantseasonal" , $row['migrantseasonal']);
206 Add("family_size" , $row['family_size']);
207 Add("monthly_income" , $row['monthly_income']);
208 Add("homeless" , $row['homeless']);
209 Add("financial_review" , LWDate(substr($row['financial_review'], 0, 10)));
210 Add("genericname1" , $row['genericname1']);
211 Add("genericval1" , $row['genericval1']);
212 Add("genericname2" , $row['genericname2']);
213 Add("genericval2" , $row['genericval2']);
214 Add("hipaa_mail" , $row['hipaa_mail']);
215 Add("hipaa_voice" , $row['hipaa_voice']);
217 // Insurance Sections.
219 addInsurance($row, '1');
220 addInsurance($row, '2');
221 addInsurance($row, '3');
223 // Primary Care Physician Section.
225 if ($row['providerID']) {
226 $query = "select id, fname, mname, lname from users where authorized = 1";
227 $query .= " AND id = " . $row['providerID'];
228 $prow = sqlFetchArray(sqlStatement($query));
229 OpenTag("pcp");
230 Add("id", $prow['id']);
231 Add("lname", $prow['lname']);
232 Add("fname", $prow['fname']);
233 Add("mname", $prow['mname']);
234 CloseTag("pcp");
237 // Employer Section.
239 if ($rowed['id']) {
240 OpenTag("employer");
241 Add("name" , $rowed['name']);
242 Add("street" , $rowed['street']);
243 Add("zip" , $rowed['postal_code']);
244 Add("city" , $rowed['city']);
245 Add("state" , $rowed['state']);
246 Add("country", $rowed['country']);
247 CloseTag("employer");
250 // All done.
251 CloseTag("patient");
253 // header('Content-type: text/xml');
254 // header('Content-Disposition: attachment; filename="pid' . $pid . '.xml"');
255 // echo $out;
257 <html>
258 <head>
259 <? html_header_show();?>
260 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
261 <title>Export Patient Demographics</title>
262 </head>
263 <body>
265 <p>The exported data appears in the text area below. You can copy and
266 paste this into an email or to any other desired destination.</p>
268 <center>
269 <form>
271 <textarea rows='10' cols='50' style='width:95%' readonly>
272 <? echo $out ?>
273 </textarea>
275 <p><input type='button' value='OK' onclick='window.close()' /></p>
276 </form>
277 </center>
279 </body>
280 </html>