<?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']);
//-------------------------
$StrDex=V($_POST['TxtDex']);		
$StrHax=V($_POST['TxtHax']);
$IdUsu=V($_SESSION['SYS_USER_ID']);
//			
if(V($_SESSION['SYS_USER_ALM'])=="%"){
	if(strlen($StrAlm)==0)$StrAlm=T($StrAlm);//$StrAlm=GetSO("alm_nombre","almacenes","","alm_nombre");
}else{
	$StrAlm=V($_SESSION['SYS_USER_ALM']);
}
//
if(strlen($StrDex)==0){
	$StrDex_="2000-01-01";
}else{
	$StrDex_=ToDate($StrDex);
}
if(strlen($StrHax)==0){
	$StrHax_="2020-12-01";
}else{
	$StrHax_=ToDate($StrHax);
}	
//-------------------------
$LStrTR=0;
$LStrPag=V($_POST['TxtPag']);	
if($StrCmd=="F"){$LStrPag="1";}
$VP_RegXPag=100;	
$LStrPag=PagGet($LStrPag,$StrCmd,$LStrTR,$VP_RegXPag);						
//-------------------------
if($StrCmd=="PDF"){	
	//
	$TI="UTILIDADES DE VENTA :: ALMACEN : $StrAlm";
	$s="SELECT COM,CONCAT(SER,'-',NUM),DATE_FORMAT(FEC,'%d-%m-%y'),DES,
		CA,FORMAT(PRC,1),FORMAT(PRV,1),FORMAT(UTI,1) 
		FROM vventas_uti WHERE ID>0 AND ALM LIKE '$StrAlm' AND 
		COM LIKE '$StrCom_' AND NUM LIKE '$StrNum_' AND 
		SER LIKE '$StrSer_' AND FEC >= '$StrDex_' AND 
		FEC <= '$StrHax_' ORDER BY ALM,COM,ISER,INUM,FEC,DES ";
	$T="COMPROB.,NUMERO,FECHA,DESCRIPCION,CANT,PRE.COM,PRE.VEN,UTIL";
	$E="12, 9, 8,22,4,6,6,9";
	$A=" 0, 0, 0, 0,1,1,1,2";		
	//
	$_ID=GenId("id","reportes");
	PDF_TIT($_ID,$TI);
	PDF_SQL($_ID,$s);
	PDF_CAM($_ID,$T);
	PDF_ESP($_ID,$E);
	PDF_ANC($_ID,$A);		
	$StrFilePDF=PDF_CREATE($_ID);	
	//
	JsDir($StrFilePDF);
	return;			
}
//
$AT=gs("at","usuarios","id_usuario=$IdUsu");
?>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Resumen de utilidad de ventas</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">
.tableFixHead          { overflow-y: auto; }
.tableFixHead thead th { position: sticky; top: 0; }

