add unspsc code infro for categories
add unspsc code infro for categories

--- a/displayCategory.php
+++ b/displayCategory.php
@@ -1,18 +1,71 @@
 <?php
-include_once ("./lib/common.inc.php");
+include_once("./lib/common.inc.php");
 if ($_REQUEST['category']) {
-    include_header($_REQUEST['category']." goods and services");
-    echo "<center><h1>".$_REQUEST['category']."</h1></center>";
-  $query = 'SELECT "CNID", description, value, "agencyName", category, "contractStart", "supplierName"
+
+    // if UNSPSC code known show, say if is a parent and offer to include children, if child link to parent
+    $query = 'SELECT key, d."UNSPSC", "Title" AS category FROM
+(SELECT \'categoryUNSPSC\' AS key, "categoryUNSPSC"::TEXT AS "UNSPSC" FROM contractnotice WHERE category = :category  
+UNION SELECT \'parentUNSPSC\', substr( "categoryUNSPSC"::TEXT, 0, 5 )||\'0000\'::TEXT AS "parentUNSPSC" FROM contractnotice WHERE category = :category 
+UNION SELECT \'grandparentUNSPSC\', substr( "categoryUNSPSC"::TEXT, 0, 3 )||\'000000\'::TEXT AS "grandparentUNSPSC" 
+FROM contractnotice WHERE category = :category ) d 
+INNER JOIN (SELECT "Title", "UNSPSC" FROM "UNSPSCcategories" ) c ON c."UNSPSC"=d."UNSPSC"::INTEGER';
+    $query = $conn->prepare($query);
+    $query->bindParam(":category", $_REQUEST['category']);
+    $query->execute();
+    databaseError($conn->errorInfo());
+    $unspsc = Array();
+    foreach ($query->fetchAll() as $d) {
+        $unspsc[$d['key']] = $d;
+    }
+
+
+    include_header($_REQUEST['category'] . " goods and services");
+    if ($unspsc['categoryUNSPSC']) {
+        echo "<center><h1>{$_REQUEST['category']} ({$unspsc['categoryUNSPSC']['UNSPSC']})</h1></center>";
+
+        if ($unspsc['parentUNSPSC']['UNSPSC'] != $unspsc['categoryUNSPSC']['UNSPSC']) {
+            echo "Parent Category: <a href='displayCategory.php?category={$unspsc['parentUNSPSC']['category']}'>{$unspsc['parentUNSPSC']['UNSPSC']} {$unspsc['parentUNSPSC']['category']}</a> <br/>";
+        }
+        if ($unspsc['grandparentUNSPSC']['UNSPSC'] != $unspsc['categoryUNSPSC']['UNSPSC']) {
+            echo "Grandparent Category: <a href='displayCategory.php?category={$unspsc['grandparentUNSPSC']['category']}'>{$unspsc['grandparentUNSPSC']['UNSPSC']} {$unspsc['grandparentUNSPSC']['category']}</a></br>";
+        }
+
+    } else {
+        echo "<center><h1>{$_REQUEST['category']}</h1></center>";
+    }
+
+    $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 category = :category AND ' . " $yearQ $standardQ ";
+    $query = $conn->prepare($query);
+    $query->bindParam(":category", $_REQUEST['category']);
+    $query->execute();
+    databaseError($conn->errorInfo());
+
+    $stats = reset($query->fetchAll());
+    show_stats($stats);
+
+
+    $query = 'SELECT "CNID", description, value, "agencyName", category, "contractStart", "supplierName"
     FROM contractnotice
-    WHERE '.$yearQ.' "childCN" is null 
-    AND category = :category
-    ORDER BY value DESC';
-$query = $conn->prepare($query);
-$query->bindParam(":category", $_REQUEST['category']);
-	$query->execute();
-		databaseError($conn->errorInfo());
-	
+    WHERE ' . $yearQ . ' "childCN" IS NULL 
+    AND CATEGORY = :CATEGORY
+    ORDER BY VALUE DESC';
+    $query = $conn->prepare($query);
+    $query->bindParam(":CATEGORY", $_REQUEST['category']);
+    $query->execute();
+    databaseError($conn->errorInfo());
+
     echo "<table>  <thead>
     <tr>
       <th>Contract Notice Number</th>
@@ -23,42 +76,42 @@
       <th>Supplier</th>
     </tr>
   </thead>";
-  foreach ($query->fetchAll() as $row) {
-    setlocale(LC_MONETARY, 'en_US');
-    $value = number_format(doubleval($row['value']) , 2);
-    echo ("<tr>
+    foreach ($query->fetchAll() as $row) {
+        setlocale(LC_MONETARY, 'en_US');
+        $value = number_format(doubleval($row['value']), 2);
+        echo("<tr>
     <td><a href=\"displayContract.php?CNID={$row['CNID']}\">{$row['CNID']}</a></td>
     <td><b>{$row['description']}</b></a></td>
     <td>\$$value</td><td>{$row['agencyName']}</td>
     <td>{$row['contractStart']}</td>
     <td>{$row['supplierName']}</td>
     </tr>");
-  }
-  echo "</table>";
+    }
+    echo "</table>";
 } else {
     /*
      split by main categories
     */
     include_header("Categories");
-$query = 'SELECT sum(value), category
+    $query = 'SELECT sum(value), category
 FROM contractnotice
-WHERE '.$yearQ.' "childCN" is null
-GROUP BY category ORDER BY sum(value) DESC ';
-$query = $conn->prepare($query);
-	$query->execute();
-		databaseError($conn->errorInfo());
-  echo "<table>  <thead>
+WHERE ' . $yearQ . ' "childCN" IS NULL
+GROUP BY CATEGORY ORDER BY sum(VALUE) DESC ';
+    $query = $conn->prepare($query);
+    $query->execute();
+    databaseError($conn->errorInfo());
+    echo "<table>  <thead>
     <tr>
       <th>Category</th>
       <th>Total Contracts Value</th>
     </tr>
   </thead>";
-  foreach ($query->fetchAll() as $row) {
-  setlocale(LC_MONETARY, 'en_US');
-  $value = number_format(doubleval($row[0]) , 2);
-  echo ("<tr><td><A href=\"displayCategory.php?category={$row[1]}\"><b>{$row[1]}</b></a></td><td>\$$value</td></tr>");
-}
-echo "</table>";
+    foreach ($query->fetchAll() as $row) {
+        setlocale(LC_MONETARY, 'en_US');
+        $value = number_format(doubleval($row[0]), 2);
+        echo("<tr><td><A href=\"displayCategory.php?category={$row[1]}\"><b>{$row[1]}</b></a></td><td>\$$value</td></tr>");
+    }
+    echo "</table>";
 }
 include_footer();
 ?>

--- a/displaySON.php
+++ b/displaySON.php
@@ -85,11 +85,11 @@
     }
     echo "</table><br/>";
 
-    $query = 'SELECT "supplierABN", text_mode("supplierName") AS "supplierName", count(*), sum(value) AS value,
+    $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 FROM standingoffer_suppliers 
+ 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']);

--- a/displaySupplier.php
+++ b/displaySupplier.php
@@ -83,17 +83,18 @@
     echo "</table><br/>";
 
 
-    $query = 'SELECT SUM("value") AS "value", count(*), text_mode(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) {
+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());