<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<?php
require('../cfg/res.php');
require('../cfg/cfg.php');
require('vrcospro.php');

$fini1= date('2000-01-01');
$sql_fini1='select * from empr0';
$res_fini1=mysql_query($sql_fini1,$link_vcrx);
$fini1=mysql_result($res_fini1,0,'empr_finve');


$fecha1 = date('Y-m-j');
$nuevafecha = strtotime ( '-120 month' , strtotime ( $fecha1 ) ) ;
$xfecha1 = date ( 'Y/m/j' , $nuevafecha );
$xfecha1= date('Y/m/'.'1');
$xfecha2= date('Y/m/d');
$xtotcost=0;
$xtotpvp1=0;


	$item=$_GET['item'];
	if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']))
	{
		$fini=$_GET['fecha_ini'];
	}
	else
	{
		$fini=$fini1;
	}
	
	if(isset($_GET['fecha_fin']) && !empty($_GET['fecha_fin']))
	{
		$ffin=$_GET['fecha_fin'];
	}
	else
	{
		$ffin=date('Y-m-d');
	}


if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']) && isset($_GET['fecha_fin']) && !empty($_GET['fecha_fin']))
{
	$xfecha1=$_GET['fecha_ini'];
	$xfecha2=$_GET['fecha_fin'];
}


$sql_itemres='drop view qitemres';
$res_itemres=mysql_query($sql_itemres,$link_vcrx);

$sql_itemres='create view qitemres as select clie12.*,sum(tcant) as reser from clie12 where status="-" and fecha>=(date_sub(date(NOW()), INTERVAL 3 DAY) group by item ';
$res_itemres=mysql_query($sql_itemres,$link_vcrx);

//echo $sql_itemres;


$sql_inves='select inve12.*,
	sum(case when fecha<"'.$xfecha1.'" then tcant end) as ttcanti,
	sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tingreso end) as ttingreso,
	sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tegreso end) as ttegreso,
	sum(tcant) as ttcant,
	sum(tsubt) as ttsubt,
	sum(total) as ttotal,
	sum(valpvp) as tvalpvp
	from inve12 where ';
	
$sql_inves.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';




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';}



$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;
}
//echo $item_codi;


$xfbode='';
if(isset($_GET['fbode']) && !empty($_GET['fbode']))
{
	$sql_inves.=' and  (bode like "'.$_GET['fbode'].'%" or bode_descri like "%'.$_GET['fbode'].'%" ) ';
	$xfbode=$_GET['fbode'];
}

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

if(isset($_GET['fitem']) && !empty($_GET['fitem']))
{
	$sql_inves.=' and  (item like "'.$_GET['fitem'].'%" or item_descri like "%'.$_GET['fitem'].'%" ) ';
	$xfitem=$_GET['fitem'];
}

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

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


$xfcost='false';
if(isset($_GET['fcost']) && ($_GET['fcost']=='true'))
{$xfcost=$_GET['fcost'];}

$xfpuni='false';
if(isset($_GET['fpuni']) && ($_GET['fpuni']=='true'))
{$xfpuni=$_GET['fpuni'];}

$xfpvp1='false';
if(isset($_GET['fpvp1']) && ($_GET['fpvp1']=='true'))
{$xfpvp1=$_GET['fpvp1'];}



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


	$sql_inves.=' and(status!="X") ';
	$sql_inves.=' and(status!="O") ';



//$xfgrou='D';
//if(isset($_GET['fgrou']) && !empty($_GET['fgrou']))
//{$xfgrou=$_GET['fgrou'];}

$xgroup = 'item ';


$xforde='D';
$xfrepo='G';
if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
{$xforde='C';}

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

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


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

switch ($xforde)
{
	case 'D':$xorder.='item_lsub,item_descri ';break;
	case 'C':$xorder=str_replace('_descri','',$xgroup);
			$xorder=str_replace(',item',','.$item_codi.',item_descri,item',$xorder);break;
	case 'T':$xorder.='ttcant desc,item_lsub,item  ';break;

	default :$xorder=$xgroup;break;
}

$xbodes=0;
$xwidth = 100;
if ($xfrepo=='B')
{
		$xfecha1=date('Y-m-01',strtotime($xfecha1));
		$xfecha2=date('Y-m-t', strtotime($xfecha2));
		$xf2col='false';
		$sql_bodes='select distinct bode from inve11 where fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" order by bode';
		$res_bodes=mysql_query($sql_bodes,$link_vcrx);
		$xbodes = mysql_num_rows($res_bodes);
		$xcbod=0;
		$sql_pivot='';
		while ($r_bod0=mysql_fetch_assoc($res_bodes))	
		{
			$xcbod++;
			$xbod=$r_bod0['bode'];
			$sql_pivot.='sum(case when bode="'.$xbod.'" then tcant end) as s'.$xbod.',';	
			$sql_pivot.='sum(case when bode="'.$xbod.'" then tingreso end) as d'.$xbod.',';	
			$sql_pivot.='sum(case when bode="'.$xbod.'" then tegreso end) as c'.$xbod.',';	
		}

	$sql_inves=str_replace($xfecha1,$xfecha1c,$sql_inves);
	$sql_inves=str_replace('inve12.*,','inve12.*,'.$sql_pivot,$sql_inves);

	$sql_invest=str_replace($xfecha1,$xfecha1c,$sql_invest);
	$sql_invest=str_replace('inve12.*,','inve12.*,'.$sql_pivot,$sql_invest);
	// 	echo $sql_inves;
	if($xbodes>6){$xwidth = 100 + ($xbodes-6)*10;}
	
}



