<?php require_once('Connections/paivaconex.php'); 
include("FusionCharts_Enterprise/Code/PHP/Includes/FusionCharts.php");
session_start();
?>

<script language="javascript" src="FusionCharts_Enterprise/JSClass/FusionCharts.js"></script>

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$sql = '';

if (count($_POST) >=1) {
	if (isset($_POST['mes'])) {
		$quando[] = "month(datafluxo) IN (".implode( " ,",$_POST['mes']).")";
	}
	if (isset($_POST['ano'])) {
		$quando[] = "year(datafluxo) IN (".implode( " ,",$_POST['ano']).")";
	}
	if (isset($_POST['dia'])) {
		$quando[] = "day(datafluxo) IN (".implode( " ,",$_POST['dia']).")";
	}
	if (isset($_POST['cliente'])) {
		$quando[] = "Cliente IN (".implode( " ,",$_POST['cliente']).")";
	}
	if (isset($_POST['pasta'])) {
		$quando[] = "Pasta IN (".implode( " ,",$_POST['pasta']).")";
	}
	if (isset($_POST['status'])) {
		$quando[] = "status IN (".implode( " ,",$_POST['status']).")";
	}
	if (isset($_POST['detalhe'])) {
		$quando[] = "dashboard.Detalhe IN ('".implode( "' ,'",$_POST['detalhe'])."')";
	}
	$sql = "WHERE ".implode( " AND ", $quando);	
}


if (strlen($sql) < 3){
	$sql = "WHERE year(datafluxo) = Year(NOW())";
}


mysql_select_db($database_paivaconex, $paivaconex);
$query_maiores_faturamento = "Select Sum(vl_rec+Custas+Juros_rec) as vl, left(NomeFantasia,13) as cli_nome from dashboard Inner Join mfin_clientes ON Cliente = mfin_clientes.Id Left Join mfin_pastas ON Pasta = mfin_pastas.Id
  ".$sql." Group by mfin_clientes.Id order by Sum(vl_rec+Custas+Juros_rec) desc limit 5";
$maiores_faturamento = mysql_query($query_maiores_faturamento, $paivaconex) or die(mysql_error());
$row_maiores_faturamento = mysql_fetch_assoc($maiores_faturamento);
$totalRows_maiores_faturamento = mysql_num_rows($maiores_faturamento);

mysql_select_db($database_paivaconex, $paivaconex);
$query_valor_total = "Select Sum(vl_rec+Custas+Juros_rec) as vl from dashboard Inner Join mfin_clientes ON Cliente = mfin_clientes.Id Left Join mfin_pastas ON Pasta = mfin_pastas.Id ".$sql."";
$valor_total = mysql_query($query_valor_total, $paivaconex) or die(mysql_error());
$row_valor_total = mysql_fetch_assoc($valor_total);
$totalRows_valor_total = mysql_num_rows($valor_total);



	$valor = 0;
   $strXML  = "";
   $strXML .= "<chart palette='2' showBorder='0'>";
   
   
   do {   
   $strXML .= "<set label='".htmlentities($row_maiores_faturamento['cli_nome'], ENT_COMPAT, 'utf-8')."' value='".$row_maiores_faturamento['vl']."' />";
   $valor +=$row_maiores_faturamento['vl'];
   } while ($row_maiores_faturamento = mysql_fetch_assoc($maiores_faturamento));
   $outro = $row_valor_total['vl']-$valor;
   $strXML .= "<set label='Outros' value='".$outro."' />";
   
   
   $strXML .= "</chart>";



//  






echo renderChartHTML("FusionCharts_Enterprise/Charts/Doughnut3D.swf", "", "$strXML", "myNext", 599, 271, false);

mysql_free_result($maiores_faturamento);

mysql_free_result($valor_total);
?>
