Пример #1
0
/**
 * get the x12 file containing the control_num ISA13
 *
 * @todo the csv for x12 files 999, 277, 835, 837 must have the control number
 *
 * @uses csv_search_record()
 * @param string $control_num   the interchange control number, isa13
 * @return  string              the file name
 */
function csv_file_by_controlnum($type, $control_num)
{
    // get the batch file containing the control_num
    //
    $tp = csv_file_type($type);
    //
    $hdr = csv_table_header($tp, 'file');
    $scol = array_search('Control', $hdr);
    $rcol = array_search('FileName', $hdr);
    //
    // $search_ar should have keys ['s_val']['s_col'] array(['r_cols'][])
    //    like "batch', 'claim, array(9, '0024', array(1, 2, 7))
    //$csv_hd_ar['batch']['file'] = array('time', 'file_name', 'control_num', 'claims', 'x12_partner', 'x12_version');
    //
    $fn = '';
    $ctln = strlen($control_num) >= 9 ? substr($control_num, 0, 9) : $control_num;
    $search = array('s_val' => $ctln, 's_col' => $scol, 'r_cols' => array($rcol));
    $result = csv_search_record($tp, 'file', $search, "1");
    if (is_array($result) && count($result[0]) == 1) {
        $fn = $result[0][0];
    }
    return $fn;
}
Пример #2
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;
}