<?php
session_start();
include_once( '/var/www/html/e5crm/config.php');
$sql=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/EcmReports/cache.php');
include_once('/var/www/html/e5crm/modules/EcmCharts/helperChart.php');

$s=getSalesByProduct1($_REQUEST['category'],$_SESSION['EcmCharts_1_d_year']."-01-01",$_SESSION['EcmCharts_1_d_year']."-12-31","",$_SESSION['EcmCharts_1_d_account']);
$sb=getSalesByProduct1($_REQUEST['category'],($_SESSION['EcmCharts_1_d_year']-1)."-01-01",($_SESSION['EcmCharts_1_d_year']-1)."-".date("m")."-".date("d"),"",$_SESSION['EcmCharts_1_d_account']);
$sbb=getSalesByProduct1($_REQUEST['category'],($_SESSION['EcmCharts_1_d_year']-1)."-01-01",($_SESSION['EcmCharts_1_d_year']-1)."-12-31","",$_SESSION['EcmCharts_1_d_account']);
$ye=$_SESSION['EcmCharts_1_d_year'];
$temp=0;
$sh=$_SESSION['EcmCharts_1_d_show'];
$ye=$_SESSION['EcmCharts_1_d_year'];
$ca=$_REQUEST['category'];
if($ca)$wh=" and product_category_id='".$ca."'";
else $wh="";
$w=mysql_query("select id,code,name from ecmproducts where deleted='0'".$wh);
while($r=mysql_fetch_array($w)){
	if(in_array("sales",$sh)){
		if($s[$r['id']]['sales']>$temp)$temp=$s[$r['id']]['sales'];
		if(!$s[$r['id']]['sales'])$value1=0;
		else $value1=$s[$r['id']]['sales'];
		$sum1+=$value1;

		if($sb[$r['id']]['sales']>$temp)$temp=$sb[$r['id']]['sales'];
		if(!$sb[$r['id']]['sales'])$value1b=0;
		else $value1b=$sb[$r['id']]['sales'];
		$sum1b+=$value1b;

		if($sbb[$r['id']]['sales']>$temp)$temp=$sbb[$r['id']]['sales'];
		if(!$sbb[$r['id']]['sales'])$value1bb=0;
		else $value1bb=$sbb[$r['id']]['sales'];
		$sum1bb+=$value1bb;
	}
	if(in_array("quantity",$sh)){
		if($s[$r['id']]['quantity']>$temp)$temp=$s[$r['id']]['quantity'];
		if(!$s[$r['id']]['quantity'])$value2=0;
		else $value2=$s[$r['id']]['quantity'];
		$sum2+=$value2;

		if($sb[$r['id']]['quantity']>$temp)$temp=$sb[$r['id']]['quantity'];
		if(!$sb[$r['id']]['quantity'])$value2b=0;
		else $value2b=$sb[$r['id']]['quantity'];
		$sum2b+=$value2b;

		if($sbb[$r['id']]['quantity']>$temp)$temp=$sbb[$r['id']]['quantity'];
		if(!$sbb[$r['id']]['quantity'])$value2bb=0;
		else $value2bb=$sbb[$r['id']]['quantity'];
		$sum2bb+=$value2bb;
	}
	if(in_array("pln_margin",$sh)){
		$pln_margin=$s[$r['id']]['sales']-$s[$r['id']]['purchase'];
		if($pln_margin>$temp)$temp=$pln_margin;
		if(!$pln_margin)$value3=0;
		$value3=$pln_margin;
		$sum3+=$value3;

		$pln_margin=$sb[$r['id']]['sales']-$sb[$r['id']]['purchase'];
		if($pln_margin>$temp)$temp=$pln_margin;
		if(!$pln_margin)$value3b=0;
		$value3b=$pln_margin;
		$sum3b+=$value3b;

		$pln_margin=$sbb[$r['id']]['sales']-$sbb[$r['id']]['purchase'];
		if($pln_margin>$temp)$temp=$pln_margin;
		if(!$pln_margin)$value3bb=0;
		$value3bb=$pln_margin;
		$sum3bb+=$value3bb;
	}
	if(in_array("margin",$sh)){
		$mar++;
		if($s[$r['id']]['sales']>0)$margin=100*($s[$r['id']]['sales']-$s[$r['id']]['purchase'])/$s[$r['id']]['sales'];
		else $margin=0;
		if($margin>$temp_margin)$temp_margin=$margin;
		if(!$margin)$value4=0;
		$value4=$margin;
		$sum4+=$value4;

		$marb++;
		if($sb[$r['id']]['sales']>0)$margin=100*($sb[$r['id']]['sales']-$sb[$r['id']]['purchase'])/$sb[$r['id']]['sales'];
		else $margin=0;
		if($margin>$temp_margin)$temp_margin=$margin;
		if(!$margin)$value4b=0;
		$value4b=$margin;
		$sum4b+=$value4b;


		$marbb++;
		if($sbb[$r['id']]['sales']>0)$margin=100*($sbb[$r['id']]['sales']-$sbb[$r['id']]['purchase'])/$sbb[$r['id']]['sales'];
		else $margin=0;
		if($margin>$temp_margin)$temp_margin=$margin;
		if(!$margin)$value4bb=0;
		$value4bb=$margin;
		$sum4bb+=$value4bb;
	}
	$arr[]=array(
		"name"=>$r['code'],
		"name1"=>$r['code']." ".$r['name'],

		"value1"=>$value1,
		"value2"=>$value2,
		"value3"=>$value3,
		"value4"=>$value4,

		"value1b"=>$value1b,
		"value2b"=>$value2b,
		"value3b"=>$value3b,
		"value4b"=>$value4b,

		"value1bb"=>$value1bb,
		"value2bb"=>$value2bb,
		"value3bb"=>$value3bb,
		"value4bb"=>$value4bb,
	);
}
$arr=multisort($arr,array(array('key'=>'value1','sort'=>'desc')));
foreach($arr as $a){
	$cm++;
	$data1[]=$a['value1'];
	$data2[]=$a['value2'];
	$data3[]=$a['value3'];
	$data4[]=$a['value4'];

	$data1b[]=$a['value1b'];
	$data2b[]=$a['value2b'];
	$data3b[]=$a['value3b'];
	$data4b[]=$a['value4b'];

	$data1bb[]=$a['value1bb'];
	$data2bb[]=$a['value2bb'];
	$data3bb[]=$a['value3bb'];
	$data4bb[]=$a['value4bb'];

	$products[]=repl($a['name']);
	$names[]=repl($a['name1']);
	if($cm==20)break;
}

