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

 $numfac=$_GET["numfac"];
 $cdgpro=$_GET["cdgpro"];

$consulta1 = "SELECT * FROM facturas_compras WHERE numfac='$numfac' AND cdgpro='$cdgpro' ";
$resultado1 = $mysqli->query($consulta1);
$fila1 = $resultado1->fetch_array();

        $codfactura = $fila1["codfactura"];
        $falcal     = $fila1["falcal"];
		
		
		      $parametros = "SELECT * FROM web_0000 ";
       $result_parametros = $mysqli->query($parametros);
       $row_para = $result_parametros->fetch_array();
     

	   $cantippre =  $row_para["cantippre"];    // se agrega en la version 4.4
	   

if ($result = $mysqli->query("SELECT * FROM factulinea_compra WHERE codfactura='$codfactura'  AND PRE001 = 0 and chkbon = 0 " )) {
   $filas = $result->num_rows;
   $result->close();
}

 
 if ($filas > 0 ){

 $falcal=1;
  	} 
	
	
 /****************AVERIGUA LA CANTIDAD DE ARTICULOS INACTIVOS************************************/
 	$consulta ="SELECT * FROM factulinea_compra WHERE codfactura='$codfactura'  ORDER BY numlinea ASC";

if ($resultado = $mysqli->query($consulta)) 
$inactivos =0;
$sumainactivos =0;

    while ($fila2 = $resultado->fetch_assoc()) {
	
	$cdgbar  = $fila2["codigo"];
	
	
	
	if ($result = $mysqli->query("SELECT * FROM web_1001 WHERE cdgbar='$cdgbar' AND ESTART = 2 ")) {
    $fila = $result->fetch_array(); 
   $inactivos = $result->num_rows;
   $sumainactivos = $sumainactivos + $inactivos;
 
}
	

	} //fin while
 
 
/****************************************************/



$consulta = "SELECT * FROM web_2201 WHERE numfac='$numfac' AND cdgpro='$cdgpro'  ";
$resultado = $mysqli->query($consulta);
$row = $resultado->fetch_array();

$cdgusu   = $row["cdgusu"];
$cdgpro   = $row["cdgpro"];
$dscpro   = $row["dscpro"];
$fecfac   = $row["fecfac"];
$fecfac   = implode("-", array_reverse(explode("-", $fecfac))); 
$fecpro   = $row["fecpro"];
$fecpro   = implode("-", array_reverse(explode("-", $fecpro))); 
$numfac   = $row["numfac"];
$fecven   = $row["fecven"];
$pladia   = $row["pladia"];
$subexe   = $row["subexe"];
$subgra   = $row["subgra"];
$desfactot= $row["desfactot"];
$impfac   = $row["impfac"];
$flefac   = $row["flefac"];
$otrfac   = $row["otrfac"];
$totfac   = $row["totfac"];
$cdgtipven= $row["cdgtipven"];
$cdgtipcxp= $row["cdgtipcxp"];
$estado   = $row["estado"];
$marcaborrado = $row["borrado"];
$marcaestado  = $row["estado"];



  if ($marcaborrado == 1) { $borrado="Factura Anulada"; } else { $borrado=""; } 
?>

<html>
	<head>
		<title>Principal</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">

		
		
				<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
		<link href="../calendario/calendar-blue.css" rel="stylesheet" type="text/css">
        
        <script src="../jquery/jquery-1.3.2.js" type="text/javascript"></script>
		<script src="../jquery/jquery.maskedinput-1.2.2.js" type="text/javascript"></script>
        	
		<script type="text/JavaScript" language="javascript" src="../calendario/calendar.js"></script>
		<script type="text/JavaScript" language="javascript" src="../calendario/lang/calendar-sp.js"></script>
		<script type="text/JavaScript" language="javascript" src="../calendario/calendar-setup.js"></script>
		<script type="text/javascript" src="../funciones/validar.js"></script>
		
		
		
		<script language="javascript">
		var cursor;
		if (document.all) {
		// Está utilizando EXPLORER
		cursor='hand';
		} else {
		// Está utilizando MOZILLA/NETSCAPE
		cursor='pointer';
		}
		
		function aceptar() {
			location.href="index.php";
		}
		
		
	
	

		
		//////////////////////////////  funcion imprimir  ///////////////////////////////
		
	

		

	function chequearEnter(event) {
    if (event.keyCode == 13) {  ocultarVentana(); }
} 
		
	
	
	function imprimir(codfactura) {
	if (document.getElementById("marcaborrado").value==1) {
  
    var ventana = document.getElementById( 'VentanaEmergente');
    ventana.style.marginTop = "100px";
    ventana.style.left = ((document.body.clientWidth-350) / 2) +  "px";
    ventana.style.display = 'block';
  
	 
	} else {	
				window.open("imprimir_detalle_compra.php?codfactura="+codfactura);
			
			}
		}

