<?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");
			$month = isset($_GET['selected_month']) ? $_GET['selected_month'] : @date("m");
			$prev_year=$year-1;
			//set_time_limit(1000); //for long queries
			if ($month==1){$month_desc=$lbl_january;}
			if ($month==2){$month_desc=$lbl_february;}
			if ($month==3){$month_desc=$lbl_march;}
			if ($month==4){$month_desc=$lbl_april;}
			if ($month==5){$month_desc=$lbl_may;}
			if ($month==6){$month_desc=$lbl_june;}
			if ($month==7){$month_desc=$lbl_july;}
			if ($month==8){$month_desc=$lbl_august;}
			if ($month==9){$month_desc=$lbl_september;}
			if ($month==10){$month_desc=$lbl_oktober;}
			if ($month==11){$month_desc=$lbl_november;}
			if ($month==12){$month_desc=$lbl_december;}
            ## get reported branch  
            $selected_branch = $_GET['selected_branch'];
?>
<!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_items 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_monthly" 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>
		<?php ################################### END UPDATE STATISTICS ?>		
		 <form name="frmYear" action="reports_items_monthly.php" method="get">
        <div class="align-center" style="padding:10px; margin-bottom:10px;border-radius:4px; border:solid 1px #CCCCCC; background-color:#F4F9FB">
		<span class="pad-left-right-10"><?php echo  $lbl_select_month_and_year;  ?></span>
		<select name="selected_month" id="selected_month" class="addform_drop_1">
				<option value="1" <?php if ($month==1) echo 'selected="selected"';?>><?php echo $lbl_january ?></option>
				<option value="2" <?php if ($month==2) echo 'selected="selected"';?>><?php echo $lbl_february ?></option>
				<option value="3" <?php if ($month==3) echo 'selected="selected"';?>><?php echo $lbl_march ?></option>
				<option value="4" <?php if ($month==4) echo 'selected="selected"';?>><?php echo $lbl_april ?></option>
				<option value="5" <?php if ($month==5) echo 'selected="selected"';?>><?php echo $lbl_may ?></option>
				<option value="6" <?php if ($month==6) echo 'selected="selected"';?>><?php echo $lbl_june ?></option>
				<option value="7" <?php if ($month==7) echo 'selected="selected"';?>><?php echo $lbl_july ?></option>
				<option value="8" <?php if ($month==8) echo 'selected="selected"';?>><?php echo $lbl_august ?></option>
				<option value="9" <?php if ($month==9) echo 'selected="selected"';?>><?php echo $lbl_september ?></option>
				<option value="10" <?php if ($month==10) echo 'selected="selected"';?>><?php echo $lbl_oktober ?></option>
				<option value="11" <?php if ($month==11) echo 'selected="selected"';?>><?php echo $lbl_november ?></option>
				<option value="12" <?php if ($month==12) echo 'selected="selected"';?>><?php echo $lbl_december ?></option>
			</select>
            <select name="selected_year" id="selected_year" class="addform_drop_1">
			<?php
				$sql_y = "SELECT DISTINCT(bi_year) AS that FROM bi_sales_items 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
 $DataSalesItems = new pData;
      	$sql_items = "SELECT bi_item_desc, SUM(sales_amount) AS total_sales FROM bi_sales_items 
                    WHERE bi_year = '$year' 
                    AND bi_month = '$month' 
                    AND sales_amount<>0 
                    ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")." 
                   	GROUP BY 1
                    ORDER BY sales_amount DESC";				
    	$res_sql_items  = mysqli_query($mysqli, $sql_items)or die("Query failed : " . mysqli_error($mysqli));  
    if (mysqli_num_rows($res_sql_items) != 0){
		while ($row = mysqli_fetch_array($res_sql_items))  
     	{
		$DataSalesItems->AddPoint($row["total_sales"], "Serie1"); 
		$DataSalesItems->AddPoint($row["bi_item_desc"], "Serie2");
		}
		//$DataCustomersT->AddAllSeries(); 
		$DataSalesItems->AddSerie('Serie1');
//		$DataCustomersT->SetSerieName("Πλήθος Πελατών","Serie1");
		$DataSalesItems->SetAbsciseLabelSerie("Serie2"); 
//		$DataCustomersT->SetYAxisName($lbl_turnover);  
//		$DataCustomersT->SetYAxisUnit($currency);     		
		// Initialise the graph
		$DataSalesItemsGraph = new pChart(480,350);
		$DataSalesItemsGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8); 
		$DataSalesItemsGraph->drawFilledRoundedRectangle(5,1,350,350,5,244,249,251);  
		//$DataSalesItemsGraph->drawFilledCircle(170,170,170,200,200,200);  
		$DataSalesItemsGraph->setColorPalette(0,224,100,46);
		$DataSalesItemsGraph->setColorPalette(1,231,221,53);
		$DataSalesItemsGraph->setColorPalette(2,129,183,49);
		$DataSalesItemsGraph->setColorPalette(3,57,128,244); //rand(0, 200)
		$DataSalesItemsGraph->setColorPalette(4,150,25,150);
		$DataSalesItemsGraph->setColorPalette(5,201,1,1);
		$DataSalesItemsGraph->setColorPalette(6,250,88,152);
		$DataSalesItemsGraph->setColorPalette(7,52,88,152);
		$DataSalesItemsGraph->setColorPalette(8,214,169,18);
		$DataSalesItemsGraph->setColorPalette(9,109,179,232);
		$DataSalesItemsGraph->setColorPalette(10,234,234,234);
		// Draw the line graph
		//$DataSalesItemsGraph->reportWarnings(); 
