|
|
@@ -43,31 +43,91 @@ $id_company = 1;
|
|
|
processFile('../test.xls', $id_company);
|
|
|
|
|
|
|
|
|
-function processFile($filename_report, $id_company)
|
|
|
+function processFile($filename, $id_company)
|
|
|
{
|
|
|
global $dbh;
|
|
|
|
|
|
+ if(!file_exists($filename))
|
|
|
+ {
|
|
|
+ echo "file doesn't exist\n";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $file_size = filesize($filename);
|
|
|
+ $file_hash = md5_file($filename);
|
|
|
+
|
|
|
+ $params = array(
|
|
|
+ ':id_company' => $id_company,
|
|
|
+ ':filename' => $filename,
|
|
|
+ ':file_size' => $file_size,
|
|
|
+ ':file_hash' => $file_hash
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ //first try to find if the file exists
|
|
|
+ $stmt_file = $dbh->prepare("SELECT id_file
|
|
|
+ FROM file
|
|
|
+ WHERE id_company = :id_company
|
|
|
+ AND filename = :filename
|
|
|
+ AND md5_hash = :file_hash
|
|
|
+ AND size = :file_size");
|
|
|
+
|
|
|
+ $stmt_file->execute($params);
|
|
|
+
|
|
|
+ $rows = $stmt_file->fetchAll(PDO::FETCH_ASSOC);
|
|
|
+
|
|
|
+
|
|
|
+ if(count($rows))
|
|
|
+ {
|
|
|
+ $id_file = $rows[0]['id_file'];
|
|
|
+ echo "re-running file #$id_file\n";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //report file processed
|
|
|
+ $stmt_file = $dbh->prepare("INSERT INTO file (id_company, filename, md5_hash, size)
|
|
|
+ VALUES (:id_company, :filename, :file_hash, :file_size)");
|
|
|
+
|
|
|
+ if(!$stmt_file->execute($params))
|
|
|
+ {
|
|
|
+ echo "\nPDO::errorInfo():\n";
|
|
|
+ print_r($dbh->errorInfo());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $id_file = $dbh->lastInsertId();
|
|
|
+
|
|
|
+ echo "processing new file #$id_file\n";
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
//TODO: check if we already processed this file
|
|
|
|
|
|
|
|
|
- $stmt = $dbh->prepare("INSERT INTO records (
|
|
|
- id_company,
|
|
|
+ $stmt_row = $dbh->prepare("INSERT INTO file_row (
|
|
|
+ id_file, row_number,
|
|
|
division, consignee, date_called_in, dispatch_number, shipper, date_ready,
|
|
|
address_1, address_2, city, state, zip, phone,
|
|
|
time_ready, time_close, time_pickup, time_depart, bol_delivered,
|
|
|
dispatch_reference,
|
|
|
ctns, weight, cube
|
|
|
) VALUES (
|
|
|
- :id_company,
|
|
|
+ :id_file, :row_number,
|
|
|
:division, :consignee, STR_TO_DATE(:date_called_in, '%m/%d/%Y'), :dispatch_number, :shipper, STR_TO_DATE(:date_ready, '%m/%d/%Y'),
|
|
|
:address_1, :address_2, :city, :state, :zip, :phone,
|
|
|
:time_ready, :time_close, :time_pickup, :time_depart, :bol_delivered,
|
|
|
:dispatch_reference,
|
|
|
:ctns, :weight, :cube
|
|
|
- )");
|
|
|
+ )
|
|
|
+ ON DUPLICATE KEY UPDATE
|
|
|
+ division = :division, consignee = :consignee, date_called_in = STR_TO_DATE(:date_called_in, '%m/%d/%Y'), dispatch_number = :dispatch_number, shipper = :shipper, date_ready = STR_TO_DATE(:date_ready, '%m/%d/%Y'),
|
|
|
+ address_1 = :address_1, address_2 = :address_2, city = :city, state = :state, zip = :zip, phone = :phone,
|
|
|
+ time_ready = :time_ready, time_close = :time_close, time_pickup = :time_pickup, time_depart = :time_depart, bol_delivered = :bol_delivered,
|
|
|
+ dispatch_reference = :dispatch_reference,
|
|
|
+ ctns = :ctns, weight = :weight, cube = :cube");
|
|
|
|
|
|
- if(!$stmt)
|
|
|
+ if(!$stmt_row)
|
|
|
{
|
|
|
echo "\nPDO::errorInfo():\n";
|
|
|
print_r($dbh->errorInfo());
|
|
|
@@ -75,6 +135,8 @@ function processFile($filename_report, $id_company)
|
|
|
}
|
|
|
|
|
|
|
|
|
+ $num_rows = 0;
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
//this is necessary to dramatically reduce the memory footprint
|
|
|
@@ -84,7 +146,7 @@ function processFile($filename_report, $id_company)
|
|
|
|
|
|
|
|
|
// figure out which type it is
|
|
|
- $type = PHPExcel_IOFactory::identify($filename_report);
|
|
|
+ $type = PHPExcel_IOFactory::identify($filename);
|
|
|
|
|
|
// create an excel reader object
|
|
|
$reader = PHPExcel_IOFactory::createReader($type);
|
|
|
@@ -94,7 +156,7 @@ function processFile($filename_report, $id_company)
|
|
|
echo "Report detected as: $type\n";
|
|
|
|
|
|
// load the file
|
|
|
- $excel = $reader->load($filename_report);
|
|
|
+ $excel = $reader->load($filename);
|
|
|
|
|
|
// pull the 'call details' worksheet, which should be the only one
|
|
|
$sheet = $excel->getSheet(0);
|
|
|
@@ -105,8 +167,9 @@ function processFile($filename_report, $id_company)
|
|
|
|
|
|
|
|
|
// let's keep track of how many succeeded and failed
|
|
|
- $num_success = 0;
|
|
|
+ $num_ok = 0;
|
|
|
$num_fail = 0;
|
|
|
+ $num_skip = 0;
|
|
|
|
|
|
|
|
|
// pull column names
|
|
|
@@ -146,13 +209,15 @@ function processFile($filename_report, $id_company)
|
|
|
if(!$row[0] && !$row[1] && !$row[2])
|
|
|
{
|
|
|
echo "SKIP\n";
|
|
|
+ $num_skip++;
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//bind appropriate values
|
|
|
$params = array();
|
|
|
|
|
|
- $params[':id_company'] = $id_company;
|
|
|
+ $params[':id_file'] = $id_file;
|
|
|
+ $params[':row_number'] = $r;
|
|
|
|
|
|
$params[':division'] = $row[0];
|
|
|
$params[':consignee'] = $row[1];
|
|
|
@@ -183,21 +248,43 @@ function processFile($filename_report, $id_company)
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- $stmt->execute($params);
|
|
|
+ $stmt_row->execute($params);
|
|
|
+
|
|
|
+ echo "OK\n";
|
|
|
+ $num_ok++;
|
|
|
}
|
|
|
catch(Exception $e)
|
|
|
{
|
|
|
echo 'FAIL: ' . $e->getMessage() . "\n";
|
|
|
- continue;
|
|
|
+ $num_fail++;
|
|
|
}
|
|
|
-
|
|
|
- echo "OK\n";
|
|
|
}
|
|
|
}
|
|
|
catch(Exception $e)
|
|
|
{
|
|
|
echo "Uncaught fatal error while attempting to process the downloaded report: " . $e->getMessage() . "\n";
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ //update the final status
|
|
|
+
|
|
|
+ $stmt_file_status = $dbh->prepare("UPDATE file SET
|
|
|
+ num_rows = :num_rows,
|
|
|
+ num_ok = :num_ok,
|
|
|
+ num_fail = :num_fail,
|
|
|
+ num_skip = :num_skip
|
|
|
+ WHERE id_file = :id_file");
|
|
|
+
|
|
|
+ $params = array(
|
|
|
+ ':id_file' => $id_file,
|
|
|
+ ':num_rows' => $num_rows-1, //subtract one because of the column header
|
|
|
+ ':num_ok' => $num_ok,
|
|
|
+ ':num_fail' => $num_fail,
|
|
|
+ ':num_skip' => $num_skip
|
|
|
+ );
|
|
|
+
|
|
|
+ $stmt_file_status->execute($params);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
?>
|