Link amendments made in new numbering scheme
[contractdashboard.git] / admin / import.php
blob:a/admin/import.php -> blob:b/admin/import.php
<?php <?php
  if (php_sapi_name() != "cli") {
   
include_once ("../lib/common.inc.php"); include_once ("../lib/common.inc.php");
function processFile($fpath, $tablename) //auth();
{ $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",  
"Agency Branch" => "contactBranch", $contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( ';
"Agency Divison" => "contactDivision", foreach ($contractNoticeFields as $key => $f) {
"Agency Postcode" => "contactPostcode", $contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?";
"" => "" }
); $contractNoticeInsertQ.= ");";
$headers; $contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ);
$contractNoticeFields = array( function processFile($fpath) {
"importFile", global $conn, $contractNoticeFields, $contractNoticeInsertQ;
"agencyName", $success = 0;
"parentCN", $row = 1;
"CNID", $handle = fopen($fpath, "r");
"publishDate", //"t" mode string translates windows line breaks to unix
"amendDate", //On the 15 April 2014 AusTender Contract Notice functionality changed to improve transparency. Two new fields have been introduced to capture the ‘Amendment Value’ and the ‘Amendment Start Date’. Where applicable, these fields will display on amendment records published after this date.
"contractStart", $datamapping0712 = array(
"contractEnd", "Agency" => "agencyName",
"value", "Parent CN ID" => "parentCN",
"description", "CN ID" => "CNID",
"agencyID", "Publish Date" => "publishDate",
"category", "Amendment Date" => "amendDate",
"procurementMethod", "Amendment Publish Date" => "amendDate",
"atmID", "Status" => "",
"SONID", "StartDate" => "contractStart",
"confidentialityContract", "EndDate" => "contractEnd",
"confidentialityContractReason", "Value" => "value",
"confidentialityOutputs", "Description" => "description",
"confidentialityOutputsReason", "Agency Ref Id" => "agencyID",
"consultancy", "Agency Ref. ID" => "agencyID",
"consultancyReason", "Category" => "category",
"amendmentReason", "Procurement Method" => "procurementMethod",
"supplierName", "ATM ID" => "atmID",
"supplierAddress", "SON ID" => "SONID",
"supplierCity", "Confidentiality - Contract" => "confidentialityContract",
"supplierPostcode", "Confidentiality - Contract Reason(s)" => "confidentialityContractReason",
"supplierCountry", "Confidentiality - Outputs" => "confidentialityOutputs",
"supplierABNExempt", "Confidentiality - Outputs Reason(s)" => "confidentialityOutputsReason",
"supplierABN", "Consultancy" => "consultancy",
"contactBranch", "Consultancy Reason(s)" => "consultancyReason",
"contactDivision", "Amendment Reason" => "amendmentReason",
"contactPostcode" "Supplier Name" => "supplierName",
); "Supplier Address" => "supplierAddress",
$agencyFields = array( "Supplier City" => "supplierCity",
"agencyName" "Supplier Postcode" => "supplierPostcode",
); "Supplier Country" => "supplierCountry",
$supplierFields = array( "Supplier ABNExempt" => "supplierABNExempt",
"supplierName", "Supplier ABN" => "supplierABN",
"supplierAddress", "Agency Branch" => "contactBranch",
"supplierCity", "Agency Divison" => "contactDivision",
"supplierPostcode", "Agency Postcode" => "contactPostcode",
"supplierCountry", "" => ""
"supplierABNExempt", );
"supplierABN"  
);  
if ($tablename == "contractnotice") { while (($data = fgetcsv($handle, 1200, "\t")) !== false) {
$contractNoticeInsertQ = 'INSERT INTO contractnotice ("' . implode('" , "', $contractNoticeFields) . '") VALUES ( '; $num = count($data);
foreach ($contractNoticeFields as $key => $f) { if ($row == 3) {
$contractNoticeInsertQ.= ($key == 0 ? "" : ", ") . "?"; $headers = $data;
} } elseif ($row > 3) {
$contractNoticeInsertQ.= ");"; if ($num > count($datamapping0712)) {
$contractNoticeInsertQ = $conn->prepare($contractNoticeInsertQ); die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data));
} }
else if ($tablename == "supplierdetails") { $contractNoticeInsert = Array();
$supplierInsertQ = 'INSERT INTO supplierdetails ("' . implode('" , "', $supplierFields) . '") VALUES ( '; $supplierInsert = Array();
foreach ($supplierFields as $key => $f) { $agencyInsert = Array();
$supplierInsertQ.= ($key == 0 ? "" : ", ") . "?"; $contractNoticeInsert[] = $fpath;
} $keys = array_keys($datamapping0712);
$supplierInsertQ.= ");"; for ($c = 0; $c < $num; $c++) {
$supplierInsertQ = $conn->prepare($supplierInsertQ); $data[$c] = trim($data[$c], "=");
} $data[$c] = trim($data[$c], "\"");
else if ($tablename == "agency") { if (in_array(($datamapping0712[$headers[$c]]), $contractNoticeFields)) {
$agencyInsertQ = 'INSERT INTO agency ("' . implode('" , "', $agencyFields) . '") VALUES ( '; if (($datamapping0712[$headers[$c]]) == "parentCN" || ($datamapping0712[$headers[$c]]) == "CNID") {
foreach ($agencyFields as $key => $f) { $data[$c] = substr($data[$c], 2); // take off the "CN" prefix
$agencyInsertQ.= ($key == 0 ? "" : ", ") . "?"; if ($data[$c] > 0 && $data[$c] != '0' && $data[$c] != '') {
} $contractNoticeInsert[] = $data[$c];
$agencyInsertQ.= ");"; } else {
$agencyInsertQ = $conn->prepare($agencyInsertQ); $contractNoticeInsert[] = null;
} }
while (($data = fgetcsv($handle, 1000, "\t")) !== false) { } elseif (($datamapping0712[$headers[$c]]) == "supplierABN" || ($datamapping0712[$headers[$c]]) == "SONID") {
$num = count($data); if ($data[$c] > 0 && $data[$c] != '0' && $data[$c] != '') {
if ($row == 3) { $contractNoticeInsert[] = $data[$c];
$headers = $data; } else {
} $contractNoticeInsert[] = null;
elseif ($row > 3) { }
if ($num > count($datamapping0711)) { } elseif (($datamapping0712[$headers[$c]]) == "amendDate" || ($datamapping0712[$headers[$c]]) == "publishDate" || ($datamapping0712[$headers[$c]]) == "contractStart" || ($datamapping0712[$headers[$c]]) == "contractEnd") {
die("<font color=red>Error in data import; data mapping fields out of bounds or changed</font><br>" . $fname . print_r($data)); $contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c]));
} } else {
$contractNoticeInsert = Array(); if (strstr("\" =", $data[$c] > 0)) {
$supplierInsert = Array(); die("Invalid Description field" . $contractNoticeInsert);
$agencyInsert = Array(); }
$contractNoticeInsert[] = $fpath; $colvalue = preg_replace('/[^[:print:]]/', '', utf8_encode($data[$c]));
$keys = array_keys($datamapping0711);  
for ($c = 0; $c < $num; $c++) { $contractNoticeInsert[] = $colvalue;
$data[$c] = trim($data[$c], "="); }
$data[$c] = trim($data[$c], "\""); }
if ($tablename == "contractnotice") { }
if (in_array(($datamapping0711[$headers[$c]]) , $contractNoticeFields)) { flush();
if (($datamapping0711[$headers[$c]]) == "parentCN" || ($datamapping0711[$headers[$c]]) == "CNID") { $contractNoticeInsertQ->execute($contractNoticeInsert);
$data[$c] = substr($data[$c], 2); // take off the "CN" prefix $errors = $conn->errorInfo();
$data[$c] = str_replace("-A", "00", $data[$c]); // make amendments really big numbers if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {
if (!is_numeric($data[$c]) && $data[$c] != "") die($data[$c] . " is not numeric");  
if ($data[$c] > 0) { } elseif ($errors[1] <= 0) {
$contractNoticeInsert[] = $data[$c]; //print_r($errors);
} $success = 1;
else { } else {
$contractNoticeInsert[] = 0; foreach ($contractNoticeFields as $key => $cnf) {
} echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>";
} }
elseif (($datamapping0711[$headers[$c]]) == "supplierABN") { echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n";
if ($data[$c] > 0) { }
$contractNoticeInsert[] = $data[$c];  
} flush();
else { //echo "<hr>\n";
$contractNoticeInsert[] = null; }
} $row++;
} }
elseif (($datamapping0711[$headers[$c]]) == "amendDate" || ($datamapping0711[$headers[$c]]) == "publishDate" || ($datamapping0711[$headers[$c]]) == "contractStart" || ($datamapping0711[$headers[$c]]) == "contractEnd") { fclose($handle);
$contractNoticeInsert[] = date('Y-m-d H:i:s', strtotime($data[$c])); $contractNoticeInsertQ->closeCursor();
}  
else { return $success;
if (strstr("\" =", $data[$c] > 0)) { }
die("Invalid Description field" . $contractNoticeInsert);  
}  
$colvalue = preg_replace( '/[^[:print:]]/', '',utf8_encode( $data[$c]));  
   
$contractNoticeInsert[] = $colvalue;  
}  
}  
}  
else if ($tablename == "supplierdetails") {  
if (in_array(($datamapping0711[$headers[$c]]) , $supplierFields)) {  
if (($datamapping0711[$headers[$c]]) == "supplierABN") {  
if ($data[$c] > 0) {  
$contractNoticeInsert[] = $data[$c];  
}  
else {  
$contractNoticeInsert[] = 0;  
}  
}  
else {  
$supplierInsert[] = $data[$c];  
}  
}  
}  
else if ($tablename == "agency") {  
if (in_array(($datamapping0711[$headers[$c]]) , $agencyFields)) {  
$agencyInsert[] = $data[$c];  
}  
}  
}  
flush();  
if ($tablename == "contractnotice") {  
$contractNoticeInsertQ->execute($contractNoticeInsert);  
$errors = $conn->errorInfo();  
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {  
}  
elseif ($errors[1] == 0) {  
$success++;  
}  
else {  
foreach ($contractNoticeFields as $key => $cnf) {  
echo var_dump($contractNoticeInsert[$key]) . $cnf . "<br>";  
}  
echo $data[2] . " failed CN insert.<br>" . print_r($errors, true) . "<br> row $row <br><br>\n";  
}  
}  
else if ($tablename == "supplierdetails") {  
$supplierInsertQ->execute($supplierInsert);  
$errors = $conn->errorInfo();  
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {  
}  
elseif ($errors[1] == 0) {  
$success++;  
}  
else {  
echo $data[2] . " failed supplier insert.<br>" . print_r($errors, true) . " <br> " . print_r($supplierInsert, true) . "<br> $row <br><br>\n";  
}  
}  
else if ($tablename == "agency") {  
$agencyInsertQ->execute($agencyInsert);  
$errors = $conn->errorInfo();  
if ($errors[1] == 7 && strpos($errors[2], "duplicate key")) {  
}  
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/>"; $files[date("c", filemtime($path . $fname)) . md5($fname)] = $fname;
} }
} }
} }
} ksort($files);
else { foreach ($files as $date => $fname) {
$success = 0; echo "<a href=\"import.php?fname=$fname\">$fname</a>&nbsp;" . filesize($path . $fname) . "&nbsp;" . $date . "<br/>";
$fname = $_REQUEST["fname"]; }
echo " ============== $fname ============== <br>"; } else {
flush(); $success = 0;
$success+= processFile($path . $fname, "contractnotice"); $fname = $_REQUEST["fname"];
$success+= processFile($path . $fname, "agency"); echo " ============== $fname ============== <br>";
$success+= processFile($path . $fname, "supplier"); flush();
echo "<br> $success records successfully created"; $success+= processFile($path . $fname, "contractnotice");
flush(); $success+= processFile($path . $fname, "agency");
} $success+= processFile($path . $fname, "supplier");
// run post import data processing echo "<br> $success records successfully created";
   
  flush();
  // run post import data processing
  //
  if ($success > 0) {
  $conn->exec("update datasets set \"lastUpdated\" = NOW() where title = 'Contract Notices'");
  echo "link amend<br>";
  include ("linkAmendments.php");
  echo "update UNSPSC<br>";
  include ("updateUNSPSC.php");
  }
// cn // cn
//include ("linkAmendments.php");  
include ("updateUNSPSC.php");  
// agency // agency
include ("setAgencyStatus.php"); //include ("setAgencyStatus.php");
include ("setAgencyURLABN.php"); //include ("setAgencyURLABN.php");
  }
  }
?> ?>