<?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');
include('php_gene1.php');

$sql_datesp='SET lc_time_names = "es_UY"';
$res_datesp=mysql_query($sql_datesp,$link_vcrx);



$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');
//echo '-'.$fini1.'-';

$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');

	if(isset($_GET['fecha_ini']) && !empty($_GET['fecha_ini']))
	{
		$fini=$_GET['fecha_ini'];
	}
	else
	{
		$fini=date('2000-01-01');
		if (mb_stristr('gnya',$_SESSION['user_vcrx_empr']))
		{$fini=date('2010-11-01');}
	}
	
	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'];
}


$xfecha1=str_replace('/','-',$xfecha1);
$xfecha2=str_replace('/','-',$xfecha2);


$sql_invest='select inve12.*,
			sum(case when fecha<"'.$xfecha1.'" then tcant end) as saldoi, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tingreso end) as tingre, 
			sum(case when fecha>="'.$xfecha1.'" and fecha<="'.$xfecha2.'" then tegreso end) as tegre, 
			sum(tcant) as saldo
			from inve12
			where ';

$sql_invest.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';

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

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


$xfgrou='I';
if(isset($_GET['grupo']) && !empty($_GET['grupo']))
{$xfgrou=$_GET['grupo'];}


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

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

switch ($xfgrou)
{
	case 'D':$xgroup='docu,ptoe,numero,idnum1 ';break;
	case 'E':$xgroup='docu,ptoe,numero,idnum1 ';break;
	case 'I':$xgroup='item_lsub,item_descri ';break;

	case 'P':$xgroup='ptoe ';break;
	case 'V':$xgroup='vndd ';break;
	case 'Z':$xgroup='zsub ';break;
	case 'L':$xgroup='item_lsub ';break;
	case 'R':$xgroup='item_at10 ';break;
	case 'C':$xgroup='cdad ';break;

	case 'A':$xgroup='fanio ';break;
	case 'M':$xgroup='fanio,fmes ';break;
	case 'S':$xgroup='fanio,fsema ';break;
	case 'F':$xgroup='fecha ';break;

	default :$xgroup='';break;
}

switch ($xforde)
{
	case 'D':$xorder=$xgroup;break;
	case 'C':$xorder=str_replace('_descri','',$xgroup);break;
	case 'T':$xorder='ttotal desc, '.str_replace('_descri','',$xgroup);break;

	default :'fecha,signo desc, bode, docu, ptonumero';break;
}

$sql_invest.=' group by item order by lsub_descri,item';


$res_invest=mysql_query($sql_invest,$link_vcrx);
//$xttdcto=mysql_result($res_invest,0,'ttdcto');
//$xttice=mysql_result($res_invest,0,'ttice');
//$xttiva=mysql_result($res_invest,0,'ttiva');
//$xttirbp=mysql_result($res_invest,0,'ttirbp');

//$sql_inves.=' group by '.$xgroup;

// 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 (Items)</title>
<style type="text/css">
body{ font-family:"Courier New", Courier, monospace;}

 H1.SaltoDePagina
 {
     PAGE-BREAK-BEFORE: always
 }


</style>

</head>
<body>

	
<?php
//echo $sql_invest;

