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

--- a/displayAgency.php
+++ b/displayAgency.php
@@ -6,12 +6,6 @@
     $agency = htmlentities(strip_tags($_REQUEST['agency']));
     include_header($agency);
     echo '<center><h1>' . $agency . '</h1></center>';
-    $query = 'SELECT category, min("categoryUNSPSC")  AS "categoryUNSPSC", count(*) AS count, sum(value) AS value  FROM contractnotice 
-  WHERE "agencyName" LIKE :agency AND ' . " " . $standardQ . ' ' . $yearQ . ' GROUP BY category ORDER BY count(*) DESC LIMIT 10';
-    $query = $conn->prepare($query);
-    $query->bindParam(":agency", $agency);
-    $query->execute();
-    databaseError($conn->errorInfo());
 
     $query = '
     SELECT 
@@ -22,16 +16,27 @@
 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 "supplierABN" = \'51194660183\'
-    ';
+ WHERE "agencyName" LIKE :agency AND ' . " $yearQ $standardQ "   ;
+    $query = $conn->prepare($query);
+    $query->bindParam(":agency", $agency);
+    $query->execute();
+    databaseError($conn->errorInfo());
+
+    $stats = reset($query->fetchAll());
+    show_stats($stats);
     /*
-    * Procurement method in text, seperate Open via SON from Open
-    * Consultancies, Confidentialities percentage
+
       Compliance statistics: amendments, delay in reporting average and number completely late */
 
-
+    $query = 'SELECT category, min("categoryUNSPSC")  AS "categoryUNSPSC", count(*) AS count, sum(value) AS value  FROM contractnotice 
+  WHERE "agencyName" LIKE :agency AND ' . " $yearQ $standardQ " . ' GROUP BY category ORDER BY count(*) DESC LIMIT 10';
+    $query = $conn->prepare($query);
+    $query->bindParam(":agency", $agency);
+    $query->execute();
+    databaseError($conn->errorInfo());
     echo "<h3>Categories</h3><table>  <thead>
     <tr>
       <th>Category</th>
@@ -52,7 +57,7 @@
     echo "</table><br/>";
 
     $query = 'SELECT contractnotice."SONID", min(title) AS title, count(*), sum(value) AS value FROM contractnotice INNER JOIN standingoffers ON contractnotice."SONID" = standingoffers."SONID" 
-WHERE "agencyName" LIKE :agency AND ' . " " . $standardQ . ' ' . $yearQ . 'AND contractnotice."SONID" != \'\' GROUP BY contractnotice."SONID"';
+WHERE "agencyName" LIKE :agency AND ' . " $yearQ $standardQ " . 'AND contractnotice."SONID" != \'\' GROUP BY contractnotice."SONID"';
     $query = $conn->prepare($query);
     $query->bindParam(":agency", $agency);
     $query->execute();

--- 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/displayProcurementMethod.php
+++ b/displayProcurementMethod.php
@@ -12,7 +12,7 @@
 include_header("Procurement Methods");
 $query = 'SELECT SUM(value) as value, "procurementMethod"
 FROM contractnotice
-WHERE "childCN" is null
+WHERE ' . $yearQ. " ". $standardQ .'
 GROUP BY "procurementMethod"';
 
     $query = $conn->prepare($query);

--- 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>

--- 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,7 +17,7 @@
     }
     include_header(str_replace("%", "", $title));
     echo '<center><h1>' . str_replace("%", "", $title) . '</h1></center>';
