Example #1
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 #2
0
/**
 * Search claims_era.csv table and find patient ID or encounter and associated filename
 * 
 * will search for only the pid value or only encounter value
 * 
 * @uses csv_pid_enctr_parse()
 * @uses csv_parameters()
 * @param string $encounter   the patient control (pid-encounter) number
 * @param string $srchtype    default is 'encounter'
 * @return array              [i](pid-enctr, trace, filename) possibly more than one
 */
function ibr_era_find_file_with_pid_enctr($pid_enctr, $srchtype = 'encounter')
{
    //
    // return the pid_encounter, trace, and filename, but there may be more than one file, so return an array
    //
    if (!$pid_enctr) {
        return "invalid encounter data<br />" . PHP_EOL;
    }
    $enctr = trim($pid_enctr);
    preg_match('/\\D/', $enctr, $match2, PREG_OFFSET_CAPTURE);
    if (count($match2)) {
        $idar = csv_pid_enctr_parse($enctr);
        if (is_array($idar) && count($idar)) {
            $p = strval($idar['pid']);
            $plen = strlen($p);
            $e = strval($idar['enctr']);
            $elen = strlen($e);
        } else {
            csv_edihist_log("ibr_era_find_file_with_pid_enctr: error parsing pid_encounter {$pid_enctr}");
            return FALSE;
        }
    } else {
        $p = strval($enctr);
        $e = strval($enctr);
        $plen = strlen($p);
        $elen = strlen($e);
    }
    $ret_ar = array();
    //array('PtName', 'SvcDate', 'clm01', 'Status', 'trace', 'claimID', 'File_835', 'Pmt', 'PtResp', 'Payer');
    //
    $params = csv_parameters('era');
    $fp = $params['claims_csv'];
    //
    if (($fh1 = fopen($fp, "r")) !== FALSE) {
        if ($srchtype == 'encounter') {
            while (($data = fgetcsv($fh1, 1024, ",")) !== FALSE) {
                // check for a match
                if (substr($data[2], -$elen) == $e) {
                    // since e=123 will match 1123 and 123
                    $peval = csv_pid_enctr_parse($data[2]);
                    if (is_array($peval) && count($peval)) {
                        if ($peval['enctr'] == $e) {
                            $ret_ar[] = array($data[2], $data[4], $data[5]);
                        }
                    }
                }
            }
        } else {
            while (($data = fgetcsv($fh1, 1024, ",")) !== FALSE) {
                // check for a match
                if (substr($data[2], 0, $plen) == $p) {
                    // since p=123 will match 1123 and 123
                    $peval = csv_pid_enctr_parse($data[2]);
                    if (is_array($peval) && count($peval)) {
                        if ($peval['pid'] == $p) {
                            $ret_ar[] = array($data[2], $data[4], $data[5]);
                        }
                    }
                }
            }
        }
        fclose($fh1);
    } else {
        csv_edihist_log("ibr_era_find_file_with_pid: failed to open file claims_era.csv");
    }
    return $ret_ar;
}
Example #3
0
/**
 * Save the uploaded files array to the correct directory
 *
 * If a matching filename file is already in the directory it is not overwritten.
 * The uploaded file will just be discarded.
 *
 * @uses csv_parameters()
 * @see edih_upload_files()
 * @param array $files_array  files array created by edih_upload_files()
 * @param bool       -- whether to return html output
 * @param bool       -- whether to only report errors (ignored)
 * @return string    html formatted messages
 */
