<?php

Yii::import("application.vendors.fpdf.*", true);

if (function_exists('mcrypt_encrypt')) {

    function RC4($key, $data) {
        return mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $data, MCRYPT_MODE_STREAM, '');
    }

} else {

    function RC4($key, $data) {
        static $last_key, $last_state;

        if ($key != $last_key) {
            $k = str_repeat($key, 256 / strlen($key) + 1);
            $state = range(0, 255);
            $j = 0;
            for ($i = 0; $i < 256; $i++) {
                $t = $state[$i];
                $j = ($j + $t + ord($k[$i])) % 256;
                $state[$i] = $state[$j];
                $state[$j] = $t;
            }
            $last_key = $key;
            $last_state = $state;
        } else
            $state = $last_state;

        $len = strlen($data);
        $a = 0;
        $b = 0;
        $out = '';
        for ($i = 0; $i < $len; $i++) {
            $a = ($a + 1) % 256;
            $t = $state[$a];
            $b = ($b + $t) % 256;
            $state[$a] = $state[$b];
            $state[$b] = $t;
            $k = $state[($state[$a] + $state[$b]) % 256];
            $out .= chr(ord($data[$i]) ^ $k);
        }
        return $out;
    }

}

class PDF extends FPDF {

    var $encrypted = false;  //whether document is protected
    var $Uvalue;             //U entry in pdf document
    var $Ovalue;             //O entry in pdf document
    var $Pvalue;             //P entry in pdf document
    var $enc_obj_id;         //encryption object id
    var $widths;
    var $aligns;

    function SetWidths($w) {
        //Set the array of column widths
        $this->widths = $w;
    }

    function SetAligns($a) {
        //Set the array of column alignments
        $this->aligns = $a;
    }

    function Row($data) {
        //Calculate the height of the row
        $nb = 0;
        for ($i = 0; $i < count($data); $i++)
            $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
        $h = 8 * $nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for ($i = 0; $i < count($data); $i++) {
            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'J';
            //Save the current position
            $x = $this->GetX();
            $y = $this->GetY();
            //Draw the border

            $this->Rect($x, $y, $w, $h);
            //Print the text


            $this->MultiCell($w, 5, $data[$i], 0, $a);
            //Put the position to the right of the cell
            $this->SetXY($x + $w, $y);
        }
        //Go to the next line
        $this->Ln($h);
    }

    function RowS($data) {
        //Calculate the height of the row
        $nb = 0;
        for ($i = 0; $i < count($data); $i++)
            $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
        $h = 6 * $nb;
        //Issue a page break first if needed
        $this->CheckPageBreak($h);
        //Draw the cells of the row
        for ($i = 0; $i < count($data); $i++) {
            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            //Save the current position
            $x = $this->GetX();
            $y = $this->GetY();
            //Draw the border
            $this->Rect($x,$y,$w,$h);
            //Print the text


            $this->MultiCell($w, 5, $data[$i], 0, $a);
            //Put the position to the right of the cell
            $this->SetXY($x + $w, $y);
        }
        //Go to the next line
        $this->Ln($h);
    }

    function CheckPageBreak($h) {
        //If the height h would cause an overflow, add a new page immediately
        if ($this->GetY() + $h > $this->PageBreakTrigger)
            $this->AddPage($this->CurOrientation);
    }

    function NbLines($w, $txt) {
        //Computes the number of lines a MultiCell of width w will take
        $cw = &$this->CurrentFont['cw'];
        if ($w == null)
            $w = $this->w - $this->rMargin - $this->x;
        $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->FontSize;
        $s = str_replace("\r", '', $txt);
        $nb = strlen($s);
        if ($nb > 0 and $s[$nb - 1] == "\n")
            $nb--;
        $sep = -1;
        $i = 0;
        $j = 0;
        $l = 0;
        $nl = 1;
        while ($i < $nb) {
            $c = $s[$i];
            if ($c == "\n") {
                $i++;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
                continue;
            }
            if ($c == ' ')
                $sep = $i;
            $l += $cw[$c];
            if ($l > $wmax) {
                if ($sep == -1) {
                    if ($i == $j)
                        $i++;
                } else
                    $i = $sep + 1;
                $sep = -1;
                $j = $i;
                $l = 0;
                $nl++;
            } else
                $i++;
        }
        return $nl;
    }

