new sitemap.xml
new sitemap.xml

--- 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 
@@ -24,14 +18,24 @@
 sum(("procurementMethod" = \'Direct\' OR "procurementMethod" = \'Limited tender\')::int) AS limited,
 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 +56,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/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,30 @@
     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,
+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']);
@@ -35,6 +52,32 @@
         $category = urlencode($row['category']);
         echo("<tr>
     <td><B><a href='displayCategory.php?category=$category'>{$row['category']}</a></b></td>
+    <td>{$row['count']}</td>
+    <td>\$$value</td>
+    </tr>");
+    }
+    echo "</table><br/>";
+
+    $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>");
@@ -67,6 +110,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
@@ -34,7 +34,7 @@
 sum(("procurementMethod" = \'Direct\' OR "procurementMethod" = \'Limited tender\')::int) AS limited,
 COUNT(*) AS total
 FROM contractnotice 
-WHERE' . $supplierQ . " " . $standardQ ;
+WHERE' . $supplierQ . " " . $yearQ . " " .$standardQ  ;
     $query = $conn->prepare($query);
     if ($supplierParts[0] > 0) {
         $query->bindParam(":supplierABN", $supplierABN);
@@ -48,7 +48,7 @@
     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';
+  WHERE ' . $supplierQ . " $yearQ $standardQ ". ' GROUP BY category ORDER BY count(*) DESC LIMIT 10';
     $query = $conn->prepare($query);
     if ($supplierParts[0] > 0) {
         $query->bindParam(":supplierABN", $supplierABN);
@@ -79,7 +79,7 @@
     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);

--- 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/")) {
@@ -100,6 +96,37 @@
     foreach ($a as $key => $value)
         $totale += array_sum_all($value);
     return $totale;
+}
+
+function show_stats($stats) {
+  echo "<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
@@ -157,6 +184,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 +316,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>';
 }
 ?>