<?php
  session_start();
  $ruta="../../../";
  include_once($ruta."class/dominio.php");
  $dominio=new dominio;
  include_once($ruta."class/vadmejecutivo.php");
  $vadmejecutivo=new vadmejecutivo;
  include_once($ruta."class/individual.php");
  $individual=new individual;
  include_once($ruta."class/ciclo.php");
  $ciclo=new ciclo;
  include_once($ruta."class/individualcredito.php");
  $individualcredito=new individualcredito;
  include_once($ruta."class/credito.php");
  $credito=new credito;
  include_once($ruta."class/creditodetalle.php");
  $creditodetalle=new creditodetalle;
  include_once($ruta."class/personagrupo.php");
  $personagrupo=new personagrupo;
  include_once($ruta."class/persona.php");
  $persona=new persona;
  include_once($ruta."class/tipobanca.php");
  $tipobanca=new tipobanca;
  include_once($ruta."class/miempresa.php");
  $miempresa=new miempresa;
  include_once($ruta."class/usuario.php");
  $usuario=new usuario;
  include_once($ruta."funciones/funciones.php");
  include($ruta."recursos/qr/qrlib.php");
  require_once '../../../recursos/pdf/mpdf/vendor/autoload.php';

  /******************    SEGURIDAD *************/
  extract($_GET);
  //********* SEGURIDAD GET *************/
  $valor=dcUrl($lblcode);//$valor=id
  $valor=dcUrl($lblcode);
  $cred=$individualcredito->muestra($valor);
  $gr=$individual->muestra($cred['idindividual']);
  $per=$persona->muestra($gr['idpersona']);
  $rutaImg=$ruta."imagenes/logo.png";
  $demp=$miempresa->muestra(1);
  $dus=$usuario->muestra($cred["usuariocreacion"]);
  $tba=$tipobanca->muestra($cred['idtipobanca']);
  $eje=$vadmejecutivo->muestra($gr['idadmejecutivo']);
  $ddom=$dominio->muestra($cred['frecuenciapago']);
  $cuotacobrar=$cred['cuota']+1;
  $canTCred=$credito->mostrarPrimero("idgrupocredito=".$valor." and tipocredito=1");
  $fechaActual=date('Y-m-d');
  $horaActual=date("H:i:s");

  $defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
  $fontDirs = $defaultConfig['fontDir'];

  $defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
  $fontData = $defaultFontConfig['fontdata'];

  $mpdf = new \Mpdf\Mpdf([
      'fontDir' => array_merge($fontDirs, [
            $ruta.'recursos/font/titilium',
        ]),
        'fontdata' => $fontData + [
            'frutiger' => [
                'R' => 'TitilliumWeb-Regular.ttf',
                'I' => 'TitilliumWeb-Italic.ttf',
            ]
        ],
        'default_font' => 'frutiger',
        'mode' => 'utf-8',
        'margin_header' => 5, 
        'margin_top' => 30, 
        'orientation' => ''
    ]
  );