    function SetProtection($permissions = array(), $user_pass = '', $owner_pass = null) {
        $options = array('print' => 4, 'modify' => 8, 'copy' => 16, 'annot-forms' => 32);
        $protection = 192;
        foreach ($permissions as $permission) {
            if (!isset($options[$permission]))
                $this->Error('Incorrect permission: ' . $permission);
            $protection += $options[$permission];
        }
        if ($owner_pass === null)
            $owner_pass = uniqid(rand());
        $this->encrypted = true;
        $this->padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08" .
                "\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
        $this->_generateencryptionkey($user_pass, $owner_pass, $protection);
    }

  
    function _putstream($s) {
        if ($this->encrypted) {
            $s = RC4($this->_objectkey($this->n), $s);
        }
        parent::_putstream($s);
    }

    function _textstring($s) {
        if ($this->encrypted) {
            $s = RC4($this->_objectkey($this->n), $s);
        }
        return parent::_textstring($s);
    }

    /**
     * Compute key depending on object number where the encrypted data is stored
     */
    function _objectkey($n) {
        return substr($this->_md5_16($this->encryption_key . pack('VXxx', $n)), 0, 10);
    }

    function _putresources() {
        parent::_putresources();
        if ($this->encrypted) {
            $this->_newobj();
            $this->enc_obj_id = $this->n;
            $this->_out('<<');
            $this->_putencryption();
            $this->_out('>>');
            $this->_out('endobj');
        }
    }

    function _putencryption() {
        $this->_out('/Filter /Standard');
        $this->_out('/V 1');
        $this->_out('/R 2');
        $this->_out('/O (' . $this->_escape($this->Ovalue) . ')');
        $this->_out('/U (' . $this->_escape($this->Uvalue) . ')');
        $this->_out('/P ' . $this->Pvalue);
    }

    function _puttrailer() {
        parent::_puttrailer();
        if ($this->encrypted) {
            $this->_out('/Encrypt ' . $this->enc_obj_id . ' 0 R');
            $this->_out('/ID [()()]');
        }
    }

    /**
     * Get MD5 as binary string
     */
    function _md5_16($string) {
        return pack('H*', md5($string));
    }

    /**
     * Compute O value
     */
    function _Ovalue($user_pass, $owner_pass) {
        $tmp = $this->_md5_16($owner_pass);
        $owner_RC4_key = substr($tmp, 0, 5);
        return RC4($owner_RC4_key, $user_pass);
    }

    /**
     * Compute U value
     */
    function _Uvalue() {
        return RC4($this->encryption_key, $this->padding);
    }

    /**
     * Compute encryption key
     */
    function _generateencryptionkey($user_pass, $owner_pass, $protection) {
        // Pad passwords
        $user_pass = substr($user_pass . $this->padding, 0, 32);
        $owner_pass = substr($owner_pass . $this->padding, 0, 32);
        // Compute O value
        $this->Ovalue = $this->_Ovalue($user_pass, $owner_pass);
        // Compute encyption key
        $tmp = $this->_md5_16($user_pass . $this->Ovalue . chr($protection) . "\xFF\xFF\xFF");
        $this->encryption_key = substr($tmp, 0, 5);
        // Compute U value
        $this->Uvalue = $this->_Uvalue();
        // Compute P value
        $this->Pvalue = -(($protection ^ 255) + 1);
    }

    /*     * funcion para parrafo de las constancias* */

    var $flowingBlockAttr;

