<?php
ini_set("session.cookie_lifetime",60*60*10);
ini_set("session.gc_maxlifetime", 60*60*10);
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
	//
	if(vbval($_SESSION['SYS_USER_GI'])!=USU_GET_GI($_SESSION['SYS_USER_ID'])){
		JsDir("cerrar.php");return;
	}
	$B=V($_GET['B']);
	//
	$StrHWND=$_SESSION['SYS_HW']."_RES";
	//
	$StrCmd=V($_POST['TxtAc']);
	//		
	$StrEmp=V($_POST['CboEmp']);	
	$StrDex=V($_POST['TxtDex']);		
	$StrHax=V($_POST['TxtHax']);		
	//
	if(strlen($StrEmp)==0)$StrEmp=GE("N");
	//
	$StrEmp_=LIKE($StrEmp);
	if(strlen($StrDex)==0)$StrDex=date("d-m-y");
	$StrDex_=val_dex($StrDex);
	if(strlen($StrHax)==0)$StrHax=date("d-m-y");
	$StrHax_=val_dex($StrHax);
	//
	if($B=="N"){
		$StrDex_=date("Y-m-d");
		$StrHax_=date("Y-m-d");
	}
	//
	$CX=1;
	ejt("DELETE FROM resumen ");
	$s="SELECT * FROM vcotizaciones ".
	   "WHERE FEC>='$StrDex_' AND FEC<='$StrHax_' AND ANU='N' AND PRI_='1' AND EMPC LIKE '$StrEmp_'";	
	$result = grs($s);				
	while ($row = $result->fetch_assoc()){
		$_ID=$row['ID'];
		$_TD="P";
		$_NU=$row['SER']."-".$row['NUM'];
		$_FE=$row['FEC'];
		$_GI=$row['GI'];
		$_TO=$row['TOT_REAL'];
		$_CL=$row['CLI'];
		//
		$DEV=gn("SUM(dev_total)","devoluciones",
				"dev_id_comprobante=-3 AND dev_id_ref=$_ID AND dev_anu='N' AND dev_tipo='D' AND dev_dinero='0'");
		$DEJ=gn("SUM(dev_total)","devoluciones",
				"dev_id_comprobante=-3 AND dev_id_ref=$_ID AND dev_anu='N' AND dev_tipo='J' AND dev_dinero='0'");
	    //echo $DEJ."@";
		$ADE=gn("acu - vue","cotizaciones","id_cotizacion=$_ID");
		$COB=gn("SUM(cob_tot)","cobroz",
				"cob_id_com=-3 AND cob_icser=".$row['SER']." AND cob_icnum=".$row['NUM']." AND cob_anu='NO'");
		$ACU=$ADE + $COB;
		//
		$s="INSERT INTO resumen ".
		   "VALUES ($CX,'$StrHWND',$_ID,'$_TD','$_NU','$_FE',$_GI,$_TO,'$_CL',0,$DEV,$DEJ,$ACU)";
		ejt($s);
		$CX++;
	}
	//
	$s="SELECT * FROM vventas ".
	   "WHERE FEC>='$StrDex_' AND FEC<='$StrHax_' AND PRI_=1 AND ANU='N' AND EMPC LIKE '$StrEmp_' "; 
	$result = grs($s);				
	while ($row = $result->fetch_assoc()){
		$_ID=$row['ID'];
		$_TC=$row['COM_'];
		$_TD=substr($row['COM'],0,1);
		$_NU=$row['SER']."-".$row['NUM'];
		$_FE=$row['FEC'];
		$_GI=$row['GI'];
		$_TO=$row['TOT'];
		$_CL=$row['CLI'];
		//
		$DEV=gn("SUM(dev_total)","devoluciones",
				"dev_id_comprobante<>-3 AND dev_id_ref=$_ID AND dev_anu='NO' AND dev_tipo='D' AND dev_dinero='0'");
		$DEJ=gn("SUM(dev_total)","devoluciones",
				"dev_id_comprobante<>-3 AND dev_id_ref=$_ID AND dev_anu='NO' AND dev_tipo='J' AND dev_dinero='0'");
		$ADE=gn("acuenta","ventas","id_venta=$_ID");
		$COB=gn("SUM(cob_tot)","cobroz",
				"cob_id_com=$_TC AND cob_icser=".$row['SER']." AND cob_icnum=".$row['NUM']." AND cob_anu='NO'");
		$ACU=$ADE + $COB;
		//
		$s="INSERT INTO resumen ".
		   "VALUES ($CX,'$StrHWND',$_ID,'$_TD','$_NU','$_FE',$_GI,$_TO,'$_CL',0,$DEV,$DEJ,$ACU)";
		ejt($s);
		$CX++;
	}
	//
	$CX=1;
	$s="SELECT * FROM resumen ORDER BY fec,gi";
	$result = grs($s);				
	while ($row = $result->fetch_assoc()){
		$s="UPDATE resumen ".
		   "SET cx=$CX WHERE gi=".$row['gi']." AND hw='$StrHWND'";
		ejt($s);
		$CX++;
	}
	//
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reporte - Resumen de cobros / devoluciones</title>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="jquery.maskedinput.min.js" type="text/javascript"></script>
<script type="text/javascript" src="fe/funciones.js"></script>