if ($graph_mode=='' || $graph_mode=="pie"){	
		 //$DataSalesItemsGraph->drawPieGraph($DataSalesItems->GetData(),$DataSalesItems->GetDataDescription(),175,130,100,PIE_PERCENTAGE_LABEL,TRUE,50,20,5);  
		 $DataSalesItemsGraph->drawBasicPieGraph($DataSalesItems->GetData(),$DataSalesItems->GetDataDescription(),175,200,115,PIE_PERCENTAGE_LABEL,255,255,218); 
		 $DataSalesItemsGraph->drawPieLegend(360,1,$DataSalesItems->GetData(),$DataSalesItems->GetDataDescription(),250,250,250); 
}
		// Finish the graph
		$DataSalesItemsGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",12);
		$DataSalesItemsGraph->drawTitle(0,25, $month_desc.(($selected_branch != '') ? " (".getBranchName($selected_branch).")" : "")." ".$year,50,50,50,350);		
		// Render the graph  
        (($selected_branch != "") ? $image_name_1 = 'sales_items_monthly.png' : $image_name_1 = 'sales_items_monthly_'.$selected_branch.'.png');
		$DataSalesItemsGraph->Render("graphs/".$image_name_1."");	
flush();	
}		
 $DataSalesItemsAll = new pData;
      	$sql_items_all = "SELECT bi_item_desc, 
						SUM(sales_amount) AS sales
						FROM bi_sales_items 
						WHERE bi_year='$year' 
						AND sales_amount<>0 
                        ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")." 
						GROUP BY 1
						ORDER BY sales DESC";				
    	$res_sql_items_all  = mysqli_query($mysqli, $sql_items_all)or die("Query failed : " . mysqli_error($mysqli));  
    	 if (mysqli_num_rows($res_sql_items_all) != 0){
		while ($row = mysqli_fetch_array($res_sql_items_all))  
     	{
		$DataSalesItemsAll->AddPoint($row["sales"], "Serie1"); 
		$DataSalesItemsAll->AddPoint($row["bi_item_desc"], "Serie2");
		}
		//$DataCustomersT->AddAllSeries(); 
		$DataSalesItemsAll->AddSerie('Serie1');
//		$DataCustomersT->SetSerieName("Πλήθος Πελατών","Serie1");
		$DataSalesItemsAll->SetAbsciseLabelSerie("Serie2"); 
//		$DataCustomersT->SetYAxisName($lbl_turnover);  
//		$DataCustomersT->SetYAxisUnit($currency);     		
		// Initialise the graph
		$DataSalesItemsGraphAll = new pChart(480,350);
		$DataSalesItemsGraphAll->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8); 
		$DataSalesItemsGraphAll->drawFilledRoundedRectangle(5,1,350,350,5,244,249,251);  
		//$DataSalesItemsGraph->drawFilledCircle(170,170,170,200,200,200);  
		$DataSalesItemsGraphAll->setColorPalette(10,224,100,46);
		$DataSalesItemsGraphAll->setColorPalette(9,231,221,53);
		$DataSalesItemsGraphAll->setColorPalette(8,129,183,49);
		$DataSalesItemsGraphAll->setColorPalette(7,57,128,244); //rand(0, 200)
		$DataSalesItemsGraphAll->setColorPalette(6,150,25,150);
		$DataSalesItemsGraphAll->setColorPalette(5,201,1,1);
		$DataSalesItemsGraphAll->setColorPalette(4,250,88,152);
		$DataSalesItemsGraphAll->setColorPalette(3,52,88,152);
		$DataSalesItemsGraphAll->setColorPalette(2,234,234,234);
		$DataSalesItemsGraphAll->setColorPalette(1,220,120,145);		
		$DataSalesItemsGraphAll->setColorPalette(0,109,179,232);
		// Draw the line graph
		//$DataSalesItemsGraph->reportWarnings(); 
