Merge branch 'master' of ssh://apples.lambdacomplex.org/git/contractdashboard
Merge branch 'master' of ssh://apples.lambdacomplex.org/git/contractdashboard

<?php <?php
include_once ("../lib/common.inc.php"); include_once ("../lib/common.inc.php");
$query = 'SELECT c."CNID",c."parentCN",c."amendmentReason",p."childCN" FROM contractnotice as c LEFT OUTER JOIN contractnotice as p on c."parentCN" = p."CNID" $query = 'SELECT c."CNID",c."parentCN",c."amendmentReason",p."childCN" FROM contractnotice as c LEFT OUTER JOIN contractnotice as p on c."parentCN" = p."CNID"
WHERE WHERE
c."parentCN" > 0 AND p."childCN" IS NULL '; c."parentCN" > 0 AND p."childCN" IS NULL ';
$query = $conn->prepare($query); $query = $conn->prepare($query);
$query->execute(); $query->execute();
if (!$query) { if (!$query) {
databaseError($conn->errorInfo()); databaseError($conn->errorInfo());
} }
foreach ($query->fetchAll() as $row) { foreach ($query->fetchAll() as $row) {
$conn->exec('UPDATE contractnotice SET "childCN" = \'' . $conn->exec('UPDATE contractnotice SET "childCN" = \'' .
$row['CNID'] . '\', "amendmentReason" = \'' . $row['CNID'] . '\', "amendmentReason" = \'' .
$row['amendmentReason'] . '\' where "CNID" = \'' . $row['amendmentReason'] . '\' where "CNID" = \'' .
$row['parentCN'] . '\';'); $row['parentCN'] . '\';');
echo 'UPDATE contractnotice SET "childCN" = \'' . echo 'UPDATE contractnotice SET "childCN" = \'' .
$row['CNID'] . '\', "amendmentReason" = \'' . $row['CNID'] . '\', "amendmentReason" = \'' .
$row['amendmentReason'] . '\' where "CNID" = \'' . $row['amendmentReason'] . '\' where "CNID" = \'' .
$row['parentCN'] . '\';'; $row['parentCN'] . '\';';
$errors = $conn->errorInfo(); $errors = $conn->errorInfo();
if ($errors[1] == 7 || $errors[1] ==0) if ($errors[1] == 7 || $errors[1] ==0)
echo $row['CNID'] . " linked to parent " . $row['parentCN'] . echo $row['CNID'] . " linked to parent " . $row['parentCN'] .
"<br>\n"; "<br>\n";
else print_r($errors); else print_r($errors);
   
} }
  // also need to eliminate CN 100528/100529 - check for double parent CNs with no childCN, latest sequent CN id wins childCN = 0
?> ?>
   
<!DOCTYPE html> <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<meta charset="UTF-8"/> <meta charset="UTF-8"/>
<title>Minimal BubbleTree Demo</title> <title>Minimal BubbleTree Demo</title>
<script type="text/javascript" src="lib/bubbletree/lib/jquery-1.5.2.min.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/jquery.history.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/jquery.history.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/raphael.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/raphael.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/vis4.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/vis4.js"></script>
<script type="text/javascript" src="lib/bubbletree/lib/Tween.js"></script> <script type="text/javascript" src="lib/bubbletree/lib/Tween.js"></script>
<script type="text/javascript" src="lib/bubbletree/build/bubbletree.js"></script> <script type="text/javascript" src="lib/bubbletree/build/bubbletree.js"></script>
<link rel="stylesheet" type="text/css" href="lib/bubbletree/build/bubbletree.css" /> <link rel="stylesheet" type="text/css" href="lib/bubbletree/build/bubbletree.css" />
<script type="text/javascript" src="lib/bubbletree/styles/cofog.js"></script> <script type="text/javascript" src="lib/bubbletree/styles/cofog.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function() { $(function() {
<?php <?php
include_once ("lib/common.inc.php"); include_once ("lib/common.inc.php");
   
  include("lib/Color.php");
  $color = new Lux_Color();
   
   
$unspscresult = mysql_query("select * from UNSPSCcategories;"); $unspscresult = mysql_query("select * from UNSPSCcategories;");
while ($row = mysql_fetch_assoc($unspscresult)) { while ($row = mysql_fetch_assoc($unspscresult)) {
$unspsc[$row['UNSPSC']] = $row['Title']; $unspsc[$row['UNSPSC']] = $row['Title'];
} }
$total = 0; $total = 0;
  $cats = 0;
