To make it easy to download the latest version from online CVS browser.
[openemr.git] / custom / export_xml.php
blob973d6328c7f9ab514e79ea760f49d86fcddce87a
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 $query = "SELECT " .
105 "p.pubpid, p.fname, p.mname, p.lname, p.DOB, p.providerID, " .
106 "p.ss, p.street, p.city, p.state, p.postal_code, p.phone_home, p.sex, " .
107 "p.title, p.country_code, p.occupation, p.phone_biz, p.phone_contact, p.phone_cell, " .
108 "p.status, p.contact_relationship, p.referrer, p.referrerID, p.email, " .
109 "p.language, p.ethnoracial, p.interpretter, p.migrantseasonal, p.family_size, " .
110 "p.monthly_income, p.homeless, p.financial_review, p.hipaa_mail, p.hipaa_voice, " .
111 "p.genericname1, p.genericval1, p.genericname2, p.genericval2, " .
112 "i1.policy_number AS policy1, i1.group_number AS group1, i1.provider as provider1, " .
113 "i1.subscriber_fname AS fname1, i1.subscriber_mname AS mname1, i1.subscriber_lname AS lname1, " .
114 "i1.subscriber_street AS sstreet1, i1.subscriber_city AS scity1, i1.subscriber_state AS sstate1, " .
115 "i1.subscriber_postal_code AS szip1, i1.subscriber_relationship AS relationship1, " .
116 "i1.subscriber_DOB AS sdob1, i1.subscriber_ss AS sss1, i1.subscriber_phone AS sphone1, " .
117 "i1.subscriber_sex AS ssex1, i1.subscriber_country AS scountry1, " .
118 "i1.subscriber_employer AS semployer1, i1.subscriber_employer_street AS semployer_street1, " .
119 "i1.subscriber_employer_city AS semployer_city1, i1.subscriber_employer_state AS semployer_state1, " .
120 "i1.subscriber_employer_postal_code AS semployer_zip1, " .
121 "i1.subscriber_employer_country AS semployer_country1, i1.copay AS copay1, " .
122 "c1.name AS name1, c1.freeb_type AS instype1, " .
123 "a1.line1 AS street11, a1.line2 AS street21, a1.city AS city1, a1.state AS state1, " .
124 "a1.zip AS zip1, a1.plus_four AS zip41, a1.country AS country1, " .
125 "i2.policy_number AS policy2, i2.group_number AS group2, i2.provider as provider2, " .
126 "i2.subscriber_fname AS fname2, i2.subscriber_mname AS mname2, i2.subscriber_lname AS lname2, " .
127 "i2.subscriber_postal_code AS szip2, i2.subscriber_relationship AS relationship2, " .
128 "i2.subscriber_DOB AS sdob2, i2.subscriber_ss AS sss2, i2.subscriber_phone AS sphone2, " .
129 "i2.subscriber_sex AS ssex2, i2.subscriber_country AS scountry2, " .
130 "i2.subscriber_employer AS semployer2, i2.subscriber_employer_street AS semployer_street2, " .
131 "i2.subscriber_employer_city AS semployer_city2, i2.subscriber_employer_state AS semployer_state2, " .
132 "i2.subscriber_employer_postal_code AS semployer_zip2, " .
133 "i2.subscriber_employer_country AS semployer_country2, i2.copay AS copay2, " .
134 "c2.name AS name2, c2.freeb_type AS instype2, " .
135 "a2.line1 AS street12, a2.line2 AS street22, a2.city AS city2, a2.state AS state2, " .
136 "a2.zip AS zip2, a2.plus_four AS zip42, a2.country AS country2, " .
137 "i3.policy_number AS policy3, i3.group_number AS group3, i3.provider as provider3, " .
138 "i3.subscriber_fname AS fname3, i3.subscriber_mname AS mname3, i3.subscriber_lname AS lname3, " .
139 "i3.subscriber_postal_code AS szip3, i3.subscriber_relationship AS relationship3, " .
140 "i3.subscriber_DOB AS sdob3, i3.subscriber_ss AS sss3, i3.subscriber_phone AS sphone3, " .
141 "i3.subscriber_sex AS ssex3, i3.subscriber_country AS scountry3, " .
142 "i3.subscriber_employer AS semployer3, i3.subscriber_employer_street AS semployer_street3, " .
143 "i3.subscriber_employer_city AS semployer_city3, i3.subscriber_employer_state AS semployer_state3, " .
144 "i3.subscriber_employer_postal_code AS semployer_zip3, " .
145 "i3.subscriber_employer_country AS semployer_country3, i3.copay AS copay3, " .
146 "c3.name AS name3, c3.freeb_type AS instype3, " .
147 "a3.line1 AS street13, a3.line2 AS street23, a3.city AS city3, a3.state AS state3, " .
148 "a3.zip AS zip3, a3.plus_four AS zip43, a3.country AS country3 " .
149 "FROM patient_data AS p " .
150 "LEFT OUTER JOIN insurance_data AS i1 ON i1.pid = p.pid AND i1.type = 'primary' " .
151 "LEFT OUTER JOIN insurance_data AS i2 ON i2.pid = p.pid AND i2.type = 'secondary' " .
152 "LEFT OUTER JOIN insurance_data AS i3 ON i3.pid = p.pid AND i3.type = 'tertiary' " .
153 "LEFT OUTER JOIN insurance_companies AS c1 ON c1.id = i1.provider " .
154 "LEFT OUTER JOIN insurance_companies AS c2 ON c2.id = i2.provider " .
155 "LEFT OUTER JOIN insurance_companies AS c3 ON c3.id = i3.provider " .
156 "LEFT OUTER JOIN addresses AS a1 ON a1.foreign_id = c1.id " .
157 "LEFT OUTER JOIN addresses AS a2 ON a2.foreign_id = c2.id " .
158 "LEFT OUTER JOIN addresses AS a3 ON a3.foreign_id = c3.id " .
159 "WHERE p.pid = '$pid' LIMIT 1";
161 $row = sqlFetchArray(sqlStatement($query));
163 $rowed = getEmployerData($pid);
165 OpenTag("patient");
167 // Patient Section.
169 Add("pid" , $pid);
170 Add("pubpid" , $row['pubpid']);
171 Add("lname" , $row['lname']);
172 Add("fname" , $row['fname']);
173 Add("mname" , $row['mname']);
174 Add("title" , $row['title']);
175 Add("ss" , Digits($row['ss']));
176 Add("dob" , LWDate($row['DOB']));
177 Add("sex" , Sex($row['sex']));
178 Add("street" , $row['street']);
179 Add("city" , $row['city']);
180 Add("state" , $row['state']);
181 Add("zip" , $row['postal_code']);
182 Add("country" , $row['country_code']);
183 Add("phone_home" , Digits($row['phone_home']));
184 Add("phone_biz" , Digits($row['phone_biz']));
185 Add("phone_contact" , Digits($row['phone_contact']));
186 Add("phone_cell" , Digits($row['phone_cell']));
187 Add("occupation" , $row['occupation']);
188 Add("status" , $row['status']);
189 Add("contact_relationship", $row['contact_relationship']);
190 Add("referrer" , $row['referrer']);
191 Add("referrerID" , $row['referrerID']);
192 Add("email" , $row['email']);
193 Add("language" , $row['language']);
194 Add("ethnoracial" , $row['ethnoracial']);
195 Add("interpreter" , $row['interpretter']);
196 Add("migrantseasonal" , $row['migrantseasonal']);
197 Add("family_size" , $row['family_size']);
198 Add("monthly_income" , $row['monthly_income']);
199 Add("homeless" , $row['homeless']);
200 Add("financial_review" , LWDate(substr($row['financial_review'], 0, 10)));
201 Add("genericname1" , $row['genericname1']);
202 Add("genericval1" , $row['genericval1']);
203 Add("genericname2" , $row['genericname2']);
204 Add("genericval2" , $row['genericval2']);
205 Add("hipaa_mail" , $row['hipaa_mail']);
206 Add("hipaa_voice" , $row['hipaa_voice']);
208 // Insurance Sections.
210 addInsurance($row, '1');
211 addInsurance($row, '2');
212 addInsurance($row, '3');
214 // Primary Care Physician Section.
216 if ($row['providerID']) {
217 $query = "select id, fname, mname, lname from users where authorized = 1";
218 $query .= " AND id = " . $row['providerID'];
219 $prow = sqlFetchArray(sqlStatement($query));
220 OpenTag("pcp");
221 Add("id", $prow['id']);
222 Add("lname", $prow['lname']);
223 Add("fname", $prow['fname']);
224 Add("mname", $prow['mname']);
225 CloseTag("pcp");
228 // Employer Section.
230 if ($rowed['id']) {
231 OpenTag("employer");
232 Add("name" , $rowed['name']);
233 Add("street" , $rowed['street']);
234 Add("zip" , $rowed['postal_code']);
235 Add("city" , $rowed['city']);
236 Add("state" , $rowed['state']);
237 Add("country", $rowed['country']);
238 CloseTag("employer");
241 // All done.
242 CloseTag("patient");
244 // header('Content-type: text/xml');
245 // header('Content-Disposition: attachment; filename="pid' . $pid . '.xml"');
246 // echo $out;
248 <html>
249 <head>
250 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
251 <title>Export Patient Demographics</title>
252 </head>
253 <body>
255 <p>The exported data appears in the text area below. You can copy and
256 paste this into an email or to any other desired destination.</p>
258 <center>
259 <form>
261 <textarea rows='10' cols='50' style='width:95%' readonly>
262 <? echo $out ?>
263 </textarea>
265 <p><input type='button' value='OK' onclick='window.close()' /></p>
266 </form>
267 </center>
269 </body>
270 </html>