<?php
session_start();
include_once( '/var/www/html/e5crm/config.php');
mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);
mysql_query("set names utf8");
include_once( '/var/www/html/e5crm/include/ECM/open_flash_chart/ofc-library/open-flash-chart.php' );
include_once('/var/www/html/e5crm/modules/EcmCharts/helperChart.php');
$ye=$_SESSION['EcmCharts_2_year'];
$sh=$_SESSION['EcmCharts_2_show'];
$s=getSalesByCategory("",$ye."-01-01",$ye."-12-31","",$_SESSION['EcmCharts_2_account']);
$s_b=getSalesByCategory("",($ye-1)."-01-01",($ye-1)."-12-31","",$_SESSION['EcmCharts_2_account']);
$s_bb=getSalesByCategory("",($ye-1)."-01-01",($ye-1)."-".date("m")."-".date("d"),"",$_SESSION['EcmCharts_2_account']);
$temp=0;

$w=mysql_query("select id,name from ecmproductcategories where deleted='0' and name!=''");
//$s=sum($_SESSION['EcmCharts_2_year'],$r['id']);
while($r=mysql_fetch_array($w)){
	//if(in_array("sales",$sh)){
		$v_sales_b=$s_b[$r['id']]['sales'];
		if($v_sales_b>$temp)$temp=$v_sales_b;
		$sum_sales_b+=$v_sales_b;
		
		$v_sales=$s[$r['id']]['sales'];
		if($v_sales>$temp)$temp=$v_sales;
		$sum_sales+=$v_sales;
		
		$v_sales_bb=$s_bb[$r['id']]['sales'];
		if($v_sales_bb>$temp)$temp=$v_sales_bb;
		$sum_sales_bb+=$v_sales_bb;
	//}
	//if(in_array("quantity",$sh)){
		$v_quantity_b=$s_b[$r['id']]['quantity'];
		if($v_quantity_b>$temp)$temp=$v_b_quantity;
		$sum_quantity_b+=$v_quantity_b;
		
		$v_quantity=$s[$r['id']]['quantity'];
		if($v_quantity>$temp)$temp=$v_quantity;
		$sum_quantity+=$v_quantity;

		$v_quantity_bb=$s_bb[$r['id']]['quantity'];
		if($v_quantity_bb>$temp)$temp=$v_quantity_bb;
		$sum_quantity_bb+=$v_quantity_bb;
	//}
	//if(in_array("margin",$sh)){
		if($s_b[$r['id']]['sales']>0)$v_margin_b=100*($s_b[$r['id']]['purchase'])/$s_b[$r['id']]['sales'];
		else $v_margin_b=0;
		if($v_margin_b>$temp)$temp=$v_margin_b;
		$sum_margin_b+=$v_margin_b;
		
		if($s[$r['id']]['sales']>0)$v_margin=100*($s[$r['id']]['purchase'])/$s[$r['id']]['sales'];
		else $v_margin=0;
		if($v_margin>$temp_margin)$temp_margin=$v_margin;
		$sum_margin+=$v_margin;

		if($s_bb[$r['id']]['sales']>0)$v_margin_bb=100*($s_bb[$r['id']]['purchase'])/$s_bb[$r['id']]['sales'];
		else $v_margin_bb=0;
		if($v_margin_bb>$temp)$temp=$v_margin_bb;
		$sum_margin_bb+=$v_margin_bb;
	//}
	//if(in_array("pln_margin",$sh)){
		$v_pln_margin_b=$s_b[$r['id']]['sales']-$s_b[$r['id']]['purchase'];
		if($v_pln_margin_b>$temp)$temp=$v_pln_margin_b;
		$sum_pln_margin_b+=$v_pln_margin_b;
		
		$v_pln_margin=$s[$r['id']]['sales']-$s[$r['id']]['purchase'];
		if($v_pln_margin>$temp)$temp=$v_pln_margin;
		$sum_pln_margin+=$v_pln_margin;

		$v_pln_margin_bb=$s_bb[$r['id']]['sales']-$s_bb[$r['id']]['purchase'];
		if($v_pln_margin_bb>$temp)$temp=$v_pln_margin_bb;
		$sum_pln_margin_bb+=$v_pln_margin_bb;
	//}
	$arr[]=array(
		"name"=>repl($r['name']),
		
		"value_sales_b"=>$v_sales_b,
		"value_pln_margin_b"=>$v_pln_margin_b,
		"value_quantity_b"=>$v_quantity_b,
		"value_margin_b"=>$v_margin_b,
		
		"value_sales"=>$v_sales,
		"value_pln_margin"=>$v_pln_margin,
		"value_quantity"=>$v_quantity,
		"value_margin"=>$v_margin,
		
		"value_sales_bb"=>$v_sales_bb,
		"value_pln_margin_bb"=>$v_pln_margin_bb,
		"value_quantity_bb"=>$v_quantity_bb,
		"value_margin_bb"=>$v_margin_bb,
	);
}
$arr=multisort($arr,array(array('key'=>'value_sales','sort'=>'desc')));
foreach($arr as $a){
	$data_sales_b[]=$a['value_sales_b'];
	$data_quantity_b[]=$a['value_quantity_b'];
	$data_pln_margin_b[]=$a['value_pln_margin_b'];
	$data_margin_b[]=$a['value_margin_b'];
	
	$data_sales[]=$a['value_sales'];
	$data_quantity[]=$a['value_quantity'];
	$data_pln_margin[]=$a['value_pln_margin'];
	$data_margin[]=$a['value_margin'];
	
	$data_sales_bb[]=$a['value_sales_bb'];
	$data_quantity_bb[]=$a['value_quantity_bb'];
	$data_pln_margin_bb[]=$a['value_pln_margin_bb'];
	$data_margin_bb[]=$a['value_margin_bb'];

	$cats[]=$a['name'];
}

