<?php

function inv_disp()
{
require('../cfg/res.php');
mysql_query("SET NAMES 'utf8'");


?>



<script type="text/javascript">



function invdisp_d(itex,obj)
{
	var obj2=obj.parentNode;
	var obj3=obj2.parentNode;	
	for(i=0;i<=document.getElementsByClassName('filax').length-1;i++)
	{
		document.getElementsByClassName('filax').item(i).style.background='#fff';
	}
       obj3.style.background='#09c';

$.ajax({
  url:'../lib/vrinvdisp2.php',
  type:'POST',
  data:{item:itex},
  success:function(dato){
  document.getElementById('invdisp2').innerHTML=dato;
  },
  error:function(objeto){console.log(objeto);}

});

}



function redireccionar() 
{
var flsub=document.getElementById('flsub').value;
var fbode=document.getElementById('fbode').value;
var fmdlo=document.getElementById('fmdlo').value;
var fmrca=document.getElementById('fmrca').value;
var fctlg=document.getElementById('fctlg').value;

var fecha_ini=document.getElementById('fecha_ini').value;
var fecha_fin=document.getElementById('fecha_fin').value;

var fposi=document.getElementById('fposi').checked;
var fcero=document.getElementById('fcero').checked;
var fnega=document.getElementById('fnega').checked;

var fcodis=document.getElementById('fcodis').checked;
var fcodia=document.getElementById('fcodia').checked;
var fcodie=document.getElementById('fcodie').checked;
var fcodi='1';

if (fcodis)
{fcodi='1';}

if (fcodia)
{fcodi='2';}

if (fcodie)
{fcodi='3';}

var fordec=document.getElementById('fordec').checked;
var forded=document.getElementById('forded').checked;
var forde='1';

if (fordec)
{forde='1';}

if (forded)
{forde='2';}



pagina="?opt=<?php echo $_GET['opt']?>" 
+"&fbode="+fbode 
+"&flsub="+flsub 
+"&fmrca="+fmrca
+"&fmdlo="+fmdlo
+"&fecha_ini="+fecha_ini 
+"&fecha_fin="+fecha_fin 
+"&fposi="+fposi
+"&fcero="+fcero
+"&fnega="+fnega
+"&fcodi="+fcodi
+"&forde="+forde
+"&fctlg="+fctlg;

location.href=pagina;
} 



function imprimirlista() 
{
var flsub=document.getElementById('flsub').value;
var fbode=document.getElementById('fbode').value;
var fmrca=document.getElementById('fmrca').value;
var fmdlo=document.getElementById('fmdlo').value;
var fctlg=document.getElementById('fctlg').value;

var fecha_ini=document.getElementById('fecha_ini').value;
var fecha_fin=document.getElementById('fecha_fin').value;

var fposi=document.getElementById('fposi').checked;
var fcero=document.getElementById('fcero').checked;
var fnega=document.getElementById('fnega').checked;

var fcodis=document.getElementById('fcodis').checked;
var fcodia=document.getElementById('fcodia').checked;
var fcodie=document.getElementById('fcodie').checked;
var fcodi='1';

if (fcodis)
{fcodi='1';}

if (fcodia)
{fcodi='2';}

if (fcodie)
{fcodi='3';}

var fordec=document.getElementById('fordec').checked;
var forded=document.getElementById('forded').checked;
var forde='1';

if (fordec)
{forde='1';}

if (forded)
{forde='2';}


pagina="../lib/impr_invdisp0.php?"
+"&fbode="+fbode 
+"&flsub="+flsub 
+"&fmrca="+fmrca
+"&fmdlo="+fmdlo
+"&fecha_ini="+fecha_ini 
+"&fecha_fin="+fecha_fin 
+"&fposi="+fposi
+"&fcero="+fcero
+"&fnega="+fnega
+"&fcodi="+fcodi
+"&forde="+forde
+"&fctlg="+fctlg;

window.open(pagina);
} 


</script>






<div style=" max-height:100px; overflow-y:scroll; margin-top:5px;">

<?php


$fecha1 = date('Y-m-j');
$nuevafecha = strtotime ( '-1 month' , strtotime ( $fecha1 ) ) ;
$xfecha1 = date ( 'Y/m/j' , $nuevafecha );
$xfecha1 = date('2000/1/1');
$xfecha2 = date('Y/m/d');



//xcalcsum = '';
//		+'tcant as b000, ';
//		+'case when bode like "B01%" then tcant else 0 end as b010, ';
//		+'case when bode like "B02%" then tcant else 0 end as b020, ';
//		+'case when bode like "B03%" then tcant else 0 end as b030, ';
//		+'case when bode like "B04%" then tcant else 0 end as b040, ';
//		+'case when bode like "B05%" then tcant else 0 end as b050, ';
//		+'case when bode like "B06%" then tcant else 0 end as b060, ';
//		+'case when bode like "B07%" then tcant else 0 end as b070, ';
//		+'case when bode like "B08%" then tcant else 0 end as b080, ';
//		+'case when bode like "B09%" then tcant else 0 end as b090 '



//$sql_idisp='select *, sum(tcant) as dispo, sum(tcant) as saldo  from inve12 where ';

$sql_idisp='select * from invdisp where ';



if(isset($_GET['fposi']) && ($_GET['fposi']=='true'))
{$xfposi=$_GET['fposi'];}
else
{$xfposi='false';}

if(isset($_GET['fcero']) && ($_GET['fcero']=='true'))
{$xfcero=$_GET['fcero'];}
else
{$xfcero='false';}

if(isset($_GET['fnega']) && ($_GET['fnega']=='true'))
{$xfnega=$_GET['fnega'];}
else
{$xfnega='false';}




if($xfposi=='false' && $xfnega=='false' && $xfcero=='false')
{$xfposi='true';}
$sql_idisp.='(';

if($xfposi=='true')
{$sql_idisp.='(dispo>0)';}

if($xfcero=='true')
{$sql_idisp.='(saldo=0)';}

if($xfnega=='true')
{$sql_idisp.='(saldo<0)';}

$sql_idisp.=')';
$sql_idisp=str_replace(')(',')||(',$sql_idisp);


if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']) && isset($_GET['fecha_fin']) && !empty($_GET['fecha_fin']))
{
	//$sql_idisp.='and (fecha BETWEEN "'.$_GET['fecha_ini'].'" and "'.$_GET['fecha_fin'].'")';
//	$sql_idisp.=' and (fecha<="'.$_GET['fecha_fin'].'")';
	$xfecha1=$_GET['fecha_ini'];
	$xfecha2=$_GET['fecha_fin'];
}
else
{
	//$sql_idisp.='and (fecha BETWEEN "'.$xfecha1.'" and "'.$xfecha2.'")' ;
//	$sql_idisp.=' and (fecha<='.$xfecha2.'")' ;
}



if(isset($_GET['flsub']) && !empty($_GET['flsub']))
{
	$sql_idisp.=' and  (item_lsub like "'.$_GET['flsub'].'%" or lsub_descri like "%'.$_GET['flsub'].'%" ) ';
	$xflsub=$_GET['flsub'];
}

if(isset($_GET['fmrca']) && !empty($_GET['fmrca']))
{
	$sql_idisp.=' and  (item_mrca like "'.$_GET['fmrca'].'%" ) ';
	$xfmrca=$_GET['fmrca'];
}

if(isset($_GET['fmdlo']) && !empty($_GET['fmdlo']))
{
	$sql_idisp.=' and  (item like "%'.$_GET['fmdlo'].'%" ) ';
	$xfmdlo=$_GET['fmdlo'];
}




if(isset($_GET['fctlg']) && !empty($_GET['fctlg']))
{
	$sql_idisp.=' and  (item_nctlg ='.$_GET['fctlg'].' )';
	$xfctlg=$_GET['fctlg'];
}


$xfcodi='1';
if (mb_stristr('elco',substr($_SESSION['user_vcrx_empr'],0,4)))
{
$xfcodi='2';
}

if(isset($_GET['fcodi']) && !empty($_GET['fcodi']))
{
	$xfcodi=$_GET['fcodi'];
}
switch ($xfcodi)
{
	case '1': $item_codi='item';break;
	case '2': $item_codi='item_codigov';break;
	case '3': $item_codi='item_ean';break;
	default : $item_codi='item';break;
}


$xforde='2';
if (mb_stristr('gnya',substr($_SESSION['user_vcrx_empr'],0,4)))
{
$xforde='1';
}

if(isset($_GET['forde']) && !empty($_GET['forde']))
{
	$xforde=$_GET['forde'];
}



$xorder='';
if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
{$xorder='item_nctlg desc,';}


switch ($xforde)
{
	case '1': $xorder.='lsub_descri,'.$item_codi.',';break;
	case '2': $xorder.='lsub_descri,item_descri,';break;
	default : $xorder.='lsub_descri,';break;
}





$sql_idisp.=' order by '.$xorder.$item_codi;


//echo $sql_idisp;
$res_idisp=mysql_query($sql_idisp,$link_vcrx);

?>



<table id="tabla_filtros" width="100%" border="0" cellspacing="0" cellpadding="2" style="border-collapse:collapse;font-size:12px; border:#000000 solid 1px;">
<tr align="left" valign="top">

<th>
<div>
<span>Fec.Ini</span> <input type="text" id="fecha_ini" placeholder="aaaa/mm/dd" size="8" value="<?php echo $xfecha1;?>" /> 
</div>
<div>
<span>Fec.Fin</span> <input type="text" id="fecha_fin" placeholder="aaaa/mm/dd" size="8" value="<?php echo $xfecha2;?>" />
</div>


</th>



<th>
<div>
    	<div>
	    <span>Bodega</span> 
	    <input type="text" id="fbode" placeholder="bodega" size="10" value="<?php echo $xfbode;?>" /> 
	</div>
    	<div>
	    <span>Lineas</span> 
	    <input type="text" id="flsub" placeholder="linea" size="10" value="<?php echo $xflsub;?>" /> 
	</div>
</div>
</th>

<th>
<div>
    	<div>
	    <span>Marca.</span> 
	    <input type="text" id="fmrca" placeholder="marca" size="10" value="<?php echo $xfmrca;?>" /> 
	</div>
    	<div>
	    <span>Modelo</span> 
	    <input type="text" id="fmdlo" placeholder="modelo" size="10" value="<?php echo $xfmdlo;?>" /> 
	</div>
</div>
</th>



<th>
<div>
<input type="checkbox" id="fposi" name="fposi" <?php if($xfposi=='true'){echo 'checked';}?>/><span>Positivos</span> <br/>
<input type="checkbox" id="fcero" name="fcero" <?php if($xfcero=='true'){echo 'checked';}?>/><span>Agotados</span> <br/>
<input type="checkbox" id="fnega" name="fnega" <?php if($xfnega=='true'){echo 'checked';}?>/><span>Negativos</span> <br/>
</div>
</th>

<th>
<div>
<input type="radio" id="fcodis" name="tcodi" <?php if($xfcodi=='1'){echo 'checked';}?>/><span>Sistema</span> <br/>
<input type="radio" id="fcodia" name="tcodi" <?php if($xfcodi=='2'){echo 'checked';}?>/><span>Auxiliar</span> <br/>
<input type="radio" id="fcodie" name="tcodi" <?php if($xfcodi=='3'){echo 'checked';}?>/><span>EAN (Barras)</span> <br/>
</div>
</th>

<th>
<div>
<input type="radio" id="fordec" name="forder" <?php if($xforde=='1'){echo 'checked';}?>/><span>Codigo</span> <br/>
<input type="radio" id="forded" name="forder" <?php if($xforde=='2'){echo 'checked';}?>/><span>Descripcion</span> <br/>
</div>
</th>




<th>
<div>
    	<div>
	    <span>Catalogo</span> 
	    <input type="text" id="fctlg" placeholder="catalogo" size="5" value="<?php echo $xfctlg;?>" /> 
	</div>

</div>
</th>





<th align="center">
<div>
<input type="button" value="Filtrar" onclick="redireccionar();"  />
</div>
<br>
<div>
<input type="button" value="Imprimir" onclick="imprimirlista();"  />
</div>
</th>

</tr>
</table>
</div>

<hr/>






<div style="overflow-y:scroll; height:300px;">

<table id="tabla_invdisp" width="100%" border="0" cellspacing="0" cellpadding="2" style="border-collapse:collapse;font-size:12px; border:#000000 solid 1px;">

<tr style="border-bottom:#000000 solid 2px; ">
	<th width="15%">LINEA</th>
	<th width="1%">ITEM</th>
	<th>DESCRIPCION</th>
	<th>MARCA/MODELO</th>
	<th>CTLG</th>
	<th width="1%">SALDO</th>
	<th width="1%">RESER</th>
	<th width="1%">DISPO</th>
</tr>
<?php 
$filax=1;
$umdlo='';

while($r_idisp=mysql_fetch_assoc($res_idisp))
{
$duni=$r_idisp['item_duni'];
?>

<?php	
if ($r_idisp['item_modelo']==$umdlo)
{if($umdlo==$pmdlo){$filax=1;}}
else
{if($umdlo!=$pmdlo){}else{$filax=4;}}

$pmdlo=$umdlo;
$umdlo=$r_idisp['item_modelo'];

?>
	<tr class="filax" id="<?php echo $r_ped['idnum1']; ?>" align="center" style=" <?php if($filax>=4){echo 'border-top:dotted 1px #000;';$filax=1;}else{$filax++;} ?>" >
	<td align="center"><?php echo'<span title="'.$r_idisp['item_lsub'].'">'.$r_idisp['lsub_descri'].'</span>';?></td>
	<td><a href="#" onClick="invdisp_d('<?php echo $r_idisp['item'];?>',this);"><?php echo '<span title="'.$r_idisp['item_ean'].'">'.$r_idisp[$item_codi].'</span>';?></a> </td>
	<td align="left"><?php echo'<span title="'.$r_idisp['item_modelo'].'">'.$r_idisp['item_descri'].'</span>';?></td>
	<td align="left"><?php echo'<span title="'.$r_idisp['item_modelo'].'">'.$r_idisp['item_mrca'].'/'.$r_idisp['item_modelo'].'</span>';?></td>
	<td align="left"><?php  if($r_idisp['item_nctlg']>0){echo'<span title="Pag.'.$r_idisp['item_pctlg'].'">'.$r_idisp['item_nctlg'].'</span>';}?></td>
   	<td align="right"><?php if($r_idisp['saldo']<0){echo '<font color="red">';} ?> <?php echo number_format($r_idisp['saldo'],$duni);?></td>
   	<td align="right"><?php if($r_idisp['reser']!=0){echo number_format($r_idisp['reser'],$duni);}?></td>
   	<td align="right"><?php if($r_idisp['dispo']<0){echo '<font color="red">';} ?> <?php echo number_format($r_idisp['dispo'],$duni);?></td>
</tr>
<?php }?>
</table>
</div>
<br>
<div id="invdisp2"></div>

<?php }?>