<?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[] = "dashboard2.Detalhe IN ('".implode( "' ,'",$_POST['detalhe'])."')";
	}
	$sql = "WHERE ".implode( " AND ", $quando);	
}

mysql_select_db($database_paivaconex, $paivaconex);
$query_clientes = "Select dashboard2.Detalhe, mfin_clientes.Id as cli_id, Sum(vl_rec - vl_pag+Custas+Impostos_rec) as vl, Sum(Juros_rec - Juros_pag)  as juros, NomeFantasia as cli_nome, mfin_pastas.Numero as numpasta, DATE_FORMAT(Emissao, '%d/%m/%Y') as recebimento from dashboard2 Inner Join mfin_clientes ON Cliente = mfin_clientes.Id Left Join mfin_pastas ON Pasta = mfin_pastas.Id ".$sql." GROUP BY mfin_clientes.Id, dashboard2.Detalhe, mfin_pastas.Id  order by mfin_pastas.Numero, Sum(vl_rec-vl_pag) desc";
$clientes = mysql_query($query_clientes, $paivaconex) or die(mysql_error());
$row_clientes = mysql_fetch_assoc($clientes);
$totalRows_clientes = mysql_num_rows($clientes);
?>
<style type="text/css" media="print">
div.conteudodatabela {overflow: visible;	}
table>tbody	{overflow: visible; }
td {height: 14pt;} /*adds control for test purposes*/
thead td	{font-size: 11pt;	}
tfoot td	{
	text-align: center;
	font-size: 9pt;
	border-bottom: solid 1px slategray;
	}
	
thead	{display: table-header-group;	}
tfoot	{display: table-footer-group;	}
thead th, thead td	{position: static; } 

thead tr	{position: static; } /*prevent problem if print after scrolling table*/ 
table tfoot tr {     position: static;    }


</style>

<style type="text/css">

@page { size: A4 landscape }

</style>

<table width="1100" border="1">
  <tr bgcolor="#CCCCCC" style="font-size: 12px">
    
    <th  height="22" align="center">Pasta</th>
    <th  height="22" align="center">Cliente</th>
    <th  height="22" align="center">Detalhe</th>
    <th  align="center">Valor</th>
  </tr>
  <?php
	$juros = 0;  
   do { ?>
  <tr style="font-size: 12px" >
    
    <td  height="22" align="center"><?php echo $row_clientes['numpasta']; ?></td>
    <td  height="22" align="center"><?php echo htmlentities($row_clientes['cli_nome'], ENT_COMPAT , "utf-8"); ?></td>
    <td  height="22" align="center"><?php echo htmlentities($row_clientes['Detalhe'], ENT_COMPAT , "utf-8"); ?></td>
    <td  align="right"><?php echo number_format($row_clientes['vl'],2,',','.'); ?></td>
  </tr>
  
  <?php 
  $juros += $row_clientes['juros'];
  } while ($row_clientes = mysql_fetch_assoc($clientes)); ?>
  <tr>
      <td  colspan="4" height="22" align="left">Receita Financeira Liquida</td>
        
        <td  align="right"><?php echo number_format($juros,2,',','.'); ?></td>
    </tr>
  
</table>
<?php mysql_free_result($clientes); 
echo "<script language=javascript> window.print()</script>"; 
?>
