<?php
error_reporting(0);
ini_set("display_errors", 0 );
header('Content-Type: text/html; charset=utf-8');
include("../configuracao.php");//confirma no banco de dados
include("../inclusos/funcoes.php");
include("../seguranca.php"); // Inclui o arquivo com o sistema de seguran?a
protegePagina(); // Protege a página
?>
<!doctype html>
<html ng-app="myApp">
<head>
<title>RELATÓRIO DE PROPOSTAS</title>

<link rel="stylesheet" href="inclusos/styles.css">
<link href="inclusos/css_precopratico.css" rel="stylesheet" type="text/css">
<style type="text/css">
@media print {
  #naoimprimir {
    visibility: hidden;
  }
  #printable, #printable * {
    visibility: visible;
  }
}
        </style> 


</head>
<body>
<?php

//SELECIONA E BUSCA

$busca = $_GET['busca'];
$datainicio = $_GET['datainicio'];
$datafim = $_GET['datafim'];
$status_orcamento = $_GET['status_orcamento'];

$cor_fundo = "#FFFFFF";			  
  

		$seleciona_vendas = Seleciona("vendas", "", "ORDER BY dataorcamento DESC LIMIT 0,50");
		
		//APENAS BUSCA
		if($busca != "" and $datainicio != "" and $datafim != "" and $status_orcamento == ""){
			$seleciona_vendas = Seleciona("vendas", "WHERE titulo LIKE '%".$busca."%' OR idvenda LIKE '%".$busca."%' OR idorcamento LIKE '%".$busca."%' OR dataorcamento LIKE '%".InverteDataNovoSeparador($busca, "/","-")."%' AND dataorcamento BETWEEN ('$datainicio') AND ('$datafim')", "ORDER BY dataorcamento DESC");
		}
		
		//APENAS STATUS
		if($busca == "" and $datainicio != "" and $datafim != "" and $status_orcamento != ""){
			$seleciona_vendas = Seleciona("vendas", "WHERE status='$status_orcamento' AND dataorcamento BETWEEN ('$datainicio') AND ('$datafim')", "ORDER BY dataorcamento DESC");
		}		
		
		//APENAS DATA INICIO E DATA FIM
		if($busca == "" and $datainicio != "" and $datafim != "" and $status_orcamento == ""){
			$seleciona_vendas = Seleciona("vendas", "WHERE dataorcamento BETWEEN ('$datainicio') AND ('$datafim')", "ORDER BY dataorcamento DESC");
		}				


