<?php session_start();
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
			include ('../scripts/classes.php'); if ($_SESSION['lang']=="EN"){include ('../lang/en.php');}elseif ($_SESSION['lang']=="GR"){include ('../lang/gr.php');}else {include ('../lang/en.php');}		
			include("../scripts/pchart/pChart/pData.class");
 			include("../scripts/pchart/pChart/pChart.class");	
			$mode = $lbl_business_analysis_area.": ".$lbl_sales;
			$page_title = $lbl_business_analytics; //Assign Page Title
			$array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}
			$year = isset($_GET['selected_year']) ? $_GET['selected_year'] : @date("Y");
			$prev_year = $year - 1;
			$graph_mode = $_GET['graph_mode'];
	 	if ($_GET['splot']=='toggle') {if(isset($_SESSION['plot']) && $_SESSION['plot'] == $lbl_show_labels){$_SESSION['plot'] = $lbl_hide_labels;}
			else{$_SESSION['plot'] = $lbl_show_labels;}}
			$plot=$_SESSION['plot'];
            ## get reported branch  
            $selected_branch = $_GET['selected_branch'];
            $selected_category = isset($_GET['selected_category']) ? (int)$_GET['selected_category'] : '';
            if ($selected_category == '') {
                //initialize view
                $array_categories = Core::GetAllReportCategories($year);
                $default_category = $array_categories[0][0]['bi_item'];
                $selected_category = $default_category;
            }
            $objCore = Core::GetReportCategoryName($selected_category, $year);
            $selected_category_name = $objCore['bi_item_desc'];
            
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="../img/favicon.png" />
<link href="../cms1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script>top.document.title=self.document.title;</script>
 <script type="text/javascript">
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'none'){
          e.style.display = 'block';
	   }else{
          e.style.display = 'none';
	   }
    }
</script>
		<div>
		<?php // echo $_SESSION['plot'] ; echo $year;  echo $graph_mode //debug ?> 
		<fieldset>
		<legend><?php echo  $mode ?></legend>
		 <div class="completed">
		 <ul><li><?php echo $lbl_statistics_were_updated; ?>
		 <div id="frmUpStat" style="display:none; padding-top:10px;" >
		 <form name="frmUpStat" action="reports_update.php" method="get" style="padding-top:10px;" >
         <?php echo $lbl_select_month_and_year; ?>&nbsp;
			<select name="month_to_update" id="month_to_update" class="addform_drop_1">
				<option value="1"><?php echo $lbl_january ?></option>
				<option value="2"><?php echo $lbl_february ?></option>
				<option value="3"><?php echo $lbl_march ?></option>
				<option value="4"><?php echo $lbl_april ?></option>
				<option value="5"><?php echo $lbl_may ?></option>
				<option value="6"><?php echo $lbl_june ?></option>
				<option value="7"><?php echo $lbl_july ?></option>
				<option value="8"><?php echo $lbl_august ?></option>
				<option value="9"><?php echo $lbl_september ?></option>
				<option value="10"><?php echo $lbl_oktober ?></option>
				<option value="11"><?php echo $lbl_november ?></option>
				<option value="12"><?php echo $lbl_december ?></option>
			</select>
            <select name="year_to_update" id="year_to_update" class="addform_drop_1">
				<?php
				$sql_y = "SELECT DISTINCT(bi_year) AS that FROM bi_sales ORDER BY that DESC";   
				$result_y = mysqli_query($mysqli, $sql_y) or die("Query failed : " . mysqli_error($mysqli));
				if (mysqli_num_rows($result_y) == 0){
					echo $error001;
					}else{	
						while ($line = mysqli_fetch_array($result_y, MYSQLI_ASSOC)) {
						 $result_y_array[] = $line['that'];
							echo "<option ".(($_GET['selected_year'] == $line['that']) ? "selected" : "")." value='".$line['that']."'>".$line['that']."</option>";
					}
				} 
				?>		          
			</select>
			<input type="hidden" value="items_annual" name="rpt" />
            <input type="submit" name="btnUpdate" value="<?php echo $lbl_update_statistics; ?>" class="button"/>  
			<input type="button" name="btnHide" value="<?php echo $lbl_hide ?>" class="button" onclick="toggle_visibility('frmUpStat');"/> 
		 </form>
		 </div>
		</li></ul>
        </div>	
		 <form name="frmYear" action="reports_items_annual.php?splot=<?php echo $plot ?>" method="get">
        <div class="like-a-message align-center">
        <!-- item filter -->
        <span class="pad-left-right-10"><?php echo  _SELECT_REWARD_CATEGORY;  ?></span>
        <select name="selected_category" id="selected_category" class="addform_drop_1">
            <?php 
                $array_categories = Core::GetAllReportCategories($year); 
          		if($array_categories[1] > 0){//1
                    for($i = 0; $i < $array_categories[1]; $i++){//2
                   	    echo "<option ".(($selected_category == $array_categories[0][$i]['bi_item']) ? "selected" : "")." value='".$array_categories[0][$i]['bi_item']."'>".$array_categories[0][$i]['bi_item_desc']."</option>";
                    }//end 2
                }//end 1
            ?>
        </select>
        <!-- end item filter -->
		<span class="pad-left-right-10"><?php echo  $lbl_analysis_for_the_year;  ?></span>
		<input type="hidden" name="graph_mode" value="<?php echo $graph_mode ?>" />
            <select name="selected_year" id="selected_year" class="addform_drop_1">
			<?php
				$sql_y = "SELECT DISTINCT(bi_year) AS that FROM bi_sales ORDER BY that DESC";   
				$result_y = mysqli_query($mysqli, $sql_y) or die("Query failed : " . mysqli_error($mysqli));
				if (mysqli_num_rows($result_y) == 0){
					echo $error001;
					}else{	
						while ($line = mysqli_fetch_array($result_y, MYSQLI_ASSOC)) {
							echo "<option ".(($_GET['selected_year'] == $line['that']) ? "selected" : "")." value='".$line['that']."'>".$line['that']."</option>";
					}
				}   			          
			?>
			</select>
            <!-- yeap, new selector for branches-->
            <span class="pad-left-right-10"><?php echo _BRANCH_SELECTION;  ?></span>
            <select name="selected_branch" id="selected_branch" class="addform_drop_1">
			<?php
				$sql_d = "SELECT id AS branch_code, branch_name FROM cls_branches ORDER BY branch_name ASC";   
				$result_d = mysqli_query($mysqli, $sql_d) or die("Query failed : " . mysqli_error($mysqli));
				if (mysqli_num_rows($result_d) == 0){
					echo $error002;
					}else{	
                        echo "<option ".(($_GET['selected_branch'] == '') ? "selected" : "")." value=''>-- "._ALL_BRANCHES." --</option>";
						while ($line_2 = mysqli_fetch_array($result_d, MYSQLI_ASSOC)) {
							echo "<option ".(($_GET['selected_branch'] == $line_2['branch_code']) ? "selected" : "")." value='".$line_2['branch_code']."'>".$line_2['branch_name']."</option>";
					}
				}   			          
			?>
			</select>
            <input type="submit" name="btnSubmit" value="<?php echo $lbl_view; ?>" class="button"/>            
        </div>
        </form>
