<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa']; // ********  23/11/10  **************
$id_user = $_SESSION['ss_user'];
include("../conex.php");
?>
<!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=iso-8859-1"/>
<title>Informe comprobantes de caja</title>
<?
$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"];
$txtfecha1=$_POST["txtfecha1"];

$dia = substr ($txtfecha, 0, 2);
$dia1 = substr ($txtfecha1, 0, 2);
$mes = substr ($txtfecha, 3, 2);
$mes1 = substr ($txtfecha1, 3, 2);
$mes2=$mes;

$aņo = substr ($txtfecha, 6, 4);
$aņo1 = substr ($txtfecha1, 6, 4);

$ini = $aņo.'/'.$mes.'/'.$dia;
$fin = $aņo1.'/'.$mes1.'/'.$dia1;

?>
<style type="text/css">
<!--
.Estilo1 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
.Estilo4 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; }
-->
</style>
</head>
<body>
<div align="center" class="titulos2"> INFORME COMPROBANTES DE CAJA  Bodega <?=$bodegas;?> Desde <?=$dia.'/'.$mes.'/'.$aņo?> Hasta <?=$dia1.'/'.$mes1.'/'.$aņo1?></div>
<table width="23%"border="0"  align="center">
      <tr bgcolor="#CCCCCC">
	  	<th scope="col"><div align="left">Ventas </div></th>
</table>

<table width="55%"border="0"  align="center">
<tr bgcolor="#CCCCCC">
	  	<th scope="col"><div align="center">Fecha</div></th>
		<th scope="col"><div align="center">Documentos</div></th>
        <th scope="col"><div align="center">Valor</div></th>
      </tr>

<?	
			$credito=0;
			$contadov=0;
			$contado=0;
        	$documento=0;
			$tventa=0;
			$t_total=0;
			$cont=0;
			$espacio=' ';

  if ($id_bodega==0)
   {
	 $sqlfs = "SELECT * FROM salida, tipo_dcto, nombres WHERE fecha_emision>='$ini' AND fecha_emision<='$fin' AND  (tipo_dcto.descripcion='FACTURA' OR tipo_dcto.descripcion='NC' OR tipo_dcto.descripcion='ND') AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND rut=rut_cliente ORDER BY num_dcto";
   }
   else
   {
    $sqlfs = "SELECT * FROM salida, tipo_dcto, nombres WHERE fecha_emision>='$ini' AND fecha_emision<='$fin' AND  (tipo_dcto.descripcion='FACTURA' OR tipo_dcto.descripcion='NC' OR tipo_dcto.descripcion='ND') AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND rut=rut_cliente  and bodega=$id_bodega ORDER BY num_dcto";
   }
		  	$resfs = mysql_query($sqlfs,$enlace);			
						
			while (mysql_fetch_object ($resfs))
				{
				$detalle = mysql_result($resfs, $cont, "descripcion");
				$nombre = mysql_result($resfs, $cont, "razon_social");
				$dcto = mysql_result($resfs, $cont, "num_dcto");
				$fecha = mysql_result($resfs, $cont, "fecha_emision");
				$dia = substr ($fecha, 8, 2);
				$ano = substr ($fecha, 2, 2);
				$mes = substr ($fecha, 5, 2);
				$fecha = "$dia/$mes/$ano";
				$venta = mysql_result($resfs, $cont, "total");
				$tipo_pago= mysql_result($resfs, $cont, "tipo_pago");
			if ($tipo_pago==3)  {$credito=$credito+$venta;} 
			    else {$contadov=$contadov+$venta;}
			
				$tventa=$tventa+$venta;        
				$venta=number_format($venta,0,',','.');

			
			$cont+=1;
			}
		$tcontado = $contadov;
		 $tventa=number_format($tventa,0,',','.');
		$contadov=number_format($contadov,0,',','.');
		$credito=number_format($credito,0,',','.');		
?>

<tr>
<td width="14%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong></strong></span></div></td>
				
<td width="53%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong>Totales</strong></span></div></td>

<td width="33%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong> <?=$tventa?> </strong></span></div></td>


</tr>
<tr>
<td width="14%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>Contado</strong></span></div></td>
				
<td width="53%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$contadov?></strong></span></div></td>
</tr>
<tr>
<td width="14%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>Credito</strong></span></div></td>
<td width="53%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong> <?=$credito?> </strong></span></div></td>


</tr>
			
</table>
<table width="23%"border="0"  align="center">
  <tr bgcolor="#CCCCCC">
  	<th scope="col"><div align="left">Pagos </div></th>
</table>		
 <table width="77%"border="0"  align="center">
<tr bgcolor="#CCCCCC">
	  	<th scope="col"><div align="center">Fecha</div></th>
		<th scope="col"><div align="center">Comprobante</div></th>
        <th scope="col"><div align="center">Cliente</div></th>
        <th scope="col"><div align="center">Contado</div></th>
        <th scope="col"><div align="center">Transferencia</div></th>
        <th scope="col"><div align="center">Documentos</div></th>
        <th scope="col"><div align="center">Total</div></th>
      </tr>