$res_invest=mysql_query($sql_inves,$link_vcrx);
$sql_inves.=' group by '.$xgroup;
$sql_inves.=' order by '.$xorder;
$res_inves=mysql_query($sql_inves,$link_vcrx);

//echo $sql_inves;
?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inventario Saldos</title>
<style type="text/css">
body{ font-family:"Courier New", Courier, monospace;}

</style>

</head>
<body>

<div align="center">
<div align="center" style="width:1000px; font-size:12px;">
<div align="center" style="font-size:18px;font-weight: bold; border:#000 solid 1px; background: #F7F7F7; margin:2px;">
	INVENTARIO SALDOS
<?php	if ($xfrepo=='G'){echo ' GENERAL';}	?>
<?php	if ($xfrepo=='B'){echo ' X BODEGA';}	?>
	</div>

<table width="100%" cellspacing="0" cellpadding="2">
<tr valign="top">
<td width="50%">

<table border="1" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">
<tr>
	<th width="30%" style="background: #F7F7F7;">PARAMETROS</th>
	<th></th>
</tr>

<tr>
    	<td style="font-weight:bolder;">FECHA</td>
	<td><?php echo date_format(date_create($fini),'d/M/Y').' - '.date_format(date_create($ffin),'d/M/Y');?></td>
</tr>

<tr>
    	<td style="font-weight:bolder;">BODEGA</td>
	<td><?php echo $xfbode; ?></td>
</tr>
<tr>
    	<td style="font-weight:bolder;">LINEA</td>
	<td><?php echo $xflsub;?></td>
</tr>
<tr>
    	<td style="font-weight:bolder;">ITEM</td>
	<td><?php echo $xfitem;?></td>
</tr>
<tr>
    	<td style="font-weight:bolder;">MARCAS</td>
	<td><?php echo $xfmrca;?></td>
</tr>

<tr>
    	<td style="font-weight:bolder;">SALDOS</td>
	<td><?php if($xfposi=='true'){echo '/POSITIVOS';};if($xfcero=='true'){echo '/AGOTADOS';};if($xfnega=='true'){echo '/NEGATIVOS';};?></td>
</tr>

<tr>
    	<td style="font-weight:bolder;">ORDENADO X</td>
	<td><?php echo $xorder;?></td>
</tr>


</table>




<td>
<!----++++++++++------>
<table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr>
	<th width="30%"  style="background: #F7F7F7;">I N F O</th>
	<th></th>    
</tr>
<?php
$sql_empr='select * from empr0';
$res_empr=mysql_query($sql_empr,$link_vcrx);
?>
<tr>
	<td align="right">EMPRESA:</td>
	<td><?php echo mysql_result($res_empr,0,'empr_descri');?></td>
</tr>

<tr>
	<td align="right">USUARIO:</td>
	<td><?php echo $_SESSION['user_vcrx_id'].' / '.$_SESSION['user_vcrx_descri'];?></td>
</tr>
<tr>
	<td align="right">FECHA / HORA :</td>
	<td><?php echo date('d/M/Y H:i:s');?></td>
</tr>
</table>
<!----++++++++++------>
</td>




</tr>
</table>
<!----++++++++++------>


<hr/>

<table border="0" width="100%" cellpadding="2" cellspacing="0" style="border-collapse:collapse">

<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
  <th>LINEA</th>
  <th>ITEM</th>
  <th>NOMBRE</th>  
  <th align="right">INIC.</th>
  <th align="right">INGR.</th>
  <th align="right">EGRE.</th>
  <th align="right">SALDO</th>

	<?php	

if ($xfrepo=='B')
{
		$xf2col='false';
		$res_bodes=mysql_query($sql_bodes,$link_vcrx);
		$xbodes = mysql_num_rows($res_bodes);
		$xcbod=0;
		$sql_pivot='';
		while ($r_bod0=mysql_fetch_assoc($res_bodes))	
		{
			$xbod=$r_bod0['bode'];
			if ($xbod==""){$xbod="B---";}
			echo '<th align="right">'.$xbod.'</th>';
		}
}
?>
	<?php if ($xfcost=='true'){ ?>
	<th align="right">COSTO</th>
	<th align="right">T.COSTO</th>
	<?php } ?>
	<?php if ($xfpuni=='true'){ ?>
	<th align="right">P.Unit</th>
	<th align="right">T.P.Uni</th>
	<?php } ?>
	<?php if ($xfpvp1=='true'){ ?>
	<th align="right">PVP</th>
	<th align="right">T.PVP</th>
	<?php } ?>
	
