<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Resumen Ventas</title>
<?
include("../conex.php");
$id_bodega=$_POST["lstbodega"];
if ($id_bodega==0) $bodegas='Todos';
else
	{
	$sql_bv = "SELECT * FROM bodega WHERE id = $id_bodega";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$bodegas = mysql_result($resbv, 0, "descripcion");
			}
	}
$txtfecha=$_POST["txtfecha"];
$dia = substr ($txtfecha, 0, 2);
$mes = substr ($txtfecha, 3, 2);
$mes2=$mes;
$aņo = substr ($txtfecha, 6, 4);
$ano2=$aņo;
if ($mes=='01') $mes = "ENERO";
if ($mes=='02') $mes = "FEBRERO";
if ($mes=='03') $mes = "MARZO";
if ($mes=='04') $mes = "ABRIL";
if ($mes=='05') $mes = "MAYO";
if ($mes=='06') $mes = "JUNIO";
if ($mes=='07') $mes = "JULIO";
if ($mes=='08') $mes = "AGOSTO";
if ($mes=='09') $mes = "SEPTIEMBRE";
if ($mes=='10') $mes = "OCTUBRE";
if ($mes=='11') $mes = "NOVIEMBRE";
if ($mes=='12') $mes = "DICIEMBRE";

	?>
    
<style type="text/css">
<!--
.Estilo1 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
.Estilo2 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color:#FF0000}
-->
</style>
</head>
<body LINK="#000000" VLINK="#000000" ALINK="#000000">  <br>
<div align="center" class="titulos2">RESUMEN DE COMPRA Y VENTAS MES DE  
<?=$mes?> DEL <?=$aņo?> Bodega <?=$bodegas?></div>
<div align="center">
          
            
  <table width="880" border="0">
  <tr bgcolor="#CCCCCC">
    <th width="36" scope="col"><div align="center">Cant.</div></th>
    <th width="218" scope="col"><div align="center">Dcto.</div></th>
    <th width="106" scope="col"><div align="center">Compra_Exento</div></th>
    <th width="106" scope="col"><div align="center">Compra_Neto</div></th>
    <th width="90" scope="col"><div align="center">Compra_IVA </div></th>
    <th width="114" scope="col"><div align="center">Compra_IVA_Retenido</div></th>
     <th width="103" scope="col"><div align="center">TOTAL_COMPRA</div></th>
    <th width="106" scope="col"><div align="center">Venta_Exento</div></th>
    <th width="95" scope="col"><div align="center">Venta_Neto</div></th>
    <th width="84" scope="col"><div align="center">Venta_IVA</div></th>
    <th width="103" scope="col"><div align="center">Venta_IVA_Retenido</div></th>
      <th width="103" scope="col"><div align="center">TOTAL_VENTA</div></th>
  </tr>		


<?


		
		$doctos="(tipo_dcto.id=33 OR tipo_dcto.id=34 OR tipo_dcto.id=61 OR tipo_dcto.id=56 OR tipo_dcto.id=39)";
if ($id_bodega==0)
{
$sqlfs = "SELECT count(*) as cant, tipo_dcto.abr, sum(factor*exento) as exento, sum(factor*neto) as neto, sum(factor*iva) as iva, sum(factor*total) as total, sum(factor*iva_retenido) as retenido FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND ".$doctos." AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa GROUP by tipo_dcto  ORDER by tipo_dcto";
}
else
{
$sqlfs = "SELECT count(*) as cant, tipo_dcto.abr, tipo_dcto.abr, sum(factor*exento) as exento, sum(factor*neto) as neto, sum(factor*iva) as iva, sum(factor*total) as total, sum(factor*iva_retenido) as retenido FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND extract(year from fecha_emision)='$ano2' AND ".$doctos." AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND bodega=$id_bodega GROUP by tipo_dcto ORDER by tipo_dcto";
}	


	
		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
			while (mysql_fetch_object ($resfs))
				{
				$cant = mysql_result($resfs, $cont, "cant");
				$dcto = mysql_result($resfs, $cont, "tipo_dcto.abr");

				$exento= mysql_result($resfs, $cont, "exento");
				$neto= mysql_result($resfs, $cont, "neto");
				$iva= mysql_result($resfs, $cont, "iva");
				$total= mysql_result($resfs, $cont, "total");
				$retenido= mysql_result($resfs, $cont, "retenido");
				
			$total_v_exento+=$exento;
			$total_v_neto+=$neto;
			$total_v_iva+=$iva;
			$total_v_total+=$total;
			$total_v_retenido+=$retenido;
			$cant=number_format($cant,0,',','.');
			$exento=number_format($exento,0,',','.');
			$neto=number_format($neto,0,',','.');
			$iva=number_format($iva,0,',','.');
			$total=number_format($total,0,',','.');
			$retenido=number_format($retenido,0,',','.');
	?>	
	<tr>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"> <?=$cant;?> </div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><?=$dcto;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>

      <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
       <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$exento;?></div></td>
    
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$neto;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$iva;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$retenido;?></div></td>
     <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$total;?></div></td>
  </tr>	
		
        <?
        $cont+=1;
		}
        
		
		
		
		$doctos="(tipo_dcto.id=30 OR tipo_dcto.id=32 OR tipo_dcto.id=33 OR tipo_dcto.id=34 OR tipo_dcto.id=55 OR tipo_dcto.id=56 OR  tipo_dcto.id=60 OR tipo_dcto.id=61 OR tipo_dcto.id=45 OR tipo_dcto.id=46)";
		
