<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = 'index.php';
		</script>
		<? 
		exit();
		}


$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];

include("../conex.php");
 $hora = date('H:i:s');
	$unixtime = time();
		$settime = $unixtime;
		$diaa = date("d",$settime);
		$mes = date("m",$settime);
		$anoa = date("Y",$settime); 
		$fecha= "$diaa/$mes/$anoa";    //  hoy
		
$bodega =$_POST["bodega"];


if ($bodega==0) $bodegas='Todas';
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");
			}
	}
	
?>

<!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>Imprime Despacho Facturas</title>
<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 Bodega <?=$bodegas?> <?=$fecha?> </div>
<div align="center">
<form id="form1" name="form1" method="post" action="">
    <table width="100%" border="0">
      <tr bgcolor="#CCCCCC">
	  	<th width="12%" scope="col"><div align="center">FACTURAS</div></th>
        <th width="14%" scope="col"><div align="center">FECHA</div></th>
        <th width="25%" scope="col"><div align="center">VENDEDOR</div></th>
        <th width="29%" scope="col"><div align="center">CLIENTES</div></th>
		<th width="20%" scope="col"><div align="center">RECIBE</div></th>
      </tr>
<?
// ********* Limpia paso_despacho  en bodega  **********	
$sql_paso="DELETE FROM paso_despacho WHERE id_bodega=$bodega";
	mysql_query($sql_paso,$enlace); 		

//************************************

$notas=0;
$cuenta_fact = 0;
$numero_notas=$_POST["cant"];  // ********* cantidad total notas 

while ($notas<$numero_notas)     // ciclo por todas 
  {
 
if ($sel[$notas]=='True')

  {
  
//********************* Sacar Datos Nota Venta ******************************************/		
$sql_nv = "SELECT * FROM salida WHERE id = $id_nota[$notas] ORDER BY num_dcto";
$res_nv= mysql_query($sql_nv,$enlace);
$nv_cont = 0;
$cuenta_fact+=1;
while ( mysql_fetch_object($res_nv))
	{
	$id_nota_venta=mysql_result($res_nv, $nv_cont, "id");
	$sqlnv_id_sucursal=mysql_result($res_nv, $nv_cont, "id_sucursal");
	$fecha=mysql_result($res_nv, $nv_cont, "fecha_emision");
                $dia = substr ($fecha, 8, 2);
				$mes = substr ($fecha, 5, 2);
				$aņo = substr ($fecha, 0, 4);
	$sqlnv_rut=mysql_result($res_nv, $nv_cont, "rut_cliente");
	$sqlnv_nv=mysql_result($res_nv, $nv_cont, "num_dcto");
	$id_vendedor=mysql_result($res_nv, $nv_cont, "vendedor");
	
	
      $res_ven = mysql_query("SELECT * FROM vendedor WHERE id=$id_vendedor",$enlace);
	if(mysql_fetch_object($res_ven)) $vendedor = mysql_result($res_ven, 0, "nombres");

	$res_cli = mysql_query("SELECT * FROM sucursales, nombres WHERE nombres.rut=sucursales.rut AND id=$sqlnv_id_sucursal",$enlace);
	if(mysql_fetch_object($res_cli)) 
		{
		$id_sucursal = mysql_result($res_cli, 0, "sucursales.id");
		$sucursal = mysql_result($res_cli, 0, "sucursales.sucursal");
		$txtrut = mysql_result($res_cli, 0, "sucursales.rut");
		$nombre = mysql_result($res_cli, 0, "sucursales.razon_social");
		}
	

	
//********  productos **********
$sql_pro = "SELECT * FROM salida, movimiento WHERE salida.id = $id_nota[$notas] AND id_soe=salida.id";
$res_pro= mysql_query($sql_pro,$enlace);
$p_cont = 0;
while ( mysql_fetch_object($res_pro))
	{
	$codigo=mysql_result($res_pro, $p_cont, "cod_producto");
	$cantidad=mysql_result($res_pro, $p_cont, "cantidad");
    $p_cont+=1;
		$sql_paso="INSERT INTO paso_despacho (codigo, cantidad, id_bodega) VALUES ($codigo,$cantidad, $bodega)";
		mysql_query($sql_paso,$enlace);
	}
	$sql_pro = "UPDATE salida SET despacho=1 WHERE id = $id_nota[$notas]";
     $res_pro= mysql_query($sql_pro,$enlace);	
?> 
	<tr>
	<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$sqlnv_nv?></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><?=$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><?=$vendedor?> </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="left"><span class="Estilo1"><strong>&nbsp; </strong></span></div></td>

	        </tr>	
  

<?

		
 //&&&&&&&&&&&&&& fin ciclo por todas
     }
	 } 
   $notas+= 1;
     }
	?> 
    <tr>
	<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$cuenta_fact?> Facturas</strong></span></div></td>
    
      </tr>
       
</table>

<div align="center" class="titulos2"> &nbsp; </div>


	<div align="center" class="titulos2">DETALLE PRODUCTOS</div>
    
    <table width="100%" border="0">
      <tr bgcolor="#CCCCCC">
	  	<th width="12%" scope="col"><div align="center">CODIGO</div></th>
        <th width="14%" scope="col"><div align="center">SAP</div></th>
        <th width="39%" scope="col"><div align="center">DESCRIPCION</div></th>
        <th width="18%" scope="col"><div align="center">KILOS</div></th>
		<th width="17%" scope="col"><div align="center">UNIDADES</div></th>
      </tr>
    
     
	 <?
	//&&&&&&&&&&&&&& detalle
	$t_kilos=0;
	$t_unidades=0;
	$sql_pro = "SELECT productos.codigo, sum(cantidad) as cant, sum(cantidad*peso) as kilos, descripcion, codigo_equivalente_proveedor FROM paso_despacho, productos WHERE paso_despacho.codigo=productos.codigo and id_bodega=$bodega GROUP BY productos.codigo ORDER BY descripcion ASC";
$res_pro= mysql_query($sql_pro,$enlace);
$p_cont = 0;
while ( mysql_fetch_object($res_pro))
	{
	$codigo=mysql_result($res_pro, $p_cont, "codigo");
	$sap=mysql_result($res_pro, $p_cont, "codigo_equivalente_proveedor");
	$nombre=mysql_result($res_pro, $p_cont, "descripcion");
	$kilos=mysql_result($res_pro, $p_cont, "kilos");
	$cant=mysql_result($res_pro, $p_cont, "cant");
	$t_kilos+=$kilos;
	$t_unidades+=$cant;
	
	?>
	<tr>
	<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$codigo?></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><?=$sap?>&nbsp; </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?> </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><?=$kilos?></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><?=$cant?></strong></span></div></td>

	        </tr>	
    <?
    $p_cont+=1;
		
	} 
	$sql_paso="DELETE FROM paso_despacho WHERE id_bodega=$bodega";
		mysql_query($sql_paso,$enlace); 
	?>
    <tr>
	<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong>&nbsp;</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><?=$p_cont?> Productos</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>Totales </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><?=$t_kilos?></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><?=$t_unidades?></strong></span></div></td>

	        </tr>
    </table>
    
	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
      <input type="button" name="salir" id="salir" value="Salir" onClick="cerrar();">
      <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong> &nbsp; </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> &nbsp; </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> &nbsp; </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> &nbsp; </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>Chofer .............................................................</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> &nbsp; </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> &nbsp; </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> &nbsp; </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> &nbsp; </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>Administracion........................................................</strong></span></div></td>
    
  </form>
  </div>
</body>
</html>

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