<?php
 $DataSales = new pData;
      	$Requete = "SELECT bi_year AS years, 
					SUM(IF(bi_month = '1', 1, 0) * ROUND(sales_amount,0)) AS jan,
					SUM(IF(bi_month = '2', 1, 0) * ROUND(sales_amount,0)) AS feb,
					SUM(IF(bi_month = '3', 1, 0) * ROUND(sales_amount,0)) AS mar,
					SUM(IF(bi_month= '4', 1, 0) * ROUND(sales_amount,0)) AS apr,
					SUM(IF(bi_month = '5', 1, 0) * ROUND(sales_amount,0)) AS may,
					SUM(IF(bi_month = '6', 1, 0) * ROUND(sales_amount,0)) AS jun,
					SUM(IF(bi_month = '7', 1, 0) * ROUND(sales_amount,0)) AS jul,
					SUM(IF(bi_month = '8', 1, 0) * ROUND(sales_amount,0)) AS aug,
					SUM(IF(bi_month = '9', 1, 0) * ROUND(sales_amount,0)) AS sep,
					SUM(IF(bi_month = '10', 1, 0) * ROUND(sales_amount,0)) AS oct,
					SUM(IF(bi_month = '11', 1, 0) * ROUND(sales_amount,0)) AS nov,
					SUM(IF(bi_month = '12', 1, 0) * ROUND(sales_amount,0)) AS 'dec',
					SUM(ROUND(sales_amount,0)) AS year_totals
					FROM bi_sales_items
					WHERE bi_year='$prev_year'
                    AND bi_item = '".$selected_category."'
                    ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
					GROUP BY years";				
    	$result  = mysqli_query($mysqli, $Requete)or die("Query failed : " . mysqli_error($mysqli));  
    	while ($row = mysqli_fetch_array($result))  
     	{
        	$sales_jan_p=$row["jan"]; $sales_feb_p=$row["feb"]; $sales_mar_p=$row["mar"]; $sales_apr_p=$row["apr"]; $sales_may_p=$row["may"]; $sales_jun_p=$row["jun"]; 	 		
            $sales_jul_p=$row["jul"]; $sales_aug_p=$row["aug"]; $sales_sep_p=$row["sep"]; $sales_oct_p=$row["oct"]; $sales_nov_p=$row["nov"]; $sales_dec_p=$row["dec"];
        	$DataSales->AddPoint($row["jan"], "Serie1"); $DataSales->AddPoint($row["feb"], "Serie1"); $DataSales->AddPoint($row["mar"], "Serie1");
        	$DataSales->AddPoint($row["apr"], "Serie1"); $DataSales->AddPoint($row["may"], "Serie1"); $DataSales->AddPoint($row["jun"], "Serie1");
        	$DataSales->AddPoint($row["jul"], "Serie1"); $DataSales->AddPoint($row["aug"], "Serie1"); $DataSales->AddPoint($row["sep"], "Serie1");
        	$DataSales->AddPoint($row["oct"], "Serie1"); $DataSales->AddPoint($row["nov"], "Serie1"); $DataSales->AddPoint($row["dec"], "Serie1");
		}
		 
      	$Requete2 = "SELECT bi_year AS years, 
					SUM(IF(bi_month = '1', 1, 0) * ROUND(sales_amount,0)) AS jan,
					SUM(IF(bi_month = '2', 1, 0) * ROUND(sales_amount,0)) AS feb,
					SUM(IF(bi_month = '3', 1, 0) * ROUND(sales_amount,0)) AS mar,
					SUM(IF(bi_month= '4', 1, 0) * ROUND(sales_amount,0)) AS apr,
					SUM(IF(bi_month = '5', 1, 0) * ROUND(sales_amount,0)) AS may,
					SUM(IF(bi_month = '6', 1, 0) * ROUND(sales_amount,0)) AS jun,
					SUM(IF(bi_month = '7', 1, 0) * ROUND(sales_amount,0)) AS jul,
					SUM(IF(bi_month = '8', 1, 0) * ROUND(sales_amount,0)) AS aug,
					SUM(IF(bi_month = '9', 1, 0) * ROUND(sales_amount,0)) AS sep,
					SUM(IF(bi_month = '10', 1, 0) * ROUND(sales_amount,0)) AS oct,
					SUM(IF(bi_month = '11', 1, 0) * ROUND(sales_amount,0)) AS nov,
					SUM(IF(bi_month = '12', 1, 0) * ROUND(sales_amount,0)) AS 'dec',
					SUM(ROUND(sales_amount,0)) AS year_totals
					FROM bi_sales_items
					WHERE bi_year = '$year'
                    AND bi_item = '".$selected_category."'
                    ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
					GROUP BY years";				
    	$result2  = mysqli_query($mysqli, $Requete2)or die("Query failed : " . mysqli_error($mysqli));  
    	while ($row = mysqli_fetch_array($result2))  
     	{
		$sales_jan_c=$row["jan"]; $sales_feb_c=$row["feb"]; $sales_mar_c=$row["mar"]; $sales_apr_c=$row["apr"]; $sales_may_c=$row["may"]; $sales_jun_c=$row["jun"];  			 		
        $sales_jul_c=$row["jul"]; $sales_aug_c=$row["aug"]; $sales_sep_c=$row["sep"]; $sales_oct_c=$row["oct"]; $sales_nov_c=$row["nov"]; $sales_dec_c=$row["dec"];
		if ($year==date('Y')){
			if ($row["jan"]> 0 || date("m")>=1) $DataSales->AddPoint($row["jan"], "Serie2"); 
			if ($row["feb"]> 0 || date("m")>=2) $DataSales->AddPoint($row["feb"], "Serie2");
			if ($row["mar"]> 0 || date("m")>=3) $DataSales->AddPoint($row["mar"], "Serie2");
			if ($row["apr"]> 0 || date("m")>=4) $DataSales->AddPoint($row["apr"], "Serie2");
			if ($row["may"]> 0 || date("m")>=5) $DataSales->AddPoint($row["may"], "Serie2");
			if ($row["jun"]> 0 || date("m")>=6) $DataSales->AddPoint($row["jun"], "Serie2");
			if ($row["jul"]> 0 || date("m")>=7) $DataSales->AddPoint($row["jul"], "Serie2");
			if ($row["aug"]> 0 || date("m")>=8) $DataSales->AddPoint($row["aug"], "Serie2");
			if ($row["sep"]> 0 || date("m")>=9) $DataSales->AddPoint($row["sep"], "Serie2");
			if ($row["oct"]> 0 || date("m")>=10)$DataSales->AddPoint($row["oct"], "Serie2");
			if ($row["nov"]> 0 || date("m")>=11)$DataSales->AddPoint($row["nov"], "Serie2");
			if ($row["dec"]> 0 || date("m")>=12)$DataSales->AddPoint($row["dec"], "Serie2");
		}else{
			$DataSales->AddPoint($row["jan"], "Serie2"); 
			$DataSales->AddPoint($row["feb"], "Serie2");
			$DataSales->AddPoint($row["mar"], "Serie2");
			$DataSales->AddPoint($row["apr"], "Serie2");
			$DataSales->AddPoint($row["may"], "Serie2");
			$DataSales->AddPoint($row["jun"], "Serie2");
			$DataSales->AddPoint($row["jul"], "Serie2");
			$DataSales->AddPoint($row["aug"], "Serie2");
			$DataSales->AddPoint($row["sep"], "Serie2");
			$DataSales->AddPoint($row["oct"], "Serie2");
			$DataSales->AddPoint($row["nov"], "Serie2");
			$DataSales->AddPoint($row["dec"], "Serie2");		
			}
		}		 	
		$DataSales->AddPoint(array($lbl_january, $lbl_february, $lbl_march, $lbl_april, $lbl_may, $lbl_june, $lbl_july, $lbl_august, $lbl_september, $lbl_oktober,$lbl_november, $lbl_december),"Serie3");
		//$DataSales->AddAllSeries(); 
		$DataSales->AddSerie('Serie1');
		$DataSales->AddSerie('Serie2');
		$DataSales->SetSerieName($lbl_sales." ".$prev_year,"Serie1");
		$DataSales->SetSerieName($lbl_sales." ".$year,"Serie2");
		$DataSales->SetAbsciseLabelSerie("Serie3"); 
		$DataSales->SetYAxisName($lbl_sales);  
		$DataSales->SetYAxisUnit($currency);     		
		// Initialise the graph
		$SalesGraph = new pChart(1000,245);
		$SalesGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8);
		$SalesGraph->setGraphArea(90,35,800,200); // $SalesGraph->setGraphArea(40,30,680,200);
		$SalesGraph->drawGraphArea(252,252,252,TRUE);
		$SalesGraph->drawFilledRoundedRectangle(0,0,850,240,5,242,242,230);  
		$SalesGraph->drawRoundedRectangle(0,5,850,225,5,242,242,230); 
		$SalesGraph->setColorPalette(0,192,57,43); //$SalesGraph->setColorPalette(1,63,223,63)
		$SalesGraph->setColorPalette(1,52, 152, 219);// $SalesGraph->setColorPalette(1,155,181,189);
		// Draw the line graph
		//$SalesGraph->reportWarnings(); 