$TotalVendas = mysql_num_rows($seleciona_vendas);
?>
<?php if($TotalVendas == 0){ ?>
<div class="window" style=" background-color: #FFFFFF; width: 300px; height: 150px; top:50%; margin-top:-75px; left:50%; margin-left:-150px; display: flex; border:#C0C0C0 solid 1px;">
<table width="100%" height="100%">
	<tr>
	  <td height="20" align="center" valign="center" class="TextoCinzaEscuro">&nbsp;</td>
	  <td align="right" valign="center" class="TextoCinzaEscuro"><a href="#" onclick="history.go(-1);"><img src="img/icone_excluir.gif" width="24" height="24" alt="" border="0"></a></td>
    </tr>
	<tr>
	  <td colspan="2" align="center" valign="center" class="TextoCinzaEscuro"><strong>Nenhum resultado foi encontrado para sua busca. </strong></td>
      </tr>
      <tr>
      <td align="center" colspan="2">
        <table width="100" border="0" align="center" cellpadding="2" cellspacing="0" onclick="history.go(-1);" class="BoxMenuCinza" style="margin: 10px;">
        <tr>
        <td height="25" align="center" class="TextoCinzaGrande" >Voltar</td>
        </tr>
        </table>              
      </td>
    </tr>
</table> 
</div>
<?php }?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="50" colspan="4" align="left" valign="middle">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="naoimprimir"  >
      <tr>
        <td width="80" height="40" class="TituloPagina" style="padding-left: 5px;"><a href="index.php"><img src="img/logobranco.fw.png" width="200" height="46" id="printable"></a></td>
        <td align="left" class="TituloPagina" style="padding-left: 5px;"><span class="TituloPagina" style="padding-left: 5px; text-align:right" id="printable">OBRAS</span></td>
        <td align="right">
          
          <form method="get" action="relatorio_obras.php" enctype="multipart/form-data">
            <table width="100%" border="0" align="center" cellpadding="2" cellspacing="2" >
              <tr>
                <td align="center"><strong>DATA</strong></td>
                <td align="left"><input name="datainicio" type="date" class="InputBranco" id="datainicio" value="<?php echo $_GET['datainicio'];?>" placeholder="Data Inicio" style="width: 100%; height: 30px; background-color: #D3D3D3;" required /></td>
                <td align="left"><input name="datafim" type="date" class="InputBranco" id="datafim" value="<?php echo $_GET['datafim'];?>" placeholder="Data Fim" style="width: 100%; height: 30px; background-color: #D3D3D3;" required /></td>
                <td align="center"><strong>BUSCA</strong></td>
                <td width="370" align="right"><table width="100%" border="0" align="right" cellpadding="0" cellspacing="0" class="BoxMenuBranco">
                  
                  <tr>
                    <td width="148" align="left" bgcolor="#FFFFFF"><input name="busca" type="text" class="InputBranco" id="busca" placeholder="Titulo Projeto" style="width: 100%; height: 30px; background-color: #D3D3D3;" /></td>
                    <td width="28" align="center" bgcolor="#FFFFFF"><input type="image" src="img/icone_buscar.gif" /></td>
                    </tr>
                  
                  </table>
                  
                  </td>
                </tr>
              </table>
            </form>
        </td>
        </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td colspan="4" align="center" valign="top"><table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
      <tr class="TextoCinzaEscuro">
        <td bgcolor="#A6A6A6" class="TextoCinzaEscuro"><strong>CÓD.</strong></td>
        <td height="30" bgcolor="#A6A6A6" class="TextoCinzaEscuro"><strong>TITÚLO</strong></td>
        <td align="left" bgcolor="#A6A6A6"><strong>CLIENTE</strong></td>
        <td align="left" bgcolor="#A6A6A6"><strong>DATA INICIO</strong></td>
        <td align="left" bgcolor="#A6A6A6"><strong>DATA FIM</strong></td>
        <td align="left" bgcolor="#A6A6A6"><strong>STATUS</strong></td>
        <td align="center" bgcolor="#A6A6A6"><strong>CONCLUSÃO</strong></td>
        </tr>
      <?php
			$busca = $_GET['busca'];
			$datainicio = $_GET['datainicio'];
			$datafim = $_GET['datafim'];
			$cor_fundo = "#FFFFFF";			  
		  if(empty($busca)){
			 	$seleciona = Seleciona("obras", "", "ORDER BY idobra DESC");
		  }else{
			  	$seleciona = Seleciona("obras", "WHERE titulo LIKE '%".$busca."%'", "ORDER BY idobra DESC");
		  }
		  
		//APENAS DATA INICIO E DATA FIM
		if($busca == "" and $datainicio != "" and $datafim != "" and $status_orcamento == ""){
			$seleciona = Seleciona("obras", "WHERE datainicio BETWEEN ('$datainicio') AND ('$datafim')", "ORDER BY idobra DESC");
		}			  
		  
			while($linha=mysql_fetch_array($seleciona)){
				$id_obra = $linha['idobra'];
				$id_cliente = $linha['idcliente'];
				
				//SELECIONA NOME CATEGORIA
				
			 	$SelecionaCliente = Seleciona("clientes", "WHERE CodCli='$id_cliente'", "");	
				while($clientes=mysql_fetch_array($SelecionaCliente)){
					$NomeCliente = $clientes['Fantasia'];
				}
			
			
			if($cor_fundo == "#FFFFFF"){
				$cor_fundo = "#E4E4E4";
			}else{
				$cor_fundo = "#FFFFFF";
			}
			
			//CALCULA QTDE DE SERVIÇOS
			
			$SelecionaServicosTotal = Seleciona("obras_servicos", "WHERE idobra='$id_obra'", "");	
			$TotalServico = mysql_num_rows($SelecionaServicosTotal);

			$SelecionaServicosConcluido = Seleciona("obras_servicos", "WHERE idobra='$id_obra' AND status='Concluido'", "");	
			$TotalServicoConcluido = mysql_num_rows($SelecionaServicosConcluido);			
			
			$PercentualAtual = 100 - ((($TotalServico - $TotalServicoConcluido) / $TotalServico) * 100);
			$PercentualFinal = intval($PercentualAtual);
			
			if($PercentualFinal == 100){
				$cor_percentual = "#7AE846";
			}else{
				$cor_percentual = "#BD3739";
			}
		?>
      <tr class="TextoCinzaPequeno" bgcolor="<?php echo $cor_fundo;?>" onmouseover="javascript:this.style.backgroundColor='#CCCCCC'" onmouseout="javascript:this.style.backgroundColor='<?php echo $cor_fundo;?>'" style="cursor:pointer;">
        <td ><?php echo $linha['idobra'];?></td>
        <td height="30"><?php echo $linha['titulo'];?></td>
        <td align="left"><?php echo $NomeCliente;?></td>
        <td align="left"><?php echo InverteDataNovoSeparador($linha['datainicio'], "-", "/");?></td>
        <td align="left"><?php echo InverteDataNovoSeparador($linha['datafim'], "-", "/");?></td>
        <td align="left"><?php echo $linha['status'];?></td>
        <td align="center" valign="middle"><?php echo $PercentualFinal;?>%</td>
        </tr>
      <?php 
		$PercentualAtual = "";
		}?>
    </table></td>
  </tr>
  <tr>
    <td colspan="5" align="center" valign="middle">
</td>
  </tr>
</table>
</body>
</html>