<?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_customers;
			$page_title =$lbl_business_analytics; //Assign Page Title
			$array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}
			$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'];
			$current_month = date("m");
			$current_day = date("d");
			$previous_day = date("d")-1;
			$year = isset($_GET['selected_year']) ? $_GET['selected_year'] : @date("Y");
			$prev_year=$year-1;			
			if ($current_month==1){$month_desc=$lbl_january;}
			if ($current_month==2){$month_desc=$lbl_february;}
			if ($current_month==3){$month_desc=$lbl_march;}
			if ($current_month==4){$month_desc=$lbl_april;}
			if ($current_month==5){$month_desc=$lbl_may;}
			if ($current_month==6){$month_desc=$lbl_june;}
			if ($current_month==7){$month_desc=$lbl_july;}
			if ($current_month==8){$month_desc=$lbl_august;}
			if ($current_month==9){$month_desc=$lbl_september;}
			if ($current_month==10){$month_desc=$lbl_oktober;}
			if ($current_month==11){$month_desc=$lbl_november;}
			if ($current_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>
		<div>
		<fieldset>
		<legend><?php echo $mode ?></legend>
				 <form name="frmYear" action="reports_customers_analysis.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_analysis_for_the_year;  ?></span>
            <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>
		 <div style="width:99%; padding-bottom:10px;float:left">
		<span style="padding-left:5px;"><font size="6"><?php echo $lbl_customers_analysis ?>
		<?php
 $DataCustomers = new pData;
      	$new_cust = "SELECT COUNT(CUS.id) AS plithos, GRO.name AS category FROM cls_customers CUS
					INNER JOIN cls_customer_groups GRO ON CUS.group_id = GRO.id
                    ".(($selected_branch != "") ? " WHERE CUS.branch_id ='".$selected_branch."'" : "")." 
					GROUP BY CUS.group_id ORDER BY 1 DESC";
                    				
    	$res_new_cust  = mysqli_query($mysqli, $new_cust)or die("Query failed : " . mysqli_error($mysqli));  
		if (mysqli_num_rows($res_new_cust) != 0){
		while ($row = mysqli_fetch_array($res_new_cust)) 
     	//$plithos=$row["plithos"];
		//$category=$row["category"];
		{
			$DataCustomers->AddPoint($row["plithos"], "Serie1"); 
			$DataCustomers->AddPoint($row["category"], "Serie2"); 
			$DataCustomers->AddPoint($row["category"].": ".$row["plithos"], "Serie3"); 
		}	 	
		//$DataCustomers->AddAllSeries(); 
		$DataCustomers->AddSerie('Serie1');
//		$DataCustomers->SetSerieName("Πλήθος Πελατών","Serie1");
		$DataCustomers->SetAbsciseLabelSerie("Serie2"); 
//		$DataCustomers->SetYAxisName($lbl_turnover);  
//		$DataCustomers->SetYAxisUnit($currency);     		
		// Initialise the graph
		$CustomersGraph = new pChart(480,250);
		$CustomersGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8); 
		$CustomersGraph->drawFilledRoundedRectangle(5,1,350,250,5,244,249,251);  
		$CustomersGraph->setColorPalette(0,224,100,46);
		$CustomersGraph->setColorPalette(1,231,221,53);
		$CustomersGraph->setColorPalette(2,129,183,49);
		$CustomersGraph->setColorPalette(3,57,128,244); //rand(0, 200)
		$CustomersGraph->setColorPalette(4,150,25,150);
		$CustomersGraph->setColorPalette(5,201,1,1);
		$CustomersGraph->setColorPalette(6,250,88,152);
		$CustomersGraph->setColorPalette(7,52,88,152);
		$CustomersGraph->setColorPalette(8,214,169,18);
		$CustomersGraph->setColorPalette(9,109,179,232);
		$CustomersGraph->setColorPalette(10,234,234,234);
		// Draw the line graph
		//$CustomersGraph->reportWarnings(); 
if ($graph_mode=='' || $graph_mode=="3dpie"){	
		 $CustomersGraph->drawPieGraph($DataCustomers->GetData(),$DataCustomers->GetDataDescription(),175,130,100,PIE_PERCENTAGE_LABEL,TRUE,50,20,5);  
		 $CustomersGraph->drawPieLegend(360,1,$DataCustomers->GetData(),$DataCustomers->GetDataDescription(),250,250,250); 
}
		// Finish the graph
		$CustomersGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",10);
		$CustomersGraph->drawTitle(0,25,$lbl_customers_synthesis.(($selected_branch != '') ? " (".getBranchName($selected_branch).")" : "")." ".$lbl_regardless_year,50,50,50,350);		
		// Render the graph  
        (($selected_branch != "") ? $image_name = 'customers.png' : $image_name = 'customers_'.$selected_branch.'.png');
		$CustomersGraph->Render("graphs/".$image_name."");	
	}