$g = new graph();
if($_REQUEST['category']){
	$r=mysql_fetch_array(mysql_query("select name from ecmproductcategories where id='".$_REQUEST['category']."'"));
	$cat_title=": ".$r['name'];
}
else $cat_title="";

$g->title( 'Produkty'.repl($cat_title), '{font-size:20px; color: #000000; margin: 5px; background-color: #ffffff; padding:5px; padding-left: 20px; padding-right: 20px;}' );
$g->bg_colour = '#ffffff';

$rp=0;
if(in_array("sales",$sh)){
	$rp++;
	$data1bm = new bar( 75, '#00CC00', "Sprzedaż ".($ye-1)." - ".number_format($sum1b,2,"."," ").' to date', 10 );
	$data1bm->key( "Sales ".($ye-1)." - ".number_format($sum1b,2,"."," ").' to date', 10 );
	for($i=0;$i<count($data1b);$i++)$data1bm->add_data_tip( $data1b[$i], $names[$i] );
	$g->data_sets[]=$data1bm;

	$data1m = new bar( 75, '#006600', "Sprzedaż ".$ye." - ".number_format($sum1,2,"."," "), 10 );
	$data1m->key( "Sales ".$ye." - ".number_format($sum1,2,"."," "), 10 );
	for($i=0;$i<count($data1);$i++)$data1m->add_data_tip( $data1[$i], $names[$i] );
	$g->data_sets[]=$data1m;	

	$data1bbm = new bar( 75, '#66CC00', "Sprzedaż ".($ye-1)." - ".number_format($sum1bb,2,"."," "), 10 );
	$data1bbm->key( "Sales ".($ye-1)." - ".number_format($sum1bb,2,"."," "), 10 );
	for($i=0;$i<count($data1bb);$i++)$data1bbm->add_data_tip( $data1bb[$i], $names[$i] );
	$g->data_sets[]=$data1bbm;
}

if(in_array("quantity",$sh)){
	$rp++;
	$data2bm = new bar( 75, '#999999', "Ilość ".($ye-1)." - ".number_format($sum2b,2,"."," ").' to date', 10 );
	$data2bm->key( "Quantity ".($ye-1)." - ".number_format($sum2b,2,"."," ").' to date', 10 );
	for($i=0;$i<count($data2b);$i++)$data2bm->add_data_tip( $data2b[$i], $names[$i] );
	$g->data_sets[]=$data2bm;	

	$data2m = new bar( 75, '#000000', "Ilość ".($ye)." - ".number_format($sum2,2,"."," "), 10 );
	$data2m->key( "Quantity ".($ye)." - ".number_format($sum2,2,"."," "), 10 );
	for($i=0;$i<count($data2);$i++)$data2m->add_data_tip( $data2[$i], $names[$i] );
	$g->data_sets[]=$data2m;	

	$data2bbm = new bar( 75, '#333333', "Ilość ".($ye-1)." - ".number_format($sum2bb,2,"."," "), 10 );
	$data2bbm->key( "Quantity ".($ye-1)." - ".number_format($sum2bb,2,"."," "), 10 );
	for($i=0;$i<count($data2bb);$i++)$data2bbm->add_data_tip( $data2bb[$i], $names[$i] );
	$g->data_sets[]=$data2bbm;
}