$catsresult = mysql_query("SELECT LEFT( categoryUNSPSC, 1 ) as cat , $catsresult = mysql_query("SELECT LEFT( categoryUNSPSC, 1 ) as cat ,
SUM( value ) as value SUM( value ) as value
FROM `contractnotice` FROM `contractnotice`
WHERE childCN = 0 WHERE childCN = 0
GROUP BY cat ;"); GROUP BY cat ;");
$nodes = Array(); $nodes = Array();
while ($row = mysql_fetch_assoc($catsresult)) { while ($row = mysql_fetch_assoc($catsresult)) {
  $cats++;
  $catColor = $color->hsl2hex(Array($cats/10, .7, .5));
$catName = $unspsc[$row['cat'] . "0000000"] . $row['cat']; $catName = $unspsc[$row['cat'] . "0000000"] . $row['cat'];
if ($row['cat'] == "") $catName = "null"; if ($row['cat'] == "") $catName = "null";
$subnodes = Array(); $subnodes = Array();
$cattwosresult = mysql_query("SELECT LEFT( categoryUNSPSC, 2 ) as cat , $cattwosresult = mysql_query("SELECT LEFT( categoryUNSPSC, 2 ) as cat ,
SUM( value ) as value SUM( value ) as value
FROM `contractnotice` FROM `contractnotice`
WHERE childCN = 0 and LEFT( categoryUNSPSC, 1 ) = '{$row['cat']}' WHERE childCN = 0 and LEFT( categoryUNSPSC, 1 ) = '{$row['cat']}'
GROUP BY cat ;"); GROUP BY cat ;");
while ($tworow = mysql_fetch_assoc($cattwosresult)) { while ($tworow = mysql_fetch_assoc($cattwosresult)) {
$subcatName = $unspsc[$tworow['cat'] . "000000"] . $tworow['cat']; $subcatName = $unspsc[$tworow['cat'] . "000000"] . $tworow['cat'];
if ($tworow['cat'] == "") $subcatName = "null"; if ($tworow['cat'] == "") $subcatName = "null";
$subsubnodes = Array(); $subsubnodes = Array();
$catthreesresult = mysql_query("SELECT LEFT( categoryUNSPSC, 3 ) as cat , $catthreesresult = mysql_query("SELECT LEFT( categoryUNSPSC, 3 ) as cat ,
SUM( value ) as value SUM( value ) as value
FROM `contractnotice` FROM `contractnotice`
WHERE childCN = 0 and LEFT( categoryUNSPSC, 2 ) = '{$tworow['cat']}' WHERE childCN = 0 and LEFT( categoryUNSPSC, 2 ) = '{$tworow['cat']}'
GROUP BY cat ;"); GROUP BY cat ;");
  $subCatColor = $color->hsl2hex(Array($cats/10, rand(1,10)/10, .5));
while ($threerow = mysql_fetch_assoc($catthreesresult)) { while ($threerow = mysql_fetch_assoc($catthreesresult)) {
$subsubcatName = $unspsc[$threerow['cat'] . "00000"] . $threerow['cat']; $subsubcatName = $unspsc[$threerow['cat'] . "00000"] . $threerow['cat'];
if ($threerow['cat'] == "") $subsubcatName = "null"; if ($threerow['cat'] == "") $subsubcatName = "null";
$subsubnodes[] = Array( $subsubnodes[] = Array(
"label" => $subsubcatName, "label" => $subsubcatName,
"amount" => $threerow['value'], "amount" => $threerow['value'],
"color" => "#000000" "color" => "#".$subCatColor
); );
} }
$subnodes[] = Array( $subnodes[] = Array(
"label" => $subcatName, "label" => $subcatName,
"amount" => $tworow['value'], "amount" => $tworow['value'],
"color" => "#000000", "color" => "#".$subCatColor,
"children" => $subsubnodes "children" => $subsubnodes
); );
} }
$nodes[] = Array( $nodes[] = Array(
"label" => $catName, "label" => $catName,
"amount" => $row['value'], "amount" => $row['value'],
"color" => "#000000", "color" => "#".$catColor,
"children" => $subnodes "children" => $subnodes
); );
$total+= $row['value']; $total+= $row['value'];
} }
$data = Array( $data = Array(
"label" => "Australian Federal Government Contract Spending", "label" => "Australian Federal Government Contract Spending",
"amount" => $total, "amount" => $total,
"color" => "#000000", "color" => "#000000",
"children" => $nodes "children" => $nodes
); );
echo "var data =eval('('+'" . json_encode($data) . "'+')');"; echo "var data =eval('('+'" . json_encode($data) . "'+')');";
?> ?>
   
