<?php
session_start();

   include_once("../scripts/connect.php");
   $conexion=conecta_default();
   
   include_once("../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: ../index.php?err=NoSesion");

    }/*no hay sesion*/

    else{

       $dato=explode("|",$_SESSION['usuario']);
       $sesion->setDatos($dato[0],$dato[1],$dato[2]);
       $sesion->setSesion();

    }
    //fin validar session

   $id_cd=1;
$id_cd=$sesion->id;
   extract($_POST);
	
           $lafecha=$_POST['fecha'];        
     $qp = "select  month('$lafecha')+1, year('$lafecha')";
$cp = mysqli_query($conexion, $qp) or die ('Error (Registro No Encontrado): ' . mysqli_error($conexion));
                 while($rDG = mysqli_fetch_row($cp)) 
                     {
                $elmes = $rDG[0];
                $elano = $rDG[1];

               };
                   
         if($elmes==13){$elmes=1; $elano=$elano+1;}



	$qPP = "SELECT idreg, inversionista.nombre as elnombre, appat, apmat, `calificados`,`total`,`apagar`,`fechap`, impuestos, pagado, estampa, cd, banco.nombre as elbanco, nocuenta, status, cd, inversionista.id_inver as cliente, rfc, tel1, tel2, tel, ciudad FROM `regs2` join inversionista on 
    inversionista.id_inver=regs2.id_inver left join banco on banco.idbanco=inversionista.banco_idbanco left join teldist on inversionista.id_inver=teldist.id_inver WHERE fechap between '$elano-$elmes-01' and '$elano-$elmes-31' and `status`=0 and apagar<=500";
   
	

 $totalin = 0;
  $totalin2 = 0;

$rPP = mysqli_query($conexion, $qPP) or die ('ErrPP: '. mysqli_error($conexion));
  

   
if(!mysqli_affected_rows($conexion))
{
	echo "<center>No hay Regalias por el momento.</center><br>";
    exit;
}




echo "<html>
			<body>
			
			<br>
			<center><font size=\"+3\" color=\"#594106\"><b>REGALIAS POR PAGAR</b></font>
			
			<br><br>

	        <table cellpadding=\"1\" align=\"center\">
	 ";
	
	
echo " <tr bgcolor=\"#594106\"><th><font color=\"#FFFFFF\">ID</font></th><th><font color=\"#FFFFFF\">CD</font></th><th><font color=\"#FFFFFF\">RFC</font></th><th><font color=\"#FFFFFF\">Nombre</font></th><th><font color=\"#FFFFFF\">Id Dist</font></th>
<th><font color=\"#FFFFFF\">Del mes</font></th><th><font color=\"#FFFFFF\">Acumulado</font></th><th><font color=\"#FFFFFF\">Banco</font></th><th><font color=\"#FFFFFF\">Nvo</font></th><th><font color=\"#FFFFFF\">Tel1</font></th><th><font color=\"#FFFFFF\">Tel2</font></th><th><font color=\"#FFFFFF\">Ciudad</font></th><th></th></tr>";

while (($objetoPP=mysqli_fetch_object($rPP)) )
{$totalin+=$objetoPP->apagar; $stat=$objetoPP->status;$inver=$objetoPP->cliente;
$qp = "select  sum(apagar) as regas from regs2 where id_inver='$inver' and status='0' and fechap>='2016-04-01'";
$cp = mysqli_query($conexion, $qp) or die ('Error (Registro No Encontrado): ' . mysqli_error($conexion));

	$rp = mysqli_fetch_object($cp);	
		$totreg = $rp->regas;


	echo "<tr bgcolor=\"#ece3bb\">
			 <td>$objetoPP->idreg</td><td>$objetoPP->cd</td><td>$objetoPP->rfc</td><td>$objetoPP->elnombre $objetoPP->appat $objetoPP->apmat</td><td> $inver</td>
			 <td>$objetoPP->apagar</td>";
			 if($totreg>=500){echo"<td>><b>$totreg</b></td>";}else{echo"<td>$totreg</td>";}
			 
			 
			 echo"";
             
			
       
	echo"<td>$objetoPP->elbanco</td><td>$objetoPP->tel</td><td>$objetoPP->tel1</td><td>$objetoPP->tel2</td><td>$objetoPP->ciudad</td>
	<td><form name=\"pagar\" action=\"agrupar_regs3.php\" method=\"post\">
			<input type=hidden name=cliente value='$inver'>
			<input type=\"submit\" value=\"VER MAS\" name=\"pago\"></form></td>
           
                 
	
		  </tr> 
		 ";
   
}
echo "</table><br>
El total de regalias por pagar $ $totalin<br>"; 