    function saveFont() {

        $saved = array();

        $saved['family'] = $this->FontFamily;
        $saved['style'] = $this->FontStyle;
        $saved['sizePt'] = $this->FontSizePt;
        $saved['size'] = $this->FontSize;
        $saved['curr'] = & $this->CurrentFont;

        return $saved;
    }

    function restoreFont($saved) {

        $this->FontFamily = $saved['family'];
        $this->FontStyle = $saved['style'];
        $this->FontSizePt = $saved['sizePt'];
        $this->FontSize = $saved['size'];
        $this->CurrentFont = & $saved['curr'];

        if ($this->page > 0)
            $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
    }

    function newFlowingBlock($w, $h, $b = 0, $a = 'J', $f = 0) {
        $this->flowingBlockAttr['width'] = $w * $this->k;
        $this->flowingBlockAttr['height'] = $h;
        $this->flowingBlockAttr['lineCount'] = 0;
        $this->flowingBlockAttr['border'] = $b;
        $this->flowingBlockAttr['align'] = $a;
        $this->flowingBlockAttr['fill'] = $f;
        $this->flowingBlockAttr['font'] = array();
        $this->flowingBlockAttr['content'] = array();
        $this->flowingBlockAttr['contentWidth'] = 1;
    }

    function finishFlowingBlock() {

        $maxWidth = & $this->flowingBlockAttr['width'];

        $lineHeight = & $this->flowingBlockAttr['height'];
        $border = & $this->flowingBlockAttr['border'];
        $align = & $this->flowingBlockAttr['align'];
        $fill = & $this->flowingBlockAttr['fill'];

        $content = & $this->flowingBlockAttr['content'];
        $font = & $this->flowingBlockAttr['font'];


        $this->_out(sprintf('%.3f Tw', 0));


        $usedWidth = 0;

        foreach ($content as $k => $chunk) {

            $b = '';

            if (is_int(strpos($border, 'B')))
                $b .= 'B';

            if ($k == 0 && is_int(strpos($border, 'L')))
                $b .= 'L';

            if ($k == count($content) - 1 && is_int(strpos($border, 'R')))
                $b .= 'R';

            $this->restoreFont($font[$k]);


            if ($k == count($content) - 1)
                $this->Cell(( $maxWidth / $this->k ) - $usedWidth + 2 * $this->cMargin, $lineHeight, $chunk, $b, 1, $align, $fill);
            else
                $this->Cell($this->GetStringWidth($chunk), $lineHeight, $chunk, $b, 0, $align, $fill);

            $usedWidth += $this->GetStringWidth($chunk);
        }
    }