if(in_array("pln_margin",$sh)){	
	$rp++;
	$data3bm = new bar( 75, '#3334AD', "Margines PLN ".($ye-1)." - ".number_format($sum3b,2,"."," ").' to date', 10 );
	$data3bm->key( "Margin PLN ".($ye-1)." - ".number_format($sum3b,2,"."," ").' to date', 10 );
	for($i=0;$i<count($data3b);$i++)$data3bm->add_data_tip( $data3b[$i], $names[$i] );
	$g->data_sets[]=$data3bm;	

	$data3m = new bar( 75, '#0000cc', "Margines PLN ".($ye)." - ".number_format($sum3,2,"."," "), 10 );
	$data3m->key( "Margin PLN ".($ye)." - ".number_format($sum3,2,"."," "), 10 );
	for($i=0;$i<count($data3);$i++)$data3m->add_data_tip( $data3[$i], $names[$i] );
	$g->data_sets[]=$data3m;	

	$data3bbm = new bar( 75, '#333333', "Margines PLN ".($ye-1)." - ".number_format($sum3bb,2,"."," "), 10 );
	$data3bbm->key( "Margin PLN ".($ye-1)." - ".number_format($sum3bb,2,"."," "), 10 );
	for($i=0;$i<count($data3bb);$i++)$data3bbm->add_data_tip( $data3bb[$i], $names[$i] );
	$g->data_sets[]=$data3bbm;	
}

if(in_array("margin",$sh)){
	$margb=100*$sum3b/$sum1b;
	$data4bm = new bar( 75, '#D54C78', "Margines ".($ye-1)." - ".number_format($margb,2,"."," ").' to date', 10 );
	$data4bm->key( "Margin ".($ye-1)." - ".number_format($margb,2,"."," "), 10 ).' to date';
	for($i=0;$i<count($data4b);$i++)$data4bm->add_data_tip( $data4b[$i], $names[$i] );
	$g->data_sets[]=$data4bm;	

	$margn=100*$sum3/$sum1;
	$data4m = new bar( 75, '#882042', "Margines ".($ye)." - ".number_format($margn,2,"."," "), 10 );
	$data4m->key( "Margin ".($ye)." - ".number_format($margn,2,"."," "), 10 );
	for($i=0;$i<count($data4);$i++)$data4m->add_data_tip( $data4[$i], $names[$i] );
	$g->data_sets[]=$data4m;	

	$margbb=100*$sum3bb/$sum1bb;
	$data4bbm = new bar( 75, '#DDDDDD', "Margines ".($ye-1)." - ".number_format($margbb,2,"."," "), 10 );
	$data4bbm->key( "Margin ".($ye-1)." - ".number_format($margbb,2,"."," "), 10 );
	for($i=0;$i<count($data4bb);$i++)$data4bbm->add_data_tip( $data4bb[$i], $names[$i] );
	$g->data_sets[]=$data4bbm;
}

$g->set_tool_tip( '#tip#:<br>#val#');


/*if(in_array("sales",$sh)){
	$rp++;
	$g->bar( 75, '#00CC00', "Sales ".($ye-1)." - ".number_format($sum1b,2,"."," "), 10 );
	$g->bar( 75, '#006600', "Sales ".$ye." - ".number_format($sum1,2,"."," "), 10 );
}
if(in_array("quantity",$sh)){
	$rp++;
	$g->bar( 75, '#999999', "Quantity ".($ye-1)." - ".number_format($sum2b,2,"."," "), 10 );
	$g->bar( 75, '#000000', "Quantity ".$ye." - ".number_format($sum2,2,"."," "), 10 );
}
if(in_array("pln_margin",$sh)){
	$rp++;
	$g->bar( 75, '#3334AD', "Margin PLN ".($ye-1)." - ".number_format($sum3b,2,"."," "), 10 );
	$g->bar( 75, '#0000cc', "Margin PLN ".$ye." - ".number_format($sum3,2,"."," "), 10 );
}
if(in_array("margin",$sh)){
	$margb=100*$sum3b/$sum1b;
	$margn=100*$sum3/$sum1;
	$g->bar( 75, '#D54C78', "Margin ".($ye-1)." - ".round($margb,2)." ", 10 );
	$g->bar( 75, '#882042', "Margin ".($ye)." - ".round($margn,2)." ", 10 );
}*/



$as=$sum1/count($products);

if(in_array("margin",$sh)){
	$g->attach_to_y_right_axis(3*($rp)+1);
	$g->attach_to_y_right_axis(3*($rp)+2);
}
$g->x_axis_colour( '#909090', '#ADB5C7' );
$g->y_axis_colour( '#909090', '#ADB5C7' );
$g->set_x_labels( $products );
$g->set_x_label_style( 10, '#000000', 2 );
$g->set_y_max( $temp );
$g->set_y_right_max( $temp_margin );
$g->y_label_steps( 20 );
$g->set_y_legend( '', 12, '#736AFF' );

echo $g->render();
mysql_close($sql);
?>