several changes to the output
[openemr.git] / interface / main / ippf_export.php
blob7fe2fa6673e4f589efe1c324a920cae000abffc0
1 <?php
2 // Copyright (C) 2008, 2009 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 // This script creates an export file and sends it to the users's
10 // browser for download.
12 require_once("../globals.php");
13 require_once("$srcdir/acl.inc");
14 require_once("$srcdir/patient.inc");
16 if (!acl_check('admin', 'super')) die("Not authorized!");
18 //////////////////////////////////////////////////////////////////////
19 // XML Stuff //
20 //////////////////////////////////////////////////////////////////////
22 $out = "";
23 $indent = 0;
25 // Add a string to output with some basic sanitizing.
26 function Add($tag, $text) {
27 global $out, $indent;
28 $text = trim(str_replace(array("\r", "\n", "\t"), " ", $text));
29 if ($text) {
30 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
31 $out .= "<$tag>$text</$tag>\n";
35 function OpenTag($tag) {
36 global $out, $indent;
37 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
38 ++$indent;
39 $out .= "<$tag>\n";
42 function CloseTag($tag) {
43 global $out, $indent;
44 --$indent;
45 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
46 $out .= "</$tag>\n";
49 // Remove all non-digits from a string.
50 function Digits($field) {
51 return preg_replace("/\D/", "", $field);
54 // Translate sex.
55 function Sex($field) {
56 $sex = strtoupper(substr(trim($field), 0, 1));
57 if ($sex != "M" && $sex != "F") $sex = "U";
58 return $sex;
61 // Translate a date.
62 function LWDate($field) {
63 return fixDate($field);
66 //////////////////////////////////////////////////////////////////////
68 // Utility function to get the value for a specified key from a string
69 // whose format is key:value|key:value|...
71 function getTextListValue($string, $key) {
72 $tmp = explode('|', $string);
73 foreach ($tmp as $value) {
74 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
75 if ($matches[1] == $key) return $matches[2];
78 return '';
81 function endClient($pid) {
82 // Output issues.
83 $ires = sqlStatement("SELECT " .
84 "l.id, l.type, l.begdate, l.enddate, l.title, l.diagnosis, " .
85 "c.prev_method, c.new_method, c.reason_chg, c.reason_term, c.risks, " .
86 "c.hor_history, c.hor_lmp, c.hor_flow, c.hor_bleeding, c.hor_contra, " .
87 "c.iud_history, c.iud_lmp, c.iud_pain, c.iud_upos, c.iud_contra, " .
88 "c.sur_screen, c.sur_anes, c.sur_type, c.sur_post_ins, c.sur_contra, " .
89 "c.nat_reason, c.nat_method, c.emg_reason, c.emg_method, " .
90 "g.client_status, g.in_ab_proc, g.ab_types, g.ab_location, g.pr_status, " .
91 "g.gest_age_by, g.sti, g.prep_procs, g.reason, g.exp_p_i, g.ab_contraind, " .
92 "g.screening, g.pre_op, g.anesthesia, g.side_eff, g.rec_compl, g.post_op, " .
93 "g.qc_ind, g.contrameth, g.fol_compl " .
94 "FROM lists AS l " .
95 "LEFT JOIN lists_ippf_con AS c ON l.type = 'contraceptive' AND c.id = l.id " .
96 "LEFT JOIN lists_ippf_gcac AS g ON l.type = 'ippf_gcac' AND g.id = l.id " .
97 "WHERE l.pid = '$pid' " .
98 "ORDER BY l.begdate");
100 while ($irow = sqlFetchArray($ires)) {
101 OpenTag('IMS_eMRUpload_Issue');
102 Add('IssueType' , $irow['type']);
103 Add('IssueStartDate', $irow['begdate']);
104 Add('IssueEndDate' , $irow['enddate']);
105 Add('IssueTitle' , $irow['title']);
106 Add('IssueDiagnosis', $irow['diagnosis']);
107 foreach ($irow AS $key => $value) {
108 if (empty($value)) continue;
109 if ($key == 'id' || $key == 'type' || $key == 'begdate' ||
110 $key == 'enddate' || $key == 'title' || $key == 'diagnosis')
111 continue;
112 $avalues = explode('|', $value);
113 foreach ($avalues as $tmp) {
114 OpenTag('IMS_eMRUpload_IssueData');
115 // TBD: Add IssueCodeGroup to identify the list, if any???
116 Add('IssueCodeGroup', '?');
117 Add('IssueCode', $key);
118 Add('IssueCodeValue', $tmp);
119 CloseTag('IMS_eMRUpload_IssueData');
122 // List the encounters linked to this issue. We include pid
123 // to speed up the search, as it begins the primary key.
124 $ieres = sqlStatement("SELECT encounter FROM issue_encounter " .
125 "WHERE pid = '$pid' AND list_id = '" . $irow['id'] . "' " .
126 "ORDER BY encounter");
127 while ($ierow = sqlFetchArray($ieres)) {
128 OpenTag('IMS_eMRUpload_VisitIssue');
129 Add('emrVisitId', $ierow['encounter']);
130 CloseTag('IMS_eMRUpload_VisitIssue');
132 CloseTag('IMS_eMRUpload_Issue');
135 CloseTag('IMS_eMRUpload_Client');
138 function endFacility() {
139 global $beg_year, $beg_month;
140 OpenTag('IMS_eMRUpload_Version');
141 Add('XMLversionNumber', '1');
142 Add('Period', sprintf('%04u-%02u-01T00:00:00', $beg_year, $beg_month));
143 CloseTag('IMS_eMRUpload_Version');
144 CloseTag('IMS_eMRUpload_Point');
147 if (!empty($form_submit)) {
149 $beg_year = $_POST['form_year'];
150 $beg_month = $_POST['form_month'];
151 $end_year = $beg_year;
152 $end_month = $beg_month + 1;
153 if ($end_month > 12) {
154 $end_month = 1;
155 ++$end_year;
158 $query = "SELECT " .
159 "fe.facility_id, fe.pid, fe.encounter, fe.date, " .
160 "f.name, f.street, f.city, f.state, f.postal_code, f.country_code, " .
161 "f.federal_ein, " .
162 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, " .
163 "p.userlist2 AS education " .
164 "FROM form_encounter AS fe " .
165 "LEFT OUTER JOIN facility AS f ON f.id = fe.facility_id " .
166 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
167 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
168 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
169 "ORDER BY fe.facility_id, fe.pid, fe.encounter";
171 $res = sqlStatement($query);
173 $last_pid = -1;
174 $last_facility = -1;
176 while ($row = sqlFetchArray($res)) {
177 if ($row['facility_id'] != $last_facility) {
178 if ($last_facility >= 0) {
179 if ($last_pid >= 0) endClient($last_pid);
180 $last_pid = -1;
181 endFacility();
183 $last_facility = $row['facility_id'];
184 // Starting a new facility.
185 OpenTag('IMS_eMRUpload_Point');
186 Add('ServiceDeliveryPointName' , $row['name']);
187 Add('EmrServiceDeliveryPointId', $row['facility_id']);
188 // Add('EntityId' , $row['federal_ein']);
189 Add('Channel' , '01');
190 Add('Latitude' , '222222'); // TBD: Add this to facility attributes
191 Add('Longitude' , '433333'); // TBD: Add this to facility attributes
192 Add('Address' , $row['street']);
193 Add('City' , $row['city']);
194 Add('PostCode' , $row['postal_code']);
197 if ($row['pid'] != $last_pid) {
198 if ($last_pid >= 0) endClient($last_pid);
199 $last_pid = $row['pid'];
201 // Get most recent contraceptive issue.
202 $crow = sqlQuery("SELECT l.begdate, c.new_method " .
203 "FROM lists AS l, lists_ippf_con AS c WHERE " .
204 "l.pid = '$last_pid' AND c.id = l.id " .
205 "ORDER BY l.begdate DESC LIMIT 1");
207 // Get obstetric and abortion data from most recent static history.
208 $hrow = sqlQuery("SELECT date, " .
209 "usertext16 AS genobshist, " .
210 "usertext17 AS genabohist " .
211 "FROM history_data WHERE pid = '$last_pid' " .
212 "ORDER BY date DESC LIMIT 1");
214 // Starting a new client (patient).
215 OpenTag('IMS_eMRUpload_Client');
216 Add('emrClientId' , $row['pid']);
217 Add('RegisteredOn' , $row['regdate']);
218 Add('LastUpdated' , $row['last_update']);
219 Add('NewAcceptorDate' , $row['contrastart']);
220 if (!empty($crow['new_method'])) {
221 $methods = explode('|', $crow['new_method']);
222 foreach ($methods as $method) {
223 Add('CurrentMethod', $method);
226 Add('Dob' , $row['DOB']);
227 // Add('DobType' , "TBD"); // TBD
228 Add('Pregnancies', 0 + getTextListValue($hrow['genobshist'],'npreg')); // number of pregnancies
229 Add('Children' , 0 + getTextListValue($hrow['genobshist'],'nlc')); // number of living children
230 Add('Abortions' , 0 + getTextListValue($hrow['genabohist'],'nia')); // number of induced abortions
231 Add('Education' , $row['education']);
234 // Starting a new visit (encounter).
235 OpenTag('IMS_eMRUpload_Visit');
236 Add('VisitDate' , $row['date']);
237 Add('emrVisitId', $row['encounter']);
239 $query = "SELECT b.code_type, b.code, b.units, b.fee, c.related_code " .
240 "FROM billing AS b, codes AS c WHERE " .
241 "b.pid = '$last_pid' AND b.encounter = '" . $row['encounter'] . "' AND " .
242 "c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier ";
244 $bres = sqlStatement($query);
245 while ($brow = sqlFetchArray($bres)) {
246 if (!empty($brow['related_code'])) {
247 $relcodes = explode(';', $brow['related_code']);
248 foreach ($relcodes as $codestring) {
249 if ($codestring === '') continue;
250 list($codetype, $code) = explode(':', $codestring);
251 if ($codetype !== 'IPPF') continue;
252 // Starting a new service (IPPF code).
253 OpenTag('IMS_eMRUpload_Service');
254 Add('Type' , $codetype);
255 Add('IppfServiceProductId', $code);
256 Add('IppfQuantity' , $brow['units']);
257 Add('CurrID' , "TBD"); // TBD: Currency e.g. USD
258 Add('Amount' , $brow['fee']);
259 CloseTag('IMS_eMRUpload_Service');
260 } // end foreach
261 } // end if related code
262 } // end while billing row found
264 CloseTag('IMS_eMRUpload_Visit');
267 if ($last_pid >= 0) endClient($last_pid);
268 if ($last_facility >= 0) endFacility();
270 header("Pragma: public");
271 header("Expires: 0");
272 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
273 header("Content-Type: application/force-download");
274 header("Content-Length: " . strlen($out));
275 header("Content-Disposition: attachment; filename=export.xml");
276 header("Content-Description: File Transfer");
277 echo $out;
279 exit(0);
282 $months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
283 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September',
284 10 => 'October', 11 => 'November', 12 => 'December');
286 $selmonth = date('m') - 1;
287 $selyear = date('Y') + 0;
288 if ($selmonth < 1) {
289 $selmonth = 12;
290 --$selyear;
293 <html>
295 <head>
296 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
297 <title><?php xl('Backup','e'); ?></title>
298 </head>
300 <body class="body_top">
301 <center>
302 &nbsp;<br />
303 <form method='post' action='ippf_export.php'>
305 <table style='width:30em'>
306 <tr>
307 <td align='center'>
308 <?php echo xl('Month'); ?>:
309 <select name='form_month'>
310 <?php
311 foreach ($months as $key => $value) {
312 echo " <option value='$key'";
313 if ($key == $selmonth) echo " selected";
314 echo ">" . xl($value) . "</option>\n";
317 </select>
318 <input type='text' name='form_year' size='4' value='<?php echo $selyear; ?>' />
319 &nbsp;
320 <input type='submit' name='form_submit' value='Generate XML' />
321 </td>
322 </tr>
323 </table>
325 </form>
327 </center>
329 </body>
330 </html>