<?
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"];


if ($bodega==0) $bodegas='Todas';
else
	{
	$sql_bv = "SELECT descripcion 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 nombres FROM vendedor WHERE id = $vende";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$vendedor = mysql_result($resbv, 0, "nombres");
			}
	}

?>
<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">DESPACHO FACTURAS VENDEDOR <?=$vendedor?> BODEGA <?=$bodegas?> </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="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="10%" scope="col"><div align="center">Unidades</div></th>
        <th width="9%" scope="col"><div align="center">Kilos</div></th>
        <th width="10%" scope="col"><div align="center">Total</div></th>
		<th width="6%" scope="col"><div align="center">Seleccion</div></th>
       
        <td width="10" align="center" bordercolor="#FFFFFF" bgcolor="#FFFFFF">&nbsp;</td>
      </tr>
		<?			
		$total_unidades=0;
		$total_kilos=0;
		$total_total=0;
	
		
		
		if ($vende==0 &&  $bodega==0)  
			{$sqlfs = "SELECT salida.Id, id_sucursal, fecha_emision, hora, rut_cliente, num_dcto, razon_social, sucursal, total, tipo_dcto  FROM salida, sucursales WHERE  id_sucursal=sucursales.id AND (tipo_dcto=2 OR tipo_dcto=33) AND salida.empresa = $id_empresa AND rut_cliente <>'00.000.000-0' AND despacho=0 ORDER BY num_dcto";
			}
		else
		if ($vende!=0  && $bodega==0) 

			{$sqlfs = "SELECT salida.Id, id_sucursal, fecha_emision, hora, rut_cliente, num_dcto, razon_social, sucursal, total, tipo_dcto  FROM salida, sucursales WHERE  id_sucursal=sucursales.id AND (tipo_dcto=2 OR tipo_dcto=33) AND salida.empresa = $id_empresa  AND vendedor = $vende  AND rut_cliente <>'00.000.000-0' AND despacho=0 ORDER BY num_dcto";
			}
		else
		
		if ($vende==0 && $bodega!=0) 

			{$sqlfs = "SELECT salida.Id, id_sucursal, fecha_emision, hora, rut_cliente, num_dcto, razon_social, sucursal, total, tipo_dcto  FROM salida, sucursales WHERE  id_sucursal=sucursales.id AND (tipo_dcto=2 OR tipo_dcto=33) AND salida.empresa = $id_empresa  AND bodega = $bodega  AND rut_cliente <>'00.000.000-0' AND despacho=0 ORDER BY num_dcto";
			}	
		
   else
		
		if ($vende!=0 && $bodega!=0) 

			{$sqlfs = "SELECT salida.Id, id_sucursal, fecha_emision, hora, rut_cliente, num_dcto, razon_social, sucursal, total, tipo_dcto  FROM salida, sucursales WHERE  id_sucursal=sucursales.id AND (tipo_dcto=2 OR tipo_dcto=33) AND  salida.empresa = $id_empresa  AND vendedor = $vende AND bodega = $bodega AND despacho=0 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");
								$iddcto = mysql_result($resfs, $cont, "tipo_dcto");
				if ($iddcto == 2) $dcto='F'; else	$dcto='FE';
			
				$rut = mysql_result($resfs, $cont, "rut_cliente");
				$nombre = mysql_result($resfs, $cont, "razon_social");
				$sucursal = mysql_result($resfs, $cont, "sucursal");
				
					$unidades=0;
					$kilos=0;
					
				if ($id_sucursal!='')
				{
				$sqlc = "SELECT sum(cantidad*peso) as kilos, sum(cantidad) as unidades FROM movimiento, productos WHERE cod_producto=codigo AND id_soe=$id_nota[$cont]";
					$resc = mysql_query($sqlc,$enlace);
					
					$kilos = mysql_result($resc, 0, "kilos");
					$unidades = mysql_result($resc, 0, "unidades");
				}
				
				$nro = mysql_result($resfs, $cont, "num_dcto");
				
				$total_unidades+=$unidades;
				
				$unidades=number_format($unidades,0,',','.');
			
				$total_kilos+=$kilos;
				
				$kilos=number_format($kilos,0,',','.');
				$total = mysql_result($resfs, $cont, "total");
				$total_total+=$total;
				
				$total=number_format($total,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><?=$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>
				  <?=$unidades?>
			    </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>
				  <?=$kilos?>
			    </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>
<td><input name="bodega" type="text" id="bodega" value="<?=$bodega;?>" readonly="1" style="visibility:hidden"  size="1"></td>
<td><input name="vendedor" type="text" id="vendedor" value="<?=$vende;?>" readonly="1" style="visibility:hidden"  size="1"></td>
	        </tr>	
			<?
			$cont+=1;
		
			}
			$total_unidades=number_format($total_unidades,0,',','.');
			$total_kilos=number_format($total_kilos,0,',','.');
			$total_total=number_format($total_total,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>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_unidades?>
			    </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_kilos;?>
			    </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="despacho" id="despacho" value="Generar Despacho" onClick="fun_factura_lote();">
     <input type="button" name="salir" id="salir" value="Salir" onClick="cerrar();">
       <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">
    </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";
	document.getElementById('despacho').style.display = "none";
	window.print();
	document.getElementById('imprimir').style.display = "block";
	document.getElementById('despacho').style.display = "block";
	};
function fun_fact(cual,dct)
	{
	var donde;
	donde = "factura.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 = "imprime_despacho.php";
	document.form1.action=donde;
	document.form1.target="_blank";
	document.form1.submit();
	}	
</script>