function edih_sort_upload($files_array, $html_out = true, $err_only = true)
{
    //
    $prc_htm = '';
    $rmk_htm = '';
    $dirpath = csv_edih_basedir();
    //
    if (is_array($files_array) && count($files_array)) {
        // we have some files
        $p_ar = csv_parameters($type = "ALL");
        //
        $prc_htm .= "<p><em>Received Files</em></p>" . PHP_EOL;
        foreach ($files_array as $key => $val) {
            //
            $prc_htm .= "<ul class='fupl'>" . PHP_EOL;
            if (isset($p_ar[$key])) {
                $tp_dir = $p_ar[$key]['directory'];
                $tp_base = basename($tp_dir);
                $idx = 0;
                $prc_htm .= "<li>type {$key}</li>" . PHP_EOL;
                if (!is_array($val) || !count($val)) {
                    $prc_htm .= "<li>no new files</li>" . PHP_EOL;
                    continue;
                }
                foreach ($val as $idx => $nf) {
                    // check if the file has already been stored
                    // a matching file name will not be replaced
                    $nfb = basename($nf);
                    $testname = $tp_dir . DS . $nfb;
                    $prc_htm .= "<li>{$nfb}</li>" . PHP_EOL;
                    if (is_file($testname)) {
                        $prc_htm .= "<li> -- file exists</li>" . PHP_EOL;
                    } elseif (rename($nf, $testname)) {
                        $iscm = chmod($testname, 0400);
                        if (!$iscm) {
                            // if we could write, we should be able to set permissions
                            $prc_htm .= "<li> -- file save error</li>" . PHP_EOL;
                            unlink($testname);
                        }
                    } else {
                        $prc_htm .= "<li> -- file save error</li>" . PHP_EOL;
                    }
                }
            } elseif ($key == 'reject') {
                $prc_htm .= "<li><bd>Reject:</bd></li>" . PHP_EOL;
                foreach ($val as $idx => $nf) {
                    $prc_htm .= "<li>" . $nf['name'] . "</li>" . PHP_EOL;
                    $prc_htm .= "<li> --" . $nf['comment'] . "</li>" . PHP_EOL;
                }
            } elseif ($key == 'remark') {
                $rmk_htm .= "<p><bd>Remarks:</bd><br>" . PHP_EOL;
                foreach ($val as $idx => $r) {
                    $rmk_htm .= $r . "<br>" . PHP_EOL;
                }
                $rmk_htm .= "</p>" . PHP_EOL;
            } else {
                $prc_htm .= "<li>{$key} type not stored</li>" . PHP_EOL;
                foreach ($val as $idx => $nf) {
                    $prc_htm .= "<li>" . basename($nf) . "</li>" . PHP_EOL;
                }
            }
            $prc_htm .= "</ul>" . PHP_EOL;
            $prc_htm .= $rmk_htm . PHP_EOL;
        }
    } else {
        // should not happen since this function should not be called unless there are new files
        $prc_htm .= "<ul><li>No files submitted</li></ul>" . PHP_EOL;
    }
    //
    return $prc_htm;
}
Example #4
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 #5
0
/**
 * Check that the file path we are working with is a readable file.
 * 
 * If it is a file we have uploaded and we have only the file name
 * this function will type the file and find it in the uploaded files directories
 * and return the complete path.
 * 
 * @uses csv_parameters()
 * @param string $filename    name of a file that is one of our types
 * @param string $type        optional; one of our file types
 * @return string             either an empty string or a readable filepath
 */
function csv_check_filepath($filename, $type = "ALL")
{
    //
    // if file is readable, just return it
    if (is_file($filename) && is_readable($filename)) {
        return $filename;
    }
    //
    $goodpath = "";
    $fn = basename($filename);
    //
    if ($type != "ALL") {
        $p = csv_parameters($type);
        if (is_array($p) && array_key_exists('type', $p)) {
            // type was found
            $fp = $p['directory'] . DIRECTORY_SEPARATOR . $fn;
            if (is_file($fp) && is_readable($fp)) {
                $goodpath = $fp;
            }
        } else {
            csv_edihist_log("csv_check_filepath: invalid type {$type}");
        }
    } else {
        $p_ar = csv_parameters("ALL");
        foreach ($p_ar as $tp => $par) {
            //
            if (!$p_ar[$tp]['regex'] || !preg_match($p_ar[$tp]['regex'], $fn)) {
                continue;
            } else {
                //
                $fp = $p_ar[$tp]['directory'] . DIRECTORY_SEPARATOR . $fn;
                if (is_file($fp) && is_readable($fp)) {
                    $goodpath = $fp;
                }
                break;
            }
        }
    }
    //
    return $goodpath;
}
Example #6
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 #7
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;
}
Example #8
0
/**
 * call new uploaded files process functions
 * 
 * @todo    save the newfiles lists to file so they can
 *          be re-displayed if user has to close app before
 *          finishing review (need to have csv_write option)
 * 
 * @uses csv_newfile_list()
 * @uses ibr_batch_process_new()
 * @uses ibr_ack_process_new()
 * @uses ibr_997_process_new()
 * @uses ibr_277_process_new()
 * @uses ibr_ebr_process_new()
 * @uses ibr_dpr_process_new()
 * @uses ibr_era_process_new()
 * 
 * @return string  html format
 */
