<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = 'index.php';
		</script>
		<? 
		exit();
		}
?>
<?
include("../conex.php");
include("../css/mascara_fecha.js");
?>
<!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>NOTA DE VENTA</title>
</head>
<script type="text/javascript" src="../css/validarut.js"></script>
<?
$cant = "1";
$flag=0;
$id_empresa = $_SESSION['empresa'];
/********************************** Datos Empresa *********************************************************************/
$sql_empresa = "SELECT * FROM empresa, comunas WHERE comunas.cod_comuna = empresa.comuna AND id = $id_empresa";
	$res_empresa = mysql_query($sql_empresa,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_empresa))
		{
		$empresa_rut=mysql_result($res_empresa, $row, "rut_empresa");
		$empresa_nombre=mysql_result($res_empresa, $row, "nombre");
		$empresa_direccion=mysql_result($res_empresa, $row, "direccion");
		$empresa_ciudad=mysql_result($res_empresa, $row, "ciudad");
		$empresa_comuna=mysql_result($res_empresa, $row, "comunas.comuna");
		$empresa_actividad=mysql_result($res_empresa, $row, "actividad");
		$empresa_rut_representante=mysql_result($res_empresa, $row, "rut_representante");
		$empresa_nombre_representante=mysql_result($res_empresa, $row, "nombre_representante");
		$row=$row+1;
		}


	
	
	/********************************** Saca el tipo de documento *********************************************************************/
	$sql_dcto = "SELECT id, correlativo FROM tipo_dcto WHERE descripcion = 'NOTA VENTA'";
	$res_dcto = mysql_query($sql_dcto,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_dcto))
		{
		$id_tipo_dcto=mysql_result($res_dcto, $row, "id");
		$sql_num_bol = mysql_result($res_dcto,0 ,"correlativo");
		$sql_num_bol = $sql_num_bol + 1;
		$row=$row+1;
		}
		
	/********************************** Descuentos *********************************************************************/
	$cal_descuento=($txtsubtotal-$txttotal);

	/************************************************** sacar valor de iva ***********************************************/	
	$sql_par = "SELECT valor FROM parametros WHERE descripcion = 'IVA'";
	$res_par = mysql_query($sql_par,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_par))
		{
		$par_iva=mysql_result($res_par, $row, "valor");
		$row=$row+1;
		}
	/************************************************** calculo neto e iva ***********************************************/	
	
	$sql_total = ($txttotal*$par_iva);
	$sql_iva=(int)$sql_total;
	$r=($sql_total-intval($sql_total))*10;
	$r=(int)$r;
	if ($r>=5)
		{
		$sql_iva+=1;
		$sql_neto = $txttotal-$sql_iva; 
		}
	else
		{
		$sql_neto = $txttotal-$sql_iva;
		}
		$sql_total = $sql_neto + $sql_iva;
?>
<body onLoad="fun_descuento();">

<form id="form1" name="form1" method="post" action="">
<table width="640" align="center" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="60px"><div align="center"><img src="Images/logo.gif" width="60px" height="60px"></div></td>
	<td width="580px"><div align="left"><font size="1">
		<?=$empresa_nombre?><br>
		<?=$empresa_direccion?><br>
		<?=$empresa_ciudad;?>

		</font></div>
	</td>
    <td width="20%">&nbsp;</td>
  </tr>
</table>
<br>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr> 
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></td>
		<td width="20%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></td>
		<td width="40%"><div align="center"><strong><font color="#000000" face="Arial, Helvetica, sans-serif">NOTA DE VENTA Nº <?=$sql_num_bol?></font></strong></div></td>
		<td width="20%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></td>
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif"></font></strong></div></td>
    </tr>
