Example #1
0
/**
 * The main function in this ibr_archive.php script.  This function gets the parameters array
 * from csv_parameters() and calls the archiving functions on each type of file
 * in the parameters array.
 * 
 * @param string $archive_date    yyyy/mm/dd date prior to which is archived 
 * @return string                 descriptive message in html format
 */
function csv_archive_old($archive_date)
{
    //
    // paths
    $edih_dir = csv_edih_basedir();
    $archive_dir = $edih_dir . DIRECTORY_SEPARATOR . 'archive';
    $csv_dir = $edih_dir . DIRECTORY_SEPARATOR . 'csv';
    $tmp_dir = csv_edih_tmpdir();
    $tmp_dir .= $tmp_dir . DIRECTORY_SEPARATOR;
    //
    if (!is_dir($edih_dir . DIRECTORY_SEPARATOR . 'archive')) {
        // should have been created at setup
        mkdir($edih_dir . DIRECTORY_SEPARATOR . 'archive', 0755);
    }
    //
    $days = csv_days_prior($archive_date);
    if (!$days || $days < 90) {
        $out_html = "Archive date {$archive_date} invalid or less than 90 days prior <br />" . PHP_EOL;
        return $out_html;
    }
    //
    $out_html = "Archiving records prior to {$archive_date} <br />" . PHP_EOL;
    //
    $dt = str_replace('/', '', $archive_date);
    //
    $isarchived = FALSE;
    $haserr = FALSE;
    $params = csv_parameters();
    //
    $f_max = 200;
    //
    foreach ($params as $k => $p) {
        $type = $p['type'];
        $fdir = $p['directory'] . DIRECTORY_SEPARATOR;
        //
        $fn_ar = array();
        $arch_csv = array();
        $curr_csvd = array();
        //
        $archive_ar = array();
        //
        // type dpr has only a claim csv type
        $head_ar = $type == 'dpr' ? csv_files_header($type, 'claim') : csv_files_header($type, 'file');
        //
        $fncol = $p['fncolumn'];
        $datecol = $p['datecolumn'];
        //
        // files csv temporary names
        $file_csv = $p['files_csv'];
        $file_csv_copy = $tmp_dir . basename($file_csv);
        $tmp_fold_csv = $tmp_dir . $type . '_old_' . basename($file_csv);
        $tmp_fnew_csv = $tmp_dir . $type . '_new_' . basename($file_csv);
        $iscpf = copy($file_csv, $file_csv_copy);
        //
        // claims csv temporary names
        $claim_csv = $p['claims_csv'];
        $claim_csv_copy = $tmp_dir . basename($claim_csv);
        $tmp_cold_csv = $tmp_dir . $type . '_old_' . basename($claim_csv);
        $tmp_cnew_csv = $tmp_dir . $type . '_new_' . basename($claim_csv);
        $iscpc = copy($claim_csv, $claim_csv_copy);
        //
        if (!$iscpf || !$iscpc) {
            csv_edihist_log("csv_archive_old: copy to tmp dir failed for csv file {$type}");
            $out_html = "Archive temporary files operation failed ... aborting <br />" . PHP_EOL;
            return $out_html;
        }
        //
        // lock the original files
        $fh1 = fopen($file_csv, 'r');
        $islk1 = flock($fh1, LOCK_EX);
        if (!$islk1) {
            fclose($fh1);
        }
        // assume we are on a system that does not support locks
        $fh2 = fopen($claim_csv, 'r');
        $islk2 = flock($fh2, LOCK_EX);
        if (!$islk2) {
            fclose($fh2);
        }
        // assume we are on a system that does not support locks
        //
        // do the archive for the files_type.csv
        $archive_ar = csv_archive_array('file', $file_csv_copy, $datecol, $fncol, $dt);
        if (!$archive_ar) {
            csv_edihist_log("csv_archive_old: creating archive information failed for " . basename($file_csv_copy));
            continue;
        }
        $och = csv_rewrite_record($tmp_old_csv, $head_ar, $archive_ar['arch_csv']);
        $nch = csv_rewrite_record($tmp_new_csv, $head_ar, $archive_ar['curr_csv']);
        $zarch = csv_zip_dir($params, $archive_ar['files'], $archive_date);
        // now move the reconfigured files
        // unlink the present csv file, since it is possible for a rename error if it exists
        $islk1 = $islk1 ? flock($fh1, LOCK_UN) : $islk1;
        if ($islk1) {
            fclose($fh1);
        }
        $isunl = unlink($file_csv);
        if ($zarch) {
            // we got back the zip archive name from csv_zip_dir()
            $ismvz = rename($zarch, $archive_dir . DIRECTORY_SEPARATOR . basename($zarch));
            $ismvo = rename($tmp_fold_csv, $archive_dir . DIRECTORY_SEPARATOR . $dt . basename($tmp_fold_csv));
            $ismvn = rename($tmp_fnew_csv, $file_csv);
            //
            if ($ismvz && $ismvo && $ismvn) {
                // everything is working - clear out the files we put in tmp_dir
                // the tmp dir should be empty, but there might have been something else created there
                $isclr = csv_clear_tmpdir();
                $out_html .= "Archived: type {$type} <br />" . PHP_EOL;
                $out_html .= "&nbsp; archived " . count($archive_ar['files']) . " files\t <br />" . PHP_EOL;
                $out_html .= "&nbsp; archived " . count($archive_ar['arch_csv']) . " rows from " . basename($file_csv) . " <br />" . PHP_EOL;
                $out_html .= "&nbsp; there are now " . count($archive_ar['curr_csv']) . " rows in " . basename($file_csv) . " <br />" . PHP_EOL;
            } else {
                // in case or error, try to restore everything
                $fl_ar = csv_restore_files('file', $p, $archive_ar['files']);
                if (is_array($fl_ar) && count($fl_ar) > 0) {
                    foreach ($fl_ar as $f) {
                        csv_edihist_log("csv_archive_old: lost file {$f}");
                    }
                } elseif (is_array($fl_ar) && count($fl_ar) == 0) {
                    csv_edihist_log("csv_archive_old archiving failed, and files restored");
                } else {
                    csv_edihist_log("csv_archive_old archive failed and files were lost");
                }
                // give a message and quit
                $out_html .= "Archiving error: type {$type} archive errors ... aborting <br />" . PHP_EOL;
                return $out_html;
            }
        } else {
            // zip create error
            csv_edihist_log("csv_archive_old: creating zip archive failed for " . basename($file_csv));
            $fl_ar = csv_restore_files('file', $p, $archive_ar['files']);
            if (is_array($fl_ar) && count($fl_ar) > 0) {
                foreach ($fl_ar as $f) {
                    csv_edihist_log("csv_archive_old: lost file {$f}");
                }
            }
            $out_html .= "Archiving error: type {$type} archive errors ... aborting <br />" . PHP_EOL;
            return $out_html;
        }
        //
        // now we do the claims table
        //$cldate = date_create($archive_date);
        //date_sub($cldate, date_interval_create_from_date_string('1 month'));
        //$cldt = date_format($cldate, 'Ymd');
        //
        // dpr type has only claim table, treated as a file table above
        if ($type == 'dpr') {
            continue;
        }
        //
        $head_ar = csv_files_header($type, 'claim');
        //
        $archive_ar = csv_archive_array('claim', $claim_csv_copy, $datecol, $fncol, $dt);
        if (!$archive_ar) {
            csv_edihist_log("csv_archive_old: creating archive information failed for " . basename($file_csv_copy));
            continue;
        }
        //
        $och = csv_rewrite_record($tmp_cold_csv, $head_ar, $archive_ar['arch_csv']);
        $nch = csv_rewrite_record($tmp_cnew_csv, $head_ar, $archive_ar['curr_csv']);
        //
        $islk2 = $islk2 ? flock($fh2, LOCK_UN) : $islk2;
        if ($islk2) {
            fclose($fh2);
        }
        $isunl = unlink($claim_csv);
        $ismvo = rename($tmp_cold_csv, $archive_dir . DIRECTORY_SEPARATOR . $dt . basename($tmp_cold_csv));
        $ismvn = rename($tmp_cnew_csv, $claim_csv);
        //
        if ($ismvo && $ismvn) {
            // everything is working - clear out the files we put in tmp_dir
            // the tmp dir should be empty, but there might have been something else created there
            $isclr = csv_clear_tmpdir();
            $out_html .= "&nbsp; archived " . count($archive_ar['arch_csv']) . " rows from " . basename($claim_csv) . " <br />" . PHP_EOL;
            $out_html .= "&nbsp; there are now " . count($archive_ar['curr_csv']) . " rows in " . basename($claim_csv) . " <br />" . PHP_EOL;
        } else {
            $fl_ar = csv_restore_files('claim', $p, $archive_ar['files']);
            if (is_array($fl_ar) && count($fl_ar) > 0) {
                foreach ($fl_ar as $f) {
                    csv_edihist_log("csv_archive_old: lost file {$f}");
                }
            } elseif (is_array($fl_ar) && count($fl_ar) == 0) {
                csv_edihist_log("csv_archive_old: archiving failed, and files restored");
            } else {
                csv_edihist_log("csv_archive_old: archive failed and " . count($fl_ar) . " files were lost");
            }
            $out_html .= "Archiving error: type {$type} archive errors ... aborting <br />" . PHP_EOL;
            return $out_html;
        }
    }
    // end foreach($params as $k=>$p)
    //
    return $out_html;
}
Example #2
0
/**
 * create a display for an individual claim status response	
 * 
 * @uses csv_file_by_controlnum()
 * @uses csv_x12_segments()
 * @uses ibr_277_bhtblock()
 * @uses ibr_277_bht_array()
 * @uses ibr_277_bhthtml()
 * @param string  $filename the filename
 * @param string  $isa13 the isa13 control number for the file
 * @param string  $bht03 the identifier from the 837 bht segment
 * @param string  $clm01 the pid-encounter from the 837 clm segment
 * @param string  $st02 the st number from the 277 file
 * @return string  either an error message or a table with the information from the response
 */
function ibr_277_response_html($filename = '', $isa13 = '', $bht03 = '', $clm01 = '', $st02 = '')
{
    // create a display for an individual 277 response
    $html_str = '';
    //
    if (!$filename && !$isa13) {
        csv_edihist_log("ibr_277_response_html: called with no file arguments");
        $html_str .= "Error, no file given<br />" . PHP_EOL;
        return $html_str;
    } elseif (!$filename && $isa13) {
        $fn = csv_file_by_controlnum('f277', $isa13);
    } elseif ($filename) {
        $fn = basename($filename);
    }
    if ($fn) {
        //
        $ar_277_seg = csv_x12_segments($fn, "f277", false);
        if (!is_array($ar_277_seg)) {
            $html_str .= "Error in file name or file parsing <br />" . PHP_EOL;
            return $html_str;
        }
    }
    //
    if ($bht03 || $clm01 || $st02) {
        //
        $sliceparams = ibr_277_bhtblock($ar_277_seg['segments'], $ar_277_seg['delimiters'], $clm01, $bht03, $st02);
        if ($sliceparams) {
            $bhtsegs = array_slice($ar_277_seg['segments'], $sliceparams[0], $sliceparams[1]);
            $bht_ar = ibr_277_bht_array($bhtsegs, $ar_277_seg['delimiters']);
            $bht_html = ibr_277_bhthtml($bht_ar);
            if ($bht_html) {
                $html_str .= $bht_html;
            } else {
                $html_str .= "Error encountered in generating display <br />" . PHP_EOL;
            }
        } else {
            $html_str .= "Did not find status for {$bht03} {$clm01} {$st02} in {$fn} <br />" . PHP_EOL;
        }
    } else {
        csv_edihist_log("ibr_277_response_html: called with no claim identifying arguments");
        $html_str .= "Error, no claim identification given<br />" . PHP_EOL;
    }
    //
    return $html_str;
}
Example #3
0
/**
 * Render one of our csv record files as an html table
 *
 * This function determines the actual csv file from the file_type and the
 * csv_type.  Tthe date field of each row is checked against the optional
 * lookback period or date parameters.
 * 
 * @uses csv_file_type()
 * @uses csv_parameters()
 * @uses csv_thead_html()
 * 
 * @param string      $file_type -- see function csv_file_type()
 * @param string      $csv_type -- either "file" or "claim"
 * @param string      $period -- lookback 2 weeks, 1 month, ALL, etc
 * @param string      $datestart -- from date  CCYY-MM-DD
 * @param string      $dateend -- to date  CCYY-MM-DD
 * 
 * @return string
 */
