<?php
session_start();
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors', '1');
//error_reporting(0);

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");
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);
}
$year=$_SESSION['test_y'];
$month=$_SESSION['test_m'];
	for($i=1;$i<=12;$i++){
		if($i<10)$n="0".$i;
		else $n=$i;
		$r=mysql_fetch_array(mysql_query("select if(e.currency_value>0 and e.currency_value is not null,sum(i.quantity*i.price*e.currency_value),sum(i.quantity*i.price)) as s,sum(i.quantity*i.purchase_price) as p from ecminvoiceouts as e inner join ecminvoiceoutitems as i on e.id=i.ecminvoiceout_id inner join ecmproducts as p on p.id=i.ecmproduct_id inner join ecmstockdocouts as w on w.id=e.wz_id inner join ecmproductcategories as pc on pc.id=p.product_category_id where e.register_date like '".$year."-".$n."%' and e.type!='correct' and e.status='accepted' and i.deleted='0' and e.deleted='0'"));
		$arr[$i]=$r;
		
		$w=mysql_query("select i.price,i.quantity,i.ecminvoiceoutitem_id,e.currency_value from ecminvoiceouts as e inner join ecminvoiceoutitems as i on e.id=i.ecminvoiceout_id inner join ecmproducts as p on p.id=i.ecmproduct_id inner join ecmproductcategories as pc on pc.id=p.product_category_id where e.register_date like '".$year."-".$n."%' and e.type='correct' and e.status='accepted' and i.deleted='0' and e.deleted='0'");
		while($r=mysql_fetch_array($w)){
			if(!$r['currency_value'])$r['currency_value']=1;
			echo $arr[$i]['s']+=$r['currency_value']*$r['price']*$r['quantity'];
			$rr=mysql_fetch_array(mysql_query("select price,quantity,purchase_price from ecminvoiceoutitems where id='".$r['ecminvoiceoutitem_id']."'"));
			$arr[$i]['s']-=$rr['price']*$rr['quantity'];
			$arr[$i]['p']+=$rr['purchase_price']*$r['quantity'];
			$arr[$i]['p']-=$rr['purchase_price']*$rr['quantity'];
		}

	

}

$year_month=$_SESSION['EcmReports_Dashlet_sales_year'];
$e=explode("-",$_SESSION['EcmReports_Dashlet_sales_year']);


$months=array("January","February","March","April","May","June","July","August","September","October","November","Dezember");

echo $year;
for($i=1;$i<=12;$i++){
	$s=$arr;
	if($temp<$s[$i]['s'])$temp=$s[$i]['s'];
	$arr_s[]=array(
		"name"=>repl($months[$i-1]),
		"value"=>$s[$i]['s'],
	);
	$arr_p[]=array(
		"name"=>repl($months[$i-1]),
		"value"=>$s[$i]['p'],
	);
}

for($i=0;$i<count($arr_s);$i++){
	$data_1[]=$arr_s[$i]['value'];
	$data_2[]=$arr_s[$i]['value']-$arr_p[$i]['value'];
	$cats[]=$arr_s[$i]['name']." ".$year;
}


$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';
//$g->bg_colour = '#000000';

$d1 = new bar( 75, '#D54C78' );
$d2 = new bar( 75, '#3334AD' );
$d1->key( 'Sales', 10 );
$d2->key( 'PLN Margin', 10 );
for($i=0;$i<count($data_1);$i++){
  	$d1->add_data_tip($data_1[$i],"Sales: ".round($data_1[$i],2)."\nPLN Margin: ".round($data_2[$i],2)."\nMargin: ".@round(100*($data_2[$i])/$data_1[$i],2)."%");
  	$d2->add_data_tip($data_2[$i],"Sales: ".round($data_1[$i],2)."\nPLN Margin: ".round($data_2[$i],2)."\nMargin: ".@round(100*($data_2[$i])/$data_1[$i],2)."%");
}
$g->data_sets[]=$d1;
$g->data_sets[]=$d2;

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

echo $g->render();

mysql_close($sql);
?>