function ibr_disp_newfiles()
{
    //
    if (!isset($_POST['NewFiles'])) {
        // should only be called with this value existing
        $str_html = "Error: invalid value for Process New <br />" . PHP_EOL;
        return $str_html;
    }
    $htm = $er = false;
    if (isset($_POST['htmlout'])) {
        $htmval = filter_input(INPUT_POST, 'htmlout', FILTER_SANITIZE_STRING);
        $htm = $htmval == 'on' ? true : false;
    }
    if (isset($_POST['erronly'])) {
        $errval = filter_input(INPUT_POST, 'erronly', FILTER_SANITIZE_STRING);
        $er = $errval == 'on' ? true : false;
    }
    $str_html = "<p>Process new files</p>" . PHP_EOL;
    //
    $p = csv_parameters();
    $ftype = array_keys($p);
    //
    foreach ($ftype as $tp) {
        $checkdir = false;
        // check for directory contents
        $ckdir = $p[$tp]['directory'];
        if (is_dir($ckdir)) {
            $dh = opendir($ckdir);
            if ($dh) {
                while (($file = readdir($dh)) !== false) {
                    if ($file != '.' && $file != '..') {
                        $checkdir = true;
                        break;
                    }
                }
                closedir($dh);
            }
        }
        // if false, no files in directory
        if (!$checkdir) {
            continue;
        }
        //
        $upload_ar = csv_newfile_list($tp);
        //
        if (count($upload_ar) > 0) {
            if ($tp == 'batch') {
                $str_html .= ibr_batch_process_new($upload_ar, $htm);
            } elseif ($tp == 'ack') {
                $str_html .= ibr_ack_process_new($upload_ar, $htm);
            } elseif ($tp == 'ta1') {
                $str_html .= ibr_ta1_process_new($upload_ar, $htm);
            } elseif ($tp == 'f997') {
                $str_html .= ibr_997_process_new($upload_ar, $htm, $er);
            } elseif ($tp == 'f277') {
                $str_html .= ibr_277_process_new($upload_ar, $htm, $er);
            } elseif ($tp == 'ibr') {
                $str_html .= ibr_ebr_process_new_files($upload_ar, 'ibr', $htm, $er);
            } elseif ($tp == 'ebr') {
                $str_html .= ibr_ebr_process_new_files($upload_ar, 'ebr', $htm, $er);
            } elseif ($tp == 'dpr') {
                $str_html .= ibr_dpr_process_new($upload_ar, $htm, $er);
            } elseif ($tp == 'era') {
                $str_html .= ibr_era_process_new($upload_ar, $htm, $er);
            } elseif ($tp == 'text') {
                // do nothing
                continue;
            } else {
                $str_html .= "unknown type {$tp} <br />" . PHP_EOL;
            }
        } else {
            $str_html .= "No new files for type {$tp} <br />" . PHP_EOL;
        }
    }
    return $str_html;
}
Example #9
0
/**
 * list claim records with Denied or Reject status in  given file
 *
 * @param string
 * @param string
 *
 * @return array
 */
