<?php require_once('../../Connections/controle.php'); ?>
<?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;
}
}


mysql_select_db($database_controle, $controle);
$query_clientes = "SELECT * FROM clientes WHERE ativo = 'sim'";
$clientes = mysql_query($query_clientes, $controle) or die(mysql_error());
$row_clientes = mysql_fetch_assoc($clientes);
$totalRows_clientes = mysql_num_rows($clientes);

mysql_select_db($database_controle, $controle);
$query_pgto = "SELECT * FROM pagamentos WHERE MONTH(data) = MONTH(NOW())";
$pgto = mysql_query($query_pgto, $controle) or die(mysql_error());
$row_pgto = mysql_fetch_assoc($pgto);
$totalRows_pgto = mysql_num_rows($pgto);




        //Busca dos Números para a Soma
mysql_select_db($database_controle, $controle);
$query_clientes = mysql_query("SELECT sum(Valor_Mensal) FROM clientes WHERE ativo = 'sim' ");
while($sum = mysql_fetch_array($query_clientes)){
$soma = $sum['sum(Valor_Mensal)'];
}
       //Fim Busca dos Números para a Soma
	   

        //Busca dos Números para a Soma
mysql_select_db($database_controle, $controle);
$query_pgto = mysql_query("SELECT sum(valor) FROM pagamentos WHERE MONTH(data) = MONTH(NOW())");
while($sum = mysql_fetch_array($query_pgto)){
$subtrair = $sum['sum(valor)'];
}
       //Fim Busca dos Números para a Soma
	   
	           //Busca dos Números para a Soma
mysql_select_db($database_controle, $controle);
$query_pgto = mysql_query("SELECT sum(valor) -clientes.sum(Valor_Mensal) FROM pagamentos WHERE MONTH(data) = MONTH(NOW())");
while($sum = mysql_fetch_array($query_pgto)){
$pagamentos = $sum['sum(Valor_Mensal)'];
}
       //Fim Busca dos Números para a Soma
	   

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>

<link href="../css/estilos_clientes.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div id="controlmensal">
  <table width="559" cellspacing="10"  class="table">
    <tr>
      <td width="279">&nbsp;</td>
      <td width="279">&nbsp;</td>
      <td width="202">&nbsp;</td>
    </tr>
    <?php do { ?>
      <tr>
        <td><?php echo $row_clientes['Nome']; ?></td>
        <td align="right">R$&nbsp;</td>
        <td width="202" align="right"><?php echo $row_clientes['Valor_Mensal']; ?></td>
      </tr>
      <?php } while ($row_clientes = mysql_fetch_assoc($clientes)); ?>
<tr>
      <td>&nbsp;</td>
      <td colspan="2" align="right"><span>TOTAL R$ <?php echo number_format($soma, 2, ',', '.'); ?></span></td>
    </tr>
    <tr>
      <td colspan="3" align="right"><span>TOTAL R$ <?php echo number_format($subtrair, 2, ',', '.'); ?></span></td>
    </tr>
    <tr>
      <td colspan="3" align="right"><span>TOTAL R$ <?php echo number_format($pagamentos, 2, ',', '.'); ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</div>
</body>
</html>
<?php
mysql_free_result($clientes);

mysql_free_result($pgto);





?>
