<?
include ("../conectar.php"); 
include ("../funciones/fechas.php"); 

$codfactura=$_GET["numfac"];
$cadena_busqueda=$_GET["cadena_busqueda"];

$query="SELECT * FROM facturas_contado WHERE codfactura='$codfactura'";
$rs_query=mysql_query($query);
$cdgcli=mysql_result($rs_query,0,"cdgcli");
$fecha=mysql_result($rs_query,0,"fecha");
//$cob_iv=mysql_result($rs_query,0,"cob_iv");
//$vehpla=mysql_result($rs_query,0,"vehpla");

?>

<html>
	<head>
		<title>Principal</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
		<script language="javascript">
		
			function aceptar()
	
			{
				if(window.confirm("desea imprimir la factura ?"))
				var mensaje="";
				if (document.getElementById("pagacon").value=="") mensaje+="  - Monto Pagado\n";
				//if (document.getElementById("vuelto").value=="") mensaje+="  - Monto Pagado\n";
				
				if (mensaje!="") {
					alert("Atencion, falta completar los siguientes campos:\n\n"+mensaje);
				} else {
					document.getElementById("formulario").submit();
				}
			}
		
		function Calcular_Vuelto()

			{
				var mensaje="";
				if (parseFloat(document.getElementById("preciototal").value) <= parseFloat ( document.getElementById("pagacon").value )){ 
	  				var preciototal=document.getElementById("preciototal").value;
					var pagacon=document.getElementById("pagacon").value;
	                var vuelto =  pagacon - preciototal  ;
	 
			         document.getElementById("vuelto").value=vuelto;
			
				} else {
				   
					alert("Atencion, El monto pagado no puede ser menor al Abono:\n\n"+document.getElementById("preciototal").value);
				document.getElementById("pagacon").value="";
			    document.getElementById("vuelto").value="";
			}
			}
		
		var cursor;
		if (document.all) {
		// Está utilizando EXPLORER
		cursor='hand';
		} else {
		// Está utilizando MOZILLA/NETSCAPE
		cursor='pointer';
		}
		
	
		
		function imprimir(codfactura) {
			window.open("../fpdf/imprimir_facturamx.php?codfactura="+codfactura);
		}
		
		
		
		function puntitos(donde,caracter,campo)
{
var decimales = false
campo = eval("donde.form." + campo)
	for (d =0; d < campo.length; d++)
		{
		if(campo[d].checked == true)
			{
			dec = new Number(campo[d].value)
			break;
			}
		}
	if (dec != 0)
		{decimales = true}




pat = /[\*,\+,\(,\),\?,\\,\$,\[,\],\^]/
valor = donde.value
largo = valor.length
crtr = true
if(isNaN(caracter) || pat.test(caracter) == true)
	{
	if (pat.test(caracter)==true) 
		{caracter = "\\" + caracter}
	carcter = new RegExp(caracter,"g")
	valor = valor.replace(carcter,"")
	donde.value = valor
	crtr = false
	}
else
	{
	var nums = new Array()
	cont = 0
	for(m=0;m<largo;m++)
		{
		if(valor.charAt(m) == "" || valor.charAt(m) == " " || valor.charAt(m) == ".")
			{continue;}
		else{
			nums[cont] = valor.charAt(m)
			cont++
			}
		
		}
	}

if(decimales == true) {
	ctdd = eval(1 + dec);
	nmrs = 1
	}
else {
	ctdd = 1; nmrs = 3
	}
var cad1="",cad2="",cad3="",tres=0
if(largo > nmrs && crtr == true)
	{
	for (k=nums.length-ctdd;k>=0;k--){
		cad1 = nums[k]
		cad2 = cad1 + cad2
		tres++
		if((tres%3) == 0){
			if(k!=0){
				cad2 = "" + cad2
				}
			}
		}
		
	for (dd = dec; dd > 0; dd--)	
	{cad3 += nums[nums.length-dd] }
	if(decimales == true)
	{cad2 += "." + cad3}
	 donde.value = cad2
	}
donde.focus()
}	

		
						function nobackbutton(){	
   window.location.hash="no-back-button";	
   window.location.hash="Again-No-back-button" //chrome	
   window.onhashchange=function(){window.location.hash="no-back-button";}	
}
		
		</script>
	    <style type="text/css">
