<?php
include("seguranca.php"); // Inclui o arquivo com o sistema de seguran?a
protegePagina(); // Protege a página
header('Content-Type: text/html; charset=utf-8');

include("configuracao.php");//confirma no banco de dados
include("inclusos/funcoes.php");

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Home Elegance | Sistema de Gerenciamento</title>
	
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
    <!-- jQuery 2.1.3 -->
    <script src="plugins/jQuery/jQuery-2.1.3.min.js"></script>
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" />
    <script type="text/javascript" src="https://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>    
    <!-- Bootstrap 3.3.2 -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <!-- Font Awesome Icons -->
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
    <!-- Ionicons -->
    <link href="https://code.ionicframework.com/ionicons/2.0.0/css/ionicons.min.css" rel="stylesheet" type="text/css" />
    <!-- Morris chart -->
    <link href="plugins/morris/morris.css" rel="stylesheet" type="text/css" />
    <!-- jvectormap -->
    <link href="plugins/jvectormap/jquery-jvectormap-1.2.2.css" rel="stylesheet" type="text/css" />
    <!-- Daterange picker -->
    <link href="plugins/daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css" />
    <!-- Theme style -->
    <link href="dist/css/AdminLTE.min.css" rel="stylesheet" type="text/css" />
    <!-- AdminLTE Skins. Choose a skin from the css/skins 
         folder instead of downloading all of them to reduce the load. -->
    <link href="dist/css/skins/_all-skins.min.css" rel="stylesheet" type="text/css" />

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
	<script src="inclusos/script.js"></script>  
     
  </head>
  <body class="skin-blue">
    <div class="wrapper">
      
<?php
$idcliente = $_GET['idcliente'];

//LOG USUARIO
$SalvarLogUsuario = Salvar("logs_usuarios", "idusuario, acao, programa_origem, datacadastro, horacadastro", "'".$_SESSION['usuarioID']."', 'Impressão de Relatório Histórico de Cliente $idcliente', 'null', '".date('Y-m-d')."', '".date('H:i:s')."'");
//FIM LOGUSUARIO         
        
$mes = $_GET['mes'];

  $seleciona_cliente = mysql_query("SELECT * FROM clientes WHERE idcliente='$idcliente' ", $conexao) or die(mysql_error());
  while($dados_cliente = mysql_fetch_array($seleciona_cliente)){
  	$nome_cliente 	= utf8_encode($dados_cliente['nome']);
  }
?>


      <!-- Right side column. Contains the navbar and content of the page -->
      <div class="wrapper" style="background-color:#FFFFFF">
      <span style="font-size:16px; font-weight:bold;"><?php echo $nome_cliente;?></span>
      <table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
        <tr>
          <td width="10%" align="left" bgcolor="#CCCCCC">TIPO</td>
          <td width="10%" height="25" align="left" bgcolor="#CCCCCC">C&Oacute;D.</td>
          <td width="30%" align="left" bgcolor="#CCCCCC">TIT&Uacute;LO</td>
          <td width="20%" align="left" bgcolor="#CCCCCC">D&Eacute;BITO</td>
          <td width="20%" align="left" bgcolor="#CCCCCC">CR&Eacute;DITO</td>
          <td width="5%" align="left" bgcolor="#CCCCCC">DATA</td>
        </tr>
        <?php
  
  
  $cor_fundo = "#FFFFFF";
