<?
session_start();
header("Content-type: image/png"); 
include_once("../../includes/dentalplus.inc.php");
require_once("../../Connection/dentalplus.php");
require_once('../../drawing_functions.php');

if ($_GET["grey"] == "Y")
{
	$im = imagecreatefromgif ("icon7_grey.gif");
	$arrow_colour = imagecolorallocate ($im, 181,181,181);
}
else
{
	$im = imagecreatefromgif ("icon7_coloured.gif");
	$arrow_colour = imagecolorallocate($im, 206, 170, 0);
}

if (!$_SESSION["uda"]["total"])
{
        $sql = sprintf("select * from nhs_setup where uda_type = 's' and uda_id = '%s'",
                       $_SESSION["system_vars"]["staffid"]);
        $nhs_setup = queryfetch($sql);
        
        $_SESSION["uda"]["total"] = $nhs_setup["uda_count"];
}

// used count
$date_from = "01/".date("m/Y");
$date_to = date("d/m/Y");
$sql = sprintf("select * from view_treatment_overview where 
completed_date >= '%s0000' 
and completed_date <= '%s2359' 
and staff_id = '%s'
and status = '5'",
date2db($date_from),
date2db($date_to),
$_SESSION["system_vars"]["staffid"]);
$uda_data = queryfetchall($sql);
//	//echo "<pre>"; print_r($uda_data); //echo "</pre>";

if ($uda_data)
{
	foreach ($uda_data as $data)
	{
		$uda_used += $data["uda_weight"];
	}	
}
//echo "uda_used: $uda_used<br>";

$target = $_SESSION["uda"]["total"]/12;
//echo "target: $target<br>";
// work out the percentage of the month already gone
$pmonth = ceil((100/21)*date("j"));
//echo "pmonth: $pmonth<br>";
// use the percentage of the month to find the difference from the target
$dtarget = ceil($target * ($pmonth/100));
//echo "dtarget: $dtarget<br>";
// work out the difference between the target and the udas completed
$tdiff = $dtarget - $uda_used;
//echo "tdiff: $tdiff<br>";
// percentage difference
$pdiff = round((100/$dtarget) * $tdiff, 0);
//echo "pdiff: $pdiff<br>";


if ($uda_used < $dtarget)
	$angle = 90-((90*$pdiff)/100);
else
	$angle = 90+((90*$pdiff)/100);

//$angle = (90/$dtarget) * (100-$pdiff);
//echo "angle: $angle<br>";

$y = round(16 * sin(deg2rad($angle)),0);
$x = round(16 * cos(deg2rad($angle)),0);

if ($uda_used < $dtarget)
	$x = $x - ($x*2);
//echo "x: $x<br>y: $y<br>";

$x = 28+$x;
$y = 26-$y;
//echo "x: $x<br>y: $y<br>";

//echo "<hr>";

function imageCreateTransparent($x, $y) { 
   $imageOut = imagecreatetruecolor($x, $y);
   $colourBlack = imagecolorallocate($imageOut, 0, 0, 0);
   imagecolortransparent($imageOut, $colourBlack);
   return $imageOut;
}

$target = round($_SESSION["uda"]["total"]/12, 0);

imagelinethick($im, $x, $y, 27, 25, $arrow_colour, 2);

imagepng($im);
?>