// tworzymy nowy objekt
$g = new graph();

//Tytuł wykresu
$title="Kategorie";
$g->title( repl($title), '{font-size:20px; color: #000000; margin: 5px; background-color: #ffffff; padding:5px; padding-left: 20px; padding-right: 20px;}' );

//kolor tła
$g->bg_colour = '#ffffff';
if(in_array("sales",$sh)){
$g->set_data($data_sales_b);
$g->set_data($data_sales_bb);
$g->set_data($data_sales);
}
if(in_array("quantity",$sh)){
$g->set_data($data_quantity_b);
$g->set_data($data_quantity_bb);
$g->set_data($data_quantity);
}
if(in_array("pln_margin",$sh)){
$g->set_data($data_pln_margin_b);
$g->set_data($data_pln_margin_bb);
$g->set_data($data_pln_margin);
}
if(in_array("margin",$sh)){
$g->set_data($data_margin_b);
$g->set_data($data_margin_bb);
$g->set_data($data_margin);
}

if(in_array("sales",$sh)){
	$rp++;
	$g->bar( 75, '#00CC00', "Sprzedaż ".($ye-1)." - ".number_format($sum_sales_b,2,"."," "), 10 );
	$g->bar( 75, '#66CC00', "Sprzedaż ".($ye-1)." - ".number_format($sum_sales_bb,2,"."," ")." to date", 10 );
	$g->bar( 75, '#006600', "Sprzedaż ".$ye." - ".number_format($sum_sales,2,"."," "), 10 );
}
if(in_array("quantity",$sh)){
	$rp++;
	$g->bar( 75, '#999999', "Ilość ".($ye-1)." - ".number_format($sum_quantity_b,2,"."," "), 10 );
	$g->bar( 75, '#333333', "Ilość ".($ye-1)." - ".number_format($sum_quantity_bb,2,"."," ")." to date", 10 );
	$g->bar( 75, '#000000', "Ilość ".$ye." - ".number_format($sum_quantity,2,"."," "), 10 );
}
if(in_array("pln_margin",$sh)){
	$rp++;
	$g->bar( 75, '#3334AD', "Margines PLN ".($ye-1)." - ".number_format($sum_pln_margin_b,2,"."," "), 10 );
	$g->bar( 75, '#9994AD', "Margines PLN ".($ye-1)." - ".number_format($sum_pln_margin_bb,2,"."," ")." to date", 10 );
	$g->bar( 75, '#0000cc', "Margines PLN ".$ye." - ".number_format($sum_pln_margin,2,"."," "), 10 );
}
if(in_array("margin",$sh)){
	$margb=100*$sum_pln_margin_b/$sum_sales_b;
	$marg=100*$sum_pln_margin/$sum_sales;
	$g->bar( 75, '#D54C78', "Margin ".($ye-1)." - ".round($margb,2)." ", 10 );
	$g->bar( 75, '#DDDDDD', "Margin ".($ye-1)." - ".round($margbb,2)." to date", 10 );
	$g->bar( 75, '#882042', "Margin ".($ye)." - ".round($marg,2)." ", 10 );
}

if(in_array("margin",$sh)){
	$g->attach_to_y_right_axis(2*($rp)+1);
	$g->attach_to_y_right_axis(2*($rp)+2);
	$g->set_y_right_max( $temp_margin );
}

$g->x_axis_colour( '#909090', '#ADB5C7' );
$g->y_axis_colour( '#909090', '#ADB5C7' );

$g->set_x_labels( $cats );
$g->set_x_label_style( 10, '#000000', 2 );
$g->set_y_max( $temp );
$g->y_label_steps( 20 );
$g->set_y_legend( '', 12, '#736AFF' );
echo $g->render();

mysql_close($sql);
?>