/*echo"$id_cd";
	
	$qPP = "SELECT idreg, nombre, appat, apmat, `calificados`,`total`,`apagar`,`fechap` FROM `regs2` join inversionista on inversionista.id_inver=regs2.id_inver WHERE `status`=0 and cd='".$id_cd."'";

 $totalin = 0;
  $totalin2 = 0;

$rPP = mysqli_query($conexion, $qPP) or die ('ErrPP: '. mysqli_error($conexion));


   
if(!mysqli_affected_rows($conexion))
{
	echo "<center>No hay Regalias por el momento (Inscripcion).</center><br>";
    exit;
}




echo "<html>
			<body>
			
			<br>
			<center><b>REGALIAS POR PAGAR</b>
			
			<br>

	        <table border=\"1\" align=\"center\">
	 ";
	
	
echo " <th>ID</th><th>Fecha</th><th>Nombre</th><th>Calificados</th><th>Generado</th>
<th>A pagar</th>";

while (($objetoPP=mysqli_fetch_object($rPP)) )
{$totalin+=$objetoPP->apagar; 
	echo "<tr>
			 <td>$objetoPP->idreg</td><td>$objetoPP->fechap</td><td>$objetoPP->nombre $objetoPP->appat $objetoPP->apmat</td><td>$objetoPP->calificados</td>
			 <td>$objetoPP->total</td><td>$objetoPP->apagar</td>";
			
			echo"<td><form name=\"pagar\" action=\"detalle_regs.php\" method=\"post\">
			<input type=hidden name=id_regalia value=$objetoPP->idreg>
			<input type=\"submit\" value=\"VER MAS\" name=\"pago\"></form></td>";
            echo"<td><form name=\"pagar\" action=\"crearvoucher.php\" method=\"post\">
            <input type=hidden name=id_regalia value=$objetoPP->idreg>
            <input type=\"submit\" value=\"CREAR VOUCHER\" name=\"pago\"></form></td>";
            echo"<td><form name=\"pagar\" action=\"reciboregs.php\" method=\"post\">
            <input type=hidden name=id_regalia value=$objetoPP->idreg>
            <input type=\"submit\" value=\"PAGO EFECTIVO\" name=\"pago\"></form></td>";

			echo"</tr> ";
   
}
echo "<br>
El total de regalias por pagar $ $totalin<br>"; 
   
 /*  $qPP2 = "SELECT nombre, appat, apmat, `calificados`,`primera`,`segunda`,`tercera`,`cuarta`,`total`,`apagar`,`fechap` FROM `regs2` join inversionista on inversionista.id_inver=regs2.id_inver WHERE `status`=0 and cd='".$id_cd."'";



$rPP2 = mysqli_query($conexion, $qPP2) or die ('ErrPP: '. mysqli_error($conexion));

   			echo"
			
			
			

	        <table border=\"1\" align=\"center\">
	 ";
	
	
echo " <th>Nombre</th><th>Calificados</th><th>1ra Gen</th><th>2da Gen</th><th>3ra Gen</th><th>4ta Gen</th><th>Tipo de pago</th><th>Observaciones</th>";

while (($objetoPP=mysqli_fetch_object($rPP2)) )
{$totalin2+=$objetoPP->pagar;
	echo "<tr>
			 <td>$objetoPP->rfc</td><td>$ $objetoPP->pagar</td><td>$objetoPP->fechap</td><td>$objetoPP->tipop</td>  <td>$objetoPP->observaciones</td> <td>
			
			</td>
                 
	
		  </tr> 
		 ";
   
}
echo "<br><b>REGALIAS PAGADAS</b><br><br>
El total de regalias pagadas $ $totalin2<br>"; /*/
?>