<?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=$sesion->id;
   extract($_POST);
   $cont=0;
	
           $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;}
   
if($continua!=1){


	$qPP = "SELECT idreg, inversionista.nombre as elnombre, appat, apmat, `calificados`,`total`,`apagar`,`fechap`, impuestos, pagado, estampa, cd, banco.nombre as elbanco, nocuenta, inversionista.id_inver as inver, status, tel1, tel2, ciudad, rfc, _real, tel, vip, depo FROM `regs2` join inversionista on 
    inversionista.id_inver=regs2.id_inver left join teldist on teldist.id_inver=inversionista.id_inver left join banco on banco.idbanco=inversionista.banco_idbanco WHERE fechap between '$elano-$elmes-01' and '$elano-$elmes-31' and (`status`=0 or status=8 or status=10) order by total desc";}
    else{
    $qPP = "SELECT idreg, inversionista.nombre as elnombre, appat, apmat, `calificados`,`total`,`apagar`,`fechap`, impuestos, pagado, estampa, cd, banco.nombre as elbanco, nocuenta, inversionista.id_inver as inver, status, tel1, tel2, ciudad, rfc,  _real, tel, vip, depo FROM `regs2` join inversionista on 
    inversionista.id_inver=regs2.id_inver left join teldist on teldist.id_inver=inversionista.id_inver left join banco on banco.idbanco=inversionista.banco_idbanco WHERE fechap between '$elano-$elmes-01' and '$elano-$elmes-31' and (`status`=0 or status=8 or status=10) ORDER BY $listacd, $listacd2 $listaor";
    
    
    }
	

 $totalin = 0;
  $totalin2 = 0;

$rPP = mysqli_query($conexion, $qPP) or die ('ErrPP: '. mysqli_error($conexion));
  echo"<center><br><br><form name=\"Agrega2\" action=\"pagarregalias.php\" method=\"post\">
  Ordenar por:<select name = \"listacd\"><option value=\"idreg\">ID Regalia</option>
  <option value=\"cd\">Centro Pago</option>
  <option value=\"fechap\">Fecha Generado</option>
  
  <option value=\"elnombre\">Nombre</option>
  <option value=\"appat\">Apellido P.</option>
  <option value=\"apmat\">Apellido M.</option>
  <option value=\"pagado\">Cantidad Pagada</option>
  <option value=\"fechaf\">Fecha Pago</option>
  <option value=\"elbanco\">Banco</option>
  <option value=\"estampa\">Ingreso</option></select>
  Y despues:<select name = \"listacd2\"><option value=\"idreg\">ID Regalia</option>
  <option value=\"cd\">Centro Pago</option>
  <option value=\"fechap\">Fecha Generado</option>
  
  <option value=\"elnombre\">Nombre</option>
  <option value=\"appat\">Apellido P.</option>
  <option value=\"apmat\">Apellido M.</option>
  <option value=\"pagado\">Cantidad Pagada</option>
  <option value=\"fechaf\">Fecha Pago</option>
  <option value=\"elbanco\">Banco</option><option value=\"estampa\">Ingreso</option></select>
  Orden: <select name = \"listaor\"><option value=\"ASC\">Ascendente</option><option value=\"DESC\">Descendente</option></select>
  <input type=hidden name=continua value=\"1\" />
  <input type=hidden name=fecha value=\"$lafecha\" />
  <input type=\"submit\" name=\"lista2\"  value=\"Continuar\" />
  
  
  
  </form>";

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




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

	        <table border=\"1\" align=\"center\">
	 ";
	
	
echo " <th></th><th>ID Reg</th><th>ID Dist</th><th>RFC</th><th></th><th></th><th></th><th>Nombre</th><th>Ap Paterno</th><th>Ap Materno</th><th>PAGO</th><th>VIP</th><th>estatus</th><th>Fecha</th><th>Calificados</th><th>Generado</th>
<th>A pagar</th><th>Impuestos</th><th>A Depositar</th><th>Ingreso</th><th>CD</th><th>Banco</th><th>Cuenta</th>
<th>Tel1</th><th>Tel2</th><th>Tel3</th><th>ciudad</th>";

while (($objetoPP=mysqli_fetch_object($rPP)) )
{$totalin+=$objetoPP->apagar;$totalin2+=$objetoPP->_real; $stat=$objetoPP->status;
if($stat==0){$elstat="Por Pagar";}   if($stat==8){$elstat="En transito";}  if($stat==10){$elstat="En ruta";} 
$apa=$objetoPP->apagar;
$imp=round($apa*.07);
$dep=round($apa*.93);

	echo "<tr>
			 <td>$cont</td><td>$objetoPP->idreg</td><td>$objetoPP->inver</td><td>$objetoPP->rfc</td>";
 
 if($stat==0){

			echo"<td><form name=\"pagar\" action=\"mandartransito.php\" method=\"post\">
			<input type=hidden name=lareg value=$objetoPP->idreg>
			<input type=hidden name=elval value='8'>
			
			
								 <input type=\"submit\" value=\"TRANSITO\" name=\"pago\"></form>";

			echo"</td><td><form name=\"retener\" action=\"retener_regs.php\" method=\"post\">
			<input type=hidden name=id_regalia value=$objetoPP->idreg>
			
			
								 <input type=\"submit\" value=\"Retener\" name=\"reten\"></form>";

			echo"</td><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>";
 }else{echo"<td></td><td></td><td></td>";}
               
 
 
 echo"<td>$objetoPP->elnombre</td><td>$objetoPP->appat</td><td>$objetoPP->apmat</td><td>$objetoPP->depo</td><td>$objetoPP->vip</td><td>$elstat</td><td>$objetoPP->fechap</td><td>$objetoPP->calificados</td>
			 <td>$objetoPP->_real</td><td>$apa</td><td>$imp</td><td>$dep</td><td>$objetoPP->estampa</td><td>$objetoPP->cd</td>
             <td>$objetoPP->elbanco</td><td>$objetoPP->nocuenta</td>";$cont++;
             
			  echo" <td>$objetoPP->tel1</td> <td>$objetoPP->tel2</td><td>$objetoPP->tel</td> <td>$objetoPP->ciudad</td>
	
		  </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>"; /*/
?>