preparing for 5.0.1 release in several weeks (#1509)
[openemr.git] / library / edihistory / edih_io.php
blob1c6f4af89036ffb9028ddab7ef75b55d8f4ccea2
1 <?php
2 /*
3 * edih_io.php
5 * Copyright 2016 Kevin McCormick Longview, Texas
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; version 3 or later. You should have
16 * received a copy of the GNU General Public License along with this program;
17 * if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * <http://opensource.org/licenses/gpl-license.php>
22 * @author Kevin McCormick
23 * @link: http://www.open-emr.org
24 * @package OpenEMR
25 * @subpackage ediHistory
28 /**
29 * jQuery adds a special HTTP header for ajax requests
31 * @return bool
33 function is_xhr()
35 return @ $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] === 'XMLHttpRequest';
38 /**
39 * Get some values from php ini functions for interface
41 * @return array json
43 function edih_php_inivals()
45 $ival = array();
46 $td = basename(sys_get_temp_dir());
47 $ival['maxfsize'] = ini_get('upload_max_filesize');
48 $ival['maxfuploads'] = ini_get('max_file_uploads');
49 $ival['postmaxsize'] = ini_get('post_max_size');
50 $ival['tmpdir'] = $td;
51 $json = json_encode($ival);
53 return $json;
56 /**
57 * display the log file selected
59 * @uses csv_log_html()
60 * @return string
62 function edih_disp_log()
64 $lfn = '';
65 if (isset($_GET['log_select'])) {
66 $lfn = filter_input(INPUT_GET, 'log_select', FILTER_DEFAULT);
69 $str_html = csv_log_html($lfn);
70 return $str_html;
73 function edih_disp_logfiles()
75 $str_html = '';
76 $lst = true;
77 if (isset($_GET['loglist'])) {
78 // loglist: 'yes'
79 $lval = filter_input(INPUT_GET, 'loglist', FILTER_DEFAULT);
80 $lst = ($lval == 'yes') ? true : false;
81 } elseif (isset($_GET['archivelog'])) {
82 // archivelog: 'yes'
83 $lval = filter_input(INPUT_GET, 'archivelog', FILTER_DEFAULT);
84 $lst = ($lval == 'yes') ? false : true;
85 } else {
86 csv_edihist_log('edih_disp_logfiles: input parameter error');
87 return "input parameter error<br />";
90 // returns json array
91 $str_html = csv_log_manage($lst);
92 return $str_html;
94 /**
95 * read or write simple notes to a text file
97 * @uses csv_notes_file()
98 * @return string
100 function edih_user_notes()
103 if (isset($_GET['getnotes'])) {
104 $getnt = filter_input(INPUT_GET, 'getnotes', FILTER_DEFAULT);
105 if ($getnt == 'yes') {
106 $str_html = csv_notes_file();
108 } elseif (isset($_POST['notes_hidden']) && isset($_POST['txtnotes'])) {
109 $putnt = filter_input(INPUT_POST, 'putnotes', FILTER_DEFAULT);
110 if ($putnt == 'yes') {
111 $notetext = trim($_POST['txtnotes']);
112 $filtered = filter_var($notetext, FILTER_DEFAULT);
113 //echo $filtered .PHP_EOL;
114 $str_html = csv_notes_file($filtered, false);
116 } else {
117 csv_edihist_log('edih_user_notes: invalid values in request');
118 $str_html = "<p>User Notes: invalid values in request.</p>";
121 return $str_html;
125 * generate the heading string for an html page
127 * @return string html heading stanza
129 function edih_html_heading($option, $title = '')
132 //if (!is_string($title)) { $title=''; }
133 $title = (is_string($title)) ? $title : '';
134 //$srcdir = $GLOBALS['srcdir'];
135 $webdir = $GLOBALS['webroot'];
136 $vendordir = $GLOBALS['assets_static_relative'];
138 $str_html = "<!DOCTYPE html>".PHP_EOL."<html>".PHP_EOL."<head>".PHP_EOL;
139 $str_html .= " <meta http-equiv='content-type' content='text/html;charset=utf-8' />".PHP_EOL;
140 $str_html .= " <title>##TITLE##</title>".PHP_EOL;
141 //$str_html .= " <link rel='stylesheet' href='jscript/style/csv_new.css' type='text/css' media='print, projection, screen' />".PHP_EOL;
142 //$str_html .= " <link rel='stylesheet' href='../css/edi_history.css' type='text/css' />".PHP_EOL;
143 $str_html .= " <link rel='stylesheet' href='$webdir/library/css/edi_history.css' type='text/css' />".PHP_EOL;
144 $str_html .= " <link type='text/javascript' src='$vendordir/jquery-min-1-9-1/index.js' />".PHP_EOL;
145 $str_html .= " <link type='text/javascript' src='$webdir/library/js/jquery-ui-1.8.21.custom.min.js' />".PHP_EOL;
147 $str_html .= "</head>".PHP_EOL."<body>".PHP_EOL;
149 if ($option == 'newfiles') {
150 $str_html = str_replace('##TITLE##', 'Process New Files '.$title, $str_html);
151 } elseif ($option == 'eradisplay') {
152 $str_html = str_replace('##TITLE##', 'ERA Display '.$title, $str_html);
153 } elseif ($option == 'claimstatus') {
154 $str_html = str_replace('##TITLE##', 'Claim Status '.$title, $str_html);
155 } elseif ($option == 'eligibility') {
156 $str_html = str_replace('##TITLE##', 'Eligiility '.$title, $str_html);
157 } elseif ($option == 'authorization') {
158 $str_html = str_replace('##TITLE##', 'Authorization '.$title, $str_html);
159 } elseif ($option == 'x12display') {
160 $str_html = str_replace('##TITLE##', 'x12 File '.$title, $str_html);
161 } elseif ($option == 'csvtable') {
162 $str_html = str_replace('##TITLE##', 'CSV Table '.$title, $str_html);
163 } elseif ($option == 'textdisplay') {
164 $str_html = str_replace('##TITLE##', 'Text '.$title, $str_html);
165 } elseif ($option == 'readme') {
166 $str_html = str_replace('##TITLE##', 'Readme '.$title, $str_html);
167 } else {
168 $str_html = str_replace('##TITLE##', 'OEMR edi_history '.$title, $str_html);
172 return $str_html;
176 * generate the trailing tags for html page
178 * @return string
180 function edih_html_tail()
182 $str_html = PHP_EOL."</body></html>";
183 return $str_html;
188 * Restore an existing archive
190 * @uses edih_archive_restore()
192 * @return string
194 function edih_disp_archive_restore()
196 //name="archrestore_sel" { archrestore: 'yes', archfile: archf };
197 $fn = (isset($_POST['archrestore_sel'])) ? filter_input(INPUT_POST, 'archrestore_sel', FILTER_DEFAULT) : '';
198 if (strlen($fn)) {
199 $str_html = edih_archive_restore($fn);
200 } else {
201 $str_html = "<p>Invalid archive name for archive resstore function</p>".PHP_EOL;
204 return $str_html;
208 * Create a report on edi files and csv tables
210 * @uses edih_archive_report()
212 * @return string
214 function edih_disp_archive_report()
217 $str_html = '';
218 $la = filter_input(INPUT_GET, 'archivereport', FILTER_DEFAULT);
219 $pd = (isset($_GET['period'])) ? filter_input(INPUT_GET, 'period', FILTER_DEFAULT) : '';
221 csv_edihist_log("GET archivereport: archivereport $la period $pd");
223 if ($la == 'yes') {
224 $str_html = edih_archive_report($pd);
225 } else {
226 $str_html = "File Information report input parameter error<br>";
230 return $str_html;
235 * Archive of old edi files
237 * @uses edih_archive_main()
239 * @return string
241 function edih_disp_archive()
244 $pd = (isset($_POST['archive_sel'])) ? filter_input(INPUT_POST, 'archive_sel', FILTER_DEFAULT) : '';
246 if ($pd) {
247 $str_html = edih_archive_main($pd);
248 } else {
249 $str_html = "<p>Invalid aging period for archive function</p>".PHP_EOL;
252 return $str_html;
256 * call new uploaded files process functions
258 * @todo save the newfiles lists to file so they can
259 * be re-displayed if user has to close app before
260 * finishing review (need to have csv_write option)
262 * @uses csv_newfile_list()
263 * @uses edih_parse_select()
264 * @uses edih_csv_write()
265 * @uses edih_csv_process_html()
267 * @return string html format
269 function edih_disp_file_process()
271 //file "fileUplMulti "submit" "uplsubmit" "button""uplreset"
273 // debug
274 if (isset($_GET)) {
275 $dbg_str = 'GET vars ';
276 foreach ($_GET as $k => $v) {
277 $dbg_str .= $k.' '.$v.' ';
280 csv_edihist_log("edih_disp_file_process $dbg_str");
284 if (!isset($_GET['ProcessFiles'])) {
285 // should only be called with this value existing
286 $str_html = "Error: invalid value for Process New <br />".PHP_EOL;
287 return $str_html;
290 $htm = $er = false;
291 if (isset($_GET['process_html'])) {
292 // show tables for process results
293 $htmval = filter_input(INPUT_GET, 'process_html', FILTER_DEFAULT);
294 $htm = ($htmval == 'htm') ? true : false;
297 if (isset($_GET['process_err'])) {
298 // show only claims with errors (denied, rejected, etc)
299 $errval = filter_input(INPUT_GET, 'process_err', FILTER_DEFAULT);
300 $er = ($errval == 'err') ? true : false;
303 $str_html = "";
305 $p = csv_parameters();
306 $ftype = array_keys($p);
307 $fct = 0;
309 foreach ($ftype as $tp) {
310 $checkdir = false;
311 // check for directory contents
312 $fdir = $p[$tp]['directory'];
313 if (is_dir($fdir)) {
314 $dh = opendir($fdir);
315 if ($dh) {
316 while (($file = readdir($dh)) !== false) {
317 if ($file != '.' && $file != '..') {
318 $checkdir = true;
319 break;
323 closedir($dh);
327 // if false, no files in directory
328 if (!$checkdir) {
329 continue;
333 $upload_ar = csv_newfile_list($tp);
335 if (is_array($upload_ar) && count($upload_ar)) {
336 $dirct = count($upload_ar);
337 if ($htm) {
338 $dtl = ($er) ? "(claims: errors only)" : "(claims: all)";
339 //$hvals = csv_table_header($tp, 'file');
340 //$col_ct = count($hvals);
341 ////csv_table_header(
342 //$str_html .= "<table class='$tp' cols=$col_ct><caption>$tp Files Summary $dtl</caption>".PHP_EOL;
343 //$str_html .= csv_thead_html($tp, 'file');
344 //$str_html .= "<tbody>".PHP_EOL;
345 $str_html .= "<h2 class='prcs'>$tp $dirct files $dtl</h2>".PHP_EOL;
346 $str_html .= "<dl class='$tp'>".PHP_EOL;
349 foreach ($upload_ar as $fn) {
350 $fp = $fdir.DS.$fn;
351 $csvdata = edih_parse_select($fp);
352 $csvchr = edih_csv_write($csvdata);
353 $fct++;
354 if ($htm) {
355 $str_html .= edih_csv_process_html($csvdata, $er);
359 //$str_html .= ($htm) ? "</tbody>".PHP_EOL."</table>".PHP_EOL : "";
360 $str_html .= ($htm) ? "</dl>".PHP_EOL : "";
361 } else {
362 $str_html .= "<p>No new $tp files</p>";
366 $capt_html = "<p>Process new files ($fct files)</p>".PHP_EOL;
367 return $capt_html . $str_html;
371 * uploading of new files
373 * @uses edih_upload_files()
374 * @uses edih_sort_upload()
375 * @return string
377 function edih_disp_file_upload()
379 // multiple file upload
380 $str_html = '';
381 if (isset($_FILES) && count($_FILES)) {
382 $f_array = edih_upload_files();
383 if (is_array($f_array) && count($f_array)) {
384 $str_html .= edih_sort_upload($f_array);
385 } else {
386 $str_html .= "no files accepted <br />".PHP_EOL;
388 } else {
389 $str_html .= "no files submitted <br />" . PHP_EOL;
393 return $str_html;
396 function edih_disp_denied_claims()
399 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_DEFAULT) : '';
400 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_DEFAULT) : '';
401 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_DEFAULT) : '';
403 $str_html = edih_list_denied_claims($ft, $fn, $trace);
405 return $str_html;
409 * display the contents of an x12_edi transaction selected from
410 * a csv table or processed files table
412 * @uses csv_file_by_enctr()
413 * @uses csv_file_by_controlnum()
414 * @uses ibr_batch_get_st_block()
415 * @return string
417 function edih_disp_x12trans()
420 // query source ['gtbl'] file claim hist
422 // file: FileName fname=$fn1&ftype=$ft&fmt=htm' filename x12type format
423 // file: Control fname=$fn1&ftype=$ft&icn=$ctl&fmt=seg filename x12type isa13 format
424 // file: Trace trace=$tr&ftype=$typ&fmt=htm trace x12type format
426 // claim: FileName fname=$fn1&ftype=$ft&fmt=htm filename x12type format:html
427 // claim: Control fname=$fn1&ftype=$ft&icn=$ctl&fmt=seg filename x12type icn format:segment text
428 // claim: CLM01 fname=$fn1&ftype=$ft&pid=$pid filename x12type pid-enctr
429 // claim: Status fname=fname=$fn1&ftype=$ft&pid=$pid&summary=yes filename x12type pid-enctr summary
430 // claim: Status fname=$fn1&ftype=$ft&pid=$pid&summary=no' filename x12type pid-enctr detail
431 // claim: Trace (835) fname=$fn1&ftype=$ft&trace=$trc trace filename x12type
432 // claim: Trace (999) trace=$trc&rsptype=$typ&ftype=$ft trace(bht03syn) response-type x12type
433 // claim: Trace (277) trace=$v&ftype=$tp&rsptype=f837&fmt=seg' trace(clm01) response-type {837) x12type
434 // claim: BHT03 (27x) fname=$fn1&ftype=$ft&bht03=$bht03&fmt=htm filename x12type bht03
435 // claim: err_seg fname=$fn1&ftype=$ft&trace=$trc&rsptype=$typ&err=$err filename x12type trace(bht03syn) response_type error_segment
437 // use files (1) x12 display of file segments (2) 835 html RA or Payment Trace (3) trace from 997 or 271/277/278
438 // $fn or $icn & $ft $fn $icn $trace & $ft $trace & $rsptype
439 // claims (1) html of transaction (2) segments of transaction (3) trace to precedent transaction
440 // $fn & $ft $ pid $trace & $rsptype
442 $str_htm = '';
443 if (isset($_GET['gtbl'])) {
444 $qs = filter_input(INPUT_GET, 'gtbl', FILTER_DEFAULT);
447 if (!$qs) {
448 $str_htm .= '<p>edih_disp_x12 error: missing parameter</p>';
449 csv_edihist_log("edih_io_disp_x12: missing parameter, no 'gtbl' value");
450 return $str_htm;
454 $fmt = isset($_GET['fmt']) ? filter_input(INPUT_GET, 'fmt', FILTER_DEFAULT) : '';
456 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_DEFAULT) : '';
457 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_DEFAULT) : '';
458 $icn = isset($_GET['icn']) ? filter_input(INPUT_GET, 'icn', FILTER_DEFAULT) : '';
459 $rsptype = isset($_GET['rsptype']) ? filter_input(INPUT_GET, 'rsptype', FILTER_DEFAULT) : '';
461 $clm01 = isset($_GET['pid']) ? filter_input(INPUT_GET, 'pid', FILTER_DEFAULT) : '';
462 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_DEFAULT) : '';
463 $bht03 = isset($_GET['bht03']) ? filter_input(INPUT_GET, 'bht03', FILTER_DEFAULT) : '';
464 $err = isset($_GET['err']) ? filter_input(INPUT_GET, 'err', FILTER_DEFAULT) : '';
465 $summary = isset($_GET['summary']) ? filter_input(INPUT_GET, 'summary', FILTER_DEFAULT) : false;
467 // debug
468 //$str_htm .= "<p>edih_disp_x12trans values: <br>".PHP_EOL;
469 //$str_htm .= "qs $qs fmt $fmt fn $fn ft $ft icn $icn rsptype $rsptype clm01 $clm01 trace $trace bht03 $bht03 err $err summary $summary</p>".PHP_EOL;
471 if ($ft) {
472 $ft = csv_file_type($ft);
476 if ($qs == 'claim') {
477 if ($ft == 'f997') {
478 if ($trace && $rsptype) {
479 $fname = csv_file_by_trace($trace, $ft, $rsptype);
480 if ($fname) {
481 $str_htm .= edih_display_text($fname, $rsptype, $trace, $err);
482 } else {
483 $str_htm .= "<p>Did not find $trace in the $rsptype claims table.</p>";
486 //$fnar = csv_file_by_enctr($trace, $rsptype, $srchtype='ptidn' );
487 //if (is_array($fnar) && count($fnar)) {
488 //foreach($fnar as $fa) {
489 //$fname = $fa['FileName'];
490 //$str_htm .= edih_display_text($fname, $rsptype, $trace, $err);
492 //} else {
493 //$str_htm .= "<p>Did not find $trace in the $rsptype claims table.</p>";
496 } elseif ($ft == 'f837') {
497 // either transaction or file
498 $str_htm .= edih_display_text($fn, $ft, $clm01);
499 } elseif ($ft == 'f835') {
500 if ($fmt == 'seg') {
501 // either transaction or file
502 $str_htm .= edih_display_text($fn, $ft, $clm01);
503 } elseif ($trace) {
504 // the check trace
505 $str_htm .= edih_835_html($fn, $trace);
506 } elseif ($clm01) {
507 // this claim payment
508 $str_htm .= edih_835_html($fn, '', $clm01, $summary);
510 } elseif (strpos('|f270|f271|f276|f277|f278', $ft)) {
511 if ($fmt == 'seg') {
512 if ($trace && $rsptype) {
513 // 270|276|278|837 claim or request segments
514 // here the 'trace' is from trace or clm01
515 //$fnar = csv_file_by_enctr($trace, $rsptype, $srchtype='ptidn' );
516 //if (is_array($fnar) && count($fnar)) {
517 //foreach($fnar as $fa) {
518 //$fname = $fa['FileName'];
519 //$str_htm .= edih_display_text($fname, $rsptype, $trace);
521 $fname = csv_file_by_trace($trace, $ft, $rsptype);
522 if ($fname) {
523 $str_htm .= edih_display_text($fname, $rsptype, $trace);
524 } else {
525 $str_htm .= "<p>Did not find $trace in type $rsptype csv_claims table</p>".PHP_EOL;
526 csv_edihist_log("edih_disp_x12trans: Did not find $trace in type $rsptype csv_claims table");
528 } else {
529 // entire file or transaction if bht03 has a value
530 $str_htm .= edih_display_text($fn, $ft, $bht03);
532 } else {
533 // html format
534 if ($ft == 'f277') {
535 $str_htm .= edih_277_html($fn, $bht03);
536 } elseif ($ft == 'f271') {
537 $str_htm .= edih_271_html($fn, $bht03);
538 } elseif ($ft == 'f278') {
539 $str_htm .= edih_278_html($fn, $bht03);
540 } else {
541 // html display not available, use segments
542 $str_htm .= edih_display_text($fn, $ft, $bht03);
546 } elseif ($qs == 'hist') {
547 if ($fn && $ft == 'f837') {
548 if ($clm01) {
549 $str_htm .= edih_display_text($fn, $ft, $clm01);
550 } else {
551 $str_htm .= edih_display_text($fn, $ft);
553 } elseif ($fn && $ft == 'f997') {
554 if ($trace && $rsptype && $err) {
555 $str_htm .= edih_display_text($fn, $rsptype, $trace, true, $err);
556 } elseif ($trace && $rsptype) {
557 $str_htm .= edih_display_text($fn, $rsptype);
558 } else {
559 $str_htm .= edih_display_text($fn, $ft);
561 } elseif ($fn && $ft == 'f277') {
562 if ($trace && $rsptype) {
563 $fname = csv_file_by_trace($trace, $ft, $rsptype);
564 if ($fname) {
565 $str_htm .= edih_display_text($fname, $rsptype, $trace);
566 } else {
567 $str_htm .= "<p>Did not find $trace in type $rsptype csv_claims table</p>".PHP_EOL;
568 csv_edihist_log("edih_disp_x12trans: Did not find $trace in type $rsptype csv_claims table");
570 } elseif ($clm01) {
571 $str_htm .= edih_277_html($fn, $clm01);
572 } elseif ($bht03) {
573 $str_htm .= edih_277_html($fn, $bht03);
574 } else {
575 $str_htm .= edih_display_text($fn, $ft);
577 } elseif ($fn && $ft == 'f835') {
578 if ($clm01) {
579 if ($summary == 'yes') {
580 $str_htm .= edih_835_html($fn, '', $clm01, true);
581 } else {
582 $str_htm .= edih_835_html($fn, '', $clm01);
584 } elseif ($trace) {
585 $str_htm .= edih_835_html($fn, $trace);
588 } else {
589 $str_htm .= 'error: could not process request.';
592 return $str_htm;
596 * display fule uploaded from x12 File tab
597 * wrap individual transactions in accordian jquery ui widget
599 * @uses csv_check_x12_obj()
600 * @uses edih_html_heading()
601 * @uses edih_271_transaction_html()
602 * @uses edih_278_transaction_html()
603 * @uses edih_277_transaction_html()
604 * @uses edih_835_html_page()
605 * @uses edih_display_text()
607 * @param string path to x12 file
608 * @return string
610 function edih_disp_x12file()
613 $str_htm = '';
614 $fn = $ft = $icn = $trace = $rsptype = $format = '';
616 if (isset($_POST['x12_html'])) {
617 $htmval = filter_input(INPUT_POST, 'x12_html', FILTER_DEFAULT);
618 $format = ($htmval == 'html') ? 'htm' : 'seg';
619 $upldir = csv_edih_tmpdir();
620 } else {
621 $format = 'seg';
624 // post request from x12 file tab
625 if (count($_FILES) && isset($_FILES['fileUplx12'])) {
626 $fnupl = htmlentities($_FILES['fileUplx12']['name']);
627 // the upload files validator
628 $f_array = edih_upload_files();
630 if (is_array($f_array) && count($f_array)) {
631 // was file rejected?
632 if (isset($f_array['reject'])) {
633 $fn = (count($f_array['reject'][0])) ? $f_array['reject'][0]['name'] : '';
634 $cmt = (count($f_array['reject'][0])) ? $f_array['reject'][0]['comment'] : '';
635 //$str_html = edih_html_heading('error');
636 $str_htm .= "<p>Rejected file:</p>".PHP_EOL;
637 $str_htm .= "<p>$fn<br>".PHP_EOL;
638 $str_htm .= " -- $cmt</p>".PHP_EOL;
640 csv_edihist_log("edih_disp_x12file: rejected file $fn comment: $cmt");
642 return $str_htm;
643 } else {
644 $fnar = reset($f_array); // type filename array
645 $ft = key($f_array); // type
646 $fn1 = $f_array[$ft][0]; //$upldir.DS.
647 $fn = csv_check_filepath($fn1);
648 csv_edihist_log("edih_disp_x12file: submitted POST $format $ft $fn1 $fnupl");
650 if (!$fn) {
651 //$str_htm = edih_html_heading('error');
652 $str_htm .= "<p>Path error for $fn1</p>" . PHP_EOL;
653 csv_edihist_log("edih_disp_x12file: Path error for $fn1");
654 return $str_htm;
657 } else {
658 //$str_htm = edih_html_heading('error');
659 $str_htm .= "<p>File not accepted $fnupl</p>" . PHP_EOL;
660 csv_edihist_log("edih_disp_x12file: File not accepted $fnupl");
661 return $str_htm;
663 } elseif (isset($_GET['gtbl']) && $_GET['gtbl'] == 'file') {
664 // this is a GET request from csv files table
665 // assemble variables
666 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_DEFAULT) : '';
667 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_DEFAULT) : '';
668 $icn = isset($_GET['icn']) ? filter_input(INPUT_GET, 'icn', FILTER_DEFAULT) : '';
669 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_DEFAULT) : '';
670 $rsptype = isset($_GET['rsptype']) ? filter_input(INPUT_GET, 'rsptype', FILTER_DEFAULT) : '';
671 $format = isset($_GET['fmt']) ? filter_input(INPUT_GET, 'fmt', FILTER_DEFAULT) : '';
673 } else {
674 //$str_htm = edih_html_heading('error');
675 $str_htm .= "<p>Error: No request received by server</p>" . PHP_EOL;
676 csv_edihist_log("edih_disp_x12file: No request received by server");
677 return $str_htm;
681 if (!$fn) {
682 if ($ft && $icn) {
683 $fnr = csv_file_by_controlnum($ft, $icn);
684 $fn = csv_check_filepath($fnr);
685 } elseif ($ft && $trace && $rsptype) {
686 $fnr = csv_file_by_trace($trace, $ft, $rsptype);
687 $fn = csv_check_filepath($fnr);
688 $ft = $rsptype;
689 $trace = '';
690 } elseif ($ft == 'f835' && $trace) {
691 $fnr = csv_file_by_trace($trace, $ft, $rsptype);
692 $fn = csv_check_filepath($fnr);
693 } elseif ($ft == 'f997' && $trace && $rsptype) {
694 $fnr = csv_file_by_controlnum($rsptype, $trace);
695 $fn = csv_check_filepath($fnr);
696 $ft = $rsptype;
697 $trace = '';
698 if (!$fn) {
699 $str_htm .= "<p>997/999 Trace value $trace not found for type $rsptype</p>" . PHP_EOL;
700 csv_edihist_log("edih_disp_x12file: 997/999 Trace value $trace not found for type $rsptype");
701 return $str_htm;
706 if (!$fn) {
707 //$str_htm = edih_html_heading('error');
708 $str_htm .= "<p>Name error for file: type $ft icn $icn trace $trace rsp $rsptype</p>" . PHP_EOL;
709 csv_edihist_log("edih_disp_x12file: Name error for file: type $ft icn $icn trace $trace rsp $rsptype");
710 return $str_htm;
714 if ($format == 'seg') {
715 if ($ft == 'f835' && $trace) {
716 $str_htm .= edih_display_text($fn, $ft, $trace, true);
717 } elseif ($icn) {
718 $str_htm .= edih_display_text($fn, $ft, $icn, true);
719 } else {
720 $str_htm .= edih_display_text($fn, $ft);
723 csv_edihist_log("edih_disp_x12file: segments display $fn");
725 return $str_htm;
728 // request is for html display
729 // now go through each file type
730 // 'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277',
731 // 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
732 if ($ft == 'f271' || $ft == 'f270') {
733 //$str_htm .= edih_html_heading('eligibility', $fn);
734 $str_htm .= edih_271_html($fn);
735 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
736 } elseif ($ft == 'f276' || $ft == 'f277') {
737 //$str_htm .= edih_html_heading('claimstatus', $fn);
738 $str_htm .= edih_277_html($fn);
739 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
740 } elseif ($ft == 'f278') {
741 //$str_htm .= edih_html_heading('claimstatus', $fn);
742 $str_htm .= edih_278_html($fn);
743 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
744 } elseif ($ft == 'f835') {
745 //$str_htm .= edih_html_heading('eradisplay', $fn);
746 $str_htm = edih_835_html($fn, $trace);
747 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
748 } else {
749 // no html format for this type
750 // object is created in edih_display_text function
751 // edih_display_text($filepath, $filetype='', $claimid='', $trace=false, $err_info='')
752 //$str_htm .= edih_html_heading('x12display', $fn);
753 $str_htm .= edih_display_text($fn, $ft);
754 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
758 return $str_htm;
763 * csv tables filter input and generate table
765 * @uses csv_to_html()
766 * @return string
768 function edih_disp_csvtable()
771 $str_html = '';
772 $prd = (isset($_GET['csv_period'])) ? filter_input(INPUT_GET, 'csv_period', FILTER_DEFAULT) : '';
773 $dts = (isset($_GET['csv_date_start'])) ? filter_input(INPUT_GET, 'csv_date_start', FILTER_SANITIZE_NUMBER_INT) : '';
774 $dte = (isset($_GET['csv_date_end'])) ? filter_input(INPUT_GET, 'csv_date_end', FILTER_SANITIZE_NUMBER_INT) : '';
775 $csvfile = (isset($_GET['csvtables'])) ? filter_input(INPUT_GET, 'csvtables', FILTER_DEFAULT) : '';
777 // debug
778 csv_edihist_log("edih_disp_csvtable: $csvfile period $prd datestart $dts dateend $dte");
780 if ($dts && strpos($dts, '-') != 4) {
781 if (strlen($_GET['csv_date_start']) == 10 && strpos($_GET['csv_date_start'], '/') == 4) {
782 $dts = str_replace('/', '-', $dts);
783 } else {
784 $str_html = "<p>Date $dts must be in YYYY-MM-DD format, no / or . please</p>".PHP_EOL;
785 csv_edihist_log("invalid date $dts submitted for csv_table filter");
786 return $str_html;
790 if ($dte && strpos($dte, '-') != 4) {
791 if (strlen($_GET['csv_date_end']) == 10 && strpos($_GET['csv_date_end'], '/') == 4) {
792 $dte = str_replace('/', '-', $dte);
793 } else {
794 $dte = '';
798 if (!$csvfile || $csvfile == null || $csvfile === false) {
799 // here we have an error and must quit
800 $str_html = "<p>Error in CSV table name </p>".PHP_EOL;
801 return $str_html;
802 } else {
803 $tp_ar = explode('_', $csvfile);
804 $tbl_type = ($tp_ar[0] == 'claims') ? 'claim' : 'file';
805 $f_type = strval($tp_ar[1]);
806 if (ctype_digit($f_type)) {
807 $f_type = 'f'.$f_type;
811 $str_html = edih_csv_to_html($f_type, $tbl_type, $prd, $dts, $dte);
813 return $str_html;
818 * Report 835 file as processed by billing routine if the file name
819 * is found in the 'era' directory. The file name is a concatenation
820 * of GS04_TRN04_ISA13.edi per parse_era_inc.php
823 * @param string
824 * @return bool
826 function edih_disp_835_processed($erasavename)
828 // openemr/interface/billing/era_payments.php
829 // openemr/library/parse_era.inc.php
830 // OpenEMR filename for era should be just the upload filename or
831 // $out['gs_date'] . '_' . $out['payer_id'] . '_' .$out['isa_control_number']
832 // with 'payer_id' taken from BPR10 or TRN03 (same value) and not from TRN04
834 // search for YYYYMMDD_NNNNNNNN_ISA13
835 $eraname = $out['gs_date'] . '_' . ltrim($out['isa_control_number'], '0') .
836 '_' . ltrim($out['payer_id'], '0');
837 $out['payer_id'] = trim($seg[4]); //TRN04
839 $srchlen = strlen($erasavename);
840 $found = false;
841 $eradir = $GLOBALS['OE_SITE_DIR'].DS.'edi'.DS.'era';
843 if ($hd = opendir($eradir)) {
844 while (false !== ($entry = readdir($hd))) {
845 if (strncmp($entry, $erasavename, $srchlen) === 0) {
846 $found = true;
847 break;
851 closedir($hd);
852 } else {
853 csv_edihist_log("edih_disp_processed_835: did not find processed era directory");
856 return $found;
859 function edih_disp_clmhist()
862 if (isset($_GET['hist_enctr'])) {
863 $enctr = filter_input(INPUT_GET, 'hist_enctr', FILTER_DEFAULT);
864 if ($enctr) {
865 $str_html = edih_claim_history($enctr);
866 } else {
867 $str_html = "Invalid or unknown encounter number".PHP_EOL;
869 } else {
870 $str_html = "Invalid or unknown encounter number".PHP_EOL;
873 return $str_html;
878 * display the message part of a 999 response
880 * @uses ibr_997_errscan()
881 * @return string
883 function ibr_disp_997_message()
886 $fname = '';
887 $akval = '';
888 $errval = '';
889 $fname = filter_input(INPUT_GET, 'fv997', FILTER_DEFAULT);
890 if (isset($_GET['aknum'])) {
891 $akval = filter_input(INPUT_GET, 'aknum', FILTER_DEFAULT);
894 if (isset($_GET['err997'])) {
895 $errval = filter_input(INPUT_GET, 'err997', FILTER_DEFAULT);
898 if (!$fname) {
899 $str_html = "Missing file name.<br />".PHP_EOL;
900 } else {
901 $str_html = ibr_997_errscan($fname, $akval);
904 return $str_html;
908 * display the message part of a ACK or TA1 response
910 * @uses ibr_ack_error()
911 * @return string
913 function ibr_disp_ta1_message()
916 $fname = '';
917 $code = '';
918 $fname = filter_input(INPUT_GET, 'ackfile', FILTER_DEFAULT);
919 if (isset($_GET['ackcode'])) {
920 $code = filter_input(INPUT_GET, 'ackcode', FILTER_DEFAULT);
923 if ($fname && $code) {
924 $str_html = ibr_ack_error($fname, $code);
925 } else {
926 $str_html = 'Code value invalid <br />'.PHP_EOL;
929 return $str_html;
934 * check if the batch control number is found in the 997/999 files table
936 * @uses csv_search_record()
937 * @return string
939 function ibr_disp_997_for_batch()
941 $str_html = '';
942 $batch_icn = filter_input(INPUT_GET, 'batchicn', FILTER_DEFAULT);
943 if ($batch_icn) {
944 $ctln = (strlen($batch_icn) >= 9) ? substr($batch_icn, 0, 9) : trim(strval($batch_icn));
945 $search = array('s_val'=>$ctln, 's_col'=>3, 'r_cols'=>'all');
946 $result = csv_search_record('f997', 'file', $search, "1");
948 // should be matching row(s) from files_997.csv
949 if (is_array($result) && count($result)) {
950 $str_html .= "<p>Acknowledgement information</p>".PHP_EOL;
951 foreach ($result as $rslt) {
952 $ext = substr($rslt[1], -3);
954 $str_html .= "Date: {$rslt[0]} <br />".PHP_EOL;
955 $str_html .= "File: <a target=\"blank\" href=edi_history_main.php?fvkey={$rslt[1]}>{$rslt[1]}</a> <br />".PHP_EOL;
956 $str_html .= "Batch ICN: {$rslt[3]} <br />";
957 // error count or code in position 4
958 if ($ext == '999' || $ext == '997') {
959 $str_html .= "Rejects: {$rslt[4]} <br />".PHP_EOL;
960 // don't have dialog from this dialog, so don't link
961 //$str_html .= "Rejects: <a class=\"codeval\" target=\"_blank\" href=\"edi_history_main.php?fv997={$rslt[1]}&err997={$rslt[4]}\">{$rslt[4]}</a><br />".PHP_EOL;
962 } elseif ($ext == 'ta1' || $ext == 'ack') {
963 $str_html .= "Code: {$rslt[4]} <br />".PHP_EOL;
964 //$str_html .= "Code: <a class=\"codeval\" target=\"_blank\" href=\"edi_history_main.php?ackfile={$rslt[1]}&ackcode={$rslt[4]}\">{$rslt[4]}</a><br />".PHP_EOL;
967 } else {
968 $str_html .= "Did not find corresponding 997/999 file for $ctln<br />".PHP_EOL;
970 } else {
971 $str_html .= "Invalid value for ICN number<br />".PHP_EOL;
974 return $str_html;
978 * function to check whether an era payment has been processed and applied
980 * @uses sqlQuery()
982 * @return string
984 function edih_disp_era_processed()
987 $str_html = '';
988 $ckno = filter_input(INPUT_GET, 'tracecheck', FILTER_DEFAULT);
989 if ($ckno) {
990 $srchval = 'ePay - '.$ckno;
991 // reference like '%".$srchval."%'"
992 $row = sqlQuery("SELECT reference, pay_total, global_amount FROM ar_session WHERE reference = ?", array($srchval));
993 if (!empty($row)) {
994 $str_html .= "trace {$row['reference']} total \${$row['pay_total']}";
995 if ($row['global_amount'] === '0') {
996 $str_html .= " fully allocated";
997 } else {
998 $str_html .= " ({$row['global_amount']} not allocated)";
1000 } else {
1001 $str_html .= "trace $ckno not posted";
1003 } else {
1004 $str_html .= "trace $ckno not found";
1007 return $str_html;