<?php
    
    session_start();
    include("../../lib/conexion.php");    
    require "../../lib/headers.php";
    //$cedula= $_GET['cedusu'];      

   $cedula=$_POST['cedula'];
   $opc=$_POST['opc'];  
   $qn=$_POST['qn'];  
   //En $peri realizo el calculo del periodo segun la asignacion de quincena
 $peri= $opc*2-$qn%2;
 //Imprimo una cadena con formato asignado, en este caso de un valor numerico a char para poder comparar mis valores en la bd
 $pers=sprintf("%'.03d",$peri); 
 $fecha=date("m");
 echo "$fecha";



 echo "<br><div align='center'>$cedula <br> Mes:$opc <br> Quincena:$qn <br> Período:$pers </div>";

$html='    
 

<!DOCTYPE html>
<html lang="es">
<head>
<style type="text/css">
    @import "/sigespr_2017/menu.css";
    @import "/sigespr_2017/estilos.css";
    @import "/sigespr_2017/fonts.css";
</style>

</head>

';
 
 $html.='

 
<!-- TABLA PERIODO-->
  
<body>
  <div id="body"> 
    <div id="cuerpo4">

  <form name="html" method="POST" action="pdf.php" onsubmit= "submit()" > 
   
   <div align="center">
     <br><br>
     <b> Reporte General de Pago </b>
     <br><br>
     <table align="center" border="0" style="text-align: center;">
          <tr>
            <th>
              <b>Código de nomina:&nbsp;</b>
            </th>
              <th>
                <b>Período:&nbsp;</b>
              </th>

              <th>
                <b>Fecha 1:&nbsp;</b>
              </th>
              
              <th>
                <b>Fecha 2:&nbsp;</b>
              </th>  
                               
        </tr>';


 $query= "SELECT t1.codperi, t1.fecdesper, t1.fechasper, t2.codnom, t3.desnom FROM sno_hperiodo AS t1 INNER JOIN sno_hpersonalnomina AS t2 ON (t1.codemp=t2.codemp AND t1.codnom=t2.codnom AND t1.codperi=t2.codperi AND t1.anocur=t2.anocur) INNER JOIN sno_hnomina AS t3 ON (t3.codnom=t2.codnom)  WHERE t2.codper LIKE '%".$cedula."%' AND t1.codperi LIKE '%" . $pers . "' ";

   $result = mysql_query($query,$conexion2);
            if(!$result){
                echo "Error:".mysql_error($result);                
            } 
            else{   
              if(mysql_num_rows($result)){
                  /*if($codpers>=$codperi){
                      
                    }*/
                    $registros=mysql_num_rows($result);
                    $cont=0;
                                      
                      while($fila=mysql_fetch_array($result)){   
                        $cont++;    
                        $codperi=$fila['codperi'];         
                        $fecdesper=$fila['fecdesper'];
                        $fechasper=$fila['fechasper'];
                        $codnom=$fila['codnom'];
                        $desnom=$fila['desnom'];
                        $cedper=$fila['cedper'];                        
                        $nomper=$fila['nomper'];
                        $apeper=$fila['apeper'];
                        $desuniadm=$fila['desuniadm'];
                        $fecingper=$fila['fecingper'];                             

                        if($registros==$cont){
                      
$html.='
                        <tr>
                         <td>
                            '.$codnom.'
                        </td>

                         <td>
                            '.$codperi.'
                        </td>

                         <td>
                            '.$fecdesper.'
                        </td>

                         <td>
                            '.$fechasper.'
                        </td> 

                        </tr>

                        <tr>
                          <th colspan="4" bgcolor="#848484">
                              '.$desnom.'
                          </th>                       
                        </tr>
             </table> 
                         
';


            }  //echo "<input type='text'  value=" . $nomper . " disabled >"; 
          }
       }
     } 

echo "$html ";