    function WriteFlowingBlock($s) {

        $contentWidth = & $this->flowingBlockAttr['contentWidth'];
        $maxWidth = & $this->flowingBlockAttr['width'];
        $lineCount = & $this->flowingBlockAttr['lineCount'];

        $lineHeight = & $this->flowingBlockAttr['height'];
        $border = & $this->flowingBlockAttr['border'];
        $align = & $this->flowingBlockAttr['align'];
        $fill = & $this->flowingBlockAttr['fill'];
        $content = & $this->flowingBlockAttr['content'];
        $font = & $this->flowingBlockAttr['font'];
        $font[] = $this->saveFont();
        $content[] = '';
        $currContent = & $content[count($content) - 1];
        $cutoffWidth = $contentWidth;


        for ($i = 0; $i < strlen($s); $i++) {


            $c = $s{ $i };


            $cw = $this->CurrentFont['cw'][$c] * ( $this->FontSizePt / 1000 );

            if ($c == ' ') {

                $currContent .= ' ';
                $cutoffWidth = $contentWidth;

                $contentWidth += $cw;

                continue;
            }


            if ($contentWidth + $cw > $maxWidth) {


                $lineCount++;


                $savedContent = '';
                $savedFont = array();


                $words = explode(' ', $currContent);


                if (count($words) == 1) {


                    $savedContent = array_pop($content);
                    $savedFont = array_pop($font);

                    // trim any trailing spaces off the last bit of content
                    $currContent = & $content[count($content) - 1];

                    $currContent = rtrim($currContent);
                } else {

                    $lastContent = '';

                    for ($w = 0; $w < count($words) - 1; $w++)
                        $lastContent .= "{$words[$w]} ";

                    $savedContent = $words[count($words) - 1];
                    $savedFont = $this->saveFont();

                    $currContent = rtrim($lastContent);
                }

                $contentWidth = 0;

                foreach ($content as $k => $chunk) {

                    $this->restoreFont($font[$k]);

                    $contentWidth += $this->GetStringWidth($chunk) * $this->k;
                }

                $cutoffWidth = $contentWidth;


                if ($align == 'J') {


                    $numSpaces = 0;

                    foreach ($content as $chunk)
                        $numSpaces += substr_count($chunk, ' ');


                    if ($numSpaces > 0)
                        $this->ws = ( $maxWidth - $cutoffWidth ) / $numSpaces;
                    else
                        $this->ws = 0;

                    $this->_out(sprintf('%.3f Tw', $this->ws));
                } else
                    $this->_out(sprintf('%.3f Tw', 0));


                $usedWidth = 0;

                foreach ($content as $k => $chunk) {

                    $this->restoreFont($font[$k]);

                    $stringWidth = $this->GetStringWidth($chunk) + ( $this->ws * substr_count($chunk, ' ') / $this->k );


                    $b = '';

                    if ($lineCount == 1 && is_int(strpos($border, 'T')))
                        $b .= 'T';

                    if ($k == 0 && is_int(strpos($border, 'L')))
                        $b .= 'L';

                    if ($k == count($content) - 1 && is_int(strpos($border, 'R')))
                        $b .= 'R';


                    if ($k == count($content) - 1)
                        $this->Cell(( $maxWidth / $this->k ) - $usedWidth + 2 * $this->cMargin, $lineHeight, $chunk, $b, 1, $align, $fill);
                    else {

                        $this->Cell($stringWidth + 2 * $this->cMargin, $lineHeight, $chunk, $b, 0, $align, $fill);
                        $this->x -= 2 * $this->cMargin;
                    }

                    $usedWidth += $stringWidth;
                }


                $this->restoreFont($savedFont);

                $font = array($savedFont);
                $content = array($savedContent . $s{ $i });

                $currContent = & $content[0];

                $contentWidth = $this->GetStringWidth($currContent) * $this->k;
                $cutoffWidth = $contentWidth;
            } else {

                $contentWidth += $cw;
                $currContent .= $s{ $i };
            }
        }
    }


     function Header() {
  
     }

    function Footer() {
        $this->SetY(-25);
        $this->Ln();
        $this->SetX(10);
        $this->SetFont('Arial', '', 6);
        $this->Cell(180, 10, utf8_decode('GENERADO A TRAVES DEL SISTEMA  EN FECHA ' . date('d/m/Y') . ' POR EL USUARIO: ' . rtrim(Yii::app()->user->name)), 0, 0, 'L');
        $this->Cell(20, 10, utf8_decode('Página ' . $this->PageNo() . '/{nb}'), 0, 0, 'R');
        
    }

    
        
