<?php

include "../../funcs.php";

$linhap=12;
//$_REQUEST['cod'] = "0";
if (isset($_REQUEST['cod']))
{
  if (intval($_REQUEST['cod'])==0) # pagina 0
  {
	$filtro='';
	$sql="SELECT D.DOCCAB_SEQDOC C0,
				D.DOCTIP_CODIGO C1,
				D.DOCCAB_LOCALORIGEM C2,
				D.DOCCAB_LOCALDESTINO C3,
				D.DOCCAB_DATA C4,
				D.DOCCAB_NUMERO C5,
				D.DOCCAB_NUMEROEXT C6,
				D.DOCCAB_LOJA C7,
				D.DOCCAB_NUMEROSUBST C8,
				D.DOCCAB_NUMEROPEDIDO C9,
				D.DOCCAB_BACKORDER C10,
				D.DOCCAB_ESTILO C11,
				D.DOCCAB_LOCALIMPRESSAO C12,
				D.DOCCAB_RAZAOSOCIAL C13,
				D.DOCCAB_CNPJ C14,
				D.DOCCAB_NUMEROSEMANA C15,
				D.DOCCAB_TIPOETIQUETA C16,
				D.DOCCAB_LAYOUTETIQUETA C17,
				D.DOCCAB_TIPOEVENTO C18,
				D.DOCCAB_DATALIMITE C19,
				D.DOCCAB_DATAEMISSAO C20,
				S.DOCSTS_DESCRICAO C21,
				D.DOCCAB_SEQ C22,
				D.DOCCAB_QTDREIMPR C23, 
				D.DOCCAB_STATUS C24 
			FROM doccab D
		LEFT JOIN docsts S ON (D.DOCCAB_STATUS = S.DOCCAB_STATUS)
		WHERE D.DOCTIP_CODIGO
		IN (SELECT DOCTIP_CODIGO
				FROM doctip
					WHERE DOCTIP_MOVIMENTO = 070)
					AND D.EMPRES_CODIGO='".$_SESSION['Pempresa']."'
					AND D.FILIAL_CODIGO='".$_SESSION['Pfilial']."'
					AND D.DOCCAB_STATUS <= 9";
       $ou = " AND (";
       if (isset($_REQUEST['doccab_seqdoc']) and (strlen($_REQUEST['doccab_seqdoc'])>0))
       {
          $sql=$sql." AND D.DOCCAB_SEQDOC='".$_REQUEST['doccab_seqdoc']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doctip_codigo']) and (strlen($_REQUEST['doctip_codigo'])>0))
       {
          $sql=$sql." AND D.DOCTIP_CODIGO='".$_REQUEST['doctip_codigo']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_localorigem']) and (strlen($_REQUEST['doccab_localorigem'])>0))
       {
          $sql=$sql." AND D.DOCCAB_LOCALORIGEM='".$_REQUEST['doccab_localorigem']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_localdestino']) and (strlen($_REQUEST['doccab_localdestino'])>0))
       {
          $sql=$sql." AND D.DOCCAB_LOCALDESTINO='".$_REQUEST['doccab_localdestino']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_data'])and (strlen($_REQUEST['doccab_data'])>0))
       {
          $sql=$sql." AND D.DOCCAB_DATA = '".ivtdata($_REQUEST['doccab_data'])."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_numero']) and (strlen($_REQUEST['doccab_numero'])>0))
       {
          $sql=$sql." AND D.DOCCAB_NUMERO='".$_REQUEST['doccab_numero']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_numeroext']) and (strlen($_REQUEST['doccab_numeroext'])>0))
       {
          $sql=$sql." AND D.DOCCAB_NUMEROEXT='".$_REQUEST['doccab_numeroext']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_loja']) and (strlen($_REQUEST['doccab_loja'])>0))
       {
          $sql=$sql." AND D.DOCCAB_LOJA='".$_REQUEST['doccab_loja']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_numerosubst']) and (strlen($_REQUEST['doccab_numerosubst'])>0))
       {
          $sql=$sql." AND D.DOCCAB_NUMEROSUBST='".$_REQUEST['doccab_numerosubst']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_numeropedido']) and (strlen($_REQUEST['doccab_numeropedido'])>0))
       {
          $sql=$sql." AND D.DOCCAB_NUMEROPEDIDO='".$_REQUEST['doccab_numeropedido']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_backorder']) and (strlen($_REQUEST['doccab_backorder'])>0))
       {
          $sql=$sql." AND D.DOCCAB_BACKORDER='".$_REQUEST['doccab_backorder']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_estilo']) and (strlen($_REQUEST['doccab_estilo'])>0))
       {
          $sql=$sql." AND D.DOCCAB_ESTILO='".$_REQUEST['doccab_estilo']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_localimpressao']) and (strlen($_REQUEST['doccab_localimpressao'])>0))
       {
          $sql=$sql." AND D.DOCCAB_LOCALIMPRESSAO='".$_REQUEST['doccab_localimpressao']."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_datalimite'])and (strlen($_REQUEST['doccab_datalimite'])>0))
       {
          $sql=$sql." AND D.DOCCAB_DATALIMITE = '".ivtdata($_REQUEST['doccab_datalimite'])."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_dataemissao'])and (strlen($_REQUEST['doccab_dataemissao'])>0))
       {
          $sql=$sql." AND D.DOCCAB_DATAEMISSAO = '".ivtdata($_REQUEST['doccab_dataemissao'])."'";
          $filtro='Filtro Ativado';
       }
       if (isset($_REQUEST['doccab_status']) and (strlen($_REQUEST['doccab_status'])>0))
       {
          $sql=$sql." AND D.DOCCAB_STATUS='".$_REQUEST['doccab_status']."'";
          $filtro='Filtro Ativado';
       }
		$sql.="
		group by D.DOCCAB_SEQDOC,
				D.DOCTIP_CODIGO,
				D.DOCCAB_LOCALORIGEM,
				D.DOCCAB_LOCALDESTINO,
				D.DOCCAB_DATA,
				D.DOCCAB_NUMERO,
				D.DOCCAB_NUMEROEXT,
				D.DOCCAB_LOJA,
				D.DOCCAB_NUMEROSUBST,
				D.DOCCAB_NUMEROPEDIDO,
				D.DOCCAB_BACKORDER,
				D.DOCCAB_ESTILO,
				D.DOCCAB_LOCALIMPRESSAO,
				D.DOCCAB_RAZAOSOCIAL,
				D.DOCCAB_CNPJ,
				D.DOCCAB_NUMEROSEMANA,
				D.DOCCAB_TIPOETIQUETA,
				D.DOCCAB_LAYOUTETIQUETA,
				D.DOCCAB_TIPOEVENTO,
				D.DOCCAB_DATALIMITE,
				D.DOCCAB_DATAEMISSAO,
				S.DOCSTS_DESCRICAO,
				D.DOCCAB_SEQ,
				D.DOCCAB_QTDREIMPR,
				D.DOCCAB_STATUS ";
		   $sql=$sql." ORDER BY D.DOCCAB_DATAEMISSAO desc";
    //echo "<pre>$sql";
	$_SESSION['Ptabseparacao1']=$sql;
	$pag  = 1;
	$rows = tw_get_limit_array($_SESSION['Ptabseparacao1'],$pag,$linhap,$regs);
	$npag = round(($regs/$linhap),0);
	if (($npag*$linhap)<$regs) $npag+=1;
	cab1($npag,$pag,$con,$filtro);
	while (current($rows)){
		det1(current($rows),$pag,$con);
		next($rows);
	}
	echo '</table> ';
	echo '</div>';
    echo '<table border="0" bordercolor="#000000" width="150px"> ';
	echo $_SESSION['grid'];	
	echo'</table>';
   }
  else
  {
	$pag  = intval($_REQUEST['cod']);
	$rows = tw_get_limit_array($_SESSION['Ptabseparacao1'],$pag,$linhap,$regs);
	$npag = round(($regs/$linhap),0);
    if (($npag*$linhap)<$regs) $npag+=1;
    cab1($npag,$pag,$con,$filtro);
    while (current($rows)){
      det1(current($rows),$pag,$con);
	  next($rows);
    }
    echo '</table> ';
	echo '</div>';
	echo '<table border="0" bordercolor="#000000" width="150px"> ';
	echo $_SESSION['grid'];	
	echo'</table>';
  }
}
else
{ echo '<h5>Não foram informados campos para o processo...</h5>';
}

function cab1($npag,$pag,$con,$filtro)
{
?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="../../padrao/css/estilos.css" rel="stylesheet" type="text/css"></style>
    <script type="text/javascript"> djConfig = { isDebug: false }; </script>
    <script type="text/javascript" src="../../../Comp_WMS/dojo/dojo.js"></script>
    <script type="text/javascript">
    	dojo.require("dojo.widget.Button");
	    dojo.require("dojo.widget.ComboBox");
    </script>

    <script type="text/javascript">

    function chama1()
    {
     var wb=dojo.widget.byId("datag");
     var wd=dojo.widget.byId("datag1");
     var xx;
     xx="?cod="+0;
     xx=xx+"&datag="+wb.valueNode.value;
     xx=xx+"&datag1="+wd.valueNode.value;
     parent.document.getElementById("ge0").src="tabseparacao1.php"+xx;
    }
    </script>
	<style type="text/css">
    body {
     margin-left: 0px;
     margin-top: 0px;
     margin-right: 0px;
     margin-bottom: 0px;
    }
    </style>
    <body scroll="no" style="overflow-x:hidden;overflow-y:hidden;">
<?
    cssz();
    echo '<script>';
    echo 'function formatadata(obj)';
    echo '{if (obj.value.length==2)';
    echo ' {obj.value=obj.value+"/";}';
    echo 'if (obj.value.length==5)';
    echo ' {obj.value=obj.value+"/";}
          if (obj.value.length>=10){
            obj.value=obj.value.substring(0,10);
          }
    }';
    echo '</script>';
    echo ' <table style="width: 100%;" border="0" class=ca4 align="top">';
    echo '    <tr>  ';
    echo '        <td class=ca4 width=60% align=left>';
	if (tw_get_permissao(802)==1)
	{
		echo '<a href=botao1_incluir.php?cod=0&pag='.$pag.'&acao=filtrar id=bt1>'.tw_button('Filtrar').'</a> ';
	}
	if (tw_get_permissao(803)==1)
	{
		echo '<a href="selects.php?cod=0&pag='.$pag.'&importar=1" id=bt1>'.tw_button('Importar').'</a> ';
	}
	if (tw_get_permissao(803)==1)
	{
		echo '<a href="selects.php?cod=0&pag='.$pag.'&importar_past=1" id=bt1>'.tw_button('Imp Pasta').'</a> ';
	}
    //echo '<a href="selects.php?cod=0&pag='.$pag.'&exportar=1" id=bt1>'.tw_button('Exportar').'</a> ';
    echo '        </td>';
    echo '      <td class=ca4 align=left height="0" border="0" width="10%" style="color:red;">'.$filtro.'</td>';
    echo '      <td class=ca4 align=right height="0" border="0" width="30%">  ';
    barra1($npag,$pag);
    echo '        </td>';
    echo '        </tr>';
echo '  </tbody></table>';
	echo '<div style="width:740px;height:335px;overflow:auto;float:left;display:block;">';
    echo '<table border="0" bordercolor="#000000" align="botton" width="3000px"> ';
    echo '    <tr>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        ID MSG';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Tipo MSG';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Local Orig';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Local Dest';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Data';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Nro.Solicitação';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Ext.Solicitação';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Loja';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Solicitação Subst';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Nro.Pedido';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Nro.Backorder';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Estilo';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Local Imp';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="10%">  ';
    echo '        Razão Social';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        CNPJ';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Nro.Semana';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Tipo Etq';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width=3%">  ';
    echo '        Layout Etq';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Tipo Evento';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Data Entrega';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
    echo '        Data Solic';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Itens';
    echo '      </td>  ';
    echo '      <td class="ca3" align=center height="10" border="0" width="3%">  ';
    echo '        Status';
    echo '      </td>  ';
	echo '    </tr>'; 
	$_SESSION['grid']='';
    $_SESSION['grid'].= '<tr><td class="ca3" align=center height="5" border="0" width="5%">';
    $_SESSION['grid'].= '        Ações';
    $_SESSION['grid'].= '      </td> </tr> ';
}
function det1($row,$pag,$con)
{
	$select = "select count(*) from doccab where doccab_onda = '".$row[11]."'
					and doccab_seq != '".$row[7]."'";
	$re = tw_query($select, $con);
	$ro = tw_fetch_row($re);
	if($ro[0] == 0){
		$pode = 1;
	}else{
		$pode = 0;
	}
	if ($row[21]=='Liberado'){
		$cor='l';
	}else
	if ($row[21]=='Cancelado'){
		$cor='f';
	}else
	if ($row[21]=='Encerrado'){
		$cor='e';
	}else
	if ($row[21]=='Divergência'){
		$cor='dv';
	}else
	if (($row[21]=='Em execucao') or ($row[8]==2)){
		$cor='ex';
	}

	$sql_itens = "select count(*) from docitm where DOCCAB_SEQ = $row[22]
							and   DOCITM_STATUS <> 9";
	$res_itens = tw_query($sql_itens,$con);
	$row_itens = tw_fetch_row($res_itens);

	echo '    <tr>  ';
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[0].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[1].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[2].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[3].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=center height="10" border="0" width="3%">  ';
	echo '        '.cvtdata($row[4]).'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[5].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[6].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[7].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[8].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[9].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[10].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[11].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[12].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=left height="10" border="0" width="10%">  ';
	echo '        '.substr($row[13],0,35).'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="5%">  ';
	echo '        '.$row[14].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[15].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[16].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[17].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row[18].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=center height="10" border="0" width="5%">  ';
	echo '        '.cvtdata($row[19]).'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=center height="10" border="0" width="5%">  ';
	echo '        '.cvtdata($row[20]).'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=right height="10" border="0" width="3%">  ';
	echo '        '.$row_itens[0].'</a> ';
	echo '      </td>' ;
	echo '      <td class="de1'.$cor.'" align=left height="10" border="0" width="3%">  ';
	echo '        '.$row[21].'</a> ';
	echo '      </td>' ;	
	$_SESSION['grid'].= '<tr><td class="de1'.$cor.'" align=center height="10" border="0" width="5%" >  ';
	if (tw_get_permissao(804)==1)
	{
		$_SESSION['grid'].='       <a href=into_detalhes.php?cod=0&doccab='.$row[22].'&numero='.$row[0].'&pag='.$pag.' style="text-decoration:none;"><IMG src="'.$_SESSION['sc_main_path'].'/Comp_WMS/icones/detalhar.png" class="icones" style="cursor:hand;border:0px" onClick="" title="Detalhes"></IMG></a>';
	}
	if ( ($row[8] == 7) or ($row[8]) == 8 )
	{
		$_SESSION['grid'].='       <a href=rel_doc_conf.php?cod=0&doccab='.$row[7].'&docnum='.$row[1].'&status='.$row[8].'&pag='.$pag.' style="text-decoration:none;"><IMG src="'.$_SESSION['sc_main_path'].'/Comp_WMS/icones/relatorio.png" class="icones" style="cursor:hand;border:0px" onClick="" title="Emitir Documento de Conferência"></IMG></a>';
	}
	
	
	if (tw_get_permissao(804)==1)
	{
		if ($row[24]==8)
		{
			$_SESSION['grid'].='       <a href=reimpressao.php?cod=0&doccab='.$row[22].'&numero='.$row[0].'&pag='.$pag.'&reimp='.$row[23].'&status='.$row[24].'&tip_etq='.$row[17].' style="text-decoration:none;"><IMG src="'.$_SESSION['sc_main_path'].'/Comp_WMS/icones/imprimir.png" class="icones" style="cursor:hand;border:0px" onClick="" title="Reimpressão de Etiqueta"></IMG></a>';
		}
		else
		{
			$_SESSION['grid'].='       <a href=print_etq.php?cod=0&doccab='.$row[22].'&numero='.$row[0].'&pag='.$pag.'&reimp='.$row[23].'&status='.$row[24].'&tip_etq='.$row[17].' style="text-decoration:none;"><IMG src="'.$_SESSION['sc_main_path'].'/Comp_WMS/icones/imprimir.png" class="icones" style="cursor:hand;border:0px" onClick="" title="Imprimir Etiqueta"></IMG></a>';
		}
	}





	$_SESSION['grid'].= '   </td></tr>' ;
	echo '   </tr>';
}
	echo '<table border="0" bordercolor="#000000" align="botton" width="100%"> ';
	echo '    <tr>';
	echo '      <td class="ca3" align=center height="10" border="0" width="5%">  ';
	echo '        '.$_REQUEST['msg'].'</a> ';
	echo '      </td>  ';
	echo '    </tr>  ';

function barra1($npag,$pag)
{
    paginacao($npag,$pag,'tabseparacao1.php?');
}
    $sq_param = "Select PARAM_VALOR
                        FROM param
                    Where PARAM_ID = 'gera_txt_nserie'
                      and EMPRES_CODIGO = '".$_SESSION['Pempresa']."'
                      and FILIAL_CODIGO = '".$_SESSION['Pfilial']."' ";
    //echo $sq_param.'<br>';
    $res_param = tw_query($sq_param,$_SESSION['con']);
    $row_param = tw_fetch_row($res_param);
    if( (trim($_REQUEST['caminho'])<>'') and (trim($_REQUEST['nome'])<>'') and ($row_param[0]==1) ){
?>
    <script language="javaScript">
        window.location="save_txt.php?caminho=<?echo addslashes($_REQUEST['caminho'])?>&nome=<?echo $_REQUEST['nome']?>&msg=<?echo $_REQUEST['msg']?>";
    </script>
<?php
   }
?>
</body>
</html>