$html.='
<!-- DATOS DEL FUNCIONARIO-->
<br>

        <table align="center" border="1" style="text-align: center;">
          <tr>
             <th>
                <b>Cédula:&nbsp;</b> 
             </th>

            <th>
              <b>Nombres:&nbsp;</b>
            </th>

            <th>
              <b>Apellidos:&nbsp;</b>
            </th>

            <th>
              <b>Cargo:&nbsp;</b>
            </th>
            
            <th>
              <b>Unidad Adscripción:&nbsp;</b>
            </th>  

            <th>
              <b> Fecha de ingreso:&nbsp;</b>
            </th>                        
        </tr>
';

 

$query2="SELECT t1.cedper, t1.nomper, t1.apeper, t2.fecingper, t2.codnom, t3.descar, t4.desuniadm FROM sno_personal AS t1 INNER JOIN sno_hpersonalnomina AS t2 ON (t1.codemp=t2.codemp AND t1.codper=t2.codper) INNER JOIN sno_hcargo AS t3 ON(t2.codemp=t3.codnom AND t2.codnom=t3.codnom AND t2.codcar=t3.codcar AND t2.anocur=t3.anocur AND t2.codperi=t3.codperi) INNER JOIN sno_hunidadadmin AS t4 ON (t2.codemp=t4.codemp AND t2.codnom=t4.codnom AND t2.anocur=t4.anocur AND t2.codperi=t4.codperi AND t2.minorguniadm=t4.minorguniadm AND t2.ofiuniadm=t4.ofiuniadm AND t2.uniuniadm=t4.uniuniadm AND t2.depuniadm=t4.depuniadm AND t2.prouniadm=t4.prouniadm) WHERE t1.codper LIKE '%" . $cedula . "%'";

  $result2 = mysql_query($query2,$conexion2);
        //echo $query;         
            
            if(!$result2){
                echo "Error:".mysql_error($result2);                
            } 
            else{
                if(mysql_num_rows($result2)){
                    $registros=mysql_num_rows($result2);
                    $cont=0;
                    
                    while($fila=mysql_fetch_array($result2)){   
                        $cont++;                                                
                        $cedper=$fila['cedper'];
                        $nomper=$fila['nomper'];
                        $apeper=$fila['apeper'];
                        $fecingper=$fila['fecingper'];
                        $codnom=$fila['codnom'];
                        $descar=$fila['descar'];
                        $desuniadm=$fila['desuniadm'];
                    
                        
                        if($registros==$cont){  

                        $html.='
                        <tr>

                        <td>
                            '.$cedper.'
                        </td>

                         <td>
                            '.$nomper.'
                        </td>

                         <td>
                            '.$apeper.'
                        </td>

                         <td>
                            '.$descar.'
                        </td> 

                        <td >
                            '.$desuniadm.'
                        </td>                       

                        <td >
                            '.$fecingper.'
                        </td>
                        </tr>
                    </table>  
                    
                    ';                      

        }
      } 
    }
 }

$html.='
<!-- TABLA VALORES DE CONCEPTOS-->
 <br> 
        <table align="center" border="0" style="text-align: center;">
          <tr bgcolor="#848484">
             <th>
                <b>Código:&nbsp;</b> 
             </th>
            
            <th>
              <b>Concepto:&nbsp;</b>
            </th>

            <th>
              <b>Asignación:&nbsp;</b>
            </th>

            <th>
              <b>Deducción:&nbsp;</b>
            </th>
             
            <th>
              <b>Neto:&nbsp;</b>
            </th> 
        </tr>';