function csv_denied_by_file($filetype, $filename, $trace = '')
{
    //
    $ret_ar = array();
    $ft = csv_file_type($filetype);
    if (strpos('|f997|f271|f277|f835', $ft)) {
        $param = csv_parameters($ft);
        $csv_file = $param['claims_csv'];
    } else {
        csv_edihist_log("csv_errors_by_file: incorrect file type {$filetype}");
        return $ret_ar;
    }
    //
    csv_edihist_log("csv_errors_by_file: {$ft} searching {$filename} with trace {$trace}");
    //
    if (($fh1 = fopen($csv_file, "r")) !== false) {
        if ($ft == 'f835') {
            while (($data = fgetcsv($fh1, 1024, ",")) !== false) {
                // check filename, then status
                if ($trace) {
                    if ($data[4] == $trace) {
                        if (!in_array($data[3], array('1', '2', '3', '19', '20', '21'))) {
                            $ret_ar[] = $data;
                        }
                    }
                } elseif ($data[5] == $filename) {
                    if (!in_array($data[3], array('1', '2', '3', '19', '20', '21'))) {
                        $ret_ar[] = $data;
                    }
                }
            }
        } elseif ($ft == 'f277') {
            while (($data = fgetcsv($fh1, 1024, ",")) !== false) {
                if ($data[5] == $filename) {
                    if (!strpos('|A1|A2|A5', substr($data[3], 0, 2))) {
                        $ret_ar[] = $data;
                    }
                }
            }
        } elseif (strpos('|f997|f999|f271', $ft)) {
            while (($data = fgetcsv($fh1, 1024, ",")) !== false) {
                if ($data[5] == $filename) {
                    if ($data[3] !== 'A') {
                        $ret_ar[] = $data;
                    }
                }
            }
        } else {
            csv_edihist_log("csv_errors_by_file: file type did not match {$filetype}");
        }
        fclose($fh1);
    }
    //
    return $ret_ar;
}
Example #10
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 #11
0
/**
 * call new uploaded files process functions
 * 
 * @todo    save the newfiles lists to file so they can
 *          be re-displayed if user has to close app before
 *          finishing review (need to have csv_write option)
 * 
 * @uses csv_newfile_list()
 * @uses edih_parse_select()
 * @uses edih_csv_write()
 * @uses edih_csv_process_html()
 * 
 * @return string  html format
 */
function edih_disp_file_process()
{
    //file "fileUplMulti "submit" "uplsubmit" "button""uplreset"
    //
    // debug
    if (isset($_GET)) {
        $dbg_str = 'GET vars ';
        foreach ($_GET as $k => $v) {
            $dbg_str .= $k . ' ' . $v . '  ';
        }
        csv_edihist_log("edih_disp_file_process {$dbg_str}");
    }
    //
    if (!isset($_GET['ProcessFiles'])) {
        // should only be called with this value existing
        $str_html = "Error: invalid value for Process New <br />" . PHP_EOL;
        return $str_html;
    }
    $htm = $er = false;
    if (isset($_GET['process_html'])) {
        // show tables for process results
        $htmval = filter_input(INPUT_GET, 'process_html', FILTER_SANITIZE_STRING);
        $htm = $htmval == 'htm' ? true : false;
    }
    if (isset($_GET['process_err'])) {
        // show only claims with errors (denied, rejected, etc)
        $errval = filter_input(INPUT_GET, 'process_err', FILTER_SANITIZE_STRING);
        $er = $errval == 'err' ? true : false;
    }
    $str_html = "";
    //
    $p = csv_parameters();
    $ftype = array_keys($p);
    $fct = 0;
    //
    foreach ($ftype as $tp) {
        $checkdir = false;
        // check for directory contents
        $fdir = $p[$tp]['directory'];
        if (is_dir($fdir)) {
            $dh = opendir($fdir);
            if ($dh) {
                while (($file = readdir($dh)) !== false) {
                    if ($file != '.' && $file != '..') {
                        $checkdir = true;
                        break;
                    }
                }
                closedir($dh);
            }
        }
        // if false, no files in directory
        if (!$checkdir) {
            continue;
        }
        //
        $upload_ar = csv_newfile_list($tp);
        //
        if (is_array($upload_ar) && count($upload_ar)) {
            $dirct = count($upload_ar);
            if ($htm) {
                $dtl = $er ? "(claims: errors only)" : "(claims: all)";
                //$hvals = csv_table_header($tp, 'file');
                //$col_ct = count($hvals);
                ////csv_table_header(
                //$str_html .= "<table class='$tp' cols=$col_ct><caption>$tp Files Summary $dtl</caption>".PHP_EOL;
                //$str_html .= csv_thead_html($tp, 'file');
                //$str_html .= "<tbody>".PHP_EOL;
                $str_html .= "<h2 class='prcs'>{$tp} {$dirct} files {$dtl}</h2>" . PHP_EOL;
                $str_html .= "<dl class='{$tp}'>" . PHP_EOL;
            }
            foreach ($upload_ar as $fn) {
                $fp = $fdir . DS . $fn;
                $csvdata = edih_parse_select($fp);
                $csvchr = edih_csv_write($csvdata);
                $fct++;
                if ($htm) {
                    $str_html .= edih_csv_process_html($csvdata, $er);
                }
            }
            //$str_html .= ($htm) ? "</tbody>".PHP_EOL."</table>".PHP_EOL : "";
            $str_html .= $htm ? "</dl>" . PHP_EOL : "";
        } else {
            $str_html .= "<p>No new {$tp} files</p>";
        }
    }
    $capt_html = "<p>Process new files ({$fct} files)</p>" . PHP_EOL;
    return $capt_html . $str_html;
}
Example #12
0
/**
 * Sort the uploaded files array and save them to the correct directory
 * 
 * If a matching filename file is already in the directory it is not overwritten.
 * The uploaded file will just be discarded.
 * 
 * @uses csv_parameters()
 * @see ibr_upload_files() 
 * @param array $files_array  files array created by ibr_upload_files()
 * @param bool       -- whether to return html output
 * @param bool       -- whether to only report errors (ignored)
 * @return string    html formatted messages
 */
