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