Пример #1
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;
}
Пример #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;
}