flush();	
?>
<?php ###################################### SECOND ###################################
 $DataCustomersT = new pData;
      	$new_custT = "SELECT `name` AS groupaki, ROUND(SUM(c.amount),0) as tziros
					FROM cls_transactions c
					INNER JOIN cls_customer_groups b ON c.cust_category_id = b.id
					WHERE c.transaction_type<>'Εξαργύρωση' AND YEAR(created_on)='$year'
                    ".(($selected_branch != "") ? " AND c.branch_id ='".$selected_branch."'" : "")." 
					GROUP BY 1 DESC
					";				
    	$res_new_custT  = mysqli_query($mysqli, $new_custT)or die("Query failed : " . mysqli_error($mysqli));  
		if (mysqli_num_rows($res_new_custT) != 0){
		while ($row = mysqli_fetch_array($res_new_custT)) 
     	//$tziros=$row["tziros"];
		{
			$DataCustomersT->AddPoint($row["tziros"], "Serie1"); 
			$DataCustomersT->AddPoint($row["groupaki"], "Serie2"); 
		}	 	
		//$DataCustomersT->AddAllSeries(); 
		$DataCustomersT->AddSerie('Serie1');
//		$DataCustomersT->SetSerieName("Πλήθος Πελατών","Serie1");
		$DataCustomersT->SetAbsciseLabelSerie("Serie2"); 
//		$DataCustomersT->SetYAxisName($lbl_turnover);  
//		$DataCustomersT->SetYAxisUnit($currency);     		
		// Initialise the graph
		$CustomersTGraph = new pChart(480,250);
		$CustomersTGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",8); 
		$CustomersTGraph->drawFilledRoundedRectangle(5,1,350,250,5,244,249,251);  
		$CustomersTGraph->setColorPalette(10,224,100,46);
		$CustomersTGraph->setColorPalette(9,231,221,53);
		$CustomersTGraph->setColorPalette(8,129,183,49);
		$CustomersTGraph->setColorPalette(7,57,128,244); //rand(0, 200)
		$CustomersTGraph->setColorPalette(6,150,25,150);
		$CustomersTGraph->setColorPalette(5,201,1,1);
		$CustomersTGraph->setColorPalette(4,250,88,152);
		$CustomersTGraph->setColorPalette(3,52,88,152);
		$CustomersTGraph->setColorPalette(2,234,234,234);
		$CustomersTGraph->setColorPalette(1,220,120,145);		
		$CustomersTGraph->setColorPalette(0,109,179,232);

		// Draw the line graph
		//$CustomersTGraph->reportWarnings(); 
if ($graph_mode=='' || $graph_mode=="3dpie"){	
		 $CustomersTGraph->drawPieGraph($DataCustomersT->GetData(),$DataCustomersT->GetDataDescription(),175,130,100,PIE_PERCENTAGE_LABEL,TRUE,50,20,5);  
		 $CustomersTGraph->drawPieLegend(360,1,$DataCustomersT->GetData(),$DataCustomersT->GetDataDescription(),250,250,250); 
}
		// Finish the graph
		$CustomersTGraph->setFontProperties("../scripts/pchart/Fonts/tahoma.ttf",10);
		$CustomersTGraph->drawTitle(0,25,$lbl_customer_groups_turnover.(($selected_branch != '') ? " (".getBranchName($selected_branch).")" : "")." ".$year,50,50,50,350);		
		// Render the graph  
        (($selected_branch != "") ? $image_name_2 = 'customers_turnover.png' : $image_name_2 = 'customers_turnover_'.$selected_branch.'.png');
		$CustomersTGraph->Render("graphs/".$image_name_2."");	
}
flush();		
?>
		</font></span></div>
		<div style="width:99%; padding-left:5px; float:left; border-radius:4px;">