function ibr_sort_upload($files_array, $html_out = TRUE, $err_only = TRUE)
{
    //
    $prc_htm = '';
    //
    if (count($files_array) > 0) {
        // we have some files
        $p_ar = csv_parameters($type = "ALL");
        $ptypes = array_keys($p_ar);
        //
        if (array_key_exists('reject', $files_array)) {
            foreach ($files_array['reject'] as $rjc) {
                $prc_htm .= "Rejected file: {$rjc} <br />" . PHP_EOL;
            }
            $prc_htm .= "<p>&nbsp;</p>";
            unset($files_array['reject']);
        }
        foreach ($files_array as $key => $val) {
            // use keys from parameters array $ftypes
            //if ( strpos("|batch|ibr|ebr|dpr|f997|f277|era|ack|ta1|text", $key) ) {
            if (in_array($key, $ptypes)) {
                $t_dir = $p_ar[$key]['directory'];
                $t_base = basename($t_dir);
            } else {
                $prc_htm .= "<p>Type {$key} is not stored </p>" . PHP_EOL;
                continue;
            }
            $idx = 0;
            foreach ($files_array[$key] as $idx => $nf) {
                // check if the file has already been stored
                // a matching file name will not be replaced
                $nfb = basename($nf);
                //
                if ($key == 'reject') {
                    $prc_htm .= "Rejected: {$nfb}<br />" . PHP_EOL;
                    continue;
                }
                $testname = $t_dir . DIRECTORY_SEPARATOR . $nfb;
                if (is_file($testname)) {
                    $prc_htm .= "File already in {$t_base}: {$nfb} <br />" . PHP_EOL;
                } elseif (rename($nf, $testname)) {
                    $iscm = chmod($testname, 0400);
                    if (!$iscm) {
                        // if we could write, we should be able to set permissions
                        $prc_htm .= "Error: failed to set permissions for {$nfb}, attempting to remove file<br />" . PHP_EOL;
                        unlink($testname);
                    }
                    $prc_htm .= "Saved in {$t_base}: {$nfb} <br />" . PHP_EOL;
                } else {
                    $prc_htm .= "error saving {$nf} to {$t_dir} directory <br />" . PHP_EOL;
                }
            }
            if (count($files_array[$key]) == 0) {
                $prc_htm .= "Upload: type {$key} submitted with no files <br />" . PHP_EOL;
                continue;
            }
        }
    } else {
        // should not happen since this function should not be called unless there are new files
        $prc_htm .= "No edi files submitted<br />" . PHP_EOL;
    }
    //
    $prc_htm .= "<p>Upload more or click the \"<em>Process New</em>\" button to process new files.</p>" . PHP_EOL;
    //$prc_htm .= "</body></html>";
    return $prc_htm;
}