more panel/category tables
[contractdashboard.git] / displaySON.php
blob:a/displaySON.php -> blob:b/displaySON.php
--- a/displaySON.php
+++ b/displaySON.php
@@ -10,13 +10,31 @@
     include_header($title);
     echo "<center><h1>" . $_REQUEST['SONID'] . " - " . $title . "</h1></center>";
     echo '<br><a href="https://www.tenders.gov.au/?event=public.advancedsearch.CNSONRedirect&type=sonSearchEvent&SONID=' . $_REQUEST['SONID'] . '"> View original record @ tenders.gov.au</a><br>';
-    /*
-     * 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 "SONID" = :SONID AND ' . " $yearQ $standardQ "   ;
+    $query = $conn->prepare($query);
+    $query->bindParam(":SONID", $_REQUEST['SONID']);
+    $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 "SONID" = :SONID 
+WHERE "SONID" = :SONID  AND ' . " $yearQ $standardQ " . ' 
 GROUP BY category';
     $query = $conn->prepare($query);
     $query->bindParam(":SONID", $_REQUEST['SONID']);
@@ -41,9 +59,37 @@
     }
     echo "</table><br/>";
 
-    $query = 'SELECT "supplierABN", text_mode("supplierName") AS "supplierName", count(*), sum(value) AS value FROM contractnotice
- WHERE ' . $yearQ . ' "childCN" IS NULL AND "SONID" = :SONID GROUP BY "supplierABN"
- UNION SELECT abn AS "supplierABN",name AS "supplierName",0 AS sum,0 AS value FROM standingoffer_suppliers 
+    $query = 'SELECT text_mode(contractnotice."agencyName") AS agencyname, count(*), sum(value) AS value FROM contractnotice
+ WHERE ' . $yearQ . ' "childCN" IS NULL AND "SONID" = :SONID GROUP BY "agencyName"';
+    $query = $conn->prepare($query);
+    $query->bindParam(":SONID", $_REQUEST['SONID']);
+    $query->execute();
+    databaseError($conn->errorInfo());
+
+    echo "<h3>Agencies</h3><table>  <thead>
+    <tr>
+      <th>Agency</th>
+      <th>Contracts Count</th>
+      <th>Total Contract Value</th>
+    </tr>
+  </thead>";
+    foreach ($query->fetchAll() as $row) {
+        setlocale(LC_MONETARY, 'en_US');
+        $value = number_format(doubleval($row['value']), 2);
+
+        echo("<tr>
+ <td><B><a href=\"displayAgency . php ? agency = $agency\">{$row['agencyname']}</a></b></td>
+    <td>{$row['count']}</td>
+    <td>\$$value</td>
+    </tr>");
+    }
+    echo "</table><br/>";
+
+    $query = 'SELECT min("supplierABN"), text_mode("supplierName") AS "supplierName", count(*), sum(value) AS value,
+ (CASE WHEN "supplierABN" != 0 THEN "supplierABN"::TEXT ELSE lower("supplierName") END) AS "supplierID"
+FROM contractnotice
+ WHERE ' . $yearQ . ' "childCN" IS NULL AND "SONID" = :SONID GROUP BY "supplierID"
+ UNION SELECT abn AS "supplierABN",name AS "supplierName",0 AS sum,0 AS value, NULL AS "supplierID" FROM standingoffer_suppliers 
  WHERE "SONID" = :SONID AND abn NOT IN (SELECT DISTINCT "supplierABN" AS abn FROM contractnotice WHERE "SONID" = :SONID)';
     $query = $conn->prepare($query);
     $query->bindParam(":SONID", $_REQUEST['SONID']);
@@ -67,6 +113,7 @@
     <td>\$$value</td>
     </tr>");
     }
+
     echo "</table><br/><h3>Contracts</h3><table>  <thead>
     <tr>
       <th>Contract Notice Number</th>
@@ -99,9 +146,7 @@
     }
     echo "</table>";
 } else {
-    /*
-     split by main categories
-    */
+
     include_header("Standing Offers");
     $query = 'SELECT sum(value), count(*), contractnotice."SONID", min(title) AS title, text_mode(category) as category
 FROM contractnotice INNER JOIN standingoffers ON contractnotice."SONID" = standingoffers."SONID"