$header='
  <table style="width:100%; font-size:12px; text-align:center;"  align="center">
        <tr >
            <td style="width:30%;" class="letras">
              <center>
                <img style="width:180px;" src="'.$rutaImg.'" ><br>
              </center>
            </td>
            <td style="width:30%;">
              <table class="" align="center">
              <tr>
                <td>
                  <div class="titSUB">C O M P R O B A N T E</div>
                  <div class="titPRI"><h3>D E S E M B O L S O</h3></div>
                </td>
              </tr>
              </table>
            </td>
            <td style="width:30%;">
              <table border="0">
                <tr>
                  <td>
                    <table class="letras" cellpadding="2">
                      <tr>
                        <td align="right">
                          FECHA:
                        </td>
                        <td align="left">
                          <b>'.$fechaActual.'</b>
                        </td>
                      </tr>
                      <tr>
                        <td align="right">
                          HORA:
                        </td>
                        <td align="left">
                          <b>'.$horaActual.'</b>
                        </td>
                      </tr>
                      <tr>
                        <td align="right">
                          Usuario:
                        </td>
                        <td align="left">
                          <b>'.$dus['usuario'].'</b>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
              </table>                     
            </td>
      </tr>
  </table>
  ';

  $html = '
    <table  style="width:100%; font-size:12px;">
       <tr>  
        <td colspan="2"><b>Socio(a): </b>'.$per['nombre'].' '.$per['paterno'].' '.$per['materno'].'</td>             
          <td><b>Carnet: </b>'.$per['carnet'].' '.$per['expedido'].'</td> 
           <td><b>Fecha Desembolso: </b>'.$cred['fechadesembolso'].'</td>    
        </tr>
        <tr>
          
          <td width="25%"><b>Código : </b>'.$cred['idindividualcredito'].'</td>
          <td width="23%"><b>Cuotas: </b>'.$cred['nrocuotas'].'</td>
          <td width="27%"><b>Ciclo: </b> '.$gr['idciclo'].'</td>  
          <td width="25%"><b>Fecha Reunión: </b>'.$cred['fechaproximo'].'</td> 
          
        </tr>
        <tr>
          <td><b>Reunión: </b>'.$gr['lugarreunion'].'</td>
          <td><b>Lapso : </b>'.$ddom['nombre'].'</td>
          <td><b>Tipo Banca: </b>'.$tba['nombre'].'</td> 
           <td><b>Fecha Cierre: </b>'.$cred['fechacierre'].'</td>
        </tr>
      </table><br>
  <table style="width:100%; font-size:12px; text-align:center;"  align="center">
        <tr >
            <td style="width:20%;" class="letras">
              &nbsp;
            </td>
            <td style="width:60%;">
              <table class="titREP" align="center" style="width:45%;">
              <tr>
                <td>
                  <div class="titPRI"><h3>DESEMBOLSO DE CRÉDITO</h3></div>
                </td>
              </tr>
              </table>
            </td>
            <td style="width:20%;">
              &nbsp;                    
            </td>
      </tr>
  </table>
    <table class="tablaLIST3">
      <thead>
        <tr>
          <th style="width:30%;">MONTO <br>APROBADO</th>
          <th style="width:30%;">MONTO <br>DESEMBOLSADO</th>
          <th style="width:40%;">FIRMA DE <br>CONFORMIDAD</th>
        </tr>
      </thead>
      <tbody>
      ';
      $nro=1;
      $totalPago=0;
      $totalCI=0;
      $totalSaldoCI=0;
      $totalinteresCI=0;
      $saldoAnt=0;
      foreach($credito->mostrarTodo("idgrupocredito=".$valor." and tipocredito=1") as $f) {
        //$dper=$persona->muestra($f['idpersona']);
        $cuotaPago=$f['cuotaspagadas']+1;
        $tba=$tipobanca->muestra($f['idtipobanca']);
        $totalPago=$totalPago+$f['montoOT'];

        $html = $html.'
          <tr>
            <td style="text-align:center;">'.number_format($f['montoOT'], 2, '.', '').'</td>
            <td style="text-align:center;">_____________________</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
        ';
        $nro++;
        }
      $html = $html.'
      </tbody>
    </table>