    function estadoCuenta($prestamo,$fechas,$num_pagos_cancelados,$listaPagos,$cliente) {
        
        $this->Ln(5);
        $this->SetFillColor(223,223,223);
        $this->SetFont( 'Arial', 'B', 10 );
        $this->Cell(75,8,utf8_decode( 'ESTADO DE CUENTA '),0,0,'L', TRUE);
        
        $this->Cell(115,8,utf8_decode( 'ESTATUS: '.Pagos::estatusPrestamo($prestamo['id_prestamo']).'/ PAGOS: '.Pagos::cuotasCanceladas($prestamo['id_prestamo']) . '/ ATRASOS:' .Pagos::atrazos($prestamo['id_prestamo'])  ),0,0,'R', TRUE);
        
        
        
        $this->Ln(10);
        $this->SetFont( 'Arial', 'B', 12 ); 
        $this->Cell(25, 12, 'GESTOR: ', '', 0, 'j');
        $this->SetFont( 'Arial', '', 12 ); 
        
      
        $this->Cell(70, 12, utf8_decode(Gestor::descripcion($prestamo['id_gestor'])), '', 0, 'J');
        $this->SetFont( 'Arial', 'B', 10 );
        
        
        
        $this->Cell(45, 12,'FECHA DE INICIO', '', 0, 'R');
        $this->Cell(35, 12,Utility::fecha($prestamo['fecha_inicio']), '', 0, 'R');
        $this->Ln(6);
        
        $this->SetFont( 'Arial', 'B', 12 );      
        $this->Cell(25, 12, 'CLIENTE: ', '', 0, 'J');
        $this->SetFont( 'Arial', '', 10 );
        
      
        $this->Cell(65, 12,utf8_decode($cliente->nombre.' '.$cliente->apellido), '', 0, 'J');
        $this->SetFont( 'Arial', 'B', 10 );
       
        
        
        $this->Cell(50, 12,'MONTO CREDITO', '', 0, 'R');
        $this->Cell(35, 12,number_format($prestamo['id_monto'], 2, ',', '.'), '', 0, 'R');
        $this->SetFillColor(216, 216, 216);
        
        $this->Ln(6);
        $this->Cell(90, 12,'', '', 0, 'R');
        $this->Cell(50, 12,'MONTO CREDITO + INTERES', '', 0, 'R');
        $this->Cell(35, 12,number_format(($prestamo['cuota']*$prestamo['id_duracion'] ), 2, ',', '.'), '', 0, 'R');
        $this->SetFillColor(216, 216, 216);
        
        $debe=$prestamo['cuota']*$prestamo['id_duracion'];
        $debe=$debe-$prestamo['total'];
        
        $this->Ln(6);
        
        
        
        $this->Cell(140, 12,'DEBE', '', 0, 'R');
        $this->Cell(35, 12,number_format($debe, 2, ',', '.'), '', 0, 'R');
        $this->SetFillColor(216, 216, 216);
        
        
        $this->Ln(6);
        $this->Ln(10);
        $this->SetFont( 'Arial', 'B', 7 );
        $this->Cell(10, 8, 'NUM.', 'RTL', 0, 'C',TRUE);
        $this->Cell(20, 8, 'CRONOGRAMA', 'RTL', 0, 'C',TRUE);
        $this->Cell(20, 8, 'ABONO/CUOTA', 'LRT', 0, 'C',TRUE);
        $this->Cell(20, 8, 'ABONO/MORA', 'LRT', 0, 'C',TRUE);
        $this->Cell(15, 8, 'ADELANTADO', 'LRT', 0, 'C',TRUE);
        
        $this->Cell(15, 8, 'CAPITAL', 'LRT', 0, 'C',TRUE);
        $this->Cell(20, 8, 'FECHA/PAGO', 'LRT', 0, 'C',TRUE);
        $this->Cell(15, 8, 'CANCELO', 'LRT', 0, 'C',TRUE);
        
        $this->Cell(12, 8, 'D. MORA', 'LRT', 0, 'C',TRUE);
        $this->Cell(12, 8, 'S/ MORA', 'LRT', 0, 'C',TRUE);
        /*
        $this->Cell(25, 8, 'TIPO', 'LRT', 0, 'C',TRUE);
        */
        
        $this->Cell(25, 8, 'TIPO', 'LRT', 0, 'C',TRUE);
        $this->SetFont( 'Arial', 'B', 6 );
        $this->Cell(20, 8, 'NRO. OPERACION', 'LRT', 0, 'C',TRUE);
        $this->SetFont( 'Arial', 'B', 8);
        $this->Ln(8);
     
        $limit=0;
        $num_fecha= count($fechas);
        $num_fecha=$num_fecha-1;
        $num_pagos=  count($listaPagos);
        
    
        
        if($num_fecha>$num_pagos){
           $limit= $num_fecha;
        }  else {
           $limit=$num_pagos;
        }
        
     
        $sumar_mora=0;
        $sumar_dias=0;
        $num_cuota=1;
        $total_mora = 0;
        $total_mora_pagada = 0;
        $n=0;
    for ($i = 0; $i<$limit; $i++) {
        
        
        
        if(($i+1)==1){
           //color amarrillo
           $this->SetFillColor(247, 220, 111 ); 
        }
        
        if(($i+1)==2){
           //color azul
           $this->SetFillColor(33, 150, 243 ); 
        }
        
        if(($i+1)==3){
           //color verde
           $this->SetFillColor(0, 255, 255); 
        }
         if(($i+1)==4){
           //color verde
           $this->SetFillColor(230, 230, 250); 
        }
        
          if(($i+1)==5){
           //color verde
           $this->SetFillColor(248, 247, 2 ); 
        }
           
          if(($i+1)==6){
           //color verde
           $this->SetFillColor(240, 31, 99); 
        }
          if(($i+1)==7){
           //color verde
           $this->SetFillColor(250, 250, 210); 
        }
             if(($i+1)==8){
           //color verde
           $this->SetFillColor(255, 165, 0); 
        }
        
               if(($i+1)==9){
           //color verde
           $this->SetFillColor(211, 211, 211); 
        }
        
        if(($i+1)==10){
           //color verde
           $this->SetFillColor(255, 160, 122); 
        }
        
        if(($i+1)==11){
           //color verde
           $this->SetFillColor(152, 251, 152); 
        }
            if(($i+1)==12){
           //color verde
           $this->SetFillColor(224, 255, 255); 
        }
        
              if(($i+1)==12){
           //color verde
           $this->SetFillColor(189, 183, 107); 
        }
        
      
              if(($i+1)==13){
           //color verde
           $this->SetFillColor(238, 130, 238); 
        }
        
        if(($i+1)==14){
           //color verde
           $this->SetFillColor(147, 112, 219); 
        }
        if(($i+1)==15){
           //color verde
           $this->SetFillColor(173, 255, 47); 
        }
        
          if(($i+1)==16){
           //color amarrillo
           $this->SetFillColor(247, 220, 111 ); 
        }
        
        if(($i+1)==17){
           //color azul
           $this->SetFillColor(33, 150, 243 ); 
        }
        
        if(($i+1)==18){
           //color verde
           $this->SetFillColor(0, 255, 255); 
        }
         if(($i+1)==19){
           //color verde
           $this->SetFillColor(230, 230, 250); 
        }
        
        if(($i+1)==20){
           //color verde
           $this->SetFillColor(248, 247, 2 ); 
        }
        
        if(($i+1)==21){
           $this->SetFillColor(0, 255, 225); 
        }
        
        if(($i+1)==22){
           $this->SetFillColor(205, 205, 205); 
        }
        
                if(($i+1)==23){
           $this->SetFillColor(129, 255, 100); 
        }
        
                if(($i+1)==24){
           $this->SetFillColor(100, 255, 222); 
        }
        
    
        
        
    
          $n=$n+1;
          //fecha de cronograma
    
        $this->Cell(10, 6,$n, 'RTL', 0, 'C',true);
        
        
          if($i<$num_fecha){
              
              
           $this->Cell(20, 6, Utility::fecha($fechas[$i]["fecha"]), 'RTL', 0, 'C');
          }else{
          $this->Cell(20, 6,'', 'RTL', 0, 'C');
          }
          
          
          //cuota pago
           if($i < $num_pagos){
                $this->Cell(20, 6,number_format($listaPagos[$i]["cuota"], 2, ',', '.'), 'RTL', 0, 'R');  
              
           }  else {
                $this->Cell(20, 6, '', 'RTL', 0, 'C');  
           }
          
            if($i < $num_pagos){
                $this->Cell(20, 6,number_format($listaPagos[$i]["mora"], 2, ',', '.'), 'RTL', 0, 'R');  
                
                $total_mora_pagada += $listaPagos[$i]["mora"]; 
              
           }  else {
                $this->Cell(20, 6, '', 'RTL', 0, 'C');  
           }
          
            
          //cuota que agrego de adelantada
           if($i < $num_pagos){
              $this->Cell(15, 6,number_format($listaPagos[$i]["cuota_adelantada"], 2, ',', '.') , 'RTL', 0, 'R');  
            }  else {
              $this->Cell(15, 6, '', 'RTL', 0, 'C');  
            } 
            
            
          //cuota del credito
           if($i < $num_fecha){ 
          $this->Cell(15, 6, number_format($prestamo["cuota"], 2, ',', '.'), 'RT', 0, 'R');
          }  else {
              $this->Cell(15, 6, '', 'RT', 0, 'C');  
            }
          
          //fecha de pago
           if($i < $num_pagos){
               $this->Cell(20, 6,  Utility::fecha($listaPagos[$i]["fecha"]), 'RTL', 0, 'C');    
            }  else {
              $this->Cell(20, 6, '', 'RTL', 0, 'C');  
            }
            
            
            
              
        
            if(($i+1)==1){
           //color amarrillo
           $this->SetFillColor(247, 220, 111 ); 
        }
        
        if(($i+1)==2){
           //color azul
           $this->SetFillColor(33, 150, 243 ); 
        }
        
        if(($i+1)==3){
           //color verde
           $this->SetFillColor(0, 255, 255); 
        }
         if(($i+1)==4){
           //color verde
           $this->SetFillColor(230, 230, 250); 
        }
        
          if(($i+1)==5){
           //color verde
           $this->SetFillColor(248, 247, 2 ); 
        }
           
          if(($i+1)==6){
           //color verde
           $this->SetFillColor(240, 31, 99); 
        }
          if(($i+1)==7){
           //color verde
           $this->SetFillColor(250, 250, 210); 
        }
             if(($i+1)==8){
           //color verde
           $this->SetFillColor(255, 165, 0); 
        }
        
               if(($i+1)==9){
           //color verde
           $this->SetFillColor(211, 211, 211); 
        }
        
        if(($i+1)==10){
           //color verde
           $this->SetFillColor(255, 160, 122); 
        }
        
        if(($i+1)==11){
           //color verde
           $this->SetFillColor(152, 251, 152); 
        }
            if(($i+1)==12){
           //color verde
           $this->SetFillColor(224, 255, 255); 
        }
        
              if(($i+1)==12){
           //color verde
           $this->SetFillColor(189, 183, 107); 
        }
        
      
              if(($i+1)==13){
           //color verde
           $this->SetFillColor(238, 130, 238); 
        }
        
        if(($i+1)==14){
           //color verde
           $this->SetFillColor(147, 112, 219); 
        }
        if(($i+1)==15){
           //color verde
           $this->SetFillColor(173, 255, 47); 
        }
        
          if(($i+1)==16){
           //color amarrillo
           $this->SetFillColor(247, 220, 111 ); 
        }
        
        if(($i+1)==17){
           //color azul
           $this->SetFillColor(33, 150, 243 ); 
        }
        
        if(($i+1)==18){
           //color verde
           $this->SetFillColor(0, 255, 255); 
        }
         if(($i+1)==19){
           //color verde
           $this->SetFillColor(230, 230, 250); 
        }
        
        if(($i+1)==20){
           //color verde
           $this->SetFillColor(248, 247, 2 ); 
        }
        
                if(($i+1)==21){
           $this->SetFillColor(0, 255, 225); 
        }
        
        if(($i+1)==22){
           $this->SetFillColor(205, 205, 205); 
        }
        
                if(($i+1)==23){
           $this->SetFillColor(129, 255, 100); 
        }
        
                if(($i+1)==24){
           $this->SetFillColor(100, 255, 222); 
        }
        
    
            if(($i<$num_fecha) and  ($i < $num_pagos_cancelados)){
               $this->Cell(15, 6,  Utility::fecha(Pagos::fechaPagoCuota($prestamo['id_prestamo'],$num_cuota)), 'RTL', 0, 'C',true);    
            }  else {
              $this->Cell(15, 6, '', 'RTL', 0, 'C');  
            }
            
                    if(($i+1)>=24){
           $this->SetFillColor(255, 255, 255); 
        }
        

          //dias de mora  
           
          if(($i<$num_fecha) and  ($i < $num_pagos_cancelados)){

            $fecha_pago=  Pagos::fechaPagoCuota($prestamo['id_prestamo'],$num_cuota); 
        
            $dias_mora=  Pagos::diferenciafechas($fechas[$i]["fecha"], $fecha_pago);
            
            
            $this->Cell(12, 6, $dias_mora, 'RTL', 0, 'C');
        
 
            $sal=  Pagos::calcularSaldoMora($dias_mora, $prestamo['id_monto']);
            
         
            $total_mora += $sal;
            $this->Cell(12, 6, number_format($sal, 2, ',', '.'), 'RTL', 0, 'R');
            
            
          }else {
                 $this->Cell(12, 6, '', 'RTL', 0, 'C');
                 $this->Cell(12, 6, '', 'RTL', 0, 'C');
          }  
   
            /*
         
              $this->Cell(25, 6, '', 'RTL', 0, 'C');  
            
            */
            
            
            
            
            if($i < $num_pagos){
                $this->Cell(25, 6, ModalidadPago::descripcion($listaPagos[$i]['id_modalidad_pago']), 'RTL', 0, 'C');  
               $this->Cell(20, 6, $listaPagos[$i]["num_operacion"] , 'RTL', 0, 'R');    
            }  else {
              $this->Cell(25, 6, '', 'RTL', 0, 'C');
              $this->Cell(20, 6, '', 'RTL', 0, 'C');  
            }
            $this->Ln(6);  
            
            
           
            $num_cuota++;
        }

        $this->SetFont( 'Arial', 'B', 7 );  
   
        $this->Cell(30, 7, 'TOTALES ', 'TLB',0 , 'C',TRUE);
        $this->Cell(20, 7, number_format(Pagos::totalCuota($prestamo["id_prestamo"]), 2, ',', '.')
               , 'TB', 0, 'R',TRUE);
        $this->Cell(20, 7, number_format(Pagos::totalMora($prestamo["id_prestamo"]), 2, ',', '.'), 'TBR', 0, 'R',TRUE);
        $this->Cell(109, 7, '', 'LT', 0, 'C',FALSE);
        
         $this->Ln(8);
        
        
        if($total_mora > 0){
        $this->Cell(30, 7, 'TOTAL MORA ', 'TLB',0 , 'C',TRUE);
        $this->Cell(20, 7, number_format($total_mora, 2, ',', '.')
               , 'TB', 0, 'R',TRUE);
        $this->Cell(20, 7, number_format(($total_mora - $total_mora_pagada), 2, ',', '.'), 'TBR', 0, 'R',TRUE);
        $this->Cell(109, 7, '', 'LT', 0, 'C',FALSE);
        
        $this->Ln(8);
        $this->Cell(30, 7, 'DIAS MORA ', 'TLB',0 , 'C',TRUE);
        $this->Cell(20, 7, ($total_mora/5)
               , 'TB', 0, 'R',TRUE);
          $this->Cell(20, 7, ($total_mora - $total_mora_pagada)/5, 'TBR', 0, 'R',TRUE);
        $this->Cell(109, 7, '', 'LT', 0, 'C',FALSE);
       
        
        }
         
        
        $this->Ln(7);
        
        $this->Cell(30, 12, 'OBSERVACION: ', '', 0, 'J');
        
        $this->Cell(150, 12, $prestamo["observacion_cancelo"], '', 0, 'J');
        
        
        
        
        
        }

       
        

    
}

?>