<?php
date_default_timezone_set('America/Costa_Rica');

session_start();

 $usu=$_SESSION['username'];
 

		include('../conexion.php'); 
		if($_SESSION['tipo_usu']=='a' or $_SESSION['tipo_usu']=='s'  or $_SESSION['tipo_usu']=='c'  or $_SESSION['tipo_usu']=='v'){
		}else{
			header('location:acesso_denegado.php');
		}

		
		$consulta = "SELECT * FROM usuarios WHERE usu='$usu'";
		$resultado = $mysqli->query($consulta);
		$row = $resultado->fetch_array();
		$cod= $row["codusu"];


		$codfactura=$_GET["codfactura"];
		
		$sel_cxc="SELECT * FROM facturas_compras WHERE codfactura='$codfactura'"; 
		$resultado = $mysqli->query($sel_cxc);
		$row2 = $resultado->fetch_array();
		
		$cdgpro   = $row2["cdgpro"];
		$dscpro   = $row2["dscpro"];
		$numfac   = $row2["numfac"];
		
		
		$query_estado="SELECT * FROM web_2201 WHERE '$cdgpro' AND  numfac = '$numfac' ";
		$result = $mysqli->query($query_estado);
		$row3 = $result->fetch_array();
		
		$estado = $row3["estado"];
		
			
	//************************************************ Anulo en la  tabla Facturas y 2201 *********************************


	$query = "delete from WEB_2201 where cdgpro = '$cdgpro' AND  numfac = '$numfac'"; 
    $mysqli->query($query);
				

	$query2 = "delete from facturas_compras where  codfactura = '$codfactura'";  
	$mysqli->query($query2);

  	
	//************************************************ Anulo en la  tabla 1001 *********************************

 
	
     if ($estado==1) {

/////////////////////////////////////////////////////  rebajar de inventario ///////////////////////////////

	$consulta4 ="SELECT * FROM factulinea_compra WHERE codfactura='$codfactura'   ORDER BY numlinea ASC";
$contador=0;
if ($resultado4 = $mysqli->query($consulta4)) 

    while ($fila2 = $resultado4->fetch_assoc()) {

	
	            $numlinea = $fila2["numlinea"];
				$cdgbar  = $fila2["codigo"];
				$dscart  = $fila2["dscart"];
				$cdglin  = $fila2["cdglin"];
				$cdgfam  = $fila2["cdgfam"];
				$canart  = $fila2["canart"];
				$cosart  = $fila2["cosart"];
				$utiart1 = $fila2["utiart1"];
				$utiart2 = $fila2["utiart2"];  
				$PRE001  = $fila2["PRE001"];
				$PRE002  = $fila2["PRE002"];
				$importe = $fila2["importe"];
				$desfac  = $fila2["desfac"];
				$despro  = $fila2["despro"];  
				$impart  = $fila2["impart"];
				$fleart  = $fila2["fleart"];
				$implin  = $fila2["implin"];
				$deslin  = $fila2["deslin"];
				$fleart  = $fila2["fleart"];
				$modpre  = $fila2["modpre"];
				$importe  = $fila2["importe"];
	


		 ////////////////////////////////////////////////   rebajo de unidades al inventatio ///////////////////////

  
   	 	$query_unidades= "SELECT CDGBAR, EXIACT  FROM web_1001 WHERE cdgbar='$cdgbar'";
		$resultado = $mysqli->query($query_unidades);
		$row2 = $resultado->fetch_array();
	    $exiact = $row2["EXIACT"];

		 $exinueva=$exiact - $canart;


		  $sel_articulos="UPDATE web_1001 SET exiact = '$exinueva' WHERE cdgbar='$cdgbar' and tipart != 2     ";
		  $mysqli->query($sel_articulos);
				  
				  
				  
		  
  //////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		$contador++;
	}
		
	}
	
	$query_saldo ="SELECT SUM(salact) AS saldo FROM web_2201 WHERE cdgpro='$cdgpro' AND salact > 0 ";
    $resultado3 = $mysqli->query($query_saldo);
    $row3 = $resultado3->fetch_array();
		
	$salact = $row3['saldo'];


	 $nuevo_saldo="UPDATE proveedores SET  salact='$salact' WHERE cdgpro='$cdgpro'";
	 $mysqli->query($nuevo_saldo);

	
	/////////////   borra las lineas de las compras ///////////
	
	 $query3 = "delete from factulinea_compra where  codfactura = '$codfactura'";  
	 $mysqli->query($query3);
		
	?>
<html>
	<head>
		<title>Factura Nula</title>
		<link href="../estilos/estilos.css" type="text/css" rel="stylesheet">
		<link href="../calendario/calendar-blue.css" rel="stylesheet" type="text/css">
		<script type="text/javascript" src="../funciones/validar.js"></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 language="javascript">
		var cursor;
		if (document.all) {
		// Está utilizando EXPLORER
		cursor='hand';
		} else {
		// Está utilizando MOZILLA/NETSCAPE
		cursor='pointer';
		}
		
	
	    function cancelar() {
			location.href="index.php";
		}
		
				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>








</head>
	<body onLoad="nobackbutton();">
		<div id="">
			<div id="zonaContenido">
         	<div class="contenidoFactura">
			   <div align="center">
				<div id="tituloForm" class="header">
               Factura Anulada Exitosamente 
			   </div>
			    <br> </br>
				<table width="100%" border="0">
  <tr>
    <td colspan="4">&nbsp;</td>
    </tr>
</table>

				
			    
			  </div>
				<div id="botonBusquedaAWR">					
				
				<div id="" align="center">
					<img src="../img/Symbol-Check_128x128-32.png" width="128" height="128" onClick="cancelar()" border="0" onMouseOver="style.cursor=cursor" title="Salir">
			
			  </div>
				    
			   </div>
			  </div>
		  </div>
		</div>
	</body>
</html>