<br>
';
if ($canTCred['fgarantia']>0) 
{
 $html = $html.' 
        <table style="width:100%; font-size:12px; text-align:center;"  align="center">
        <tr >
            <td style="width:20%;" class="letras">
              &nbsp;
            </td>
            <td style="width:60%;">
              <table class="titREP" align="center" style="width:45%;">
              <tr>
                <td >
                  <div ><h3>AHORRO INICIAL</h3></div>
                </td>
              </tr>
              </table>
            </td>
            <td style="width:20%;">
              &nbsp;                    
            </td>
      </tr>
  </table>
   <table class="tablaLIST3">
      <thead>
        <tr>
          <th style="width:30%;">MONTO <br>DESEMBOLSADO</th>
          <th style="width:30%;">AHORRO  <br>INICIAL</th>
          <th style="width:40%;">FIRMA DE <br>CONFORMIDAD</th>
        </tr>
      </thead>
      <tbody>
      ';
      $nro=1;
      $totalPago=0;
      $totalFG=0;
      $totalSaldoCI=0;
      $totalinteresCI=0;
      $saldoAnt=0;
      foreach($credito->mostrarTodo("idgrupocredito=".$valor." and tipocredito=1") as $f) {
        //$dper=$persona->muestra($f['idpersona']);
        $fgarantia=$f['montoOT']*($f['fgarantia']/100);
        $totalPago=$totalPago+$f['montoOT'];
        $totalFG=$totalFG+$fgarantia;

        $html = $html.'
          <tr>
            <td style="text-align:center;">'.number_format($f['montoOT'], 2, '.', '').'</td>
            <td style="text-align:center;">'.number_format($fgarantia, 2, '.', '').'</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
        ';
        $nro++;
        }
      $html = $html.'
      </tbody>
      <tfoot>
        <tr>
          <td colspan="3"><center style="font-size:14px;">
    Acepto de forma voluntaria la entrega del Ahorro Inicial a PRO-AYUDA.
  </center></td>
        </tr>
      </tfoot>
    </table>
    <br>
 ';
}
if ($canTCred['frs']>0) 
{
$html = $html.'
  
  <table style="width:100%; font-size:12px; text-align:center;"  align="center">
        <tr >
            <td style="width:20%;" class="letras">
              &nbsp;
            </td>
            <td style="width:60%;">
              <table class="titREP" align="center" style="width:45%;">
              <tr>
                <td>
                  <div class="titPRI"><h3>GASTOS ADMINISTRATIVOS</h3></div>
                </td>
              </tr>
              </table>
            </td>
            <td style="width:20%;">
              &nbsp;                    
            </td>
      </tr>
  </table>
   <table class="tablaLIST3">
      <thead>
        <tr>
          <th style="width:30%;">MONTO <br>DESEMBOLSADO</th>
          <th style="width:30%;">G.A.<br>'.$cred['gastosadmin'].'% </th>
          <th style="width:40%;">FIRMA DE <br>CONFORMIDAD</th>
        </tr>
      </thead>
      <tbody>
      ';
      $nro=1;
      $totalPago=0;
      $totalGA=0;
      $totalFRS=0;
      $totalinteresCI=0;
      $saldoAnt=0;
      foreach($credito->mostrarTodo("idgrupocredito=".$valor." and tipocredito=1") as $f) {
       // $dper=$persona->muestra($f['idpersona']);
        $totalPago=$totalPago+$f['montoOT'];
        $gadmin=$f['montoOT']*($f['gastosadmin']/100);
        $frs=$f['montoOT']*($f['frs']/100);//individual, no se cobra frs al desenbolsar

        $totalGA=$totalGA+$gadmin;
        $totalFRS=$totalFRS+$frs;

        $html = $html.'
          <tr>
            <td style="text-align:center;">'.number_format($f['montoOT'], 2, '.', '').'</td>
            <td style="text-align:center;">'.number_format($gadmin, 2, '.', '').'</td>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr>
        ';
        $nro++;
        }
      $html = $html.'
      </tbody>
      <tfoot>
        <tr>
          <td></td>
          <td style="text-align:center;">Total: '.number_format($totalFRS+$totalGA, 2, '.', '').'</td>
          <td></td>
        </tr>
      </tfoot>
    </table>
    <br>
    ';
  }
  $html = $html.'  
    <table style="width:100%; font-size:12px; text-align:center;">
      <tr>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td><br><br><br>______________________________<br>ASESOR(A) '.$eje['nombre']." ".$eje['paterno'].'</td>
        <td><br><br><br>______________________________<br>GERENTE</td>
      </tr>
    </table>
    
  ';
  //==============================================================
  //==============================================================
  //==============================================================

  //$mpdf->SetDisplayMode('fullpage');

  $stylesheet = file_get_contents($ruta.'recursos/css/elisyam-1.5.css');
  $mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no
  $mpdf->SetHTMLFooter('
  <table width="100%">
      <tr>
          <td width="33%"></td>
          <td width="33%" align="center"></td>
          <td width="33%" style="text-align: right;"></td>
      </tr>
  </table>');
  $mpdf->SetHeader($header,50);
  $mpdf->WriteHTML($html);
  $mpdf->Output();

?>