<?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['fornecedor'])) {
		$quando[] = "Fornecedor IN (".implode( " ,",$_POST['fornecedor']).")";
	}
	if (isset($_POST['conta'])) {
		$quando[] = "mfin_fornecedores.ContaResultado IN (".implode( " ,",$_POST['conta']).")";
	}
	if (isset($_POST['grupo'])) {
		$quando[] = "mfin_grupocontas.Id IN (".implode( " ,",$_POST['grupo']).")";
	}
	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_clientes = "Select Sum(vl_pag+Juros_pag) as vl, mfin_pastas.Id as pasta_id, 
mfin_pastas.Nome as pasta_nome, mfin_pastas.Numero as pasta_num from dashboard
Inner Join mfin_clientes ON Cliente = mfin_clientes.Id
Left Join mfin_pastas ON Pasta = mfin_pastas.Id
Inner Join mfin_fornecedores ON Fornecedor = mfin_fornecedores.Id
Inner Join mfin_contas ON mfin_fornecedores.ContaResultado = mfin_contas.Id
Inner Join mfin_grupocontas ON Grupo = mfin_grupocontas.Id
".$sql."Group by mfin_pastas.Id order by Sum(vl_pag+Juros_pag) desc";
$clientes = mysql_query($query_clientes, $paivaconex) or die(mysql_error());
$row_clientes = mysql_fetch_assoc($clientes);
$totalRows_clientes = mysql_num_rows($clientes);
?>
<input name="todos_pastas" id="todos_pastas" type="checkbox" value="" />
Selecionar Todas as Pastas

<table width="1100" border="1">
  <tr bgcolor="#CCCCCC">
    <td width="195" height="22" align="center">Pasta</td>
    <td width="695" align="center">Nome</td>
    <td width="188" align="center">Valor</td>
  </tr>
</table>
<div style="overflow:auto; width:1190px; height:350px;">
<table width="1100" border="1">
  
    <?php do { ?>
      <tr>
        <td width="196" height="22" align="left"><input type="checkbox" name="pasta" id="checkbox" value="<?php echo htmlentities($row_clientes['pasta_id'], ENT_COMPAT, 'iso-8859-1'); ?>" />
        <?php echo htmlentities($row_clientes['pasta_num'], ENT_COMPAT, 'iso-8859-1'); ?></td>
        <td width="694" align="left"><?php echo htmlentities($row_clientes['pasta_nome'], ENT_COMPAT, 'iso-8859-1'); ?></td>
        <td width="188" align="right"><?php echo number_format($row_clientes['vl'],2,',','.'); ?></td>
      </tr>
      <?php } while ($row_clientes = mysql_fetch_assoc($clientes)); ?>
    
</table>
</div>
<?php mysql_free_result($clientes); ?>