EDI history module, version 2.(take 3)
[openemr.git] / library / edihistory / edih_io.php
blob4c0bfed45e156d7f724926cd3014d663c455a48c
1 <?php
2 /*
3 * edih_io.php
4 *
5 * Copyright 2016 Kevin McCormick Longview, Texas
6 *
7 *
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() {
34 return @ $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] === 'XMLHttpRequest';
37 /**
38 * Get some values from php ini functions for interface
40 * @return array json
42 function edih_php_inivals() {
43 $ival = array();
44 $td = basename(sys_get_temp_dir());
45 $ival['maxfsize'] = ini_get('upload_max_filesize');
46 $ival['maxfuploads'] = ini_get('max_file_uploads');
47 $ival['postmaxsize'] = ini_get('post_max_size');
48 $ival['tmpdir'] = $td;
49 $json = json_encode($ival);
51 return $json;
54 /**
55 * display the log file selected
57 * @uses csv_log_html()
58 * @return string
60 function edih_disp_log() {
61 $lfn = '';
62 if ( isset($_GET['log_select']) ) {
63 $lfn = filter_input(INPUT_GET, 'log_select', FILTER_SANITIZE_STRING);
65 $str_html = csv_log_html($lfn);
66 return $str_html;
69 function edih_disp_logfiles() {
70 $str_html = '';
71 $lst = true;
72 if ( isset($_GET['loglist']) ) {
73 // loglist: 'yes'
74 $lval = filter_input(INPUT_GET, 'loglist', FILTER_SANITIZE_STRING);
75 $lst = ($lval == 'yes') ? true : false;
76 } elseif ( isset($_GET['archivelog']) ) {
77 // archivelog: 'yes'
78 $lval = filter_input(INPUT_GET, 'archivelog', FILTER_SANITIZE_STRING);
79 $lst = ($lval == 'yes') ? false : true;
80 } else {
81 csv_edihist_log('edih_disp_logfiles: input parameter error');
82 return "input parameter error<br />";
84 // returns json array
85 $str_html = csv_log_manage($lst);
86 return $str_html;
88 /**
89 * read or write simple notes to a text file
91 * @uses csv_notes_file()
92 * @return string
94 function edih_user_notes() {
96 if ( isset($_GET['getnotes']) ) {
97 $getnt = filter_input(INPUT_GET, 'getnotes', FILTER_SANITIZE_STRING);
98 if ($getnt == 'yes') {
99 $str_html = csv_notes_file();
101 } elseif ( isset($_POST['notes_hidden']) && isset($_POST['txtnotes']) ) {
102 $putnt = filter_input(INPUT_POST, 'putnotes', FILTER_SANITIZE_STRING);
103 if ($putnt == 'yes') {
104 $notetext = trim($_POST['txtnotes']);
105 $filtered = filter_var($notetext, FILTER_SANITIZE_STRING);
106 //echo $filtered .PHP_EOL;
107 $str_html = csv_notes_file($filtered, false);
109 } else {
110 csv_edihist_log('edih_user_notes: invalid values in request');
111 $str_html = "<p>User Notes: invalid values in request.</p>";
113 return $str_html;
117 * generate the heading string for an html page
119 * @return string html heading stanza
121 function edih_html_heading($option, $title='') {
123 //if (!is_string($title)) { $title=''; }
124 $title = (is_string($title)) ? $title : '';
125 //$srcdir = $GLOBALS['srcdir'];
126 $webdir = $GLOBALS['webroot'];
128 $str_html = "<!DOCTYPE html>".PHP_EOL."<html>".PHP_EOL."<head>".PHP_EOL;
129 $str_html .= " <meta http-equiv='content-type' content='text/html;charset=utf-8' />".PHP_EOL;
130 $str_html .= " <title>##TITLE##</title>".PHP_EOL;
131 //$str_html .= " <link rel='stylesheet' href='jscript/style/csv_new.css' type='text/css' media='print, projection, screen' />".PHP_EOL;
132 //$str_html .= " <link rel='stylesheet' href='../css/edi_history.css' type='text/css' />".PHP_EOL;
133 $str_html .= " <link rel='stylesheet' href='$webdir/library/css/edi_history.css' type='text/css' />".PHP_EOL;
134 $str_html .= " <link type='text/javascript' src='$webdir/library/js/jquery-1.9.1.min.js' />".PHP_EOL;
135 $str_html .= " <link type='text/javascript' src='$webdir/library/js/jquery-ui-1.8.21.custom.min.js' />".PHP_EOL;
137 $str_html .= "</head>".PHP_EOL."<body>".PHP_EOL;
139 if ($option == 'newfiles') {
140 $str_html = str_replace('##TITLE##', 'Process New Files '.$title, $str_html);
141 } elseif ($option == 'eradisplay') {
142 $str_html = str_replace('##TITLE##', 'ERA Display '.$title, $str_html);
143 } elseif ($option == 'claimstatus') {
144 $str_html = str_replace('##TITLE##', 'Claim Status '.$title, $str_html);
145 } elseif ($option == 'eligibility') {
146 $str_html = str_replace('##TITLE##', 'Eligiility '.$title, $str_html);
147 } elseif ($option == 'authorization') {
148 $str_html = str_replace('##TITLE##', 'Authorization '.$title, $str_html);
149 } elseif ($option == 'x12display') {
150 $str_html = str_replace('##TITLE##', 'x12 File '.$title, $str_html);
151 } elseif ($option == 'csvtable') {
152 $str_html = str_replace('##TITLE##', 'CSV Table '.$title, $str_html);
153 } elseif ($option == 'textdisplay') {
154 $str_html = str_replace('##TITLE##', 'Text '.$title, $str_html);
155 } elseif ($option == 'readme') {
156 $str_html = str_replace('##TITLE##', 'Readme '.$title, $str_html);
157 } else {
158 $str_html = str_replace('##TITLE##', 'OEMR edi_history '.$title, $str_html);
161 return $str_html;
165 * generate the trailing tags for html page
167 * @return string
169 function edih_html_tail() {
170 $str_html = PHP_EOL."</body></html>";
171 return $str_html;
176 * Restore an existing archive
178 * @uses edih_archive_restore()
180 * @return string
182 function edih_disp_archive_restore() {
183 //name="archrestore_sel" { archrestore: 'yes', archfile: archf };
184 $fn = (isset($_POST['archrestore_sel'])) ? filter_input(INPUT_POST, 'archrestore_sel', FILTER_SANITIZE_STRING) : '';
185 if (strlen($fn)) {
186 $str_html = edih_archive_restore($fn);
187 } else {
188 $str_html = "<p>Invalid archive name for archive resstore function</p>".PHP_EOL;
190 return $str_html;
194 * Create a report on edi files and csv tables
196 * @uses edih_archive_report()
198 * @return string
200 function edih_disp_archive_report() {
202 $str_html = '';
203 $la = filter_input(INPUT_GET, 'archivereport', FILTER_SANITIZE_STRING);
204 $pd = (isset($_GET['period'])) ? filter_input(INPUT_GET, 'period', FILTER_SANITIZE_STRING) : '';
206 csv_edihist_log("GET archivereport: archivereport $la period $pd");
208 if ($la == 'yes') {
209 $str_html = edih_archive_report($pd);
210 } else {
211 $str_html = "File Information report input parameter error<br>";
214 return $str_html;
219 * Archive of old edi files
221 * @uses edih_archive_main()
223 * @return string
225 function edih_disp_archive() {
227 $pd = (isset($_POST['archive_sel'])) ? filter_input(INPUT_POST, 'archive_sel', FILTER_SANITIZE_STRING) : '';
229 if ($pd) {
230 $str_html = edih_archive_main($pd);
231 } else {
232 $str_html = "<p>Invalid aging period for archive function</p>".PHP_EOL;
234 return $str_html;
238 * call new uploaded files process functions
240 * @todo save the newfiles lists to file so they can
241 * be re-displayed if user has to close app before
242 * finishing review (need to have csv_write option)
244 * @uses csv_newfile_list()
245 * @uses edih_parse_select()
246 * @uses edih_csv_write()
247 * @uses edih_csv_process_html()
249 * @return string html format
251 function edih_disp_file_process() {
252 //file "fileUplMulti "submit" "uplsubmit" "button""uplreset"
254 // debug
255 if (isset($_GET)) {
256 $dbg_str = 'GET vars ';
257 foreach($_GET as $k=>$v) {
258 $dbg_str .= $k.' '.$v.' ';
260 csv_edihist_log("edih_disp_file_process $dbg_str");
263 if (!isset($_GET['ProcessFiles']) ) {
264 // should only be called with this value existing
265 $str_html = "Error: invalid value for Process New <br />".PHP_EOL;
266 return $str_html;
268 $htm = $er = false;
269 if (isset($_GET['process_html'])) {
270 // show tables for process results
271 $htmval = filter_input(INPUT_GET, 'process_html', FILTER_SANITIZE_STRING);
272 $htm = ($htmval == 'htm') ? true : false;
274 if (isset($_GET['process_err'])) {
275 // show only claims with errors (denied, rejected, etc)
276 $errval = filter_input(INPUT_GET, 'process_err', FILTER_SANITIZE_STRING);
277 $er = ($errval == 'err') ? true : false;
279 $str_html = "";
281 $p = csv_parameters();
282 $ftype = array_keys($p);
283 $fct = 0;
285 foreach($ftype as $tp) {
286 $checkdir = false;
287 // check for directory contents
288 $fdir = $p[$tp]['directory'];
289 if (is_dir($fdir)) {
290 $dh = opendir($fdir);
291 if ($dh) {
292 while (($file = readdir($dh)) !== false) {
293 if ($file != '.' && $file != '..') {
294 $checkdir = true;
295 break;
298 closedir($dh);
301 // if false, no files in directory
302 if (!$checkdir) { continue; }
304 $upload_ar = csv_newfile_list($tp);
306 if ( is_array($upload_ar) && count($upload_ar) ) {
307 $dirct = count($upload_ar);
308 if ($htm) {
309 $dtl = ($er) ? "(claims: errors only)" : "(claims: all)";
310 //$hvals = csv_table_header($tp, 'file');
311 //$col_ct = count($hvals);
312 ////csv_table_header(
313 //$str_html .= "<table class='$tp' cols=$col_ct><caption>$tp Files Summary $dtl</caption>".PHP_EOL;
314 //$str_html .= csv_thead_html($tp, 'file');
315 //$str_html .= "<tbody>".PHP_EOL;
316 $str_html .= "<h2 class='prcs'>$tp $dirct files $dtl</h2>".PHP_EOL;
317 $str_html .= "<dl class='$tp'>".PHP_EOL;
319 foreach($upload_ar as $fn) {
320 $fp = $fdir.DS.$fn;
321 $csvdata = edih_parse_select($fp);
322 $csvchr = edih_csv_write($csvdata);
323 $fct++;
324 if ($htm) {
325 $str_html .= edih_csv_process_html($csvdata, $er);
328 //$str_html .= ($htm) ? "</tbody>".PHP_EOL."</table>".PHP_EOL : "";
329 $str_html .= ($htm) ? "</dl>".PHP_EOL : "";
330 } else {
331 $str_html .= "<p>No new $tp files</p>";
334 $capt_html = "<p>Process new files ($fct files)</p>".PHP_EOL;
335 return $capt_html . $str_html;
339 * uploading of new files
341 * @uses edih_upload_files()
342 * @uses edih_sort_upload()
343 * @return string
345 function edih_disp_file_upload() {
346 // multiple file upload
347 $str_html = '';
348 if ( isset($_FILES) && count($_FILES) ) {
349 $f_array = edih_upload_files();
350 if ( is_array($f_array) && count($f_array) ) {
351 $str_html .= edih_sort_upload($f_array);
352 } else {
353 $str_html .= "no files accepted <br />".PHP_EOL;
355 } else {
356 $str_html .= "no files submitted <br />" . PHP_EOL;
359 return $str_html;
362 function edih_disp_denied_claims() {
364 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_SANITIZE_STRING) : '';
365 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_SANITIZE_STRING) : '';
366 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_SANITIZE_STRING) : '';
368 $str_html = edih_list_denied_claims($ft, $fn, $trace);
370 return $str_html;
374 * display the contents of an x12_edi transaction selected from
375 * a csv table or processed files table
377 * @uses csv_file_by_enctr()
378 * @uses csv_file_by_controlnum()
379 * @uses ibr_batch_get_st_block()
380 * @return string
382 function edih_disp_x12trans() {
384 // query source ['gtbl'] file claim hist
386 // file: FileName fname=$fn1&ftype=$ft&fmt=htm' filename x12type format
387 // file: Control fname=$fn1&ftype=$ft&icn=$ctl&fmt=seg filename x12type isa13 format
388 // file: Trace trace=$tr&ftype=$typ&fmt=htm trace x12type format
390 // claim: FileName fname=$fn1&ftype=$ft&fmt=htm filename x12type format:html
391 // claim: Control fname=$fn1&ftype=$ft&icn=$ctl&fmt=seg filename x12type icn format:segment text
392 // claim: CLM01 fname=$fn1&ftype=$ft&pid=$pid filename x12type pid-enctr
393 // claim: Status fname=fname=$fn1&ftype=$ft&pid=$pid&summary=yes filename x12type pid-enctr summary
394 // claim: Status fname=$fn1&ftype=$ft&pid=$pid&summary=no' filename x12type pid-enctr detail
395 // claim: Trace (835) fname=$fn1&ftype=$ft&trace=$trc trace filename x12type
396 // claim: Trace (999) trace=$trc&rsptype=$typ&ftype=$ft trace(bht03syn) response-type x12type
397 // claim: Trace (277) trace=$v&ftype=$tp&rsptype=f837&fmt=seg' trace(clm01) response-type {837) x12type
398 // claim: BHT03 (27x) fname=$fn1&ftype=$ft&bht03=$bht03&fmt=htm filename x12type bht03
399 // claim: err_seg fname=$fn1&ftype=$ft&trace=$trc&rsptype=$typ&err=$err filename x12type trace(bht03syn) response_type error_segment
401 // use files (1) x12 display of file segments (2) 835 html RA or Payment Trace (3) trace from 997 or 271/277/278
402 // $fn or $icn & $ft $fn $icn $trace & $ft $trace & $rsptype
403 // claims (1) html of transaction (2) segments of transaction (3) trace to precedent transaction
404 // $fn & $ft $ pid $trace & $rsptype
406 $str_htm = '';
407 if ( isset($_GET['gtbl']) ) {
408 $qs = filter_input(INPUT_GET, 'gtbl', FILTER_SANITIZE_STRING);
410 if (!$qs) {
411 $str_htm .= '<p>edih_disp_x12 error: missing parameter</p>';
412 csv_edihist_log("edih_io_disp_x12: missing parameter, no 'gtbl' value");
413 return $str_htm;
416 $fmt = isset($_GET['fmt']) ? filter_input(INPUT_GET, 'fmt', FILTER_SANITIZE_STRING) : '';
418 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_SANITIZE_STRING) : '';
419 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_SANITIZE_STRING) : '';
420 $icn = isset($_GET['icn']) ? filter_input(INPUT_GET, 'icn', FILTER_SANITIZE_STRING) : '';
421 $rsptype = isset($_GET['rsptype']) ? filter_input(INPUT_GET, 'rsptype', FILTER_SANITIZE_STRING) : '';
423 $clm01 = isset($_GET['pid']) ? filter_input(INPUT_GET, 'pid', FILTER_SANITIZE_STRING) : '';
424 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_SANITIZE_STRING) : '';
425 $bht03 = isset($_GET['bht03']) ? filter_input(INPUT_GET, 'bht03', FILTER_SANITIZE_STRING) : '';
426 $err = isset($_GET['err']) ? filter_input(INPUT_GET, 'err', FILTER_SANITIZE_STRING) : '';
427 $summary = isset($_GET['summary']) ? filter_input(INPUT_GET, 'summary', FILTER_SANITIZE_STRING) : false;
429 // debug
430 //$str_htm .= "<p>edih_disp_x12trans values: <br>".PHP_EOL;
431 //$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;
433 if ($ft) { $ft = csv_file_type($ft); }
435 if ($qs == 'claim') {
436 if ($ft == 'f997') {
437 if ($trace && $rsptype){
438 $fname = csv_file_by_trace($trace, $ft, $rsptype);
439 if ($fname) {
440 $str_htm .= edih_display_text($fname, $rsptype, $trace, $err);
441 } else {
442 $str_htm .= "<p>Did not find $trace in the $rsptype claims table.</p>";
444 //$fnar = csv_file_by_enctr($trace, $rsptype, $srchtype='ptidn' );
445 //if (is_array($fnar) && count($fnar)) {
446 //foreach($fnar as $fa) {
447 //$fname = $fa['FileName'];
448 //$str_htm .= edih_display_text($fname, $rsptype, $trace, $err);
450 //} else {
451 //$str_htm .= "<p>Did not find $trace in the $rsptype claims table.</p>";
454 } elseif ($ft == 'f837') {
455 // either transaction or file
456 $str_htm .= edih_display_text($fn, $ft, $clm01);
457 } elseif ($ft == 'f835') {
458 if ($fmt == 'seg') {
459 // either transaction or file
460 $str_htm .= edih_display_text($fn, $ft, $clm01);
461 } elseif ($trace) {
462 // the check trace
463 $str_htm .= edih_835_html($fn, $trace);
464 } elseif ($clm01) {
465 // this claim payment
466 $str_htm .= edih_835_html($fn, '', $clm01, $summary);
468 } elseif ( strpos('|f270|f271|f276|f277|f278', $ft) ) {
469 if ($fmt == 'seg') {
470 if ($trace && $rsptype) {
471 // 270|276|278|837 claim or request segments
472 // here the 'trace' is from trace or clm01
473 //$fnar = csv_file_by_enctr($trace, $rsptype, $srchtype='ptidn' );
474 //if (is_array($fnar) && count($fnar)) {
475 //foreach($fnar as $fa) {
476 //$fname = $fa['FileName'];
477 //$str_htm .= edih_display_text($fname, $rsptype, $trace);
479 $fname = csv_file_by_trace($trace, $ft, $rsptype);
480 if ($fname) {
481 $str_htm .= edih_display_text($fname, $rsptype, $trace);
482 } else {
483 $str_htm .= "<p>Did not find $trace in type $rsptype csv_claims table</p>".PHP_EOL;
484 csv_edihist_log("edih_disp_x12trans: Did not find $trace in type $rsptype csv_claims table");
486 } else {
487 // entire file or transaction if bht03 has a value
488 $str_htm .= edih_display_text($fn, $ft, $bht03);
490 } else {
491 // html format
492 if ($ft == 'f277') {
493 $str_htm .= edih_277_html($fn, $bht03);
494 } elseif ($ft == 'f271') {
495 $str_htm .= edih_271_html($fn, $bht03);
496 } elseif ($ft == 'f278') {
497 $str_htm .= edih_278_html($fn, $bht03);
498 } else {
499 // html display not available, use segments
500 $str_htm .= edih_display_text($fn, $ft, $bht03);
504 } elseif ($qs == 'hist') {
505 if ($fn && $ft == 'f837') {
506 if ($clm01) {
507 $str_htm .= edih_display_text($fn, $ft, $clm01);
508 } else {
509 $str_htm .= edih_display_text($fn, $ft);
511 } elseif ($fn && $ft == 'f997') {
512 if ($trace && $rsptype && $err) {
513 $str_htm .= edih_display_text($fn, $rsptype, $trace, true, $err);
514 } elseif ($trace && $rsptype) {
515 $str_htm .= edih_display_text($fn, $rsptype);
516 } else {
517 $str_htm .= edih_display_text($fn, $ft);
519 } elseif ($fn && $ft == 'f277') {
520 if ($trace && $rsptype) {
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 } elseif ($clm01) {
529 $str_htm .= edih_277_html($fn, $clm01);
530 } elseif ($bht03) {
531 $str_htm .= edih_277_html($fn, $bht03);
532 } else {
533 $str_htm .= edih_display_text($fn, $ft);
535 } elseif ($fn && $ft == 'f835') {
536 if ($clm01) {
537 if ($summary == 'yes') {
538 $str_htm .= edih_835_html($fn, '', $clm01, true);
539 } else {
540 $str_htm .= edih_835_html($fn, '', $clm01);
542 } elseif ($trace) {
543 $str_htm .= edih_835_html($fn, $trace);
546 } else {
547 $str_htm .= 'error: could not process request.';
550 return $str_htm;
554 * display fule uploaded from x12 File tab
555 * wrap individual transactions in accordian jquery ui widget
557 * @uses csv_check_x12_obj()
558 * @uses edih_html_heading()
559 * @uses edih_271_transaction_html()
560 * @uses edih_278_transaction_html()
561 * @uses edih_277_transaction_html()
562 * @uses edih_835_html_page()
563 * @uses edih_display_text()
565 * @param string path to x12 file
566 * @return string
568 function edih_disp_x12file() {
570 $str_htm = '';
571 $fn = $ft = $icn = $trace = $rsptype = $format = '';
573 if ( isset($_POST['x12_html']) ) {
574 $htmval = filter_input(INPUT_POST, 'x12_html', FILTER_SANITIZE_STRING);
575 $format = ($htmval == 'html') ? 'htm' : 'seg';
576 $upldir = csv_edih_tmpdir();
577 } else {
578 $format = 'seg';
580 // post request from x12 file tab
581 if ( count($_FILES) && isset($_FILES['fileUplx12']) ) {
582 $fnupl = htmlentities($_FILES['fileUplx12']['name']);
583 // the upload files validator
584 $f_array = edih_upload_files();
586 if ( is_array($f_array) && count($f_array) ) {
587 // was file rejected?
588 if ( isset($f_array['reject']) ) {
589 $fn = (count($f_array['reject'][0])) ? $f_array['reject'][0]['name'] : '';
590 $cmt = (count($f_array['reject'][0])) ? $f_array['reject'][0]['comment'] : '';
591 //$str_html = edih_html_heading('error');
592 $str_htm .= "<p>Rejected file:</p>".PHP_EOL;
593 $str_htm .= "<p>$fn<br>".PHP_EOL;
594 $str_htm .= " -- $cmt</p>".PHP_EOL;
596 csv_edihist_log("edih_disp_x12file: rejected file $fn comment: $cmt");
598 return $str_htm;
599 } else {
600 $fnar = reset($f_array); // type filename array
601 $ft = key($f_array); // type
602 $fn1 = $f_array[$ft][0]; //$upldir.DS.
603 $fn = csv_check_filepath($fn1);
604 csv_edihist_log("edih_disp_x12file: submitted POST $format $ft $fn1 $fnupl");
606 if (!$fn) {
607 //$str_htm = edih_html_heading('error');
608 $str_htm .= "<p>Path error for $fn1</p>" . PHP_EOL;
609 csv_edihist_log("edih_disp_x12file: Path error for $fn1");
610 return $str_htm;
613 } else {
614 //$str_htm = edih_html_heading('error');
615 $str_htm .= "<p>File not accepted $fnupl</p>" . PHP_EOL;
616 csv_edihist_log("edih_disp_x12file: File not accepted $fnupl");
617 return $str_htm;
619 } elseif ( isset($_GET['gtbl']) && $_GET['gtbl'] == 'file') {
620 // this is a GET request from csv files table
621 // assemble variables
622 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_SANITIZE_STRING) : '';
623 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_SANITIZE_STRING) : '';
624 $icn = isset($_GET['icn']) ? filter_input(INPUT_GET, 'icn', FILTER_SANITIZE_STRING) : '';
625 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_SANITIZE_STRING) : '';
626 $rsptype = isset($_GET['rsptype']) ? filter_input(INPUT_GET, 'rsptype', FILTER_SANITIZE_STRING) : '';
627 $format = isset($_GET['fmt']) ? filter_input(INPUT_GET, 'fmt', FILTER_SANITIZE_STRING) : '';
629 } else {
630 //$str_htm = edih_html_heading('error');
631 $str_htm .= "<p>Error: No request received by server</p>" . PHP_EOL;
632 csv_edihist_log("edih_disp_x12file: No request received by server");
633 return $str_htm;
636 if (!$fn) {
637 if ($ft && $icn) {
638 $fnr = csv_file_by_controlnum($ft, $icn);
639 $fn = csv_check_filepath($fnr);
640 } elseif ($ft && $trace && $rsptype) {
641 $fnr = csv_file_by_trace($trace, $ft, $rsptype);
642 $fn = csv_check_filepath($fnr);
643 $ft = $rsptype;
644 $trace = '';
645 } elseif ($ft == 'f835' && $trace) {
646 $fnr = csv_file_by_trace($trace, $ft, $rsptype);
647 $fn = csv_check_filepath($fnr);
648 } elseif ($ft == 'f997' && $trace && $rsptype) {
649 $fnr = csv_file_by_controlnum($rsptype, $trace);
650 $fn = csv_check_filepath($fnr);
651 $ft = $rsptype;
652 $trace = '';
653 if (!$fn) {
654 $str_htm .= "<p>997/999 Trace value $trace not found for type $rsptype</p>" . PHP_EOL;
655 csv_edihist_log("edih_disp_x12file: 997/999 Trace value $trace not found for type $rsptype");
656 return $str_htm;
661 if (!$fn) {
662 //$str_htm = edih_html_heading('error');
663 $str_htm .= "<p>Name error for file: type $ft icn $icn trace $trace rsp $rsptype</p>" . PHP_EOL;
664 csv_edihist_log("edih_disp_x12file: Name error for file: type $ft icn $icn trace $trace rsp $rsptype");
665 return $str_htm;
668 if ( $format == 'seg' ) {
669 if ($ft == 'f835' && $trace) {
670 $str_htm .= edih_display_text($fn, $ft, $trace, true);
671 } elseif ($icn) {
672 $str_htm .= edih_display_text($fn, $ft, $icn, true);
673 } else {
674 $str_htm .= edih_display_text($fn, $ft);
676 csv_edihist_log("edih_disp_x12file: segments display $fn");
678 return $str_htm;
680 // request is for html display
681 // now go through each file type
682 // 'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277',
683 // 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
684 if ($ft == 'f271' || $ft == 'f270') {
685 //$str_htm .= edih_html_heading('eligibility', $fn);
686 $str_htm .= edih_271_html($fn);
687 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
688 } elseif ($ft == 'f276' || $ft == 'f277') {
689 //$str_htm .= edih_html_heading('claimstatus', $fn);
690 $str_htm .= edih_277_html($fn);
691 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
692 } elseif ($ft == 'f278') {
693 //$str_htm .= edih_html_heading('claimstatus', $fn);
694 $str_htm .= edih_278_html($fn);
695 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
696 } elseif ($ft == 'f835') {
697 //$str_htm .= edih_html_heading('eradisplay', $fn);
698 $str_htm = edih_835_html($fn, $trace);
699 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
700 } else {
701 // no html format for this type
702 // object is created in edih_display_text function
703 // edih_display_text($filepath, $filetype='', $claimid='', $trace=false, $err_info='')
704 //$str_htm .= edih_html_heading('x12display', $fn);
705 $str_htm .= edih_display_text($fn, $ft);
706 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
709 return $str_htm;
714 * csv tables filter input and generate table
716 * @uses csv_to_html()
717 * @return string
719 function edih_disp_csvtable() {
721 $str_html = '';
722 $prd = (isset($_GET['csv_period'])) ? filter_input(INPUT_GET, 'csv_period', FILTER_SANITIZE_STRING) : '';
723 $dts = (isset($_GET['csv_date_start'])) ? filter_input(INPUT_GET, 'csv_date_start', FILTER_SANITIZE_NUMBER_INT) : '';
724 $dte = (isset($_GET['csv_date_end'])) ? filter_input(INPUT_GET, 'csv_date_end', FILTER_SANITIZE_NUMBER_INT) : '';
725 $csvfile = (isset($_GET['csvtables'])) ? filter_input(INPUT_GET, 'csvtables', FILTER_SANITIZE_STRING) : '';
727 // debug
728 csv_edihist_log("edih_disp_csvtable: $csvfile period $prd datestart $dts dateend $dte");
730 if ( $dts && strpos($dts, '-') != 4 ) {
731 if ( strlen($_GET['csv_date_start']) == 10 && strpos($_GET['csv_date_start'], '/') == 4 ) {
732 $dts = str_replace('/', '-', $dts);
733 } else {
734 $str_html = "<p>Date $dts must be in YYYY-MM-DD format, no / or . please</p>".PHP_EOL;
735 csv_edihist_log("invalid date $dts submitted for csv_table filter");
736 return $str_html;
739 if ( $dte && strpos($dte, '-') != 4 ) {
740 if ( strlen($_GET['csv_date_end']) == 10 && strpos($_GET['csv_date_end'], '/') == 4 ) {
741 $dte = str_replace('/', '-', $dte);
742 } else {
743 $dte = '';
746 if ( !$csvfile || $csvfile == NULL || $csvfile === FALSE ) {
747 // here we have an error and must quit
748 $str_html = "<p>Error in CSV table name </p>".PHP_EOL;
749 return $str_html;
750 } else {
751 $tp_ar = explode('_', $csvfile);
752 $tbl_type = ($tp_ar[0] == 'claims') ? 'claim' : 'file';
753 $f_type = strval($tp_ar[1]);
754 if ( ctype_digit($f_type) ) {
755 $f_type = 'f'.$f_type;
758 $str_html = edih_csv_to_html($f_type, $tbl_type, $prd, $dts, $dte);
760 return $str_html;
765 * Report 835 file as processed by billing routine if the file name
766 * is found in the 'era' directory. The file name is a concatenation
767 * of GS04_TRN04_ISA13.edi per parse_era_inc.php
770 * @param string
771 * @return bool
773 function edih_disp_835_processed($erasavename) {
774 // openemr/interface/billing/era_payments.php
775 // openemr/library/parse_era.inc.php
776 // OpenEMR filename for era should be just the upload filename or
777 // $out['gs_date'] . '_' . $out['payer_id'] . '_' .$out['isa_control_number']
778 // with 'payer_id' taken from BPR10 or TRN03 (same value) and not from TRN04
780 // search for YYYYMMDD_NNNNNNNN_ISA13
781 $eraname = $out['gs_date'] . '_' . ltrim($out['isa_control_number'], '0') .
782 '_' . ltrim($out['payer_id'], '0');
783 $out['payer_id'] = trim($seg[4]); //TRN04
785 $srchlen = strlen($erasavename);
786 $found = false;
787 $eradir = $GLOBALS['OE_SITE_DIR'].DS.'edi'.DS.'era';
789 if ($hd = opendir($eradir)) {
790 while (false !== ($entry = readdir($hd))) {
791 if (strncmp($entry, $erasavename, $srchlen) === 0 ) {
792 $found = true;
793 break;
796 closedir($hd);
797 } else {
798 csv_edihist_log("edih_disp_processed_835: did not find processed era directory");
800 return $found;
803 function edih_disp_clmhist() {
805 if ( isset($_GET['hist_enctr']) ) {
806 $enctr = filter_input(INPUT_GET, 'hist_enctr', FILTER_SANITIZE_STRING);
807 if ($enctr) {
808 $str_html = edih_claim_history($enctr);
809 } else {
810 $str_html = "Invalid or unknown encounter number".PHP_EOL;
812 } else {
813 $str_html = "Invalid or unknown encounter number".PHP_EOL;
815 return $str_html;
820 * display the message part of a 999 response
822 * @uses ibr_997_errscan()
823 * @return string
825 function ibr_disp_997_message() {
827 $fname = ''; $akval = ''; $errval = '';
828 $fname = filter_input(INPUT_GET, 'fv997', FILTER_SANITIZE_STRING);
829 if (isset($_GET['aknum'])) { $akval = filter_input(INPUT_GET, 'aknum', FILTER_SANITIZE_STRING); }
830 if (isset($_GET['err997'])) { $errval = filter_input(INPUT_GET, 'err997', FILTER_SANITIZE_STRING); }
831 if (!$fname) {
832 $str_html = "Missing file name.<br />".PHP_EOL;
833 } else {
834 $str_html = ibr_997_errscan($fname, $akval);
836 return $str_html;
840 * display the message part of a ACK or TA1 response
842 * @uses ibr_ack_error()
843 * @return string
845 function ibr_disp_ta1_message() {
847 $fname = ''; $code = '';
848 $fname = filter_input(INPUT_GET, 'ackfile', FILTER_SANITIZE_STRING);
849 if (isset($_GET['ackcode'])) $code = filter_input(INPUT_GET, 'ackcode', FILTER_SANITIZE_STRING);
850 if ($fname && $code) {
851 $str_html = ibr_ack_error($fname, $code);
852 } else {
853 $str_html = 'Code value invalid <br />'.PHP_EOL;
855 return $str_html;
860 * check if the batch control number is found in the 997/999 files table
862 * @uses csv_search_record()
863 * @return string
865 function ibr_disp_997_for_batch() {
866 $str_html = '';
867 $batch_icn = filter_input(INPUT_GET, 'batchicn', FILTER_SANITIZE_STRING);
868 if ($batch_icn) {
869 $ctln = (strlen($batch_icn) >= 9) ? substr($batch_icn, 0, 9) : trim(strval($batch_icn));
870 $search = array('s_val'=>$ctln, 's_col'=>3, 'r_cols'=>'all');
871 $result = csv_search_record('f997', 'file', $search, "1");
873 // should be matching row(s) from files_997.csv
874 if (is_array($result) && count($result)) {
875 $str_html .= "<p>Acknowledgement information</p>".PHP_EOL;
876 foreach($result as $rslt) {
877 $ext = substr($rslt[1], -3);
879 $str_html .= "Date: {$rslt[0]} <br />".PHP_EOL;
880 $str_html .= "File: <a target=\"blank\" href=edi_history_main.php?fvkey={$rslt[1]}>{$rslt[1]}</a> <br />".PHP_EOL;
881 $str_html .= "Batch ICN: {$rslt[3]} <br />";
882 // error count or code in position 4
883 if ($ext == '999' || $ext == '997') {
884 $str_html .= "Rejects: {$rslt[4]} <br />".PHP_EOL;
885 // don't have dialog from this dialog, so don't link
886 //$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;
887 } elseif ($ext == 'ta1' || $ext == 'ack') {
888 $str_html .= "Code: {$rslt[4]} <br />".PHP_EOL;
889 //$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;
892 } else {
893 $str_html .= "Did not find corresponding 997/999 file for $ctln<br />".PHP_EOL;
895 } else {
896 $str_html .= "Invalid value for ICN number<br />".PHP_EOL;
898 return $str_html;
902 * function to check whether an era payment has been processed and applied
904 * @uses sqlQuery()
906 * @return string
908 function edih_disp_era_processed() {
910 $str_html = '';
911 $ckno = filter_input(INPUT_GET, 'tracecheck', FILTER_SANITIZE_STRING);
912 if ($ckno) {
913 $srchval = 'ePay - '.$ckno;
914 // reference like '%".$srchval."%'"
915 $row = sqlQuery("SELECT reference, pay_total, global_amount FROM ar_session WHERE reference = ?", array($srchval) );
916 if (!empty($row)) {
917 $str_html .= "trace {$row['reference']} total \${$row['pay_total']}";
918 if ($row['global_amount'] === '0') {
919 $str_html .= " fully allocated";
920 } else {
921 $str_html .= " ({$row['global_amount']} not allocated)";
923 } else {
924 $str_html .= "trace $ckno not posted";
926 } else {
927 $str_html .= "trace $ckno not found";
929 return $str_html;