if ($id_bodega==0)
{	$sqlfs = "SELECT count(*) as cant, tipo_dcto.abr, sum(factor*exento) as exento, sum(factor*neto) as neto, sum(factor*iva) as iva, sum(factor*total) as total, sum(factor*iva_retenido) as retenido FROM entrada, tipo_dcto WHERE extract(month from fecha_recepcion)='$mes2' AND extract(year from fecha_recepcion)='$ano2' AND ".$doctos." AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa GROUP by tipo_dcto  ORDER by tipo_dcto";
}
else
{$sqlfs = "SELECT count(*) as cant, tipo_dcto, tipo_dcto.abr, sum(factor*exento) as exento, sum(factor*neto) as neto, sum(factor*iva) as iva, sum(factor*total) as total, sum(factor*iva_retenido) as retenido FROM entrada, tipo_dcto WHERE extract(month from fecha_recepcion)='$mes2' AND extract(year from fecha_recepcion)='$ano2' AND ".$doctos." AND tipo_dcto.id = entrada.tipo_dcto AND entrada.empresa = $id_empresa AND bodega=$id_bodega GROUP by tipo_dcto ORDER by tipo_dcto";

}
$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
			while (mysql_fetch_object ($resfs))
				{
				$cant = mysql_result($resfs, $cont, "cant");
				$dcto = mysql_result($resfs, $cont, "tipo_dcto.abr");
				$exento= mysql_result($resfs, $cont, "exento");
				$neto= mysql_result($resfs, $cont, "neto");
				$iva= mysql_result($resfs, $cont, "iva");
				$total= mysql_result($resfs, $cont, "total");
				$retenido= mysql_result($resfs, $cont, "retenido");
				
			$total_c_exento+=$exento;
			$total_c_neto+=$neto;
			$total_c_iva+=$iva;
			$total_c_total+=$total;
			$total_c_retenido+=$retenido;
			$cant=number_format($cant,0,',','.');
			$exento=number_format($exento,0,',','.');
			$neto=number_format($neto,0,',','.');
			$iva=number_format($iva,0,',','.');
			$total=number_format($total,0,',','.');
			$retenido=number_format($retenido,0,',','.');
			
			
	?>		
    <tr>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><?=$cant;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><?=$dcto;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$exento;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$neto;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$iva;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$retenido;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><?=$total;?></div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">&nbsp;</div></td>
  </tr>
  
    <?
				
			$cont+=1;
			}

	
		
			$total_v_exento=number_format($total_v_exento,0,',','.');
			$total_v_neto=number_format($total_v_neto,0,',','.');
			$total_v_iva=number_format($total_v_iva,0,',','.');
			$total_v_total=number_format($total_v_total,0,',','.');
			$total_v_retenido=number_format($total_v_retenido,0,',','.');
			$total_c_exento=number_format($total_c_exento,0,',','.');
			$total_c_neto=number_format($total_c_neto,0,',','.');
			$total_c_iva=number_format($total_c_iva,0,',','.');
			$total_c_total=number_format($total_c_total,0,',','.');
			$total_c_retenido=number_format($total_c_retenido,0,',','.');
			
			
		
			
		
	?>	


  <tr>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center">&nbsp;</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left">Totales</div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_c_exento;?>
    </div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_c_neto;?>
    </div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_c_iva;?>
    </div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_c_retenido;?>
    </div></td>
     <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_c_total;?>
    </div></td>
     <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_v_exento;?>
    </div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_v_neto;?>
    </div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_v_iva;?>
    </div></td>
    <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_v_retenido;?>
    </div></td>
     <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right">
      <?=$total_v_total;?>
    </div></td>
  </tr>
</table>

<form name="form1" method="post" action="">

	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
	
  </form>
  </div>
</body>
</html>

<script language="javascript" type="text/javascript">
function cerrar()
{
window.top.open('','_parent','');
window.top.close();
}
function fun_imprimir()
	{
	document.getElementById('imprimir').style.display = "none";
	window.print();
	document.getElementById('imprimir').style.display = "block";
	};

</script>