<?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");


//LOG USUARIO
$SalvarLogUsuario = Salvar("logs_usuarios", "idusuario, acao, programa_origem, datacadastro, horacadastro", "'".$_SESSION['usuarioID']."', 'Impressão de Vendas por Vendedor', 'null', '".date('Y-m-d')."', '".date('H:i:s')."'");
//FIM LOGUSUARIO      

?>
<!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">
      <!-- Right side column. Contains the navbar and content of the page -->
      <div class="">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Vendas e Saldo de Clientes por Vendedor - <?php echo $_GET['vendedor'];?>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="#">Vendas e Saldo de Clientes por Vendedor</a></li>
            <li class="active">Listagem</li>
          </ol>
        </section>

        <!-- Main content -->
        <section class="content">
          <div class="row">
            <div class="col-xs-12"><!-- /.box -->             

              <div class="box">
                <div class="box-header">
                  <h3 class="box-title">Vendas e Saldo de Clientes por Vendedor</h3>
                </div><!-- /.box-header -->                 
                <div class="box-body">
                <?php
				if($_GET['idusuario'] != ""){
				?>
                  <table id="example1" class="table table-bordered table-striped">
                    <thead>
                      <tr>
                        <th style="cursor:pointer">Cliente</th>
                        <th style="cursor:pointer">Total Vendas / Débitos em Aberto</th>                        
                        <th style="cursor:pointer">Total de Pagamentos</th> 
                        <th style="cursor:pointer">Saldo</th>                         
                      </tr>
                    </thead>
                    <tbody>
					<?php
					$datainicio = $_GET['datainicio'];
					$datafim = $_GET['datafim'];

					if($_GET['idusuario'] != ""){
						//echo "estou 3";
						$Seleciona = Seleciona("clientes", "WHERE idvendedor='".$_GET['idusuario']."'", "");
					}else{
						//echo "estou 4";
						$Seleciona = Seleciona("clientes", "", "");
					}
					//echo  mysql_num_rows($Seleciona);
					$TotalVendaFinal = "";
					$TotalPagoFinal = "";
					while($clientes = mysql_fetch_array($Seleciona)){
                    
					$idcliente = $clientes['idcliente'];
					
					//NOVO TOTAL DE VENDAS
					$datainicio = $_GET['datainicio'];
					$datafim = $_GET['datafim'];
					if($datainicio != "" and $datafim != ""){					
					
					$selec_vendas = "select sum(valor_conta) AS soma_vendas FROM contas WHERE idcliente='$idcliente' AND tipo_conta='Receber' AND data_conta BETWEEN ('$datainicio') AND ('$datafim')";
					}else{
					$selec_vendas = "select sum(valor_conta) AS soma_vendas FROM contas WHERE idcliente='$idcliente' AND tipo_conta='Receber'";	
					}
					$exec_vendas = mysql_query($selec_vendas, $conexao) or die(mysql_error());       
					while($vendas=mysql_fetch_array($exec_vendas)) {
					$ValorTotalFinal = $vendas['soma_vendas'];	
															
					}											
					
					//PEGO VALOR TOTAL DE PAGAMENTOS			
					if($datainicio != "" and $datafim != ""){	

					$selec_pagamentos = "select sum(valor) AS soma_pagamentos FROM pagamentos WHERE idcliente='$idcliente' AND data BETWEEN ('$datainicio') AND ('$datafim')";
					//$selec_pagamentos = "select sum(valor) AS soma_pagamentos FROM pagamentos WHERE idcliente='$idcliente' AND tipo_conta='Receber' AND status='Pago' AND data BETWEEN ('$datainicio') AND ('$datafim')";
					}else{
					$selec_pagamentos = "select sum(valor) AS soma_pagamentos FROM pagamentos WHERE idcliente='$idcliente'";	
					
					//$selec_pagamentos = "select sum(valor) AS soma_pagamentos FROM pagamentos WHERE idcliente='$idcliente' AND tipo_conta='Receber' AND status='Pago'";	
					}
					$exec_pagamentos = mysql_query($selec_pagamentos, $conexao) or die(mysql_error());       
					while($pagamentos=mysql_fetch_array($exec_pagamentos)) {
					$ValorTotalPagamentos = $pagamentos['soma_pagamentos'];	
															
					}						

					$SaldoAtual = $ValorTotalPagamentos - $ValorTotalFinal;
					
					if($SaldoAtual != "0"){
                    ?>
                      <tr>
                        <td><?php echo utf8_encode($clientes['nome']);?></td>
                        <td><?php if($ValorTotalFinal > 0){ echo "R$ " . number_format($ValorTotalFinal, 2, ',', '.');}?></td>
                        <td><?php if($ValorTotalPagamentos > 0){ echo "R$ " . number_format($ValorTotalPagamentos, 2, ',', '.');}?></td> 
                        <td><?php echo "R$ " . number_format($SaldoAtual, 2, ',', '.');?></td>                       
                      </tr>
                      <?php 
					  $TotalVendaFinal = $ValorTotalFinal + $TotalVendaFinal;
					  $TotalPagoFinal = $ValorTotalPagamentos + $TotalPagoFinal;
					  $ValorTotalFinal = "";
					  $ValorTotalPagamentos = "";
					  $SaldoAtual = "";
					  $idcliente = "";

					}
					  }
					  
					  ?>
                    </tbody>
                    <tfoot>
                      <tr>
                        <th style="cursor:pointer">Cliente</th>
                        <th style="cursor:pointer"><?php if($TotalVendaFinal > 0){ echo "R$ " . number_format($TotalVendaFinal, 2, ',', '.');}?> </th>                        
                        <th style="cursor:pointer"><?php if($TotalPagoFinal > 0){ echo "R$ " . number_format($TotalPagoFinal, 2, ',', '.');}?></th> 
                        <th style="cursor:pointer"><?php if($TotalPagoFinal > 0 and  $TotalVendaFinal > 0 ) {echo "R$ " . number_format($TotalPagoFinal - $TotalVendaFinal, 2, ',', '.');}?></th>                    
                      </tr>
                    </tfoot>
                  </table>
                  <?php }?>
                </div><!-- /.box-body -->
              </div><!-- /.box -->
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
<?php //require_once('rodape.php');?>
  </body>
</html>