preparing for 5.0.1 release in several weeks (#1509)
[openemr.git] / interface / main / ippf_export.php
blobf0dfa6e0e7ea9d6f0fe39a79d7bfd7e6a78833e1
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 use OpenEMR\Services\FacilityService;
18 if (!acl_check('admin', 'super')) {
19 die("Not authorized!");
22 $facilityService = new FacilityService();
24 //////////////////////////////////////////////////////////////////////
25 // XML Stuff //
26 //////////////////////////////////////////////////////////////////////
28 $out = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
29 $indent = 0;
31 // Add a string to output with some basic sanitizing.
32 function Add($tag, $text)
34 global $out, $indent;
35 $text = trim(str_replace(array("\r", "\n", "\t"), " ", $text));
36 $text = substr(htmlspecialchars($text, ENT_NOQUOTES), 0, 50);
37 if (/* $text */ true) {
38 if ($text === 'NULL') {
39 $text = '';
42 for ($i = 0; $i < $indent;
43 ++$i) {
44 $out .= "\t";
47 $out .= "<$tag>$text</$tag>\n";
51 function AddIfPresent($tag, $text)
53 if (isset($text) && $text !== '') {
54 Add($tag, $text);
58 function OpenTag($tag)
60 global $out, $indent;
61 for ($i = 0; $i < $indent;
62 ++$i) {
63 $out .= "\t";
66 ++$indent;
67 $out .= "<$tag>\n";
70 function CloseTag($tag)
72 global $out, $indent;
73 --$indent;
74 for ($i = 0; $i < $indent;
75 ++$i) {
76 $out .= "\t";
79 $out .= "</$tag>\n";
82 // Remove all non-digits from a string.
83 function Digits($field)
85 return preg_replace("/\D/", "", $field);
88 // Translate sex.
89 function Sex($field)
91 /*******************************************************************
92 $sex = strtoupper(substr(trim($field), 0, 1));
93 if ($sex != "M" && $sex != "F") $sex = "U";
94 return $sex;
95 *******************************************************************/
96 return mappedOption('sex', $field);
99 // Translate a date.
100 function LWDate($field)
102 return fixDate($field);
105 function xmlTime($str, $default = '9999-12-31T23:59:59')
107 if (empty($default)) {
108 $default = '1800-01-01T00:00:00';
111 if (strlen($str) < 10 || substr($str, 0, 4) == '0000') {
112 $str = $default;
113 } else if (strlen($str) > 10) {
114 $str = substr($str, 0, 10) . 'T' . substr($str, 11);
115 } else {
116 $str .= 'T00:00:00';
119 // Per discussion with Daniel 2009-05-12, replace zero day or month with 01.
120 $str = preg_replace('/-00/', '-01', $str);
121 return $str;
124 //////////////////////////////////////////////////////////////////////
126 // Utility function to get the value for a specified key from a string
127 // whose format is key:value|key:value|...
129 function getTextListValue($string, $key)
131 $tmp = explode('|', $string);
132 foreach ($tmp as $value) {
133 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
134 if ($matches[1] == $key) {
135 return $matches[2];
140 return '';
143 // Return the mapped list item ID if there is one, else the option_id.
144 // Or return 9 if the option_id is empty (unspecified).
146 function mappedOption($list_id, $option_id, $default = '9')
148 if ($option_id === '') {
149 return $default;
152 $row = sqlQuery("SELECT mapping FROM list_options WHERE " .
153 "list_id = '$list_id' AND option_id = '$option_id' LIMIT 1");
154 if (empty($row)) {
155 return $option_id; // should not happen
158 // return ($row['mapping'] === '') ? $option_id : $row['mapping'];
159 $maparr = explode(':', $row['mapping']);
160 return ($maparr[0] === '') ? $option_id : $maparr[0];
163 // Like the above but given a layout item form and field name.
164 // Or return 9 for a list whose id is empty (unspecified).
166 function mappedFieldOption($form_id, $field_id, $option_id)
168 $row = sqlQuery("SELECT list_id FROM " .
169 "layout_options WHERE " .
170 "form_id = '$form_id' AND " .
171 "field_id = '$field_id' " .
172 "LIMIT 1");
173 if (empty($row)) {
174 return $option_id; // should not happen
177 $list_id = $row['list_id'];
178 if ($list_id === '') {
179 return $option_id;
182 if ($option_id === '') {
183 return '9';
186 $row = sqlQuery("SELECT mapping FROM " .
187 "list_options WHERE " .
188 "list_id = '$list_id' AND " .
189 "option_id = '$option_id' " .
190 "LIMIT 1");
191 if (empty($row)) {
192 return $option_id; // should not happen
195 // return ($row['mapping'] === '') ? $option_id : $row['mapping'];
196 $maparr = explode(':', $row['mapping']);
197 return ($maparr[0] === '') ? $option_id : $maparr[0];
200 function exportEncounter($pid, $encounter, $date)
202 // Starting a new visit (encounter).
203 OpenTag('IMS_eMRUpload_Visit');
204 Add('VisitDate', xmlTime($date));
205 Add('emrVisitId', $encounter);
207 // Dump IPPF services.
208 $query = "SELECT b.code_type, b.code, b.units, b.fee, c.related_code " .
209 "FROM billing AS b, codes AS c WHERE " .
210 "b.pid = '$pid' AND b.encounter = '$encounter' AND " .
211 "b.activity = 1 AND " .
212 "c.code_type = '12' AND c.code = b.code AND c.modifier = b.modifier ";
213 $bres = sqlStatement($query);
214 while ($brow = sqlFetchArray($bres)) {
215 if (!empty($brow['related_code'])) {
216 $relcodes = explode(';', $brow['related_code']);
217 foreach ($relcodes as $codestring) {
218 if ($codestring === '') {
219 continue;
222 list($codetype, $code) = explode(':', $codestring);
223 if ($codetype !== 'IPPF') {
224 continue;
227 // Starting a new service (IPPF code).
228 OpenTag('IMS_eMRUpload_Service');
229 Add('IppfServiceProductId', $code);
230 Add('Type', '0'); // 0=service, 1=product, 2=diagnosis, 3=referral
231 Add('IppfQuantity', $brow['units']);
232 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
233 Add('Amount', $brow['fee']);
234 CloseTag('IMS_eMRUpload_Service');
235 } // end foreach
236 } // end if related code
237 } // end while billing row found
239 // Dump products.
240 $query = "SELECT drug_id, quantity, fee FROM drug_sales WHERE " .
241 "pid = '$pid' AND encounter = '$encounter' " .
242 "ORDER BY drug_id, sale_id";
243 $pres = sqlStatement($query);
244 while ($prow = sqlFetchArray($pres)) {
245 OpenTag('IMS_eMRUpload_Service');
246 Add('IppfServiceProductId', $prow['drug_id']);
247 Add('Type', '1'); // 0=service, 1=product, 2=diagnosis, 3=referral
248 Add('IppfQuantity', $prow['quantity']);
249 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
250 Add('Amount', $prow['fee']);
251 CloseTag('IMS_eMRUpload_Service');
252 } // end while drug_sales row found
254 // Dump diagnoses.
255 $query = "SELECT code FROM billing WHERE " .
256 "pid = '$pid' AND encounter = '$encounter' AND " .
257 "code_type = 'ICD9' AND activity = 1 ORDER BY code, id";
258 $dres = sqlStatement($query);
259 while ($drow = sqlFetchArray($dres)) {
260 OpenTag('IMS_eMRUpload_Service');
261 Add('IppfServiceProductId', $drow['code']);
262 Add('Type', '2'); // 0=service, 1=product, 2=diagnosis, 3=referral
263 Add('IppfQuantity', '1');
264 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
265 Add('Amount', '0');
266 CloseTag('IMS_eMRUpload_Service');
267 } // end while billing row found
269 // Export referrals. Match by date. Export code type 3 and
270 // the Requested Service which should be an IPPF code.
271 $query = "SELECT refer_related_code FROM transactions WHERE " .
272 "pid = '$pid' AND refer_date = '$date' AND " .
273 "refer_related_code != '' " .
274 "ORDER BY id";
275 $tres = sqlStatement($query);
276 while ($trow = sqlFetchArray($tres)) {
277 $relcodes = explode(';', $trow['refer_related_code']);
278 foreach ($relcodes as $codestring) {
279 if ($codestring === '') {
280 continue;
283 list($codetype, $code) = explode(':', $codestring);
284 if ($codetype == 'REF') {
285 // This is the expected case; a direct IPPF code is obsolete.
286 $rrow = sqlQuery("SELECT related_code FROM codes WHERE " .
287 "code_type = '16' AND code = '$code' AND active = 1 " .
288 "ORDER BY id LIMIT 1");
289 if (!empty($rrow['related_code'])) {
290 list($codetype, $code) = explode(':', $rrow['related_code']);
294 if ($codetype !== 'IPPF') {
295 continue;
298 OpenTag('IMS_eMRUpload_Service');
299 Add('IppfServiceProductId', $code);
300 Add('Type', '3'); // 0=service, 1=product, 2=diagnosis, 3=referral
301 Add('IppfQuantity', '1');
302 Add('CurrID', "TBD"); // TBD: Currency e.g. USD
303 Add('Amount', '0');
304 CloseTag('IMS_eMRUpload_Service');
305 } // end foreach
306 } // end referral
308 CloseTag('IMS_eMRUpload_Visit');
311 function endClient($pid, &$encarray)
313 // Output issues.
314 $ires = sqlStatement("SELECT " .
315 "l.id, l.type, l.begdate, l.enddate, l.title, l.diagnosis, " .
316 "c.prev_method, c.new_method, c.reason_chg, c.reason_term, " .
317 "c.hor_history, c.hor_lmp, c.hor_flow, c.hor_bleeding, c.hor_contra, " .
318 "c.iud_history, c.iud_lmp, c.iud_pain, c.iud_upos, c.iud_contra, " .
319 "c.sur_screen, c.sur_anes, c.sur_type, c.sur_post_ins, c.sur_contra, " .
320 "c.nat_reason, c.nat_method, c.emg_reason, c.emg_method, " .
321 "g.client_status, g.in_ab_proc, g.ab_types, g.ab_location, g.pr_status, " .
322 "g.gest_age_by, g.sti, g.prep_procs, g.reason, g.exp_p_i, g.ab_contraind, " .
323 "g.screening, g.pre_op, g.anesthesia, g.side_eff, g.rec_compl, g.post_op, " .
324 "g.qc_ind, g.contrameth, g.fol_compl " .
325 "FROM lists AS l " .
326 "LEFT JOIN lists_ippf_con AS c ON l.type = 'contraceptive' AND c.id = l.id " .
327 "LEFT JOIN lists_ippf_gcac AS g ON l.type = 'ippf_gcac' AND g.id = l.id " .
328 "WHERE l.pid = '$pid' " .
329 "ORDER BY l.begdate");
331 while ($irow = sqlFetchArray($ires)) {
332 OpenTag('IMS_eMRUpload_Issue');
333 Add('IssueType', substr($irow['type'], 0, 15)); // per email 2009-03-20
334 Add('emrIssueId', $irow['id']);
335 Add('IssueStartDate', xmlTime($irow['begdate'], 0));
336 Add('IssueEndDate', xmlTime($irow['enddate']));
337 Add('IssueTitle', $irow['title']);
338 Add('IssueDiagnosis', $irow['diagnosis']);
339 $form_id = ($irow['type'] == 'ippf_gcac') ? 'GCA' : 'CON';
340 foreach ($irow as $key => $value) {
341 if (empty($value)) {
342 continue;
345 if ($key == 'id' || $key == 'type' || $key == 'begdate' ||
346 $key == 'enddate' || $key == 'title' || $key == 'diagnosis') {
347 continue;
350 $avalues = explode('|', $value);
351 foreach ($avalues as $tmp) {
352 OpenTag('IMS_eMRUpload_IssueData');
353 // TBD: Add IssueCodeGroup to identify the list, if any???
354 Add('IssueCodeGroup', '?');
355 Add('IssueCode', $key);
356 Add('IssueCodeValue', mappedFieldOption($form_id, $key, $tmp));
357 CloseTag('IMS_eMRUpload_IssueData');
361 // List the encounters linked to this issue. We include pid
362 // to speed up the search, as it begins the primary key.
363 $ieres = sqlStatement("SELECT encounter FROM issue_encounter " .
364 "WHERE pid = '$pid' AND list_id = '" . $irow['id'] . "' " .
365 "ORDER BY encounter");
366 while ($ierow = sqlFetchArray($ieres)) {
367 OpenTag('IMS_eMRUpload_VisitIssue');
368 Add('emrIssueId', $irow['id']);
369 Add('emrVisitId', $ierow['encounter']);
370 CloseTag('IMS_eMRUpload_VisitIssue');
373 CloseTag('IMS_eMRUpload_Issue');
376 // Loop on $encarray and generate an "issue" for each GCAC visit form,
377 // similarly to the above.
378 foreach ($encarray as $erow) {
379 $fres = sqlStatement("SELECT form_id FROM forms WHERE " .
380 "pid = '$pid' AND " .
381 "encounter = '" . $erow['encounter'] . "' AND " .
382 "formdir = 'LBFgcac' AND " .
383 "deleted = 0 " .
384 "ORDER BY id");
385 // For each GCAC form in this encounter...
386 while ($frow = sqlFetchArray($fres)) {
387 $form_id = $frow['form_id'];
388 OpenTag('IMS_eMRUpload_Issue');
389 Add('IssueType', 'ippf_gcac');
390 Add('emrIssueId', 10000000 + $form_id);
391 Add('IssueStartDate', xmlTime($erow['date'], 0));
392 Add('IssueEndDate', xmlTime(''));
393 Add('IssueTitle', 'GCAC Visit Form');
394 Add('IssueDiagnosis', '');
395 $gres = sqlStatement("SELECT field_id, field_value FROM lbf_data WHERE " .
396 "form_id = '$form_id' ORDER BY field_id");
397 // For each data item in the form...
398 while ($grow = sqlFetchArray($gres)) {
399 $key = $grow['field_id'];
400 $value = $grow['field_value'];
401 if (empty($value)) {
402 continue;
405 $avalues = explode('|', $value);
406 foreach ($avalues as $tmp) {
407 OpenTag('IMS_eMRUpload_IssueData');
408 Add('IssueCodeGroup', '?');
409 Add('IssueCode', $key);
410 Add('IssueCodeValue', mappedFieldOption('LBFgcac', $key, $tmp));
411 CloseTag('IMS_eMRUpload_IssueData');
415 OpenTag('IMS_eMRUpload_VisitIssue');
416 Add('emrIssueId', 10000000 + $form_id);
417 Add('emrVisitId', $erow['encounter']);
418 CloseTag('IMS_eMRUpload_VisitIssue');
419 CloseTag('IMS_eMRUpload_Issue');
423 CloseTag('IMS_eMRUpload_Client');
426 function endFacility()
428 global $beg_year, $beg_month;
429 OpenTag('IMS_eMRUpload_Version');
430 Add('XMLversionNumber', '1');
431 Add('Period', sprintf('%04u-%02u-01T00:00:00', $beg_year, $beg_month));
432 CloseTag('IMS_eMRUpload_Version');
433 CloseTag('IMS_eMRUpload_Point');
436 if (!empty($form_submit)) {
437 $beg_year = $_POST['form_year'];
438 $beg_month = $_POST['form_month'];
439 $end_year = $beg_year;
440 $end_month = $beg_month + 1;
441 if ($end_month > 12) {
442 $end_month = 1;
443 ++$end_year;
446 /*******************************************************************
447 $query = "SELECT " .
448 "fe.facility_id, fe.pid, fe.encounter, fe.date, " .
449 "f.name, f.street, f.city, f.state, f.postal_code, f.country_code, " .
450 "f.federal_ein, " .
451 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, " .
452 "p.userlist2 AS education " .
453 "FROM form_encounter AS fe " .
454 "LEFT OUTER JOIN facility AS f ON f.id = fe.facility_id " .
455 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
456 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
457 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
458 "ORDER BY fe.facility_id, fe.pid, fe.encounter";
460 $query = "SELECT DISTINCT " .
461 "fe.facility_id, fe.pid, " .
462 "f.name, f.street, f.city, f.state, f.postal_code, f.country_code, " .
463 "f.federal_ein, " .
464 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, " .
465 "p.userlist2 AS education " .
466 "FROM form_encounter AS fe " .
467 "LEFT OUTER JOIN facility AS f ON f.id = fe.facility_id " .
468 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
469 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
470 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
471 "ORDER BY fe.facility_id, fe.pid";
472 *******************************************************************/
474 // $last_pid = -1;
475 // $last_facility = -1;
477 // Dump info for the main facility.
478 $facrow = $facilityService->getPrimaryBillingLocation();
479 OpenTag('IMS_eMRUpload_Point');
480 Add('ServiceDeliveryPointName', $facrow['name']);
481 // Add('EmrServiceDeliveryPointId', $facrow['id']);
482 Add('EmrServiceDeliveryPointId', $facrow['facility_npi']);
483 Add('Channel', '01');
484 Add('Latitude', '222222'); // TBD: Add this to facility attributes
485 Add('Longitude', '433333'); // TBD: Add this to facility attributes
486 Add('Address', $facrow['street']);
487 Add('Address2', '');
488 Add('City', $facrow['city']);
489 Add('PostCode', $facrow['postal_code']);
491 $query = "SELECT DISTINCT " .
492 "fe.pid, " .
493 "p.regdate, p.date AS last_update, p.contrastart, p.DOB, p.sex, " .
494 "p.city, p.state, p.occupation, p.status, p.ethnoracial, " .
495 "p.interpretter, p.monthly_income, p.referral_source, p.pricelevel, " .
496 "p.userlist1, p.userlist3, p.userlist4, p.userlist5, " .
497 "p.usertext11, p.usertext12, p.usertext13, p.usertext14, p.usertext15, " .
498 "p.usertext16, p.usertext17, p.usertext18, p.usertext19, p.usertext20, " .
499 "p.userlist2 AS education " .
500 "FROM form_encounter AS fe " .
501 "LEFT OUTER JOIN patient_data AS p ON p.pid = fe.pid WHERE " .
502 sprintf("fe.date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
503 sprintf("fe.date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month) .
504 "ORDER BY fe.pid";
505 $res = sqlStatement($query);
507 while ($row = sqlFetchArray($res)) {
508 /*****************************************************************
509 if ($row['facility_id'] != $last_facility) {
510 if ($last_facility >= 0) {
511 endFacility();
513 $last_facility = $row['facility_id'];
514 // Starting a new facility.
515 OpenTag('IMS_eMRUpload_Point');
516 Add('ServiceDeliveryPointName' , $row['name']);
517 Add('EmrServiceDeliveryPointId', $row['facility_id']);
518 // Add('EntityId' , $row['federal_ein']);
519 Add('Channel' , '01');
520 Add('Latitude' , '222222'); // TBD: Add this to facility attributes
521 Add('Longitude' , '433333'); // TBD: Add this to facility attributes
522 Add('Address' , $row['street']);
523 Add('Address2' , '');
524 Add('City' , $row['city']);
525 Add('PostCode' , $row['postal_code']);
527 *****************************************************************/
529 $last_pid = $row['pid'];
531 /*****************************************************************
532 // Compute education: 0 = none, 1 = some, 9 = unassigned.
533 // The MAs should be told to use "none" for no education.
534 $education = 9;
535 if (!empty($row['education'])) {
536 if (preg_match('/^il/i', $row['education']) ||
537 preg_match('/^no/i', $row['education']))
538 $education = 0;
539 else
540 $education = 1;
542 *****************************************************************/
543 $education = mappedOption('userlist2', $row['education']);
545 // Get most recent contraceptive issue.
546 $crow = sqlQuery("SELECT l.begdate, c.new_method " .
547 "FROM lists AS l, lists_ippf_con AS c WHERE " .
548 "l.pid = '$last_pid' AND c.id = l.id " .
549 "ORDER BY l.begdate DESC LIMIT 1");
551 // Get obstetric and abortion data from most recent static history.
552 $hrow = sqlQuery("SELECT date, " .
553 "usertext16 AS genobshist, " .
554 "usertext17 AS genabohist " .
555 "FROM history_data WHERE pid = '$last_pid' " .
556 "ORDER BY date DESC LIMIT 1");
558 // Starting a new client (patient).
559 OpenTag('IMS_eMRUpload_Client');
560 Add('emrClientId', $row['pid']);
561 Add('RegisteredOn', xmlTime($row['regdate']));
562 Add('LastUpdated', xmlTime($row['last_update']));
563 Add('NewAcceptorDate', xmlTime($row['contrastart']));
565 // Get the current contraceptive method with greatest effectiveness.
566 $methodid = '';
567 $methodvalue = -999;
568 if (!empty($crow['new_method'])) {
569 $methods = explode('|', $crow['new_method']);
570 /***************************************************************
571 foreach ($methods as $method) {
572 $lorow = sqlQuery("SELECT option_value FROM list_options WHERE " .
573 "list_id = 'contrameth' AND option_id = '$method' LIMIT 1");
574 $value = empty($lorow) ? 0 : (0 + $lorow['option_value']);
575 if ($value > $methodvalue) {
576 $methodid = $method;
577 $methodvalue = $value;
580 ***************************************************************/
581 $methodid = mappedOption('contrameth', $methods[0]);
584 Add('CurrentMethod', $methodid);
586 Add('Dob', xmlTime($row['DOB']));
587 Add('DobType', "rel"); // rel=real, est=estimated
588 Add('Pregnancies', 0 + getTextListValue($hrow['genobshist'], 'npreg')); // number of pregnancies
589 Add('Children', 0 + getTextListValue($hrow['genobshist'], 'nlc')); // number of living children
590 Add('Abortions', 0 + getTextListValue($hrow['genabohist'], 'nia')); // number of induced abortions
591 Add('Education', $education);
592 Add('Demo5', Sex($row['sex']));
594 // Things included if they are present (July 2010)
595 AddIfPresent('City', $row['city']);
596 AddIfPresent('State', mappedOption('state', $row['state'], ''));
597 AddIfPresent('Occupation', mappedOption('occupations', $row['occupation'], ''));
598 AddIfPresent('MaritalStatus', mappedOption('marital', $row['status'], ''));
599 AddIfPresent('Ethnoracial', mappedOption('ethrace', $row['ethnoracial'], ''));
600 AddIfPresent('Interpreter', $row['interpretter']);
601 AddIfPresent('MonthlyIncome', $row['monthly_income']);
602 AddIfPresent('ReferralSource', mappedOption('refsource', $row['referral_source'], ''));
603 AddIfPresent('PriceLevel', mappedOption('pricelevel', $row['pricelevel'], ''));
604 AddIfPresent('UserList1', mappedOption('userlist1', $row['userlist1'], ''));
605 AddIfPresent('UserList3', mappedOption('userlist3', $row['userlist3'], ''));
606 AddIfPresent('UserList4', mappedOption('userlist4', $row['userlist4'], ''));
607 AddIfPresent('UserList5', mappedOption('userlist5', $row['userlist5'], ''));
608 AddIfPresent('UserText11', $row['usertext11']);
609 AddIfPresent('UserText12', $row['usertext12']);
610 AddIfPresent('UserText13', $row['usertext13']);
611 AddIfPresent('UserText14', $row['usertext14']);
612 AddIfPresent('UserText15', $row['usertext15']);
613 AddIfPresent('UserText16', $row['usertext16']);
614 AddIfPresent('UserText17', $row['usertext17']);
615 AddIfPresent('UserText18', $row['usertext18']);
616 AddIfPresent('UserText19', $row['usertext19']);
617 AddIfPresent('UserText20', $row['usertext20']);
619 // Dump the visits for this patient.
620 $query = "SELECT " .
621 "encounter, date " .
622 "FROM form_encounter WHERE " .
623 // "pid = '$last_pid' AND facility_id = '$last_facility' " .
624 "pid = '$last_pid' ";
625 if (true) {
626 // The new logic here is to restrict to the given date range.
627 // Set the above to false if all visits are wanted.
628 $query .= "AND " .
629 sprintf("date >= '%04u-%02u-01 00:00:00' AND ", $beg_year, $beg_month) .
630 sprintf("date < '%04u-%02u-01 00:00:00' ", $end_year, $end_month);
633 $query .= "ORDER BY encounter";
635 // Add('Debug', $query); // debugging
637 $eres = sqlStatement($query);
638 $encarray = array();
639 while ($erow = sqlFetchArray($eres)) {
640 exportEncounter($last_pid, $erow['encounter'], $erow['date']);
641 $encarray[] = $erow;
644 endClient($last_pid, $encarray);
647 // if ($last_facility >= 0) endFacility();
648 endFacility();
650 header("Pragma: public");
651 header("Expires: 0");
652 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
653 header("Content-Type: application/force-download");
654 header("Content-Length: " . strlen($out));
655 header("Content-Disposition: attachment; filename=export.xml");
656 header("Content-Description: File Transfer");
657 echo $out;
659 exit(0);
662 $months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April',
663 5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September',
664 10 => 'October', 11 => 'November', 12 => 'December');
666 $selmonth = date('m') - 1;
667 $selyear = date('Y') + 0;
668 if ($selmonth < 1) {
669 $selmonth = 12;
670 --$selyear;
673 <html>
675 <head>
676 <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
677 <title><?php xl('Backup', 'e'); ?></title>
678 </head>
680 <body class="body_top">
681 <center>
682 &nbsp;<br />
683 <form method='post' action='ippf_export.php'>
685 <table style='width:30em'>
686 <tr>
687 <td align='center'>
688 <?php echo xl('Month'); ?>:
689 <select name='form_month'>
690 <?php
691 foreach ($months as $key => $value) {
692 echo " <option value='$key'";
693 if ($key == $selmonth) {
694 echo " selected";
697 echo ">" . xl($value) . "</option>\n";
700 </select>
701 <input type='text' name='form_year' size='4' value='<?php echo $selyear; ?>' />
702 &nbsp;
703 <input type='submit' name='form_submit' value='Generate XML' />
704 </td>
705 </tr>
706 </table>
708 </form>
710 </center>
712 </body>
713 </html>