</table>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td width="100">Fecha:</td>
		<td width="157">
			<input name="txtfecha" type="text" style="border-width:0;" value="<?=$fecha?>" size="20" readonly="1">
	  </td>
		<td width="23">&nbsp;</td>
		<td width="82">&nbsp;</td>
		<td width="308">&nbsp;</td>
	</tr>
	<tr>
		<td width="100">R.U.T.</td>
		<td width="157">
			<input name="txtrut" type="text" size="20"  onKeyPress="return valida(event)" onBlur="comprueba()"/>
	  </td>
		<td width="23" align="left"><a onClick="JavaScript:PopWindow('buscacliente.php')">
			<IMG align="middle" border="0" src="Images/modificar.gif" style="cursor:pointer" title="Presione aqui para buscar cliente" width="16" height="16"></a>		</td>
		<td width="82" align="right"><div align="left">Señor: &nbsp;</div></td>
		<td width="308">
		  <input name="txtrsoc" id="txtrsoc" type="text" size="45" style="width:340px">
		</td>           
  </tr>
<tr>
		<td width="100">
			E-Mail:		</td>
		<td width="157">
			<input name="txtemail" id="txtemail" type="text" size="20">
		</td>
		<td width="23">&nbsp;</td>
		<td width="82" align="right"><div align="left">Direcci&oacute;n:&nbsp;		</div></td>
		<td width="308">
			<input name="txtdireccion" id="txtdireccion" type="text" size="45" style="width:340px">
		</td>
	</tr>
<tr>
		<td width="100">
			Fono:		</td>
		<td width="157">
			<input name="txtfono" id="txtfono" type="text" size="20">
		</td>
		<td width="23">&nbsp;</td>
		<td width="82" align="right"><div align="left">Regi&oacute;n:&nbsp;		</div></td>
		<td width="308"><label><select name="lstreg" onChange="rellenaCombo('form1');" style="width:340px">
        <option value="null" selected>SELECCIONE REGION
          	<?
			$cons_region = mysql_query("SELECT * FROM regiones;", $enlace);
 			$cont=0;
			while ( mysql_fetch_object ($cons_region) ) 
 			{
 				$ides=mysql_result($cons_region, $cont, "Cod_Region");
  				$nombres=mysql_result($cons_region, $cont, "Region");
  				echo "              <option value=\"".$ides."\">".$nombres."\n";
  				$cont = $cont + 1;
 			}
			?>
      </select></label></td>
	</tr>
	<tr>
		<td width="100">
			Fax:		</td>
		<td width="157">
			<input name="txtfax" id="txtfax" type="text" size="20">
		</td>
		<td width="23">&nbsp;</td>
		<td width="82" align="right">
			<div align="left">Comuna:&nbsp;		</div></td>
		<td width="308"><label><select name="lstcom" disabled="disabled" style="width:340px">
        <option value="null">SELECCIONE CIUDAD
        </select></label></td>
	</tr>
</table>

<br>
<table width="640" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
	<tr bgcolor="#000000"> 
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Cant.</font></strong></div></td>
		<td width="20%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">C&oacute;digo</font></strong></div></td>
		<td width="45%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Precio</font></strong></div></td>
		<td width="15%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Total $</font></strong></div></td>
    </tr>
<? 
$cant = "1";
$cant_real=0;
while ($cant<=$cnt)
	{
	if ($total[$cant]>0)
		{
		$cant_real+=1;
		?>

		
		
		<tr> 
			<td width="10%"><div align="left">
			    <input name="cantidad[<? echo "$cant_real";?>]" id="cantidad[<? echo "$cant_real";?>]" type="text" size="5" value="<? echo "$cantidad[$cant]";?>" readonly="1" style="border-width:0;" border="0">
			</div></td>
			<td width="20%"><div align="leftr">
				  <input name="codigo[<? echo "$cant_real";?>]" id="codigo[<? echo "$cant_real";?>]" type="text" size="17" value="<? echo "$codigo[$cant]";?>" readonly="1" style="border-width:0;" border="0">
			</div></td>
			<td width="45%"><div align="left">  
				<input name="detalle[<? echo "$cant_real";?>]" id="detalle[<? echo "$cant_real";?>]" type="text" size="38" value="<? echo "$detalle[$cant]";?>" readonly="1" style="border-width:0;" border="0">
			</div></td>
			
			<td width="10%"><div align="right">
				<input name="precio[<? echo "$cant_real";?>]" id="precio[<? echo "$cant_real";?>]" type="text" size="4" value="<? echo "$precio[$cant]";?>" readonly="1" style="border-width:0; text-align:right" border="0">
			</div></td>
				
			<td width="15%"><div align="right">
				<input name="total[<? echo "$cant_real";?>]" id="total[<? echo "$cant_real";?>]" type="text" size="9" value="<? echo "$total[$cant]";?>" readonly="1" style="border-width:0; text-align:right" border="0">
			</div></td>
		</tr>
		<? 
		}
	$cant=$cant+1;
	}
	?>