new BubbleTree({ new BubbleTree({
data: data, data: data,
container: '.bubbletree' container: '.bubbletree'
}); });
}); });
</script> </script>
</head> </head>
<body> <body>
<div class="bubbletree-wrapper"> <div class="bubbletree-wrapper">
<div class="bubbletree"></div> <div class="bubbletree"></div>
</div> </div>
</body> </body>
</html> </html>
   
<?php <?php
include_once("./lib/common.inc.php"); include_once("./lib/common.inc.php");
include_header("Months and Years"); include_header("Months and Years");
if ($_REQUEST['month']) { if ($_REQUEST['month']) {
echo "<center><h1>".$_REQUEST['month']."</h1></center>"; echo "<center><h1>".$_REQUEST['month']."</h1></center>";
$monthParts = explode("-",$_REQUEST['month']); $monthParts = explode("-",$_REQUEST['month']);
$query = "SELECT CNID, description, value, agencyName, category, contractStart, supplierName $query = 'SELECT "CNID", "description", "value", "agencyName", "category", "contractStart", "supplierName"
FROM `contractnotice` FROM contractnotice
WHERE childCN = 0 WHERE "childCN" = 0
AND YEAR(contractStart) = {$monthParts[1]} AND extract(year from "contractStart") = :year
AND MONTH(contractStart) = {$monthParts[0]} AND extract(month from "contractStart") = :month
ORDER BY value DESC"; ORDER BY value DESC';
$result = mysql_query($query); $query = $conn->prepare($query);
echo '<img src="graphs/displayMethodCountGraph.php?month=' . stripslashes($supplier) . '">'; $query->bindParam(":month", $monthParts[0]);
echo '<img src="graphs/displayCnCGraph.php?month=' . stripslashes($supplier) . '">';  
  $query->bindParam(":year", $monthParts[1]);
  $query->execute();
  if (!$query) {
  databaseError($conn->errorInfo());
  }
   
  MethodCountGraph($supplier);
  CnCGraph($supplier);
echo "<table> <thead> echo "<table> <thead>
<tr> <tr>
<th>Contract Notice Number</th> <th>Contract Notice Number</th>
<th>Contract Description</th> <th>Contract Description</th>
<th>Total Contract Value</th> <th>Total Contract Value</th>
<th>Agency</th> <th>Agency</th>
<th>Contract Start Date</th> <th>Contract Start Date</th>
<th>Supplier</th> <th>Supplier</th>
</tr> </tr>
</thead>"; </thead>";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { foreach ($query->fetchAll() as $row) {
setlocale(LC_MONETARY, 'en_US'); setlocale(LC_MONETARY, 'en_US');
$value = number_format(doubleval($row['value']) , 2); $value = number_format(doubleval($row['value']) , 2);
echo ("<tr> echo ("<tr>
<td><a href=\"displayContract.php?CNID={$row['CNID']}\">{$row['CNID']}</a></td> <td><a href=\"displayContract.php?CNID={$row['CNID']}\">{$row['CNID']}</a></td>
<td><b>{$row['description']}</b></a></td> <td><b>{$row['description']}</b></a></td>
<td>\$$value</td><td>{$row['agencyName']}</td> <td>\$$value</td><td>{$row['agencyName']}</td>
<td>{$row['contractStart']}</td> <td>{$row['contractStart']}</td>
<td>{$row['supplierName']}</td> <td>{$row['supplierName']}</td>
</tr>"); </tr>");
} }
echo "</table>"; echo "</table>";
mysql_free_result($result); mysql_free_result($result);
} else { } else {
/* /*
split by year split by year
todo: todo:
Year/Month drilldown - largest contracts, agencies, suppliers Year/Month drilldown - largest contracts, agencies, suppliers
count per month count per month
big picture graphs?*/ big picture graphs?*/
echo '<img src="graphs/displayContractStartingGraph.php">'; echo '<img src="graphs/displayContractStartingGraph.php">';
echo '<img src="graphs/displayContractPublishedGraph.php">'; echo '<img src="graphs/displayContractPublishedGraph.php">';
$query = "SELECT YEAR(contractStart), MONTH(contractStart), $query = 'SELECT extract(year from "contractStart"), extract(month from "contractStart"),
SUM(value) as val, count(1) as count FROM `contractnotice` WHERE childCN = 0 GROUP BY MONTH(contractStart), YEAR(contractStart) ORDER BY YEAR(contractStart), MONTH(contractStart) "; SUM(value) as val, count(1) as count FROM contractnotice WHERE "childCN" = 0 GROUP BY extract(year from "contractStart"), extract(month from "contractStart") ORDER BY extract(year from "contractStart"), extract(month from "contractStart") ';
  $query = $conn->prepare($query);
  $query->execute();
  if (!$query) {
  databaseError($conn->errorInfo());
  }
$result = mysql_query($query);  
echo "<table> <thead> echo "<table> <thead>
<tr> <tr>
<th>Month/Year</th> <th>Month/Year</th>
<th>Total Contracts Value</th> <th>Total Contracts Value</th>
<th>Number of Contracts</th> <th>Number of Contracts</th>
</tr> </tr>
</thead>"; </thead>";
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { foreach ($query->fetchAll() as $row) {
setlocale(LC_MONETARY, 'en_US'); setlocale(LC_MONETARY, 'en_US');
$value = number_format(doubleval($row["val"]),2); $value = number_format(doubleval($row["val"]),2);
$month_name = date( 'F', mktime(0, 0, 0, $row[1]) ); $month_name = date( 'F', mktime(0, 0, 0, $row[1]) );
echo ("<tr><td><b><a href=\"?month=$row[1]-$row[0]\">$month_name {$row[0]}</a></b></td><td>\$$value</td><td>({$row['count']} contracts)</td></tr>"); echo ("<tr><td><b><a href=\"?month=$row[1]-$row[0]\">$month_name {$row[0]}</a></b></td><td>\$$value</td><td>({$row['count']} contracts)</td></tr>");
} }
echo "</table>"; echo "</table>";
mysql_free_result($result); mysql_free_result($result);
} }
include_footer(); include_footer();
?> ?>
   
