<?php
session_start();
include("../conexion.php");
//++++++++
if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("../login.php");	return;}

include("ventas_control_bx.php");
?>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Control de ventas - Buscar</title>
<!--INI_SCRIPT------------------------------------------------------------------------------>
<?php AddJs(); ?>
<script type="text/javascript" src="e3/funciones.js"></script>
<!--FIN_SCRIPT------------------------------------->
<link rel="stylesheet" type="text/css" href="e2/estiloss.css"  />
<link rel="stylesheet" type="text/css" href="e2/basic.css"  />
<style type="text/css">
.cF {height:600px;width:1200px;position:relative;top:0px}
</style>
<script>
$(document).ready(function(){  
   Inicializar();
   jF("Cod");
});
//
function fRecalcular(){
	
	sv("TXT_CAL","S");
	fFiltrar();
}
</script>
</head>

<body  <?php echo $LStrMsg;?>  >
<div align="center">

<div Id="DivFRM"  class="cF" style="background-color:#FFFFFF;" >
<form id="Frm" name="Frm" method="post" action="ventas_control_b.php">
<!--INI_TITULO-->
<div id="DivT" class="dT" style="width:1200px" >
<img src="../img/buscar.png" class="iT" style="width:62px; height:62px" />
<div class="tT">CONTROL DE VENTAS - BUSCAR</div>
</div>
<!--FIN_TITULO-->
<div class="s1" style="left: 116px; top: 41px; width: 140px; height: 16px; color:#FFF">
USUARIO: </div>

<div class="s10" style="left:35px; top:109px; width: 85px; height: 18px;">
EMPRESA</div>
<select name="Emp" id="Emp" 
class="lt" style="left:35px; top:126px; width: 126px;"
tabindex="<?php echo ++$TI;?>">
<?php
echo FCBO("emp_nomc","empresas","id_emp>0",$Emp,"S","N");
?>
</select>


<div class="s10" style="left:170px; top:109px; width: 85px; height: 18px;">
MES</div>
<select name="Mes" id="Mes" 
class="lt" style="left:170px; top:126px; width: 126px;"
tabindex="<?php echo ++$TI;?>">
<?php
echo FCBO("mes","meses","id_mes>0",$Mes,"S","N");
?>
</select>

<?php if(strlen($Ani)==0)$Ani=date("Y");?>
<div class="s10" style="left:304px; top:109px; width: 85px; height: 18px;">
AÑO</div>
<select name="Ani" id="Ani" 
class="lt" style="left:304px; top:126px; width: 95px;"
tabindex="<?php echo ++$TI;?>">
<?php
echo FCBO("a_anio","anios","id_a>0",$Ani,"S","N");
?>
</select>

<input name="CmdK" type="button"  value="Recalcular" 
onClick="fRecalcular()"
style="position:absolute; top:126px; left:412px; width: 131px;">
<?php if(strlen($Dia)==0)$Dia=date("d");?>
<!--INI_BOTONES----->
<?php 
$VOLVER="ventas_control.php";
?>
<?php 
include("botones_b.php");
?>
<!--FIN_BOTONES-->
<!--INI_TABLA----->
<div class="datagrid" style="position:absolute; left:35px; top: 166px; width: 1126px; height: 407px;">
<table width="100%">
<thead>
<tr>
<td colspan="19" height="18">
<?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?>
</td>
</tr>
<tr>
<th width="2%">&nbsp;</th>
<th width="6%">EMPRESA</th>
<th width="3%">MES</th>
<th width="3%">AÑO</th>
<th width="7%">TOTAL</th>
<th width="4%">TOTAL VENTA</th>
<th width="6%" align="center">TOT. FACTURA</th>
<th width="5%" align="center">TOT. BOLETA</th>
<th width="5%" align="center">TOT. OTROS</th>
<th width="4%" align="center">TOT. NDC</th>
<th width="7%" align="center">TOTAL VENTA menos T.NDC</th>
<th width="7%" align="center">DIFE- RENCIA</th>
<th width="10%" align="center">FECHA CALCULADO</th>
<th width="2%">&nbsp;</th>
<th width="5%" align="center">AL DIA</th>
<th width="7%">&nbsp;</th>

<th width="4%" align="center">DIA</th>
<th width="7%" align="center">TOTAL VENTA menos T.NDC</th>
<th width="6%" align="center">DIFE- RENCIA</th>