</table>
<div align="center"></div>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">SUB TOTAL</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_subtotal" id="sql_subtotal" type="text" size="9" value="<?=$txtsubtotal?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">DESCUENTO</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_descuento" id="sql_descuento" type="text" size="9" value="<?=$cal_descuento;?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">NETO</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<input name="sql_neto" id="sql_neto" type="text" size="9" value="<?=$sql_neto?>" readonly="1" style="border-width:0; text-align:right" border="0">
        </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="35%"><label>
		  <input type="text" name="txtcom" id="txtcom" style="visibility:hidden" />
		</label></td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">IVA</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<input name="sql_iva" id="sql_iva" type="text" size="9" value="<?=$sql_iva?>" readonly="1" style="border-width:0;text-align:right" border="0">
	    </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
	  	<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">TOTAL </font></strong></div></td>
	  	<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
	    $<input name="sql_total" id="sql_total" type="text" size="9" value="<?=$sql_total?>" readonly="1" style="border-width:0;text-align:right" border="0">
      </font></div></td>
    </tr>
</table>
<table width="640px" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  	<td width="100%" align="center"><input type="button" name="correo" id="correo" value="Enviar por correo" onClick="fun_mail();" />
	  	  <input type="button" name="Imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();" />
	  	    <input type="button" name="salir" id="salir" value="Salir" onClick="fun_salir();" /></td>
    </tr>
</table>
</form>
</body>
</html>
<script>
var nav4 = window.Event ? true : false;
<!-- Ingreso solo numeros y k K - . -->
function valida(evt)
	{
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57) || (key == 107) || (key == 75) || (key == 45) || (key == 46));
	}
function PopWindow(pagina)
	{
	if (pagina=='buscacliente.php')
		{
		window.open(pagina,'TEST','width=682,height=550,menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top=70,left=300');
		}
	else
		{
		window.open(pagina,'TEST','width=1,height=1,menubar=no,scrollbars=no,toolbar=no,location=no,directories=no,resizable=no,top=70,left=300');
		}
	}
function comprueba()
	{
	if (Rut(document.form1.txtrut.value)) PopWindow('comprueba.php?rutc='+document.form1.txtrut.value);
	}
function fun_guardar()
	{
	if (document.form1.txtrut.value!='' || document.form1.txtrsoc.value!='' || document.form1.txtdireccion.value!='' || 
		document.form1.txtemail.value!='' || document.form1.lstreg.value!='null' || document.form1.lstcom.value!='null' || 
		document.form1.txtfono.value!='' || document.form1.txtfax.value!='')
		{
		document.form1.action="notaventa/contado.php?cnt=<?=$cnt?>";
		document.form1.target="_blank";
		document.form1.submit();
		}
	else
		{
		alert('Ingrese todos los datos del cliente');
		}
	}
