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

<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[] = "dashboard2.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_fluxo = "Select Sum(vl_rec-vl_pag+Juros_rec-Juros_pag+Custas+Impostos_rec) as saldo, DATE_FORMAT(datafluxo, '%m/%Y') as mes from dashboard2 Inner Join mfin_clientes ON Cliente = mfin_clientes.Id Left Join mfin_pastas ON Pasta = mfin_pastas.Id ".$sql." Group by DATE_FORMAT(datafluxo, '%m/%Y') Order by datafluxo";
$fluxo = mysql_query($query_fluxo, $paivaconex) or die(mysql_error());
$row_fluxo = mysql_fetch_assoc($fluxo);
$totalRows_fluxo = mysql_num_rows($fluxo);



   $strXML  = "";
   $strXML .= "<chart caption='Fluxo Mensal' xAxisName='Mes' yAxisName='Valor'  formatNumber='1' decimalSeparator=',' decimals='2' thousandSeparator='.' showValues='0'  formatNumberScale='0' showBorder='0' alternateHGridColor='FCB541' alternateHGridAlpha='20' divLineColor='FCB541' divLineAlpha='50' canvasBorderColor='666666' baseFontColor='666666' lineColor='FCB541' >";
   
   do {   
   
   $strXML .="<set label='".$row_fluxo['mes']."' value='". $row_fluxo['saldo'] ."'/>";
          
   } while ($row_fluxo = mysql_fetch_assoc($fluxo));
   
   $strXML .="<styles>
<definition>
<style name='Anim1' type='animation' param='_xscale' start='0' duration='1'/>
<style name='Anim2' type='animation' param='_alpha' start='0' duration='0.6'/>
<style name='DataShadow' type='Shadow' alpha='40'/>
</definition>
<application>
<apply toObject='DIVLINES' styles='Anim1'/>
<apply toObject='HGRID' styles='Anim2'/>
<apply toObject='DATALABELS' styles='DataShadow,Anim2'/>
</application>
</styles>";
   
   
   $strXML .= "</chart>";


	
//  




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


mysql_free_result($fluxo);
?>
