fix supplier ID, add total value stat
fix supplier ID, add total value stat

--- a/displayAgency.php
+++ b/displayAgency.php
@@ -16,6 +16,7 @@
 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 "agencyName" LIKE :agency AND ' . " $yearQ $standardQ "   ;

--- a/displaySON.php
+++ b/displaySON.php
@@ -20,6 +20,7 @@
 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 "   ;
@@ -84,8 +85,10 @@
     }
     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"
+    $query = 'SELECT "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 
  WHERE "SONID" = :SONID AND abn NOT IN (SELECT DISTINCT "supplierABN" AS abn FROM contractnotice WHERE "SONID" = :SONID)';
     $query = $conn->prepare($query);

--- 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 . " " . $yearQ . " " .$standardQ  ;
     $query = $conn->prepare($query);
-    if ($supplierParts[0] > 0) {
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -50,7 +54,7 @@
     $query = 'SELECT category, min("categoryUNSPSC") AS "categoryUNSPSC", count(*) AS count, sum(value) AS value FROM contractnotice 
   WHERE ' . $supplierQ . " $yearQ $standardQ ". ' GROUP BY category ORDER BY count(*) DESC LIMIT 10';
     $query = $conn->prepare($query);
-    if ($supplierParts[0] > 0) {
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -84,7 +88,7 @@
 GROUP BY abn ORDER BY SUM("value") DESC';
     $query = $conn->prepare($query);
     if (count($supplierParts) > 0) {
-        if ($supplierParts[0] > 0) {
+        if (isset($supplierABN)) {
             $query->bindParam(":supplierABN", $supplierABN);
         } else {
             $query->bindParam(":supplierName", $supplierName);
@@ -118,7 +122,7 @@
     $query = $conn->prepare($query);
 
 
-    if ($supplierParts[0] > 0) {
+    if (isset($supplierABN)) {
         $query->bindParam(":supplierABN", $supplierABN);
     } else {
         $query->bindParam(":supplierName", $supplierName);
@@ -163,7 +167,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
@@ -99,7 +99,9 @@
 }
 
 function show_stats($stats) {
-  echo "<br/>";
+    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/>";
@@ -136,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;
@@ -184,7 +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">
+            <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 -->