if ($graph_mode=='' || $graph_mode==filledlinegraph){	
		$SalesGraph->drawScale($DataSales->GetData(),$DataSales->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
		$SalesGraph->drawGrid(4,TRUE,230,230,230,70);
		$SalesGraph->drawPlotGraph($DataSales->GetData(),$DataSales->GetDataDescription(),3,2,244,249,251);
		$SalesGraph->drawFilledLineGraph($DataSales->GetData(),$DataSales->GetDataDescription(),50,TRUE); 
}
if ($graph_mode==linegraph){	
		$SalesGraph->drawScale($DataSales->GetData(),$DataSales->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
		$SalesGraph->drawGrid(4,TRUE,230,230,230,70);
		$SalesGraph->drawPlotGraph($DataSales->GetData(),$DataSales->GetDataDescription(),3,2,244,249,251);
		$SalesGraph->drawLineGraph($DataSales->GetData(),$DataSales->GetDataDescription());
}
if ($graph_mode==cubiccurve){	
		$SalesGraph->drawScale($DataSales->GetData(),$DataSales->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
		$SalesGraph->drawGrid(4,TRUE,230,230,230,70);
		$SalesGraph->drawCubicCurve($DataSales->GetData(),$DataSales->GetDataDescription());
}
if ($graph_mode==bargraph){	
		$SalesGraph->drawScale($DataSales->GetData(),$DataSales->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,0,TRUE,1,FALSE);
		$SalesGraph->drawGrid(4,TRUE,230,230,230,70);
		$SalesGraph->drawBarGraph($DataSales->GetData(),$DataSales->GetDataDescription(),TRUE,50);	
}
if (!isset($_SESSION['plot']) || $plot== $lbl_show_labels){
		//$SalesGraph->drawFilledCubicCurve($DataSales->GetData(),$DataSales->GetDataDescription(),.1,50);  
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_january,$sales_jan_p, 223,232,234);    
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_february,$sales_feb_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_march,$sales_mar_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_april,$sales_apr_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_may,$sales_may_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_june,$sales_jun_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_july,$sales_jul_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_august,$sales_aug_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_september,$sales_sep_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_oktober,$sales_oct_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_november,$sales_nov_p, 223,232,234);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie1",$lbl_december,$sales_dec_p, 223,232,234);
	
        if ($year == date('Y')){	
        if ($sales_jan_c >0 || date("m")>=1) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_january,$sales_jan_c, 254,254,177); 
        if ($sales_feb_c >0 || date("m")>=2) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_february,$sales_feb_c, 254,254,177);
        if ($sales_mar_c >0 || date("m")>=3) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_march,$sales_mar_c, 254,254,177);
        if ($sales_apr_c >0 || date("m")>=4) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_april,$sales_apr_c, 254,254,177);
        if ($sales_may_c >0 || date("m")>=5) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_may,$sales_may_c, 254,254,177);
        if ($sales_jun_c >0 || date("m")>=6) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_june,$sales_jun_c, 254,254,177);
        if ($sales_jul_c >0 || date("m")>=7) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_july,$sales_jul_c, 254,254,177);
        if ($sales_aug_c >0 || date("m")>=8) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_august,$sales_aug_c, 254,254,177);
        if ($sales_sep_c >0 || date("m")>=9) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_september,$sales_sep_c, 254,254,177);
        if ($sales_oct_c >0 || date("m")>=10) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_oktober,$sales_oct_c, 254,254,177);
        if ($sales_nov_c >0 || date("m")>=11) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_november,$sales_nov_c, 254,254,177);
        if ($sales_dec_c >0 || date("m")>=12) $SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_december,$sales_dec_c, 254,254,177);
	}else {
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_january,$sales_jan_c, 254,254,177); 
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_february,$sales_feb_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_march,$sales_mar_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_april,$sales_apr_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_may,$sales_may_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_june,$sales_jun_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_july,$sales_jul_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_august,$sales_aug_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_september,$sales_sep_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_oktober,$sales_oct_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_november,$sales_nov_c, 254,254,177);
		$SalesGraph->setLabel($DataSales->GetData(),$DataSales->GetDataDescription(),"Serie2",$lbl_december,$sales_dec_c, 254,254,177);
	}
}
		// Finish the graph
		$SalesGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8);
		$SalesGraph->drawLegend(860,0,$DataSales->GetDataDescription(),242,242,230);
		$SalesGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",10);
		$SalesGraph->drawTitle(60,22,$lbl_compare_sales_table.': '.$selected_category_name.''.(($selected_branch != '') ? " (".getBranchName($selected_branch).")" : "")." ".$prev_year."-".$year,50,50,50,800);
		
		// Render the graph  
        (($selected_branch != "") ? $image_name = 'annual_sales.png' : $image_name = 'annual_sales_'.$selected_branch.'.png');
		$SalesGraph->Render("graphs/".$image_name."");		
		flush();
 //end if isset sales graph