function rellenaCombo(formulario)
    {
        with (document.forms[formulario]) 
        {
          var region = lstreg[lstreg.selectedIndex].value; 
          var n = lstcom.length;  
          
          lstcom.disabled = false;  
          
          for (var i = 0; i < n; ++i)
            lstcom.remove(lstcom.options[i]); 
          
          lstcom[0] = new Option("SELECCIONE CIUDAD", 'null'); 
          
          if (region != 'null')  
          {
   <?php
   
    $cons_region = mysql_query("SELECT * FROM regiones;", $enlace);
    $cont=0;
    while ( mysql_fetch_object ($cons_region) )
    {
     $reg = mysql_result($cons_region, $cont, "Cod_Region");
   ?>
            if (region == '<?php echo $reg;?>')
            {
    <?php
    
      $cons_comuna = mysql_query("SELECT * FROM comunas WHERE region = ".$reg." ORDER BY comuna;", $enlace);
      $cont2=0;
      while ( mysql_fetch_object ($cons_comuna) )
      {
       $id_comuna = mysql_result($cons_comuna, $cont2, "cod_comuna");
       $comuna = mysql_result($cons_comuna, $cont2, "comuna");
    ?>
              lstcom[lstcom.length] = new Option("<?php echo $comuna;?>", '<?php echo $id_comuna;?>');
    <?php
    
	   	$cont2 = $cont2 + 1;
      }
    ?>
            }
   <?php
   
   		$cont = $cont + 1;
    }
   ?>
            lstcom.focus();  
          }
          else  
          {
            lstcom.disabled = true;  
            lstreg.focus(); 
          }
          
          lstcom.selectedIndex = 0;  
        }
	}
function valval()
	{
	var reg=document.form1.lstreg.value;
	rellenaCombo('form1');
	document.form1.lstcom.value=document.form1.txtcom.value;
	}
function fun_salir()
	{
	var answer = confirm ("¿Desea guardar la Nota de Venta?\n Cancelar para salir sin guardar");
	if (answer)
		{
		fun_guardar();
		}
	else
		{
		window.top.open('','_parent','');window.top.close();
		}
	}
function fun_mail()
	{
	if(document.form1.txtemail.value=="")
		{
		alert ("Ingrese correo");
		document.form1.txtemail.focus();
		return false;
		}
	form1.action="notaventa/correo.php?cnt=<?=$cont?>";
	document.form1.target="_blank";
	form1.submit();
	}	
function fun_descuento()
	{
	var total,i,j,cant1, codigo1, descrip1, precio1, total1, cnt;
	total = <?=$cant_real;?>;
	for (i=1;i<=total;i++){
		for (j=1;j<=(total-1);j++){
			cnt=(j+1);
			if ( Number(document.getElementById("total["+j+"]").value) > Number(document.getElementById("total["+cnt+"]").value))
				{
				
				cant1=document.getElementById("cantidad["+cnt+"]").value;
				codigo1=document.getElementById("codigo["+cnt+"]").value;
				descrip1=document.getElementById("detalle["+cnt+"]").value;
				precio1=document.getElementById("precio["+cnt+"]").value;
				total1=document.getElementById("total["+cnt+"]").value;
				document.getElementById("cantidad["+cnt+"]").value=document.getElementById("cantidad["+j+"]").value;
				document.getElementById("codigo["+cnt+"]").value=document.getElementById("codigo["+j+"]").value;
				document.getElementById("detalle["+cnt+"]").value=document.getElementById("detalle["+j+"]").value;
				document.getElementById("precio["+cnt+"]").value=document.getElementById("precio["+j+"]").value;
				document.getElementById("total["+cnt+"]").value=document.getElementById("total["+j+"]").value;
				document.getElementById("cantidad["+j+"]").value=cant1;
				document.getElementById("codigo["+j+"]").value=codigo1;
				document.getElementById("detalle["+j+"]").value=descrip1;
				document.getElementById("precio["+j+"]").value=precio1;
				document.getElementById("total["+j+"]").value=total1;
				}
			}
		}
	}
function fun_imprimir()
	{
	document.form1.imprimir.style.visibility="hidden";
	document.form1.salir.style.visibility="hidden";
	document.form1.correo.style.visibility="hidden";
	window.print();
	document.form1.imprimir.style.visibility="visible";
	document.form1.salir.style.visibility="visible";
	document.form1.correo.style.visibility="visible";
	}
</script>