</tr>
	
	
	

<?php 
$aux_f=0; 
$xsaldo = 0;

while($r_inves=mysql_fetch_assoc($res_inves)) 
{
$duni=$r_inves['item_duni'];

?>


<?php
$xsaldo0=1/100;
if(($xfposi=='true' && ($r_inves['ttcant'])>$xsaldo0) || ($xfcero=='true' && abs($r_inves['ttcant'])<=$xsaldo0) || ($xfnega=='true' && ($r_inves['ttcant'])<$xsaldo0*-1))
{
?>

<tr style=" <?php if($aux_f!=3){$aux_f++;}else{echo 'border-bottom:dotted 1px #000000;'; $aux_f=0;}?> ">
  <td align="center"><?php echo $r_inves['item_lsub']; ?></td>
	<?php if($xfcodi==1){?>
  <td align="left"><?php echo $r_inves['item']; ?></td>
	<?php } ?>
	<?php if($xfcodi==2){?>
  <td align="left"><?php echo $r_inves['item_codigov']; ?></td>
	<?php } ?>
	<?php if($xfcodi==3){?>
  <td align="left"><?php echo $r_inves['item_ean']; ?></td>
	<?php } ?>
  <td align="left"><?php echo $r_inves['item_descri']; ?></td>
  <td align="right"><?php if($r_inves['ttcanti']<0){echo '<font color="red">';} ?><?php echo number_format($r_inves['ttcanti'],$duni); ?></td>
  <td align="right"><?php echo number_format($r_inves['ttingreso'],$duni); ?></td>
  <td align="right"><?php echo number_format($r_inves['ttegreso'],$duni); ?></td>
  <td align="right"><?php if($r_inves['ttcant']<0){echo '<font color="red">';} ?><?php echo number_format($r_inves['ttcant'],$duni); ?></td>

	<?php	

if ($xfrepo=='B')
{
		$xf2col='false';
		$res_bodes=mysql_query($sql_bodes,$link_vcrx);
		$xbodes = mysql_num_rows($res_bodes);
		$xcbod=0;
		$sql_pivot='';
		while ($r_bod0=mysql_fetch_assoc($res_bodes))	
		{
			$xbod=$r_bod0['bode'];
			echo '<td  align="right">'.number_format($r_inves['s'.$xbod],$duni).'</td>';
		}
}
?>

	
	
	
	
	<?php if ($xfcost=='true'){ ?>
	<?php $xcospro = new cospro('../cfg/res.php'); ?>
	<?php $xcospro ->get_cospro($r_inves['item'],$xfecha2,1); ?>
	<?php $xcospro =$xcospro->cospro; ?>
	<?php $xstcos=0; if($r_inves['ttcant']>=0){$xstcos=$r_inves['ttcant']*$xcospro;} ?>
  <td align="right"><?php echo number_format($xcospro,2);?></td>
	<td align="right"><?php echo number_format($xstcos,2);?></td>
	<?php $xtotcost=$xtotcost+ $xstcos;?>	
	<?php } ?>

	<?php if ($xfpuni=='true'){ ?>
	<?php $xstpuni=0; if($r_inves['ttcant']>=0){$xstpuni=$r_inves['ttcant']*$r_inves['item_puni'];} ?>
  <td align="right"><?php echo number_format($r_inves['item_puni'],2);?></td>
	<td align="right"><?php echo number_format($xstpuni,2);?></td>
	<?php $xtotpuni=$xtotpuni+ $xstpuni;?>	
	<?php } ?>
	
	<?php if ($xfpvp1=='true'){ ?>
	<?php $xstpvp=0; if($r_inves['ttcant']>=0){$xstpvp=$r_inves['ttcant']*$r_inves['item_pvp'];} ?>
  <td align="right"><?php echo number_format($r_inves['item_pvp'],2);?></td>
	<td align="right"><?php echo number_format($xstpvp,2);?></td>
	<?php $xtotpvp1=$xtotpvp1+ $xstpvp;?>	
	<?php } ?>
	
	
</tr>
<?php }?>

<?php }?>


<?php if ($xfcost=='true' || $xfpuni=='true' || $xfpvp1=='true'){ ?>
	
<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
	<th></th>
	<th></th>
	<th>TOTALES</th>
	<th></th>
	<th></th>
	<th></th>
	<th></th>
<?php if ($xfcost=='true'){ ?>
	<th></th>
	<th align="right"><?php echo number_format($xtotcost,2);?></th>
<?php }?>
<?php if ($xfpuni=='true'){ ?>
	<th></th>
	<th align="right"><?php echo number_format($xtotpuni,2);?></th>
<?php }?>
<?php if ($xfpvp1=='true'){ ?>
	<th></th>
	<th align="right"><?php echo number_format($xtotpvp1,2);?></th>
<?php }?>
</tr>
<?php }?>

</table>




</div>
</div>

</body>
</html>

<?php //}?>