<?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/chartHelper.php');

$s=sum($_SESSION['EcmCharts_3_year'],$_SESSION['EcmCharts_3_category']);
$ye=$_SESSION['EcmCharts_3_year'];
$sh=$_SESSION['EcmCharts_3_show'];
$temp=0;
$temp_margin=0;
$sum=0;
$months=array("January","February","March","April","May","June","July","August","September","October","November","Dezember");
if($_SESSION['EcmCharts_3_category']){
for($i=1;$i<=$_SESSION['EcmCharts_3_nnn'];$i++){
	if($i<10)$ddd="0".$i;
	else $ddd=$i;
	$ss=$s[$_SESSION['EcmCharts_3_year']."-".$ddd];
	if($sh=="sales"){
		if($ss['category_'.$_SESSION['EcmCharts_3_category']]>$temp)$temp=$ss['category_'.$_SESSION['EcmCharts_3_category']];
		if(!$ss['category_'.$_SESSION['EcmCharts_3_category']])$value=0;
		else $value = $ss['category_'.$_SESSION['EcmCharts_3_category']];
		$sum+=$value;
	}
	if($sh=="quantity"){
		if($ss['category_qty_'.$_SESSION['EcmCharts_3_category']]>$temp)$temp=$ss['category_qty_'.$_SESSION['EcmCharts_3_category']];
		if(!$ss['category_qty_'.$_SESSION['EcmCharts_3_category']])$value=0;
		else $value = $ss['category_qty_'.$_SESSION['EcmCharts_3_category']];
		$sum+=$value;
	}
	if($sh=="margin"){
		if($ss['category_'.$_SESSION['EcmCharts_3_category']]>0)$margin=100*($ss['category_'.$_SESSION['EcmCharts_3_category']]-$ss['category_pur_'.$_SESSION['EcmCharts_3_category']])/$ss['category_'.$_SESSION['EcmCharts_3_category']];
		if($margin>$temp)$temp=$margin;
		if(!$margin)$value=0;
		else $value=$margin;
	}
	if($sh=="pln_margin"){
		$pln_margin=$ss['category_'.$_SESSION['EcmCharts_3_category']]-$ss['category_pur_'.$_SESSION['EcmCharts_3_category']];
		if($pln_margin>$temp)$temp=$pln_margin;
		if(!$pln_margin)$value=0;
		else $value=$pln_margin;
		$sum+=$value;
	}
	
	$arr[]=array(
		"name"=>$months[$i-1],
		"value"=>$value,
	);
}
}
else{
for($i=1;$i<=$_SESSION['EcmCharts_3_nnn'];$i++){
	if($i<10)$ddd="0".$i;
	else $ddd=$i;
	$ss=$s[$_SESSION['EcmCharts_3_year']."-".$ddd];
	if($sh=="sales"){
		if($ss['all']>$temp)$temp=$ss['all'];
		if(!$ss['all'])$value=0;
		else $value = $ss['all'];
		$sum+=$value;
	}
	if($sh=="quantity"){
		if($ss['all_qty']>$temp)$temp=$ss['all_qty'];
		if(!$ss['all_qty'])$value=0;
		else $value = $ss['all_qty'];
		$sum+=$value;
	}
	if($sh=="margin"){
		if($ss['all']>0)$margin=100*($ss['all']-$ss['all_pur'])/$ss['all'];
		if($margin>$temp)$temp=$margin;
		if(!$margin)$value=0;
		else $value=$margin;
	}
	if($sh=="pln_margin"){
		$pln_margin=$ss['all']-$ss['all_pur'];
		if($pln_margin>$temp)$temp=$pln_margin;
		if(!$pln_margin)$value=0;
		else $value=$pln_margin;
		$sum+=$value;
	}
	
	$arr[]=array(
		"name"=>$months[$i-1],
		"value"=>$value,
	);
}
}
//$arr=multisort($arr,array(array('key'=>'value','sort'=>'desc')));
if($sh=="quantity" || $sh=="sales" || $sh=="pln_margin"){
	$arr[]=array(
		"name"=>"all",
		"value"=>($sum/10),
	);
}
foreach($arr as $a){
	$data_1[]=$a['value'];
	$labels[]=repl($a['name']);
}

$g = new graph();
if($_SESSION['EcmCharts_3_category']){
	$r=mysql_fetch_array(mysql_query("select name from ecmproductcategories where id='".$_SESSION['EcmCharts_3_category']."'"));
	$cat_title=": ".$r['name'];
}
else $cat_title="";
$g->title( 'Groups'.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';

if($sh=="quantity")$leg="Pcs: ".round($sum,2);
if($sh=="sales")$leg="Sales: ".round($sum,2);
if($sh=="pln_margin")$leg="PLN Margin: ".round($sum,2);
if($sh=="margin")$leg="% Margin: ".round($sum,2);
$as=$sum/$_SESSION['EcmCharts_3_nnn'];
$g->set_data( $data_1 );

$li=array();
for($i=0;$i<$_SESSION['EcmCharts_3_mmm'];$i++)$li[]=$as;

if($sh!="margin")$g->set_data($li);
if($sh=="sales")$g->bar( 75, '#D54C78', $leg, 10 );
if($sh=="quantity")$g->bar( 75, '#333333', $leg, 10 );
if($sh=="pln_margin")$g->bar( 75, '#3334AD', $leg, 10 );
if($sh=="margin")$g->bar( 75, '#00CC00', '', 10 );
$g->line( 2,'0x9933CC', '', 18 );

//$g->set_x_axis_3d(12);

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

$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);
?>