quick minor path updates (#1968)
[openemr.git] / interface / main / ippf_export.php
blobf27113bb8cc8a311ce8a76e52e9747812fc7064c
1 <?php
2 /**
3 * This script creates an export file and sends it to the users's
4 * browser for download.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2008-2010 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
16 require_once("../globals.php");
17 require_once("$srcdir/acl.inc");
18 require_once("$srcdir/patient.inc");
20 use OpenEMR\Services\FacilityService;
22 if (!acl_check('admin', 'super')) {
23 die("Not authorized!");
26 $facilityService = new FacilityService();
28 //////////////////////////////////////////////////////////////////////
29 // XML Stuff //
30 //////////////////////////////////////////////////////////////////////
32 $out = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
33 $indent = 0;
35 // Add a string to output with some basic sanitizing.
36 function Add($tag, $text)
38 global $out, $indent;
39 $text = trim(str_replace(array("\r", "\n", "\t"), " ", $text));
40 $text = substr(text($text), 0, 50);
41 if (/* $text */ true) {
42 if ($text === 'NULL') {
43 $text = '';
46 for ($i = 0; $i < $indent;
47 ++$i) {
48 $out .= "\t";
51 $out .= "<$tag>$text</$tag>\n";
55 function AddIfPresent($tag, $text)
57 if (isset($text) && $text !== '') {
58 Add($tag, $text);
62 function OpenTag($tag)
64 global $out, $indent;
65 for ($i = 0; $i < $indent;
66 ++$i) {
67 $out .= "\t";
70 ++$indent;
71 $out .= "<$tag>\n";
74 function CloseTag($tag)
76 global $out, $indent;
77 --$indent;
78 for ($i = 0; $i < $indent;
79 ++$i) {
80 $out .= "\t";
83 $out .= "</$tag>\n";
86 // Remove all non-digits from a string.
87 function Digits($field)
89 return preg_replace("/\D/", "", $field);
92 // Translate sex.
93 function Sex($field)
95 /*******************************************************************
96 $sex = strtoupper(substr(trim($field), 0, 1));
97 if ($sex != "M" && $sex != "F") $sex = "U";
98 return $sex;
99 *******************************************************************/
100 return mappedOption('sex', $field);
103 // Translate a date.
104 function LWDate($field)
106 return fixDate($field);
109 function xmlTime($str, $default = '9999-12-31T23:59:59')
111 if (empty($default)) {
112 $default = '1800-01-01T00:00:00';
115 if (strlen($str) < 10 || substr($str, 0, 4) == '0000') {
116 $str = $default;
117 } else if (strlen($str) > 10) {
118 $str = substr($str, 0, 10) . 'T' . substr($str, 11);
119 } else {
120 $str .= 'T00:00:00';
123 // Per discussion with Daniel 2009-05-12, replace zero day or month with 01.
124 $str = preg_replace('/-00/', '-01', $str);
125 return $str;
128 //////////////////////////////////////////////////////////////////////
130 // Utility function to get the value for a specified key from a string
131 // whose format is key:value|key:value|...
133 function getTextListValue($string, $key)
135 $tmp = explode('|', $string);
136 foreach ($tmp as $value) {
137 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
138 if ($matches[1] == $key) {
139 return $matches[2];
144 return '';
147 // Return the mapped list item ID if there is one, else the option_id.
148 // Or return 9 if the option_id is empty (unspecified).
150 function mappedOption($list_id, $option_id, $default = '9')
152 if ($option_id === '') {
153 return $default;
156 $row = sqlQuery("SELECT mapping FROM list_options WHERE " .
157 "list_id = ? AND option_id = ? LIMIT 1", array($list_id, $option_id));
158 if (empty($row)) {
159 return $option_id; // should not happen
162 // return ($row['mapping'] === '') ? $option_id : $row['mapping'];
163 $maparr = explode(':', $row['mapping']);
164 return ($maparr[0] === '') ? $option_id : $maparr[0];
167 // Like the above but given a layout item form and field name.
168 // Or return 9 for a list whose id is empty (unspecified).
170 function mappedFieldOption($form_id, $field_id, $option_id)
172 $row = sqlQuery("SELECT list_id FROM " .
173 "layout_options WHERE " .
174 "form_id = ? AND " .
175 "field_id = ? " .
176 "LIMIT 1", array($form_id, $field_id));
177 if (empty($row)) {
178 return $option_id; // should not happen
181 $list_id = $row['list_id'];
182 if ($list_id === '') {
183 return $option_id;
186 if ($option_id === '') {
187 return '9';
190 $row = sqlQuery("SELECT mapping FROM " .
191 "list_options WHERE " .
192 "list_id = ? AND " .
193 "option_id = ? " .
194 "LIMIT 1", array($list_id, $option_id));
195 if (empty($row)) {
196 return $option_id; // should not happen
199 // return ($row['mapping'] === '') ? $option_id : $row['mapping'];
200 $maparr = explode(':', $row['mapping']);
201 return ($maparr[0] === '') ? $option_id : $maparr[0];
204 function exportEncounter($pid, $encounter, $date)
206 // Starting a new visit (encounter).
207 OpenTag('IMS_eMRUpload_Visit');
208 Add('VisitDate', xmlTime($date));
209 Add('emrVisitId', $encounter);
211 // Dump IPPF services.
212 $query = "SELECT b.code_type, b.code, b.units, b.fee, c.related_code " .
213 "FROM billing AS b, codes AS c WHERE " .
214 "b.pid = ? AND b.encounter = ? AND " .
215 "b.activity = 1 AND " .
216 "c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier ";
217 $bres = sqlStatement($query, array($pid, $encounter));
218 while ($brow = sqlFetchArray($bres)) {
219 if (!empty($brow['related_code'])) {
220 $relcodes = explode(';', $brow['related_code']);
221 foreach ($relcodes as $codestring) {
222 if ($codestring === '') {
223 continue;
226 list($codetype, $code) = explode(':', $codestring);
227 if ($codetype !== 'IPPF') {
228 continue;
231 // Starting a new service (IPPF code).
232 OpenTag('IMS_eMRUpload_Service');
233 Add('IppfServiceProductId', $code);
234 Add('Type', '0'); // 0=service, 1=product, 2=diagnosis, 3=referral
235 Add('IppfQuantity', $brow['units']);
236 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
237 Add('Amount', $brow['fee']);
238 CloseTag('IMS_eMRUpload_Service');
239 } // end foreach
240 } // end if related code
241 } // end while billing row found
243 // Dump products.
244 $query = "SELECT drug_id, quantity, fee FROM drug_sales WHERE " .
245 "pid = ? AND encounter = ? " .
246 "ORDER BY drug_id, sale_id";
247 $pres = sqlStatement($query, array($pid, $encounter));
248 while ($prow = sqlFetchArray($pres)) {
249 OpenTag('IMS_eMRUpload_Service');
250 Add('IppfServiceProductId', $prow['drug_id']);
251 Add('Type', '1'); // 0=service, 1=product, 2=diagnosis, 3=referral
252 Add('IppfQuantity', $prow['quantity']);
253 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
254 Add('Amount', $prow['fee']);
255 CloseTag('IMS_eMRUpload_Service');
256 } // end while drug_sales row found
258 // Dump diagnoses.
259 $query = "SELECT code FROM billing WHERE " .
260 "pid = ? AND encounter = ? AND " .
261 "code_type = 'ICD9' AND activity = 1 ORDER BY code, id";
262 $dres = sqlStatement($query, array($pid, $encounter));
263 while ($drow = sqlFetchArray($dres)) {
264 OpenTag('IMS_eMRUpload_Service');
265 Add('IppfServiceProductId', $drow['code']);
266 Add('Type', '2'); // 0=service, 1=product, 2=diagnosis, 3=referral
267 Add('IppfQuantity', '1');
268 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
269 Add('Amount', '0');
270 CloseTag('IMS_eMRUpload_Service');
271 } // end while billing row found
273 // Export referrals. Match by date. Export code type 3 and
274 // the Requested Service which should be an IPPF code.
275 $query = "SELECT refer_related_code FROM transactions WHERE " .
276 "pid = ? AND refer_date = ? AND " .
277 "refer_related_code != '' " .
278 "ORDER BY id";
279 $tres = sqlStatement($query, array($pid, $date));
280 while ($trow = sqlFetchArray($tres)) {
281 $relcodes = explode(';', $trow['refer_related_code']);
282 foreach ($relcodes as $codestring) {
283 if ($codestring === '') {
284 continue;
287 list($codetype, $code) = explode(':', $codestring);
288 if ($codetype == 'REF') {
289 // This is the expected case; a direct IPPF code is obsolete.
290 $rrow = sqlQuery("SELECT related_code FROM codes WHERE " .
291 "code_type = '16' AND code = ? AND active = 1 " .
292 "ORDER BY id LIMIT 1", array($code));
293 if (!empty($rrow['related_code'])) {
294 list($codetype, $code) = explode(':', $rrow['related_code']);
298 if ($codetype !== 'IPPF') {
299 continue;
302 OpenTag('IMS_eMRUpload_Service');
303 Add('IppfServiceProductId', $code);
304 Add('Type', '3'); // 0=service, 1=product, 2=diagnosis, 3=referral
305 Add('IppfQuantity', '1');
306 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
307 Add('Amount', '0');
308 CloseTag('IMS_eMRUpload_Service');
309 } // end foreach
310 } // end referral
312 CloseTag('IMS_eMRUpload_Visit');
315 function endClient($pid, &$encarray)
317 // Output issues.
318 $ires = sqlStatement("SELECT " .
319 "l.id, l.type, l.begdate, l.enddate, l.title, l.diagnosis, " .
320 "c.prev_method, c.new_method, c.reason_chg, c.reason_term, " .
321 "c.hor_history, c.hor_lmp, c.hor_flow, c.hor_bleeding, c.hor_contra, " .
322 "c.iud_history, c.iud_lmp, c.iud_pain, c.iud_upos, c.iud_contra, " .
323 "c.sur_screen, c.sur_anes, c.sur_type, c.sur_post_ins, c.sur_contra, " .
324 "c.nat_reason, c.nat_method, c.emg_reason, c.emg_method, " .
325 "g.client_status, g.in_ab_proc, g.ab_types, g.ab_location, g.pr_status, " .
326 "g.gest_age_by, g.sti, g.prep_procs, g.reason, g.exp_p_i, g.ab_contraind, " .
327 "g.screening, g.pre_op, g.anesthesia, g.side_eff, g.rec_compl, g.post_op, " .
328 "g.qc_ind, g.contrameth, g.fol_compl " .
329 "FROM lists AS l " .
330 "LEFT JOIN lists_ippf_con AS c ON l.type = 'contraceptive' AND c.id = l.id " .
331 "LEFT JOIN lists_ippf_gcac AS g ON l.type = 'ippf_gcac' AND g.id = l.id " .
332 "WHERE l.pid = ? " .
333 "ORDER BY l.begdate", array($pid));
335 while ($irow = sqlFetchArray($ires)) {
336 OpenTag('IMS_eMRUpload_Issue');
337 Add('IssueType', substr($irow['type'], 0, 15)); // per email 2009-03-20
338 Add('emrIssueId', $irow['id']);
339 Add('IssueStartDate', xmlTime($irow['begdate'], 0));
340 Add('IssueEndDate', xmlTime($irow['enddate']));
341 Add('IssueTitle', $irow['title']);
342 Add('IssueDiagnosis', $irow['diagnosis']);
343 $form_id = ($irow['type'] == 'ippf_gcac') ? 'GCA' : 'CON';
344 foreach ($irow as $key => $value) {
345 if (empty($value)) {
346 continue;
349 if ($key == 'id' || $key == 'type' || $key == 'begdate' ||
350 $key == 'enddate' || $key == 'title' || $key == 'diagnosis') {
351 continue;
354 $avalues = explode('|', $value);
355 foreach ($avalues as $tmp) {
356 OpenTag('IMS_eMRUpload_IssueData');
357 // TBD: Add IssueCodeGroup to identify the list, if any???
358 Add('IssueCodeGroup', '?');
359 Add('IssueCode', $key);
360 Add('IssueCodeValue', mappedFieldOption($form_id, $key, $tmp));
361 CloseTag('IMS_eMRUpload_IssueData');
365 // List the encounters linked to this issue. We include pid
366 // to speed up the search, as it begins the primary key.
367 $ieres = sqlStatement("SELECT encounter FROM issue_encounter " .
368 "WHERE pid = ? AND list_id = ? " .
369 "ORDER BY encounter", array($pid, $irow['id']));
370 while ($ierow = sqlFetchArray($ieres)) {
371 OpenTag('IMS_eMRUpload_VisitIssue');
372 Add('emrIssueId', $irow['id']);
373 Add('emrVisitId', $ierow['encounter']);
374 CloseTag('IMS_eMRUpload_VisitIssue');
377 CloseTag('IMS_eMRUpload_Issue');
380 // Loop on $encarray and generate an "issue" for each GCAC visit form,
381 // similarly to the above.
382 foreach ($encarray as $erow) {
383 $fres = sqlStatement("SELECT form_id FROM forms WHERE " .
384 "pid = ? AND " .
385 "encounter = ? AND " .
386 "formdir = 'LBFgcac' AND " .
387 "deleted = 0 " .
388 "ORDER BY id", array($pid, $erow['encounter']));
389 // For each GCAC form in this encounter...
390 while ($frow = sqlFetchArray($fres)) {
391 $form_id = $frow['form_id'];
392 OpenTag('IMS_eMRUpload_Issue');
393 Add('IssueType', 'ippf_gcac');
394 Add('emrIssueId', 10000000 + $form_id);
395 Add('IssueStartDate', xmlTime($erow['date'], 0));
396 Add('IssueEndDate', xmlTime(''));
397 Add('IssueTitle', 'GCAC Visit Form');
398 Add('IssueDiagnosis', '');
399 $gres = sqlStatement("SELECT field_id, field_value FROM lbf_data WHERE " .
400 "form_id = ? ORDER BY field_id", array($form_id));
401 // For each data item in the form...
402 while ($grow = sqlFetchArray($gres)) {
403 $key = $grow['field_id'];
404 $value = $grow['field_value'];
405 if (empty($value)) {
406 continue;
409 $avalues = explode('|', $value);
410 foreach ($avalues as $tmp) {
411 OpenTag('IMS_eMRUpload_IssueData');
412 Add('IssueCodeGroup', '?');
413 Add('IssueCode', $key);
414 Add('IssueCodeValue', mappedFieldOption('LBFgcac', $key, $tmp));
415 CloseTag('IMS_eMRUpload_IssueData');
419 OpenTag('IMS_eMRUpload_VisitIssue');
420 Add('emrIssueId', 10000000 + $form_id);
421 Add('emrVisitId', $erow['encounter']);
422 CloseTag('IMS_eMRUpload_VisitIssue');
423 CloseTag('IMS_eMRUpload_Issue');
427 CloseTag('IMS_eMRUpload_Client');
430 function endFacility()
432 global $beg_year, $beg_month;
433 OpenTag('IMS_eMRUpload_Version');
434 Add('XMLversionNumber', '1');
435 Add('Period', sprintf('%04u-%02u-01T00:00:00', $beg_year, $beg_month));
436 CloseTag('IMS_eMRUpload_Version');
437 CloseTag('IMS_eMRUpload_Point');
440 if (!empty($form_submit)) {
441 if (!verifyCsrfToken($_POST["csrf_token_form"])) {
442 csrfNotVerified();
445 $beg_year = $_POST['form_year'];
446 $beg_month = $_POST['form_month'];
447 $end_year = $beg_year;
448 $end_month = $beg_month + 1;
449 if ($end_month > 12) {
450 $end_month = 1;
451 ++$end_year;
454 /*******************************************************************
455 $query = "SELECT " .
456 "fe.facility_id, fe.pid, fe.encounter, fe.date, " .
457 "f.name, f.street, f.city, f.state, f.postal_code, f.country_code, " .
458 "f.federal_ein, " .
459 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, " .
460 "p.userlist2 AS education " .
461 "FROM form_encounter AS fe " .
462 "LEFT OUTER JOIN facility AS f ON f.id = fe.facility_id " .
463 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
464 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
465 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
466 "ORDER BY fe.facility_id, fe.pid, fe.encounter";
468 $query = "SELECT DISTINCT " .
469 "fe.facility_id, fe.pid, " .
470 "f.name, f.street, f.city, f.state, f.postal_code, f.country_code, " .
471 "f.federal_ein, " .
472 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, " .
473 "p.userlist2 AS education " .
474 "FROM form_encounter AS fe " .
475 "LEFT OUTER JOIN facility AS f ON f.id = fe.facility_id " .
476 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
477 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
478 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
479 "ORDER BY fe.facility_id, fe.pid";
480 *******************************************************************/
482 // $last_pid = -1;
483 // $last_facility = -1;
485 // Dump info for the main facility.
486 $facrow = $facilityService->getPrimaryBillingLocation();
487 OpenTag('IMS_eMRUpload_Point');
488 Add('ServiceDeliveryPointName', $facrow['name']);
489 // Add('EmrServiceDeliveryPointId', $facrow['id']);
490 Add('EmrServiceDeliveryPointId', $facrow['facility_npi']);
491 Add('Channel', '01');
492 Add('Latitude', '222222'); // TBD: Add this to facility attributes
493 Add('Longitude', '433333'); // TBD: Add this to facility attributes
494 Add('Address', $facrow['street']);
495 Add('Address2', '');
496 Add('City', $facrow['city']);
497 Add('PostCode', $facrow['postal_code']);
499 $query = "SELECT DISTINCT " .
500 "fe.pid, " .
501 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, p.sex, " .
502 "p.city, p.state, p.occupation, p.status, p.ethnoracial, " .
503 "p.interpretter, p.monthly_income, p.referral_source, p.pricelevel, " .
504 "p.userlist1, p.userlist3, p.userlist4, p.userlist5, " .
505 "p.usertext11, p.usertext12, p.usertext13, p.usertext14, p.usertext15, " .
506 "p.usertext16, p.usertext17, p.usertext18, p.usertext19, p.usertext20, " .
507 "p.userlist2 AS education " .
508 "FROM form_encounter AS fe " .
509 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
510 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", add_escape_custom($beg_year), add_escape_custom($beg_month)) .
511 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", add_escape_custom($end_year), add_escape_custom($end_month)) .
512 "ORDER BY fe.pid";
513 $res = sqlStatement($query);
515 while ($row = sqlFetchArray($res)) {
516 /*****************************************************************
517 if ($row['facility_id'] != $last_facility) {
518 if ($last_facility >= 0) {
519 endFacility();
521 $last_facility = $row['facility_id'];
522 // Starting a new facility.
523 OpenTag('IMS_eMRUpload_Point');
524 Add('ServiceDeliveryPointName' , $row['name']);
525 Add('EmrServiceDeliveryPointId', $row['facility_id']);
526 // Add('EntityId' , $row['federal_ein']);
527 Add('Channel' , '01');
528 Add('Latitude' , '222222'); // TBD: Add this to facility attributes
529 Add('Longitude' , '433333'); // TBD: Add this to facility attributes
530 Add('Address' , $row['street']);
531 Add('Address2' , '');
532 Add('City' , $row['city']);
533 Add('PostCode' , $row['postal_code']);
535 *****************************************************************/
537 $last_pid = $row['pid'];
539 /*****************************************************************
540 // Compute education: 0 = none, 1 = some, 9 = unassigned.
541 // The MAs should be told to use "none" for no education.
542 $education = 9;
543 if (!empty($row['education'])) {
544 if (preg_match('/^il/i', $row['education']) ||
545 preg_match('/^no/i', $row['education']))
546 $education = 0;
547 else
548 $education = 1;
550 *****************************************************************/
551 $education = mappedOption('userlist2', $row['education']);
553 // Get most recent contraceptive issue.
554 $crow = sqlQuery("SELECT l.begdate, c.new_method " .
555 "FROM lists AS l, lists_ippf_con AS c WHERE " .
556 "l.pid = ? AND c.id = l.id " .
557 "ORDER BY l.begdate DESC LIMIT 1", array($last_pid));
559 // Get obstetric and abortion data from most recent static history.
560 $hrow = sqlQuery("SELECT date, " .
561 "usertext16 AS genobshist, " .
562 "usertext17 AS genabohist " .
563 "FROM history_data WHERE pid = ? " .
564 "ORDER BY date DESC LIMIT 1", array($last_pid));
566 // Starting a new client (patient).
567 OpenTag('IMS_eMRUpload_Client');
568 Add('emrClientId', $row['pid']);
569 Add('RegisteredOn', xmlTime($row['regdate']));
570 Add('LastUpdated', xmlTime($row['last_update']));
571 Add('NewAcceptorDate', xmlTime($row['contrastart']));
573 // Get the current contraceptive method with greatest effectiveness.
574 $methodid = '';
575 $methodvalue = -999;
576 if (!empty($crow['new_method'])) {
577 $methods = explode('|', $crow['new_method']);
578 /***************************************************************
579 foreach ($methods as $method) {
580 $lorow = sqlQuery("SELECT option_value FROM list_options WHERE " .
581 "list_id = 'contrameth' AND option_id = '$method' LIMIT 1");
582 $value = empty($lorow) ? 0 : (0 + $lorow['option_value']);
583 if ($value > $methodvalue) {
584 $methodid = $method;
585 $methodvalue = $value;
588 ***************************************************************/
589 $methodid = mappedOption('contrameth', $methods[0]);
592 Add('CurrentMethod', $methodid);
594 Add('Dob', xmlTime($row['DOB']));
595 Add('DobType', "rel"); // rel=real, est=estimated
596 Add('Pregnancies', 0 + getTextListValue($hrow['genobshist'], 'npreg')); // number of pregnancies
597 Add('Children', 0 + getTextListValue($hrow['genobshist'], 'nlc')); // number of living children
598 Add('Abortions', 0 + getTextListValue($hrow['genabohist'], 'nia')); // number of induced abortions
599 Add('Education', $education);
600 Add('Demo5', Sex($row['sex']));
602 // Things included if they are present (July 2010)
603 AddIfPresent('City', $row['city']);
604 AddIfPresent('State', mappedOption('state', $row['state'], ''));
605 AddIfPresent('Occupation', mappedOption('occupations', $row['occupation'], ''));
606 AddIfPresent('MaritalStatus', mappedOption('marital', $row['status'], ''));
607 AddIfPresent('Ethnoracial', mappedOption('ethrace', $row['ethnoracial'], ''));
608 AddIfPresent('Interpreter', $row['interpretter']);
609 AddIfPresent('MonthlyIncome', $row['monthly_income']);
610 AddIfPresent('ReferralSource', mappedOption('refsource', $row['referral_source'], ''));
611 AddIfPresent('PriceLevel', mappedOption('pricelevel', $row['pricelevel'], ''));
612 AddIfPresent('UserList1', mappedOption('userlist1', $row['userlist1'], ''));
613 AddIfPresent('UserList3', mappedOption('userlist3', $row['userlist3'], ''));
614 AddIfPresent('UserList4', mappedOption('userlist4', $row['userlist4'], ''));
615 AddIfPresent('UserList5', mappedOption('userlist5', $row['userlist5'], ''));
616 AddIfPresent('UserText11', $row['usertext11']);
617 AddIfPresent('UserText12', $row['usertext12']);
618 AddIfPresent('UserText13', $row['usertext13']);
619 AddIfPresent('UserText14', $row['usertext14']);
620 AddIfPresent('UserText15', $row['usertext15']);
621 AddIfPresent('UserText16', $row['usertext16']);
622 AddIfPresent('UserText17', $row['usertext17']);
623 AddIfPresent('UserText18', $row['usertext18']);
624 AddIfPresent('UserText19', $row['usertext19']);
625 AddIfPresent('UserText20', $row['usertext20']);
627 // Dump the visits for this patient.
628 $query = "SELECT " .
629 "encounter, date " .
630 "FROM form_encounter WHERE " .
631 // "pid = '$last_pid' AND facility_id = '$last_facility' " .
632 "pid = '" . add_escape_custom($last_pid) . "' ";
633 if (true) {
634 // The new logic here is to restrict to the given date range.
635 // Set the above to false if all visits are wanted.
636 $query .= "AND " .
637 sprintf("date >= '%04u-%02u-01 00:00:00' AND ", add_escape_custom($beg_year), add_escape_custom($beg_month)) .
638 sprintf("date < '%04u-%02u-01 00:00:00' ", add_escape_custom($end_year), add_escape_custom($end_month));
641 $query .= "ORDER BY encounter";
643 // Add('Debug', $query); // debugging
645 $eres = sqlStatement($query);
646 $encarray = array();
647 while ($erow = sqlFetchArray($eres)) {
648 exportEncounter($last_pid, $erow['encounter'], $erow['date']);
649 $encarray[] = $erow;
652 endClient($last_pid, $encarray);
655 // if ($last_facility >= 0) endFacility();
656 endFacility();
658 header("Pragma: public");
659 header("Expires: 0");
660 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
661 header("Content-Type: application/force-download");
662 header("Content-Length: " . strlen($out));
663 header("Content-Disposition: attachment; filename=export.xml");
664 header("Content-Description: File Transfer");
665 echo $out;
667 exit(0);
670 $months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
671 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September',
672 10 => 'October', 11 => 'November', 12 => 'December');
674 $selmonth = date('m') - 1;
675 $selyear = date('Y') + 0;
676 if ($selmonth < 1) {
677 $selmonth = 12;
678 --$selyear;
681 <html>
683 <head>
684 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
685 <title><?php echo xlt('Backup'); ?></title>
686 </head>
688 <body class="body_top">
689 <center>
690 &nbsp;<br />
691 <form method='post' action='ippf_export.php'>
692 <input type="hidden" name="csrf_token_form" value="<?php echo attr(collectCsrfToken()); ?>" />
694 <table style='width:30em'>
695 <tr>
696 <td align='center'>
697 <?php echo xlt('Month'); ?>:
698 <select name='form_month'>
699 <?php
700 foreach ($months as $key => $value) {
701 echo " <option value='" . attr($key) . "'";
702 if ($key == $selmonth) {
703 echo " selected";
706 echo ">" . xlt($value) . "</option>\n";
709 </select>
710 <input type='text' name='form_year' size='4' value='<?php echo attr($selyear); ?>' />
711 &nbsp;
712 <input type='submit' name='form_submit' value='Generate XML' />
713 </td>
714 </tr>
715 </table>
717 </form>
719 </center>
721 </body>
722 </html>