<?
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>Cuentas Corrientes</title>
<?
include("../conex.php");
$bodega =$_POST["lstbodega"];
$vende =$_POST["lstvendedor"];
$cliente =$_POST["lstcliente"];

if ($bodega==0) $bodegas='Todos';
else
	{
	$sql_bv = "SELECT * FROM bodega WHERE id = $bodega";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$bodegas = mysql_result($resbv, 0, "descripcion");
			}
	}
if ($vende==0) $vendedor='Todos';
else
	{
	$sql_bv = "SELECT * FROM vendedor WHERE id = $vende";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$vendedor = mysql_result($resbv, 0, "nombres");
			}
	}
if ($cliente==0) $nombres='Todos';
else
	{
	$sql_bv = "SELECT * FROM nombres WHERE rut = '$cliente'";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$nombres = mysql_result($resbv, 0, "razon_social");
			}
	}
?>
<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">LISTADO NOTAS DE VENTAS  Vendedor <?=$vendedor?> Cliente <?=$nombres?> </div>
<div align="center">
<form id="form1" name="form1" method="post" action="">
    <table width="89%" border="0">
      <tr bgcolor="#CCCCCC">
	  	<th width="11%" scope="col"><div align="center">Día</div></th>
        <th width="7%" scope="col"><div align="center">Hora</div></th>
        <th width="7%" scope="col"><div align="center">Vendedor</div></th>
        <th width="9%" scope="col"><div align="center">RUT</div></th>
        <th width="19%" scope="col"><div align="center">Cliente</div></th>
        <th width="6%" scope="col"><div align="center">T/D</div></th>
        <th width="8%" scope="col"><div align="center">Nro</div></th>
         <th width="8%" scope="col"><div align="center">OC o Despacho</div></th>
		<th width="10%" scope="col"><div align="center">Neto</div></th>
        <th width="9%" scope="col"><div align="center">Iva</div></th>
        <th width="10%" scope="col"><div align="center">Total</div></th>
		<th width="6%" scope="col"><div align="center">Factura Lote</div></th>
       
        <td width="10" align="center" bordercolor="#FFFFFF" bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
		<?			
		$total_neto=0;
		$total_iva=0;
		$total_total=0;
		$total_ivar=0;
		
		
		if ($vende==0 && $cliente==0 && $bodega==0)  
			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE  tipo_dcto.descripcion='NOTA VENTA'  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND rut_cliente <>'00.000.000-0' AND documento_control IS NULL ORDER BY num_dcto";
			}
		else
		if ($vende!=0 && $cliente==0 && $bodega==0) 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE  tipo_dcto.descripcion='NOTA VENTA'  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND vendedor = $vende  AND rut_cliente <>'00.000.000-0' AND documento_control IS NULL ORDER BY num_dcto";
			}
		else
		if ($vende==0 && $cliente!='' && $bodega==0) 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE  tipo_dcto.descripcion='NOTA VENTA'  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND rut_cliente = '$cliente'  AND rut_cliente <>'00.000.000-0' AND documento_control IS NULL ORDER BY num_dcto";
			}	
	else
		if ($vende!=0 && $cliente!='' && $bodega==0) 

			{$sqlfs = "SELECT * FROM salida WHERE  tipo_dcto=3  AND  salida.empresa = $id_empresa  AND vendedor = $vende AND rut_cliente = '$cliente' AND documento_control IS NULL ORDER BY num_dcto";
			}
	else
		if ($vende==0 && $cliente==0 && $bodega!=0) 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE  tipo_dcto.descripcion='NOTA VENTA'  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND bodega = $bodega  AND rut_cliente <>'00.000.000-0' AND documento_control IS NULL ORDER BY num_dcto";
			}	
		
   else
		if ($vende!=0 && $cliente==0 && $bodega!=0) 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE  tipo_dcto.descripcion='NOTA VENTA'  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND vendedor = $vende AND bodega = $bodega AND rut_cliente <>'00.000.000-0' AND documento_control IS NULL ORDER BY num_dcto";
			}
		else
		if ($vende==0 && $cliente!='' && $bodega!=0) 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE  tipo_dcto.descripcion='NOTA VENTA'  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND rut_cliente = '$cliente' AND bodega = $bodega AND rut_cliente <>'00.000.000-0' AND documento_control IS NULL ORDER BY num_dcto";
			}	
		else
		if ($vende!=0 && $cliente!='' && $bodega!=0) 

			{$sqlfs = "SELECT * FROM salida WHERE  tipo_dcto=3  AND  salida.empresa = $id_empresa  AND vendedor = $vende AND rut_cliente = '$cliente' AND bodega = $bodega AND documento_control IS NULL ORDER BY num_dcto";
			}		
		
		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
			while (mysql_fetch_object ($resfs))
				{
				$sel[$cont]='False';
				$id_nota[$cont] = mysql_result($resfs, $cont, "salida.Id");
				$fecha = mysql_result($resfs, $cont, "fecha_emision");
				$hora = mysql_result($resfs, $cont, "hora");
				$dia = substr ($fecha, 8, 2);
				$mes = substr ($fecha, 5, 2);
				$año = substr ($fecha, 0, 4);
				$id_sucursal = mysql_result($resfs, $cont, "id_sucursal");
				$orden_compra = mysql_result($resfs, $cont, "orden_compra");
				$despacho = mysql_result($resfs, $cont, "despachar");
				$iddcto = mysql_result($resfs, $cont, "tipo_dcto");
				$id_vendedor = mysql_result($resfs, $cont, "vendedor");
				if($iddcto==3)$dcto='NV';
			
				$rut = mysql_result($resfs, $cont, "rut_cliente");
				if ($id_sucursal!=''){
					
					$sqlc = "SELECT * FROM sucursales WHERE id=$id_sucursal";
					$resc = mysql_query($sqlc,$enlace);
					
					$nombre = mysql_result($resc, 0, "razon_social");
					$sucursal = mysql_result($resc, 0, "sucursal");
				}
				else 
					{
					$nombre = "Boleta";
					$sucursal="&nbsp;";
					
					}
				if ($id_vendedor!=''){
					
					$sqlc = "SELECT * FROM vendedor WHERE id=$id_vendedor";
					$resc = mysql_query($sqlc,$enlace);					
					$nombre_vende = mysql_result($resc, 0, "nombres");
									}	
					
				$nro = mysql_result($resfs, $cont, "num_dcto");
				$iva = mysql_result($resfs, $cont, "iva");
				$total_iva+=$iva;
				
				$iva=number_format($iva,0,',','.');
				$neto= mysql_result($resfs, $cont, "neto");
				$total_neto+=$neto;
				
				$neto=number_format($neto,0,',','.');
				$total = mysql_result($resfs, $cont, "total");
				$total_total+=$total;
				
				$total=number_format($total,0,',','.');
				$ivar = mysql_result($resfs, $cont, "iva_retenido");
				$total_ivar+=$ivar;
				
				$ivar=number_format($ivar,0,',','.');

		  	?>
			<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$dia?> /<?=$mes?> /<?=$año?> </strong></span></div></td>
                 <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$hora?> </strong></span></div></td>
                 <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$nombre_vende?> </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$rut?></strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong><?=$nombre?> <?=$sucursal?></strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$dcto?></strong></span></div></td>
				<td 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('<?=$nro;?>','<?=$dcto?>');"> <?=$nro?> </a>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$orden_compra?><?=$despacho?>
			    </strong></span></div></td>
                <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$neto?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$iva?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong>
				<input name="sel[<?=$cont;?>]" type="checkbox" id="sel[<?=$cont;?>]" value="True" <? if ($sel[$cont]=='True') { ?> checked="checked" <? }?>>
			    </strong></span></div></td>
<td><input name="id_nota[<?=$cont;?>]" type="text" id="id_nota[<?=$cont;?>]" value="<?=$id_nota[$cont];?>" readonly="1" style="visibility:hidden"  size="1"></td>
	        </tr>	
			<?
			$cont+=1;
		
			}
			$total_neto=number_format($total_neto,0,',','.');
			$total_iva=number_format($total_iva,0,',','.');
			$total_total=number_format($total_total,0,',','.');
			$total_ivar=number_format($total_ivar,0,',','.');
			?>
			<tr>
		        <td 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 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 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 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 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 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 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 style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>Total:</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total_neto?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total_iva;?>
			    </strong></span></div></td> 
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				  <?=$total_total;?>
			    </strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
				 
			    </strong></span></div></td>
	        </tr>
    </table>


	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
      <input type="button" name="lote" id="lote" value="Factura Lote" onClick="fun_factura_lote();">
       <input name="txt_nv" id="txt_nv" type="text" style="visibility:hidden" size="1">
	  <input name="cant" id="cant" type="text" value="<?=$cont?>" style="visibility:hidden" size="1">
      <input name="id_bodega" id="id_bodega" type="text" value="<?=$bodega?>" style="visibility:hidden" size="1">
    
    </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";
	};
function fun_fact(cual,dct)
	{
	var donde;
	donde = "index2.php";
	document.form1.txt_nv.value=cual;
	document.form1.action=donde;
	document.form1.target="_blank";
	document.form1.submit();
	}
function fun_factura_lote()
	{
	var donde;
	donde = "factura_lote_sii.php";
	document.form1.action=donde;
	document.form1.target="_blank";
	document.form1.submit();
	}	
</script>