<?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 reporteregs()
  {
    $sel = "SELECT id_inver, nombre, appat, apmat, primeraG, segundaG, terceraG, cuartaG, quintaG, (primeraG*15.20) as priG, (segundaG*15.20) as segG, (terceraG*15.20) as terG, (cuartaG*15.20) as cuaG, (quintaG*15.20) as quiG, ((primeraG+segundaG+terceraG+cuartaG+quintaG)*15.20) as total FROM inversionista JOIN calificados ON inversionista.id_inver = calificados.inversionista_id_inver WHERE inversionista.califico='SI' AND ((primeraG+segundaG+terceraG+cuartaG+quintaG)*15.20) > '0' ORDER BY total DESC";
    $cons = mysqli_query($conexion, $sel) or die('Err0: '. mysqli_error($conexion));
	echo "<center><label style='color:#5C3C12; font-size:28px; font-weight:bold'>Regal&iacute;as del mes.</label></center>";        
    if (mysqli_affected_rows($conexion))
    {   
	echo "<br /><table border='1' align='center' width='700'>";    
    echo "<tr align='center'> <td>No. <td>Nombre <td>G1 <td>G2 <td>G3 <td>G4 <td>G5 <td>Total <td>Regs. a Recibir</tr>";
    $con = 1;

    while($res = mysqli_fetch_object($cons))
    {
  	  $idin = $res->id_inver;
      $g1 = $res->priG;
      $g2 = $res->segG;
      $g3 = $res->terG;
      $g4 = $res->cuaG;
      $g5 = $res->quiG;
  	  $regals = $res->total;
      
  	  if ($res->primeraG > 0)
  	  {
  	    if($res->primeraG < 3)
  	  	 $regarec = $g1;
  	  	else{
  	  		if($res->primeraG < 6)
  	  		 $regarec = $g1 + $g2;
  	  		else{
  	  			if($res->primeraG < 9)
  	  		     $regarec = $g1 + $g2 + $g3;
  	  		    else{
  	  		      if($res->primeraG < 10)
  	  		        $regarec = $g1 + $g2 + $g3 + $g4;
  	  		      else 
  	  		        $regarec = $g1 + $g2 + $g3 + $g4 +$g5;
  	  		        }
  	  		     }
  	  	    }  		
  	   }
  	  
  	  $g1 = number_format($g1,2);
  	  $g2 = number_format($g2,2);
  	  $g3 = number_format($g3,2);
  	  $g4 = number_format($g4,2);
  	  $g5 = number_format($g5,2);
  	  
  	  
  	  $regarec = number_format($regarec,2);
  	  if ($regals != 0) 
  	  {
  	  	$regals = number_format($regals,2);
  	  	echo "<tr align='right'> <td>$con <td align='left'>$res->nombre $res->appat $res->apmat <td>$g1 <td>$g2 <td>$g3 <td>$g4 <td>$g5 <td>$regals <td style='font-weight: bold'>$$regarec </tr>";     
  	    $con++;
  	  }
      
     } 
     echo "</table>";
    }
    else 
    echo "<br />No se han registrado regalias hasta el momento"; 	 
   }
   
   reporteregs();
?>