function edih_csv_to_html($file_type, $csv_type, $period = '', $datestart = '', $dateend = '')
{
    //
    $csv_html = "";
    $modstr = '';
    $csv_d = array();
    // debug
    csv_edihist_log("edih_csv_to_html: period {$period} datestart {$datestart} dateend {$dateend}");
    //
    $is_date = false;
    $is_period = false;
    //
    if ($file_type && $csv_type) {
        $csv_ar = csv_assoc_array($file_type, $csv_type);
        if (is_array($csv_ar) && count($csv_ar)) {
            $rwct = count($csv_ar);
        } else {
            csv_edihist_log("edih_csv_to_html: error opening csv file {$file_type} {$csv_type}");
            $csv_html .= "error opening csv file {$file_type} {$csv_type}<br>";
            return false;
        }
    } else {
        $csv_html .= "invalid arguments for opening csv table<br>";
        return false;
    }
    $tp = csv_file_type($file_type);
    if ($tp) {
        $params = csv_parameters($tp);
    } else {
        csv_edihist_log("csv_to_html error: incorrect file type {$file_type}");
        $csv_html .= "csv_to_html error: incorrect file type {$file_type} <br />" . PHP_EOL;
        return false;
    }
    // csv tables date is in col 0 for file, col 5 for claim
    // file name is under FileName for all tables
    $dtcol = strpos($csv_type, "aim") ? $params['claimdate'] : $params['filedate'];
    $tp_lbl = strpos($csv_type, "aim") ? 'Claims' : 'Files';
    //
    // given dates shold be in CCYY-MM-DD or CCYY/MM/DD format
    // edih_view.php is supposed to use CCYY-MM-DD
    $dts = $dt4 = $dte = '';
    if (preg_match('/(19|20)\\d{2}\\D[01][0-9]\\D[0-3][0-9]/', $datestart)) {
        $dts = implode('', preg_split("/\\D/", $datestart));
        if ($dateend && preg_match('/(19|20)\\d{2}\\D[01][0-9]\\D[0-3][0-9]/', $dateend)) {
            $dte = implode('', preg_split("/\\D/", $dateend));
        } else {
            $dt4 = date_create(date('Y-m-d'));
            $dte = date_format($dt4, 'Ymd');
        }
        // php DateTime is poorly documented
        $is_date = $dts && $dte;
        // debug
        csv_edihist_log("edih_csv_to_html: dts {$dts} dte {$dte} isdate " . strval($is_date));
        //
        //
    } elseif ($period) {
        $dtstr1 = '';
        $is_period = preg_match('/\\d{1,2}(?=w|m|y)/', $period, $matches);
        if (count($matches)) {
            $pd = $matches[0];
            $gtdt = getdate();
            $mon = (string) $gtdt['mon'];
            $day = (string) ($gtdt['mday'] - $gtdt['wday'] + 1);
            $yr = (string) $gtdt['year'];
            if (strtoupper($period) == 'ALL') {
                $is_period = false;
                $modstr = '';
            } elseif (strpos($period, 'w')) {
                // take the first character of 'period'
                $modstr = '-' . $pd . ' week';
                $dtstr1 = $mon . '/' . $day . '/' . $yr;
            } elseif (strpos($period, 'm')) {
                $modstr = '-' . $pd . ' month';
                $dtstr1 = $mon . '/01/' . $yr;
            } elseif (strpos($period, 'y')) {
                $modstr = '-' . $pd . ' year';
                $dtstr1 = $mon . '/01/' . $yr;
            } else {
                csv_edihist_log("csv_to_html error: incorrect date period {$period}");
                return false;
            }
        }
        //
        // debug
        csv_edihist_log("edih_csv_to_html: period {$period} modstr {$modstr} dtstr1 {$dtstr1}");
        //
        if ($is_period && $modstr) {
            $dtpd1 = date_create($dtstr1);
            $dtm = date_modify($dtpd1, $modstr);
            $dtpd2 = $dtm->format('Ymd');
            $dtpdlbl = $dtm->format('Y-m-d');
        } else {
            $dtpd2 = '';
            $dtpdlbl = 'All Dates';
        }
    }
    //
    if ($is_date) {
        $isok = false;
        $idx = 0;
        foreach ($csv_ar as $data) {
            $isok = strcmp($data[$dtcol], $dts) >= 0 ? true : false;
            $isok = strcmp($data[$dtcol], $dte) > 0 ? false : $isok;
            //
            if ($isok) {
                $csv_d[] = $data;
            }
        }
    } elseif ($is_period) {
        foreach ($csv_ar as $data) {
            if (strcmp($data[$dtcol], $dtpd2) > 0) {
                $csv_d[] = $data;
            }
        }
    } else {
        $csv_d =& $csv_ar;
    }
    //
    $ln_ct = count($csv_d);
    //
    if ($is_date) {
        //$csv_html .= "<div id='dttl'>".PHP_EOL;
        $csv_html .= "<h4>Table: {$tp_lbl} &nbsp;{$tp} &nbsp; Start: {$datestart} &nbsp; End: {$dateend} &nbsp;Rows: {$rwct} &nbsp;Shown: {$ln_ct}</h4>" . PHP_EOL;
        //$csv_html .= "</div>".PHP_EOL;
    } elseif ($is_period) {
        //div id='dttl'></div>
        $csv_html .= "<h4>Table: {$tp_lbl} &nbsp;{$tp} &nbsp;From Date: {$dtpdlbl} &nbsp;Rows: {$rwct} &nbsp;Shown: {$ln_ct}</h4>" . PHP_EOL;
    } else {
        //<div id='dttl'></div>
        $csv_html .= "<h4>Table: {$tp_lbl} &nbsp;{$tp} &nbsp;All Dates Rows: {$rwct} &nbsp; Shown: {$ln_ct}</h4>" . PHP_EOL;
    }
    //
    //$csv_html .= "<table id='csvTable' class='csvDisplay'>".PHP_EOL;  class='display'max-width: fit-content
    $csv_html .= "<table id='csvTable' style='width: fit-content; float: left'>" . PHP_EOL;
    $csv_html .= csv_thead_html($tp, $csv_type);
    $csv_html .= "<tbody>" . PHP_EOL;
    //
    // now create the body of the table
    //
    $cls = strpos('|f837|f270|f276|f278', $tp) ? 'sub' : 'rsp';
    //
    $idx = 0;
    if ($csv_type == 'file') {
        //
        if ($tp == 'f835') {
            //array('Date', 'FileName', 'Control', 'Trace', 'Claim_ct', 'Denied', 'Payer')
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                foreach ($val as $k => $v) {
                    if ($k == 'Date') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $fn = $v;
                        $csv_html .= "<td><a class='{$cls}' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=htm'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'Control') {
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=file&icn={$v}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'Trace') {
                        $trc = $v;
                        $csv_html .= "<td><a class='{$cls}' href='edih_main.php?gtbl=file&trace={$v}&ftype={$tp}&rsptype={$tp}&fmt=htm'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'Claim_ct') {
                        $csv_html .= "<td>{$v} <a class='sub' href='edih_main.php?tracecheck={$trc}&ckprocessed=yes'><em>P?</em></a></td>" . PHP_EOL;
                    } elseif ($k == 'Denied') {
                        if ((int) $v > 0) {
                            $csv_html .= "<td><a class='sub' href='edih_main.php?chkdenied=yes&fname={$fn}&ftype={$tp}&trace={$trc}'>{$v}</a></td>" . PHP_EOL;
                        } else {
                            $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                        }
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f997') {
            //array('Date', 'FileName', 'Control', 'Trace', 'RspType', 'RejCt')
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                //
                $rsp = $val['RspType'];
                foreach ($val as $k => $v) {
                    if ($k == 'Date') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $fn = $v;
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'Trace') {
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=file&trace={$v}&ftype={$tp}&rsptype={$rsp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'RejCt') {
                        if ((int) $v > 0) {
                            $csv_html .= "<td><a class='rpt' href='edih_main.php?chkdenied=yes&fname={$fn}&ftype={$tp}'>{$v}</a></td>" . PHP_EOL;
                        } else {
                            $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                        }
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
            //
        } else {
            // the generic case -- for 'file' type tables
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                foreach ($val as $k => $v) {
                    if ($k == 'Date') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=htm'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'Control') {
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=file&icn={$v}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'Reject') {
                        if ((int) $v > 0) {
                            $csv_html .= "<td><a class='sub' href='edih_main.php?&chkdenied=yes&fname={$val['FileName']}&ftype={$tp}'>{$v}</a></td>" . PHP_EOL;
                        } else {
                            $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                        }
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        }
    } elseif ($csv_type == 'claim') {
        //
        if ($tp == 'f837') {
            // array('PtName', 'SvcDate', 'CLM01', 'InsLevel', 'Control', 'FileName', 'Fee', 'PtPaid', 'Provider' );
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                foreach ($val as $k => $v) {
                    if ($k == 'CLM01') {
                        $csv_html .= "<td><a class='sub' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&pid={$v}'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'SvcDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=file&fname={$fn}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f835') {
            // array('PtName', 'SvcDate', 'CLM01', 'Status', 'Trace', 'FileName', 'ClaimID', 'Pmt', 'PtResp', 'Payer')
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                $pid = $val['CLM01'];
                foreach ($val as $k => $v) {
                    if ($k == 'SvcDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'CLM01') {
                        $csv_html .= "<td>{$v} <a class='{$cls}' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&pid={$pid}&fmt=htm'>H</a>&nbsp;";
                        $csv_html .= "&nbsp;<a class='seg' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&pid={$pid}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } elseif ($k == 'Trace') {
                        $csv_html .= "<td><a class='{$cls}' href='edih_main.php?gtbl=file&fname={$fn}&trace={$v}&ftype={$tp}&fmt=htm'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td title='{$v}'>File <a class='{$cls}' href='edih_main.php?gtbl=file&fname={$fn}&ftype={$tp}&fmt=htm'>H</a>&nbsp;";
                        $csv_html .= "&nbsp;<a class='{$cls}' href='edih_main.php?gtbl=file&fname={$fn}&ftype={$tp}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f277') {
            // array('PtName', 'SvcDate', 'CLM01', 'Status', 'BHT03', 'FileName', 'Payer', 'Ins_ID', 'Trace');
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                $bht03 = $val['BHT03'];
                $trc = $val['CLM01'];
                foreach ($val as $k => $v) {
                    if ($k == 'SvcDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'CLM01') {
                        $csv_html .= "<td><a class='sub' href='edih_main.php?gtbl=claim&trace={$v}&ftype={$tp}&rsptype=f837&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'BHT03') {
                        $csv_html .= "<td>{$v} <a class='rsp' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=htm'>H</a>&nbsp;";
                        $csv_html .= "&nbsp;<a class='seg' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td title='{$v}'>File <a class='rsp' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=htm'>H</a>&nbsp;";
                        $csv_html .= "&nbsp;<a class='seg' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f276') {
            // array('PtName', 'ReqDate', 'CLM01', 'InsBnft', 'BHT03', 'FileName', 'Payer', 'Trace');
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                $bht03 = $val['BHT03'];
                $trc = $val['CLM01'];
                foreach ($val as $k => $v) {
                    if ($k == 'ReqDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'CLM01') {
                        $csv_html .= "<td><a class='sub' href='edih_main.php?gtbl=claim&trace={$v}&ftype={$tp}&rsptype=f837&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'BHT03') {
                        $csv_html .= "<td>{$v} <a class='{$cls}' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=htm'>H</a>&nbsp;";
                        $csv_html .= "&nbsp;<a class='seg' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td><a class='{$cls}' href='edih_main.php?gtbl=file&fname={$fn}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f270') {
            // array('PtName', 'ReqDate', 'Trace', 'InsBnft', 'BHT03', 'FileName', 'Payer');
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                $bht03 = $val['BHT03'];
                foreach ($val as $k => $v) {
                    if ($k == 'ReqDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'BHT03') {
                        $csv_html .= "<td> <a class='{$cls}' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td><a class='{$cls}' href='edih_main.php?gtbl=file&fname={$fn}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f271') {
            // array('PtName', 'RspDate', 'Trace', 'Status', 'BHT03', 'FileName', 'Payer');
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                $bht03 = $val['BHT03'];
                foreach ($val as $k => $v) {
                    if ($k == 'RspDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'BHT03') {
                        $csv_html .= "<td>{$v} <a class='{$cls}' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=htm'>H</a>&nbsp;" . PHP_EOL;
                        $csv_html .= "&nbsp;<a class='seg' target='_blank' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td title='{$v}'> File <a class='{$cls}' href='edih_main.php?gtbl=file&fname={$fn}&ftype={$tp}&fmt=htm'>H</a>&nbsp;";
                        $csv_html .= "&nbsp;<a class='{$cls}' href='edih_main.php?gtbl=file&fname={$fn}&ftype={$tp}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f278') {
            // array('PtName', 'FileDate', 'Trace', 'Status', 'BHT03', 'FileName', 'Auth', 'Payer')
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                $bht03 = $val['BHT03'];
                foreach ($val as $k => $v) {
                    if ($k == 'FileDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'BHT03') {
                        $csv_html .= "<td>{$v} <a class='{$cls}' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=htm'>H</a>&nbsp;" . PHP_EOL;
                        $csv_html .= "&nbsp;<a class='seg' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&bht03={$v}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td title='{$v}'> File <a class='{$cls}' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=htm'>H</a>&nbsp;";
                        $csv_html .= "&nbsp;<a class='{$cls}' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=seg'>T</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } elseif ($tp == 'f997') {
            // array('PtName', 'RspDate', 'Trace', 'Status', 'Control', 'FileName', 'RspType', 'err_seg');
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                // needed values for links
                $fn = $val['FileName'];
                $rsp = $val['RspType'];
                $err = $val['err_seg'];
                foreach ($val as $k => $v) {
                    if ($k == 'RspDate') {
                        $csv_html .= "<td>" . substr($v, 0, 4) . '-' . substr($v, 4, 2) . '-' . substr($v, 6, 2) . "</td>" . PHP_EOL;
                    } elseif ($k == 'FileName') {
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'Trace') {
                        $csv_html .= "<td><a class='seg' href='edih_main.php?gtbl=claim&fname={$fn}&ftype={$tp}&trace={$v}&rsptype={$rsp}&err={$err}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } elseif ($k == 'err_seg') {
                        $csv_html .= "<td title='{$v}'>" . substr($v, 0, 8) . "...</td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        } else {
            // all types in the tables are covered in an elseif, so this is unexpected
            foreach ($csv_d as $val) {
                $bgc = $idx % 2 == 1 ? 'odd' : 'even';
                $csv_html .= "<tr class='{$bgc}'>" . PHP_EOL;
                foreach ($val as $k => $v) {
                    if ($k == 'FileName') {
                        $csv_html .= "<td><a class='{$cls}' href='edih_main.php?gtbl=file&fname={$v}&ftype={$tp}&fmt=seg'>{$v}</a></td>" . PHP_EOL;
                    } else {
                        $csv_html .= "<td>{$v}</td>" . PHP_EOL;
                    }
                }
                $csv_html .= "</tr>" . PHP_EOL;
                $idx++;
            }
        }
    }
    // end body of the table
    //$csv_html .= "</tbody>".PHP_EOL."</table>".PHP_EOL."</div>".PHP_EOL;
    $csv_html .= "</tbody>" . PHP_EOL . "</table>" . PHP_EOL;
    //
    return $csv_html;
}
Example #4
0
/**
 * process new 997/999 files
 * 
 * @uses csv_newfile_list()
 * @uses csv_verify_file()
 * @uses csv_parameters()
 * @param array $file_array -- optional, this array is sent from the ibr_io.php script
 * @param bool $html_out -- whether to produce and return html output
 * @param bool $err_only -- whether to generate claim information only for errors (ignored)
 * @return string
 */
function ibr_997_process_new($file_array = NULL, $html_out = TRUE, $err_only = TRUE)
{
    //
    $ret_str = "";
    $chr_ct1 = 0;
    $chr_ct2 = 0;
    $new_997 = array();
    $need_dir = TRUE;
    //
    if (is_null($file_array) || empty($file_array)) {
        // directory will need to be prepended to name
        $new_997 = csv_newfile_list('f997');
    } elseif (is_array($file_array) && count($file_array)) {
        // files that are not verified will just be ignored
        foreach ($file_array as $finp) {
            $fp = csv_verify_file($finp, 'f997');
            if ($fp) {
                $new_997[] = $fp;
            }
        }
        $need_dir = FALSE;
    }
    //
    if (count($new_997) == 0) {
        $ret_str = "<p>ibr_997_process_new: no new 997/999 files. </p>";
        return $ret_str;
    } else {
        $f997count = count($new_997);
    }
    //
    $ar_htm = array();
    //
    $params = csv_parameters("f997");
    $tdir = dirname(__FILE__) . $params['directory'];
    //
    // get batch files parameters, we need the batch_dir
    $bp = csv_parameters("batch");
    $batch_dir = dirname(__FILE__) . $bp['directory'];
    //
    foreach ($new_997 as $f997) {
        // make the file path
        $fpath = $need_dir ? $tdir . DIRECTORY_SEPARATOR . $f997 : $f997;
        // get file m-time
        //$ftime = date('Ymd:His', filemtime($fpath));
        // read file into string
        $f_str = file_get_contents($fpath);
        if ($f_str) {
            // transform file contents into segment arrays
            $ar_seg = csv_x12_segments($fpath, "f997", FALSE);
            //
            if (!$ar_seg) {
                // file was rejected
                csv_edihist_log("ibr_997_process_new: failed to get segments for {$fpath}");
                $ret_str .= "ibr_997_process_new: failed to get segments for {$fpath}</p>" . PHP_EOL;
                continue;
            }
            // parse arrays into data arrays
            $ar_data = ibr_997_parse($ar_seg);
            // $ar_data = array("file" => $ar_997file, "rejects" => $ar_reject);
            //
            $csv_claims = ibr_997_data_csv($ar_data['file'], 'file');
            if ($csv_claims) {
                $chr_ct1 += csv_write_record($csv_claims, "f997", "file");
            } else {
                csv_edihist_log("ibr_997_process_new: error with files csv array");
            }
            //
            if (isset($ar_data['claims']) && count($ar_data['claims'])) {
                // only add to claims_997.csv if there are rejected claims
                $csv_claims = ibr_997_data_csv($ar_data['claims'], 'claim');
                if ($csv_claims) {
                    $chr_ct2 += csv_write_record($csv_claims, "f997", "claim");
                } else {
                    csv_edihist_log("ibr_997_process_new: error with claims csv array");
                }
                //
            }
            //
            // save all the ar_datas for html output
            if ($html_out) {
                $ar_htm[] = $ar_data;
            }
            //
        } else {
            $ret_str .= "<p>ibr_997_process_new: failed to read {$fpath} </p>" . PHP_EOL;
        }
    }
    //
    csv_edihist_log("ibr_997_process_new: {$chr_ct1} characters written to files_997.csv");
    csv_edihist_log("ibr_997_process_new: {$chr_ct2} characters written to claims_997.csv");
    //
    if ($html_out) {
        // generate html output and return that
        $ret_str .= ibr_997_file_data_html($ar_htm, $err_only);
    } else {
        $ret_str .= "x12_999 files: processed {$f997count} x12-999 files <br />";
    }
    //
    return $ret_str;
}
        $html_str = is_xhr() ? '' : ibr_html_heading('claimstatus');
        $html_str .= ibr_disp_997_for_batch();
    } elseif (array_key_exists('showlog', $_GET)) {
        $la = filter_input(INPUT_GET, 'showlog', FILTER_SANITIZE_STRING);
        $html_str = $la ? csv_log_html() : "input parameter error<br />";
    } elseif (array_key_exists('archivelog', $_GET)) {
        $la = filter_input(INPUT_GET, 'archivelog', FILTER_SANITIZE_STRING);
        $html_str = $la ? csv_log_archive() : "input parameter error<br />";
    } elseif (array_key_exists('getnotes', $_GET)) {
        $la = filter_input(INPUT_GET, 'getnotes', FILTER_SANITIZE_STRING);
        $html_str = $la ? ibr_history_notes() : "input parameter error<br />";
    } else {
        $html_str = "EDI History: unknown parameter<br />" . PHP_EOL;
        //$html_str .= var_dump($_GET) . PHP_EOL;
    }
} else {
    die("EDI History: invalid input method <br />");
}
//
$isclear = csv_clear_tmpdir();
if (!$isclear) {
    echo "file contents remain in {$ibr_tmp} <br />" . PHP_EOL;
    csv_edihist_log("file contents remain in {$ibr_tmp}");
}
//
if (!$html_str) {
    csv_edihist_log("no html output!");
    die("No content in response <br />" . PHP_EOL);
}
//
print $html_str;
Example #6
0
/**
 * Report 835 file as processed by billing routine if the file name
 * is found in the 'era' directory.  The file name is a concatenation
 * of GS04_TRN04_ISA13.edi per parse_era_inc.php
 *
 * 
 * @param string
 * @return bool
 */
function edih_disp_835_processed($erasavename)
{
    // openemr/interface/billing/era_payments.php
    // openemr/library/parse_era.inc.php
    //  OpenEMR filename for era should be just the upload filename or
    //  $out['gs_date'] . '_' . $out['payer_id'] . '_' .$out['isa_control_number']
    //  with 'payer_id' taken from BPR10 or TRN03 (same value) and not from TRN04
    //
    // search for YYYYMMDD_NNNNNNNN_ISA13
    $eraname = $out['gs_date'] . '_' . ltrim($out['isa_control_number'], '0') . '_' . ltrim($out['payer_id'], '0');
    $out['payer_id'] = trim($seg[4]);
    //TRN04
    //
    $srchlen = strlen($erasavename);
    $found = false;
    $eradir = $GLOBALS['OE_SITE_DIR'] . DS . 'edi' . DS . 'era';
    //
    if ($hd = opendir($eradir)) {
        while (false !== ($entry = readdir($hd))) {
            if (strncmp($entry, $erasavename, $srchlen) === 0) {
                $found = true;
                break;
            }
        }
        closedir($hd);
    } else {
        csv_edihist_log("edih_disp_processed_835: did not find processed era directory");
    }
    return $found;
}
Example #7
0
/**
 * Display of x12 edi transaction listing or all segments in the files.
 * When using $err_info, you must use the filepath of the submitted file
 * 
 * @param string   $filepath path to desired file
 * @param string   $filetype used when filepath is just filename
 * @param string   optional $claimid CLM01, or BHT03 to identify a transaction or a trace value 
 * @param bool     false: $claimid is pt transaction, true: $claimid is trace from 835 or 999 
 * @param string   optional $err_info  the prepared error info from a 997/999 response
 * @return string  html for display of file segments
 */
function edih_display_text($filepath, $filetype = '', $claimid = '', $trace = false, $err_info = '')
{
    //
    $str_html = '';
    $de = '';
    $segments = '';
    $stsegkey = '';
    $ft = $filetype ? $filetype : '';
    $errs = strlen($err_info) ? $err_info : '';
    $bht03 = '';
    //
    // verify x12 file
    $x12obj = csv_check_x12_obj($filepath, $ft);
    //
    if ($x12obj && 'edih_x12_file' == get_class($x12obj)) {
        $ftype = $x12obj->edih_type();
        $ft = csv_file_type($ftype);
        $delims = $x12obj->edih_delimiters();
        $de = $delims['e'];
        $fn = $x12obj->edih_filename();
        $segs_ar = $x12obj->edih_segments();
        $env_ar = $x12obj->edih_envelopes();
        if (!$de || strlen($de) != 1) {
            // error in object
            // debug
            $str_html = 'edih_display_text(): error in delimiters<br />' . PHP_EOL;
            $str_html .= $x12obj->edih_message() . PHP_EOL;
            return $str_html;
        }
        if (!is_array($segs_ar) || !count($segs_ar)) {
            // unknown error
            $str_html = "<p>unknown error retrieving segments for {$fn}</p>" . PHP_EOL;
            $str_html .= $x12obj->edih_message() . PHP_EOL;
            return $str_html;
        }
    } else {
        //debug
        csv_edihist_log('edih_transaction_text(): invalid path ' . $filepath);
        $str_html = 'edih_display_text(): error accessing file<br />' . PHP_EOL;
        return $str_html;
    }
    //
    if ($claimid) {
        // claimid can be for transaction, payment, or error response
        if ($trace && array_key_exists($claimid, $env_ar['ISA'])) {
            $arg_ar = array('ISA13' => $claimid, 'keys' => true);
            $segments = $x12obj->edih_x12_slice($arg_ar);
        } else {
            // claimid alone can be clm01 or bht03, if trace=true, expect trn02 for claimid
            foreach ($env_ar['ST'] as $st) {
                if ($trace && $claimid == $st['trace']) {
                    $arg_ar = array('ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'trace' => $claimid, 'keys' => true);
                    $segments = $x12obj->edih_x12_slice($arg_ar);
                    break;
                } elseif (in_array($claimid, $st['acct'])) {
                    if ($errs) {
                        $arg_ar = array('ST02' => $st['stn'], 'ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'keys' => true);
                        $segments = $x12obj->edih_x12_slice($arg_ar);
                    } else {
                        // request for individual transaction segments
                        $segments = $x12obj->edih_x12_transaction($claimid);
                    }
                    break;
                } elseif (in_array($claimid, $st['bht03'])) {
                    // also possible that bht03 number is given for claimid
                    // this will likely be a 27x
                    if ($errs) {
                        $arg_ar = array('ST02' => $st['stn'], 'ISA13' => $st['icn'], 'GS06' => $st['gsn'], 'keys' => true);
                        $segments = $x12obj->edih_x12_slice($arg_ar);
                    } else {
                        $segments = $x12obj->edih_x12_transaction($claimid);
                    }
                    $bht03 = $claimid;
                    break;
                }
            }
        }
    } else {
        $segments = $segs_ar;
    }
    //
    // now check if we have segments
    if (empty($segments) || !count($segments)) {
        if ($claimid) {
            $str_html = "<p>error: transaction {$claimid} not found in {$fn}</p>" . PHP_EOL;
            $str_html .= $x12obj->edih_message() . PHP_EOL;
            return $str_html;
        } else {
            // unknown error
            $str_html = "<p>unknown error retrieving segments for {$fn}</p>" . PHP_EOL;
            $str_html .= $x12obj->edih_message() . PHP_EOL;
            return $str_html;
        }
    }
    // if the segments are from a slice or transaction
    // a multidimensional array  segs[i][j] must be flattened
    $ar_sngl = csv_singlerecord_test($segments);
    // false when segments are a transaction or trace only
    if (!$ar_sngl) {
        //
        // append segments to single array
        // keys should not duplicate since all segments
        // are from the same x12 file
        $trnsegs = array();
        for ($i = 0; $i < count($segments); $i++) {
            $trnsegs = array_merge($trnsegs, $segments[$i]);
        }
        $segments = $trnsegs;
        unset($trnsegs);
    }
    //
    $capstr = '';
    $tbl_id = $claimid ? $claimid : $fn;
    //
    //'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278','HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837');
    switch ((string) $ftype) {
        case 'HC':
            $capstr = "Claim ";
            //$cls = "txt837";
            $trn_html = edih_837_text($segments, $de, $errs);
            break;
        case 'HP':
            $capstr = "Payment ";
            //$cls = "txt835";
            $trn_html = edih_835_text($segments, $de);
            break;
        case 'HR':
            $capstr = "Status Query ";
            //$cls = "txt276";
            $trn_html = edih_277_text($segments, $de, $errs);
            break;
        case 'HN':
            $capstr = "Claim Status ";
            //$cls = "txt277";
            $trn_html = edih_277_text($segments, $de, $stsegkey);
            break;
        case 'HS':
            $capstr = "Eligibility Query ";
            //$cls = "txt270";
            $trn_html = edih_271_text($segments, $de, $errs);
            break;
        case 'HB':
            $capstr = "Eligibility Report ";
            //$cls = "txt271";
            $trn_html = edih_271_text($segments, $de);
            break;
        case 'HI':
            $capstr = "Authorization ";
            //$cls = "txt278";
            $trn_html = edih_278_text($segments, $de, $errs);
            break;
        case 'FA':
            $capstr = "Batch Acknowledgment ";
            //$cls = "txt997";
            $trn_html = edih_997_text($segments, $de);
            break;
        default:
            $capstr = "x12 {$ftype} ";
            //$cls = "txt_x12";
            $trn_html = edih_segments_text($segments, $de);
            break;
    }
    //
    $capstr .= $claimid ? " <em>ID:</em> {$claimid}" : "";
    //
    $str_html .= "<table id={$tbl_id} cols=3 class='segtxt'><caption>{$capstr}</caption>" . PHP_EOL;
    $str_html .= "<thead>" . PHP_EOL;
    $str_html .= "<tr><th class='btloop'>Loop</th><th class='btloop'>Num</th>";
    $str_html .= "<th class='segtxt'>Segment (<em>File:</em> {$fn})</th></tr>" . PHP_EOL;
    $str_html .= "</thead>" . PHP_EOL . "<tbody>" . PHP_EOL;
    //
    $str_html .= $trn_html;
    //
    $str_html .= "</tbody></table>" . PHP_EOL;
    //
    return $str_html;
}
Example #8
0
/**
 * Main function that handles the upload files array
 *
 * The return array has keys 'type' and subarray of file names
 * relies on global $_POST and $_FILES variables
 *
 * @uses edih_upload_reindex()
 * @uses edih_ziptoarray()
 * @uses ibr_upload_match_file()
 * @uses csv_parameters()
 * 
 * @param string &$html_str   referenced and appended to in this function
 * @return array             array of files that pass the checks and scans
 */
function edih_upload_files()
{
    //
    $html_str = '';
    //
    // from php manual ling 03-Nov-2010 08:35
    if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
        $pmax = ini_get('post_max_size');
        //
        csv_edihist_log('edih_upload_files: Error: upload too large, max size is ' . $pmax);
        return false;
    }
    if (empty($_FILES)) {
        csv_edihist_log('Error: upload files indicated, but none received.');
        return false;
    }
    // only one $_FILES array key is expected
    $uplkey = '';
    $uplkey = array_key_exists("fileUplMulti", $_FILES) ? "fileUplMulti" : $uplkey;
    $uplkey = array_key_exists("fileUplx12", $_FILES) ? "fileUplx12" : $uplkey;
    //
    if (!$uplkey) {
        csv_edihist_log('edih_upload_files: Error: file array name error');
        return false;
    }
    //
    if ($uplkey != "fileUplMulti") {
        // Undefined | Multiple Files | $_FILES Corruption Attack
        // If this request falls under any of them, treat it invalid.
        if (!isset($_FILES[$uplkey]['error']) || is_array($_FILES[$uplkey]['error'])) {
            csv_edihist_log('edih_upload_files: Error: file array keys error');
            return false;
        }
    }
    //
    // these are the mime-types that we will accept -- however, mime-type is not reliable
    // for linux, system("file -bi -- ".escapeshellarg($uploadedfile)) gives mime-type and character encoding
    $m_types = array('application/octet-stream', 'text/plain', 'application/zip', 'application/x-zip-compressed');
    //
    // some unwanted file extensions that might be accidentally included in upload files
    $ext_types = 'sh|asp|html|htm|cm|js|xml|jpg|png|tif|xpm|pdf|php|py|pl|tcl|doc|pub|ppt|xls|xla|vsd|rtf|odt|ods|odp';
    // we get the parameters here to send to ibr_upload_match_file()
    $param_ar = csv_parameters("ALL");
    //
    // initialize retained files array and counter
    $f_ar = array();
    $p_ct = 0;
    //
    // here send the $_FILES array to edih_upload_reindex for "fileUplMulti"
    // instead of $_FILES[$uplkey] ["name"][$i] ["tmp_name"][$i] ["type"][$i] ["error"][$i] ["size"][$i]
    // we will have $files[$uplkey][$i] ["name"]["tmp_name"]["type"]["error"]["size"]
    if ($uplkey == "fileUplMulti") {
        $files = edih_upload_reindex($_FILES);
    } else {
        $files[$uplkey][] = $_FILES[$uplkey];
    }
    //
    $f_ct = count($files[$uplkey]);
    //begin the check and processing loop
    foreach ($files[$uplkey] as $idx => $fa) {
        // basic php verification checks
        if ($fa['error'] !== UPLOAD_ERR_OK) {
            //$html_str .= "Error: [{$fa['name']}] " . edih_upload_err_message($fa['error']) . "<br />" . PHP_EOL;
            $err = edih_upload_err_message($fa['error']);
            $f_ar['reject'][] = array('name' => $fa['name'], 'comment' => $err);
            csv_edihist_log('edih_upload_files: _FILES error ' . $fa['name'] . ' ' . $err);
            unset($files[$uplkey][$idx]);
            continue;
        }
        if (!is_uploaded_file($fa['tmp_name'])) {
            //$html_str .= "Error: uploaded_file error for {$fa['name']}<br />". PHP_EOL;
            $f_ar['reject'][] = array('name' => $fa['name'], 'comment' => 'php uploaded file error');
            csv_edihist_log('edih_upload_files: _FILES error tmp_name ' . $fa['name']);
            unset($files[$uplkey][$idx]);
            continue;
        }
        if (!in_array($fa['type'], $m_types)) {
            //$html_str .= "Error: mime-type {$fa['type']} not accepted for {$fa['name']} <br />" . PHP_EOL;
            $f_ar['reject'][] = array('name' => $fa['name'], 'comment' => 'mime-type ' . $fa['type']);
            csv_edihist_log('edih_upload_files: _FILES error mime-type ' . $fa['name'] . ' mime-type ' . $fa['type']);
            unset($files[$uplkey][$idx]);
            continue;
        }
        // verify that we have a usable name
        $fext = strpos($fa['name'], '.') ? pathinfo($fa['name'], PATHINFO_EXTENSION) : '';
        if ($fext && preg_match('/' . $ext_types . '\\?/i', $fext)) {
            //$html_str .= 'Error: uploaded_file error for '.$fa['name'].' extension '.$fext.'<br />'. PHP_EOL;
            $f_ar['reject'][] = array('name' => $fa['name'], 'comment' => 'extension ' . $fext);
            csv_edihist_log('edih_upload_files: _FILES error name ' . $fa['name'] . ' extension ' . $fext);
            unset($files[$uplkey][$idx]);
            continue;
        }
        if (is_string($fa['name'])) {
            // check for null byte in file name, linux hidden file, directory
            if (strpos($fa['name'], '.') === 0 || strpos($fa['name'], "") !== false || strpos($fa['name'], "./") !== false) {
                //$html_str .= "Error: uploaded_file error for " . $fa['name'] . "<br />". PHP_EOL;
                $fname = preg_replace("/[^a-zA-Z0-9_.-]/", "_", $fa['name']);
                $f_ar['reject'][] = array('name' => $fname, 'comment' => 'null byte, hidden, invalid');
                csv_edihist_log('edih_upload_files: null byte, hidden, invalid ' . $fname);
                unset($files[$uplkey][$idx]);
                continue;
            }
            // replace spaces in file names -- should not happen, but response files from payers might have spaces
            // $fname = preg_replace("/[^a-zA-Z0-9_.-]/","_",$fname);
            $fa['name'] = str_replace(' ', '_', $fa['name']);
        } else {
            // name is not a string
            //$html_str .= "Error: uploaded_file error for " . $fa['tmp_name'] . "<br />". PHP_EOL;
            $f_ar['reject'][] = array('name' => (string) $fa['name'], 'comment' => 'invalid name');
            unset($files[$uplkey][$idx]);
            continue;
        }
        if (!$fa['tmp_name'] || !$fa['size']) {
            //$html_str .= "Error: file name or size error <br />" . PHP_EOL;
            $f_ar['reject'][] = array('name' => (string) $fa['name'], 'comment' => 'php file upload error');
            unset($files[$uplkey][$idx]);
            continue;
        }
        // verification checks special to our application
        //
        //////////////////////////////////
        // check for zip file archive -- sent to edih_ziptoarray
        //
        if (strpos(strtolower($fa['name']), '.zip') || strpos($fa['type'], 'zip')) {
            //
            // this is a bit involved since we cannot predict how many files will be returned
            // get an array of files from the zip unpack function"fileUplx12"
            //
            //if ($uplkey != "fileUplmulti") {
            //$f_upl = edih_ziptoarray($fa['tmp_name'], $param_ar, false);
            //} else {
            //$f_upl = edih_ziptoarray($fa['tmp_name'], $param_ar, true);
            //}
            $f_upl = edih_ziptoarray($fa['tmp_name'], $param_ar);
            //
            // put them in the correct type array
            // expect fupl in form [type] = array(fn1, fn2, fn3, ...)
            if (is_array($f_upl) && count($f_upl)) {
                // $tp is file type, fz is file name
                foreach ($f_upl as $tp => $fz) {
                    if ($tp == 'reject') {
                        if (isset($f_ar['reject']) && is_array($fz)) {
                            array_merge($f_ar['reject'], $fz);
                        } else {
                            $f_ar['reject'] = is_array($fz) ? $fz : array();
                        }
                    } else {
                        // expect $fz to be an array of file names
                        foreach ($fz as $zf) {
                            $f_ar[$tp][] = $zf;
                            $p_ct++;
                        }
                    }
                }
            } else {
                // nothing good from edih_ziptoarray()
                // $html_str .= "error with zip file or no files accepted for " . $fa['name'] . "<br />" .PHP_EOL;
                $f_ar['reject'][] = array('name' => $fa['name'], 'comment' => 'error with zip archive');
                unset($files[$uplkey][$idx]);
            }
            // continue, since we have done everything that would happen below
            continue;
        }
        //////////
        // at this point, since we have come through all the if statements
        // then we have:
        //  a single file under "fileUplEra"
        //  a single file under "fileUplx12"
        //  or one of possibly several files under "fileUplMulti"
        //////////
        $f_upl = edih_upload_match_file($param_ar, $fa);
        //
        if (is_array($f_upl) && count($f_upl) > 0) {
            $f_ar[$f_upl['type']][] = $f_upl['name'];
            $p_ct++;
        } else {
            // verification failed
            csv_edihist_log('edih_upload_file: verification failed for ' . $fa['name']);
            $f_ar['reject'][] = array('name' => $fa['name'], 'comment' => 'verification failed');
            unset($files[$uplkey][$idx]);
        }
    }
    // end foreach($files[$uplkey] as $idx=>$fa)
    //
    $f_ar['remark'][] = "Received {$f_ct} files, accepted {$p_ct}" . PHP_EOL;
    return $f_ar;
}
Example #9
0
/**
 * create a display for an individual claim status response	
 *
 * @uses csv_check_x12_obj()
 * @uses edih_277_transaction_html()
 * 
 * @param string  $filename the filename
 * @param string  $clm01 identifier from 837 CLM of BHT segment
 * 
 * @return string  either an error message or a table with the information from the response
 */
function edih_277_html($filename, $bht03 = '')
{
    // create a display for an individual 277 response
    $html_str = '';
    //
    if ($filename) {
        $fn = $filename;
    } else {
        csv_edihist_log("edih_277_html: called with no file arguments");
        $html_str .= "Error, no file given<br />" . PHP_EOL;
        return $html_str;
    }
    if ($fn) {
        $obj277 = csv_check_x12_obj($fn, 'f277');
        if ($obj277 && 'edih_x12_file' == get_class($obj277)) {
            if ($bht03) {
                // particular transaction
                $html_str .= edih_277_transaction_html($obj277, $bht03);
            } else {
                // file contents
                $env_ar = $obj277->edih_envelopes();
                if (!isset($env_ar['ST'])) {
                    $html_str .= "<p>edih_277_html: file parse error, envelope error</p>" . PHP_EOL;
                    $html_str .= $obj277->edih_message();
                    return $html_str;
                } else {
                    $html_str .= "<div id='accordion'>" . PHP_EOL;
                }
                foreach ($env_ar['ST'] as $st) {
                    foreach ($env_ar['GS'] as $gs) {
                        if ($gs['icn'] != $st['icn']) {
                            continue;
                        } else {
                            $gs_date = edih_format_date($gs['date']);
                            $gs_sender = $gs['sender'];
                            break;
                        }
                    }
                    //
                    // get each transaction
                    foreach ($st['bht03'] as $bht) {
                        //$html_str .= "<h3>$bht Claim Status <em>Date</em> $gs_date <em>Source</em> $gs_sender</h3>".PHP_EOL;
                        //$html_str .= "<div id='ac_$bht'>".PHP_EOL;
                        //
                        $html_str .= edih_277_transaction_html($obj277, $bht, true);
                        //
                        //$html_str .= "</div>".PHP_EOL;
                    }
                }
                $html_str .= "</div>" . PHP_EOL;
            }
        } else {
            $html_str .= "<p>{$filename} : file parse error</p>" . PHP_EOL;
        }
    } else {
        $html_str .= "Error with file name or file parsing <br />" . PHP_EOL;
        csv_edihist_log("edih_277_html: error in retreiving file object");
        return $html_str;
    }
    //
    return $html_str;
}
Example #10
0
function edih_parse_select($file_path)
{
    $csvdata = array();
    // csvdata array design:
    //  $csvdata[$icn]['claims'][i]  [$icn]['files'][i]  [$icn]['type']
    //
    $x12_obj = csv_check_x12_obj($file_path);
    //$x12_obj = new edih_x12_file($file_path);
    if ($x12_obj instanceof edih_x12_file) {
        $ft = $x12_obj->edih_type();
    } else {
        csv_edihist_log('edih_parse_select: error in file path');
        return $csvdata;
    }
    //'HB'=>'271', 'HS'=>'270', 'HR'=>'276', 'HI'=>'278',
    //'HN'=>'277', 'HP'=>'835', 'FA'=>'999', 'HC'=>'837'
    //
    if ($ft == 'HP') {
        $csvdata = edih_835_csv_data($x12_obj);
    } elseif ($ft == 'HC') {
        $csvdata = edih_837_csv_data($x12_obj);
    } elseif ($ft == 'HN' || $ft == 'HR') {
        $csvdata = edih_277_csv_data($x12_obj);
    } elseif ($ft == 'FA') {
        $csvdata = edih_997_csv_data($x12_obj);
    } elseif ($ft == 'HB' || $ft == 'HS') {
        $csvdata = edih_271_csv_data($x12_obj);
    } elseif ($ft == 'HI') {
        $csvdata = edih_278_csv_data($x12_obj);
    } else {
        // debug
        csv_edihist_log('edih_parse_select(): unsupported file type ' . $ft . ' name: ' . basename($file_path));
    }
    //
    return $csvdata;
}
Example #11
0
/**
 * create a display for an individual authorization response	
 * 
 * @uses csv_check_x12_obj()
 * @uses edih_278_transaction_html
 * 
 * @param string  $filename the filename
 * @param string  $bht03 identifier from 837 CLM or27 BHT segment
 * 
 * @return string  either an error message or an html table
 */
function edih_278_html($filename, $bht03 = '')
{
    // create a display for an individual 277 response
    $html_str = '';
    //
    if (!$filename) {
        csv_edihist_log("edih_278_html: called with no file arguments");
        $html_str .= "Error, no file given<br />" . PHP_EOL;
        return $html_str;
    } else {
        $obj278 = csv_check_x12_obj($filename, 'f278');
        if ($obj278 && 'edih_x12_file' == get_class($obj278)) {
            if ($bht03) {
                // particular transaction
                $html_str .= edih_278_transaction_html($obj278, $bht03);
            } else {
                // file contents
                $env_ar = $obj278->edih_envelopes();
                if (!isset($env_ar['ST'])) {
                    $html_str .= "<p>edih_278_html: file parse error, envelope error</p>" . PHP_EOL;
                    $html_str .= $obj278->edih_message();
                    return $html_str;
                } else {
                    $html_str .= "<div id='accordion'>" . PHP_EOL;
                }
                foreach ($env_ar['ST'] as $st) {
                    foreach ($env_ar['GS'] as $gs) {
                        if ($gs['icn'] != $st['icn']) {
                            continue;
                        }
                        if ($gs['gsn'] == $st['gsn']) {
                            $gs_date = edih_format_date($gs['date']);
                            $gs_sender = $gs['sender'];
                            break;
                        }
                    }
                    //
                    // get each transaction
                    foreach ($st['bht03'] as $bht) {
                        $html_str .= "<h3>{$bht} Services Review</h3>" . PHP_EOL;
                        $html_str .= "<div id='ac_{$bht}'>" . PHP_EOL;
                        //
                        $html_str .= edih_278_transaction_html($obj278, $bht);
                        //
                        $html_str .= "</div>" . PHP_EOL;
                    }
                    $html_str .= "</div>" . PHP_EOL;
                }
            }
        } else {
            csv_edihist_log("edih_278_html: error in retreiving file object");
            $html_str .= "<p>x12 278 file parse error</p>" . PHP_EOL;
        }
    }
    //
    return $html_str;
}
Example #12
0
/**
 * The main function in this edih_archive.php script.  This function gets the parameters array
 * from csv_parameters() and calls the archiving functions on each type of file
 * in the parameters array.
 *
 * @uses edih_archive_date()
 * @uses csv_edih_basedir()
 * @uses csv_parameters()
 * @uses csv_edih_tmpdir()
 * @uses csv_table_header()
 * @uses edih_archive_filenames()
 * @uses edih_archive_rewrite_csv()
 * @uses edih_archive_csv_split()
 * @uses edih_archive_create_zip()
 * 
 * @param string 		from select drop-down 6m, 12m, 18m, etc
 * 
 * @return string       descriptive message in html format
 */
function edih_archive_main($period)
{
    //
    $out_html = '';
    if ($period) {
        $archive_date = edih_archive_date($period);
        if ($archive_date) {
            $archive_dir = csv_edih_basedir() . DS . 'archive';
            $tmp_dir = csv_edih_tmpdir();
            $arch_fn = $archive_date . '_archive.zip';
            $params = csv_parameters();
        } else {
            csv_edihist_log("edih_archive_main: error creating archive date from {$period}");
            $out_html = "Error creating archive date from {$period}<br />" . PHP_EOL;
        }
    } else {
        $out_html = "Archive period invalid.<br />" . PHP_EOL;
        return $out_html;
    }
    //
    if (is_dir($archive_dir)) {
        if (is_file($archive_dir . DS . $arch_fn)) {
            csv_edihist_log("edih_archive_main: archive file {$arch_fn} already exists");
            $out_html = "Archive: archive file {$arch_fn} already exists<br>" . PHP_EOL;
            return $out_html;
        }
    } else {
        // should have been created at setup
        if (!mkdir($archive_dir, 0755)) {
            csv_edihist_log("edih_archive_main: archive directory does not exist");
            $out_html = "Archive: archive directory does not exist<br>" . PHP_EOL;
            return $out_html;
        }
    }
    //
    foreach ($params as $k => $p) {
        //
        $ft = $p['type'];
        // could be $k
        //
        if ($ft == 'f837') {
            csv_edihist_log("edih_archive_main: 837 Claims files are not archived");
            continue;
        }
        $fdir = $p['directory'];
        $scan = scandir($fdir);
        if (!$scan || count($scan) < 3) {
            continue;
        }
        //
        $files_csv = $p['files_csv'];
        $claims_csv = $p['claims_csv'];
        $date_col = $p['filedate'];
        $fncol = 'FileName';
        //
        // create three csv file paths 'old_', 'arch_', and 'keep_'
        // files csv temporary names
        $fn_files_old = $tmp_dir . DS . 'old_' . basename($files_csv);
        $fn_files_arch = $tmp_dir . DS . 'arch_' . basename($files_csv);
        $fn_files_keep = $tmp_dir . DS . 'keep_' . basename($files_csv);
        // claims csv temporary names
        $fn_claims_old = $tmp_dir . DS . 'old_' . basename($claims_csv);
        $fn_claims_arch = $tmp_dir . DS . 'arch_' . basename($claims_csv);
        $fn_claims_keep = $tmp_dir . DS . 'keep_' . basename($claims_csv);
        // table headings
        $fh_ar = csv_table_header($ft, 'file');
        $ch_ar = csv_table_header($ft, 'claim');
        // copy existing csv files -- continue to next type if no files_csv
        $iscpc = $iscpf = false;
        if (is_file($files_csv)) {
            $iscpf = copy($files_csv, $fn_files_old);
            csv_edihist_log("edih_archive_main: copy {$ft} files csv to tmp dir");
        } else {
            csv_edihist_log("edih_archive_main: {$ft} files csv does not exist");
            continue;
        }
        if (is_file($claims_csv)) {
            $iscpc = copy($claims_csv, $fn_claims_old);
            csv_edihist_log("edih_archive_main: copy {$ft} claims csv to tmp dir");
        } else {
            if ($ft == 'f997') {
                // there may be no 997 type claims records, so create a dummy file
                $fh = fopen($fn_claims_old, 'wb');
                if ($fh) {
                    fputcsv($fh, $ch_ar);
                    fclose($fh);
                }
            } else {
                csv_edihist_log("edih_archive_main: {$ft} claims csv does not exist");
                continue;
            }
        }
        //
        if (!$iscpf || !$iscpc) {
            csv_edihist_log("edih_archive_csv_old: copy to tmp dir failed for csv file {$ft}");
            $out_html = "Archive temporary files operation failed ... aborting <br />" . PHP_EOL;
            // need to call archive_undo()
            $out_html .= edih_archive_undo();
            return $out_html;
        }
        // get the csv data
        $csv_files_ar = csv_assoc_array($ft, 'file');
        $csv_claims_ar = csv_assoc_array($ft, 'claim');
        // get filenames to be archived
        $fn_ar = array();
        $tp_ar = array();
        $fn_ar = edih_archive_filenames($csv_files_ar, $archive_date);
        if (count($fn_ar)) {
            // add type to list
            $tp_ar[] = $ft;
            // get the old and new csv row arrays for files_csv
            $arch_new = edih_archive_csv_split($csv_files_ar, $fn_ar);
            if ($arch_new) {
                if (isset($arch_new['keep'])) {
                    // write the new
                    $frws = edih_archive_rewrite_csv($fn_files_keep, $fh_ar, $arch_new['keep']);
                    $out_html .= "type {$ft} keep files_csv file with {$frws} rows<br>";
                }
                if (isset($arch_new['arch'])) {
                    // write the old
                    $frws2 = edih_archive_rewrite_csv($fn_files_arch, $fh_ar, $arch_new['arch']);
                    $out_html .= "type {$ft} archive files_csv file with {$frws2} rows<br>";
                }
            } else {
                $out_html .= "type {$ft} error creating new files_csv tables";
            }
            // repeat for claims_csv
            $arch_new = edih_archive_csv_split($csv_claims_ar, $fn_ar);
            if ($arch_new) {
                if (isset($arch_new['keep'])) {
                    // write the new
                    $crws = edih_archive_rewrite_csv($fn_claims_keep, $ch_ar, $arch_new['keep']);
                    $out_html .= "type {$ft} keep claims_csv file with {$crws} rows<br>";
                }
                if (isset($arch_new['arch'])) {
                    // write the old
                    $crws = edih_archive_rewrite_csv($fn_claims_arch, $ch_ar, $arch_new['arch']);
                    $out_html .= "type {$ft} archive claims_csv file with {$crws} rows<br>";
                }
            } else {
                $out_html .= "type {$ft} error creating new claims csv tables<br>";
            }
            // now the csv_records are in files
            // file records in $fn_files_arch  $fn_files_keep
            // claim records in $fn_claims_arch  $fn_claims_new
            //
            // create a zip archive
            // zf is result of zipArchive functions true or false
            $zf = edih_archive_create_zip($p, $fn_ar, $archive_date, $arch_fn);
            //
            // delete archived files
            if ($zf) {
                // replace the csv files
                $rn = rename($fn_files_keep, $files_csv);
                if ($rn) {
                    csv_edihist_log("edih_archive_main: replaced {$files_csv}");
                } else {
                    csv_edihist_log("edih_archive_main: error trying to replace {$files_csv}");
                }
                $rn = rename($fn_claims_keep, $claims_csv);
                if ($rn) {
                    csv_edihist_log("edih_archive_main: replaced {$claims_csv}");
                } else {
                    csv_edihist_log("edih_archive_main: error trying to replace {$claims_csv}");
                }
                // move archive files to tmpdir/ftype
                // $rndir = mkdir($tmpdir.DS.$fdir);
                csv_edihist_log("edih_archive_main: {$ft} now moving old files ");
                $del = edih_archive_move_old($p, $fn_ar);
                $out_html .= "Archive moved {$del} {$ft} type files<br>" . PHP_EOL;
                //
            } else {
                csv_edihist_log("edih_archive_main: type {$ft} error in creating archive");
                $out_html .= "type {$ft} error in creating archive<br>" . PHP_EOL;
            }
        } else {
            csv_edihist_log("edih_archive_main: search found no type {$ft} files older than {$period}");
            $out_html .= "Archive: type {$ft} archive found no files older than {$period}<br>" . PHP_EOL;
        }
    }
    // end foreach($params as $k=>$p)
    //
    if (is_file($tmp_dir . DS . $arch_fn)) {
        $rn = rename($tmp_dir . DS . $arch_fn, $archive_dir . DS . $arch_fn);
        $cm = chmod($archive_dir . DS . $arch_fn, 0400);
        if ($rn) {
            csv_edihist_log("edih_archive_main: moved {$arch_fn} to archive directory");
        } else {
            csv_edihist_log("edih_archive_main: error moving archive file {$arch_fn}");
            $out_html .= "<p>edih_archive_main: error moving archive file {$arch_fn}</p>";
        }
    } else {
        csv_edihist_log("edih_archive_main: is_file false {$tmp_dir}.DS.{$arch_fn}");
    }
    //edih_archive_cleanup($arch_fn, $tp_ar);
    $is_clear = csv_clear_tmpdir();
    if ($is_clear) {
        $out_html .= "Archive: temporary files removed. Process complete.<br>" . PHP_EOL;
    } else {
        $out_html .= "Archive: still some files in /history/tmp/. Process complete.<br>" . PHP_EOL;
    }
    //
    return $out_html;
}
Example #13
0
/**
 * generate output as if file is being processed
 * 
 * @param string  filename
 * @param bool    display errors only 
 * @return string
 */
function ibr_ebr_filetohtml($filepath, $err_only = false)
{
    // simply create an html output for the file
    $html_str = "";
    $data_ar = array();
    $fp = false;
    $ext = substr($filepath, -3);
    if (strpos('|ibr|ebr', $ext)) {
        $fp = csv_verify_file($filepath, "ebr");
    }
    if ($fp) {
        if ($ext == 'ebr') {
            $data_ar = ibr_ebr_values($fp);
        } elseif ($ext == 'ibr') {
            $data_ar = ibr_ibr_values($fp);
        } else {
            csv_edihist_log("ibr_ebr_filetohtml: invalid extension {$ext} " . basename($fp));
            return "<p>invalid extension {$ext} </p>" . PHP_EOL;
        }
        //
        $html_str .= ibr_ebr_html($data_ar, $err_only);
    } else {
        csv_edihist_log("ibr_ebr_filetohtml: verification failed {$filepath}");
        $html_str .= "Error, validation failed {$filepath} <br />";
    }
    //
    return $html_str;
}
Example #14
0
/**
 * Parse x12 file into array of segments.
 * 
 * This function relies on csv_x12_delimiters() to get the delimiters array 
 * and on csv_verify_file() to supply the next_segment value.
 * There are some basic verifications and failures are noted in the log.
 * <pre>
 *  'path'=>pathtofile
 *  'delimiters'=>array from x12_delimiters()
 *  'segments'=>segments[i]=>segment text
 *     if $seg_array is true then segments[i]=>array of elements
 * </pre>
 * 
 * @uses csv_x12_delimiters() 
 * @uses csv_verify_file()
 * @param string $file_path  the full path for the file
 * @param string $type       one of  batch|837|era|835|997|999|277|271
 * @param bool $seg_array    whether each segment should be made into an array of elements
 * @return array|bool        array['delimiters']['segments']['path'], or false on error
 */
function csv_x12_segments($file_path, $type, $seg_array = FALSE)
{
    // do verifications
    $fp_ar = csv_verify_file($file_path, $type, TRUE);
    //
    if (!$fp_ar || !$fp_ar[0]) {
        csv_edihist_log("csv_x12_segments: verification failed for {$file_path}");
        return FALSE;
    }
    //
    if ($fp_ar) {
        //
        $fp = $fp_ar[0];
        $next_segment = $fp_ar[1];
        $f_str = file_get_contents($fp);
        //
        // verify $delimiters
        $delimiters = csv_x12_delimiters(substr($f_str, 0, 126), $next_segment);
        $dlm_ok = FALSE;
        // here, expect $delimiters['t'] ['e'] ['s'] ['r']
        if (is_array($delimiters) && array_keys($delimiters) == array('t', 'e', 's', 'r')) {
            $dlm_ok = TRUE;
            $seg_d = $delimiters['t'];
            $elem_d = $delimiters['e'];
            $subelem_d = $delimiters['s'];
            $rep_d = $delimiters['r'];
        } else {
            csv_edihist_log("csv_x12_segments: invalid delimiters or delimiters wrong for {$fp}");
            return FALSE;
        }
    }
    // OK, now initialize variables
    $fn = basename($fp);
    $ar_seg = array();
    $ar_seg['path'] = $fp;
    $ar_seg['delimiters'] = $delimiters;
    $ar_seg['segments'] = array();
    $seg_pos = 0;
    // position where segment begins
    $st_segs_ct = 0;
    // segments in ST-SE envelope
    $se_seg_ct = "";
    // segment count from SE segment
    $isa_segs_ct = 0;
    // segments in ISA envelope
    $isa_ct = 0;
    // ISA envelope count
    $iea_ct = 0;
    // IEA count
    $trnset_seg_ct = 0;
    // segments by sum of isa segment count
    //
    $isa_str = "ISA" . $elem_d;
    // to reduce evaluations
    $iea_str = "IEA" . $elem_d;
    $st_str = "ST" . $elem_d;
    $se_str = "SE" . $elem_d;
    //
    $idx = -1;
    $moresegs = true;
    while ($moresegs) {
        $idx++;
        // extract each segment from the file text
        $seg_end = strpos($f_str, $seg_d, $seg_pos);
        $moresegs = strpos($f_str, $seg_d, $seg_end + 1);
        //
        $seg_text = substr($f_str, $seg_pos, $seg_end - $seg_pos);
        $seg_pos = $seg_end + 1;
        //
        // we trim in case there are line or carriage returns
        $seg_text = trim($seg_text);
        //
        // check for non ASCII basic characters.  Note reg_ex '/[^\x20-\xFF]/' allows extended ASCII characters
        // this is partly file syntax and partly protection -- we don't want to process an imposter
        // We are mostly concerned with \x00 to \x19, the "control" characters, but apparently some are allowed
        //
        if (preg_match_all('/[^\\x20-\\x7E]/', $seg_text, $matches)) {
            csv_edihist_log("csv_x12_segments: Non-basic ASCII character in segment ({$idx}) in file {$fn}");
            // quit here? return false;  -- actually files have probably been scanned before in upload.php
            // also x12 files have more allowed characters than these
        }
        if ($seg_array) {
            $ar_seg['segments'][] = explode($elem_d, $seg_text);
        } else {
            $ar_seg['segments'][] = $seg_text;
        }
        $st_segs_ct++;
        $isa_segs_ct++;
        //
        // some checks, if wanted
        if (substr($seg_text, 0, 4) == $isa_str) {
            $isa_seg = explode($elem_d, $seg_text);
            $isa_id = $isa_seg[13];
            $isa_segs_ct = 1;
            $isa_ct++;
            continue;
        }
        //
        if (substr($seg_text, 0, 3) == $st_str) {
            // $e = strpos($seg_text, $elem_d, 8);  // ST*835* is 7 characters
            // $st02 = substr($seg_text, 7, $e-7);
            $st_ar = explode($elem_d, $seg_text);
            $st_num = $st_ar[2];
            $st_segs_ct = 1;
            continue;
        }
        if (substr($seg_text, 0, 3) == $se_str) {
            $se_ar = explode($elem_d, $seg_text);
            $se_seg_ct = $se_ar[1];
            $se_num = $se_ar[2];
            if ($se_num != $st_num) {
                csv_edihist_log("csv_x12_segments: ST-SE number mismatch {$st_num} {$se_num} in {$fn}");
            }
            if (intval($se_seg_ct) != $st_segs_ct) {
                csv_edihist_log("csv_x12_segments: ST-SE segment count mismatch {$st_segs_ct} {$se_seg_ct} in {$fn}");
            }
            continue;
        }
        if (substr($seg_text, 0, 4) == $iea_str) {
            $iea_seg = explode($elem_d, $seg_text);
            $iea_id = $iea_seg[2];
            $iea_ct++;
            //
            if ($isa_id != $iea_id) {
                csv_edihist_log("csv_x12_segments: ISA-IEA identifier mismatch set {$iea_ct} in {$fn}");
            }
            if ($iea_ct == $isa_ct) {
                $trnset_seg_ct += $isa_segs_ct;
                if ($idx + 1 != $trnset_seg_ct) {
                    //
                    csv_edihist_log("csv_x12_segments: IEA segment count error ({idx+1}:{$trnset_seg_ct} set) {$iea_ct} in {$fn}");
                }
            } else {
                csv_edihist_log("csv_x12_segments: ISA-IEA count mismatch set {$isa_ct} {$iea_ct} in {$fn}");
            }
        }
        //
    }
    //
    return $ar_seg;
}
Example #15
0
/**
* A function to try and assure the pid-encounter is correctly parsed
*
* assume a format of pid-encounter, since that is sent in the OpenEMR x12 837
*
* @param string $pid_enctr   the value from element CLM01
* return array               array('pid' => $pid, 'enctr' => $enc)
*/
function csv_pid_enctr_parse($pid_enctr)
{
    // evaluate the patient account field
    //
    if (!$pid_enctr || !is_string($pid_enctr)) {
        csv_edihist_log("csv_pid_enctr_parse: invalid argument");
        return false;
    }
    $pval = trim($pid_enctr);
    if (strpos($pval, '-')) {
        $pid = substr($pval, 0, strpos($pval, '-'));
        $enc = substr($pval, strpos($pval, '-') + 1);
    } elseif (ctype_digit($pval)) {
        if (preg_match('/(19|20)\\d{2}[01]\\d{1}[0-3]\\d{1}/', $pval)) {
            $enc = $pval;
        } else {
            $enc = strlen($pval) >= ENCOUNTER_MIN_DIGIT_LENGTH ? $pval : '';
            $pid = '';
        }
    } elseif (preg_match('/\\D/', $pval, $match2, PREG_OFFSET_CAPTURE)) {
        $inv_split = count($match2) ? preg_split('/\\D/', $pval, 2, PREG_SPLIT_NO_EMPTY) : false;
        if ($inv_split) {
            $pid = $inv_split[0];
            $enc = $inv_split[1];
        }
    } else {
        $enc = strlen($pval) >= ENCOUNTER_MIN_DIGIT_LENGTH ? $pval : '';
        $pid = '';
    }
    return array('pid' => $pid, 'enctr' => $enc);
}
Example #16
0
/**
 * Process new x12 835 files so data is written to csv tables and html output is created
 * 
 * This is a multi-operational function, calling other functions in this script and in 
 * csv_record_include.php to list newly uploaded files, parse them for csv data, write 
 * to the csv data files, and generate html output.
 * 
 * @uses csv_newfile_list()
 * @uses csv_x12_segments()
 * @uses ibr_era_csv_file_data()
 * @uses ibr_era_csv_write_data()
 * @uses ibr_era_csv_files_html()
 * 
 * @param array $file_array     not required, array of new filenames, generated by csv_newfile_list()
 * @param boolean $html_out     whether to generate html files summary table
 * @param boolean $err_only     whether to generate html for denied claims
 * @return string               html output
 */
function ibr_era_process_new($file_array = NULL, $html_out = TRUE, $err_only = TRUE)
{
    //
    // get new files from directory and write data to the .csv files
    //
    // create html table if $html_out is true
    // 'mtime', 'dirname', 'fname', 'trace' 'payer' 'claims'
    $html_str = "";
    $ar_csv_data = array();
    //
    $err_ref = "";
    //
    if (is_array($file_array) && count($file_array)) {
        $ar_newfiles = $file_array;
    } else {
        $ar_newfiles = csv_newfile_list("era");
    }
    // cut it off if there are no new files
    if (count($ar_newfiles) == 0) {
        // no new files
        if ($html_out) {
            // return a message
            $html_str = "<p>ibr_era_process_new: No new ERA 835 files found</p>";
            return $html_str;
        } else {
            return FALSE;
        }
    } else {
        $fcount = count($ar_newfiles);
    }
    // we have new files
    foreach ($ar_newfiles as $f_era) {
        //
        $era_segments = csv_x12_segments($f_era, "era", FALSE);
        //
        if (is_array($era_segments) && count($era_segments['segments']) > 0) {
            $ar_csv_data[] = ibr_era_csv_file_data($era_segments);
            //
        } else {
            csv_edihist_log("ibr_era_process_new: did not get segments for " . basename($full_path));
            $htm_hdrc .= "<p>did not get segments for {$f_era} </p>" . PHP_EOL;
            continue;
        }
    }
    // now send the data to be written to csv table and html output
    foreach ($ar_csv_data as $eradata) {
        $chars = ibr_era_csv_write_data($eradata, $err_ref);
        //$html_out
        $html_str .= isset($htm_hdrc) ? $htm_hdrc : "";
        //
        if (is_array($chars)) {
            if ($chars[0]) {
                csv_edihist_log("ibr_era_process_new: {$chars[0]} characters written to files_era.csv");
            } else {
                csv_edihist_log("ibr_era_process_new: error writing csv data for files: " . PHP_EOL . $err_ref);
                $html_str .= "<p>ibr_era_process_new: error writing csv data</p>";
            }
            if ($chars[1]) {
                csv_edihist_log("ibr_era_process_new: {$chars[1]} characters written to claims_era.csv");
            } else {
                csv_edihist_log("ibr_era_process_new: error writing csv data for claims ");
                $html_str .= "<p>ibr_era_process_new: error writing csv data</p>";
            }
        }
    }
    //
    if ($html_out) {
        $html_str .= ibr_era_csv_files_html($ar_csv_data, $err_only);
    } elseif ($chars[0] && $chars[1]) {
        $html_str .= "x12_835 ERA files: processed {$fcount} ERA files <br />" . PHP_EOL;
    } else {
        $html_str .= "x12_835 ERA: error writing csv data <br />";
    }
    return $html_str;
}
Example #17
0
/**
 * create a display for an individual claim status response	
 * 
 * @uses csv_check_x12_obj()
 * @uses edih_271_transaction_html()
 * 
 * @param string  $filename the filename
 * @param string  $bht03 identifier from 837 CLM or BHT segment
 * 
 * @return string  either an error message or an html table 
 */
function edih_271_html($filename, $bht03 = '')
{
    // create a display for an individual 277 response
    $html_str = '';
    //
    if ($filename) {
        $obj271 = csv_check_x12_obj($filename, 'f271');
        if ('edih_x12_file' == get_class($obj271)) {
            if ($bht03) {
                // particular transaction
                $html_str .= edih_271_transaction_html($obj271, $bht03);
            } else {
                // file contents
                $env_ar = $obj271->edih_envelopes();
                if (!isset($env_ar['ST'])) {
                    $html_str .= "<p>edih_271_html: file parse error, envelope error</p>" . PHP_EOL;
                    $html_str .= $obj271->edih_message();
                    return $html_str;
                } else {
                    $html_str .= "<div id='accordion'>" . PHP_EOL;
                }
                foreach ($env_ar['ST'] as $st) {
                    foreach ($env_ar['GS'] as $gs) {
                        if ($gs['icn'] != $st['icn']) {
                            continue;
                        }
                        if ($gs['gsn'] == $st['gsn']) {
                            $gs_date = edih_format_date($gs['date']);
                            $gs_sender = $gs['sender'];
                            break;
                        }
                    }
                    //
                    // get each transaction
                    foreach ($st['bht03'] as $bht) {
                        $html_str .= "<h3>{$bht} Benefit Eligibility</h3>" . PHP_EOL;
                        $html_str .= "<div id='ac_{$bht}'>" . PHP_EOL;
                        //
                        $html_str .= edih_271_transaction_html($obj271, $bht);
                        //
                        $html_str .= "</div>" . PHP_EOL;
                    }
                    $html_str .= "</div>" . PHP_EOL;
                }
            }
        } else {
            $html_str .= "<p>edih_271_html: file parse error</p>" . PHP_EOL;
        }
    } else {
        csv_edihist_log("edih_271_html: error in file arguments");
        $html_str .= "Error: invalid file name<br />" . PHP_EOL;
        return $html_str;
    }
    //
    return $html_str;
}
Example #18
0
/**
 * Process new batch files for csv data and html output
 * 
 * The html output is only a file summary, no claim detail is created
 * 
 * 
 * @uses csv_verify_file()
 * @uses ibr_batch_csv_data()
 * @uses csv_write_record() 
 * @uses csv_newfile_list()
 * @param array $file_array   optional default is NULL 
 * @param boolean $html_out   whether to generate html files summary table
 * @return string             html output only for files table
 */
function ibr_batch_process_new($file_array = NULL, $html_out = TRUE)
{
    //
    // 'mtime', 'dirname', 'fname', 'trace' 'payer' 'claims'
    $html_str = "";
    $chars1 = 0;
    $chars2 = 0;
    //$chars2 = 0;
    $idx = 0;
    $ar_batchf = array();
    //$need_dir = TRUE;
    // get the list of new files
    if ($file_array === NULL || !is_array($file_array) || count($file_array) == 0) {
        $ar_newfiles = csv_newfile_list("batch");
    } else {
        $ar_newfiles = $file_array;
        //$need_dir = FALSE;
    }
    //
    if (count($ar_newfiles) == 0) {
        if ($html_out) {
            $html_str .= "<p>ibr_batch_process_new: no new batch files <br />";
            return $html_str;
        } else {
            return false;
        }
    } else {
        $btfcount = count($ar_newfiles);
    }
    // we have some new ones
    // verify and get complete path
    foreach ($ar_newfiles as $fbt) {
        $fp = csv_verify_file($fbt, 'batch', false);
        if ($fp) {
            $ar_batchf[] = $fp;
        }
    }
    $p = csv_parameters("batch");
    $b_dir = $p['directory'];
    //
    if ($html_out) {
        $html_str .= "<table cols=5 class=\"batch\">\n\t\t  <caption>Batch Files CSV</caption>\n\t\t  <thead>\n\t\t\t<tr>\n\t\t\t  <th>File Time</th><th>File Name</th>\n\t\t\t  <th>Control</th><th>Claims</th><th>x12_Partner</th>\n\t\t\t</tr>\n\t\t  </thead>\n\t\t  <tbody>";
    }
    foreach ($ar_batchf as $f_batch) {
        //
        // increment counter for alternating html backgrounds
        $bgc = $idx % 2 == 1 ? 'odd' : 'even';
        $idx++;
        //
        //$full_path = ($need_dir) ? $b_dir.DIRECTORY_SEPARATOR.$f_batch : $f_batch;
        // get the file data for csv output
        //$ar_csv_data = ibr_batch_csv_data($full_path);
        $ar_csv_data = ibr_batch_csv_data($f_batch);
        //
        // write to the files_batch csv record
        $chars1 += csv_write_record($ar_csv_data['file'], 'batch', 'file');
        $chars2 += csv_write_record($ar_csv_data['claim'], 'batch', 'claim');
        //
        // link for viewing file <a href=\"edi_view_file.php?\'fvkey\'=$dta\" target=\"_blank\">$dta</a></td>";
        if ($html_out) {
            $html_str .= "<tr class=\"{$bgc}\">\n                 <td>{$ar_csv_data['file'][0]}</td>\n                 <td><a target='_blank' href='edi_history_main.php?fvkey={$ar_csv_data['file'][1]}'>{$ar_csv_data['file'][1]}</a></td>\n                 <td>{$ar_csv_data['file'][2]}</td>\n                 <td>{$ar_csv_data['file'][3]}</td>\n                 <td>{$ar_csv_data['file'][4]}</td>\n               </tr>";
        }
    }
    if ($html_out) {
        $html_str .= "</tbody>\n\t\t   </table>\n\t\t <p></p>";
    }
    //
    csv_edihist_log("ibr_batch_process_new: {$chars1} characters written to files_batch.csv");
    csv_edihist_log("ibr_batch_process_new: {$chars2} characters written to claims_batch.csv");
    //
    if ($html_out) {
        return $html_str;
    } else {
        return "<p>Batch files: processed {$btfcount} files </p>";
    }
}
Example #19
0
/**
 * process new .ta1 files
 * 
 * 
 * @uses ibr_process_ta1()
 * @uses ibr_ack_html()
 * @uses csv_newfile_list()
 * @uses csv_verify_file()
 * @param array|string -- optional array of filenames or filename
 * @param bool --whether to produce html table
 * @return string
 */
function ibr_ta1_process_new($ta1_files, $html_out = TRUE)
{
    //
    $str_html = "";
    //
    if (is_array($ta1_files)) {
        $new_files = $ta1_files;
    } elseif (is_string($ta1_files) && strlen($ta1_files)) {
        $new_files[] = $ta1_files;
    } else {
        $new_files = csv_newfile_list("ta1");
    }
    //
    if (is_array($new_files) && count($new_files) > 0) {
        $fcount = count($new_files);
        foreach ($new_files as $ta) {
            $fp = csv_verify_file($ta, 'ta1');
            if ($fp) {
                $ar_d[] = ibr_process_ta1($fp);
            } else {
                $str_html .= "TA1 failed to verify file: {$ta} <br />" . PHP_EOL;
                csv_edihist_log("ibr_ta1_process_new: failed to verify file: {$ta}");
            }
        }
    }
    // write a line to csv file -- put in file_997.csv
    if (is_array($ar_d) && count($ar_d)) {
        foreach ($ar_d as $arec) {
            // do not put message, batch, or text file in csv record
            $ar_csv[] = array_slice($arec, 0, count($arec) - 3);
        }
        $rslt = csv_write_record($ar_csv, "ta1", "file");
        csv_edihist_log("ibr_ta1_process_new: {$rslt} characters written to files_997.csv");
        if ($html_out) {
            $str_html .= ibr_ack_html($ar_d);
        } else {
            $str_html .= "TA1 files: processed {$fcount} TA1 files <br />" . PHP_EOL;
        }
    } else {
        $str_html .= "No new TA1 files found. <br />" . PHP_EOL;
    }
    //
    return $str_html;
}
Example #20
0
/**
 * Main function in this script
 *
 * @uses csv_check_x12_obj()
 * @uses edih_997_errdata()
 * @uses edih_997_err_report()
 * 
 * @param string
 * @return string
 */
function edih_997_error($filepath)
{
    //
    $html_str = '';
    //
    $obj997 = csv_check_x12_obj($filepath, 'f997');
    if ($obj997 && 'edih_x12_file' == get_class($obj997)) {
        $data = edih_997_errdata($obj997);
        $html_str .= edih_997_err_report($data);
    } else {
        $html_str .= "<p>Error: invalid file path</p>" . PHP_EOL;
        csv_edihist_log("edih_997_error: invalid file path {$filepath}");
    }
    return $html_str;
}
Example #21
0
/**
 * create a display for an 835 claim payment file or transaction
 *
 * @uses csv_check_x12_obj()
 * 
 * @param string  $filename the filename
 * @param string  TRN02 identifier from 835 check ir EFT
 * @param string  CLM01 identifier from 837 CLM
 * 
 * @return string  error message or a table with file information
 */
function edih_835_html($filename, $trace = '', $clm01 = '', $summary = false)
{
    //
    $html_str = '';
    //
    if (trim($filename)) {
        $obj835 = csv_check_x12_obj($filename, 'f835');
        if ($obj835 && 'edih_x12_file' == get_class($obj835)) {
            $fn = $obj835->edih_filename();
            $delims = $obj835->edih_delimiters();
            $env_ar = $obj835->edih_x12_envelopes();
            //
            $de = isset($delims['e']) ? $delims['e'] : '';
            $ds = isset($delims['s']) ? $delims['s'] : '';
            $dr = isset($delims['r']) ? $delims['r'] : '';
            // $dr is not used, but just in case
        } else {
            $html_str .= "<p>edih_835_html: invalid file name</p>" . PHP_EOL;
            return $html_str;
        }
    } else {
        $html_str .= "Error in file name or file parsing <br />" . PHP_EOL;
        csv_edihist_log("edih_835_html: error in parsing file {$filename}");
        return $html_str;
    }
    if ($de && $ds) {
        // note $dr, repetition separator, is not always available
        $cd27x = new edih_271_codes($ds, $dr);
        $cd835 = new edih_835_codes($ds, $dr);
    } else {
        csv_edihist_log("edih_835_html: Did not get delimiters");
        $html_str .= "<p>Did not get delimiters for {$fn}</p>" . PHP_EOL;
        return $html_str;
    }
    //
    // if given, one only of trace or clm01
    $pid = $chk = '';
    if ($clm01) {
        $pid = trim((string) $clm01);
    } elseif ($trace) {
        $chk = trim((string) $trace);
    }
    //
    if ($pid) {
        $clp_ar = $obj835->edih_x12_transaction($pid);
        // $clp_ar is array[i][j]
        if (count($clp_ar)) {
            if ($summary) {
                $html_str .= edih_835_clp_summary($clp_ar, $cd27x, $cd835, $delims, $fn);
            } else {
                // create a table for this transaction for jquery-ui dialog
                $html_str .= edih_835_transaction_html($clp_ar, $cd27x, $cd835, $delims, $fn);
            }
        } else {
            csv_edihist_log("edih_835_html: Did not find PtID {$pid} in {$fn}");
            $html_str .= "<p>Did not find PtID {$pid} in {$fn}</p>" . PHP_EOL;
            return $html_str;
        }
    } elseif ($chk) {
        // check detail
        if (isset($env_ar['ST']) && count($env_ar['ST'])) {
            $trans_ar = array();
            foreach ($env_ar['ST'] as $st) {
                if ($st['trace'] != $chk) {
                    continue;
                }
                $trans_ar[] = $obj835->edih_x12_slice(array('trace' => $chk));
            }
        } else {
            csv_edihist_log("edih_835_transaction_html: Did not get envelopes information for {$fn}");
            $html_str .= "<p>Did not get envelopes information for {$fn}</p>" . PHP_EOL;
            return $html_str;
        }
        if (is_array($trans_ar) && count($trans_ar)) {
            // $trans_ar is a slice, array[i]
            $html_str .= edih_835_payment_html($trans_ar, $cd27x, $cd835, $delims, $fn);
        } else {
            csv_edihist_log("edih_835_transaction_html: Did not find trace {$chk} in {$fn}");
            $html_str .= "<p>Did not find trace {$chk} in {$fn}</p>" . PHP_EOL;
            return $html_str;
        }
    } else {
        // entire file
        if (isset($env_ar['ST']) && count($env_ar['ST'])) {
            $trans_ar = array();
            foreach ($env_ar['ST'] as $st) {
                $trans_ar[] = $obj835->edih_x12_slice(array('trace' => $st['trace']));
            }
        } else {
            csv_edihist_log("edih_835_transaction_html: Did not envelopes information for {$fn}");
            $html_str .= "<p>Did not get envelopes information for {$fn}</p>" . PHP_EOL;
            return $html_str;
        }
        if (is_array($trans_ar) && count($trans_ar)) {
            // $trans_ar is a slice, array[i]
            $html_str .= edih_835_payment_html($trans_ar, $cd27x, $cd835, $delims, $fn);
        } else {
            csv_edihist_log("edih_835_transaction_html: Did not get ST envelopes for {$fn}");
            $html_str .= "<p>Did not get ST envelopes for {$fn}</p>" . PHP_EOL;
            return $html_str;
        }
    }
    //
    return $html_str;
}