Ejemplo n.º 1
0
/**
 * Append rows to one of the csv record files.
 * 
 * @uses csv_singlerecord_test()
 * @uses csv_parameters()
 * @uses csv_files_header()
 * @param array $csv_data    the data array, either file data or claim data
 * @param string $file_type  which of our file types to use
 * @param string $csv_type   either 'claim' or 'file'
 * @return int               number of characters written per fputcsv()
 */
function csv_write_record($csv_data, $file_type, $csv_type)
{
    //
    if (!is_array($csv_data)) {
        return FALSE;
    }
    // use CSV_RECORD class to write ibr or ebr claims data to the csv file
    //  csv, batch, ibr, ebr, f997, or era
    if (!strpos("|era|f997|ibr|ebr|dpr|f277|batch|ta1|ack", $file_type)) {
        csv_edihist_log("csv_write_record error: incorrect file type {$file_type}");
        return FALSE;
    }
    $ft = $file_type;
    $ft = strpos("|835", $file_type) ? 'era' : $ft;
    $ft = strpos("|837", $file_type) ? 'batch' : $ft;
    $ft = strpos("|999|ack|ta1", $file_type) ? 'f997' : $ft;
    $params = csv_parameters($ft);
    //
    if ($csv_type == "claim") {
        $fp = $params['claims_csv'];
    } elseif ($csv_type == "file") {
        $fp = $params['files_csv'];
    } else {
        csv_edihist_log("csv_writedata_csv error: incorrect csv type {$csv_type}");
        return FALSE;
    }
    //
    $fh = fopen($fp, 'a');
    // count characters written -- returned by fputcsv
    $indc = 0;
    // if we fail to open the file, return the result, expect FALSE
    if (!$fh) {
        return FALSE;
    }
    // test for a new file
    if (filesize($fp) === 0) {
        $ar_h = csv_files_header($file_type, $csv_type);
        $td = fgetcsv($fh);
        if ($td === FALSE || $td === NULL) {
            // assume we have an empty file
            // write header row if this is a new csv file
            if (count($ar_h)) {
                $indc += fputcsv($fh, $ar_h);
            }
        }
    }
    // test array for dimension counts
    $is_sngl = csv_singlerecord_test($csv_data);
    if ($is_sngl) {
        $indc += fputcsv($fh, $csv_data);
    } else {
        // multi-dimensional array -- we rely on array_flatten to
        // assure us that the array depth is 1
        foreach ($csv_data as $row) {
            $wr = csv_array_flatten($row);
            // $wr is false if $row is not an array
            if ($wr) {
                $indc += fputcsv($fh, $wr);
            } else {
                continue;
            }
        }
    }
    fclose($fh);
    //
    return $indc;
}
Ejemplo n.º 2
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;
}