Example #1
0
function parse_csv_sheet($xls = null, $csv_id = 0)
{
    //oks
    $csv_err = sprintf("%s.err", $xls);
    //new load
    $objPHPExcel = PHPExcel_IOFactory::load($xls);
    $sheets = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
    debug("INFO", "CSV-save( {$csvX} ) ->CSV-ID> {$csv_id}");
    //format the CSV here
    $csv_stats = array();
    foreach ($sheets as $csvK => $csvV) {
        $csvX++;
        debug("INFO", "CSV-save( {$csvX} ) -> raw line> " . @var_export($csvV, true));
        if (@is_array($csvV)) {
            $csvcols = array();
            //xls fmt
            if (0) {
                $alltotA = @count($csvV);
                if ($alltotA >= MAX_CSV_COLS) {
                    $csvcols = $csvV;
                    debug("INFO", "CSV-save( {$csvX} ) -> A-tot={$alltotB}; !");
                }
            }
            //pipe delimited
            $csvcols = @explode("|", $csvV["A"]);
            //ignore header
            if (@preg_match("/operator_code/i", $csvcols[1])) {
                debug("INFO", "CSV-save( {$csvX} ) -> header is ignored!" . $csvcols[0]);
                continue;
            }
            //all total
            $csv_stats['total']++;
            $alltotC = @count($csvcols);
            debug("INFO", "CSV-save( {$csvX} ) -> C-tot={$alltotC}; !");
            if ($alltotC < MAX_CSV_COLS) {
                $csv_stats['error']++;
                //err log
                $err_row = @join("|", $csvcols);
                utils_io_file_save($csv_err, "{$err_row}\n", "a+");
                debug("INFO", "CSV-save( {$csvX} ) -> record total is below as expected! {$alltotC};{$err_row}");
                continue;
            }
            //clean ?
            foreach ($csvcols as $k1 => $v1) {
                $v2 = $csvcols[$k1];
                $v2 = @preg_replace('/^"/', '', $v2);
                $v2 = @preg_replace('/"$/', '', $v2);
                $csvcols[$k1] = $v2;
            }
            //loop it here
            $xdata = null;
            $xdata["trafficrecap_id"] = $csvcols[0];
            $xdata["operator_code"] = $csvcols[1];
            $xdata["traffic_code"] = $csvcols[2];
            $xdata["job_id"] = $csvcols[3];
            $xdata["station_code"] = $csvcols[4];
            $xdata["dateshift"] = $csvcols[5];
            $xdata["shift_code"] = $csvcols[6];
            $xdata["user_code"] = $csvcols[7];
            $xdata["state"] = $csvcols[8];
            $xdata["cardtype"] = $csvcols[9];
            $xdata["manless"] = $csvcols[10];
            $xdata["member"] = $csvcols[11];
            $xdata["seccodeval"] = $csvcols[12];
            $xdata["status_id"] = $csvcols[13];
            $xdata["totalvalue"] = $csvcols[14];
            $xdata["totalvaluefine"] = $csvcols[15];
            $xdata["totalvalueest"] = $csvcols[16];
            $xdata["payment_code"] = $csvcols[17];
            $xdata["totalqty"] = $csvcols[18];
            $xdata["action_id"] = $csvcols[19];
            $xdata["useradd"] = $csvcols[20];
            $xdata["usermod"] = $csvcols[21];
            $xdata["userdel"] = $csvcols[22];
            $xdata["dateadd"] = $csvcols[23];
            $xdata["datemod"] = $csvcols[24];
            $xdata["datedel"] = $csvcols[25];
            $xdata["flag_id"] = $csvcols[26];
            $xdata["seccodetype"] = $csvcols[27];
            $xdata["traffic_name"] = $csvcols[28];
            $xdata["typetraffic_code"] = $csvcols[29];
            $xdata["member_code"] = $csvcols[30];
            $xdata["product_code"] = $csvcols[31];
            $xdata["csv_id"] = $csv_id;
            //save rec
            $pret = saveTraffic($xdata);
            if (!$pret) {
                //err log
                $err_row = @join("|", $csvcols);
                utils_io_file_save($csv_err, "{$err_row}\n", "a+");
                debug("INFO", "CSV-save() -> ignored >{$err_row} ");
                $csv_stats['error']++;
                continue;
            }
            $csv_stats['success']++;
        }
        //if arr
    }
    //for sheets
    //hit the total parsed
    $ydata["id"] = $csv_id;
    $ydata["tot"] = intval($csv_stats['total']);
    $ydata["oks"] = intval($csv_stats['success']);
    $ydata["err"] = intval($csv_stats['error']);
    $pret = update_stats($ydata);
    $dmp = @var_export($ydata, true);
    debug("INFO", "CSV-save() STATS-> {$dmp} !");
    /*
    	$CSV_STATUS_ARR  = array(
    	'0'    => 'Pending',
    	'1'    => 'Processing',
    	'2'    => 'Processed',
    	'3'    => 'Zero Rows',
    	'9'    => 'Failed',
    	);
    */
    //update status to 2
    $csv_status = 2;
    $csv_mesg = "Successfully processed!";
    //no-rows
    if ($ydata["oks"] <= 0 and $ydata["tot"] > 0) {
        $csv_status = 3;
        $csv_mesg = "No valid rows found!";
    }
    //fail
    if ($ydata["oks"] <= 0 and $ydata["tot"] <= 0) {
        $csv_status = 9;
        $csv_mesg = "Failed to parse!";
    }
    //update it now
    $upd = set_csv_summary(array('id' => $csv_id, 'status' => $csv_status, 'desc' => $csv_mesg));
}
Example #2
0
//lock
$gPORT_LOCKER = new PortLocker(17897);
if (!$gPORT_LOCKER->lock()) {
    debug("cron_parse_csv() : an instance still running!");
    exit;
}
debug("cron_parse_csv() : an instance will run now!");
//get settings
$pdata = get_csv_summary();
// found
if ($pdata['exists']) {
    $rdata = $pdata['data'];
    $rdatb = 0;
    foreach ($rdata as $kk => $vv) {
        $rdatb++;
        $csv = sprintf("%s/%s", CSV_DIR, $vv['csv_file']);
        //update 1
        $upd = set_csv_summary(array('id' => $vv['id'], 'status' => 1, 'desc' => 'ongoing parsing'));
        //run
        parse_csv_sheet($csv, $vv['id']);
        debug("cron_parse_csv({$rdatb} # {$upd}) file> {$csv}");
    }
    status_msg(SUCCESS, "Total {$rdatb} pending summary found in db.");
} else {
    status_msg(FAILED, "No pending summary found in db.");
}
//free
$gPORT_LOCKER->unlock();
//free
free_up();
debug("cron_parse_csv() : done!!!!");