$query3="SELECT t1.codnom, t1.codper, t1.asires, t1.dedres, t1.monnetres, t1.codperi, t2.tipsal, t2.valsal,t3.codconc,t3.nomcon FROM sno_hresumen AS t1 INNER JOIN sno_hsalida AS t2 ON ( t1.codper = t2.codper AND t1.codnom = t2.codnom AND t1.codperi = t2.codperi AND t1.anocur = t2.anocur ) INNER JOIN sno_hconcepto AS t3 ON (t2.codconc=t3.codconc AND t2.codnom=t3.codnom AND t2.anocur=t3.anocur AND t2.codperi=t3.codperi) WHERE t1.codper LIKE '%" .$cedula."%' AND t1.codperi LIKE '%".$pers."%' AND ( tipsal = 'A' OR tipsal = 'D' OR tipsal = 'P1' ) AND valsal <>0";


  $result3 = mysql_query($query3,$conexion2);
        //echo $query;         
            
            if(!$result3){
                echo "Error:".mysql_error($result3);                
            } 
            else{
                if(mysql_num_rows($result3)){
                    
                    
                    while($fila=mysql_fetch_array($result3)){   
                        $cont++;
                                                                  
                        $codnom=$fila['codnom'];
                        $codper=$fila['codper'];
                        $asires=$fila['asires'];
                        $dedres=$fila['dedres'];
                        $monnetres=$fila['monnetres'];
                        $codperi=$fila['codperi'];
                        $codconc=$fila['codconc'];
                        $tipsal=$fila['tipsal'];
                        $valsal=$fila['valsal'];  
                        $nomcon=$fila['nomcon'];

              $html.='
                    <tr>
                         <td>
                            '.$codconc.'
                        </td>

                        <td>
                            '.$nomcon.'
                        </td>

                          <td>
                        ';
                        if($qn==2){
                          if($tipsal=='A'){
                            $asigm=$valsal*2;
                            $asig= number_format($asigm, 2, ',', '.');//Formato de valor asignacion
                            $html.=$asig;
                            $sum+=$valsal*2;//sumador de asignaciones
                          }  
                        }
                        else{
                            if ($qn==1) {
                              if($tipsal=='A'){
                                $asig= number_format($valsal, 2, ',', '.');//Formato de valor asignacion
                                $html.=$asig;
                                $sum+=$valsal;//sumador de asignaciones
                              }  
                            }
                        }  

                      $html.='
                      </td>

                      <td>';
                        if($qn==2){
                          if($tipsal=='D' OR $tipsal=='P1'){
                                $n=($valsal)*-2;
                                $de= number_format($n, 2, ',', '.');//Formato valor deducciones
                                 $html.=$de;
                                $sumd+=$n;//sumador de deducciones
                              }
                        }
                        else{
                          if($qn==1){
                            if($tipsal=='D' OR $tipsal=='P1'){
                              $n=($valsal)*-1;
                              $de= number_format($n, 2, ',', '.');//Formato valor deducciones
                              $html.=$de;
                              $sumd+=$n;//sumador de deducciones
                            }
                          }
                        }
                        
                        $html.='      
                       </td> 
                       
                      </tr>';

 
    }
      $html.='
                      <tr bgcolor="#848484">
                          <th colspan="2">
                              <b>Totales</b>
                          </th>
                         
                          <th>';

                              //Total de Asignaciones
                             $sum++;//sumador de asignaciones
                             $a=number_format($sum, 2, ',', '.');
                             $html.=$a;
                        $html.='                    
                          </th>

                          <th>';
                              $d=number_format($sumd, 2, ',', '.');
                              $html.=$d;
                          $html.='    
                          </th>
                          <th>';
                              if ($qn==1) {
                                $total=number_format($monnetres, 2, ',', '.');
                                $html.=$total;
                              }
                              else{
                                  if ($qn==2){
                                    $monto=$monnetres*2;
                                    $total=number_format($monto, 2, ',', '.');
                                    $html.=$total;
                                  }
                              }
                              
                           $html.='                        
                          </th>
                        </tr>  
              </table> 
              <br>
              <br>

            <div>
            <div>
           </body>  
          
                  ';

   }
}   

echo "$html";

echo"<div align='center'> <input type='image' src='/sigespr_2017/grafico/img/pdf.png'/>";
echo "<a href='/sigespr_2017/inicio.php?cedper=$cedula'><i class='icon-home'></i> Menú</a> </div><br><br>"

?>
  
<input type="hidden" name="html" value='<?php echo "$html" ?>' >
</div>
</form>

