<?php
session_start();
include_once("../scripts/connect.php");
include("../scripts/session.php");
//Validar Session

    $sesion=new Sesion("","","");/*Se crea un objeto nuevo con el privilegio necesario para la pag. actual*/

    if(!$sesion->isValida("1")){ /*Sesion invalida*/
	header("Location: ../login.php?err=NoSesion");
    }/*no hay sesion*/

    else{
       $dato=explode("|",$_SESSION['usuario']);
       $sesion->setDatos($dato[0],$dato[1],$dato[2]);
    }
    //fin validar session


  $conexion=conecta_default();
  
  
  function limpiacal()
  {
  	$fech = date("Y-m-d");
  	$limpia = "UPDATE calificados SET primeraG = '0', segundaG = '0', terceraG = '0', cuartaG = '0', quintaG = '0', fecha = '$fech'";
  	mysqli_query($conexion, $limpia) or die('Err0: '.mysqli_error($conexion));
  }
  
  function de5endelante($p1,$p2,$p3,$p4,$p5)
  {
  	$sel5 = "SELECT id_inver, califico FROM inversionista WHERE idprom = '$p5'";
	$cons5 = mysqli_query($conexion, $sel5) or die('Err0: '. mysqli_error($conexion));
	if(mysqli_affected_rows($conexion))
	{
		$calif5 = "SELECT COUNT(*) as numc FROM inversionista WHERE idprom = '$p5' and califico = 'SI'";
	 	$qry5 = mysqli_query($conexion, $calif5) or die('Err0: '. mysqli_error($conexion));
	 	$n = mysqli_fetch_object($qry5);
	 		  
	 	$priG = "SELECT primeraG FROM calificados WHERE inversionista_id_inver = '$p5'";
	 	$qq = mysqli_query($conexion, $priG) or die('Err0: '. mysqli_error($conexion));
		$r = mysqli_fetch_object($qq);
	   	$tot = $r->primeraG + $n->numc;
  	    $actprom1 = "UPDATE calificados SET primeraG = '$tot' WHERE inversionista_id_inver = '$p5'";
	   	$qu1 = mysqli_query($conexion, $actprom1) or die('Err0: '. mysqli_error($conexion)); 
	 	  	  
	   	$segG = "SELECT segundaG FROM calificados WHERE inversionista_id_inver = '$p4'";
	 	$qq2 = mysqli_query($conexion, $segG) or die('Err0: '. mysqli_error($conexion));
	 	$r = mysqli_fetch_object($qq2);
	   	$tot = $r->segundaG + $n->numc;
  		$actprom2 = "UPDATE calificados SET segundaG = '$tot' WHERE inversionista_id_inver = '$p4'";
	    $qu1 = mysqli_query($conexion, $actprom2) or die('Err0: '. mysqli_error($conexion));	
	   	      
	    $terG = "SELECT terceraG FROM calificados WHERE inversionista_id_inver = '$p3'";
	 	$qq3 = mysqli_query($conexion, $terG) or die('Err0: '. mysqli_error($conexion));
	 	$r = mysqli_fetch_object($qq3);
	   	$tot = $r->terceraG + $n->numc;
  		$actprom3 = "UPDATE calificados SET terceraG = '$tot' WHERE inversionista_id_inver = '$p3'";
	    $qu1 = mysqli_query($conexion, $actprom3) or die('Err0: '. mysqli_error($conexion));
	      	   	 
	    $cuaG = "SELECT cuartaG FROM calificados WHERE inversionista_id_inver = '$p2'";
	 	$qq4 = mysqli_query($conexion, $cuaG) or die('Err0: '. mysqli_error($conexion));
	 	$r = mysqli_fetch_object($qq4);
	   	$tot = $r->cuartaG + $n->numc;
  		$actprom4 = "UPDATE calificados SET cuartaG = '$tot' WHERE inversionista_id_inver = '$p2'";
	    $qu1 = mysqli_query($conexion, $actprom4) or die('Err0: '. mysqli_error($conexion));
	    
	    $quiG = "SELECT quintaG FROM calificados WHERE inversionista_id_inver = '$p1'";
	    $qq5 = mysqli_query($conexion, $quiG);
	    $r = mysqli_fetch_object($qq5);
	    $tot = $r->quintaG + $n->numc;
  		$actprom5 = "UPDATE calificados SET quintaG = '$tot' WHERE inversionista_id_inver = '$p1'";
	    $qu1 = mysqli_query($conexion, $actprom5) or die('Err0: '. mysqli_error($conexion));
	    
	    $p1 = $p2;
	    $p2 = $p3;
	    $p3 = $p4;
	    $p4 = $p5;
	      
	    while($res5 = mysqli_fetch_object($cons5))
	    {
	 	  $p5 = $res5 -> id_inver;
	 	  de5endelante($p1,$p2,$p3,$p4,$p5);
	     }   
     }
  } 
  
  
  function revisa_cal($raiz)
  {
  	$prom1 = $raiz;
  	$sel = "SELECT id_inver, califico FROM inversionista WHERE idprom = '$prom1'";
	$cons = mysqli_query($conexion, $sel) or die('Err0: '. mysqli_error($conexion));
	if(mysqli_affected_rows($conexion))
	{	
	 $calif = "SELECT COUNT(*) as numc FROM inversionista WHERE idprom = '$prom1' and califico = 'SI'";
	 $qry = mysqli_query($conexion, $calif) or die('Err0: '. mysqli_error($conexion));
	 $n = mysqli_fetch_object($qry);
     
	 $priG = "SELECT primeraG FROM calificados WHERE inversionista_id_inver = '$prom1'";
	 $qq = mysqli_query($conexion, $priG) or die('Err0: '. mysqli_error($conexion));
	 $r = mysqli_fetch_object($qq);
	 $tot = $r->primeraG + $n->numc;
  	 $actprom1 = "UPDATE calificados SET primeraG = '$tot' WHERE inversionista_id_inver = '$prom1'";
	 $qu1 = mysqli_query($conexion, $actprom1) or die('Err0: '. mysqli_error($conexion));
  	   
	 while($res = mysqli_fetch_object($cons))
	 {
		$prom2 = $res->id_inver;
		$sel2 = "SELECT id_inver, califico FROM inversionista WHERE idprom = '$prom2'";
	    $cons2 = mysqli_query($conexion, $sel2) or die('Err0: '. mysqli_error($conexion));
		if(mysqli_affected_rows($conexion))
		{	
	 	  $calif2 = "SELECT COUNT(*) as numc FROM inversionista WHERE idprom = '$prom2' and califico = 'SI'";
	 	  $qry2 = mysqli_query($conexion, $calif2) or die('Err0: '. mysqli_error($conexion));
	 	  $n = mysqli_fetch_object($qry2);
	 	  
	 	  $priG = "SELECT primeraG FROM calificados WHERE inversionista_id_inver = '$prom2'";
	 	  $qq = mysqli_query($conexion, $priG) or die('Err0: '. mysqli_error($conexion));
		  $r = mysqli_fetch_object($qq);
	      $tot = $r->primeraG + $n->numc;
  	      $actprom1 = "UPDATE calificados SET primeraG = '$tot' WHERE inversionista_id_inver = '$prom2'";
	      $qu1 = mysqli_query($conexion, $actprom1) or die('Err0: '. mysqli_error($conexion)); 
	 	  
	      $segG = "SELECT segundaG FROM calificados WHERE inversionista_id_inver = '$prom1'";
	 	  $qq2 = mysqli_query($conexion, $segG) or die('Err0: '. mysqli_error($conexion));
	 	  $r = mysqli_fetch_object($qq2);
	      $tot = $r->segundaG + $n->numc;
  	      $actprom2 = "UPDATE calificados SET segundaG = '$tot' WHERE inversionista_id_inver = '$prom1'";
	      $qu1 = mysqli_query($conexion, $actprom2) or die('Err0: '. mysqli_error($conexion));	
	  	  
	      while($res2 = mysqli_fetch_object($cons2))
		  {
	 		$prom3 = $res2->id_inver;
			$sel3 = "SELECT id_inver, califico FROM inversionista WHERE idprom = '$prom3'";
	    	$cons3 = mysqli_query($conexion, $sel3) or die('Err0: '. mysqli_error($conexion));
			if(mysqli_affected_rows($conexion))
			{	
	 		  $calif3 = "SELECT COUNT(*) as numc FROM inversionista WHERE idprom = '$prom3' and califico = 'SI'";
	 		  $qry3 = mysqli_query($conexion, $calif3) or die('Err0: '. mysqli_error($conexion));
	 		  $n = mysqli_fetch_object($qry3);
	 		  
	 		  $priG = "SELECT primeraG FROM calificados WHERE inversionista_id_inver = '$prom3'";
	 		  $qq = mysqli_query($conexion, $priG) or die('Err0: '. mysqli_error($conexion));
			  $r = mysqli_fetch_object($qq);
	   		  $tot = $r->primeraG + $n->numc;
  	    	  $actprom1 = "UPDATE calificados SET primeraG = '$tot' WHERE inversionista_id_inver = '$prom3'";
	   	      $qu1 = mysqli_query($conexion, $actprom1) or die('Err0: '. mysqli_error($conexion)); 
	 	  	  
	   	      $segG = "SELECT segundaG FROM calificados WHERE inversionista_id_inver = '$prom2'";
	 		  $qq2 = mysqli_query($conexion, $segG) or die('Err0: '. mysqli_error($conexion));
	 		  $r = mysqli_fetch_object($qq2);
	   		  $tot = $r->segundaG + $n->numc;
  		      $actprom2 = "UPDATE calificados SET segundaG = '$tot' WHERE inversionista_id_inver = '$prom2'";
	      	  $qu1 = mysqli_query($conexion, $actprom2) or die('Err0: '. mysqli_error($conexion));	
	   	      
	      	  $terG = "SELECT terceraG FROM calificados WHERE inversionista_id_inver = '$prom1'";
	 		  $qq3 = mysqli_query($conexion, $terG) or die('Err0: '. mysqli_error($conexion));
	 		  $r = mysqli_fetch_object($qq3);
	   		  $tot = $r->terceraG + $n->numc;
  		      $actprom3 = "UPDATE calificados SET terceraG = '$tot' WHERE inversionista_id_inver = '$prom1'";
	      	  $qu1 = mysqli_query($conexion, $actprom3) or die('Err0: '. mysqli_error($conexion));
	      	  
	  	  	  while($res3 = mysqli_fetch_object($cons3))
		  	  {
	 			$prom4 = $res3->id_inver;
				$sel4 = "SELECT id_inver, califico FROM inversionista WHERE idprom = '$prom4'";
	    		$cons4 = mysqli_query($conexion, $sel4) or die('Err0: '. mysqli_error($conexion));
				if(mysqli_affected_rows($conexion))
				{	
	 		 	 $calif4 = "SELECT COUNT(*) as numc FROM inversionista WHERE idprom = '$prom4' and califico = 'SI'";
	 		 	 $qry4 = mysqli_query($conexion, $calif4) or die('Err0: '. mysqli_error($conexion));
	 		 	 $n = mysqli_fetch_object($qry4);
	 		  
	 		 	 $priG = "SELECT primeraG FROM calificados WHERE inversionista_id_inver = '$prom4'";
	 		  	 $qq = mysqli_query($conexion, $priG) or die('Err0: '. mysqli_error($conexion));
			  	 $r = mysqli_fetch_object($qq);
	   		  	 $tot = $r->primeraG + $n->numc;
  	    	  	 $actprom1 = "UPDATE calificados SET primeraG = '$tot' WHERE inversionista_id_inver = '$prom4'";
	   	      	 $qu1 = mysqli_query($conexion, $actprom1) or die('Err0: '. mysqli_error($conexion)); 
	 	  	  
	   	       	 $segG = "SELECT segundaG FROM calificados WHERE inversionista_id_inver = '$prom3'";
	 		  	 $qq2 = mysqli_query($conexion, $segG) or die('Err0: '. mysqli_error($conexion));
	 		  	 $r = mysqli_fetch_object($qq2);
	   		  	 $tot = $r->segundaG + $n->numc;
  		      	 $actprom2 = "UPDATE calificados SET segundaG = '$tot' WHERE inversionista_id_inver = '$prom3'";
	      	  	 $qu1 = mysqli_query($conexion, $actprom2) or die('Err0: '. mysqli_error($conexion));	
	   	      
	      	   	 $terG = "SELECT terceraG FROM calificados WHERE inversionista_id_inver = '$prom2'";
	 		  	 $qq3 = mysqli_query($conexion, $terG) or die('Err0: '. mysqli_error($conexion));
	 		   	 $r = mysqli_fetch_object($qq3);
	   		  	 $tot = $r->terceraG + $n->numc;
  		      	 $actprom3 = "UPDATE calificados SET terceraG = '$tot' WHERE inversionista_id_inver = '$prom2'";
	      	  	 $qu1 = mysqli_query($conexion, $actprom3) or die('Err0: '. mysqli_error($conexion));
	      	   	 
	      	  	 $cuaG = "SELECT cuartaG FROM calificados WHERE inversionista_id_inver = '$prom1'";
	 		  	 $qq4 = mysqli_query($conexion, $cuaG) or die('Err0: '. mysqli_error($conexion));
	 		   	 $r = mysqli_fetch_object($qq4);
	   		  	 $tot = $r->cuartaG + $n->numc;
  		      	 $actprom4 = "UPDATE calificados SET cuartaG = '$tot' WHERE inversionista_id_inver = '$prom1'";
	      	  	 $qu1 = mysqli_query($conexion, $actprom4) or die('Err0: '. mysqli_error($conexion));
	      	  	 
	      	  	 while($res4 = mysqli_fetch_object($cons4))
		  	     {
	 			  $prom5 = $res4->id_inver;
				  de5endelante($prom1, $prom2, $prom3, $prom4, $prom5);			 
		  	       }
		         }          
		       }        
		     }
	       }
         }
       }
	 }
   }	    

   limpiacal();
   revisa_cal(1);
   echo "TODO SALIO BIEN DAVID";
 ?>