<?	
		$t_contado=0;
		$t_tranferencia=0;
        $t_documento=0;
		if ($id_bodega==0)
	{	
		
		$sql_cpc = "SELECT * FROM cabeza_pago_cliente, nombres WHERE  fecha>='$ini' AND fecha<='$fin' AND cabeza_pago_cliente.rut=nombres.rut ORDER BY comprobante";
	}
	else
	{
		
		$sql_cpc =  "SELECT * FROM pago_cliente, nombres, cabeza_pago_cliente, vendedor WHERE cabeza_pago_cliente.fecha>='$ini' AND cabeza_pago_cliente.fecha<='$fin' AND  tipo='A' AND empresa = $id_empresa AND pago_cliente.rut=nombres.rut AND id_cpc=cabeza_pago_cliente.id AND cabeza_pago_cliente.id_vendedor=vendedor.id AND id_bodega=$id_bodega ORDER BY cabeza_pago_cliente.comprobante";
		
		
		}
		
  		$res_cpc = mysql_query($sql_cpc,$enlace);
     	$cont=0;
			while (mysql_fetch_object ($res_cpc))
			{
			$id_numero = mysql_result($res_cpc, $cont, "cabeza_pago_cliente.id");
			$numero = mysql_result($res_cpc, $cont, "comprobante");
			$contado = mysql_result($res_cpc, $cont, "contado");	
			$tranferencia = mysql_result($res_cpc, $cont, "tranferencia");	
			$documento = mysql_result($res_cpc, $cont, "documento");
			$total=$contado+$tranferencia+$documento;
			$fecha = mysql_result($res_cpc,$cont, "fecha");
				$dia = substr ($fecha, 8, 2);
				$ano = substr ($fecha, 0, 4);
				$mes = substr ($fecha, 5, 2);
				$fecha = "$dia/$mes/$ano";
			$nombre = mysql_result($res_cpc, $cont, "razon_social");
			
			$t_contado+=$contado;
			$t_tranferencia+=$tranferencia;
			$t_documento+=$documento;
			$t_total+=$total;
		  $contado=number_format($contado,0,',','.');	
		  $tranferencia=number_format($tranferencia,0,',','.');	
		  $documento=number_format($documento,0,',','.');
		   $total=number_format($total,0,',','.');
?>
<tr>
<td width="6%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong><?=$fecha?></strong></span></div></td>


<td width="10%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong>
   	<a  href="javascript:fun_fact('<?=$id_numero;?>');" > <?=$numero?> </a></strong></span></div></td>
                
<td width="29%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong><?=$nombre?></strong></span></div></td>
<td width="13%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$contado?></strong></span></div></td>
<td width="14%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$tranferencia?></strong></span></div></td>
<td width="12%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$documento?></strong></span></div></td>
<td width="13%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$total?></strong></span></div></td>
</tr>			


<?
			
			$cont+=1;
			}
		
		  $t_contado=number_format($t_contado,0,',','.');	
		  $t_tranferencia=number_format($t_tranferencia,0,',','.');
		  $t_documento=number_format($t_documento,0,',','.');	
		  $t_total=number_format($t_total,0,',','.');	
?>

<tr>
<td width="6%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong></strong></span></div></td>
				
<td width="13%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong>Totales</strong></span></div></td>
<td width="29%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong></strong></span></div></td>

<td width="13%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong> <?=$t_contado?> </strong></span></div></td>

<td width="14%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong> <?=$t_tranferencia?> </strong></span></div></td>
<td width="12%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong> <?=$t_documento?> </strong></span></div></td>
<td width="13%" style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong> <?=$t_total?> </strong></span></div></td>
</tr>
</table>
		
<form name="form1" method="post" action="">
	<table width="150" border="0" align="center">
	<tr>
		<td width="30%" align="rigth">
			<input type="submit" name="salir" value="Salir" onClick="fun_salir();"/>
		</td>
		<td width="70%" align="right">
			<input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
		</td>
	</tr>
</table>
		<input name="idsalidaant" type="text" id="idsalidaant" style="visibility:hidden" size="1">
    </form>


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

<script language="javascript" type="text/javascript">


function fun_salir()
	{
	window.top.open('','_parent','');window.top.close();
	}
	
function fun_fact(cual)
	{
	var donde;
	donde = "comprobante.php";
	document.form1.idsalidaant.value=cual;
	document.form1.action=donde;
	document.form1.target="_blank";
	document.form1.submit();
	}
	
function fun_imprimir()
	{
	document.getElementById('salir').style.display = "none";
	document.getElementById('imprimir').style.display = "none";
	window.print();
	document.getElementById('salir').style.display = "block"
	document.getElementById('imprimir').style.display = "block";
	};
</script>