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;
}
        $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 #3
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;
}