<?php
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	if(strlen($_SESSION['SYS_USER_ID'])==0){JsDir("login.php");	return;}
	$StrId=V($_POST['TxtId']);
	$StrCmd=V($_POST['TxtAc']);
	//-------------------------
	$StrAlm=V($_POST['CboAlm']);			
	$StrDex=V($_POST['TxtDex']);		
	$StrHax=V($_POST['TxtHax']);					
	//-------------------------
	$IdUsu=$_SESSION['SYS_USER_ID'];
	
	if($_SESSION['SYS_USER_ALM']=="%"){
		if(strlen($StrAlm)==0)$StrAlm=GetSO("alm_nombre","almacenes","","alm_nombre");
	}else{
		$StrAlm=$_SESSION['SYS_USER_ALM'];
	}
	
	$StrAlm=T($StrAlm);

	//-------------------------	
	$StrDex_=VDex($StrDex);
	$StrHax_=VHax($StrHax);
	//-------------------------
	$FF="ID>0 
	AND ANU='N' 
	AND FEC>='$StrDex_' 
	AND FEC<='$StrHax_' 
	AND ALM LIKE '$StrAlm' ";
	//-------------------------
	$LStrTR=gn("count(*)","vventas_det",$FF);
	//-------------------------
	$LStrPag=V($_POST['TxtPag']);
	if($StrCmd=="F"){$LStrPag="1";}
	$VP_RegXPag=50000;	
	$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);
	

$AT=gs("at","usuarios","id_usuario=$IdUsu");

?>

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reporte de ventas agrupado por marca y descripcion</title>
<!----------------------------------------->
<?php AddJs(); ?>
<script type="text/javascript" src="ja/funciones.js"></script>

<!----------------------------------------->
<link rel="stylesheet" type="text/css" href="css/estiloss.css"  />
<link rel="stylesheet" type="text/css" href="css/basic.css"  />
<link rel="stylesheet" type="text/css" href="css/mnu.css"  />
<!----------------------------------------->
<style type="text/css">
.cF {height:600px;width:1250px;position:relative;top:0px}
.mM {height:34px;width:1250px;position:relative;top:0px;background-color:#09C; z-index:500;}

</style>

<!----------------------------------------->
<script>
$(document).ready(function(){  
   Inicializar();
   jF("CboAlm");
});

function fGenPDF(){	
	document.getElementById("TxtAc").value="GEN_PDF";
	document.getElementById("Frm").submit();
}

function AjImprimir(){
	document.getElementById("CmdI").disabled=true;
	DImp=document.getElementById("DivP");
	DImp.style.visibility="visible";
	DImp.innerHTML="<img src='load2.gif' width='15' height='15' />";	
	//
	var CboAlm=gv("CboAlm");
	var TxtDex=gv("TxtDex");
	var TxtHax=gv("TxtHax");
	//
	var stru="rpt_ventas_axm_z.php?CboAlm="+CboAlm+
	"&TxtDex="+TxtDex+
	"&TxtHax="+TxtHax;
	//a=prompt(stru,stru);
	try{	
	ajax=CreateAJAX();
	ajax.open("GET",stru);	
	ajax.onreadystatechange=function() {
	   if (ajax.readyState==4) {
		   var Rpt = (ajax.responseText);
		   Rpt=Rpt.trim();		    
		   if(jLen(Rpt)>0){
			   DImp.innerHTML=Rpt;	
		   }else{
			   DImp.innerHTML="";
			   alert("No se pudo generar el reporte");
		   }
		   document.getElementById("CmdI").disabled=false;
	   }
	}
    ajax.send(null);
	}
	catch(err){
	}
	//	
}


$(document).keydown(function(e){
	var AT=gv("TxtAT");

	if(e.which==66 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdB").click();
		return;
		}
	}

	if(e.which==86 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdZ").click();
		return;
		}
	}
  
});
</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>
<!----------------------------------------->
<body  <?php echo $LStrMsg;?>  >
<div align="center">

<div class="mM" align="left" style="font-size:12px">
<?php
$HTM=file_get_contents("http://inversionessounduniversal.com/ventas/phpmnu.php?USER_ID=$IdUsu");
echo $HTM;
?>
</div>

<div Id="DivFRM"  class="cF" style="background-color:#FFFFFF;" >
<form id="Frm" name="Frm" method="post" action="rpt_ventas_axm2.php" 
style="margin:0px; padding:0px; display:inline;" onSubmit="return false;">

<!----------------------------------------->
<div id="DivT" class="dT"  style="width:1250px;">
<img src="b.png" class="iT" />
<div class="tT">REPORTE DE VENTAS AGRUPADO POR MARCA Y DESCRIPCION</div>
</div>

<!----------------------------------------->

<div class="s1" style="left: 35px; top: 121px; width: 102px;">
ALMACEN</div>
<select name="CboAlm" id="CboAlm" 
class="lt" style="left: 35px; top: 138px; width: 205px" 
tabindex="<?php echo ++$TI;?>">
<?php 
	if($IdUsu>=1  && V($_SESSION['SYS_USER_ALM'])!="%"){
		echo ListALM($StrAlm); 
	}else{
    	echo FCBO("alm_nombre","almacenes","id_almacen>0",$StrAlm,"N","N");
    }
