<?php require_once('topo.php');?>
<?php require_once('lateral.php');?>
<?php

$data_inicio = $_GET['data_inicio'];    
$data_fim = $_GET['data_fim'];  

$seleciona = Seleciona("transacoes", "", "");
$Totalpedidos = mysql_num_rows($seleciona);

//VERIFICA SE TRANSAÇÃO TEM IDPACOTE E IDSAIDA

$SelecionaTransacoesPacotes = Seleciona("transacoes", "WHERE idpacote IS NULL", "");
$TotalTransacoesPacotes = mysql_num_rows($SelecionaTransacoesPacotes);
if($TotalTransacoesPacotes > 0){
    while($transacoes = mysql_fetch_array($SelecionaTransacoesPacotes)){
    //ATUALIZO TRANSAÇÃO
    //SELECIONO PEDIDO
        $SelecionaPedido = Seleciona("pedidos", "WHERE idpedido='".$transacoes['idpedido']."'", "");
        while($pedido = mysql_fetch_array($SelecionaPedido)){
            
            //ALTERO TRANSACAO
            $AlterarTransacao = Alterar("transacoes", "idcliente='".$pedido['idcliente']."', idpacote='".$pedido['idpacote']."', idsaida_hotel='".$pedido['idhotel']."'", "idtransacao", $transacoes['idtransacao']);            
        }
    }
}
?>
      <!-- Right side column. Contains the navbar and content of the page -->
      <div class="content-wrapper" style="<?php if($_GET['Box'] != ""){ echo "margin-left: 0px;"; }?>">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Transações
            <small>Total de <?php echo $Totalpedidos;?> transações</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="index.php?Box=<?php echo $_GET['Box'];?>"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="listar_transacoes.php?Box=<?php echo $_GET['Box'];?>">Transações</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="row">			    
                <form method="get" action="listar_transacoes.php">                
                <input type="hidden" name="Box" value="<?php echo $_GET['Box'];?>">	
                <input type="hidden" name="Filtro" value="<?php echo $_GET['Filtro'];?>">	
                <div class='col-xs-4'>
                <label for='nome'>Selecione Pacote</label>
                <select name="idpacote" class="form-control">
                <option value="">Selecione um Pacote</option>
                <?php 
                $seleciona_pacote = Seleciona("pacotes", "WHERE excluido IS NULL", "ORDER BY titulo ASC");                    
                while($pacote=mysql_fetch_array($seleciona_pacote)){	
                ?>
                <option value="<?php echo $pacote['idpacote'];?>" <?php if($pacote['idpacote'] == $_GET['idpacote']){ echo "selected";}?>><?php echo $pacote['titulo'];?></option>	
                <?php }?>	
                </select>				
                </div>

                <div class='col-xs-4'>
                <label for='nome'>Selecione uma Saída</label>
                <select name="idsaida_hotel" class="form-control">
                <option value="">Selecione uma Saída</option>
                <?php 
                $seleciona_saida = Seleciona("saida_hotel", "WHERE excluido IS NULL", "ORDER BY idsaida_hotel DESC"); 

                if($_GET['idpacote'] > 0){
                $seleciona_saida = Seleciona("saida_hotel", "WHERE idpacote='".$_GET['idpacote']."' AND excluido IS NULL", "ORDER BY titulo ASC"); 
                }	

                while($saida=mysql_fetch_array($seleciona_saida)){	
                ?>
                <option value="<?php echo $saida['idsaida_hotel'];?>" <?php if($saida['idsaida_hotel'] == $_GET['idsaida_hotel']){ echo "selected";}?>><?php echo $saida['titulo'];?></option>	
                <?php }?>	
                </select>				
                </div>				
                <div class='col-xs-4'>
                <br>	
                <button type="submit" class="btn btn-primary">Filtrar</button>
                </div>	
                </form>    
                </div>    
                <br>
                <div class="row">    
                <form method="get" action="listar_transacoes.php">
                <input type="hidden" name="Box" value="<?php echo $_GET['Box'];?>">	
                <input type="hidden" name="Filtro" value="<?php echo $_GET['Filtro'];?>">	
                <input type="hidden" name="idpacote" value="<?php echo $_GET['idpacote'];?>">	
                <input type="hidden" name="idsaida_hotel" value="<?php echo $_GET['idsaida_hotel'];?>">	
                <input type="hidden" name="Categoria" value="DataPedido">	
                <div class='col-xs-4'>
                <label for='data_inicio'>Data Inicio</label>
                <input type="date" name="data_inicio" class="form-control" value="<?php echo $data_inicio;?>">			
                </div>

                <div class='col-xs-4'>
                <label for='data_fim'>Data Fim</label>
                <input type="date" name="data_fim" class="form-control" value="<?php echo $data_fim;?>">			
                </div>
                <div class='col-xs-4'>
                <br>	
                <button type="submit" class="btn btn-primary">Filtrar Data Pedido</button>
                </div>	
                </form>                
                </div>    
                <br><br><br><br>   
              <div class="box">
                  
                <div class="box-header">
                  <h3 class="box-title">Listagem de Transações</h3>
                    
                    <button type="button" class="btn btn-primary" style=" float:right;" onclick="location.href='listar_transacoes.php?Box=<?php echo $_GET['Box'];?>';">Todos</button>
					<button type="button" class="btn btn-vk" style=" float:right;" onclick="location.href='listar_transacoes.php?Filtro=Aprovado&Box=<?php echo $_GET['Box'];?>';">Aprovados</button>
					<button type="button" class="btn btn-primary" style=" float:right;" onclick="location.href='listar_transacoes.php?Filtro=Reprovado&Box=<?php echo $_GET['Box'];?>';">Reprovado</button>
                    
                </div><!-- /.box-header -->
                <div class="box-body">
                  <table id="tabela_pedido" class="table table-bordered table-striped">
                    <thead>
                      <tr>
                        <th style="cursor:pointer">ID.</th>
                        <th style="cursor:pointer">PEDIDO</th>
                        <th style="cursor:pointer">CÓD</th>
                        <th style="cursor:pointer">BANDEIRA</th>
                        <th style="cursor:pointer; width: 100px;">MSG</th>                                             
                        <th style="cursor:pointer">NUMCV</th>                        
                        <th style="cursor:pointer">NUMSQN</th>                          
                        <th style="cursor:pointer">TOTAL</th>
                        <th style="cursor:pointer">PARCELAS</th>
                        <th style="cursor:pointer">PORTADOR</th>
                        <th style="cursor:pointer">STATUS</th>
                        <th style="cursor:pointer">DATA</th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php

                        
					if($_GET['Filtro'] == ""){
				
                    if($data_inicio != "" and $data_fim != ""){    
					    $Seleciona = Seleciona("transacoes", "WHERE DATA BETWEEN ('$data_inicio') AND ('$data_fim') ", "ORDER BY DATA DESC");
                    }else{
                        $Seleciona = Seleciona("transacoes", "", "ORDER BY DATA DESC");
                    }
                        
					if($_GET['idpacote'] > 0 and $_GET['idsaida_hotel'] == ""){
                    if($data_inicio != "" and $data_fim != ""){    
					   $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."' AND DATA BETWEEN ('$data_inicio') AND ('$data_fim')", "ORDER BY DATA DESC");						
                    }else{
                        $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."'", "ORDER BY DATA DESC");						
                    }
					}
						
					if($_GET['idpacote'] == "" and $_GET['idsaida_hotel'] > 0){	
                    if($data_inicio != "" and $data_fim != ""){      
					   $Seleciona = Seleciona("transacoes", "WHERE idsaida_hotel='".$_GET['idsaida_hotel']."' AND DATA BETWEEN ('$data_inicio') AND ('$data_fim')", "ORDER BY DATA DESC");						
                    }else{
                        $Seleciona = Seleciona("transacoes", "WHERE idsaida_hotel='".$_GET['idsaida_hotel']."'", "ORDER BY DATA DESC");						
                    }
					}	
						
					if($_GET['idpacote'] > 0 and $_GET['idsaida_hotel'] > 0){	
                    if($data_inicio != "" and $data_fim != ""){       
					   $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."' AND idsaida_hotel='".$_GET['idsaida_hotel']."'  AND DATA BETWEEN ('$data_inicio') AND ('$data_fim')", "ORDER BY DATA DESC");						
                    }else{
                        $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."' AND idsaida_hotel='".$_GET['idsaida_hotel']."'", "ORDER BY DATA DESC");						
                    }
					}		
						
					}
                        
                        
                        
					if($_GET['Filtro'] != ""){
				
                    if($data_inicio != "" and $data_fim != ""){    
					    $Seleciona = Seleciona("transacoes", "WHERE DATA BETWEEN ('$data_inicio') AND ('$data_fim') AND status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");
                    }else{
                        $Seleciona = Seleciona("transacoes", "WHERE status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");
                    }
                        
					if($_GET['idpacote'] > 0 and $_GET['idsaida_hotel'] == ""){
                    if($data_inicio != "" and $data_fim != ""){    
					   $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."' AND DATA BETWEEN ('$data_inicio') AND ('$data_fim') AND status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");						
                    }else{
                        $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."' AND status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");						
                    }
					}
						
					if($_GET['idpacote'] == "" and $_GET['idsaida_hotel'] > 0){	
                    if($data_inicio != "" and $data_fim != ""){      
					   $Seleciona = Seleciona("transacoes", "WHERE idsaida_hotel='".$_GET['idsaida_hotel']."' AND DATA BETWEEN ('$data_inicio') AND ('$data_fim') AND status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");						
                    }else{
                        $Seleciona = Seleciona("transacoes", "WHERE idsaida_hotel='".$_GET['idsaida_hotel']."' AND status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");						
                    }
					}	
						
					if($_GET['idpacote'] > 0 and $_GET['idsaida_hotel'] > 0){	
                    if($data_inicio != "" and $data_fim != ""){       
					   $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."' AND idsaida_hotel='".$_GET['idsaida_hotel']."'  AND DATA BETWEEN ('$data_inicio') AND ('$data_fim') AND status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");						
                    }else{
                        $Seleciona = Seleciona("transacoes", "WHERE idpacote='".$_GET['idpacote']."' AND idsaida_hotel='".$_GET['idsaida_hotel']."' AND status='".$_GET['Filtro']."'", "ORDER BY DATA DESC");						
                    }
					}		
						
					}                        
										
						
					while($linha = mysql_fetch_array($Seleciona)){						
					
					$SelecionaCliente = Seleciona("clientes", "WHERE idcliente='".$linha['idcliente']."'", "LIMIT 0,1");
					while($cliente = mysql_fetch_array($SelecionaCliente)){
						$NomeCliente = 	$cliente['nome'];
					}
					
					$SelecionaPacote = Seleciona("pacotes", "WHERE idpacote='".$linha['idpacote']."'", "LIMIT 0,1");
					while($pacote = mysql_fetch_array($SelecionaPacote)){
						$NomePacote = 	$pacote['titulo'];
					}
						
					$SelecionaHotel = Seleciona("saida_hotel", "WHERE idsaida_hotel='".$linha['idhotel']."'", "LIMIT 0,1");
					while($pacote = mysql_fetch_array($SelecionaHotel)){
						$NomeHotel = 	$pacote['titulo'];
					}
                        
                    $novadata = implode("/",array_reverse(explode("-",$linha['DATA'])));    
					?>
					
											
                      <tr style="cursor:pointer; font-size:11px;">
                        <td><?php echo $linha['idtransacao'];?></td>                      
                        <td><a href="manutencao_pedidos.php?idpedido=<?php echo $linha['idpedido'];?>&Box=<?php echo $_GET['Box'];?>"><?php echo $linha['idpedido'];?></a></td>
                        <td><?php echo $linha['CODRET'];?></td>
                        <td><?php echo $linha['bandeira'];?></td>
                        <td><?php echo $linha['MSGRET'];?></td>                                              
                        <td><?php echo $linha['NUMCV'];?></td>                        
                        <td><?php echo $linha['NUMSQN'];?></td>
                        <td><?php echo $linha['TOTAL'];?></td>
                        <td><?php if($linha['PARCELAS'] == "00"){ echo "1";}else{ echo $linha['PARCELAS'];}?></td>
                        <td><?php echo $linha['PORTADOR'];?></td>                                                
                        <td><?php echo $linha['status'];?></td>                                                
                        <td data-search='<?php echo $novadata;?>' data-order='<?php echo $linha['DATA'];?>'><strong><?php echo $novadata;?></strong></td>                      
                      </tr>
                      <?php 	
                    $novadata = "";    
					  }?>
                    </tbody>
                      
                  </table>
                </div><!-- /.box-body -->
              </div><!-- /.box -->
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
<?php require_once('rodape.php');?>