<script language="javascript">
$(document).ready(function() {
	$(".botonExcel").click(function(event) {
		$("#datos_a_enviar").val( $("<div>").append( $("#Exportar_a_Excel").eq(0).clone()).html());
		$("#FormularioExportacion").submit();
});
});
</script>
<script>
$(document).ready(function(){
$("#TxtDex").mask("99-99-99");
$("#TxtHax").mask("99-99-99");
});
</script>
<link rel="stylesheet" type="text/css" href="basic.css"  />
<style type="text/css">
<!--
body {
	background-color: #CCC;
}
#Layer1 {
	position:absolute;
	left:345px;
	top:115px;
	width:123px;
	height:35px;
	z-index:1;
}
.centrarFrm{
	width:1000px;
	height:600px;
	position:absolute;
	left:<?php echo "50%";?>;
	top:<?php echo "50%";?>;
	background-color:#006699;
	margin:<?php echo "-300px";?> 0 0 <?php echo "-500px";?>;
}
#Layer2 {
	position:absolute;
	left:409px;
	top:205px;
	width:82px;
	height:24px;
	z-index:1;
}
.Estilo1 {
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
	color: #FFFFFF;
}
.f2 {
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
	color: #999;
	font-size: 12px;
}
.Estilo3 {color: #FFFFFF; font-size: 12px; font-family: Arial, Helvetica, sans-serif;}
.s4 {
	color: #000099;
	text-align: right;
}
.s5 {color: #000000}

-->
</style>

</head>
<body  <?php echo $StrMsg;?>>
<div Id="DivFRM"  class="centrarFrm" >  
<form id="Frm" name="Frm" method="post" action="rpt_resumen.php">
<center> 
  

<input name="CmdV" type="button" id="CmdV" 
style="position:absolute; left: 116px; top: 50px; width: 60px; "
value="Volver" onclick="fVolver('menu.php');" />
<input type="hidden" name="TxtAc" id="TxtAc" />
<input name="FEC_SERVER" type="hidden" id="FEC_SERVER" value="<?php echo date("d-m-y");?>"/>  

<div id="layer3" 
style="position:absolute; left: 19px; top: 183px; width: 966px; height: 390px; overflow:auto">
<div align="left" class="f2">
<table id="Exportar_a_Excel" width="950" height="19" border="1" cellpadding="0" cellspacing="0">
<tr  >
<td height="18" colspan="8" bgcolor="#666666" align="center">
</td>

</tr>
<tr>
<td width="86" bgcolor="#999999" style="color:#000">GUIA</td>
<td width="77" bgcolor="#999999" style="color:#000">FECHA</td>
<td width="72" bgcolor="#999999" style="color:#000">TOTAL</td>
<td width="361" bgcolor="#999999" style="color:#000">CLIENTE</td>
<td width="74" bgcolor="#999999" align="right" style="color:#000" >A CUENTA</td>
<td width="95" bgcolor="#999999" align="right" style="color:#000" >T.DEVO.</td>
<td width="83" bgcolor="#999999" align="right" style="color:#000" >T.DEJA</td>
<td width="84" bgcolor="#999999" align="right" style="color:#000" ></td>
</tr>
<?php	
$T_TOT=0; 				  		
$T_ACU=0; 				  		
$T_DEV=0; 				  		
$T_DEJ=0; 				  		
$T_SAL=0; 				  		
$s="SELECT tdd,num,DATE_FORMAT(fec,'%d-%m-%y') as fex,tot,SUBSTRING(cli,1,42) as cli_,acuenta,devo,deja ".
"FROM resumen WHERE hw='$StrHWND' ORDER BY cx";
$X1="<td bgcolor=\"#FFFFFF\"><span class=\"s5\">";
$X1B="<td bgcolor=\"#FFFFFF\" align=\"right\"><span style=\"color: #000;\">";
$X2="</span></td>";
$result = grs($s);				
while ($row = $result->fetch_assoc()){
$_GIA=$row['tdd'].$row['num'];
$_FEC=$row['fex'];
$_TOT=$row['tot'];
$_CLI=$row['cli_'];
$_ACU=$row['acuenta'];
$_DEV=$row['devo'];
$_DEJ=$row['deja'];
$_SAL=$_TOT - ($_ACU + $_DEV +$_DEJ);
//
$T_TOT=$T_TOT + $_TOT;
$T_ACU=$T_ACU + $_ACU;
$T_DEV=$T_DEV + $_DEV;
$T_DEJ=$T_DEJ + $_DEJ;
$T_SAL=$T_SAL + $_SAL;
//
echo "<tr>";
echo $X1.$_GIA.$X2;
echo $X1.$_FEC.$X2;
echo $X1B.NF3($_TOT).$X2;
echo $X1.$_CLI.$X2;
echo $X1B.NF3($_ACU).$X2;
echo $X1B.NF3($_DEV).$X2;					
echo $X1B.NF3($_DEJ).$X2;					
echo $X1B."".$X2;
echo "</tr>";
}

?>		  
<tr  >
<td colspan="8" >&nbsp;</td>
</tr>
<tr  >
<td bgcolor="#FFFFFF">&nbsp;</td>
<td bgcolor="#FFFFFF"><span style="text-align:right; color: #000;">TOTAL</span></td>
<td bgcolor="#FFFFFF" align="right" ><span style="color: #000;"><?php echo NF3($T_TOT);?></span></td>
<td bgcolor="#FFFFFF"  align="right" ></td>
<td bgcolor="#FFFFFF"  align="right"><span style="text-align:right; color: #000;"><?php echo NF3($T_ACU);?></span></td>
<td  bgcolor="#FFFFFF" align="right" ><span style="text-align:right; color: #000;"><?php echo NF3($T_DEV);?></span></td>
<td  bgcolor="#FFFFFF"  align="right"><span style="text-align:right; color: #000;"><?php echo NF3($T_DEJ);?></span></td>
<td  bgcolor="#FFFFFF"  align="right"><span style="text-align:right; color: #000;"></span></td>
</tr>
</table>
</div>
</div>
<div id="layer" style="position:absolute; left: 116px; top: 30px; width: 622px; height: 20px;">
<div align="left" class="Estilo1">REPORTE - RESUMEN DE COBROS / DEVOLUCIONES</div>
</div>
<div id="LblTit" style="position:absolute; left: 57px; top: 25px; width: 64px;">
<div align="left" class="Estilo1"><img src="img/buscar.png" width="57" height="60" /></div>
</div>	
<input name="CmdB" type="button" id="CmdB" 
style="position:absolute; left: 344px; top: 150px; width: 64px;"
value="Filtrar" onclick="fFiltrar();" />
<div class="s1" style="left: 29px; top: 99px; width: 179px;">
FECHA DE EMISION (VENTA)
</div>
<div class="s1" style="left: 149px; top: 117px; width: 55px;">
DESDE
</div>

<div class="s1" style="left:22px; top: 132px; width: 105px;"> EMPRESA
</div>
<select name="CboEmp" id="CboEmp" 
style="position:absolute; left:19px; top:150px; width: 120px;" 
onkeydown="return SoloTextoEx(event,'TxtDex');" tabindex="1">
<?php 
	echo LE($StrEmp,"S","N"); 
?>
</select>

<div class="EV" style="left: 149px; top: 133px; width: 88px; color:#FF0"><u><a onclick="fFecServer('TxtDex')">(dd-mm-yy) +</a></u></div>
<input name="TxtDex" type="text" id="TxtDex" 
style="left: 149px; top: 150px; width: 84px;" value="<?php echo $StrDex;?>"  
onkeydown="return SoloFec(this,event,'TxtHax');"
tabindex="1" />
<div class="s1" style="left: 245px; top: 117px; width: 45px;">
HASTA
</div>
<div class="EV" style="left: 246px; top: 133px; width: 88px; color:#FF0"><u><a onclick="fFecServer('TxtHax')">(dd-mm-yy) +</a></u></div>
<input name="TxtHax" type="text" id="TxtHax" 
style="left: 245px; top: 150px; width: 84px;" value="<?php echo $StrHax;?>"  
onkeydown="return SoloFec(this,event,'CmdB');"
tabindex="1" />


</center>	
</form>
<form action="ficheroExcel.php" 
method="post" target="_blank" 
id="FormularioExportacion" >
<div 
style="position:absolute; left: 416px; top: 150px; width: 51px; height: 14px;">  
<input name="" type="button" style="height: 23px;"  class="botonExcel" value="Exportar" />
<input type="hidden" id="datos_a_enviar" name="datos_a_enviar" />
</div>
</form>
</div>
</body>
</html>