$datainicio = $_GET['datainicio'];
$datafim = $_GET['datafim'];
if($datainicio == "" and $datafim == ""){
$seleciona = mysql_query("SELECT * FROM contas WHERE idcliente='$idcliente' AND tipo_conta='Receber' ORDER BY data_conta ASC", $conexao) or die(mysql_error());    
$seleciona_pagamentos = mysql_query("SELECT * FROM pagamentos WHERE idcliente='$idcliente' ORDER BY data ASC", $conexao) or die(mysql_error()); 
}else{
$seleciona = mysql_query("SELECT * FROM contas WHERE idcliente='$idcliente' AND tipo_conta='Receber' AND data_conta BETWEEN ('$datainicio') AND ('$datafim') ORDER BY data_conta ASC", $conexao) or die(mysql_error());    
$seleciona_pagamentos = mysql_query("SELECT * FROM pagamentos WHERE idcliente='$idcliente' AND data BETWEEN ('$datainicio') AND ('$datafim') ORDER BY data ASC", $conexao) or die(mysql_error());  
}
	if($cor_fundo == "#FFFFFF"){
		$cor_fundo = "#DEF2FF";
	}else{
		$cor_fundo = "#FFFFFF";
	}  
  $saldo_conta = $pagamento['valor'] - $linha['valor_conta'];
  ?>
        <?php 
if($seleciona){
  while($linha=mysql_fetch_array($seleciona)){
	  
	  $venda_atual = $linha['idvenda'];
	  
	  $SelecionaObsVenda = Seleciona("vendas", "WHERE idvenda='$venda_atual'", "LIMIT 0,1");
	  while($venda_atual=mysql_fetch_array($SelecionaObsVenda)){
		$ObservacaoVenda = $venda_atual['observacao'];
	  }
	   
  $dataconta = $linha['datacadastro'];  
  if($linha['idconta'] <> $idconta){
  ?>
        <tr bgcolor="#FFC1C1"  style="border: #686868 1px solid;">
          <td align="left"><?php echo "Venda";?>.<?php echo $linha['idconta'];?></td>
          <td height="25" align="left"><?php echo $linha['idvenda'];?></td>
          <td align="left"><?php echo $linha['titulo_conta'] . " - " . $ObservacaoVenda;?><br>
            <?php echo utf8_encode($linha['observacao']);?></td>
          <td align="left"><?php if($linha['valor_conta'] > 0){ echo number_format($linha['valor_conta'], 2, ',', '.');}?></td>
          <td align="left"></td>
          <td align="left"><?php echo date("d/m/Y", strtotime($linha['data_conta']));?></td>
        </tr>
        <?php 
  }
 if($seleciona_pagamentos){
	while($pagamento=mysql_fetch_array($seleciona_pagamentos)){	
	$valor_pg = $pagamento['valor'];
	$datapagamento = $pagamento['datacadastro'];
  if($pagamento['idpagamento'] <> $idpagamento){
	  
	  $status_atual = $pagamento['status'];
	  
	  if($status_atual == "Aberto"){
		$cor_celula = "#FFE599";  
	  }else{
		$cor_celula = "#B0FFD0";		  
	  }
	  
  ?>
        <tr bgcolor="<?php echo $cor_celula;?>" style="border: #686868 1px solid;">
          <td align="left"><?php echo $pagamento['tipo'];?></td>
          <td height="25" align="left"><?php echo $pagamento['idpagamento'];?></td>
          <td align="left"><?php echo $pagamento['status'] . " - " . $pagamento['observacao_pagamento'];
	if($pagamento['tipo'] == "cheque"){
	echo "<br>Titular: " . $pagamento['titular_cheque'] . "<br>";
	echo "Data Comp: " . date("d/m/Y", strtotime($pagamento['data_compensacao'])) . "<br>";
	echo "N. Cheque: " . $pagamento['numero_cheque'] . "<br>";
	
	}	
	
	?></td>
          <td align="left"></td>
          <td align="left"><?php if($pagamento['valor'] > 0){ echo number_format($pagamento['valor'], 2, ',', '.');}?></td>
          <td align="left"><?php echo date("d/m/Y", strtotime($pagamento['data']));?></td>
        </tr>
        <?php 
  $soma_pagamento += $pagamento['valor'];
  } }}
  $valorconta = $linha['valor_conta'];
  $valorpagamento = $pagamento['valor'];
  if($linha['idconta'] <> $idconta){
  $soma_conta += $valorconta;
  }
  $datapagamento = "";
  $dataconta = "";
  $ObservacaoVenda = "";
  }
  }
  ?>
        <tr bgcolor="#DEF2FF">
          <td align="left">&nbsp;</td>
          <td height="25" align="left">&nbsp;</td>
          <td align="left">&nbsp;</td>
          <td align="left"><strong>R$
            <?php
		echo number_format($soma_conta, 2, ',', '.');
		?>
          </strong></td>
          <td align="left"><strong> R$
            <?php
		echo number_format($soma_pagamento, 2, ',', '.');
		?>
          </strong></td>
          <td align="left">&nbsp;</td>
        </tr>
        <tr bgcolor="#DEF2FF">
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td height="25" bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td align="right" bgcolor="#CCCCCC" style="font-size:18px"><strong>Compras</strong></td>
          <td align="center" bgcolor="#CCCCCC" style="font-size:18px"><strong>R$
            <?php
		echo number_format($soma_conta, 2, ',', '.');
		?>
            <?php