?>
</select>

<div class="s1" style="left: 249px; top: 121px; width: 45px;">
DESDE</div>
<input name="TxtDex" type="text" id="TxtDex" 
class="fe" style="left: 249px; top: 138px; width: 65px" 
value="<?php echo $StrDex;?>"  
tabindex="<?php echo ++$TI;?>"/>

<div class="s1" style="left: 328px; top: 121px; width: 60px;">
HASTA</div>
<input name="TxtHax" type="text" id="TxtHax" 
class="fe" style="left: 327px; top: 138px; width: 65px" 
value="<?php echo $StrHax;?>"  
tabindex="<?php echo ++$TI;?>"/>


<!----------------------------------------->
<div id="DivBtn" align="left" style="position:absolute; left: 33px; top: 71px; width: 551px; height: 26px;">
<input name="CmdB" type="button" id="CmdB" class="btm"
value="Buscar" onClick="fFiltrar();"  
tabindex="<?php echo ++$TI;?>" />

<input name="CmdPDF" type="button" id="CmdPDF" class="btm" 
value="Generar PDF" onClick="fGenPDF();"
tabindex="<?php echo ++$TI;?>" />

<input name="CmdB" type="button" id="CmdI" class="btm" 
value="Generar TXT" onClick="AjImprimir();" 
tabindex="<?php echo ++$TI;?>"/>
</div>
    
<!----------------------------------------->

<div id="DivP" style="visibility: hidden; position: absolute; text-align: center; background: #FF0; left: 860px; top: 102px; width: 90px; left: 220px;">
Descarga</div>


<div class="datagrid" style="position: absolute; left: 35px; top: 165px; width: 1180px; height: 409px; overflow: auto">
<table id="Exportar_a_Excel" width="75%">
<thead>
<tr>
<td colspan="5" align="center"><?php echo PagBtn($LStrTR,$VP_RegXPag,$LStrPag);?></td>
</tr>

<tr>
<th width="70" style="text-align:left; background:#999">CODIGO</th>
<th width="43" style="text-align:center; background:#999">CANT</th>
<th width="881" style="text-align:left; background:#999"">MARCA / DESCRIPCION</th>
<th width="94" style="text-align:left; background:#999"">PRECIO</th>
<th width="66" style="text-align:left; background:#999"">IMPORTE</th>
</tr>
</thead>
<tbody>

<?php 	
	$SUMA=0;	
	$SUM_IMP=0;
	$s="SELECT COD,SUM(CA) as KA, MAR,DES,ROUND(ROUND(SUM(IM),2)/SUM(CA),2) as PRE,ROUND(SUM(IM),2) as IMP ".
	   "FROM vventas_det WHERE ".$FF." GROUP BY COD,MAR,DES ORDER BY MAR,DES ";

	$X1="<td bgcolor=\"#FFFFFF\"><span class=\"Estilo5\">";
	$X1B="<td bgcolor=\"#FFFFFF\" align=\"right\"><span class=\"Estilo5\">";
	$X2="</span></td>";	
	//$s=PagSql($s,$LStrPag,$VP_RegXPag,$LStrTR);
	$result = grs($s);				
	while ($row = $result->fetch_assoc()){
		$CAN_=$row['KA'];
		$DES_=$row['MAR']." ".$row['DES'];
		echo "<tr >";
		echo $X1.$row['COD'].$X2;
		echo $X1B.$CAN_.$X2;
		echo $X1.$DES_.$X2;
		echo $X1B.$row['PRE'].$X2;
		echo $X1B.$row['IMP'].$X2;
		echo "</tr>";	
	    $SUMA=$SUMA+$row['KA'];
		$SUM_IMP=$SUM_IMP+$row['IMP'];
	}

?>
  
<tr>
<td colspan="5" bgcolor="#999999" ><span class="x4">TOTAL CANT :</span> <?php echo $SUMA;?></td>
</tr>

<tr>
<td colspan="5" bgcolor="#999999" ><span class="x4">TOTAL IMPORTE :</span> <?php echo NF2($SUM_IMP,2);?></td>
</tr>

</tbody>
</table>
</div>

<!----------------------------------------->
<?php include("ocultos.php");?> 

</form>

<form action="ficheroExcel.php" 
    method="post" target="_blank" id="FormularioExportacion" >
    
    <div style="position: absolute; left: 316px; top: 71px; width: 45px; height: 15px;">  
    <input name="" type="button" class="botonExcel btm" value="Exportar"  />
    <input type="hidden" id="datos_a_enviar" name="datos_a_enviar" />
    </div>
</form>

<?php
if($StrCmd=="GEN_PDF"){
	$_PDF=GEN_PDF($FF,$StrDex,$StrHax);
	$StrDES_2=$_PDF;
}