</tr>
</thead>
<tbody>
<?php
$CX=0;
$s="SELECT * FROM vis_ventas_control WHERE ".$FF." ORDER BY ANI_ DESC,MES_ DESC";
$s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);	  				
$rs = grs($s);
while ($r = $rs->fetch_assoc()){
	$CX++;
	$CSS="";
	if($TXT_CAL=="S"){
		$KOD=$r['ID'];
		$EWP=$r['EMP_'];
		$MES=$r['MES_'];
		$ANI=$r['ANI_'];
		$TOT=$r['TOT'];
		$FEC=date("Y-m-d");		
		//++++
		$SUM_TOT=round(gn("SUM(TOT)","vis_ventas_control_ex",
					   "ANU='N' AND YEAR(FEC)=$ANI AND MONTH(FEC)=$MES AND EMP_ID=$EWP"),3);
		$SUM_FAC=round(gn("SUM(TOT)","vis_ventas_control_ex",
					   "(COM_ID=1 OR COM_ID=3) AND ANU='N' AND YEAR(FEC)=$ANI AND MONTH(FEC)=$MES AND EMP_ID=$EWP"),3);
		$SUM_BOL=round(gn("SUM(TOT)","vis_ventas_control_ex",
					   "(COM_ID=2 OR COM_ID=4) AND ANU='N' AND YEAR(FEC)=$ANI AND MONTH(FEC)=$MES AND EMP_ID=$EWP"),3);					
		$SUM_NDC=round(gn("SUM(TOT)","vis_fe_ventas_ex",
					  "COM_ID=5 AND ANU='N' AND YEAR(FEC)=$ANI AND MONTH(FEC)=$MES AND EMP_ID=$EWP"),3);		
		//						  
		$SUM_TOT_M_NDC=round($SUM_TOT - $SUM_NDC,2);	
		//
		$SUM_OTR=round($SUM_TOT - ($SUM_FAC+$SUM_BOL),2);
		$DIF=round($SUM_TOT_M_NDC - $TOT ,2);
		//
		$s="UPDATE ventas_control 
			SET vc_cal=$SUM_TOT,
				vc_bol=$SUM_BOL,
				vc_fac=$SUM_FAC,
				vc_otr=$SUM_OTR,
				vc_ndc=$SUM_NDC,
				vc_cal_m_ndc=$SUM_TOT_M_NDC,
				vc_fec='".$FEC."',
				vc_dif=$DIF WHERE id_vc=$KOD";
		ejt($s);
		//
		$r['CAL']=$SUM_TOT;
		$r['FAC']=$SUM_FAC;
		$r['BOL']=$SUM_BOL;
		$r['OTR']=$SUM_OTR;
		$r['NDC']=$SUM_NDC;
		$r['CAL_M_NDC']=$SUM_TOT_M_NDC;
		$r['DIF']=$DIF;
		$r['FEC_']=date("d-m-y");		
	}
	if($CX%2==0)$CSS=' style="background-color:#CCC" ';
	//
	$CBO="CBO_".$r['ID'];
?>
	<tr onDblClick="location.href='ventas_control.php?Id=<?php echo $r['ID'];?>&Pag=0'" <?php echo $CSS;?> >
    <td bgcolor="#FFFFFF" align="center">
	<a href="ventas_control.php?Id=<?php echo $r['ID'];?>&Pag=0">
	<img src="../img/b_edit.png"  border="0"/></a></td>
	<!----------------------------------------------->
   	<td ><?php echo $r['EMP'];?></td>
	<td ><?php echo $r['MES'];?></td>
    <td ><?php echo $r['ANI_'];?></td>
    <td align="right"><?php echo $r['TOT'];?></td>
    
    <td align="right"><?php echo NF2($r['CAL'],2);?></td> 
    <td align="right"><?php echo NF2($r['FAC'],2);?></td> 
    <td align="right"><?php echo NF2($r['BOL'],2);?></td> 
    <td align="right"><?php echo NF2($r['OTR'],2);?></td> 
    <td align="right"><?php echo NF2($r['NDC'],2);?></td>
    <td align="right"><?php echo NF2($r['CAL_M_NDC'],2);?></td>
    <td align="right"><?php echo NF2($r['DIF'],2);?></td> 
    <td align="right"><?php echo $r['FEC_'];?></td> 
    <td width="2%">&nbsp;</td>
    
    <td width="5%">
    <select name="<?php echo $CBO;?>" id="<?php echo $CBO;?>" 
    class="lt" style="left:553px; top:126px; width: 50px; position:inherit"
    tabindex="<?php echo ++$TI;?>">
    <?php
    echo FCBOO("dias","dias","dia>0","","N","S","dia");
    ?>
    </select>
    </td>
    
    <td width="7%">
    <input name="" onClick="fProyectar('<?php echo $r['ID'];?>')" value="Proyectar" type="button" style="position:inherit">
    </td>

    <td width="4%" id="TD_DIA_<?php echo $r['ID'];?>" align="right"><?php echo $r['P_DIA'];?></td>
    <td width="7%" id="TD_TOT_<?php echo $r['ID'];?>" align="right"><?php echo NF2($r['P_TOT'],2);?></td>
    <td width="6%" id="TD_DIF_<?php echo $r['ID'];?>" align="right"><?php echo NF2($r['P_DIF'],2);?></td>
	<!----------------------------------------------->
	
	</tr>
<?php
}
?>
</tbody>
</table>
<script>
function fProyectar(id){
	var dia=gv("CBO_"+id);
	if(jL(dia)==0){
		alert("Seleccione un dia");
		jF("CBO_"+id);
		return;
	}
	var d=AX("PROYECTAR",id,dia);
	var r=d.split("|");
	if(r[1]=="OK"){
		document.getElementById("TD_DIA_"+id).innerHTML=r[2];
		document.getElementById("TD_TOT_"+id).innerHTML=r[3];
		document.getElementById("TD_DIF_"+id).innerHTML=r[4];
		alert("Proceso completado");
	}else{
		alert("Error de conexion");
	}
}
</script>
</div>
<!--FIN_TABLA----->
<?php include("ocultos.php");?>
<input name="TXT_CAL" type="hidden" id="TXT_CAL" value="" />
</form>
</div>
</div>
</body>
</html>