//TOTAL VALOR PAGO
if($datainicio == "" and $datafim == ""){
	$selec = "select sum(valor) AS soma FROM pagamentos WHERE idcliente='$idcliente' AND status='Pago'";
}else{
	$selec = "select sum(valor) AS soma FROM pagamentos WHERE idcliente='$idcliente' AND status='Pago' AND data BETWEEN ('$datainicio') AND ('$datafim')";
	
}
	$exec = mysql_query($selec, $conexao) or die(mysql_error());       
	while($areceberpago=mysql_fetch_array($exec)) {
	$pago = $areceberpago['soma'];	
	}	
	
//TOTAL VALOR ABERTO
if($datainicio == "" and $datafim== ""){
	$selec_aberto = "select sum(valor) AS soma_aberto FROM pagamentos WHERE idcliente='$idcliente' AND status='Aberto'";
}else{
	$selec_aberto = "select sum(valor) AS soma_aberto FROM pagamentos WHERE idcliente='$idcliente' AND status='Aberto' AND data BETWEEN ('$datainicio') AND ('$datafim')";	
}
	$exec_aberto = mysql_query($selec_aberto, $conexao) or die(mysql_error());       
	while($aberto=mysql_fetch_array($exec_aberto)) {
	$totalaberto = $aberto['soma_aberto'];	
	}		
?>
          </strong></td>
        </tr>
        <tr bgcolor="#DEF2FF">
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td height="25" bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td align="right" bgcolor="#CCCCCC" style="font-size:18px"><strong>Total Pago</strong></td>
          <td align="center" bgcolor="#CCCCCC" style="font-size:18px"><?php echo "R$ " . number_format($pago, 2, ',', '.'); ?></td>
        </tr>
        <tr bgcolor="#DEF2FF">
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td height="25" bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td align="right" bgcolor="#CCCCCC" style="font-size:18px"><strong>Total Em Aberto</strong></td>
          <td align="center" bgcolor="#CCCCCC" style="font-size:18px"><?php echo "R$ " . number_format($totalaberto, 2, ',', '.'); ?></td>
        </tr>
        <tr bgcolor="#DEF2FF">
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td height="25" bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td bgcolor="#CCCCCC">&nbsp;</td>
          <td align="right" bgcolor="#CCCCCC" style="font-size:18px"><strong>Saldo Cliente</strong></td>
          <td align="center" bgcolor="#CCCCCC" style="font-size:18px"><strong>
            <?php
		$total = $pago - $soma_conta;
		
		if($total > 0){
			echo " + R$ " . number_format($total, 2, ',', '.');
		}else{
			echo " - R$ " . number_format($total, 2, ',', '.');
		}
		?>
          </strong></td>
        </tr>
      </table>
      </div><!-- ./wrapper -->

</body>
</html>