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