<?php <?php
include('../lib/common.inc.php'); include('lib/common.inc.php');
$year = 2006; $year = 2006;
$ZeroX = 112.5; $ZeroX = 112.5;
$MaxX = 157; $MaxX = 157;
$ZeroY = -9; $ZeroY = -9;
$MaxY = -45; $MaxY = -45;
$XRange = $MaxX - $ZeroX; $XRange = $MaxX - $ZeroX;
$YRange = abs($MaxY) - abs($ZeroY); $YRange = abs($MaxY) - abs($ZeroY);
$xdivs = 250; $xdivs = 250;
$xdivlength = $XRange / $xdivs; $xdivlength = $XRange / $xdivs;
$ydivs = 250; $ydivs = 250;
$ydivheight = $YRange / $ydivs; $ydivheight = $YRange / $ydivs;
$borderLeft = 100; $borderLeft = 100;
$borderTop = 35; $borderTop = 35;
$width = 497; $width = 497;
$height = 457; $height = 457;
//echo "http://dev.openstreetmap.org/~pafciu17/?module=map&bbox=".$ZeroX.",".$ZeroY.",".$MaxX.",".$MaxY."&width=".$width."&height=".$height; //echo "http://dev.openstreetmap.org/~pafciu17/?module=map&bbox=".$ZeroX.",".$ZeroY.",".$MaxX.",".$MaxY."&width=".$width."&height=".$height;
//$handle = ImageCreate ($width, $height) or die ("Cannot Create image"); //$handle = ImageCreate ($width, $height) or die ("Cannot Create image");
$handle = imagecreatefrompng('../images/australia.png'); $handle = imagecreatefrompng('images/australia.png');
$white = imagecolorallocate($handle, 0, 0, 0); $white = imagecolorallocate($handle, 0, 0, 0);
imagecolortransparent($handle, $white); imagecolortransparent($handle, $white);
$query = "SELECT supplierPostcode, sum(value) as value, lat, lon FROM `contractnotice`,`postcodes` where childCN = 0 AND supplierCountry = 'Australia' AND YEAR(contractStart) >= '$year' AND supplierPostcode = postcode GROUP BY supplierPostcode"; $query = 'SELECT "supplierPostcode", sum("value") as value, max(lat) as lat, max(lon) as lon FROM contractnotice inner join postcodes on "supplierPostcode" = postcode::text where "childCN" = 0 AND "supplierCountry" = \'Australia\' GROUP BY "supplierPostcode"';
$result = mysql_query($query); $query = $conn->prepare($query);
  $query->execute();
  if (!$query) {
  databaseError($conn->errorInfo());
  }
   
