Fix in acl.inc library to fully support phpmyadmin (some scope issues needed to be...
[openemr.git] / interface / main / ippf_export.php
blob836744a3aaac06b2c2a590f35dd18806fb0691d3
1 <?php
2 // Copyright (C) 2008-2010 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 = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
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 $text = substr(htmlspecialchars($text, ENT_NOQUOTES), 0, 50);
30 if (/* $text */ true) {
31 if ($text === 'NULL') $text = '';
32 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
33 $out .= "<$tag>$text</$tag>\n";
37 function AddIfPresent($tag, $text) {
38 if (isset($text) && $text !== '') Add($tag, $text);
41 function OpenTag($tag) {
42 global $out, $indent;
43 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
44 ++$indent;
45 $out .= "<$tag>\n";
48 function CloseTag($tag) {
49 global $out, $indent;
50 --$indent;
51 for ($i = 0; $i < $indent; ++$i) $out .= "\t";
52 $out .= "</$tag>\n";
55 // Remove all non-digits from a string.
56 function Digits($field) {
57 return preg_replace("/\D/", "", $field);
60 // Translate sex.
61 function Sex($field) {
62 /*******************************************************************
63 $sex = strtoupper(substr(trim($field), 0, 1));
64 if ($sex != "M" && $sex != "F") $sex = "U";
65 return $sex;
66 *******************************************************************/
67 return mappedOption('sex', $field);
70 // Translate a date.
71 function LWDate($field) {
72 return fixDate($field);
75 function xmlTime($str, $default='9999-12-31T23:59:59') {
76 if (empty($default)) $default = '1800-01-01T00:00:00';
77 if (strlen($str) < 10 || substr($str, 0, 4) == '0000')
78 $str = $default;
79 else if (strlen($str) > 10)
80 $str = substr($str, 0, 10) . 'T' . substr($str, 11);
81 else
82 $str .= 'T00:00:00';
83 // Per discussion with Daniel 2009-05-12, replace zero day or month with 01.
84 $str = preg_replace('/-00/', '-01', $str);
85 return $str;
88 //////////////////////////////////////////////////////////////////////
90 // Utility function to get the value for a specified key from a string
91 // whose format is key:value|key:value|...
93 function getTextListValue($string, $key) {
94 $tmp = explode('|', $string);
95 foreach ($tmp as $value) {
96 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
97 if ($matches[1] == $key) return $matches[2];
100 return '';
103 // Return the mapped list item ID if there is one, else the option_id.
104 // Or return 9 if the option_id is empty (unspecified).
106 function mappedOption($list_id, $option_id, $default='9') {
107 if ($option_id === '') return $default;
108 $row = sqlQuery("SELECT mapping FROM list_options WHERE " .
109 "list_id = '$list_id' AND option_id = '$option_id' LIMIT 1");
110 if (empty($row)) return $option_id; // should not happen
111 // return ($row['mapping'] === '') ? $option_id : $row['mapping'];
112 $maparr = explode(':', $row['mapping']);
113 return ($maparr[0] === '') ? $option_id : $maparr[0];
116 // Like the above but given a layout item form and field name.
117 // Or return 9 for a list whose id is empty (unspecified).
119 function mappedFieldOption($form_id, $field_id, $option_id) {
120 $row = sqlQuery("SELECT list_id FROM " .
121 "layout_options WHERE " .
122 "form_id = '$form_id' AND " .
123 "field_id = '$field_id' " .
124 "LIMIT 1");
125 if (empty($row)) return $option_id; // should not happen
126 $list_id = $row['list_id'];
127 if ($list_id === '') return $option_id;
128 if ($option_id === '') return '9';
129 $row = sqlQuery("SELECT mapping FROM " .
130 "list_options WHERE " .
131 "list_id = '$list_id' AND " .
132 "option_id = '$option_id' " .
133 "LIMIT 1");
134 if (empty($row)) return $option_id; // should not happen
135 // return ($row['mapping'] === '') ? $option_id : $row['mapping'];
136 $maparr = explode(':', $row['mapping']);
137 return ($maparr[0] === '') ? $option_id : $maparr[0];
140 function exportEncounter($pid, $encounter, $date) {
141 // Starting a new visit (encounter).
142 OpenTag('IMS_eMRUpload_Visit');
143 Add('VisitDate' , xmlTime($date));
144 Add('emrVisitId', $encounter);
146 // Dump IPPF services.
147 $query = "SELECT b.code_type, b.code, b.units, b.fee, c.related_code " .
148 "FROM billing AS b, codes AS c WHERE " .
149 "b.pid = '$pid' AND b.encounter = '$encounter' AND " .
150 "b.activity = 1 AND " .
151 "c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier ";
152 $bres = sqlStatement($query);
153 while ($brow = sqlFetchArray($bres)) {
154 if (!empty($brow['related_code'])) {
155 $relcodes = explode(';', $brow['related_code']);
156 foreach ($relcodes as $codestring) {
157 if ($codestring === '') continue;
158 list($codetype, $code) = explode(':', $codestring);
159 if ($codetype !== 'IPPF') continue;
160 // Starting a new service (IPPF code).
161 OpenTag('IMS_eMRUpload_Service');
162 Add('IppfServiceProductId', $code);
163 Add('Type' , '0'); // 0=service, 1=product, 2=diagnosis, 3=referral
164 Add('IppfQuantity' , $brow['units']);
165 Add('CurrID' , "TBD"); // TBD: Currency e.g. USD
166 Add('Amount' , $brow['fee']);
167 CloseTag('IMS_eMRUpload_Service');
168 } // end foreach
169 } // end if related code
170 } // end while billing row found
172 // Dump products.
173 $query = "SELECT drug_id, quantity, fee FROM drug_sales WHERE " .
174 "pid = '$pid' AND encounter = '$encounter' " .
175 "ORDER BY drug_id, sale_id";
176 $pres = sqlStatement($query);
177 while ($prow = sqlFetchArray($pres)) {
178 OpenTag('IMS_eMRUpload_Service');
179 Add('IppfServiceProductId', $prow['drug_id']);
180 Add('Type' , '1'); // 0=service, 1=product, 2=diagnosis, 3=referral
181 Add('IppfQuantity' , $prow['quantity']);
182 Add('CurrID' , "TBD"); // TBD: Currency e.g. USD
183 Add('Amount' , $prow['fee']);
184 CloseTag('IMS_eMRUpload_Service');
185 } // end while drug_sales row found
187 // Dump diagnoses.
188 $query = "SELECT code FROM billing WHERE " .
189 "pid = '$pid' AND encounter = '$encounter' AND " .
190 "code_type = 'ICD9' AND activity = 1 ORDER BY code, id";
191 $dres = sqlStatement($query);
192 while ($drow = sqlFetchArray($dres)) {
193 OpenTag('IMS_eMRUpload_Service');
194 Add('IppfServiceProductId', $drow['code']);
195 Add('Type' , '2'); // 0=service, 1=product, 2=diagnosis, 3=referral
196 Add('IppfQuantity' , '1');
197 Add('CurrID' , "TBD"); // TBD: Currency e.g. USD
198 Add('Amount' , '0');
199 CloseTag('IMS_eMRUpload_Service');
200 } // end while billing row found
202 // Export referrals. Match by date. Export code type 3 and
203 // the Requested Service which should be an IPPF code.
204 $query = "SELECT refer_related_code FROM transactions WHERE " .
205 "pid = '$pid' AND refer_date = '$date' AND " .
206 "refer_related_code != '' " .
207 "ORDER BY id";
208 $tres = sqlStatement($query);
209 while ($trow = sqlFetchArray($tres)) {
210 $relcodes = explode(';', $trow['refer_related_code']);
211 foreach ($relcodes as $codestring) {
212 if ($codestring === '') continue;
213 list($codetype, $code) = explode(':', $codestring);
214 if ($codetype == 'REF') {
215 // This is the expected case; a direct IPPF code is obsolete.
216 $rrow = sqlQuery("SELECT related_code FROM codes WHERE " .
217 "code_type = '16' AND code = '$code' AND active = 1 " .
218 "ORDER BY id LIMIT 1");
219 if (!empty($rrow['related_code'])) {
220 list($codetype, $code) = explode(':', $rrow['related_code']);
223 if ($codetype !== 'IPPF') continue;
224 OpenTag('IMS_eMRUpload_Service');
225 Add('IppfServiceProductId', $code);
226 Add('Type' , '3'); // 0=service, 1=product, 2=diagnosis, 3=referral
227 Add('IppfQuantity' , '1');
228 Add('CurrID' , "TBD"); // TBD: Currency e.g. USD
229 Add('Amount' , '0');
230 CloseTag('IMS_eMRUpload_Service');
231 } // end foreach
232 } // end referral
234 CloseTag('IMS_eMRUpload_Visit');
237 function endClient($pid, &$encarray) {
238 // Output issues.
239 $ires = sqlStatement("SELECT " .
240 "l.id, l.type, l.begdate, l.enddate, l.title, l.diagnosis, " .
241 "c.prev_method, c.new_method, c.reason_chg, c.reason_term, " .
242 "c.hor_history, c.hor_lmp, c.hor_flow, c.hor_bleeding, c.hor_contra, " .
243 "c.iud_history, c.iud_lmp, c.iud_pain, c.iud_upos, c.iud_contra, " .
244 "c.sur_screen, c.sur_anes, c.sur_type, c.sur_post_ins, c.sur_contra, " .
245 "c.nat_reason, c.nat_method, c.emg_reason, c.emg_method, " .
246 "g.client_status, g.in_ab_proc, g.ab_types, g.ab_location, g.pr_status, " .
247 "g.gest_age_by, g.sti, g.prep_procs, g.reason, g.exp_p_i, g.ab_contraind, " .
248 "g.screening, g.pre_op, g.anesthesia, g.side_eff, g.rec_compl, g.post_op, " .
249 "g.qc_ind, g.contrameth, g.fol_compl " .
250 "FROM lists AS l " .
251 "LEFT JOIN lists_ippf_con AS c ON l.type = 'contraceptive' AND c.id = l.id " .
252 "LEFT JOIN lists_ippf_gcac AS g ON l.type = 'ippf_gcac' AND g.id = l.id " .
253 "WHERE l.pid = '$pid' " .
254 "ORDER BY l.begdate");
256 while ($irow = sqlFetchArray($ires)) {
257 OpenTag('IMS_eMRUpload_Issue');
258 Add('IssueType' , substr($irow['type'], 0, 15)); // per email 2009-03-20
259 Add('emrIssueId' , $irow['id']);
260 Add('IssueStartDate', xmlTime($irow['begdate'], 0));
261 Add('IssueEndDate' , xmlTime($irow['enddate']));
262 Add('IssueTitle' , $irow['title']);
263 Add('IssueDiagnosis', $irow['diagnosis']);
264 $form_id = ($irow['type'] == 'ippf_gcac') ? 'GCA' : 'CON';
265 foreach ($irow AS $key => $value) {
266 if (empty($value)) continue;
267 if ($key == 'id' || $key == 'type' || $key == 'begdate' ||
268 $key == 'enddate' || $key == 'title' || $key == 'diagnosis')
269 continue;
270 $avalues = explode('|', $value);
271 foreach ($avalues as $tmp) {
272 OpenTag('IMS_eMRUpload_IssueData');
273 // TBD: Add IssueCodeGroup to identify the list, if any???
274 Add('IssueCodeGroup', '?');
275 Add('IssueCode', $key);
276 Add('IssueCodeValue', mappedFieldOption($form_id, $key, $tmp));
277 CloseTag('IMS_eMRUpload_IssueData');
280 // List the encounters linked to this issue. We include pid
281 // to speed up the search, as it begins the primary key.
282 $ieres = sqlStatement("SELECT encounter FROM issue_encounter " .
283 "WHERE pid = '$pid' AND list_id = '" . $irow['id'] . "' " .
284 "ORDER BY encounter");
285 while ($ierow = sqlFetchArray($ieres)) {
286 OpenTag('IMS_eMRUpload_VisitIssue');
287 Add('emrIssueId', $irow['id']);
288 Add('emrVisitId', $ierow['encounter']);
289 CloseTag('IMS_eMRUpload_VisitIssue');
291 CloseTag('IMS_eMRUpload_Issue');
294 // Loop on $encarray and generate an "issue" for each GCAC visit form,
295 // similarly to the above.
296 foreach ($encarray as $erow) {
297 $fres = sqlStatement("SELECT form_id FROM forms WHERE " .
298 "pid = '$pid' AND " .
299 "encounter = '" . $erow['encounter'] . "' AND " .
300 "formdir = 'LBFgcac' AND " .
301 "deleted = 0 " .
302 "ORDER BY id");
303 // For each GCAC form in this encounter...
304 while ($frow = sqlFetchArray($fres)) {
305 $form_id = $frow['form_id'];
306 OpenTag('IMS_eMRUpload_Issue');
307 Add('IssueType' , 'ippf_gcac');
308 Add('emrIssueId' , 10000000 + $form_id);
309 Add('IssueStartDate', xmlTime($erow['date'], 0));
310 Add('IssueEndDate' , xmlTime(''));
311 Add('IssueTitle' , 'GCAC Visit Form');
312 Add('IssueDiagnosis', '');
313 $gres = sqlStatement("SELECT field_id, field_value FROM lbf_data WHERE " .
314 "form_id = '$form_id' ORDER BY field_id");
315 // For each data item in the form...
316 while ($grow = sqlFetchArray($gres)) {
317 $key = $grow['field_id'];
318 $value = $grow['field_value'];
319 if (empty($value)) continue;
320 $avalues = explode('|', $value);
321 foreach ($avalues as $tmp) {
322 OpenTag('IMS_eMRUpload_IssueData');
323 Add('IssueCodeGroup', '?');
324 Add('IssueCode', $key);
325 Add('IssueCodeValue', mappedFieldOption('LBFgcac', $key, $tmp));
326 CloseTag('IMS_eMRUpload_IssueData');
329 OpenTag('IMS_eMRUpload_VisitIssue');
330 Add('emrIssueId', 10000000 + $form_id);
331 Add('emrVisitId', $erow['encounter']);
332 CloseTag('IMS_eMRUpload_VisitIssue');
333 CloseTag('IMS_eMRUpload_Issue');
337 CloseTag('IMS_eMRUpload_Client');
340 function endFacility() {
341 global $beg_year, $beg_month;
342 OpenTag('IMS_eMRUpload_Version');
343 Add('XMLversionNumber', '1');
344 Add('Period', sprintf('%04u-%02u-01T00:00:00', $beg_year, $beg_month));
345 CloseTag('IMS_eMRUpload_Version');
346 CloseTag('IMS_eMRUpload_Point');
349 if (!empty($form_submit)) {
351 $beg_year = $_POST['form_year'];
352 $beg_month = $_POST['form_month'];
353 $end_year = $beg_year;
354 $end_month = $beg_month + 1;
355 if ($end_month > 12) {
356 $end_month = 1;
357 ++$end_year;
360 /*******************************************************************
361 $query = "SELECT " .
362 "fe.facility_id, fe.pid, fe.encounter, fe.date, " .
363 "f.name, f.street, f.city, f.state, f.postal_code, f.country_code, " .
364 "f.federal_ein, " .
365 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, " .
366 "p.userlist2 AS education " .
367 "FROM form_encounter AS fe " .
368 "LEFT OUTER JOIN facility AS f ON f.id = fe.facility_id " .
369 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
370 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
371 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
372 "ORDER BY fe.facility_id, fe.pid, fe.encounter";
374 $query = "SELECT DISTINCT " .
375 "fe.facility_id, fe.pid, " .
376 "f.name, f.street, f.city, f.state, f.postal_code, f.country_code, " .
377 "f.federal_ein, " .
378 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, " .
379 "p.userlist2 AS education " .
380 "FROM form_encounter AS fe " .
381 "LEFT OUTER JOIN facility AS f ON f.id = fe.facility_id " .
382 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
383 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
384 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
385 "ORDER BY fe.facility_id, fe.pid";
386 *******************************************************************/
388 // $last_pid = -1;
389 // $last_facility = -1;
391 // Dump info for the main facility.
392 $facrow = sqlQuery("SELECT * FROM facility ORDER BY " .
393 "billing_location DESC, id ASC LIMIT 1");
394 OpenTag('IMS_eMRUpload_Point');
395 Add('ServiceDeliveryPointName' , $facrow['name']);
396 // Add('EmrServiceDeliveryPointId', $facrow['id']);
397 Add('EmrServiceDeliveryPointId', $facrow['facility_npi']);
398 Add('Channel' , '01');
399 Add('Latitude' , '222222'); // TBD: Add this to facility attributes
400 Add('Longitude' , '433333'); // TBD: Add this to facility attributes
401 Add('Address' , $facrow['street']);
402 Add('Address2' , '');
403 Add('City' , $facrow['city']);
404 Add('PostCode' , $facrow['postal_code']);
406 $query = "SELECT DISTINCT " .
407 "fe.pid, " .
408 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, p.sex, " .
409 "p.city, p.state, p.occupation, p.status, p.ethnoracial, " .
410 "p.interpretter, p.monthly_income, p.referral_source, p.pricelevel, " .
411 "p.userlist1, p.userlist3, p.userlist4, p.userlist5, " .
412 "p.usertext11, p.usertext12, p.usertext13, p.usertext14, p.usertext15, " .
413 "p.usertext16, p.usertext17, p.usertext18, p.usertext19, p.usertext20, " .
414 "p.userlist2 AS education " .
415 "FROM form_encounter AS fe " .
416 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
417 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
418 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
419 "ORDER BY fe.pid";
420 $res = sqlStatement($query);
422 while ($row = sqlFetchArray($res)) {
424 /*****************************************************************
425 if ($row['facility_id'] != $last_facility) {
426 if ($last_facility >= 0) {
427 endFacility();
429 $last_facility = $row['facility_id'];
430 // Starting a new facility.
431 OpenTag('IMS_eMRUpload_Point');
432 Add('ServiceDeliveryPointName' , $row['name']);
433 Add('EmrServiceDeliveryPointId', $row['facility_id']);
434 // Add('EntityId' , $row['federal_ein']);
435 Add('Channel' , '01');
436 Add('Latitude' , '222222'); // TBD: Add this to facility attributes
437 Add('Longitude' , '433333'); // TBD: Add this to facility attributes
438 Add('Address' , $row['street']);
439 Add('Address2' , '');
440 Add('City' , $row['city']);
441 Add('PostCode' , $row['postal_code']);
443 *****************************************************************/
445 $last_pid = $row['pid'];
447 /*****************************************************************
448 // Compute education: 0 = none, 1 = some, 9 = unassigned.
449 // The MAs should be told to use "none" for no education.
450 $education = 9;
451 if (!empty($row['education'])) {
452 if (preg_match('/^il/i', $row['education']) ||
453 preg_match('/^no/i', $row['education']))
454 $education = 0;
455 else
456 $education = 1;
458 *****************************************************************/
459 $education = mappedOption('userlist2', $row['education']);
461 // Get most recent contraceptive issue.
462 $crow = sqlQuery("SELECT l.begdate, c.new_method " .
463 "FROM lists AS l, lists_ippf_con AS c WHERE " .
464 "l.pid = '$last_pid' AND c.id = l.id " .
465 "ORDER BY l.begdate DESC LIMIT 1");
467 // Get obstetric and abortion data from most recent static history.
468 $hrow = sqlQuery("SELECT date, " .
469 "usertext16 AS genobshist, " .
470 "usertext17 AS genabohist " .
471 "FROM history_data WHERE pid = '$last_pid' " .
472 "ORDER BY date DESC LIMIT 1");
474 // Starting a new client (patient).
475 OpenTag('IMS_eMRUpload_Client');
476 Add('emrClientId' , $row['pid']);
477 Add('RegisteredOn' , xmlTime($row['regdate']));
478 Add('LastUpdated' , xmlTime($row['last_update']));
479 Add('NewAcceptorDate' , xmlTime($row['contrastart']));
481 // Get the current contraceptive method with greatest effectiveness.
482 $methodid = '';
483 $methodvalue = -999;
484 if (!empty($crow['new_method'])) {
485 $methods = explode('|', $crow['new_method']);
486 /***************************************************************
487 foreach ($methods as $method) {
488 $lorow = sqlQuery("SELECT option_value FROM list_options WHERE " .
489 "list_id = 'contrameth' AND option_id = '$method' LIMIT 1");
490 $value = empty($lorow) ? 0 : (0 + $lorow['option_value']);
491 if ($value > $methodvalue) {
492 $methodid = $method;
493 $methodvalue = $value;
496 ***************************************************************/
497 $methodid = mappedOption('contrameth', $methods[0]);
499 Add('CurrentMethod', $methodid);
501 Add('Dob' , xmlTime($row['DOB']));
502 Add('DobType' , "rel"); // rel=real, est=estimated
503 Add('Pregnancies', 0 + getTextListValue($hrow['genobshist'],'npreg')); // number of pregnancies
504 Add('Children' , 0 + getTextListValue($hrow['genobshist'],'nlc')); // number of living children
505 Add('Abortions' , 0 + getTextListValue($hrow['genabohist'],'nia')); // number of induced abortions
506 Add('Education' , $education);
507 Add('Demo5' , Sex($row['sex']));
509 // Things included if they are present (July 2010)
510 AddIfPresent('City', $row['city']);
511 AddIfPresent('State', mappedOption('state', $row['state'], ''));
512 AddIfPresent('Occupation', mappedOption('occupations', $row['occupation'], ''));
513 AddIfPresent('MaritalStatus', mappedOption('marital', $row['status'], ''));
514 AddIfPresent('Ethnoracial', mappedOption('ethrace', $row['ethnoracial'], ''));
515 AddIfPresent('Interpreter', $row['interpretter']);
516 AddIfPresent('MonthlyIncome', $row['monthly_income']);
517 AddIfPresent('ReferralSource', mappedOption('refsource', $row['referral_source'], ''));
518 AddIfPresent('PriceLevel', mappedOption('pricelevel', $row['pricelevel'], ''));
519 AddIfPresent('UserList1', mappedOption('userlist1', $row['userlist1'], ''));
520 AddIfPresent('UserList3', mappedOption('userlist3', $row['userlist3'], ''));
521 AddIfPresent('UserList4', mappedOption('userlist4', $row['userlist4'], ''));
522 AddIfPresent('UserList5', mappedOption('userlist5', $row['userlist5'], ''));
523 AddIfPresent('UserText11', $row['usertext11']);
524 AddIfPresent('UserText12', $row['usertext12']);
525 AddIfPresent('UserText13', $row['usertext13']);
526 AddIfPresent('UserText14', $row['usertext14']);
527 AddIfPresent('UserText15', $row['usertext15']);
528 AddIfPresent('UserText16', $row['usertext16']);
529 AddIfPresent('UserText17', $row['usertext17']);
530 AddIfPresent('UserText18', $row['usertext18']);
531 AddIfPresent('UserText19', $row['usertext19']);
532 AddIfPresent('UserText20', $row['usertext20']);
534 // Dump the visits for this patient.
535 $query = "SELECT " .
536 "encounter, date " .
537 "FROM form_encounter WHERE " .
538 // "pid = '$last_pid' AND facility_id = '$last_facility' " .
539 "pid = '$last_pid' ";
540 if (true) {
541 // The new logic here is to restrict to the given date range.
542 // Set the above to false if all visits are wanted.
543 $query .= "AND " .
544 sprintf("date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
545 sprintf("date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month);
547 $query .= "ORDER BY encounter";
549 // Add('Debug', $query); // debugging
551 $eres = sqlStatement($query);
552 $encarray = array();
553 while ($erow = sqlFetchArray($eres)) {
554 exportEncounter($last_pid, $erow['encounter'], $erow['date']);
555 $encarray[] = $erow;
558 endClient($last_pid, $encarray);
561 // if ($last_facility >= 0) endFacility();
562 endFacility();
564 header("Pragma: public");
565 header("Expires: 0");
566 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
567 header("Content-Type: application/force-download");
568 header("Content-Length: " . strlen($out));
569 header("Content-Disposition: attachment; filename=export.xml");
570 header("Content-Description: File Transfer");
571 echo $out;
573 exit(0);
576 $months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
577 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September',
578 10 => 'October', 11 => 'November', 12 => 'December');
580 $selmonth = date('m') - 1;
581 $selyear = date('Y') + 0;
582 if ($selmonth < 1) {
583 $selmonth = 12;
584 --$selyear;
587 <html>
589 <head>
590 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
591 <title><?php xl('Backup','e'); ?></title>
592 </head>
594 <body class="body_top">
595 <center>
596 &nbsp;<br />
597 <form method='post' action='ippf_export.php'>
599 <table style='width:30em'>
600 <tr>
601 <td align='center'>
602 <?php echo xl('Month'); ?>:
603 <select name='form_month'>
604 <?php
605 foreach ($months as $key => $value) {
606 echo " <option value='$key'";
607 if ($key == $selmonth) echo " selected";
608 echo ">" . xl($value) . "</option>\n";
611 </select>
612 <input type='text' name='form_year' size='4' value='<?php echo $selyear; ?>' />
613 &nbsp;
614 <input type='submit' name='form_submit' value='Generate XML' />
615 </td>
616 </tr>
617 </table>
619 </form>
621 </center>
623 </body>
624 </html>