function ocultarVentana()
{
    var ventana = document.getElementById('miVentana');
    ventana.style.display = 'none';
	
	}
	
	
	function seleccionaFoco(){ 
   	document.formulario_emergente.boton.focus() 
   	document.formulario_emergente.texto.select() 
}
	
					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";}	
}
	
	
	   	function inicio() {
		
	
			document.getElementById("formulario_lineas").submit();

		}
		
		
		function Aplicar(codfactura,marcaestado,falcal,sumainactivos) {
	 if (confirm(" Desea Aplicar la Factura ? "))
	if (falcal==1) {
		 
		alert ("favor verifique factura posee articulos con precio de venta en cero. No puede ser aplicada")
	
		} else {	
	
	
				if (marcaestado==1) {
				
				alert ("favor verifique factura ya fue aplicada")
				
				} else {	
				
				
						if (sumainactivos >= 1) {
						alert ("Atencion, Factura posee articulos inactivos en Inventario , Corrigalo para poder APLICAR la factura")
					
						} else {	
						
						document.getElementById('bloquea').style.display='block';
						location.href="aplicar_factura.php?codfactura="+codfactura;
						}
						
						
					}		
			
			
		}	
		
			}
		
		
		
				function convertir_factura(codfactura) {
	
	    if (confirm(" Desea convertir esta Factura en Ajuste de Inventario ? "))
				   
			location.href="convertir_ajuste_inventario.php?codfactura=" + codfactura;
			
			}

		
		
		
			function insert_art(){
			
			
			
			
	var codfactura=document.getElementById("codfactura2").value;
	
		if( $('#checkart').is(':checked') ) {	
		
		var checkart=1;

		
		miPopup = window.open("marcararticulos_aplicar.php?checkart="+checkart+"&codfactura="+codfactura,"frame_datos","width=700,height=80,scrollbars=yes");
		
		//document.getElementById("checkpromos").value="1";
		//document.getElementById("form_busqueda").submit();
		
		}else{
		var checkart=0;
		
	
		
		miPopup = window.open("marcararticulos_aplicar.php?checkart="+checkart+"&codfactura="+codfactura,"frame_datos","width=700,height=80,scrollbars=yes");
		
		
		//document.getElementById("checkpromos").value="0";
		//document.getElementById("form_busqueda").submit();
		}
		
		document.getElementById("aplica").value="";
		document.getElementById("formulario_lineas").submit();
		
		
	
		
		
	
	}
	
	
		
		</script>
	</head>
		<body onLoad=" inicio(); ">
		<div id="">
			<div id="zonaVentanasCompras">
         	<div class="contenidoFactura">
			   <div align="center">
			     <form id="formulario_lineas" name="formulario_lineas" method="post" action="frame_lineas_ver.php" target="frame_lineas" >	
			<input  name="marcaborrado" type="hidden"  id="marcaborrado"  value="<?php echo $marcaborrado?>" />
				<input  name="numfac" type="hidden"  id="numfac"  value="<?php echo $numfac?>" />
					<input  name="cdgpro" type="hidden"  id="cdgpro"  value="<?php echo $cdgpro	?>" />
						<input  name="codfactura2" type="hidden"  id="codfactura2"  value="<?php echo $codfactura	?>" />
			
			
				<input  name="numlin" type="hidden"  id="numlin"  value="" />
			    <input  name="aplica" type="hidden"  id="aplica"  value="" />
				<input  name="cdgbar" type="hidden"  id="cdgbar"  value="" />
				
			     </form>
				
				         <?php
						 
						
						$query_tipoventa = "SELECT * FROM tipoventa WHERE cdgtipven='$cdgtipven' ";
                        $result = $mysqli->query($query_tipoventa);
                        $fila1 = $result->fetch_array();

     				    $cdgtipven = $fila1["dsctipven"];
    
						$query_tipocxp="SELECT * FROM tipo_cxp WHERE cdgcxp='$cdgtipcxp' ";
                        $result2 = $mysqli->query($query_tipocxp);
                        $fila2 = $result2->fetch_array();

     				    $cdgtipcxp = $fila2["dsccxp"];
					 
						
						if ($estado==0)  { 
						
						$estado=" FACTURA SIN APLICAR"; 
						
						} else { 
						
						$estado="FACTURA APLICADA";
						
						}
					  ?>
					  
					  
					  
				
				
				
			     <table class="fuente8" width="98%" cellspacing=0 cellpadding=3 border=0>
					
					   <tr>
					     <td>Proveedor</td>
					     <td colspan="6"><?php echo $cdgpro . " - " . $dscpro ?></td>
					     <td colspan="2"><?php echo $cdgtipven?></td>
					     <td width="9%">N. Factura</td>
					     <td width="5%"><?php echo $numfac;?></td>
				         <td rowspan="2"><img src="../img/Symbol-Check_48x48-32.png" alt="aceptar" width="48" height="48"  border="0" onClick="aceptar()" onMouseOver="style.cursor=cursor"></td>
				         <td rowspan="2"><img src="../img/HP_Printer_Icon_48.png" alt="imprimir" width="48" height="48" border="0" onClick="imprimir(<?php echo $codfactura?>) , seleccionaFoco() " onMouseOver="style.cursor=cursor"></td>
				         <td rowspan="2"><img src="../img/Rename_48x48-32.png" alt="Aplicar" width="48" height="48" border="0" onClick="Aplicar(<?php echo $codfactura?>,<?php echo $marcaestado?>,<?php echo $falcal?>, <?php echo $sumainactivos?>) " onMouseOver="style.cursor=cursor"></td>
		                 <td rowspan="2"><a href="#"><img src="../img/carrito.png" alt="Convertir Factura" width="48" height="48" title="Convertir Factura en Ajuste de Inventario" onClick="convertir_factura( '<?php echo $codfactura?>'  )"  border="0"></a></td>
				     </tr>
					   <tr>
					     <td height="31"> Estado</td>
					     <td colspan="4"><?php echo $estado?></td>
					     <td width="9%">Fecha Factura</td>
					     <td width="8%"><?php echo $fecfac?></td>
					     <td colspan="2">Fecha Proceso</td>
					     <td colspan="2"><?php echo $fecpro?></td>
			       </tr>
					   <tr>
					     <td>Cargada</td>
					     <td colspan="4"><?php echo $cdgtipcxp;?></td>
					     <td>Plazo</td>
					     <td><?php echo $pladia?></td>
					     <td colspan="2">Fecha Vence </td>
					     <td colspan="2"><?php echo $fecven?></td>
						   <td>&nbsp;</td>
						   <td>&nbsp;</td>
						   <td width="7%" colspan="2"><div align="right"><input type="checkbox" value="" name="checkart" id="checkart"  checked onClick="insert_art()" /></div></td>
				       </tr>
					   <tr>
						   <td width="7%">SubGra</td>
					       <td width="10%"><?php echo number_format($subgra,2,".",",");?></td>
					       <td width="5%">SubExe</td>
					       <td width="9%"><?php echo number_format($subexe,2,".",",");?></td>
					       <td width="3%">Desc</td>
					       <td><?php echo number_format($desfactot,2,".",",");?></td>
					       <td>Impuesto</td>
					       <td width="10%"><?php echo number_format($impfac,2,".",",");?></td>
					       <td width="6%"> Flete</td>
					       <td><?php echo number_format($flefac,2,".",",");?></td>
					       <td>Otros</td>
					       <td width="6%"><?php echo number_format($otrfac,2,".",",");?></td>
					       <td width="6%">Total</td>
					       <td colspan="2"><?php echo number_format($totfac,2,".",",");?></td>
				       </tr>
			         <br>
				     <tr>
					     <td></td>
					     <td colspan="14"></td>
				     </tr>
			     </table>
              </div>
					<table class="fuente8" width="100%" cellspacing=0 cellpadding=3 border=0 ID="Table1">
						<tr class="cabeceraTabla">
				  <?php  if ($cantippre > 1 )  {  ?>

							<td width="4%"><span >COD.</span></td>
							<td width="4%"><span>CANT.</span></td>
							<td width="25%"><span >DETALLE</span></td>
							<td width="4%"><span>IMP.PRO</span></td>
							<td width="4%">IMP.RED</td>
							<td width="5%"><span>COSTO</span></td>
							<td width="7%">FLETE</td>
							<td width="3%"><span >UTI</span></td>
							<td width="6%">D.PRO</td>
							<td width="6%">ANTERIOR</td>
                            <td width="6%">ACTUAL</td>
							<td width="4%"><span>D.FAC</span></td>
                            <td width="5%">UTI.M</td>
							<td width="5%"><span>P.MAY</span></td>
							<td width="5%"><span>MONTO</span></td>
							<td width="7%">&nbsp;</td>
							
				
					 <?php	  } else  {  ?>
					 
					 		<td width="7%"><span >COD.</span></td>
							<td align="left" width="11%"><span>CANT.</span></td>
							<td width="24%"><span >DETALLE</span></td>
							<td width="5%"><span>COSTO</span></td>
							<td width="9%"><span >FLETE</span></td>
							<td width="3%">UTI</td>
							<td width="7%">D.PRO</td>
							<td width="9%">ANTERIOR</td>
							<td width="7%"><span>ACTUAL</span></td>
                            <td width="5%"><span>D.FAC</span></td>
							<td width="9%"><span>MONTO</span></td>
					 
					 
					 	 <?php	  }  ?>		
							
						</tr>
				</table>
						<div id="lineaResultado2">
				  <iframe width="100%" height="370" id="frame_lineas" name="frame_lineas" frameborder="0">
                  <ilayer width="100%" height="335" id="frame_lineas" name="frame_lineas"></ilayer>
				  </iframe>
				</div>	
					
			  </div>
				<div id="botonBusquedaAWR">					<div align="center">
		     
		  <p>&nbsp;</p>

				        </div>
                        
                        
                   
      <div    style="display:none; position:absolute; top:10%;  left:50%;  height:15%;  width:30%; background-color:white"   id="VentanaEmergente">
           
		  <p>
		  
		    </form>
			
						  				<iframe id="frame_datos" name="frame_datos" width="0" height="0" frameborder="0">
					<ilayer width="0" height="0" id="frame_datos" name="frame_datos"></ilayer>
					</iframe>



        <form id="formulario_emergente" name="formulario_emergente"  >
				
			     <table class="fuente8" width="98%" cellspacing=0 cellpadding=3 border=0>
					   <tr>
					     <td colspan="27" align="center" >Factura ya fue anulada no puede ser impresa !!     Favor Verifique</td>
			       </tr>
					   <tr>
				
					    
					     <td  width="20%" align="center" >&nbsp;</td>
					     <td  width="63%" align="center"  ><input  type="botton" value="OK" id="boton"   border="0" onClick= "ocultarVentana()"  style='text-align:center;' onMouseOver="style.cursor=cursor"  /></td>
					     <td  width="17%" align="center" >&nbsp;</td>
			       </tr>
					   <tr>
					     <td colspan="12" align="center" ><input type="image" value="Botón" id="boton2"   src="../img/Symbol-Error_128x128-32.png"  width="128" height="128" border="0" onClick= "ocultarVentana()"  onMouseOver="style.cursor=cursor"  /> </td>
                         
                        
			       </tr>

                        
					    <input id="codfactura" name="codfactura" value="<?php echo $codfactura?>"  onkeypress="chequearEnter(event)" type="hidden">	
							
		  </table>
		
	    </form>
		
		

</div>
                        
			   </div>
			  </div>
		  </div>
		</div>
		
		<div id="bloquea" class="cargando" style="display:none;">
	<img style="margin-left: 5%;margin-top: 15%" alt="Espere..." src="../img/procesando.gif" />
	</div>
	
	</body>
</html>