<!--
.Estilo1 {color: #00FF33}
-->
        </style>
</head>
	<body>
		<div id="pagina">
			<div id="zonaVentanas">
         	<div class="contenidoFactura">
			   <div align="center">
				<div id="tituloForm" class="header">
                FACTURA </div>
				<form id="formulario" name="formulario" method="post" action="../Factura/guardar_proforma.php">
				
			     <table class="fuente8" width="98%" cellspacing=0 cellpadding=3 border=0>
					   <? 
						 $sel_cliente="SELECT * FROM web_0301 WHERE cdgcli=$cdgcli"; 
						  $rs_cliente=mysql_query($sel_cliente); ?>
					   <tr>
					     <td>&nbsp;</td>
					     <td>&nbsp;</td>
			             <td>&nbsp;</td>
			             <td>&nbsp;</td>
				         <td>&nbsp;</td>
				         <td>&nbsp;</td>
				   </tr>
					   <tr>
					     <td>&nbsp;</td>
					     <td>&nbsp;</td>
				         <td>&nbsp;</td>
				         <td>&nbsp;</td>
				         <td>Total</td>
				         <td><?php echo $preciototal?></td>
				     </tr>
					   <tr>
						   <td width="11%" height="26">Cliente</td>
						   <td width="15%"><?php echo mysql_result($rs_query,0,"dsccli");?></td>
							<td width="16%">N. Factura  </td>
							<td width="15%"><?php echo $numfactura?></td>
				            <td width="14%">Pagado</td>
				            <td width="29%">&nbsp;</td>
					   </tr>
					   <tr>
						   <td width="11%"><p>Cédula</p></td>
					       <td><?php echo mysql_result($rs_cliente,0,"cedcli");?></td>
					       <td>Fecha</td>
					       <td><?php echo $fecha; ?></td>
				           <td>Vuelto</td>
				           <td>&nbsp;</td>
					   </tr>
					   <tr>
					     <td>Direcci&oacute;n</td>
					     <td><?php echo mysql_result($rs_cliente,0,"dircli"); ?></td>
				         <td>&nbsp;</td>
				         <td><?php echo $vehpla; ?></td>
				         <td>&nbsp;</td>
				         <td>&nbsp;</td>
				     </tr>
			         <br>
				     <tr>
					     <td></td>
					     <td colspan="5"></td>
				     </tr>
			     </table>
               </div>
					 <table class="fuente8" width="100%" cellspacing=0 cellpadding=3 border=0 ID="Table1">
						<tr class="cabeceraTabla">
							<td width="8%">LINEA</td>
							<td width="10%">CODIGO</td>
							<td width="42%">DESCRIPCION</td>
							<td width="10%">CANTIDAD</td>
							<td width="10%">PRECIO</td>
							<td width="10%">DCTO %</td>
							<td width="10%">MONTO</td>
						</tr>
					</table>
					<table class="fuente8" width="100%" cellspacing=0 cellpadding=3 border=0 ID="Table1">
					  <? $sel_lineas="SELECT factulinea_contado.*,web_1001.* FROM factulinea_contado,web_1001 WHERE factulinea_contado.codfactura='$codfactura' AND factulinea_contado.codigo=web_1001.cdgart ORDER BY factulinea_contado.numlinea ASC";
$rs_lineas=mysql_query($sel_lineas);
						for ($i = 0; $i < mysql_num_rows($rs_lineas); $i++) {
							$numlinea=mysql_result($rs_lineas,$i,"numlinea");
							//$codfamilia=mysql_result($rs_lineas,$i,"codfamilia");
							//$nombrefamilia=mysql_result($rs_lineas,$i,"nombrefamilia");
							//$codarticulo=mysql_result($rs_lineas,$i,"codarticulo");
							$cdgart=mysql_result($rs_lineas,$i,"cdgart");
							$dscart=mysql_result($rs_lineas,$i,"dscart");
							$cantidad=mysql_result($rs_lineas,$i,"cantidad");
							$preivi=mysql_result($rs_lineas,$i,"preivi");
							$prenivi=mysql_result($rs_lineas,$i,"prenivi");
							$importe=mysql_result($rs_lineas,$i,"importe");
							$impuesto=mysql_result($rs_lineas,$i,"impuesto");
								
							$descuento=mysql_result($rs_lineas,$i,"dcto");
							if ($i % 2) { $fondolinea="itemParTabla"; } else { $fondolinea="itemImparTabla"; } ?>
									<tr class="<? echo $fondolinea?>">
										<td width="8%"><? echo $i+1?></td>
										<td width="10%"><? echo $cdgart?></td>
										<td width="42%"><? echo $dscart?></td>
										<td width="10%"><? echo $cantidad?></td>
										<td width="10%" ><? echo $prenivi?></td>
										<td width="10%" ><? echo $descuento?></td>
										<td width="10%" ><? echo $importe?></td>
									</tr>
									<? $baseimponible=$baseimponible+$importe; 
									 $baseimpuestos=$baseimpuestos + $impuesto;?>
					<? } ?>
					</table>
			
			  <?
				$preciototal=$baseimponible+$baseimpuestos;
				//$preciototal=number_format($preciototal,2); ?>
			   <div id="ztotalesawR" align="right">
					<p>&nbsp;</p>
					<table width="50%" border=0 align="right" cellpadding=3 cellspacing=0 class="fuente8">
						<tr>
						  <td width="85">Paga </td>
							<td width="130" height="28"><input id="pagacon" type="text"  name="pagacon" value="" onBlur="Calcular_Vuelto()" onChange="Calcular_Vuelto()"  onKeyUp="puntitos(this,this.value.charAt(this.value.length-1),'decimales')"/></td>
							<td width="128">SubTotal</td>
							<td width="1">&nbsp;</td>
							<td width="94"  class="cajaTotales" ><?php echo number_format($baseimponible,2);?> <? echo $simbolomoneda ?></td>
					<input id="preciototal"  name="preciototal" type="hidden" value="<? echo $preciototal?>" /></td>
						</tr>
						<tr>
						  <td width="85">Vuelto</td>
							<td width="130"><input   id="vuelto" type="text"  name="vuelto" value="" readonly 
							onKeyUp="puntitos(this,this.value.charAt(this.value.length-1),'decimales')"/></td>
										  <?
				//$preciototal=$baseimponible+$baseimpuestos;
				$preciototal=number_format($preciototal,2); ?>
				        
						 <input name="decimales" type="hidden" value="2" checked>
       <input type="hidden" name="decimales" value="4" >
       <input type="hidden" name="decimales" value="3">
			  <input id="accion" name="accion" value="alta" type="hidden">
			    <input id="cdgtipven" name="cdgtipven" value="1" type="hidden">	
					    <input id="codfactura" name="codfactura" value="<?php echo $codfactura?>" type="hidden">	
							<td width="128">IVA</td>
							<td width="1">&nbsp;</td>
							<td width="94"  class="cajaTotales" ><?php echo number_format($baseimpuestos,2);?> <? echo $simbolomoneda ?></td>
						</tr>
						<tr>
						  <td width="85">&nbsp;</td>
							<td width="130">&nbsp;</td>
							<td width="128">Total</td>
							<td width="1">&nbsp;</td>
							<td width="94"  class="cajaTotales" ><?php echo $preciototal?> <? echo $simbolomoneda ?></td>
						</tr>
					</table>
			  </div>
				<div id="botonBusquedaAWR">					<div align="center">
				  <p>&nbsp;</p>
				  <p class="Estilo1">&nbsp;</p>
				  <p><img src="../img/Symbol-Check_48x48-32.png" width="48" height="48" border="0" align="right" class="aCentro" onClick="aceptar()" onMouseOver="style.cursor=cursor"><img src="../img/HP_Printer_Icon_48.png" width="48" height="48" border="0" align="right" class="aCentro" onClick="imprimir(<? echo $codfactura?>)" onMouseOver="style.cursor=cursor"></p>
						  </form>
				</div>
				

			  </div>
		  </div>
		</div>
	</body>
</html>
