net sftp bug fix
[openemr.git] / library / edihistory / edih_io.php
blobad31a0ebbd8b3db9bf08519f684b34388d667cb5
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'];
127 $vendordir = $GLOBALS['assets_static_relative'];
129 $str_html = "<!DOCTYPE html>".PHP_EOL."<html>".PHP_EOL."<head>".PHP_EOL;
130 $str_html .= " <meta http-equiv='content-type' content='text/html;charset=utf-8' />".PHP_EOL;
131 $str_html .= " <title>##TITLE##</title>".PHP_EOL;
132 //$str_html .= " <link rel='stylesheet' href='jscript/style/csv_new.css' type='text/css' media='print, projection, screen' />".PHP_EOL;
133 //$str_html .= " <link rel='stylesheet' href='../css/edi_history.css' type='text/css' />".PHP_EOL;
134 $str_html .= " <link rel='stylesheet' href='$webdir/library/css/edi_history.css' type='text/css' />".PHP_EOL;
135 $str_html .= " <link type='text/javascript' src='$vendordir/jquery-min-1-9-1/index.js' />".PHP_EOL;
136 $str_html .= " <link type='text/javascript' src='$webdir/library/js/jquery-ui-1.8.21.custom.min.js' />".PHP_EOL;
138 $str_html .= "</head>".PHP_EOL."<body>".PHP_EOL;
140 if ($option == 'newfiles') {
141 $str_html = str_replace('##TITLE##', 'Process New Files '.$title, $str_html);
142 } elseif ($option == 'eradisplay') {
143 $str_html = str_replace('##TITLE##', 'ERA Display '.$title, $str_html);
144 } elseif ($option == 'claimstatus') {
145 $str_html = str_replace('##TITLE##', 'Claim Status '.$title, $str_html);
146 } elseif ($option == 'eligibility') {
147 $str_html = str_replace('##TITLE##', 'Eligiility '.$title, $str_html);
148 } elseif ($option == 'authorization') {
149 $str_html = str_replace('##TITLE##', 'Authorization '.$title, $str_html);
150 } elseif ($option == 'x12display') {
151 $str_html = str_replace('##TITLE##', 'x12 File '.$title, $str_html);
152 } elseif ($option == 'csvtable') {
153 $str_html = str_replace('##TITLE##', 'CSV Table '.$title, $str_html);
154 } elseif ($option == 'textdisplay') {
155 $str_html = str_replace('##TITLE##', 'Text '.$title, $str_html);
156 } elseif ($option == 'readme') {
157 $str_html = str_replace('##TITLE##', 'Readme '.$title, $str_html);
158 } else {
159 $str_html = str_replace('##TITLE##', 'OEMR edi_history '.$title, $str_html);
162 return $str_html;
166 * generate the trailing tags for html page
168 * @return string
170 function edih_html_tail() {
171 $str_html = PHP_EOL."</body></html>";
172 return $str_html;
177 * Restore an existing archive
179 * @uses edih_archive_restore()
181 * @return string
183 function edih_disp_archive_restore() {
184 //name="archrestore_sel" { archrestore: 'yes', archfile: archf };
185 $fn = (isset($_POST['archrestore_sel'])) ? filter_input(INPUT_POST, 'archrestore_sel', FILTER_SANITIZE_STRING) : '';
186 if (strlen($fn)) {
187 $str_html = edih_archive_restore($fn);
188 } else {
189 $str_html = "<p>Invalid archive name for archive resstore function</p>".PHP_EOL;
191 return $str_html;
195 * Create a report on edi files and csv tables
197 * @uses edih_archive_report()
199 * @return string
201 function edih_disp_archive_report() {
203 $str_html = '';
204 $la = filter_input(INPUT_GET, 'archivereport', FILTER_SANITIZE_STRING);
205 $pd = (isset($_GET['period'])) ? filter_input(INPUT_GET, 'period', FILTER_SANITIZE_STRING) : '';
207 csv_edihist_log("GET archivereport: archivereport $la period $pd");
209 if ($la == 'yes') {
210 $str_html = edih_archive_report($pd);
211 } else {
212 $str_html = "File Information report input parameter error<br>";
215 return $str_html;
220 * Archive of old edi files
222 * @uses edih_archive_main()
224 * @return string
226 function edih_disp_archive() {
228 $pd = (isset($_POST['archive_sel'])) ? filter_input(INPUT_POST, 'archive_sel', FILTER_SANITIZE_STRING) : '';
230 if ($pd) {
231 $str_html = edih_archive_main($pd);
232 } else {
233 $str_html = "<p>Invalid aging period for archive function</p>".PHP_EOL;
235 return $str_html;
239 * call new uploaded files process functions
241 * @todo save the newfiles lists to file so they can
242 * be re-displayed if user has to close app before
243 * finishing review (need to have csv_write option)
245 * @uses csv_newfile_list()
246 * @uses edih_parse_select()
247 * @uses edih_csv_write()
248 * @uses edih_csv_process_html()
250 * @return string html format
252 function edih_disp_file_process() {
253 //file "fileUplMulti "submit" "uplsubmit" "button""uplreset"
255 // debug
256 if (isset($_GET)) {
257 $dbg_str = 'GET vars ';
258 foreach($_GET as $k=>$v) {
259 $dbg_str .= $k.' '.$v.' ';
261 csv_edihist_log("edih_disp_file_process $dbg_str");
264 if (!isset($_GET['ProcessFiles']) ) {
265 // should only be called with this value existing
266 $str_html = "Error: invalid value for Process New <br />".PHP_EOL;
267 return $str_html;
269 $htm = $er = false;
270 if (isset($_GET['process_html'])) {
271 // show tables for process results
272 $htmval = filter_input(INPUT_GET, 'process_html', FILTER_SANITIZE_STRING);
273 $htm = ($htmval == 'htm') ? true : false;
275 if (isset($_GET['process_err'])) {
276 // show only claims with errors (denied, rejected, etc)
277 $errval = filter_input(INPUT_GET, 'process_err', FILTER_SANITIZE_STRING);
278 $er = ($errval == 'err') ? true : false;
280 $str_html = "";
282 $p = csv_parameters();
283 $ftype = array_keys($p);
284 $fct = 0;
286 foreach($ftype as $tp) {
287 $checkdir = false;
288 // check for directory contents
289 $fdir = $p[$tp]['directory'];
290 if (is_dir($fdir)) {
291 $dh = opendir($fdir);
292 if ($dh) {
293 while (($file = readdir($dh)) !== false) {
294 if ($file != '.' && $file != '..') {
295 $checkdir = true;
296 break;
299 closedir($dh);
302 // if false, no files in directory
303 if (!$checkdir) { continue; }
305 $upload_ar = csv_newfile_list($tp);
307 if ( is_array($upload_ar) && count($upload_ar) ) {
308 $dirct = count($upload_ar);
309 if ($htm) {
310 $dtl = ($er) ? "(claims: errors only)" : "(claims: all)";
311 //$hvals = csv_table_header($tp, 'file');
312 //$col_ct = count($hvals);
313 ////csv_table_header(
314 //$str_html .= "<table class='$tp' cols=$col_ct><caption>$tp Files Summary $dtl</caption>".PHP_EOL;
315 //$str_html .= csv_thead_html($tp, 'file');
316 //$str_html .= "<tbody>".PHP_EOL;
317 $str_html .= "<h2 class='prcs'>$tp $dirct files $dtl</h2>".PHP_EOL;
318 $str_html .= "<dl class='$tp'>".PHP_EOL;
320 foreach($upload_ar as $fn) {
321 $fp = $fdir.DS.$fn;
322 $csvdata = edih_parse_select($fp);
323 $csvchr = edih_csv_write($csvdata);
324 $fct++;
325 if ($htm) {
326 $str_html .= edih_csv_process_html($csvdata, $er);
329 //$str_html .= ($htm) ? "</tbody>".PHP_EOL."</table>".PHP_EOL : "";
330 $str_html .= ($htm) ? "</dl>".PHP_EOL : "";
331 } else {
332 $str_html .= "<p>No new $tp files</p>";
335 $capt_html = "<p>Process new files ($fct files)</p>".PHP_EOL;
336 return $capt_html . $str_html;
340 * uploading of new files
342 * @uses edih_upload_files()
343 * @uses edih_sort_upload()
344 * @return string
346 function edih_disp_file_upload() {
347 // multiple file upload
348 $str_html = '';
349 if ( isset($_FILES) && count($_FILES) ) {
350 $f_array = edih_upload_files();
351 if ( is_array($f_array) && count($f_array) ) {
352 $str_html .= edih_sort_upload($f_array);
353 } else {
354 $str_html .= "no files accepted <br />".PHP_EOL;
356 } else {
357 $str_html .= "no files submitted <br />" . PHP_EOL;
360 return $str_html;
363 function edih_disp_denied_claims() {
365 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_SANITIZE_STRING) : '';
366 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_SANITIZE_STRING) : '';
367 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_SANITIZE_STRING) : '';
369 $str_html = edih_list_denied_claims($ft, $fn, $trace);
371 return $str_html;
375 * display the contents of an x12_edi transaction selected from
376 * a csv table or processed files table
378 * @uses csv_file_by_enctr()
379 * @uses csv_file_by_controlnum()
380 * @uses ibr_batch_get_st_block()
381 * @return string
383 function edih_disp_x12trans() {
385 // query source ['gtbl'] file claim hist
387 // file: FileName fname=$fn1&ftype=$ft&fmt=htm' filename x12type format
388 // file: Control fname=$fn1&ftype=$ft&icn=$ctl&fmt=seg filename x12type isa13 format
389 // file: Trace trace=$tr&ftype=$typ&fmt=htm trace x12type format
391 // claim: FileName fname=$fn1&ftype=$ft&fmt=htm filename x12type format:html
392 // claim: Control fname=$fn1&ftype=$ft&icn=$ctl&fmt=seg filename x12type icn format:segment text
393 // claim: CLM01 fname=$fn1&ftype=$ft&pid=$pid filename x12type pid-enctr
394 // claim: Status fname=fname=$fn1&ftype=$ft&pid=$pid&summary=yes filename x12type pid-enctr summary
395 // claim: Status fname=$fn1&ftype=$ft&pid=$pid&summary=no' filename x12type pid-enctr detail
396 // claim: Trace (835) fname=$fn1&ftype=$ft&trace=$trc trace filename x12type
397 // claim: Trace (999) trace=$trc&rsptype=$typ&ftype=$ft trace(bht03syn) response-type x12type
398 // claim: Trace (277) trace=$v&ftype=$tp&rsptype=f837&fmt=seg' trace(clm01) response-type {837) x12type
399 // claim: BHT03 (27x) fname=$fn1&ftype=$ft&bht03=$bht03&fmt=htm filename x12type bht03
400 // claim: err_seg fname=$fn1&ftype=$ft&trace=$trc&rsptype=$typ&err=$err filename x12type trace(bht03syn) response_type error_segment
402 // use files (1) x12 display of file segments (2) 835 html RA or Payment Trace (3) trace from 997 or 271/277/278
403 // $fn or $icn & $ft $fn $icn $trace & $ft $trace & $rsptype
404 // claims (1) html of transaction (2) segments of transaction (3) trace to precedent transaction
405 // $fn & $ft $ pid $trace & $rsptype
407 $str_htm = '';
408 if ( isset($_GET['gtbl']) ) {
409 $qs = filter_input(INPUT_GET, 'gtbl', FILTER_SANITIZE_STRING);
411 if (!$qs) {
412 $str_htm .= '<p>edih_disp_x12 error: missing parameter</p>';
413 csv_edihist_log("edih_io_disp_x12: missing parameter, no 'gtbl' value");
414 return $str_htm;
417 $fmt = isset($_GET['fmt']) ? filter_input(INPUT_GET, 'fmt', FILTER_SANITIZE_STRING) : '';
419 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_SANITIZE_STRING) : '';
420 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_SANITIZE_STRING) : '';
421 $icn = isset($_GET['icn']) ? filter_input(INPUT_GET, 'icn', FILTER_SANITIZE_STRING) : '';
422 $rsptype = isset($_GET['rsptype']) ? filter_input(INPUT_GET, 'rsptype', FILTER_SANITIZE_STRING) : '';
424 $clm01 = isset($_GET['pid']) ? filter_input(INPUT_GET, 'pid', FILTER_SANITIZE_STRING) : '';
425 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_SANITIZE_STRING) : '';
426 $bht03 = isset($_GET['bht03']) ? filter_input(INPUT_GET, 'bht03', FILTER_SANITIZE_STRING) : '';
427 $err = isset($_GET['err']) ? filter_input(INPUT_GET, 'err', FILTER_SANITIZE_STRING) : '';
428 $summary = isset($_GET['summary']) ? filter_input(INPUT_GET, 'summary', FILTER_SANITIZE_STRING) : false;
430 // debug
431 //$str_htm .= "<p>edih_disp_x12trans values: <br>".PHP_EOL;
432 //$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;
434 if ($ft) { $ft = csv_file_type($ft); }
436 if ($qs == 'claim') {
437 if ($ft == 'f997') {
438 if ($trace && $rsptype){
439 $fname = csv_file_by_trace($trace, $ft, $rsptype);
440 if ($fname) {
441 $str_htm .= edih_display_text($fname, $rsptype, $trace, $err);
442 } else {
443 $str_htm .= "<p>Did not find $trace in the $rsptype claims table.</p>";
445 //$fnar = csv_file_by_enctr($trace, $rsptype, $srchtype='ptidn' );
446 //if (is_array($fnar) && count($fnar)) {
447 //foreach($fnar as $fa) {
448 //$fname = $fa['FileName'];
449 //$str_htm .= edih_display_text($fname, $rsptype, $trace, $err);
451 //} else {
452 //$str_htm .= "<p>Did not find $trace in the $rsptype claims table.</p>";
455 } elseif ($ft == 'f837') {
456 // either transaction or file
457 $str_htm .= edih_display_text($fn, $ft, $clm01);
458 } elseif ($ft == 'f835') {
459 if ($fmt == 'seg') {
460 // either transaction or file
461 $str_htm .= edih_display_text($fn, $ft, $clm01);
462 } elseif ($trace) {
463 // the check trace
464 $str_htm .= edih_835_html($fn, $trace);
465 } elseif ($clm01) {
466 // this claim payment
467 $str_htm .= edih_835_html($fn, '', $clm01, $summary);
469 } elseif ( strpos('|f270|f271|f276|f277|f278', $ft) ) {
470 if ($fmt == 'seg') {
471 if ($trace && $rsptype) {
472 // 270|276|278|837 claim or request segments
473 // here the 'trace' is from trace or clm01
474 //$fnar = csv_file_by_enctr($trace, $rsptype, $srchtype='ptidn' );
475 //if (is_array($fnar) && count($fnar)) {
476 //foreach($fnar as $fa) {
477 //$fname = $fa['FileName'];
478 //$str_htm .= edih_display_text($fname, $rsptype, $trace);
480 $fname = csv_file_by_trace($trace, $ft, $rsptype);
481 if ($fname) {
482 $str_htm .= edih_display_text($fname, $rsptype, $trace);
483 } else {
484 $str_htm .= "<p>Did not find $trace in type $rsptype csv_claims table</p>".PHP_EOL;
485 csv_edihist_log("edih_disp_x12trans: Did not find $trace in type $rsptype csv_claims table");
487 } else {
488 // entire file or transaction if bht03 has a value
489 $str_htm .= edih_display_text($fn, $ft, $bht03);
491 } else {
492 // html format
493 if ($ft == 'f277') {
494 $str_htm .= edih_277_html($fn, $bht03);
495 } elseif ($ft == 'f271') {
496 $str_htm .= edih_271_html($fn, $bht03);
497 } elseif ($ft == 'f278') {
498 $str_htm .= edih_278_html($fn, $bht03);
499 } else {
500 // html display not available, use segments
501 $str_htm .= edih_display_text($fn, $ft, $bht03);
505 } elseif ($qs == 'hist') {
506 if ($fn && $ft == 'f837') {
507 if ($clm01) {
508 $str_htm .= edih_display_text($fn, $ft, $clm01);
509 } else {
510 $str_htm .= edih_display_text($fn, $ft);
512 } elseif ($fn && $ft == 'f997') {
513 if ($trace && $rsptype && $err) {
514 $str_htm .= edih_display_text($fn, $rsptype, $trace, true, $err);
515 } elseif ($trace && $rsptype) {
516 $str_htm .= edih_display_text($fn, $rsptype);
517 } else {
518 $str_htm .= edih_display_text($fn, $ft);
520 } elseif ($fn && $ft == 'f277') {
521 if ($trace && $rsptype) {
522 $fname = csv_file_by_trace($trace, $ft, $rsptype);
523 if ($fname) {
524 $str_htm .= edih_display_text($fname, $rsptype, $trace);
525 } else {
526 $str_htm .= "<p>Did not find $trace in type $rsptype csv_claims table</p>".PHP_EOL;
527 csv_edihist_log("edih_disp_x12trans: Did not find $trace in type $rsptype csv_claims table");
529 } elseif ($clm01) {
530 $str_htm .= edih_277_html($fn, $clm01);
531 } elseif ($bht03) {
532 $str_htm .= edih_277_html($fn, $bht03);
533 } else {
534 $str_htm .= edih_display_text($fn, $ft);
536 } elseif ($fn && $ft == 'f835') {
537 if ($clm01) {
538 if ($summary == 'yes') {
539 $str_htm .= edih_835_html($fn, '', $clm01, true);
540 } else {
541 $str_htm .= edih_835_html($fn, '', $clm01);
543 } elseif ($trace) {
544 $str_htm .= edih_835_html($fn, $trace);
547 } else {
548 $str_htm .= 'error: could not process request.';
551 return $str_htm;
555 * display fule uploaded from x12 File tab
556 * wrap individual transactions in accordian jquery ui widget
558 * @uses csv_check_x12_obj()
559 * @uses edih_html_heading()
560 * @uses edih_271_transaction_html()
561 * @uses edih_278_transaction_html()
562 * @uses edih_277_transaction_html()
563 * @uses edih_835_html_page()
564 * @uses edih_display_text()
566 * @param string path to x12 file
567 * @return string
569 function edih_disp_x12file() {
571 $str_htm = '';
572 $fn = $ft = $icn = $trace = $rsptype = $format = '';
574 if ( isset($_POST['x12_html']) ) {
575 $htmval = filter_input(INPUT_POST, 'x12_html', FILTER_SANITIZE_STRING);
576 $format = ($htmval == 'html') ? 'htm' : 'seg';
577 $upldir = csv_edih_tmpdir();
578 } else {
579 $format = 'seg';
581 // post request from x12 file tab
582 if ( count($_FILES) && isset($_FILES['fileUplx12']) ) {
583 $fnupl = htmlentities($_FILES['fileUplx12']['name']);
584 // the upload files validator
585 $f_array = edih_upload_files();
587 if ( is_array($f_array) && count($f_array) ) {
588 // was file rejected?
589 if ( isset($f_array['reject']) ) {
590 $fn = (count($f_array['reject'][0])) ? $f_array['reject'][0]['name'] : '';
591 $cmt = (count($f_array['reject'][0])) ? $f_array['reject'][0]['comment'] : '';
592 //$str_html = edih_html_heading('error');
593 $str_htm .= "<p>Rejected file:</p>".PHP_EOL;
594 $str_htm .= "<p>$fn<br>".PHP_EOL;
595 $str_htm .= " -- $cmt</p>".PHP_EOL;
597 csv_edihist_log("edih_disp_x12file: rejected file $fn comment: $cmt");
599 return $str_htm;
600 } else {
601 $fnar = reset($f_array); // type filename array
602 $ft = key($f_array); // type
603 $fn1 = $f_array[$ft][0]; //$upldir.DS.
604 $fn = csv_check_filepath($fn1);
605 csv_edihist_log("edih_disp_x12file: submitted POST $format $ft $fn1 $fnupl");
607 if (!$fn) {
608 //$str_htm = edih_html_heading('error');
609 $str_htm .= "<p>Path error for $fn1</p>" . PHP_EOL;
610 csv_edihist_log("edih_disp_x12file: Path error for $fn1");
611 return $str_htm;
614 } else {
615 //$str_htm = edih_html_heading('error');
616 $str_htm .= "<p>File not accepted $fnupl</p>" . PHP_EOL;
617 csv_edihist_log("edih_disp_x12file: File not accepted $fnupl");
618 return $str_htm;
620 } elseif ( isset($_GET['gtbl']) && $_GET['gtbl'] == 'file') {
621 // this is a GET request from csv files table
622 // assemble variables
623 $fn = isset($_GET['fname']) ? filter_input(INPUT_GET, 'fname', FILTER_SANITIZE_STRING) : '';
624 $ft = isset($_GET['ftype']) ? filter_input(INPUT_GET, 'ftype', FILTER_SANITIZE_STRING) : '';
625 $icn = isset($_GET['icn']) ? filter_input(INPUT_GET, 'icn', FILTER_SANITIZE_STRING) : '';
626 $trace = isset($_GET['trace']) ? filter_input(INPUT_GET, 'trace', FILTER_SANITIZE_STRING) : '';
627 $rsptype = isset($_GET['rsptype']) ? filter_input(INPUT_GET, 'rsptype', FILTER_SANITIZE_STRING) : '';
628 $format = isset($_GET['fmt']) ? filter_input(INPUT_GET, 'fmt', FILTER_SANITIZE_STRING) : '';
630 } else {
631 //$str_htm = edih_html_heading('error');
632 $str_htm .= "<p>Error: No request received by server</p>" . PHP_EOL;
633 csv_edihist_log("edih_disp_x12file: No request received by server");
634 return $str_htm;
637 if (!$fn) {
638 if ($ft && $icn) {
639 $fnr = csv_file_by_controlnum($ft, $icn);
640 $fn = csv_check_filepath($fnr);
641 } elseif ($ft && $trace && $rsptype) {
642 $fnr = csv_file_by_trace($trace, $ft, $rsptype);
643 $fn = csv_check_filepath($fnr);
644 $ft = $rsptype;
645 $trace = '';
646 } elseif ($ft == 'f835' && $trace) {
647 $fnr = csv_file_by_trace($trace, $ft, $rsptype);
648 $fn = csv_check_filepath($fnr);
649 } elseif ($ft == 'f997' && $trace && $rsptype) {
650 $fnr = csv_file_by_controlnum($rsptype, $trace);
651 $fn = csv_check_filepath($fnr);
652 $ft = $rsptype;
653 $trace = '';
654 if (!$fn) {
655 $str_htm .= "<p>997/999 Trace value $trace not found for type $rsptype</p>" . PHP_EOL;
656 csv_edihist_log("edih_disp_x12file: 997/999 Trace value $trace not found for type $rsptype");
657 return $str_htm;
662 if (!$fn) {
663 //$str_htm = edih_html_heading('error');
664 $str_htm .= "<p>Name error for file: type $ft icn $icn trace $trace rsp $rsptype</p>" . PHP_EOL;
665 csv_edihist_log("edih_disp_x12file: Name error for file: type $ft icn $icn trace $trace rsp $rsptype");
666 return $str_htm;
669 if ( $format == 'seg' ) {
670 if ($ft == 'f835' && $trace) {
671 $str_htm .= edih_display_text($fn, $ft, $trace, true);
672 } elseif ($icn) {
673 $str_htm .= edih_display_text($fn, $ft, $icn, true);
674 } else {
675 $str_htm .= edih_display_text($fn, $ft);
677 csv_edihist_log("edih_disp_x12file: segments display $fn");
679 return $str_htm;
681 // request is for html display
682 // now go through each file type
683 // 'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277',
684 // 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
685 if ($ft == 'f271' || $ft == 'f270') {
686 //$str_htm .= edih_html_heading('eligibility', $fn);
687 $str_htm .= edih_271_html($fn);
688 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
689 } elseif ($ft == 'f276' || $ft == 'f277') {
690 //$str_htm .= edih_html_heading('claimstatus', $fn);
691 $str_htm .= edih_277_html($fn);
692 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
693 } elseif ($ft == 'f278') {
694 //$str_htm .= edih_html_heading('claimstatus', $fn);
695 $str_htm .= edih_278_html($fn);
696 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
697 } elseif ($ft == 'f835') {
698 //$str_htm .= edih_html_heading('eradisplay', $fn);
699 $str_htm = edih_835_html($fn, $trace);
700 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
701 } else {
702 // no html format for this type
703 // object is created in edih_display_text function
704 // edih_display_text($filepath, $filetype='', $claimid='', $trace=false, $err_info='')
705 //$str_htm .= edih_html_heading('x12display', $fn);
706 $str_htm .= edih_display_text($fn, $ft);
707 //$str_htm .= "</body>".PHP_EOL."</html>".PHP_EOL;
710 return $str_htm;
715 * csv tables filter input and generate table
717 * @uses csv_to_html()
718 * @return string
720 function edih_disp_csvtable() {
722 $str_html = '';
723 $prd = (isset($_GET['csv_period'])) ? filter_input(INPUT_GET, 'csv_period', FILTER_SANITIZE_STRING) : '';
724 $dts = (isset($_GET['csv_date_start'])) ? filter_input(INPUT_GET, 'csv_date_start', FILTER_SANITIZE_NUMBER_INT) : '';
725 $dte = (isset($_GET['csv_date_end'])) ? filter_input(INPUT_GET, 'csv_date_end', FILTER_SANITIZE_NUMBER_INT) : '';
726 $csvfile = (isset($_GET['csvtables'])) ? filter_input(INPUT_GET, 'csvtables', FILTER_SANITIZE_STRING) : '';
728 // debug
729 csv_edihist_log("edih_disp_csvtable: $csvfile period $prd datestart $dts dateend $dte");
731 if ( $dts && strpos($dts, '-') != 4 ) {
732 if ( strlen($_GET['csv_date_start']) == 10 && strpos($_GET['csv_date_start'], '/') == 4 ) {
733 $dts = str_replace('/', '-', $dts);
734 } else {
735 $str_html = "<p>Date $dts must be in YYYY-MM-DD format, no / or . please</p>".PHP_EOL;
736 csv_edihist_log("invalid date $dts submitted for csv_table filter");
737 return $str_html;
740 if ( $dte && strpos($dte, '-') != 4 ) {
741 if ( strlen($_GET['csv_date_end']) == 10 && strpos($_GET['csv_date_end'], '/') == 4 ) {
742 $dte = str_replace('/', '-', $dte);
743 } else {
744 $dte = '';
747 if ( !$csvfile || $csvfile == NULL || $csvfile === FALSE ) {
748 // here we have an error and must quit
749 $str_html = "<p>Error in CSV table name </p>".PHP_EOL;
750 return $str_html;
751 } else {
752 $tp_ar = explode('_', $csvfile);
753 $tbl_type = ($tp_ar[0] == 'claims') ? 'claim' : 'file';
754 $f_type = strval($tp_ar[1]);
755 if ( ctype_digit($f_type) ) {
756 $f_type = 'f'.$f_type;
759 $str_html = edih_csv_to_html($f_type, $tbl_type, $prd, $dts, $dte);
761 return $str_html;
766 * Report 835 file as processed by billing routine if the file name
767 * is found in the 'era' directory. The file name is a concatenation
768 * of GS04_TRN04_ISA13.edi per parse_era_inc.php
771 * @param string
772 * @return bool
774 function edih_disp_835_processed($erasavename) {
775 // openemr/interface/billing/era_payments.php
776 // openemr/library/parse_era.inc.php
777 // OpenEMR filename for era should be just the upload filename or
778 // $out['gs_date'] . '_' . $out['payer_id'] . '_' .$out['isa_control_number']
779 // with 'payer_id' taken from BPR10 or TRN03 (same value) and not from TRN04
781 // search for YYYYMMDD_NNNNNNNN_ISA13
782 $eraname = $out['gs_date'] . '_' . ltrim($out['isa_control_number'], '0') .
783 '_' . ltrim($out['payer_id'], '0');
784 $out['payer_id'] = trim($seg[4]); //TRN04
786 $srchlen = strlen($erasavename);
787 $found = false;
788 $eradir = $GLOBALS['OE_SITE_DIR'].DS.'edi'.DS.'era';
790 if ($hd = opendir($eradir)) {
791 while (false !== ($entry = readdir($hd))) {
792 if (strncmp($entry, $erasavename, $srchlen) === 0 ) {
793 $found = true;
794 break;
797 closedir($hd);
798 } else {
799 csv_edihist_log("edih_disp_processed_835: did not find processed era directory");
801 return $found;
804 function edih_disp_clmhist() {
806 if ( isset($_GET['hist_enctr']) ) {
807 $enctr = filter_input(INPUT_GET, 'hist_enctr', FILTER_SANITIZE_STRING);
808 if ($enctr) {
809 $str_html = edih_claim_history($enctr);
810 } else {
811 $str_html = "Invalid or unknown encounter number".PHP_EOL;
813 } else {
814 $str_html = "Invalid or unknown encounter number".PHP_EOL;
816 return $str_html;
821 * display the message part of a 999 response
823 * @uses ibr_997_errscan()
824 * @return string
826 function ibr_disp_997_message() {
828 $fname = ''; $akval = ''; $errval = '';
829 $fname = filter_input(INPUT_GET, 'fv997', FILTER_SANITIZE_STRING);
830 if (isset($_GET['aknum'])) { $akval = filter_input(INPUT_GET, 'aknum', FILTER_SANITIZE_STRING); }
831 if (isset($_GET['err997'])) { $errval = filter_input(INPUT_GET, 'err997', FILTER_SANITIZE_STRING); }
832 if (!$fname) {
833 $str_html = "Missing file name.<br />".PHP_EOL;
834 } else {
835 $str_html = ibr_997_errscan($fname, $akval);
837 return $str_html;
841 * display the message part of a ACK or TA1 response
843 * @uses ibr_ack_error()
844 * @return string
846 function ibr_disp_ta1_message() {
848 $fname = ''; $code = '';
849 $fname = filter_input(INPUT_GET, 'ackfile', FILTER_SANITIZE_STRING);
850 if (isset($_GET['ackcode'])) $code = filter_input(INPUT_GET, 'ackcode', FILTER_SANITIZE_STRING);
851 if ($fname && $code) {
852 $str_html = ibr_ack_error($fname, $code);
853 } else {
854 $str_html = 'Code value invalid <br />'.PHP_EOL;
856 return $str_html;
861 * check if the batch control number is found in the 997/999 files table
863 * @uses csv_search_record()
864 * @return string
866 function ibr_disp_997_for_batch() {
867 $str_html = '';
868 $batch_icn = filter_input(INPUT_GET, 'batchicn', FILTER_SANITIZE_STRING);
869 if ($batch_icn) {
870 $ctln = (strlen($batch_icn) >= 9) ? substr($batch_icn, 0, 9) : trim(strval($batch_icn));
871 $search = array('s_val'=>$ctln, 's_col'=>3, 'r_cols'=>'all');
872 $result = csv_search_record('f997', 'file', $search, "1");
874 // should be matching row(s) from files_997.csv
875 if (is_array($result) && count($result)) {
876 $str_html .= "<p>Acknowledgement information</p>".PHP_EOL;
877 foreach($result as $rslt) {
878 $ext = substr($rslt[1], -3);
880 $str_html .= "Date: {$rslt[0]} <br />".PHP_EOL;
881 $str_html .= "File: <a target=\"blank\" href=edi_history_main.php?fvkey={$rslt[1]}>{$rslt[1]}</a> <br />".PHP_EOL;
882 $str_html .= "Batch ICN: {$rslt[3]} <br />";
883 // error count or code in position 4
884 if ($ext == '999' || $ext == '997') {
885 $str_html .= "Rejects: {$rslt[4]} <br />".PHP_EOL;
886 // don't have dialog from this dialog, so don't link
887 //$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;
888 } elseif ($ext == 'ta1' || $ext == 'ack') {
889 $str_html .= "Code: {$rslt[4]} <br />".PHP_EOL;
890 //$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;
893 } else {
894 $str_html .= "Did not find corresponding 997/999 file for $ctln<br />".PHP_EOL;
896 } else {
897 $str_html .= "Invalid value for ICN number<br />".PHP_EOL;
899 return $str_html;
903 * function to check whether an era payment has been processed and applied
905 * @uses sqlQuery()
907 * @return string
909 function edih_disp_era_processed() {
911 $str_html = '';
912 $ckno = filter_input(INPUT_GET, 'tracecheck', FILTER_SANITIZE_STRING);
913 if ($ckno) {
914 $srchval = 'ePay - '.$ckno;
915 // reference like '%".$srchval."%'"
916 $row = sqlQuery("SELECT reference, pay_total, global_amount FROM ar_session WHERE reference = ?", array($srchval) );
917 if (!empty($row)) {
918 $str_html .= "trace {$row['reference']} total \${$row['pay_total']}";
919 if ($row['global_amount'] === '0') {
920 $str_html .= " fully allocated";
921 } else {
922 $str_html .= " ({$row['global_amount']} not allocated)";
924 } else {
925 $str_html .= "trace $ckno not posted";
927 } else {
928 $str_html .= "trace $ckno not found";
930 return $str_html;