table  {border-collapse:collapse;width:100%;}
th, td {padding: 3px 3px;}
th     {background:#666;}

.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(){	
	var CboAlm=document.getElementById("CboAlm").value;
	if(CboAlm=="TODOS"){
		alert("Seleccione un Almacen");
		document.getElementById("CboAlm").focus();
		return;
	}
	document.getElementById("TxtAc").value="GEN_PDF";
	document.getElementById("Frm").submit();
}

$(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="res_ventas_uti.php" 
style="margin:0px; padding:0px; display:inline;" onSubmit="return false;" autocomplete="off">

<!----------------------------------------->
<div id="DivT" class="dT"  style="width:1250px; ">
<img src="b.png" class="iT" />
<div class="tT">RESUMEN DE UTILIDAD DE VENTAS</div>
</div>

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

<div class="s1" style="left: 142px; top: 121px; width: 100px;">
HASTA</div>
<input name="TxtHax" type="text" id="TxtHax" 
class="fe" style="left: 143px; top: 138px; width:100px" 
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();" style="display:none"
tabindex="<?php echo ++$TI;?>" />
</div>
    
<!----------------------------------------->

<div class="tableFixHead" style="position: absolute; left: 35px; top: 172px; width: 800px; height: 376px; overflow:auto; border:solid 1px #363636">
  <table id="Exportar_a_Excel" width="100%" style="font-family:Arial, Helvetica, sans-serif; font-size:11px;">
<thead>
<tr>
<th width="33" style="text-align:left; background:#999">#</th>
<th width="112" style="text-align:left; background:#999">FECHA</th>
<th width="84" style="text-align:center; background:#999"">N°VENTAS</th>
<th width="95"  style="text-align:right; background:#999"">TOTAL VENTA</th>
<th width="200"  style="text-align:right; background:#999""> TOTAL A PRECIO DE COMPRA</th>
<th width="125" style="text-align:right; background:#999"">UTILIDAD</th>
<th width="117" style="text-align:left; background:#999"">&nbsp;</th>
</tr>
</thead>
<tbody>
<?php
	$TOT_DIA=0;
    $TOT_NRO=0;
	$TOT_PVE=0;
	$TOT_PCO=0;
	$TOT_UTI=0;
    $i=0;            
    $s="SELECT fec FROM ventas WHERE fec>='$StrDex_' AND fec<='$StrHax_' GROUP BY fec ORDER BY fec ";
    $rs = grs($s);				
    while ($r = $rs->fetch_assoc()){
		$i++;
		$CSS="";
		if($i%2==0){
			$CSS=" style=\"background-color:#E2E2E2\" ";
		}
		$FEC=$r['fec'];
		$NRO=gn("count(*)","ventas","fec='$FEC' AND anu='N'");
		$PVE=round(gn("SUM(CAN * PVE)","vis_ventas_det_res_uti","FEC='$FEC' AND ANU='N'"),2);
		$PCO=round(gn("SUM(CAN * PCO)","vis_ventas_det_res_uti","FEC='$FEC' AND ANU='N'"),2);
		$UTI=round($PVE - $PCO,2);
		$DAT=explode("-",$FEC);
		$FEC=$DAT[2]."-".$DAT[1]."-".$DAT[0];
		//
		$TOT_DIA++;
		$TOT_NRO=$TOT_NRO+ $NRO;
		$TOT_PVE=$TOT_PVE+ $PVE;
		$TOT_PCO=$TOT_PCO+ $PCO;
		$TOT_UTI=$TOT_UTI+ $UTI;
		//
	?>	
		<tr <?php echo $CSS;?>>
		<!----------------------------------------------->
        <td><?php echo $TOT_DIA;?></td> 
        <td><?php echo $FEC;?></td> 
		<td align="center"><?php echo $NRO;?></td>
		<td align="right"><?php echo NF2($PVE,2);?></td>
		<td align="right"><?php echo NF2($PCO,2);?></td> 
        <td align="right"><?php echo NF2($UTI,2);?></td> 
        <td>&nbsp;</th>
		<!----------------------------------------------->
		</tr>
	<?php
		
    }

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

<div class="s1" style="left: 38px; top:556px; width: 109px;">
<b>N° VENTAS : <?php echo  $TOT_NRO;?></b></div>

<div class="s1" style="left:184px; top:556px; width: 174px;">
<b>TOT.VENTA : S/ <?php echo NF2($TOT_PVE,2);?></b></div>

<div class="s1" style="left:390px; top:556px; width: 233px;">
<b>TOT. A PRE.COMPRA : S/ <?php echo NF2($TOT_PCO,2);?></b></div>

<div class="s1" style="left:617px; top:556px; width: 216px; color:#006">
<b>TOT. UTILIDAD : S/ <?php echo NF2($TOT_UTI,2);?></b></div>


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

</form>

<form action="ficheroExcel.php" 
    method="post" target="_blank" id="FormularioExportacion" >
    
    <div style="position: absolute; left: 221px; 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($w,$StrAlm,$StrCom,$StrSer,$StrNum,$StrDex,$StrHax);
	$StrDESCARGAR=$_PDF;
}

function GEN_PDF($w,$StrAlm,$StrCom,$StrSer,$StrNum,$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,"REPORTE UTILIDAD DE VENTAS",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($StrCom)>0){
$_ENC=vbl("COMPROBANTE".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrCom.vbfill(" ",30),30);
$pdf->Cell(0,4,$_ENC,0,1);	
}

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

if(strlen($StrNum)>0){
$_ENC=vbl("NUMERO".vbfill(" ",15),15)." ".vbl(":".vbfill(" ",1),1);
$_ENC=$_ENC." ".vbl($StrNum.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,vbfill("-",$_CxL),0,1);
$_ENC=vbl("ALMACEN".vbfill(" ",12),12);
$_ENC=$_ENC." ".vbl("COMPR.".vbfill(" ",8),8);
$_ENC=$_ENC." ".vbl("NUMERO".vbfill(" ",8),8);
$_ENC=$_ENC." ".vbl("FECHA".vbfill(" ",8),8);
$_ENC=$_ENC." ".vbl("CLIENTE".vbfill(" ",20),20);
$_ENC=$_ENC." ".vbr(vbfill(" ",4)."CANT",4);
$_ENC=$_ENC." ".vbr(vbfill(" ",7)."PRE.COM",7);
$_ENC=$_ENC." ".vbr(vbfill(" ",7)."PRE.VEN",7);
$_ENC=$_ENC." ".vbr(vbfill(" ",8)."UTILIDAD",8);

$pdf->Cell(0,4,$_ENC,0,1);	
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	
//
$s="SELECT ID,ALM,COM,CONCAT(SER,'-',NUM),DATE_FORMAT(FEC,'%d-%m-%y'),DES,
	CA,FORMAT(PRC,1),FORMAT(PRV,1),FORMAT(UTI,1) 
	FROM vventas_uti  
	WHERE $w			 	 
	ORDER BY ALM,COM,ISER,INUM,FEC,DES ";

$result = grs($s);
while ($row = $result->fetch_row()){
	$_ENC=vbl($row[1].vbfill(" ",12),12);
	$_ENC=$_ENC." ".vbl($row[2].vbfill(" ",8),8);
	$_ENC=$_ENC." ".vbl($row[3].vbfill(" ",8),8);
	$_ENC=$_ENC." ".vbl($row[4].vbfill(" ",8),8);
	$_ENC=$_ENC." ".vbl($row[5].vbfill(" ",20),20);
	$_ENC=$_ENC." ".vbr(vbfill(" ",4).NF2($row[6],1),4);
	$_ENC=$_ENC." ".vbr(vbfill(" ",7).NF2($row[7],1),7);
	$_ENC=$_ENC." ".vbr(vbfill(" ",7).NF2($row[8],1),7);
	$_ENC=$_ENC." ".vbr(vbfill(" ",8).NF2($row[9],1),8);
	$pdf->Cell(0,4,$_ENC,0,1);	
	$TOT=$TOT+$row[9];
}
$pdf->Cell(0,4,vbfill("-",$_CxL),0,1);	

$_TI=vbr(vbfill(" ",75)."TOTAL :",75);
$_TD=vbr(vbfill(" ",14).NF2($TOT,1),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="Utilidad de Ventas.pdf";
$pdf->Output($PDF_NAME);		
return $PDF_NAME;
}
?>

<?php
if(strlen($StrDESCARGAR)>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=\"$StrDESCARGAR\" download>Desc.PDF</a> ";
echo "</div>";
echo "</div>";
}
?>


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