if ($graph_mode=='' || $graph_mode=="pie"){	
		 //$DataSalesItemsGraph->drawPieGraph($DataSalesItems->GetData(),$DataSalesItems->GetDataDescription(),175,130,100,PIE_PERCENTAGE_LABEL,TRUE,50,20,5);  
		 $DataSalesItemsGraphAll->drawBasicPieGraph($DataSalesItemsAll->GetData(),$DataSalesItemsAll->GetDataDescription(),175,200,115,PIE_PERCENTAGE_LABEL,255,255,218); 
		 $DataSalesItemsGraphAll->drawPieLegend(360,1,$DataSalesItemsAll->GetData(),$DataSalesItemsAll->GetDataDescription(),250,250,250); 
}
		// Finish the graph
		$DataSalesItemsGraphAll->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",12);
		$DataSalesItemsGraphAll->drawTitle(0,25, $lbl_year_totals.(($selected_branch != '') ? " (".getBranchName($selected_branch).")" : "")." ".$year,50,50,50,350);		
		// Render the graph  
        (($selected_branch != "") ? $image_name_2 = 'sales_items_annual.png' : $image_name_2 = 'sales_items_annual_'.$selected_branch.'.png');
		$DataSalesItemsGraphAll->Render("graphs/".$image_name_2."");	
}
flush();	
?>	
		<div style="width:99%; padding-bottom:10px;float:left">
		<span style="padding-left:5px;"><font size="6"><?php echo (($selected_branch != '') ? $lbl_sales_items_monthly.' ('.getBranchName($selected_branch).')' : $lbl_sales_items_monthly) ?></font></span></div>
	<?php if (mysqli_num_rows($res_sql_items) != 0){ ?>
	<div style="border:thin #CCCCCC solid; margin-right:10px; border-radius:3px; padding:5px; width:480px; background-color:#F2F2E6; float:left">
    <img src="graphs/<?php echo $image_name_1 ?>" alt="<?php echo $lbl_items_sales_items_perc ?>" title="<?php echo $lbl_items_sales_items_perc ?>"/><br />
	<?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 id, bi_item_desc, SUM(sales_amount) AS tziros,
	  			ROUND((SUM(sales_amount)/ 
				(SELECT SUM(sales_amount) FROM bi_sales_items WHERE bi_month='$month' AND bi_year='$year') * 100), 0) AS rate
				FROM bi_sales_items
				WHERE bi_month='$month' AND bi_year='$year' AND sales_amount<>0
                ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")." 
				GROUP BY 2 DESC
			";         
	        
    ##  *** 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 = "cmi_";    /* 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 = "tziros";
      $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);
     $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 = false;
    $dropdown_paging = false;
	$rows_numeration =false;
	$numeration_sign ="#";
    $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $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("10"=>"10", "20"=>"20", "50"=>"50");
    $default_page_size = 50;
    $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
    $dgrid->SetPagingSettings($bottom_paging, $top_paging, $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"=>"475px");
     $dgrid->SetViewModeTableProperties($vm_table_properties);  
     $vm_colimns = array(  
	    "bi_item_desc"  =>array("header"=>$lbl_grid_cat_group, "type"=>"label", "header_align"=>"center", "align"=>"left", "tooltip"=>"true", "summarize"=>"false",),	
        "tziros"  =>array("header"=>$lbl_turnover, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>"."),
		 "rate"  =>array("header"=>$lbl_share, "type"=>"percent", "header_align"=>"center","tooltip"=>"true", "summarize"=>"false")		
     );
     $dgrid->SetColumnsInViewMode($vm_colimns);
	 $dgrid->SetSummarizeNumberFormat("2", ",", ".");
	 $dgrid->DisplayLoadingImage();
    
    ## +---------------------------------------------------------------------------+
    ## | 8. Bind the DataGrid:                                                     | 
    ## +---------------------------------------------------------------------------+
    ##  *** bind the DataGrid and draw it on the screen
      $dgrid->Bind();        
      ob_end_flush();
    ##
    ################################################################################   
	?>

	</div>
	<?php }else{?>
	<div style="border:thin #CCCCCC solid; margin-right:10px; border-radius:3px; padding:5px; width:480px; background-color:#F2F2E6; float:left">
		<?php if ($_SESSION['lang']=="EN") {?>
			<img src="../img/no_data.gif" alt="<?php echo $lbl_items_sales_items_perc ?>" title="<?php echo $lbl_items_sales_items_perc ?>"/><br />
		<?php }elseif($_SESSION['lang']=="GR") {?>	
			<img src="../img/no_data_gr.gif" alt="<?php echo $lbl_items_sales_items_perc ?>" title="<?php echo $lbl_items_sales_items_perc ?>"/><br />
		<?php } ?>
	</div>
	<?php } ?>	
	<?php if (mysqli_num_rows($res_sql_items_all) != 0){ ?>
	<div style="border:thin #CCCCCC solid; margin-right:10px; border-radius:3px; padding:5px; width:480px; background-color:#F2F2E6; float:left">
    <img src="graphs/<?php echo $image_name_2 ?>" alt="<?php echo $lbl_items_sales_items_perc ?>" title="<?php echo $lbl_items_sales_items_perc ?>"/><br />
<?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 id, bi_item_desc, SUM(sales_amount) AS tziros,
	  			ROUND((SUM(sales_amount)/ 
				(SELECT SUM(sales_amount) FROM bi_sales_items WHERE bi_year='$year') * 100), 0) AS rate
				FROM bi_sales_items
				WHERE bi_year='$year' AND sales_amount<>0                        
                ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")." 
				GROUP BY 2
			";         
	        
    ##  *** 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 = "cci_";    /* 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 = "tziros";
      $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);
     $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 = false;
    $dropdown_paging = false;
	$rows_numeration =false;
	$numeration_sign ="#";
    $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign, $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("10"=>"10", "20"=>"20", "50"=>"50");
    $default_page_size = 50;
    $paging_arrows = array("first"=>"|&lt;&lt;", "previous"=>"&lt;&lt;", "next"=>"&gt;&gt;", "last"=>"&gt;&gt;|");
    $dgrid->SetPagingSettings($bottom_paging, $top_paging, $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"=>"475px");
     $dgrid->SetViewModeTableProperties($vm_table_properties);  
     $vm_colimns = array(  
	    "bi_item_desc"  =>array("header"=>$lbl_grid_cat_group, "type"=>"label", "header_align"=>"center", "align"=>"left", "tooltip"=>"true", "summarize"=>"false",),	
        "tziros"  =>array("header"=>$lbl_turnover, "type"=>"money", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"0", "dec_separator"=>",", "thousands_separator"=>"."),
		 "rate"  =>array("header"=>$lbl_share, "type"=>"percent", "header_align"=>"center","tooltip"=>"true", "summarize"=>"false")		
     );
     $dgrid->SetColumnsInViewMode($vm_colimns);
	 $dgrid->SetSummarizeNumberFormat("2", ",", ".");
	 $dgrid->DisplayLoadingImage();
    
    ## +---------------------------------------------------------------------------+
    ## | 8. Bind the DataGrid:                                                     | 
    ## +---------------------------------------------------------------------------+
    ##  *** bind the DataGrid and draw it on the screen
      $dgrid->Bind();        
      ob_end_flush();
    ##
    ################################################################################   
	}else{
	?>
		<div style="border:thin #CCCCCC solid; margin-right:10px; border-radius:3px; padding:5px; width:480px; background-color:#F2F2E6; float:left">
		<?php if ($_SESSION['lang']=="EN") {?>
			<img src="../img/no_data.gif" alt="<?php echo $lbl_items_sales_items_perc ?>" title="<?php echo $lbl_items_sales_items_perc ?>"/><br />
		<?php }elseif($_SESSION['lang']=="GR") {?>	
			<img src="../img/no_data_gr.gif" alt="<?php echo $lbl_items_sales_items_perc ?>" title="<?php echo $lbl_items_sales_items_perc ?>"/><br />
		<?php } ?>
	</div>
	<?php
	}
	?>
	</div>	
	</fieldset>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>
