<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

//========Total Sales Graph

 $month_arr=array(
	"01"=> "Jan" ,
	"02"=> "Feb" ,
	"03"=> "Mar" ,
	"04"=> "Apr" ,
	"05"=> "May" ,
	"06"=> "Jun" ,
	"07"=> "Jul" ,
	"08"=> "Aug" ,
	"09"=> "Sep" ,
	"10"=> "Oct" ,
	"11"=> "Nov" ,
	"12"=> "Dec"
); 

$sales_graph_arr=array();
list($m, $y) = explode('-', date('m-Y'));
for ($i = -12; $i <= 0; $i++) {
  $ts = mktime(0,0,0,$m+$i,1,$y);
  //$n  = date('F', $ts);
  $m1  = date('m', $ts);
  $y1  = date('Y', $ts);
  //$days = date('t', $ts);
//  echo $m1." ";
//  echo $y1."<br>";
  $sales_graph_arr[$m1."-".$y1] = 0;
}
//echo "previous:<br>";
//print_r($sales_graph_arr);
//echo"<br>";
    $sql_sales_graph = "SELECT coalesce(sum(o.order_total_price+o.taxrate),0) as total_amount,
                                lpad(EXTRACT(MONTH FROM order_date),2,'0') as month, 
                                EXTRACT(YEAR FROM order_date) as year  
                        FROM ".$tableprefix."orders o  
                        WHERE date(order_date)>=DATE_SUB(CURDATE(), INTERVAL 1 YEAR) 
                        group by month,year;";
$res_sales_graph=mysql_query($sql_sales_graph) or die(mysql_error());						
$totrow_sales_graph = mysql_num_rows($res_sales_graph);
if ( $totrow_sales_graph > 0 ) {
  while($row_sales_graph = mysql_fetch_array($res_sales_graph)) {
     $arr_key = $row_sales_graph['month']."-".$row_sales_graph['year'] ;
	 $sales_graph_arr[$arr_key] =  number_format($row_sales_graph['total_amount'],2,".",""); 
  }
}
//print_r($sales_graph_arr);
//echo"<br>";


//========Total Sales graph ends

//$img_width=450;
//$img_height=300;		
//$margins=40;

$img_width=365;
$img_height=200;		
$margins=40;
//Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2; 
$img=imagecreate($img_width,$img_height);
$bar_width=20;
$total_bars=count($sales_graph_arr);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);

//Define Colors ----------------imagecolorallocate($img,255, 0, 0)
$bar_color=imagecolorallocate($img,216,216,216);
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,255,255,255);
$border_color=imagecolorallocate($img,249,249,249);
$line_color=imagecolorallocate($img,220,220,220);


//Create the border around the graph
imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);
if ( $totrow_sales_graph > 0 ) {
//Max value is required to adjust the scale
$max_value=max($sales_graph_arr);
$ratio= $graph_height/$max_value;

//Create scale and draw horizontal lines
//	$horizontal_lines=20;
	$horizontal_lines=10;
	$horizontal_gap=$graph_height/$horizontal_lines;

	for($i=1;$i<=$horizontal_lines;$i++){
		$y=$img_height - $margins - $horizontal_gap * $i ;
		imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
		$v=intval($horizontal_gap * $i /$ratio);
		imagestring($img,0,5,$y-5,"$".$v,$bar_color);

	}
 
//Draw the bars here
	for($i=0;$i< $total_bars; $i++){ 
		// Extract key and value pair from the current pointer position
		$str_key = array();
		list($key,$value)=each($sales_graph_arr); 
		$x1= $margins + $gap + $i * ($gap+$bar_width) ;
		$x2= $x1 + $bar_width; 
		$y1=$margins +$graph_height- intval($value * $ratio) ;
		$y2=$img_height-$margins;
		imagestring($img,0,$x1+3,$y1-10,"$".$value,$bar_color);
		$str_key = split("-",$key);
		$month_name = $month_arr[$str_key[0]];
		//echo $month_name."<br>";
		imagestring($img,0,$x1+3,$img_height-30,$month_name,$bar_color);		
		imagestring($img,0,$x1+3,$img_height-20,$str_key[1],$bar_color);
		
                $r = rand(0, 255);
                $g = rand(0, 255);
                $b = rand(0, 255);
                $barNewColor = imagecolorallocate($img,$r,$g,$b);
                
                imagefilledrectangle($img,$x1,$y1,$x2,$y2,$barNewColor);
	}
} else {
   $default_text ="No Sales.";
   $df_x =$img_width/3;
   $df_y =$img_height/2;
   imagestring($img,3,$df_x,$df_y,$default_text,$bar_color);
}	
	header("Content-type:image/png");
	imagepng($img);

?>						