-if ($supplierABN) {
+if (isset($supplierABN)) {
     $query = 'SELECT distinct on (lower("supplierName")) "supplierName" from contractnotice where "supplierABN" = :supplierABN';
     $query = $conn->prepare($query);
     $query->bindParam(":supplierABN", $supplierABN);
@@ -32,11 +35,12 @@
 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 . " " . $standardQ ;
-    $query = $conn->prepare($query);
-    if ($supplierParts[0] > 0) {
+WHERE' . $supplierQ . " " . $yearQ . " " .$standardQ  ;
+    $query = $conn->prepare($query);
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -48,9 +52,9 @@
     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);
@@ -79,17 +83,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());
 
@@ -118,7 +123,7 @@
     $query = $conn->prepare($query);
 
 
-    if ($supplierParts[0] > 0) {
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -163,7 +168,7 @@
     $query = $conn->prepare($query);
 
 
-    if ($supplierParts[0] > 0) {
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);

--- a/lib/common.inc.php
+++ b/lib/common.inc.php
@@ -4,11 +4,7 @@
 error_reporting(E_ALL ^ E_NOTICE);
 
 
-$conn = new PDO("pgsql:dbname=contractdashboard;user=postgres;password=snmc;host=localhost");
-
-if (!$conn) {
-    die("A database error occurred.\n");
-}
+include('db.inc.php');
 
 define('ROOT', pathinfo(__FILE__, PATHINFO_DIRNAME));
 if (strstr($_SERVER['PHP_SELF'], "labs/") || strstr($_SERVER['PHP_SELF'], "admin/") || strstr($_SERVER['PHP_SELF'], "heuristics/")) {
@@ -102,6 +98,39 @@
     return $totale;
 }
 
+function show_stats($stats) {
+    setlocale(LC_MONETARY, 'en_US');
+  $value = number_format(doubleval($stats['total_value']) , 2);
+  echo "<br/> {$stats['total']} contracts, total value $$value <br/>";
+      if ($stats['consultancy'] > 0) {
+        echo "<b>Consultancies:</b> {$stats['consultancy']} contracts, ".percent($stats['consultancy'],$stats['total']);
+        echo "%<br/>";
+    }
+    if ($stats['confidentialityContract']+$stats['confidentialityOutputs'] > 0) {
+        $confidentialities = $stats['confidentialityContract']+$stats['confidentialityOutputs'];
+        echo "<b>Confidentialities:</b> $confidentialities contracts, ".percent( $confidentialities,$stats['total'])."%";
+        echo "( {$stats['confidentialityContract']} on the contract, {$stats['confidentialityOutputs']} on the outputs)";
+        echo "<br/>";
+    }
+    echo "<b>Procurement methods:</b> ";
+    if ($stats['open'] > 0) {
+        echo "Open {$stats['open']} contracts, ".percent($stats['open'],$stats['total']);
+        echo "%, ";
+    }
+    if ($stats['openSON'] > 0) {
+        echo "Open via SON {$stats['openSON']} contracts, ".percent($stats['openSON'],$stats['total']);
+        echo "%, ";
+    }
+    if ($stats['prequalified'] > 0) {
+        echo "Prequalified {$stats['prequalified']} contracts, ".percent($stats['prequalified'],$stats['total']);
+        echo "%, ";
+    }
+    if ($stats['limited'] > 0) {
+        echo "limited {$stats['limited']} contracts, ".percent($stats['limited'],$stats['total']);
+        echo "%, ";
+    }
+}
+
 // magic query modifiers
 $agency = filter_var($_REQUEST['agency'], FILTER_SANITIZE_STRING);
 if ($agency != "")
@@ -109,13 +138,17 @@
 
 $supplier = filter_var($_REQUEST['supplier'], FILTER_SANITIZE_STRING);
 if ($supplier != "") {
+  if ($supplier[0] !== '-') {
     $supplierParts = explode("-", $supplier);
-    $supplierName = "%" . $supplierParts[1] . "%";
-    $supplierABN = $supplierParts[0];
-    if ($supplierParts[0] > 0)
-        $supplierQ = ' "supplierABN" = :supplierABN AND ';
-    else
-        $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
+
+    $supplierABN = array_shift($supplierParts);
+    $supplierName = "%" . implode('-',$supplierParts) . "%";
+
+    $supplierQ = ' "supplierABN" = :supplierABN AND ';
+   } else {
+    $supplierName = "%" . substr($supplier,1) . "%";
+    $supplierQ = ' "supplierName" ILIKE :supplierName AND ';
+  }
 }
 
 $startYear = 2007;
@@ -157,6 +190,7 @@
               pcdt: http://purl.org/procurement/public-contracts-datatypes#">
         <head>
             <title><?php echo $title; ?> - Contract Dashboard</title>
+            <meta name="viewport" content="width=device-width, initial-scale=1">
             <link rel="stylesheet" type="text/css" href="bootstrap.min.css"> 
             <link rel="stylesheet" type="text/css" href="bootstrap-responsive.css"> 
             <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
@@ -288,7 +322,7 @@
                 	<div class="well sidebar-nav">
                     		<li class="nav-header">Year</li>
 <?php
-foreach (range(2007,2016) as $year) {
+foreach (range(2007,2017) as $year) {
 $get = $_GET;
 $get['year'] = $year;
 $query = http_build_query($get);

--- a/sitemap.xml.php
+++ b/sitemap.xml.php
@@ -1,20 +1,22 @@
 <?php
-include ('lib/common.inc.php');
-$last_updated['page'] = date('Y-m-d',@filemtime('index.php'));
+include('lib/common.inc.php');
+$last_updated['page'] = date('Y-m-d', @filemtime('index.php'));
 $sections = Array(
     "agency",
     "category",
     "supplier",
+    "SON",
     "page"
 );
 header("Content-Type: text/xml");
 echo "<?xml version='1.0' encoding='UTF-8'?>";
-$result = $conn->query('SELECT title, to_char("lastUpdated",\'YYYY-MM-DD\') as "lastUpdated" from datasets');
+$result = $conn->query('SELECT title, to_char("lastUpdated",\'YYYY-MM-DD\') AS "lastUpdated" FROM datasets');
 foreach ($result->fetchAll() as $row) {
     if ($row['title'] == "Contract Notices") {
         $last_updated['agency'] = $row['lastUpdated'];
         $last_updated['supplier'] = $row['lastUpdated'];
         $last_updated['category'] = $row['lastUpdated'];
+        $last_updated['SON'] = $row['lastUpdated'];
     }
 }
 if (isset($_REQUEST['section']) == false) {
@@ -22,47 +24,68 @@
          xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd"
          xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
     foreach ($sections as $section) {
-        echo "<sitemap>
-      <loc>" . local_url() . "sitemap.xml.php?section=$section</loc>
+        $pages = 0;
+        $i = 0;
+        if ($section == 'supplier') $pages = 1;
+        do {
+            echo "<sitemap>
+      <loc>" . local_url() . "sitemap.xml.php?section=$section&amp;page=$i</loc>
       <lastmod>" . $last_updated[$section] . '</lastmod></sitemap>';
+            $i++;
+        } while ($i <= $pages);
     }
     echo '</sitemapindex>';
 } else {
-   echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
+    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
     if ($_REQUEST['section'] == "page") {
-      echo " <url><loc>".local_url()."index.php</loc><priority>1.0</priority></url>\n";
-foreach (scandir("./") as $file) {
-      if (strpos($file,".php") !== false && $file != "index.php" && $file != "sitemap.xml.php") echo " <url><loc>".local_url()."$file</loc><priority>0.3</priority></url>\n";
-}
+        echo " <url><loc>" . local_url() . "index.php</loc><priority>1.0</priority></url>\n";
+        foreach (scandir("./") as $file) {
+            if (strpos($file, ".php") !== false && $file != "index.php" && $file != "sitemap.xml.php") echo " <url><loc>" . local_url() . "$file</loc><priority>0.3</priority></url>\n";
+        }
     }
-        if ($_REQUEST['section'] == "agency") {
-       $result = $conn->query('SELECT DISTINCT "agencyName" from contractnotice');
-       foreach ($result->fetchAll() as $row) {
+    if ($_REQUEST['section'] == "agency") {
+        $result = $conn->query('SELECT DISTINCT "agencyName" FROM contractnotice');
+
+        foreach ($result->fetchAll() as $row) {
             echo " <url><loc>" . local_url() . "displayAgency.php?agency=" . urlencode($row['agencyName']) . "</loc>
       <lastmod>" . $last_updated['agency'] . "</lastmod></url>\n";
         }
     }
     if ($_REQUEST['section'] == "supplier") {
-       $result = $conn->query('SELECT DISTINCT on ("supplierID") (
- case when "supplierABN" != 0 THEN "supplierABN"::text ELSE lower("supplierName") END) as "supplierID", "supplierABN", "supplierName"  from contractnotice');
-       
-       foreach ($result->fetchAll() as $row) {
-           
-           $supplier = urlencode(stripslashes($row['supplierABN'] . '-' . $row['supplierName']));
+        $start = $_REQUEST['page'] * 40000;
+        $result = $conn->prepare('SELECT DISTINCT ON ("supplierID") (
+ CASE WHEN "supplierABN" != 0 THEN "supplierABN"::TEXT ELSE lower("supplierName") END) AS "supplierID", "supplierABN", "supplierName"  
+ FROM contractnotice
+ order by "supplierID"
+ LIMIT 40000 OFFSET :start
+');
+        $result->execute(array(':start' => $start));
+        foreach ($result->fetchAll() as $row) {
+
+            $supplier = urlencode(stripslashes($row['supplierABN'] . '-' . $row['supplierName']));
             echo " <url><loc>" . local_url() . "displaySupplier.php?supplier=$supplier</loc>
       <lastmod>" . $last_updated['supplier'] . "</lastmod></url>\n";
         }
     }
     if ($_REQUEST['section'] == "category") {
-       $result = $conn->query('SELECT distinct category FROM contractnotice;');
-       foreach ($result->fetchAll() as $row) {
-           
-            echo " <url><loc>" . local_url() . "displayCategory.php?category=".urlencode($row['category'])."</loc>
+        $result = $conn->query('SELECT DISTINCT category FROM contractnotice;');
+        foreach ($result->fetchAll() as $row) {
+
+            echo " <url><loc>" . local_url() . "displayCategory.php?category=" . urlencode($row['category']) . "</loc>
       <lastmod>" . $last_updated['category'] . "</lastmod></url>\n";
-           
+
         }
     }
-  echo '</urlset>';
+    if ($_REQUEST['section'] == "SON") {
+        $result = $conn->query('SELECT DISTINCT "SONID" FROM contractnotice;');
+        foreach ($result->fetchAll() as $row) {
+
+            echo " <url><loc>" . local_url() . "displaySON.php?SONID=" . urlencode($row['SONID']) . "</loc>
+      <lastmod>" . $last_updated['SON'] . "</lastmod></url>\n";
+
+        }
+    }
+    echo '</urlset>';
 }
 ?>