<?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' );
function repl($t){
	$a1=array("ą","ś","ę","ż","ź","ć","ń","ó","ł");
	$a2=array("a","s","e","z","z","c","n","o","l");
	return str_replace($a1,$a2,$t);
}
	function multisort($data,$keys)
	{
		if(count($data)>0)
		{
		foreach($data as $key => $row)
		{
    		foreach($keys as $k)
			{
      			$cols[$k['key']][$key] = $row[$k['key']];
    		}
  		}

  		$idkeys=@array_keys($data);
  		$i=0;
  		foreach($keys as $k)
		{
    		if($i>0)$sort.=',';
   			$sort.='$cols['.$k['key'].']';
    		if($k['sort'])$sort.=',SORT_'.strtoupper($k['sort']);
    		if($k['type'])$sort.=',SORT_'.strtoupper($k['type']);
    		$i++;
  		}
  		$sort.=',$idkeys';
  		$sort='@array_multisort('.$sort.');';
  		eval($sort);
  		foreach($idkeys as $idkey)
		{
    		$result[$idkey]=$data[$idkey];
  		}
  		return $result;
		}
	}
function sum($year,$c=""){
	if($c)$wh=" and product_category_id='".$c."'";
	else $wh="";
	$w=mysql_query("select id,product_category_id as cid from ecmproducts where deleted='0'".$wh);
	while($r=mysql_fetch_array($w)){
		$ww=mysql_query("select ecmproducts.carton_volume_meter as cvm,ecmproducts.pieces_per_carton as ppc,ecmproducts.carton_dimensions_1 as cd1,ecmproducts.carton_dimensions_2 as cd2,ecmproducts.carton_dimensions_3 as cd3,ecminvoiceouts.register_date as d,ecminvoiceoutitems.purchase_price as purchase_price,ecminvoiceoutitems.price as price,ecminvoiceoutitems.quantity as quantity,ecminvoiceoutitems.ecmproduct_id as ecmproduct_id from ecminvoiceoutitems inner join ecminvoiceouts on ecminvoiceouts.id=ecminvoiceoutitems.ecminvoiceout_id inner join ecmproducts on ecmproducts.id=ecminvoiceoutitems.ecmproduct_id where ecminvoiceouts.register_date like '".$year."%' and ecminvoiceoutitems.deleted='0' and ecminvoiceoutitems.ecmproduct_id='".$r['id']."' and  ecminvoiceouts.type!='correct'");
		while($rr=mysql_fetch_array($ww)){
			$d=explode("-",$rr['d']);
			$dat=$year."-".$d[1];
			//if($dat>$year."-".$_SESSION['EcmCharts_1_mmm'])continue;
			
			$sum['all']+=$rr['price']*$rr['quantity'];
			$sum['all_qty']+=$rr['quantity'];
			$sum['all_pur']+=$rr['purchase_price']*$rr['quantity'];
			if($rr['ppc']>0)$sum['all_cbm']+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
			
			$sum[$dat]['all']+=$rr['price']*$rr['quantity'];
			$sum[$dat]['all_qty']+=$rr['quantity'];
			$sum[$dat]['all_pur']+=$rr['purchase_price']*$rr['quantity'];
			if($rr['ppc']>0)$sum[$dat]['all_cbm']+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
			
			if(!$all){
				$sum[$dat]['category_'.$r['cid']]+=$rr['price']*$rr['quantity'];
				$sum[$dat]['category_qty_'.$r['cid']]+=$rr['quantity'];
				$sum[$dat]['category_pur_'.$r['cid']]+=$rr['purchase_price']*$rr['quantity'];
				if($rr['ppc']>0)$sum[$dat]['category_cbm_'.$rr['cid']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
			
				$sum[$dat]['product_'.$rr['ecmproduct_id']]+=$rr['price']*$rr['quantity'];
				$sum[$dat]['product_qty_'.$rr['ecmproduct_id']]+=$rr['quantity'];
				$sum[$dat]['product_pur_'.$rr['ecmproduct_id']]+=$rr['purchase_price']*$rr['quantity'];
				if($rr['ppc']>0)$sum[$dat]['product_cbm_'.$rr['ecmproduct_id']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
			
				$sum[$year]['category_sum_'.$r['cid']]+=$rr['price']*$rr['quantity'];
				$sum[$year]['category_qty_sum_'.$r['cid']]+=$rr['quantity'];
				$sum[$year]['category_pur_sum_'.$r['cid']]+=$rr['purchase_price']*$rr['quantity'];
				if($rr['ppc']>0)$sum[$year]['category_cbm_sum_'.$r['cid']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
			
				$sum[$year]['product_sum_'.$rr['ecmproduct_id']]+=$rr['price']*$rr['quantity'];
				$sum[$year]['product_qty_sum_'.$rr['ecmproduct_id']]+=$rr['quantity'];
				$sum[$year]['product_pur_sum_'.$rr['ecmproduct_id']]+=$rr['purchase_price']*$rr['quantity'];
				if($rr['ppc']>0)$sum[$year]['product_cbm_sum_'.$rr['ecmproduct_id']]+=$rr['quantity']*($rr['cvm']/$rr['ppc']);
			}
		}
	}
	return $sum;
}

$s=sum($_SESSION['EcmCharts_2_year']);
$temp=0;

$w=mysql_query("select id,name from ecmproductcategories where deleted='0' and name!=''");
while($r=mysql_fetch_array($w)){
	if(!$s[$_SESSION['EcmCharts_2_year']]['category_sum_'.$r['id']])$v=0;
	else $v = 100*($s[$_SESSION['EcmCharts_2_year']]['category_sum_'.$r['id']]/$s['all']);
	$arr[]=array(
		"name"=>repl($r['name']),
		"value"=>$v,
	);
}
$arr=multisort($arr,array(array('key'=>'value','sort'=>'asc')));
foreach($arr as $a){
	$cats[]=$a['name'];
	$data_1[] = $a['value'];
}

// tworzymy nowy objekt
$g2 = new graph();

//kolor tła
$g2->bg_colour = '#ffffff';

//tworzymy wykres kołowy krycie 60, i style
$g2->pie(60,'#505050','{display:none;}',false,1);

//nazwy danych
$g2->pie_values($data_1,$cats);
//$g2->pie_labels=$cats;

//kolory dla danych
$g2->pie_slice_colours( array('#d01f3c','#66FF66','#356aa0','#ff3300','#0033cc') );
$g2->set_tool_tip( 'Label: #x_label#<br>Value: #val#%' );

//tytuł
$g2->title(  'Sales % in categories', '{font-size:20px; color: #FFFFFF; margin: 5px; background-color: #3334AD; padding:5px; padding-left: 20px; padding-right: 20px;}' );
echo $g2->render();
mysql_close($sql);
?>