<?php if (mysqli_num_rows($res_new_cust) != 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 ?>" alt="<?php echo $lbl_customers_synthesis?>" title="<?php echo $lbl_customers_synthesis?>"/><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 COUNT(a.id) AS plithos, `name` AS category,
			ROUND(((COUNT(a.id) / ( SELECT COUNT(id) FROM cls_customers)) * 100 ),0) AS rate
			FROM cls_customers a
			INNER JOIN cls_customer_groups b ON a.group_id = b.id
            ".(($selected_branch != "") ? " WHERE a.branch_id ='".$selected_branch."'" : "")." 
			GROUP BY a.group_id
			";         
	        
    ##  *** 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 = "cca_";    /* 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 = "plithos";
      $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;
    $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(  
	    "category"  =>array("header"=>$lbl_grid_cat_group, "type"=>"label", "header_align"=>"center", "align"=>"left", "tooltip"=>"true", "summarize"=>"false",),	
        "plithos"  =>array("header"=>$lbl_plithos, "type"=>"label", "header_align"=>"center","tooltip"=>"true", "summarize"=>"true"),
		 "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();
    ##
    ################################################################################   
	?>	
	<?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_customers_synthesis ?>" title="<?php echo $lbl_customers_synthesis ?>"/><br />
		<?php }elseif($_SESSION['lang']=="GR") {?>	
			<img src="../img/no_data_gr.gif" alt="<?php echo $lbl_customers_synthesis ?>" title="<?php echo $lbl_customers_synthesis ?>"/><br />
		<?php } ?>
	<?php } ?>	
	</div>	
	<?php if (mysqli_num_rows($res_new_custT) != 0){?>
	<div style="border:thin #CCCCCC solid; border-radius:3px; padding:5px; width:480px; background-color:#F2F2E6; float:left; margin-left:0px">
			<img src="graphs/<?php echo $image_name_2 ?>" alt="<?php echo $lbl_customers_turnover?>" title="<?php echo $$lbl_customers_turnover?>"/><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()); }  
         
      $sql = "SELECT `name` AS group_id, ROUND(SUM(c.amount),0) AS tziros,
			ROUND(((SUM(c.amount)/ 
			(SELECT SUM(b.amount) 
                FROM cls_transactions b WHERE b.transaction_type<>'Εξαργύρωση' 
                AND YEAR(b.created_on)='$year'
                AND ISNULL(is_reversed)
                AND ISNULL(reversal)
                ".(($selected_branch != "") ? " AND b.branch_id ='".$selected_branch."'" : "")."
            )) * 100), 0) as rate
			FROM cls_transactions c
			INNER JOIN cls_customer_groups b ON c.cust_category_id=b.id
			WHERE c.transaction_type<>'Εξαργύρωση' 
            AND YEAR(created_on)='$year'
            AND ISNULL(is_reversed)
            AND ISNULL(reversal)
            ".(($selected_branch != "") ? " AND c.branch_id ='".$selected_branch."'" : "")."
			GROUP BY 1";         
	        
    ##  *** 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 = "cct_";    /* 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;
    $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(  
	    "group_id"  =>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();
    ##
    ################################################################################   
	?>

	<?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_customers_turnover ?>" title="<?php echo $lbl_customers_turnover ?>"/><br />
		<?php }elseif($_SESSION['lang']=="GR") {?>	
			<img src="../img/no_data_gr.gif" alt="<?php echo $lbl_customers_turnover ?>" title="<?php echo $lbl_customers_turnover ?>"/><br />
		<?php } ?>
	<?php } ?>	
	</div>	