$res_invest=mysql_query($sql_invest,$link_vcrx);
//   echo explain_sql($sql_invest);
while($r_invest=mysql_fetch_assoc($res_invest))
{

$xitem=$r_invest['item'];
$sql_inves='select * from inve12 where ';
$sql_inves.='fecha >= "'.$fini1.'" AND fecha <= "'.$xfecha2.'"';
$sql_inves.=' and  (item like "'.$xitem.'" )';
if($xfbode!="")
{$sql_inves.=' and  (bode like "'.$xfbode.'" )';}
//$sql_inves.=' order by fecha,signo desc, bode, docu, ptonumero';
$sql_inves.=' order by fecha,(tingreso/tegreso), bode, docu, ptonumero';
$res_inves=mysql_query($sql_inves,$link_vcrx);


if(isset($_GET['idnum']) && !empty($_GET['idnum']))
{
//	$sql_inves.=' and  (item like "'.$_GET['idnum'].'%" )';
//	$xfctlg=$_GET['fctlg'];
}


?>
	
	
	
<div align="center">
<div align="center" style="width:1100px; font-size:12px;">
<div align="center" style="font-size:18px; border:#000 solid 1px; background: #F7F7F7; margin:2px; font-weight: bold;">INVENTARIO MOVIMIENTO</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>



<?php
switch ($xfgrou)
{
	case 'D':
?>

	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>NOMBRE</th>
	<th></th>


<?php	
	break;
	case 'E':
	$sql_clie='select * from enti00 where enti_codigo like "'.$_GET['idnum'].'%" ';
	$res_clie=mysql_query($sql_clie,$link_vcrx);
?>

<tr>
	<td><b>CLIENTE</td>
	<td><?php echo mysql_result($res_clie,0,'enti_codigo').' - '.mysql_result($res_clie,0,'enti_descri');?></td>
<tr>


<?php	
	break;
	case 'I':
	$sql_item='select * from item00 where item_codigo like "'.$_GET['idnum'].'" ';
	$res_item=mysql_query($sql_item,$link_vcrx);
?>

<tr>
	<td><b>ITEM</td>
	<td><?php echo mysql_result($res_item,0,'item_codigo').' - '.mysql_result($res_item,0,'item_descri');?></td>
<tr>
<tr>
	<td><b>LINEA/SUBLINEA</td>
	<td><?php echo mysql_result($res_item,0,'item_lsub').' - '.mysql_result($res_item,0,'lsub_descri');?></td>
<tr>
<tr>
	<td><b>MARCA/MODELO</td>
	<td><?php echo mysql_result($res_item,0,'item_mrca').' - '.mysql_result($res_item,0,'item_modelo');?></td>
<tr>
<tr>
	<td><b>BODEGA</td>
	<td>
	<?php
	if($xfbode=="")	{echo 'TODAS';}
	else	{echo $xfbode;}
	?>
<tr>

<?php	
	break;
	case 'M':
?>
	<th></th>
	<th>ANIO</th>
	<th>MES</th>
	<th>NOMBRE</th>
	<th></th>
<?php	
	break;
	case 'S':
?>
	<th></th>
	<th>ANIO</th>
	<th>SEMA</th>
	<th></th>
	<th></th>
<?php	
	break;
	case 'F':
?>
	<th></th>
	<th>ANIO</th>
	<th>MES</th>
	<th>FECHA</th>
	<th></th>

<?php	
	break;
	case 'P':
?>
	<th></th>
	<th>PTO/EMI</th>
	<th></th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'V':
?>
	<th></th>
	<th>VNDD</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'Z':
?>
	<th></th>
	<th>ZONA</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'L':
?>
	<th></th>
	<th>LINEA</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'R':
?>
	<th></th>
	<th>COD.RET.</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>


<?php } ?>



</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="1" width="90%" cellspacing="0" cellpadding="2" style="border-collapse:collapse">
<tr style="background: #F7F7F7;">
	<?php if($xfbode!=""){?>
<!-- 	<th align="center"><b>BODEGA</td> -->
	<?php } ?>

	<th align="center"><b>INICIAL</td>
	<th align="center"><b>INGRESOS</td>
	<th align="center"><b>EGRESOS</td>
	<th align="center"><b>SALDO</td>
</tr>
<tr>
	<?php if($xfbode!=""){?>
<!-- 	<th align="center"><?php echo $r_invest['bode_descri'];?></td> -->
	<?php } ?>
   	<td align="center"><?php if($r_invest['saldoi']<0){echo '<font color="red">';} ?><?php echo ''.number_format($r_invest['saldoi'],2);?></td>
   	<td align="center"><?php echo ''.number_format($r_invest['tingre'],2);?></td>
   	<td align="center"><?php echo ''.number_format($r_invest['tegre'],2);?></td>
   	<td align="center"><?php if($r_invest['saldo']<0){echo '<font color="red">';} ?><?php echo ''.number_format($r_invest['saldo'],2);?></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">
<?php
switch ($xfgrou)
{
	case 'D':
?>

	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>NOMBRE</th>
	<th></th>


<?php	
	break;
	case 'E':
?>

	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>


<?php	
	break;
	case 'I':
?>
	<th>DOCU</th>
	<th>PTO/NUMERO</th>
	<th>FECHA</th>
	<th>INGRESO</th>
	<th>EGRESO</th>

<?php	
	break;
	case 'M':
?>
	<th></th>
	<th>ANIO</th>
	<th>MES</th>
	<th>NOMBRE</th>
	<th></th>
<?php	
	break;
	case 'S':
?>
	<th></th>
	<th>ANIO</th>
	<th>SEMA</th>
	<th></th>
	<th></th>
<?php	
	break;
	case 'F':
?>
	<th></th>
	<th>ANIO</th>
	<th>MES</th>
	<th>FECHA</th>
	<th></th>

<?php	
	break;
	case 'P':
?>
	<th></th>
	<th>PTO/EMI</th>
	<th></th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'V':
?>
	<th></th>
	<th>VNDD</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'Z':
?>
	<th></th>
	<th>ZONA</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'L':
?>
	<th></th>
	<th>LINEA</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>

<?php	
	break;
	case 'R':
?>
	<th></th>
	<th>COD.RET.</th>
	<th>NOMBRE</th>
	<th></th>
	<th></th>


<?php } ?>


	<th>SALDO</th>
	<th>.</th>
<?php if($xfcost=='true'){?>
	<th>COST.U.</th>
	<th>SUB.T</th>
<?php } ?>	

<?php
switch ($xfgrou)
{
	case 'D':
?>
	<th>PROVINCIA /CIUDAD</th>
	<th>VENDEDOR</th>

<?php	
	break;
	case 'E':
?>
	<th>PROVINCIA /CIUDAD</th>
	<th>VENDEDOR</th>

<?php	
	break;
	case 'I':
?>
	<th>BODE</th>
	<th>ENTIDAD</th>
	<th>DETALLE</th>
	<th>ST</th>
<?php } ?>



</tr>

</div>


</tr>

<?php 
$filax=0;
$xsaldo = 0;
$xumes=0;
//$urcdd='';
$res_inves=mysql_query($sql_inves,$link_vcrx);
	//echo $sql_inves;
while($r_inves=mysql_fetch_assoc($res_inves))
{
$duni=$r_inves['item_duni'];
$dval=$r_inves['item_dval'];
$xsaldo=$xsaldo+$r_inves['tingreso']-$r_inves['tegreso'];

?>

<?php
if($r_inves['fecha']>=$xfecha1)
{
?>
<tr valign="top" 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++;} ?><?php if($xumes!=$r_inves['fmes']){echo 'border-top:solid 1px #000000;'; $xumes=$r_inves['fmes'];}?>" >

<?php
switch ($xfgrou)
{
	case 'D':
?>

        <td align="center"><?php echo'<span title="'.$r_inves['docu'].'">'.$r_inves['docu'].'</span>';?></td>
	<td align="center"><?php echo '<span title="'.$r_inves['ptonumero'].'">'.$r_inves['ptonumero'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_inves['fechadmy'].'">'.$r_inves['fechadmy'].'</span>';?></td>
	<td align="left"><?php echo'<span title="'.$r_inves['enti'].'">'.$r_inves['enti_descri'].'</span>';?></td>
	<td></td>


<?php	
	break;
	case 'E':
?>
	<td align="center"><?php echo '<span title="'.$r_inves['docu_descri'].'">'.$r_inves['docu'].'</span>';?></a> </td>
	<td align="center"><?php echo '<span title="'.$r_inves['ptonumero'].'">'.$r_inves['ptonumero'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_inves['fechadmy'].'">'.$r_inves['fechadmy'].'</span>';?></td>


<?php	
	break;
	case 'I':
?>
	<td align="center"><?php echo '<span title="'.$r_inves['docu_descri'].'">'.$r_inves['docu'].'</span>';?></a> </td>
	<td align="center"><?php echo '<span title="'.$r_inves['ptonumero'].'">'.$r_inves['numero'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_inves['fechadmy'].'">'.$r_inves['fechadmy'].'</span>';?></td>


<?php	
	break;
	case 'M':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_inves['fanio'].'">'.$r_inves['fanio'].'</span>';?></td>
	<td align="center"><?php echo '<span title="'.$r_inves['fmes'].'">'.$r_inves['fmes'].'</span>';?></a> </td>
	<td align="center"><?php echo'<span title="'.$r_inves['fmesn'].'">'.$r_inves['fmesn'].'</span>';?></td>
	<td></td>

<?php	
	break;
	case 'S':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_inves['fanio'].'">'.$r_inves['fanio'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_inves['fsema'].'">'.$r_inves['fsema'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'F':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_inves['fanio'].'">'.$r_inves['fanio'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_inves['fmesn'].'">'.$r_inves['fmesn'].'</span>';?></td>
	<td align="center"><?php echo '<span title="'.$r_inves['fechadmy'].'">'.$r_inves['fdian'].'-'.$r_inves['fdia'].'</span>';?></a> </td>
	<td></td>

<?php	
	break;
	case 'P':
?>
	<td></td>
	<td align="left"><?php echo '<span title="'.$r_inves['ptoe'].'">'.$r_inves['ptoe'].'</span>';?></a> </td>
	<td align="left"></td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'V':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_inves['vndd'].'">'.$r_inves['vndd'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_inves['vndd_descri'].'">'.$r_inves['vndd_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'Z':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_inves['zsub'].'">'.$r_inves['zsub'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_inves['zsub_descri'].'">'.$r_inves['zsub_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'L':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_inves['item_lsub'].'">'.$r_inves['item_lsub'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_inves['lsub_descri'].'">'.$r_inves['lsub_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>

<?php	
	break;
	case 'R':
?>
	<td></td>
        <td align="center"><?php echo'<span title="'.$r_inves['item_at10'].'">'.$r_inves['item_at10'].'</span>';?></td>
	<td align="left"><?php echo '<span title="'.$r_inves['at10_descri'].'">'.$r_inves['at10_descri'].'</span>';?></a> </td>
	<td align="left"></td>
	<td></td>



<?php }?>

   	<td align="right">&nbsp;<?php echo ''.number_format($r_inves['tingreso'],$duni);?></td>
   	<td align="right">&nbsp;<?php echo '<i>'.number_format($r_inves['tegreso'],$duni);?></td>

   	<td align="right">&nbsp;<?php if($xsaldo<0){echo '<font color="red">';} ?><?php echo '<b>'.number_format($xsaldo,$duni);?></td>



<?php if($xfcost=='true'){?>
<td></td>
   	<td align="right"><?php echo ''.number_format($r_inves['puni'],$dval);?></td>
   	<td align="right"><?php echo ''.number_format(abs($r_inves['puni']*$r_inves['cant']),$dval);?></td>
<?php } ?>	






<?php
switch ($xfgrou)
{
	case 'D':
?>

        <td align="center"><?php echo'<span title="'.$r_inves['cdad_descri'].'">'.$r_inves['cdad_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_inves['vndd'].'">'.$r_inves['vndd'].'</span>';?></td>

<?php	
	break;
	case 'E':
?>

	<td align="center"><?php echo'<span title="'.$r_inves['enti_telefono'].'">'.$r_inves['prvc_descri'].' /'.$r_inves['cdad_descri'].'</span>';?></td>
	<td align="center"><?php echo'<span title="'.$r_inves['vndd'].'">'.$r_inves['vndd'].'</span>';?></td>

<?php	
	break;
	case 'I':
?>
	<td align="left"></td>
	<td align="left"><?php echo'<span >'.$r_inves['bode'].'</span>';?></td>
	<td align="left"><?php echo'<span >'.$r_inves['enti_descri'].'</span>';?></td>
	<td align="left"><?php echo'<span >'.$r_inves['concepto'].' /'.$r_inves['detalle'].'</span>';?></td>
	<td align="center"><?php echo'<span >'.$r_inves['status'].'</span>';?></td>
<?php }?>



</tr>
<?php }?>

<?php }?>


<tr align="center" style="background: #F7F7F7; border-top:solid 1px#000;border-bottom:solid 1px#000">
	<th></th>
	<th>TOTALES</th>
	<th></th>
	<th align="right">&nbsp;<?php echo number_format($r_invest['tingre'],$duni)?></th>
	<th align="right">&nbsp;<?php echo number_format($r_invest['tegre'],$duni)?></th>
	<th align="right">&nbsp;<?php if($r_invest['saldo']<0){echo '<font color="red">';} ?><?php echo number_format($r_invest['saldo'],$duni)?></th>

<tr>

</table>



</div>
<br>
<H1 class=SaltoDePagina> </H1>
<?php
}	
?>	




