<?php
if(!defined('sugarEntry'))define('sugarEntry', true);
$startTime = microtime(true);

require_once('/var/www/html/e5crm/include/entryPoint.php');
ob_start();
require_once("/var/www/html/e5crm/config.php");
require_once('/var/www/html/e5crm/include/MVC/SugarApplication.php');
$mm = new MysqlManager();
$mm->connect();
/*$app = new SugarApplication();
$app->startSession();
$app->execute();
*/
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/EcmReports/class.sales.php");


$mmm=(int)date("m")-1;
if($mmm==0)$mmm=12;

$year_month=$_SESSION['EcmReports_Dashlet_sales'];
$e=explode("-",$_SESSION['EcmReports_Dashlet_sales']);
$year=$e[0];
$month=$e[1];

$s=new Sales();
$s->date_from=$year."-".$month."-01";
$s->date_to=$year."-".$month."-31";
$sdata=$s->getSalesByCategory();

$pred=new Sales();
$pred->date_from=$year."-01-01";
$pred->date_to=$year."-12-31";
$pred=$pred->getSalesByCategory();

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 salesPercent($category_id,$date){
	$w=$GLOBALS['db']->query("select value_wz as swz,value_sale as ssale from ecmproductcategories_realisation where product_category_id='".$category_id."' and date='".$date."'");
	$r=$GLOBALS['db']->fetchByAssoc($w);
	return @100*$r['swz']/$r['ssale'];
}

$year_month=$_SESSION['EcmReports_Dashlet_sales'];
$e=explode("-",$_SESSION['EcmReports_Dashlet_sales']);
$year=$e[0];
$month=$e[1];

$s=sum($year_month,"","","","");
$sy=sum($year,"","","","");

$w=$GLOBALS['db']->query("select id,name from ecmproductcategories where deleted='0' and name!='' order by name asc");
while($r=$GLOBALS['db']->fetchByAssoc($w)){
	$ss=$sdata[$month][$r['id']];
	$pp=$pred[$month][$r['id']];
	
	$v_s=$ss['sales'];
	if($v_s>$temp)$temp=$v_s;
	$sum_s+=$v_s;
		
	$v_p=$ss['purchase'];
	if($v_p>$temp)$temp=$v_p;
	$sum_p+=$v_p;
	
	$v_l=$pp['sales']/$mmm;
	if($v_l>$temp)$temp=$v_l;
	$sum_l+=$v_l;
	
	$v_r=salesPercent($r['id'],$year_month);
	
	$arr_s[]=array(
		"name"=>repl($r['name']),
		"value"=>$v_s,
	);
	$arr_p[]=array(
		"value"=>$v_p,
	);
	$arr_l[]=array(
		"value"=>$v_l,
	);
	$arr_r[]=array(
		"value"=>$v_r,
	);
}
//$arr_s=multisort($arr_s,array(array('key'=>'value','sort'=>'desc')));
for($i=0;$i<count($arr_s);$i++){
	$data_1[]=$arr_l[$i]['value'];
	$data_2[]=$arr_s[$i]['value'];
	$data_3[]=$arr_p[$i]['value'];
	$data_4[]=$arr_r[$i]['value'];
	$cats[]=$arr_s[$i]['name'];
}

$g = new graph();

//$g->title( 'Sales: '.$year_month, '{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, '#999999' );
$d2 = new bar( 75, '#D54C78' );
$d3 = new bar( 75, '#3334AD' );
$d1->key( 'Forecast', 10 );
$d2->key( 'Sales', 10 );
$d3->key( 'Margin', 10 );
for($i=0;$i<count($data_1);$i++){
  	$d1->add_data_tip($data_1[$i],"Margin: ".round($data_3[$i],2)."\nSales: ".round($data_2[$i],2)."\nForecast: ".round($data_1[$i],2)."\nFullfillment: ".round($data_4[$i],2)."%");
  	$d2->add_data_tip($data_2[$i],"Margin: ".round($data_3[$i],2)."\nSales: ".round($data_2[$i],2)."\nForecast: ".round($data_1[$i],2)."\nFullfillment: ".round($data_4[$i],2)."%");
  	$d3->add_data_tip($data_3[$i],"Margin: ".round($data_3[$i],2)."\nSales: ".round($data_2[$i],2)."\nForecast: ".round($data_1[$i],2)."\nFullfillment: ".round($data_4[$i],2)."%");
}
$g->data_sets[]=$d1;
$g->data_sets[]=$d2;
$g->data_sets[]=$d3;

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

//$g->set_data($data_1);
//$g->bar( 75, '#D54C78', '', 10 );
//$g->set_data($data_2);
//$g->bar( 75, '#3334AD', '', 10 );

//$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( $cats );
$g->set_x_label_style( 10, '#000000', 2 );

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

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

$g->set_y_legend( '', 12, '#736AFF' );
$g->set_y_right_legend( 'Free Ram (MB)' ,12 , '#164166' );

echo $g->render();

$exit_on_cleanup = true;





sugar_cleanup($exit_on_cleanup);
?>