fix: add missing use statement in facility admin script (#7428)
[openemr.git] / custom / export_xml.php
blob416436eac0151aefb619e421f8f46f917f5365f2
1 <?php
3 /**
4 * Exports patient demographics to a custom XML format
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Roberto Vasquez <robertogagliotta@gmail.com>
10 * @copyright Copyright (c) 2005 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017 Roberto Vasquez <robertogagliotta@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../interface/globals.php");
16 require_once("../library/patient.inc.php");
18 use OpenEMR\Core\Header;
20 $out = "";
21 $indent = 0;
23 // Add a string to output with some basic sanitizing.
24 function Add($tag, $text)
26 global $out, $indent;
27 $text = trim(str_replace(array("\r", "\n", "\t"), " ", ($text ?? '')));
28 if ($text) {
29 for ($i = 0; $i < $indent; ++$i) {
30 $out .= "\t";
33 $out .= "<$tag>$text</$tag>\n";
37 function OpenTag($tag)
39 global $out, $indent;
40 for ($i = 0; $i < $indent; ++$i) {
41 $out .= "\t";
44 ++$indent;
45 $out .= "<$tag>\n";
48 function CloseTag($tag)
50 global $out, $indent;
51 --$indent;
52 for ($i = 0; $i < $indent; ++$i) {
53 $out .= "\t";
56 $out .= "</$tag>\n";
59 // Remove all non-digits from a string.
60 function Digits($field)
62 return preg_replace("/\D/", "", $field);
65 // Translate sex.
66 function Sex($field)
68 $sex = strtoupper(substr(trim($field), 0, 1));
69 if ($sex != "M" && $sex != "F") {
70 $sex = "U";
73 return $sex;
76 // Translate a date.
77 function LWDate($field)
79 return fixDate($field);
82 // Add an insurance section.
83 function addInsurance($row, $seq)
85 if ($row["name$seq"]) {
86 OpenTag("insurance");
87 Add("priority", $seq);
88 Add("group", $row["group$seq"]);
89 Add("policy", $row["policy$seq"]);
90 Add("provider", $row["provider$seq"]);
91 Add("name", $row["name$seq"]);
92 Add("street1", $row["street1$seq"]);
93 Add("street2", $row["street2$seq"]);
94 Add("city", $row["city$seq"]);
95 Add("state", $row["state$seq"]);
96 Add("zip", $row["zip$seq"]);
97 Add("country", $row["country$seq"]);
98 Add("type", $row["instype$seq"]);
99 Add("copay", $row["copay$seq"]);
100 OpenTag("subscriber");
101 Add("relationship", $row["relationship$seq"]);
102 Add("lname", $row["lname$seq"]);
103 Add("fname", $row["fname$seq"]);
104 Add("mname", $row["mname$seq"]);
105 Add("street", $row["sstreet$seq"]);
106 Add("city", $row["scity$seq"]);
107 Add("state", $row["sstate$seq"]);
108 Add("zip", $row["szip$seq"]);
109 Add("country", $row["scountry$seq"]);
110 Add("dob", $row["sdob$seq"]);
111 Add("ss", $row["sss$seq"]);
112 Add("phone", $row["sphone$seq"]);
113 Add("employer", $row["semployer$seq"]);
114 Add("sex", $row["ssex$seq"]);
115 Add("employer_street", $row["semployer_street$seq"]);
116 Add("employer_city", $row["semployer_city$seq"]);
117 Add("employer_state", $row["semployer_state$seq"]);
118 Add("employer_zip", $row["semployer_zip$seq"]);
119 Add("employer_country", $row["semployer_country$seq"]);
120 CloseTag("subscriber");
121 CloseTag("insurance");
125 // This mess gets all the info for the patient.
126 //~Well, now it does...-Art
127 $insrow = array();
128 foreach (array('primary','secondary','tertiary') as $value) {
129 $insrow[] = sqlQuery("SELECT id FROM insurance_data WHERE " .
130 "pid = ? AND type = ? ORDER BY date DESC LIMIT 1", array($pid, $value));
133 $query = "SELECT " .
134 "p.*, " .
135 "i1.policy_number AS policy1, i1.group_number AS group1, i1.provider as provider1, " .
136 "i1.subscriber_fname AS fname1, i1.subscriber_mname AS mname1, i1.subscriber_lname AS lname1, " .
137 "i1.subscriber_street AS sstreet1, i1.subscriber_city AS scity1, i1.subscriber_state AS sstate1, " .
138 "i1.subscriber_postal_code AS szip1, i1.subscriber_relationship AS relationship1, " .
139 "i1.subscriber_DOB AS sdob1, i1.subscriber_ss AS sss1, i1.subscriber_phone AS sphone1, " .
140 "i1.subscriber_sex AS ssex1, i1.subscriber_country AS scountry1, " .
141 "i1.subscriber_employer AS semployer1, i1.subscriber_employer_street AS semployer_street1, " .
142 "i1.subscriber_employer_city AS semployer_city1, i1.subscriber_employer_state AS semployer_state1, " .
143 "i1.subscriber_employer_postal_code AS semployer_zip1, " .
144 "i1.subscriber_employer_country AS semployer_country1, i1.copay AS copay1, " .
145 "c1.name AS name1, c1.ins_type_code AS instype1, " .
146 "a1.line1 AS street11, a1.line2 AS street21, a1.city AS city1, a1.state AS state1, " .
147 "a1.zip AS zip1, a1.plus_four AS zip41, a1.country AS country1, " .
148 "i2.policy_number AS policy2, i2.group_number AS group2, i2.provider as provider2, " .
149 "i2.subscriber_fname AS fname2, i2.subscriber_mname AS mname2, i2.subscriber_lname AS lname2, " .
150 "i2.subscriber_postal_code AS szip2, i2.subscriber_relationship AS relationship2, " .
151 "i2.subscriber_DOB AS sdob2, i2.subscriber_ss AS sss2, i2.subscriber_phone AS sphone2, " .
152 "i2.subscriber_sex AS ssex2, i2.subscriber_country AS scountry2, " .
153 "i2.subscriber_employer AS semployer2, i2.subscriber_employer_street AS semployer_street2, " .
154 "i2.subscriber_employer_city AS semployer_city2, i2.subscriber_employer_state AS semployer_state2, " .
155 "i2.subscriber_employer_postal_code AS semployer_zip2, " .
156 "i2.subscriber_employer_country AS semployer_country2, i2.copay AS copay2, " .
157 "c2.name AS name2, c2.ins_type_code AS instype2, " .
158 "a2.line1 AS street12, a2.line2 AS street22, a2.city AS city2, a2.state AS state2, " .
159 "a2.zip AS zip2, a2.plus_four AS zip42, a2.country AS country2, " .
160 "i3.policy_number AS policy3, i3.group_number AS group3, i3.provider as provider3, " .
161 "i3.subscriber_fname AS fname3, i3.subscriber_mname AS mname3, i3.subscriber_lname AS lname3, " .
162 "i3.subscriber_postal_code AS szip3, i3.subscriber_relationship AS relationship3, " .
163 "i3.subscriber_DOB AS sdob3, i3.subscriber_ss AS sss3, i3.subscriber_phone AS sphone3, " .
164 "i3.subscriber_sex AS ssex3, i3.subscriber_country AS scountry3, " .
165 "i3.subscriber_employer AS semployer3, i3.subscriber_employer_street AS semployer_street3, " .
166 "i3.subscriber_employer_city AS semployer_city3, i3.subscriber_employer_state AS semployer_state3, " .
167 "i3.subscriber_employer_postal_code AS semployer_zip3, " .
168 "i3.subscriber_employer_country AS semployer_country3, i3.copay AS copay3, " .
169 "c3.name AS name3, c3.ins_type_code AS instype3, " .
170 "a3.line1 AS street13, a3.line2 AS street23, a3.city AS city3, a3.state AS state3, " .
171 "a3.zip AS zip3, a3.plus_four AS zip43, a3.country AS country3 " .
172 "FROM patient_data AS p " .
173 // "LEFT OUTER JOIN insurance_data AS i1 ON i1.pid = p.pid AND i1.type = 'primary' " .
174 // "LEFT OUTER JOIN insurance_data AS i2 ON i2.pid = p.pid AND i2.type = 'secondary' " .
175 // "LEFT OUTER JOIN insurance_data AS i3 ON i3.pid = p.pid AND i3.type = 'tertiary' " .
176 "LEFT OUTER JOIN insurance_data AS i1 ON i1.id = ? " .
177 "LEFT OUTER JOIN insurance_data AS i2 ON i2.id = ? " .
178 "LEFT OUTER JOIN insurance_data AS i3 ON i3.id = ? " .
180 "LEFT OUTER JOIN insurance_companies AS c1 ON c1.id = i1.provider " .
181 "LEFT OUTER JOIN insurance_companies AS c2 ON c2.id = i2.provider " .
182 "LEFT OUTER JOIN insurance_companies AS c3 ON c3.id = i3.provider " .
183 "LEFT OUTER JOIN addresses AS a1 ON a1.foreign_id = c1.id " .
184 "LEFT OUTER JOIN addresses AS a2 ON a2.foreign_id = c2.id " .
185 "LEFT OUTER JOIN addresses AS a3 ON a3.foreign_id = c3.id " .
186 "WHERE p.pid = ? LIMIT 1";
188 $row = sqlFetchArray(sqlStatement($query, array(($insrow[0]['id'] ?? null), ($insrow[1]['id'] ?? null), ($insrow[2]['id'] ?? null), $pid)));
190 $rowed = getEmployerData($pid);
192 OpenTag("patient");
194 // Patient Section.
196 Add("pid", $pid);
197 Add("pubpid", $row['pubpid']);
198 Add("lname", $row['lname']);
199 Add("fname", $row['fname']);
200 Add("mname", $row['mname']);
201 Add("title", $row['title']);
202 Add("ss", Digits($row['ss']));
203 Add("dob", LWDate($row['DOB']));
204 Add("sex", Sex($row['sex']));
205 Add("street", $row['street']);
206 Add("city", $row['city']);
207 Add("state", $row['state']);
208 Add("zip", $row['postal_code']);
209 Add("country", $row['country_code']);
210 Add("phone_home", Digits($row['phone_home']));
211 Add("phone_biz", Digits($row['phone_biz']));
212 Add("phone_contact", Digits($row['phone_contact']));
213 Add("phone_cell", Digits($row['phone_cell']));
214 Add("occupation", $row['occupation']);
215 Add("status", $row['status']);
216 Add("contact_relationship", $row['contact_relationship']);
217 Add("referrer", $row['referrer']);
218 Add("referrerID", $row['referrerID']);
219 Add("email", $row['email']);
220 Add("language", $row['language']);
221 Add("ethnoracial", $row['ethnoracial']);
222 Add("interpreter", $row['interpretter']);
223 Add("migrantseasonal", $row['migrantseasonal']);
224 Add("family_size", $row['family_size']);
225 Add("monthly_income", $row['monthly_income']);
226 Add("homeless", $row['homeless']);
227 Add("financial_review", LWDate(substr($row['financial_review'], 0, 10)));
228 Add("genericname1", $row['genericname1']);
229 Add("genericval1", $row['genericval1']);
230 Add("genericname2", $row['genericname2']);
231 Add("genericval2", $row['genericval2']);
232 Add("billing_note", $row['billing_note']);
233 Add("hipaa_mail", $row['hipaa_mail']);
234 Add("hipaa_voice", $row['hipaa_voice']);
236 // Insurance Sections.
238 addInsurance($row, '1');
239 addInsurance($row, '2');
240 addInsurance($row, '3');
242 // Primary Care Physician Section.
244 if ($row['providerID']) {
245 $query = "select id, fname, mname, lname from users where authorized = 1";
246 $query .= " AND id = ?";
247 $prow = sqlFetchArray(sqlStatement($query, array($row['providerID'])));
248 OpenTag("pcp");
249 Add("id", $prow['id']);
250 Add("lname", $prow['lname']);
251 Add("fname", $prow['fname']);
252 Add("mname", $prow['mname']);
253 CloseTag("pcp");
256 // Employer Section.
258 if (!empty($rowed['id'])) {
259 OpenTag("employer");
260 Add("name", $rowed['name']);
261 Add("street", $rowed['street']);
262 Add("zip", $rowed['postal_code']);
263 Add("city", $rowed['city']);
264 Add("state", $rowed['state']);
265 Add("country", $rowed['country']);
266 CloseTag("employer");
269 // All done.
270 CloseTag("patient");
272 // header('Content-type: text/xml');
273 // header('Content-Disposition: attachment; filename="pid' . $pid . '.xml"');
274 // echo $out;
276 <html>
277 <head>
278 <?php Header::setupHeader(); ?>
279 <title><?php echo xlt('Export Patient Demographics XML'); ?></title>
280 </head>
281 <body class="body_top">
282 <div class="container">
283 <div class="row">
284 <div class="col-12">
285 <div class="form-group"></div>
286 <div class="form-group">
287 <textarea name="export_data" class="form-control" rows="18" readonly><?php echo text($out) ?></textarea>
288 </div>
289 <div class="form-group">
290 <div class="col-12 text-right">
291 <div class="btn-group" role="group">
292 <button type="button" class="btn btn-secondary btn-cancel" onclick="dlgclose()"><?php echo xlt("Close"); ?></button>
293 </div>
294 </div>
295 </div>
296 </div>
297 </div>
298 </div>
300 </body>
301 </html>