more panel/category tables
[contractdashboard.git] / displaySupplier.php
blob:a/displaySupplier.php -> blob:b/displaySupplier.php
--- a/displaySupplier.php
+++ b/displaySupplier.php
@@ -4,8 +4,11 @@
 if ($_REQUEST['supplier']) {
     $supplierS = htmlentities(strip_tags($_REQUEST['supplier']));
     $title = $supplierName;
-    if ($supplierABN) {
-        $query = 'SELECT text_mode("supplierName") AS "supplierName" FROM contractnotice WHERE "supplierABN" = :supplierABN GROUP BY "supplierABN"';
+    if (isset($supplierABN)) {
+        $query = 'SELECT text_mode("supplierName") AS "supplierName" 
+FROM contractnotice 
+WHERE "supplierABN" = :supplierABN 
+GROUP BY "supplierABN"';
         $query = $conn->prepare($query);
         $query->bindParam(":supplierABN", $supplierABN);
         $query->execute();
@@ -14,22 +17,72 @@
     }
     include_header(str_replace("%", "", $title));
     echo '<center><h1>' . str_replace("%", "", $title) . '</h1></center>';
-if ($supplierABN) {
+
+if (isset($supplierABN)) {
+    echo "<b>ABN:</b> <a href=\"https://abr.business.gov.au/SearchByAbn.aspx?abn=$supplierABN\">$supplierABN</a>";
+
+    $cleansedName = '%'.cleanseName($title).'%';
+    $query = 'SELECT DISTINCT ON ("supplierABN") "supplierName","supplierABN" FROM contractnotice WHERE "supplierName" ILIKE :cleansedName and "supplierABN" != :supplierABN';
+    $query = $conn->prepare($query);
+    $query->bindParam(":cleansedName", $cleansedName);
+    $query->bindParam(":supplierABN", $supplierABN);
+    $query->execute();
+    databaseError($conn->errorInfo());
+    echo "<br/><b>Similar to:</b> ";
+    foreach ($query->fetchAll() as $row) {
+        echo "<a href='displaySupplier.php?supplier={$row['supplierABN']}-".urlencode($row['supplierName'])."'> {$row['supplierName']} (ABN: {$row['supplierABN']})</a>, ";
+    }
+
     $query = 'SELECT distinct on (lower("supplierName")) "supplierName" from contractnotice where "supplierABN" = :supplierABN';
     $query = $conn->prepare($query);
     $query->bindParam(":supplierABN", $supplierABN);
     $query->execute();
     databaseError($conn->errorInfo());
-    echo "Also known as: ".implode(', ', $query->fetchAll(PDO::FETCH_COLUMN, 0));
+    $names = Array();
+    foreach ($query->fetchAll() as $row) {
+        $names[cleanseName($row[0])] = $row[0];
+    }
+    if (count($names) < 20) {
+        echo "<br/><b>Also known as:</b> " . implode(', ', array_values($names))."<br/>";
+    } else {
+        echo "<details>
+  <summary>Also known as ...</summary>
+  <p>".implode(', ', array_values($names))."</p>
+</details>";
+    }
+
 }
-    /*
-     * Procurement method in text, seperate Open via SON from Open
-     * Consultancies, Confidentialities percentage
-     */
+
+
+    $query = '
+    SELECT 
+sum((consultancy = \'Yes\')::int) AS consultancy,
+sum(("confidentialityContract" = \'Yes\')::int) AS "confidentialityContract",
+sum(("confidentialityOutputs" = \'Yes\')::int) AS "confidentialityOutputs",
+sum((("procurementMethod" = \'Open\' OR "procurementMethod" = \'Open tender\') AND "SONID" IS null)::int) AS open,
+sum((("procurementMethod" = \'Open\' OR "procurementMethod" = \'Open tender\') AND "SONID" IS NOT null)::int) AS "openSON",
+sum(("procurementMethod" = \'Prequalified tender\' OR "procurementMethod" = \'Select\')::INT) AS prequalified,
+sum(("procurementMethod" = \'Direct\' OR "procurementMethod" = \'Limited tender\')::int) AS limited,
+sum("value") as total_value,
+COUNT(*) AS total
+FROM contractnotice 
+WHERE' . $supplierQ . " " . $yearQ . " " .$standardQ  ;
+    $query = $conn->prepare($query);
+    if (isset($supplierABN)) {
+        $query->bindParam(":supplierABN", $supplierABN);
+    } else {
+        $query->bindParam(":supplierName", $supplierName);
+    }
+    $query->execute();
+    databaseError($conn->errorInfo());
+
+    $stats = reset($query->fetchAll());
+    show_stats($stats);
+
     $query = 'SELECT category, min("categoryUNSPSC") AS "categoryUNSPSC", count(*) AS count, sum(value) AS value FROM contractnotice 
-  WHERE ' . $supplierQ . " " . $standardQ . ' GROUP BY category ORDER BY count(*) DESC LIMIT 10';
-    $query = $conn->prepare($query);
-    if ($supplierParts[0] > 0) {
+  WHERE ' . $supplierQ . " $yearQ $standardQ ". ' GROUP BY category ORDER BY count(*) DESC LIMIT 10';
+    $query = $conn->prepare($query);
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -58,17 +111,18 @@
     echo "</table><br/>";
 
 
-    $query = 'SELECT SUM("value") AS "value", count(*), MAX(contractnotice."agencyName") AS agencyname FROM contractnotice JOIN agency_nametoabn ON contractnotice."agencyName"=agency_nametoabn."agencyName"  
+    /*$query = 'SELECT SUM("value") AS "value", count(*), text_mode(contractnotice."agencyName") AS agencyname FROM contractnotice JOIN agency_nametoabn ON contractnotice."agencyName"=agency_nametoabn."agencyName"
 WHERE ' . $yearQ . ' ' . $supplierQ . ' "childCN" IS NULL 
-GROUP BY abn ORDER BY SUM("value") DESC';
-    $query = $conn->prepare($query);
-    if (count($supplierParts) > 0) {
-        if ($supplierParts[0] > 0) {
+GROUP BY abn ORDER BY SUM("value") DESC';*/
+    $query = 'SELECT SUM("value") AS "value", count(*), "agencyName" AS agencyname FROM contractnotice 
+WHERE ' . $yearQ . ' ' . $supplierQ . ' "childCN" IS NULL GROUP BY "agencyName" ORDER BY SUM("value") DESC';
+
+    $query = $conn->prepare($query);
+        if (isset($supplierABN)) {
             $query->bindParam(":supplierABN", $supplierABN);
         } else {
             $query->bindParam(":supplierName", $supplierName);
         }
-    }
     $query->execute();
     databaseError($conn->errorInfo());
 
@@ -97,7 +151,7 @@
     $query = $conn->prepare($query);
 
 
-    if ($supplierParts[0] > 0) {
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -142,7 +196,7 @@
     $query = $conn->prepare($query);
 
 
-    if ($supplierParts[0] > 0) {
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -172,11 +226,15 @@
 FROM contractnotice
 WHERE ' . $yearQ . ' "childCN" IS NULL
 GROUP BY supplierID,"supplierABN"
-ORDER BY val DESC
-LIMIT 100';
-    $query = $conn->prepare($query);
-    $query->execute();
-    databaseError($conn->errorInfo());
+ORDER BY val DESC';
+if ($yearQ == '') $query .=' LIMIT 1000';
+    $query = $conn->prepare($query);
+    $query->execute();
+    databaseError($conn->errorInfo());
+if ($query->rowCount() > 999 && $yearQ == '') {
+// if 1000 records warn too many results
+print "<b> More than 1000 results found so only first 1000 shown. Please filter by year to see all results </b>";
+}
     echo "<table>  <thead>
     <tr>
        <th>Position</th>