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

$sh=$_SESSION['EcmCharts_5_d_show'];
$ye=$_SESSION['EcmCharts_5_d_year'];
$temp=0;
$months=array('styczeń','luty','marzec','kwiecień','maj','czerwiec','lipiec','sierpień','wrzesień','październik','listopad','grudzień');

$s1=getSalesByProduct($_REQUEST['category'],$ye."-01-01",$ye."-12-31","",$_SESSION['EcmCharts_5_d_account']);
$s2=getSalesByProduct($_REQUEST['category'],($ye-1)."-01-01",($ye-1)."-12-31","",$_SESSION['EcmCharts_5_d_account']);

for($i=1;$i<=12;$i++){
	if($i<10)$n="0".$i;
	else $n=$i;
		
	$value1=$s1[$i];
	$value2=$s2[$i];

	$dsc1+=$value1['dsc'];
	$dsc2+=$value2['dsc'];
	if(in_array("sales",$sh)){
		if($value1['sales']>$temp)$temp=$value1['sales'];
		if(!$value1['sales'])$value1n=0;
		else $value1n=$value1['sales'];

		if($value2['sales']>$temp)$temp=$value2['sales'];
		if(!$value2['sales'])$value1b=0;
		else $value1b=$value2['sales'];
	}
	if(in_array("quantity",$sh)){
		if($value1['quantity']>$temp)$temp=$value1['quantity'];
		if(!$value1['quantity'])$value2n=0;
		else $value2n=$value1['quantity'];

		if($value2['quantity']>$temp)$temp=$value2['quantity'];
		if(!$value2['quantity'])$value2b=0;
		else $value2b=$value2['quantity'];
	}
	//if(in_array("pln_margin",$sh)){
		$pln_margin=$value1['sales']-$value1['purchase'];
		if($pln_margin>$temp)$temp=$pln_margin;
		if(!$pln_margin)$value3n=0;
		$value3n=$pln_margin;

		$pln_margin=$value2['sales']-$value2['purchase'];
		if($pln_margin>$temp)$temp=$pln_margin;
		if(!$pln_margin)$value3b=0;
		$value3b=$pln_margin;
	//}
	if(in_array("margin",$sh)){
		$mar++;
		if($value1['sales']>0)$margin=100*($value1['sales']-$value1['purchase'])/$value1['sales'];
		else $margin=0;
		if($margin>$temp_margin)$temp_margin=$margin;
		if(!$margin)$value4n=0;
		$value4n=$margin;

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

	$arr[]=array(
		"name"=>$months[$i-1],
		"value1n"=>$value1n,
		"value2n"=>$value2n,
		"value3n"=>$value3n,
		"value4n"=>$value4n,
		"value1b"=>$value1b,
		"value2b"=>$value2b,
		"value3b"=>$value3b,
		"value4b"=>$value4b,
	);
}
foreach($arr as $a){
	$sum1n+=$a['value1n'];
	$sum2n+=$a['value2n'];
	$sum3n+=$a['value3n'];
	$sum4n+=$a['value4n'];
	$sum1b+=$a['value1b'];
	$sum2b+=$a['value2b'];
	$sum3b+=$a['value3b'];
	$sum4b+=$a['value4b'];
	$data_1n[]=$a['value1n'];
	$data_1b[]=$a['value1b'];	
	$data_2n[]=$a['value2n'];
	$data_2b[]=$a['value2b'];	
	$data_3n[]=$a['value3n'];
	$data_3b[]=$a['value3b'];	
	$data_4n[]=$a['value4n'];
	$data_4b[]=$a['value4b'];

	$labels[]=repl($a['name']);
}
$rdsc=mysql_fetch_array(mysql_query("select sum(discount) as d from ecminvoiceouts where discount>0 and deleted='0' and register_date like '".$ye."%'"));
$sum1n-=$rdsc['d']/1.22;
$rdsc=mysql_fetch_array(mysql_query("select sum(discount) as d from ecminvoiceouts where discount>0 and deleted='0' and register_date like '".($ye-1)."%'"));
$sum1b-=$rdsc['d']/1.22;
if($_SESSION['EcmCharts_5_d_account']){
	$r=mysql_fetch_array(mysql_query("select name from accounts where id='".$_SESSION['EcmCharts_5_d_account']."'"));
	$acc_name=" - ".$r['name'];
}
if($_REQUEST['category']){
	$r=mysql_fetch_array(mysql_query("select name from ecmproductcategories where id='".$_REQUEST['category']."'"));
	$cat_name=" - ".$r['name'];
}
$g = new graph();

if(in_array("sales",$sh))$g->set_data( $data_1b );
if(in_array("sales",$sh))$g->set_data( $data_1n );
if(in_array("quantity",$sh))$g->set_data( $data_2b );
if(in_array("quantity",$sh))$g->set_data( $data_2n );
if(in_array("pln_margin",$sh))$g->set_data( $data_3b );
if(in_array("pln_margin",$sh))$g->set_data( $data_3n );
if(in_array("margin",$sh))$g->set_data( $data_4b );
if(in_array("margin",$sh))$g->set_data( $data_4n );

$m=(int)date("m");
if($m==1){
	$year--;
	$m=12;
}
else $m--;
if($m<10)$m="0".$m;
$rp=0;
if(in_array("sales",$sh)){
	$rp++;
	$g->bar( 75, '#00CC00', "Sprzedaż ".($ye-1)." - ".number_format($sum1b,2,"."," "), 10 );
	$g->bar( 75, '#006600', "Sprzedaż ".$ye." - ".number_format($sum1n,2,"."," "), 10 );
}
if(in_array("quantity",$sh)){
	$rp++;
	$g->bar( 75, '#999999', "Ilość ".($ye-1)." - ".number_format($sum2b,2,"."," "), 10 );
	$g->bar( 75, '#000000', "Ilość ".$ye." - ".number_format($sum2n,2,"."," "), 10 );
}
if(in_array("pln_margin",$sh)){
	$rp++;
	$g->bar( 75, '#3334AD', "Margines PLN ".($ye-1)." - ".number_format($sum3b,2,"."," "), 10 );
	$g->bar( 75, '#0000cc', "Margines PLN ".$ye." - ".number_format($sum3n,2,"."," "), 10 );
}
if(in_array("margin",$sh)){
	$margb=100*$sum3b/$sum1b;
	$margn=100*$sum3n/$sum1n;
	$g->bar( 75, '#D54C78', "Margines ".($ye-1)." - ".round($margb,2)." ", 10 );
	$g->bar( 75, '#882042', "Margines ".($ye)." - ".round($margn,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->x_axis_colour( '#909090', '#ADB5C7' );
$g->y_axis_colour( '#909090', '#ADB5C7' );

$g->set_x_labels( $labels );
$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' );
$g->set_y_right_legend( '' ,12 , '#164166' );

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