Better importing CN error display
[contractdashboard.git] / admin / import.php
blob:a/admin/import.php -> blob:b/admin/import.php
<?php <?php
   
include_once ("../lib/common.inc.php"); include_once ("../lib/common.inc.php");
function processFile($fpath, $tablename)  
{ $contractNoticeFields = array(
global $conn; "importFile",
$row = 1; "agencyName",
$handle = fopen($fpath, "r"); "parentCN",
//"t" mode string translates windows line breaks to unix "CNID",
$datamapping0711 = array( "publishDate",
"Agency" => "agencyName", "amendDate",
"Parent CN ID" => "parentCN", "contractStart",
"CN ID" => "CNID", "contractEnd",
"Publish Date" => "publishDate", "value",
"Amendment Date" => "amendDate", "description",
"Status" => "", "agencyID",
"StartDate" => "contractStart", "category",
"EndDate" => "contractEnd", "procurementMethod",
"Value" => "value", "atmID",
"Description" => "description", "SONID",
"Agency Ref Id" => "agencyID", "confidentialityContract",
"Category" => "category", "confidentialityContractReason",
"Procurement Method" => "procurementMethod", "confidentialityOutputs",
"ATM ID" => "atmID", "confidentialityOutputsReason",
"SON ID" => "SONID", "consultancy",
"Confidentiality - Contract" => "confidentialityContract", "consultancyReason",
"Confidentiality - Contract Reason(s)" => "confidentialityContractReason", "amendmentReason",
"Confidentiality - Outputs" => "confidentialityOutputs", "supplierName",
"Confidentiality - Outputs Reason(s)" => "confidentialityOutputsReason", "supplierAddress",
"Consultancy" => "consultancy", "supplierCity",
"Consultancy Reason(s)" => "consultancyReason", "supplierPostcode",
"Amendment Reason" => "amendmentReason", "supplierCountry",
"Supplier Name" => "supplierName", "supplierABNExempt",
"Supplier Address" => "supplierAddress", "supplierABN",
"Supplier City" => "supplierCity", "contactBranch",
"Supplier Postcode" => "supplierPostcode", "contactDivision",
"Supplier Country" => "supplierCountry", "contactPostcode"
"Supplier ABNExempt" => "supplierABNExempt", );
"Supplier ABN" => "supplierABN", $agencyFields = array(
"Agency Branch" => "contactBranch", "agencyName"
"Agency Divison" => "contactDivision", );
"Agency Postcode" => "contactPostcode", $supplierFields = array(
"" => "" "supplierName",
); "supplierAddress",
$headers; "supplierCity",
$contractNoticeFields = array( "supplierPostcode",
"importFile", "supplierCountry",
"agencyName", "supplierABNExempt",
"parentCN", "supplierABN"
"CNID", );
"publishDate", $contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( ';
"amendDate", foreach ($contractNoticeFields as $key => $f) {
"contractStart", $contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?";
"contractEnd", }
"value", $contractNoticeInsertQ.= ");";
"description", $contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ);
"agencyID",  
"category", $supplierInsertQ = 'INSERT INTO supplierdetails ("' . implode('" , "', $supplierFields) . '") VALUES ( ';
"procurementMethod", foreach ($supplierFields as $key => $f) {
"atmID", $supplierInsertQ.= ($key == 0 ? "" : ", ") . "?";
"SONID", }
"confidentialityContract", $supplierInsertQ.= ");";
"confidentialityContractReason", $supplierInsertQ = $conn->prepare($supplierInsertQ);
"confidentialityOutputs",  
"confidentialityOutputsReason", $agencyInsertQ = 'INSERT INTO agency_nametoabn ("' . implode('" , "', $agencyFields) . '") VALUES ( ';
"consultancy", foreach ($agencyFields as $key => $f) {
"consultancyReason", $agencyInsertQ.= ($key == 0 ? "" : ", ") . "?";
"amendmentReason", }
"supplierName", $agencyInsertQ.= ");";
"supplierAddress", $agencyInsertQ = $conn->prepare($agencyInsertQ);
"supplierCity",  
"supplierPostcode", function processFile($fpath, $tablename) {
"supplierCountry", global $conn, $contractNoticeQ, $supplierInsertQ, $agencyInsertQ;
"supplierABNExempt", $row = 1;
"supplierABN", $handle = fopen($fpath, "r");
"contactBranch", //"t" mode string translates windows line breaks to unix
"contactDivision", $datamapping0711 = array(
"contactPostcode" "Agency" => "agencyName",
); "Parent CN ID" => "parentCN",
$agencyFields = array( "CN ID" => "CNID",
"agencyName" "Publish Date" => "publishDate",
); "Amendment Date" => "amendDate",
$supplierFields = array( "Status" => "",
"supplierName", "StartDate" => "contractStart",
"supplierAddress", "EndDate" => "contractEnd",
"supplierCity", "Value" => "value",
"supplierPostcode", "Description" => "description",
"supplierCountry", "Agency Ref Id" => "agencyID",
"supplierABNExempt", "Category" => "category",
"supplierABN" "Procurement Method" => "procurementMethod",
); "ATM ID" => "atmID",
if ($tablename == "contractnotice") { "SON ID" => "SONID",
$contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( '; "Confidentiality - Contract" => "confidentialityContract",
foreach ($contractNoticeFields as $key => $f) { "Confidentiality - Contract Reason(s)" => "confidentialityContractReason",
$contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?"; "Confidentiality - Outputs" => "confidentialityOutputs",
} "Confidentiality - Outputs Reason(s)" => "confidentialityOutputsReason",
$contractNoticeInsertQ.= ");"; "Consultancy" => "consultancy",
$contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ); "Consultancy Reason(s)" => "consultancyReason",
} "Amendment Reason" => "amendmentReason",
else if ($tablename == "supplierdetails") { "Supplier Name" => "supplierName",
$supplierInsertQ = 'INSERT INTO supplierdetails ("' . implode('" , "', $supplierFields) . '") VALUES ( '; "Supplier Address" => "supplierAddress",
foreach ($supplierFields as $key => $f) { "Supplier City" => "supplierCity",
$supplierInsertQ.= ($key == 0 ? "" : ", ") . "?"; "Supplier Postcode" => "supplierPostcode",
} "Supplier Country" => "supplierCountry",
$supplierInsertQ.= ");"; "Supplier ABNExempt" => "supplierABNExempt",
$supplierInsertQ = $conn->prepare($supplierInsertQ); "Supplier ABN" => "supplierABN",
} "Agency Branch" => "contactBranch",
else if ($tablename == "agency") { "Agency Divison" => "contactDivision",
$agencyInsertQ = 'INSERT INTO agency ("' . implode('" , "', $agencyFields) . '") VALUES ( '; "Agency Postcode" => "contactPostcode",
foreach ($agencyFields as $key => $f) { "" => ""
$agencyInsertQ.= ($key == 0 ? "" : ", ") . "?"; );
} $headers;
$agencyInsertQ.= ");";  
$agencyInsertQ = $conn->prepare($agencyInsertQ); while (($data = fgetcsv($handle, 1000, "\t")) !== false) {
} $num = count($data);
while (($data = fgetcsv($handle, 1000, "\t")) !== false) { if ($row == 3) {
$num = count($data); $headers = $data;
if ($row == 3) { } elseif ($row > 3) {
$headers = $data; if ($num > count($datamapping0711)) {
} die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data));
elseif ($row > 3) { }
if ($num > count($datamapping0711)) { $contractNoticeInsert = Array();
die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data)); $supplierInsert = Array();
} $agencyInsert = Array();
$contractNoticeInsert = Array(); $contractNoticeInsert[] = $fpath;
$supplierInsert = Array(); $keys = array_keys($datamapping0711);
$agencyInsert = Array(); for ($c = 0; $c < $num; $c++) {
$contractNoticeInsert[] = $fpath; $data[$c] = trim($data[$c], "=");
$keys = array_keys($datamapping0711); $data[$c] = trim($data[$c], "\"");
for ($c = 0; $c < $num; $c++) { if ($tablename == "contractnotice") {
$data[$c] = trim($data[$c], "="); if (in_array(($datamapping0711[$headers[$c]]), $contractNoticeFields)) {
$data[$c] = trim($data[$c], "\""); if (($datamapping0711[$headers[$c]]) == "parentCN" || ($datamapping0711[$headers[$c]]) == "CNID") {
if ($tablename == "contractnotice") { $data[$c] = substr($data[$c], 2); // take off the "CN" prefix
if (in_array(($datamapping0711[$headers[$c]]) , $contractNoticeFields)) { if ($data[$c] > 0) {
if (($datamapping0711[$headers[$c]]) == "parentCN" || ($datamapping0711[$headers[$c]]) == "CNID") { $contractNoticeInsert[] = $data[$c];
$data[$c] = substr($data[$c], 2); // take off the "CN" prefix } else {
$data[$c] = str_replace("-A", "00", $data[$c]); // make amendments really big numbers $contractNoticeInsert[] = 0;
if (!is_numeric($data[$c]) && $data[$c] != "") die($data[$c] . " is not numeric"); }
if ($data[$c] > 0) { } elseif (($datamapping0711[$headers[$c]]) == "supplierABN") {
$contractNoticeInsert[] = $data[$c]; if ($data[$c] > 0) {
} $contractNoticeInsert[] = $data[$c];
else { } else {
$contractNoticeInsert[] = 0; $contractNoticeInsert[] = null;
} }
} } elseif (($datamapping0711[$headers[$c]]) == "amendDate" || ($datamapping0711[$headers[$c]]) == "publishDate" || ($datamapping0711[$headers[$c]]) == "contractStart" || ($datamapping0711[$headers[$c]]) == "contractEnd") {
elseif (($datamapping0711[$headers[$c]]) == "supplierABN") { $contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c]));
if ($data[$c] > 0) { } else {
$contractNoticeInsert[] = $data[$c]; if (strstr("\" =", $data[$c] > 0)) {
} die("Invalid Description field" . $contractNoticeInsert);
else { }
$contractNoticeInsert[] = null; $colvalue = preg_replace('/[^[:print:]]/', '', utf8_encode($data[$c]));
}  
} $contractNoticeInsert[] = $colvalue;
elseif (($datamapping0711[$headers[$c]]) == "amendDate" || ($datamapping0711[$headers[$c]]) == "publishDate" || ($datamapping0711[$headers[$c]]) == "contractStart" || ($datamapping0711[$headers[$c]]) == "contractEnd") { }
$contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c])); }
} } else if ($tablename == "supplierdetails") {
else { if (in_array(($datamapping0711[$headers[$c]]), $supplierFields)) {
if (strstr("\" =", $data[$c] > 0)) { if (($datamapping0711[$headers[$c]]) == "supplierABN") {
die("Invalid Description field" . $contractNoticeInsert); if ($data[$c] > 0) {
} $contractNoticeInsert[] = $data[$c];
$colvalue = preg_replace( '/[^[:print:]]/', '',utf8_encode( $data[$c])); } else {
  $contractNoticeInsert[] = 0;
$contractNoticeInsert[] = $colvalue; }
} } else {
} $supplierInsert[] = $data[$c];
} }
else if ($tablename == "supplierdetails") { }
if (in_array(($datamapping0711[$headers[$c]]) , $supplierFields)) { } else if ($tablename == "agency") {
if (($datamapping0711[$headers[$c]]) == "supplierABN") { if (in_array(($datamapping0711[$headers[$c]]), $agencyFields)) {
if ($data[$c] > 0) { $agencyInsert[] = $data[$c];
$contractNoticeInsert[] = $data[$c]; }
} }
else { }
$contractNoticeInsert[] = 0; flush();
} if ($tablename == "contractnotice") {
} $contractNoticeInsertQ->execute($contractNoticeInsert);
else { $errors = $conn->errorInfo();
$supplierInsert[] = $data[$c]; if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
}  
} } elseif ($errors[1] == 0) {
} $success++;
else if ($tablename == "agency") { } else {
if (in_array(($datamapping0711[$headers[$c]]) , $agencyFields)) { foreach ($contractNoticeFields as $key => $cnf) {
$agencyInsert[] = $data[$c]; echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>";
} }
} echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n";
} }
flush(); } else if ($tablename == "supplierdetails") {
if ($tablename == "contractnotice") { $supplierInsertQ->execute($supplierInsert);
$contractNoticeInsertQ->execute($contractNoticeInsert); $errors = $conn->errorInfo();
$errors = $conn->errorInfo(); if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {  
} } elseif ($errors[1] == 0) {
elseif ($errors[1] == 0) { $success++;
$success++; } else {
} echo $data[2] . " failed supplier insert.<br>" . print_r($errors, true) . " <br> " . print_r($supplierInsert, true) . "<br> $row <br><br>\n";
else { }
foreach ($contractNoticeFields as $key => $cnf) { } else if ($tablename == "agency") {
echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>"; $agencyInsertQ->execute($agencyInsert);
} $errors = $conn->errorInfo();
echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n"; if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
}  
} } elseif ($errors[1] == 0) {
else if ($tablename == "supplierdetails") { $success++;
$supplierInsertQ->execute($supplierInsert); } else {
$errors = $conn->errorInfo(); echo $data[2] . " failed agency insert.<br>" . print_r($errors, true) . " <br> " . print_r($agencyInsert, true) . "<br> $row <br><br>\n";
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { }
} }
elseif ($errors[1] == 0) { flush();
$success++; //echo "<hr>\n";
} }
else { $row++;
echo $data[2] . " failed supplier insert.<br>" . print_r($errors, true) . " <br> " . print_r($supplierInsert, true) . "<br> $row <br><br>\n"; }
} fclose($handle);
} $contractNoticeInsertQ->closeCursor();
else if ($tablename == "agency") { $supplierInsertQ->closeCursor();
$agencyInsertQ->execute($agencyInsert); $agencyInsertQ->closeCursor();
$errors = $conn->errorInfo();  
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) { return $success;
} }
elseif ($errors[1] == 0) {  
$success++;  
}  
else {  
echo $data[2] . " failed agency insert.<br>" . print_r($errors, true) . " <br> " . print_r($agencyInsert, true) . "<br> $row <br><br>\n";  
}  
}  
flush();  
//echo "<hr>\n";  
   
}  
$row++;  
}  
fclose($handle);  
return $success;  
}  
$path = 'data/'; $path = 'data/';
if ($_REQUEST["fname"] == "") { if ($_REQUEST["fname"] == "") {
echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>"; echo "Get files from: https://www.tenders.gov.au/?event=public.reports.list<br>";
$dhandle = opendir($path); $dhandle = opendir($path);
// define an array to hold the files // define an array to hold the files
$files = array(); $files = array();
if ($dhandle) { if ($dhandle) {
// loop through all of the files // loop through all of the files
while (false !== ($fname = readdir($dhandle))) { while (false !== ($fname = readdir($dhandle))) {
if (($fname != '.') && ($fname != '..')) { if (($fname != '.') && ($fname != '..')) {
echo "<a href=\"import.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . date("c", filemtime($path . $fname)) . "<br/>"; echo "<a href=\"import.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . date("c", filemtime($path . $fname)) . "<br/>";
} }
} }
} }
} } else {
else { $success = 0;
$success = 0; $fname = $_REQUEST["fname"];
$fname = $_REQUEST["fname"]; echo " ============== $fname ============== <br>";
echo " ============== $fname ============== <br>"; flush();
flush(); $success+= processFile($path . $fname, "contractnotice");
$success+= processFile($path . $fname, "contractnotice"); $success+= processFile($path . $fname, "agency");
$success+= processFile($path . $fname, "agency"); $success+= processFile($path . $fname, "supplier");
$success+= processFile($path . $fname, "agency"); echo "<br> $success records successfully created";
echo "<br> $success records successfully created";  
flush(); flush();
} // run post import data processing
// run post import data processing  
// cn // cn
//include ("linkAmendments.php"); echo "link amend<br>";
include ("updateUNSPSC.php"); include ("linkAmendments.php");
  echo "update UNSPSC<br>";
  include ("updateUNSPSC.php");
// agency // agency
include ("setAgencyStatus.php"); //include ("setAgencyStatus.php");
include ("setAgencyURLABN.php"); //include ("setAgencyURLABN.php");
  }
?> ?>