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

$vende =$_POST["lstvendedor"];
$cliente =$_POST["lstcliente"];
$txtfecha = $_POST["fecha"];

$dia = substr ($txtfecha, 0, 2);
$mes = substr ($txtfecha, 3, 2);
$mes2=$mes;
$año = substr ($txtfecha, 6, 4);
$ano2=$año;

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");
			}
	}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<form id="form1" name="form1" method="post" action="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Listado Guias</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">LISTADO GUIAS DE VENTAS  <?=$fecha?> Vendedor <?=$vendedor?> Cliente <?=$nombres?> </div>
<div align="center">

    <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">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;
		
		$fechas="extract(month from fecha_emision)=".$mes2." AND extract(year from fecha_emision)=".$ano2;
		
		if ($vende==0 && $cliente=='0')  
			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE ".$fechas." AND  tipo_dcto.id=52  AND cod_traslado=1  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND documento_control IS NULL ORDER BY num_dcto";
			}
		else
		if ($vende!=0 && $cliente=='0') 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE ".$fechas." AND  tipo_dcto.id=52   AND cod_traslado=1 AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa   AND vendedor = $vende  AND documento_control IS NULL ORDER BY num_dcto";
			}
		else
		if ($vende==0 && $cliente!='0') 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE ".$fechas."  AND tipo_dcto.id=52 AND cod_traslado=1  AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa   AND rut_cliente = '$cliente'  AND documento_control IS NULL ORDER BY num_dcto";
			}	
		else
		if ($vende!=0 && $cliente!='0') 

			{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE  ".$fechas."  AND tipo_dcto.id=52  AND cod_traslado=1 AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND vendedor = $vende AND rut_cliente = '$cliente' AND documento_control IS NULL ORDER BY num_dcto";
			}	
			
			
			
		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0; $cant=0;
			while (mysql_fetch_object ($resfs))
				{
				$sel[$cont]='x';
				$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==52)$dcto='GE';
			
				$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;";
					
					}
				$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><?=$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('<?=$id_nota[$cont];?>');"> <?=$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>
				  <?=$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;?>]" id="sel[<?=$cont;?>]" type="text" value="<?=$sel[$cont];?>"  style="text-align:center;width:30%" onkey="fun_estado('<?=$cont;?>');" > 
			    </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;
		$cant+=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>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="facturar" id="facturar" value="Marcar Todo" onClick="fun_marcatodo();" >
	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
         
  <input type="button" name="lote" id="lote" value="Factura Guias" onClick="fun_factura_lote();"  <? if ($cliente==0) {  ?> disabled="disabled"  <? } ?> />
    <input name="txt_nv" id="txt_nv" type="text" style="visibility:hidden" size="1">
	  <input name="cant" id="cant" type="text" value="<?=$cant?>" style="visibility:hidden" size="1">
       <input name="txtfecha" id="txtfecha" type="text" value="<?=$txtfecha?>" style="visibility:hidden" size="1">
          <input type="button" name="imprime_pdf" id="imprime_pdf" value="Imprime PDF" onClick="fun_Factura_PDF();"  disabled="disabled"  />  
      <input name="firmado" id="firmado" type="text">
       <input name="token" id="token" type="text" >
    </form>
</div>
</body>
</html>

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

function fun_fact(cual)
	{
	document.form1.txt_nv.value=cual;
	document.form1.action="index2.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
	
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_factura_lote()
	{
	//if (chequear()==true)
	{ 

	document.form1.action= "guarda_factura_lote_guias_xml.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
	}
function fun_Factura_PDF()
	{
	var firma = document.form1.firmado.value;
	document.form1.action = '../LibFE/DOC/PDF/'+firma;
	document.form1.target='_blank';
	document.form1.submit();
		
		document.form1.Salir.disabled=false;
	}
function chequear()
	{

	var jsi,jsf;
	jsf=0;
	for (jsi=1;jsi<=<?=$cant?>;jsi++)
		{
		if (document.getElementById("sel["+jsi+"]").value==1)
			{
			jsf+=1;
			}
		}

	if (jsf==0) return false; else return true;
	}	

function fun_estado(donde)	
	{
	
	var estado;

	
	if (document.getElementById("sel["+donde+"]").value=='') estado='';
	else estado=1;
	
	if (estado=='') 
	 {
    document.getElementById("sel["+donde+"]").value='1';
	
	 }
	else 
	if (estado==1) 
     {
	
   document.getElementById("sel["+donde+"]").value='';
	
	}
   }	
 function fun_marcatodo()	
	{
	var  cant, i; 

	cant=<?=$cant?>;
		
	for (i=0;i<cant;i++)
	{
    document.getElementById("sel["+i+"]").value='1';
	} 
   
   }	
				
</script>