?>
		<div style="width:99%; padding-bottom:10px;float:left">
		<span style="padding-left:5px;"><font size="6"><?php echo (($selected_branch != '') ? $lbl_annual_sales.': '.$selected_category_name.' ('.getBranchName($selected_branch).')' : $lbl_annual_sales.': '.$selected_category_name) ?></font></span>
		</div>
		<div style="width:99%; padding-left:5px; float:left; border-radius:3px;">
		<img src="graphs/<?php echo $image_name ?>" alt="<?php echo $lbl_compare_sales_table?>" title="<?php echo $lbl_compare_sales_table?>"/>
		</div>			
		<div style="float:left; padding-top:10px">
	<?php
	$current_year = $year;
	$previous_year = ($current_year)-1;
	if ($current_year == min($result_y_array)){$stop_arrow=1;}
	$current_month = date("m");
	$current_day = date("d");
	######################### select min max years
	$sql_mm = "SELECT MIN(bi_year) AS mini, MAX(bi_year) AS maxi FROM bi_sales_items
			WHERE bi_year < '$current_year'
            AND bi_item = '".$selected_category."'
            ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
            ";   
	$result_mm = mysqli_query($mysqli, $sql_mm) or die("Query failed : " . mysqli_error($mysqli));
	if (mysqli_num_rows($result_mm) == 0){
		echo $error001;
		}else{	
			while ($line = mysqli_fetch_array($result_mm, MYSQLI_ASSOC)) {
			$minY=$line['mini'];
			$maxY=$line['maxi'];
		}
	}   
	######################### cummulative months comparison
    $sql_cum_c = "SELECT SUM(amount) AS cum_totals FROM cls_transactions
    			WHERE created_on 
    			BETWEEN CONCAT('$current_year','-',01,'-',01,' ','00:00:00') 
    			AND CONCAT('$current_year','-','$current_month','-','$current_day',' ','23:59:59')
    			AND transaction_type<>'Εξαργύρωση' 
                AND ISNULL(is_reversed)
                AND ISNULL(reversal)
                AND category_id = '".$selected_category."'
                ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
                "; //
    $result_cum_c = mysqli_query($mysqli, $sql_cum_c) or die("Query failed : " . mysqli_error($mysqli));
    if (mysqli_num_rows($result_cum_c) == 0){echo $error001; 
    }else{$line = mysqli_fetch_array($result_cum_c, MYSQLI_ASSOC); $cum_c=$line['cum_totals'];} 
	################previous
	$sql_cum_p = "SELECT SUM(amount) AS cum_totals_p FROM cls_transactions
				WHERE created_on 
				BETWEEN CONCAT('$previous_year','-',01,'-',01,' ','00:00:00') 
				AND CONCAT('$previous_year','-','$current_month','-','$current_day',' ','23:59:59')
				AND transaction_type<>'Εξαργύρωση'
                AND ISNULL(is_reversed)
                AND ISNULL(reversal)
                AND category_id = '".$selected_category."'
                ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
                "; //
	$result_cum_p = mysqli_query($mysqli, $sql_cum_p) or die("Query failed : " . mysqli_error($mysqli));
	if (mysqli_num_rows($result_cum_p) == 0){echo $error001;
	}else{$line = mysqli_fetch_array($result_cum_p, MYSQLI_ASSOC); $cum_p=$line['cum_totals_p'];} 
	//echo  $cum_p;
	###############################################	
	#calculate variation rate
	//print_r (min($result_y_array));
	//echo  $cum_p." - ". $cum_c;
	if ($cum_p>0){
		$variation = number_format(((($cum_c - $cum_p)/$cum_p)*100),0, ',', '.');
	}elseif ($cum_p<=0){
		$variation=0;
	}
	//echo $variation;
	###############################################
	 $SalesSmall = new pData;
				$sql_annual="SELECT SUM(sales_amount) AS totals, bi_year FROM bi_sales_items
							WHERE bi_year < '$current_year'
                            AND bi_item = '".$selected_category."'
                            ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
                            GROUP BY bi_year ASC";		
				$result_annual = mysqli_query($mysqli, $sql_annual) or die("Query failed : " . mysqli_error($mysqli));
				if (mysqli_num_rows($result_annual) == 0 or mysqli_num_rows($result_annual) == 1){
							$stop=1;
							//echo "<div style='padding-left:10px'>$lbl_no_records</div>";
						}else{
							while ($line=mysqli_fetch_assoc($result_annual)) {	;
								$SalesSmall->AddPoint($line["totals"], "Serie5");
								$SalesSmall->AddPoint($line["bi_year"], "Serie6");
							}
						
				//$DataSales->AddAllSeries(); 
				$SalesSmall->AddSerie("Serie5");
				//$SalesSmall->AddSerie("Serie6");
				$SalesSmall->SetSerieName($lbl_sales,"Serie5"); 	
				$SalesSmall->SetAbsciseLabelSerie("Serie6"); 		
				// Initialise the graph
				$AnnualSaLes = new pChart(205,74);
				$AnnualSaLes->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8);
				//$AnnualSaLes->drawFilledRoundedRectangle(2,2,98,28,2,230,230,230);
				$AnnualSaLes->setGraphArea(5,5,200,70);
				$AnnualSaLes->drawGraphArea(242,242,230);
				$AnnualSaLes->drawScale($SalesSmall->GetData(),$SalesSmall->GetDataDescription(),SCALE_NORMAL,150,150,150,false,0,2,TRUE);
				//$AnnualSaLes->drawScale($SalesSmall->GetData(),$SalesSmall->GetDataDescription(),SCALE_NORMAL,220,220,220,TRUE,0,2); 
				$AnnualSaLes->drawGrid(4,FALSE,230,230,230,70);
				$AnnualSaLes->setColorPalette(0,255,0,0 ); //$SalesGraph->setColorPalette(1,63,223,63)
				$AnnualSaLes->setColorPalette(1,63,223,63);// $SalesGraph->setColorPalette(1,155,181,189);
				// Draw the line graph
				$AnnualSaLes->drawLineGraph($SalesSmall->GetData(),$SalesSmall->GetDataDescription());
				// Finish the graph
				$AnnualSaLes->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8);
				//$AnnualSaLes->drawLegend(210,5,$SalesSmall->GetDataDescription(),242,242,230);
				$AnnualSaLes->drawTitle(10,19,$lbl_across_time." ".$minY."-".$maxY,102,102,102,200);
                 (($selected_branch != "") ? $small_image_name = 'annual_sales_items.png' : $small_image_name = 'annual_sales_items_'.$selected_branch.'.png');
				$AnnualSaLes->Render("graphs/".$small_image_name."");
				}
 ?>		
 		<div style="width:99%; padding-left:5px; margin-bottom:10px; float:left">
		&raquo; <?php echo $lbl_setup ?> 
		<a href="reports_items_annual.php?graph_mode=<?php echo $graph_mode ?>&selected_year=<?php echo $year ?>&splot=toggle&selected_branch=<?php echo $selected_branch ?>">
		<?php if ($plot==$lbl_hide_labels){echo $lbl_show_labels;}else{echo $lbl_hide_labels;}?></a><br />
		&raquo; <?php echo $lbl_view_options ?> 
		<a href="reports_items_annual.php?graph_mode=filledlinegraph&selected_year=<?php echo $year ?>&selected_branch=<?php echo $selected_branch ?>"><?php echo $lbl_filled_line_graph ?></a> |
		<a href="reports_items_annual.php?graph_mode=linegraph&selected_year=<?php echo $year ?>&selected_branch=<?php echo $selected_branch ?>"><?php echo $lbl_simple_line_graph ?></a> |
		<a href="reports_items_annual.php?graph_mode=cubiccurve&selected_year=<?php echo $year ?>&selected_branch=<?php echo $selected_branch ?>"><?php echo $lbl_cubic_curve_graph ?></a> | 
		<a href="reports_items_annual.php?graph_mode=bargraph&selected_year=<?php echo $year ?>&selected_branch=<?php echo $selected_branch ?>"><?php echo $lbl_bar_graph ?></a> <br />
		</div>
 		<div style="width:99%; padding-left:5px; float:left; border-radius:3px;">
		<?php if ($stop!=1){?>
		<div style="float:left"><img src="graphs/<?php echo $small_image_name?>" alt="<?php echo $lbl_year_totals?>" title="<?php echo $lbl_year_totals?>"/></div>
		<div style="float:left;background-color:#F2F2E6; padding:5px; width:200px; height:56px; font:tahoma; font-size:11px; border-radius:3px; margin-left:1px; margin-top:3px; margin-right:5px; border:1px #CCCCCC solid; color:#666666">
			<img src="../img/info.gif" /><?php echo $lbl_diachronic_until." ".$prev_year ?> 
		</div>
		<?php } ?>
		<div>
		<?php if ($stop_arrow!=1){?>
		<div style="float:left; background-color:#F2F2E6; padding:5px; border-radius:3px; margin-top:3px; height:56px; border:1px #CCCCCC solid; color:#666666">
				<?php 
				if ($variation<0){
					echo "<img src='../img/down _arrow.gif' alt='' /><font size='50'>".$variation."%</font>";
				}elseif ($variation>0){
					echo "<img src='../img/up_arrow.gif' alt='' /><font size='50'>".$variation."%</font>";
				}elseif ($variation==0){
					echo "<font size='50'>".$variation."%</font>";
				}
				?>
			</div>
			  <div style="float:left;background-color:#F2F2E6; padding:5px; width:200px; height:56px; font:tahoma; font-size:11px; border-radius:3px; margin-left:5px; margin-top:3px; border:1px #CCCCCC solid; margin-right:5px;color:#666666">
			<img src="../img/info.gif" /> <?php echo $lbl_variation_from_last_year ?>
			</div>
			<?php } ?>
			</div>
		</div>
		
		<div style="width:850px; float:left; padding-left:5px;" class="align-left">
		<?php    

    ################################################################################
    ## +---------------------------------------------------------------------------+
    ## | 1. Creating & Calling:                                                    | 
    ## +---------------------------------------------------------------------------+
      define ("DATAGRID_DIR", "../datagrid/");                     /* Ex.: "datagrid/" */ 
      define ("PEAR_DIR", "../datagrid/pear/");                    /* Ex.: "datagrid/pear/" */
      require_once(DATAGRID_DIR.'datagrid.class.php');
      require_once(PEAR_DIR.'PEAR.php');
      require_once(PEAR_DIR.'DB.php');
      include_once('../datagrid/config.inc.php');
      ob_start();
      $db_conn = DB::factory('mysql');  /* don't forget to change on appropriate db type */
      $result_conn = $db_conn->connect(DB::parseDSN('mysql://'.$DB_USER.':'.$DB_PASS.'@'.$DB_HOST.'/'.$DB_NAME));
      if(DB::isError($result_conn)){ die($result_conn->getDebugInfo()); }  
	  
      if(DB::isError($result_conn)){ die($result_conn->getDebugInfo()); }  
    ################################################################################   
      $sql = "SELECT bi_year AS years, 
				SUM(IF(bi_month = '1', 1, 0) * ROUND(sales_amount,0)) AS jan,
				SUM(IF(bi_month = '2', 1, 0) * ROUND(sales_amount,0)) AS feb,
				SUM(IF(bi_month = '3', 1, 0) * ROUND(sales_amount,0)) AS mar,
				SUM(IF(bi_month= '4', 1, 0) * ROUND(sales_amount,0)) AS apr,
				SUM(IF(bi_month = '5', 1, 0) * ROUND(sales_amount,0)) AS may,
				SUM(IF(bi_month = '6', 1, 0) * ROUND(sales_amount,0)) AS jun,
				SUM(IF(bi_month = '7', 1, 0) * ROUND(sales_amount,0)) AS jul,
				SUM(IF(bi_month = '8', 1, 0) * ROUND(sales_amount,0)) AS aug,
				SUM(IF(bi_month = '9', 1, 0) * ROUND(sales_amount,0)) AS sep,
				SUM(IF(bi_month = '10', 1, 0) * ROUND(sales_amount,0)) AS oct,
				SUM(IF(bi_month = '11', 1, 0) * ROUND(sales_amount,0)) AS nov,
				SUM(IF(bi_month = '12', 1, 0) * ROUND(sales_amount,0)) AS 'dec',
				SUM(ROUND(sales_amount,0)) AS year_totals
				FROM bi_sales_items
                WHERE bi_item = '".$selected_category."'
                ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
				GROUP BY years";         
	        
    ##  *** set needed options and create a new class instance 
      $debug_mode = false;        /* display SQL statements while processing */    
      $messaging = true;          /* display system messages on a screen */ 
      $unique_prefix = "op1_";    /* prevent overlays - must be started with a letter */
      $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
      $dgrid->SetCssClass("default");
      $dgrid->DisplayLoadingImage("true");
      $dgrid->AllowPaging(false, "", "");
    ##  *** set data source with needed options
      $default_order_field = "years";
      $default_order_type = "DESC";
      $dgrid->DataSource($db_conn, $sql, $default_order_field, $default_order_type);
    ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
     $modes = array();
     $dgrid->SetModes($modes);
	 $layouts = array("view"=>"0", "edit"=>"1", "details"=>"1", "filter"=>"1"); 
    $dgrid->SetLayouts($layouts);
    ##  *** set DataGrid caption
     //$dg_caption = $lbl_new_customers_for_the_year." ".$year;
    // $dgrid->SetCaption($dg_caption);
     $dgrid->AllowPrinting(true);
     $dgrid->AllowSorting(false);
     $dgrid->AllowHighlighting(true);
	//webnail language handling
	if ($_SESSION['lang']=="EN"){$dg_language = "en";}elseif ($_SESSION['lang']=="GR"){$dg_language = "gk";}else{$dg_language = "en";}
	$dgrid->SetInterfaceLang($dg_language);  
     
	$paging_option = true;
    $dropdown_paging = true;
    $dgrid->AllowPaging($paging_option, false, '', $dropdown_paging);
    ##  *** set paging settings
    $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
   // $top_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
    $pages_array = array("5"=>"5", "10"=>"10", "50"=>"50");
    $default_page_size = 5;
    $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
    $dgrid->SetPagingSettings($bottom_paging, false, $pages_array, $default_page_size, $paging_arrows);
		 ## +---------------------------------------------------------------------------+
	## | 3. Printing & Exporting Settings:                                         | 
	## +---------------------------------------------------------------------------+
	##  *** set printing option: true(default) or false 
	  $printing_option = true;
	  $dgrid->AllowPrinting($printing_option);
	  $exporting_option = true;
		$exporting_directory = "../exports/";               
		$export_all = true;
		$dgrid->AllowExporting($exporting_option, $exporting_directory, $export_all);
		$exporting_types = array("excel"=>"true", "pdf"=>"false", "xml"=>"false");
		$dgrid->AllowExportingTypes($exporting_types);
		
    ## +---------------------------------------------------------------------------+
    ## | 6. View Mode Settings:                                                    | 
    ## +---------------------------------------------------------------------------+
    ##  *** set view mode table properties
     $vm_table_properties = array("width"=>"850px");
     $dgrid->SetViewModeTableProperties($vm_table_properties);  
     $vm_colimns = array(  
	 	"years"=>array("header"=>$lbl_year, "type"=>"label", "header_align"=>"center",  "align"=>"right", "width"=>"100px", "wrap"=>"nowrap", "visible"=>"true", ),
        "jan"  =>array("header"=>$lbl_Jan, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "feb"  =>array("header"=>$lbl_Feb, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "mar"  =>array("header"=>$lbl_Mar, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "apr"  =>array("header"=>$lbl_Apr, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "may"  =>array("header"=>$lbl_May, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "jun"  =>array("header"=>$lbl_Jun, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "jul"  =>array("header"=>$lbl_Jul, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "aug"  =>array("header"=>$lbl_Aug, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "sep"  =>array("header"=>$lbl_Sep, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "oct"  =>array("header"=>$lbl_Oct, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "nov"  =>array("header"=>$lbl_Nov, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),
        "dec"  =>array("header"=>$lbl_Dec, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),	
        "year_totals"  =>array("header"=>$lbl_year_totals, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>".", "align"=>"right", "width"=>"", "wrap"=>"nowrap"),				
     );
     $dgrid->SetColumnsInViewMode($vm_colimns);
	 $dgrid->SetSummarizeNumberFormat("0", ",", ".");
	 $dgrid->DisplayLoadingImage();
    
    ## +---------------------------------------------------------------------------+
    ## | 8. Bind the DataGrid:                                                     | 
    ## +---------------------------------------------------------------------------+
    ##  *** bind the DataGrid and draw it on the screen
      $dgrid->Bind();        
      //ob_end_flush();
	?>
		</div>
	</div>	
	</fieldset>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>

