<?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/EcmCharts/chartHelper.php');
include_once("/var/www/html/e5crm/modules/EcmReports/cache.php");
function repl($t){
	$a1=array("ą","ś","ę","ż","ź","ć","ń","ó","ł","Ą","Ś","Ę","Ż","Ź","Ć","Ń","Ó","Ł");
	$a2=array("a","s","e","z","z","c","n","o","l","A","S","E","Z","Z","C","N","O","L");
	return str_replace($a1,$a2,$t);
}

function getStock($category_id){
	$ww=mysql_query("select sum(h.value) as sum from ecmproducts_history_states as h inner join ecmproducts as p on p.id=h.ecmproduct_id where p.product_category_id='".$category_id."' and p.deleted='0' and h.ecmstock_id='c7afd71a-4c3a-bde4-138d-4acaee1644e4' and p.product_active='1'");
	$rr=mysql_fetch_array($ww);
	
	$i=mysql_num_rows(mysql_query("select distinct date from ecmproducts_history_states"));
	$v=$rr['sum'];

	return $v/$i;
}
if((int)date("m")==1){
	$mmm=12;
	$year=date("Y")-1;
}
else{
	$mmm=(int)date("m")-1;
	$year=date("Y");
}
$s=sum($year,"","","","");

//$w=mysql_query("select id,name from ecmproductcategories where deleted='0' and name!=''");
//while($r=mysql_fetch_array($w)){
	//$ss=getSum($s,$year,"",$r['id']);
	//@$rot[$r['id']]=12*($ss['total_purchase']/$mmm)/getStock($r['id']);
	//if($tempr<$rot[$r['id']])$tempr=$rot[$r['id']];
//}
//echo '<pre>';
//print_r($rot);
//echo '</pre>';m
$w=mysql_query("select p.id,p.product_category_id from ecmproducts as p inner join ecmproductcategories as c on c.id=p.product_category_id where p.deleted='0' order by c.name asc");
while($r=mysql_fetch_array($w)){
	$qty=0;
	$ww=mysql_query("select quantity,price from ecmstockstates where deleted='0' and product_id='".$r['id']."'");
	while($rrr=mysql_fetch_array($ww)){
		$qty+=$rrr['quantity']*$rrr['price'];
	}
	$cats[$r['product_category_id']]+=$qty;
	if($cats[$r['product_category_id']]>$temp)$temp=$cats[$r['product_category_id']];
}
//$arr_s=multisort($arr_s,array(array('key'=>'value','sort'=>'desc')));
$i=0;
foreach($cats as $k=>$v){
	$r=mysql_fetch_array(mysql_query("select name from ecmproductcategories where id='".$k."' and deleted='0'"));
	if($r['name']){
	$data_1[$i]=$v;
	if($v)$data_2[$i]=$rot[$k];
	else $data_2[$i]=0;
	$cn[$i]=repl($r['name']);
	$i++;
	}
}

$g = new graph();

//$g->title( 'Stocks', '{font-size:20px; color: #000000; margin: 5px; background-color: #ffffff; padding:5px; padding-left: 20px; padding-right: 20px;}' );

$g->bg_colour = '#ffffff';

$d1 = new bar( 75, '#D54C78' );
$d2 = new bar( 75, '#3334AD' );
$d1->key( 'Inventory valueee', 10 );
$d2->key( 'Rotation', 10 );
for($i=0;$i<count($data_1);$i++){
  	$d1->add_data_tip($data_1[$i],"Value: ".$data_1[$i]."\nRotation: ".round($data_2[$i],2));
  	//$d2->add_data_tip($data_2[$i],"Value: ".$data_1[$i]."\nRotation: ".round($data_2[$i],2));
}
$g->data_sets[]=$d1;
//$g->data_sets[]=$d2;

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

$g->attach_to_y_right_axis(2);

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

$g->set_x_labels( $cn );
$g->set_x_label_style( 10, '#000000', 2 );

$g->set_y_max( $temp );
$g->set_y_right_max( $tempr);

$g->y_label_steps( 20 );
//$g->y_right_label_steps( 5 );

$g->set_y_legend( 'Inventory value', 12, '#D54C78' );
$g->set_y_right_legend( 'Rotation' ,12 , '#3334AD' );

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