function GEN_PDF($FF,$StrDex,$StrHax){
$_CxL=90;
$_S=10;	

$pdf = new PDF('P','mm',A4);
$pdf->AddPage();$pdf->SetFont('courier','',10);
$pdf->Cell(0,4,"",0,1);	
$pdf->Cell(0,4,"REP. VENTAS. AGRUP. POR MARCA Y DESCRIPCION",0,1);	
$pdf->Cell(0,4,"",0,1);	
//------------------------------------------

if(strlen($StrAlm)>0){
$_ENC=vbl("ALMACEN".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrAlm.vbfill(" ",30),30);
$pdf->Cell(0,4,$_ENC,0,1);	
}


if(strlen($StrDex)>0){
$_ENC=vbl("DESDE".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrDex.vbfill(" ",30),30);
$pdf->Cell(0,4,$_ENC,0,1);	
}

if(strlen($StrHax)>0){
$_ENC=vbl("HASTA".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrHax.vbfill(" ",30),30);
$pdf->Cell(0,4,$_ENC,0,1);	
}

$pdf->Cell(0,4,"",0,1);
$pdf->Cell(0,4,"",0,1);
//------------------------------------------
$P=0;
$P++;	$T[$P]=10;	$_ENC=vbl("CODIGO".vbfill(" ",$T[$P]),$T[$P]);
$P++;	$T[$P]=8;	$_ENC=$_ENC." ".vbr(vbfill(" ",$T[$P])."CANT",$T[$P]);
$P++;	$T[$P]=52;	$_ENC=$_ENC." ".vbl("MARCA / DESCRIPCION".vbfill(" ",$T[$P]),$T[$P]);
$P++;	$T[$P]=6;	$_ENC=$_ENC." ".vbr(vbfill(" ",$T[$P])."PRECIO",$T[$P]);
$P++;	$T[$P]=10;	$_ENC=$_ENC." ".vbr(vbfill(" ",$T[$P])."IMPORTE",$T[$P]);
$pdf->Cell(0,4,$_ENC,0,1);
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	

$s="SELECT COD,SUM(CA) as KA, MAR,DES,ROUND(ROUND(SUM(IM),2)/SUM(CA),2) as PRE,ROUND(SUM(IM),2) as IMP FROM ".
   "vventas_det WHERE ".$FF." GROUP BY COD,MAR,DES ORDER BY MAR,DES ";
$SUMA=0;
$SUMA_I=0;
$result = grs($s);
while ($row = $result->fetch_assoc()){
	$CAN_=$row['KA'];	
	$DES_=$row['MAR']." ".$row['DES'];	
	$P=0;	
	$P++;	$_ENC=vbl($row['COD'].vbfill(" ",$T[$P]),$T[$P]);	
	$P++;	$_ENC=$_ENC." ".vbr(vbfill(" ",$T[$P]).$CAN_,$T[$P]);
	$P++;	$_ENC=$_ENC." ".vbl($DES_.vbfill(" ",$T[$P]),$T[$P]);
	$P++;	$_ENC=$_ENC." ".vbr(vbfill(" ",$T[$P]).$row['PRE'],$T[$P]);	
	$P++;	$_ENC=$_ENC." ".vbr(vbfill(" ",$T[$P]).$row['IMP'],$T[$P]);	
	$pdf->Cell(0,4,$_ENC,0,1);
	$SUMA=$SUMA+$CAN_;
	$SUMA_I=$SUMA_I+$row['IMP'];
}

$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
$_TI=vbr(vbfill(" ",75)."TOTAL CANT:",75);
$_TD=vbr(vbfill(" ",14).$SUMA,14);
$pdf->Cell(0,4,$_TI." ".$_TD,0,1);	

$_TI=vbr(vbfill(" ",75)."TOTAL IMPORTE:",75);
$_TD=vbr(vbfill(" ",14).$SUMA_I,14);
$pdf->Cell(0,4,$_TI." ".$_TD,0,1);	


$_S=20;
$pdf->Cell(0,4,"",0,1);	
$pdf->Cell(0,4,"",0,1);	
//
$PDF_NAME="RepVentas.pdf";
$pdf->Output($PDF_NAME);
return $PDF_NAME;
}
?>


<?php
if(strlen($StrDES_2)>0){
echo "<div id=\"layer51\" 
	  style=\"position:absolute; left: 128px; top: 102px; 
	  width: 88px; background-color: #FFCC00; layer-background-color: #FFCC00; 
	  border: 1px none #000000; height: 17px;\">";
echo "<div align=\"center\" class=\"Estilo2\"> ";
echo " <a href=\"$StrDES_2\" download>Desc.PDF</a> ";
echo "</div>";
echo "</div>";
}
?>

<?php
if(strlen($StrDESCARGAR)>0){
echo "<div id=\"layer51\" 
	  style=\"position:absolute; left: 250px; top: 102px; 
	  width: 100px; background-color: #FFCC00; layer-background-color: #FFCC00; 
	  border: 1px none #000000; height: 17px;\">";
echo "<div align=\"center\" class=\"Estilo2\"> ";
echo " <a href=\"$StrDESCARGAR\" download>Descargar PDF</a> ";
echo "</div>";
echo "</div>";
}
?>


</div>
</div>
</body>
</html>