<div style="float:left; margin-top:10px; width:100%">
<h1><?php echo $lbl_discount_policy_analysis ." ".$year.(($selected_branch != '') ? " (".getBranchName($selected_branch).")" : "") ?></h1>
<?php
      	$sql_disc_anal = "SELECT id, `name` AS groupaki, redeem_limit, multiplier FROM cls_customer_groups";				
    	$res_disc_anal  = mysqli_query($mysqli, $sql_disc_anal)or die("Query failed : " . mysqli_error($mysqli));  
		echo "<table align='left' cellpadding='1' cellspacing='1' border='0' class='default_dg_table'><tr>
			<td class='manual_table_td' align='center'><strong>$lbl_grid_category</strong></td>
			<td class='manual_table_td' align='center'><strong>$lbl_grid_redeem_limit</strong></td>
			<td class='manual_table_td' align='center'><strong>$lbl_grid_currency_multiplier</strong></td>
			<td class='manual_table_td' align='center'><strong>$lbl_grid_turnover</strong></td>
			<td class='manual_table_td' align='center'><strong>$lbl_discount_amount</strong></td>
			<td class='manual_table_td' align='center'><strong>$lbl_discount_rate_on_sales</strong></td></tr>";
		while ($row = mysqli_fetch_array($res_disc_anal)) 
		{ 
		$categ=$row["id"];
			echo "<tr><td class='manual_table_td_d'>".$row["groupaki"]."</td><td class='manual_table_td_d'>".$row["redeem_limit"]."</td><td class='manual_table_td_d'>".$row["multiplier"]."</td>";
				$sql_for_each = "SELECT 
                                ROUND(SUM(amount),0) AS tziros 
                                FROM cls_transactions 
                                WHERE transaction_type<>'Εξαργύρωση' 
                                AND cust_category_id='$categ' 
                                AND YEAR(created_on)='$year'
                                AND ISNULL(is_reversed)
                                AND ISNULL(reversal)
                                ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
                                ";
				$res_for_each  = mysqli_query($mysqli, $sql_for_each)or die("Query failed : " . mysqli_error($mysqli));
				$line = mysqli_fetch_array($res_for_each);
			echo "<td class='manual_table_td_d' align='right'>$currency".number_format($line["tziros"],0,'.','.')."</td>"; 
				$sql_for_each_disc = "SELECT  
                                    ROUND(SUM(amount),0) AS disc 
                                    FROM cls_transactions 
                                    WHERE transaction_type='Εξαργύρωση' 
                                    AND cust_category_id='$categ' 
                                    AND YEAR(created_on)='$year'
                                    AND ISNULL(is_reversed)
                                    AND ISNULL(reversal)
                                    ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
                                    ";
				$res_for_each_disc  = mysqli_query($mysqli, $sql_for_each_disc)or die("Query failed : " . mysqli_error($mysqli));
				$line_disc = mysqli_fetch_array($res_for_each_disc);
			echo "<td class='manual_table_td_d' align='right'>$currency".number_format($line_disc["disc"],0,'.','.')."</td>"; 	
			$sql_for_each_rate ="SELECT 
                                IFNULL(ROUND(((SELECT SUM(b.amount) 
                                FROM cls_transactions b 
                                WHERE b.transaction_type='Εξαργύρωση' 
                                AND b.cust_category_id='$categ' AND YEAR(created_on)='$year'
                                AND ISNULL(is_reversed)
                                AND ISNULL(reversal))/
								    (SELECT SUM(b.amount) 
                                    FROM cls_transactions b 
                                    WHERE b.transaction_type<>'Εξαργύρωση'
                                    AND b.cust_category_id='$categ' 
                                    AND YEAR(created_on)='$year'
                                    AND ISNULL(is_reversed)
                                    AND ISNULL(reversal)
                                    ".(($selected_branch != "") ? " AND branch_id ='".$selected_branch."'" : "")."
                                    )*100),2),0)
			                         AS rate";
				$res_for_each_rate = mysqli_query($mysqli, $sql_for_each_rate)or die("Query failed : " . mysqli_error($mysqli));
				$line_rate = mysqli_fetch_array($res_for_each_rate);			
			echo "<td class='manual_table_td_d' align='right'>".$line_rate["rate"]."%</td></tr>";
		}	
		echo "</table>";
	
?>
</div>	
</fieldset>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>