$left = "FFFF50"; $left = "FFFF50";
$right= "EF0050"; $right= "EF0050";
$leftR = hexdec(substr($left,0,2)); $leftR = hexdec(substr($left,0,2));
$leftG = hexdec(substr($left,2,2)); $leftG = hexdec(substr($left,2,2));
$leftB = hexdec(substr($left,4,2)); $leftB = hexdec(substr($left,4,2));
$rightR = hexdec(substr($right,0,2)); $rightR = hexdec(substr($right,0,2));
$rightG = hexdec(substr($right,2,2)); $rightG = hexdec(substr($right,2,2));
$rightB = hexdec(substr($right,4,2)); $rightB = hexdec(substr($right,4,2));
for($i=0;$i<250;$i++) { for($i=0;$i<250;$i++) {
$colorset[$i] = imagecolorallocatealpha($handle, $leftR + ($i*(($rightR-$leftR)/250)), $leftG + ($i*(($rightG-$leftG)/250)), $leftB + ($i*(($rightB-$leftB)/250)),117 - ($i/250)*40); $colorset[$i] = imagecolorallocatealpha($handle, $leftR + ($i*(($rightR-$leftR)/250)), $leftG + ($i*(($rightG-$leftG)/250)), $leftB + ($i*(($rightB-$leftB)/250)),117 - ($i/250)*40);
} }
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) foreach ($query->fetchAll() as $row) {
{  
$xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength)); $xpage = round((($XRange - ($MaxX - $row['lon'])) / $xdivlength));
if ($row['lat'] < -40.25) $row['lat']-= 0.75; if ($row['lat'] < -40.25) $row['lat']-= 0.75;
$ypage = round(($YRange - (abs($MaxY) - abs($row['lat']))) / $ydivheight); $ypage = round(($YRange - (abs($MaxY) - abs($row['lat']))) / $ydivheight);
@$pagevalues[$xpage][$ypage] += $row['value']; @$pagevalues[$xpage][$ypage] += $row['value'];
//echo $ypage." ".$xpage."<br>"; //echo $ypage." ".$xpage."<br>";
} }
$max = 0; $max = 0;
$min = 0; $min = 0;
for ($i = 0;$i < $xdivs; $i++ ) { for ($i = 0;$i < $xdivs; $i++ ) {
for ($j = 0;$j < $ydivs; $j++ ) { for ($j = 0;$j < $ydivs; $j++ ) {
if (@$pagevalues[$i][$j]) if (@$pagevalues[$i][$j])
{ {
if (@$pagevalues[$i][$j] > $max) $max = @$pagevalues[$i][$j]; if (@$pagevalues[$i][$j] > $max) $max = @$pagevalues[$i][$j];
if ($min == 0 || @$pagevalues[$i][$j] < $min) $min = @$pagevalues[$i][$j]; if ($min == 0 || @$pagevalues[$i][$j] < $min) $min = @$pagevalues[$i][$j];
} else { } else {
@$pagevalues[$i][$j] = ((@$pagevalues[$i][$j] + (@$pagevalues[$i+1][$j] + @$pagevalues[$i][$j+1] @$pagevalues[$i][$j] = ((@$pagevalues[$i][$j] + (@$pagevalues[$i+1][$j] + @$pagevalues[$i][$j+1]
+ @$pagevalues[$i-1][$j] + @$pagevalues[$i][$j-1])/4) /2)-9; + @$pagevalues[$i-1][$j] + @$pagevalues[$i][$j-1])/4) /2)-9;
@$pagevalues[$i-1][$j] = ((@$pagevalues[$i-1][$j] + (@$pagevalues[$i][$j] + @$pagevalues[$i-1][$j+1] @$pagevalues[$i-1][$j] = ((@$pagevalues[$i-1][$j] + (@$pagevalues[$i][$j] + @$pagevalues[$i-1][$j+1]
+ @$pagevalues[$i-2][$j] + @$pagevalues[$i-1][$j-1])/4) /2)-9; + @$pagevalues[$i-2][$j] + @$pagevalues[$i-1][$j-1])/4) /2)-9;
@$pagevalues[$i][$j-1] = ((@$pagevalues[$i][$j-1] + (@$pagevalues[$i+1][$j-1] + @$pagevalues[$i][$j] @$pagevalues[$i][$j-1] = ((@$pagevalues[$i][$j-1] + (@$pagevalues[$i+1][$j-1] + @$pagevalues[$i][$j]
+ @$pagevalues[$i-1][$j-1] + @$pagevalues[$i][$j-2])/4) /2)-9; + @$pagevalues[$i-1][$j-1] + @$pagevalues[$i][$j-2])/4) /2)-9;
} }
} }
} }
//echo $max." ".$min; //echo $max." ".$min;
//echo "<table width='100%'>"; //echo "<table width='100%'>";
for ($i = 0;$i < $xdivs; $i++ ) { for ($i = 0;$i < $xdivs; $i++ ) {
// echo "<tr>"; // echo "<tr>";
for ($j = 0; $j < $ydivs; $j++ ) { for ($j = 0; $j < $ydivs; $j++ ) {
// echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>"); // echo ("<td>". log10(@$pagevalues[$i][$j])/log10($max) ."</td>");
if ((@$p