<?php

use Anouar\Fpdf\Fpdf;

class PDF extends Fpdf{
    var $widths;
    var $aligns;

    function SetWidths($w){
        $this->widths = $w;
    }

    function SetAligns($a){
        $this->aligns = $a;
    }

    function fill($f){
        $this->fill = $f;
    }

    function Row($data){
        $nb = 0;
        for ($i = 0; $i < count($data); $i++){
            $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
        }
        $h = 5 * $nb;
        $this->CheckPageBreak($h);
        for ($i = 0; $i < count($data); $i++){
            $w = $this->widths[$i];
            $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
            $x = $this->GetX();
            $y = $this->GetY();
            $this->MultiCell($w, 5, $data[$i], '', $a, '');
            $this->SetXY($x + $w, $y);
        }
        $this->Ln($h - 5);
    }

    function CheckPageBreak($h){
        if($this->GetY() + $h > $this->PageBreakTrigger){
            $this->AddPage("L", "Legal");
        }
    }

    function NbLines($w,$txt){
        $cw=&$this->CurrentFont['cw'];
        if($w == 0){
            $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;
    }
}
$pdf = new PDF();
$pdf->AddPage("L", "Legal");
$pdf->SetFont('Arial','B', 10);
$pdf->SetY(10);
if(!empty($compania->logo)){
    $pdf->Image($compania->logo, 15, 8, 15);
}
$pdf->SetX(35);
$pdf->Multicell(305, 5, $compania->razonsocial.PHP_EOL."NIT :".$compania->numeroi.PHP_EOL.utf8_decode("INFORME DE PRESCRIPCIONES"), 0, "C");
$pdf->Ln(5);
$pdf->SetFont('Arial','B', 9);
$pdf->Cell(40, 5, "COD. CATASTRAL", 'LTBR', 0, 'C');
$pdf->Cell(90, 5, "PROPIETARIO", 'LTBR', 0, 'C');
$pdf->Cell(75, 5, "DIR. PREDIO", 'LTBR', 0, 'C');
$pdf->Cell(35, 5, "NRO RESOLUCION", 'LTBR', 0, 'C');
$pdf->Cell(25, 5, "FECHA", 'LTBR', 0, 'C');
$pdf->Cell(40, 5, "VALOR", 'LTBR', 0, 'C');
$pdf->Cell(30, 5, utf8_decode("AÑOS"), 'LTBR', 0, 'C');
$pdf->Ln(5);
$data = DB::table('gr_pago_predial as gpp')
            ->leftJoin('gr_detalle_pago_predial as gdp', 'gdp.pago', '=', 'gpp.id_unico')
            ->leftJoin('gr_detalle_factura_predial as gdf', 'gdp.detallefactura', '=', 'gdf.id_unico')
            ->leftJoin('gr_factura_predial as gfp', 'gdf.factura', '=', 'gfp.id_unico')
            ->leftJoin('gp_predio1 as gpr', 'gfp.predio', '=', 'gpr.id_unico')
            ->leftJoin('gp_tercero_predio as gtp', 'gtp.predio', '=', 'gpr.id_unico')
            ->leftJoin('gr_propietarios as gps', 'gtp.tercero', '=', 'gps.id_unico')
            ->leftJoin('gr_avaluo as gra', 'gdf.avaluo', '=', 'gra.id_unico')
            ->select(
                'gpr.codigo_catastral as codCas', 'gps.nombres as nompr', 'gpr.direccion as dirpr',
                DB::raw('DATE_FORMAT(gpp.fechapago, "%d/%m/%Y") as fecha'), 'gfp.id_unico as factura',
                'gpr.id_unico as predio'
            )
            ->where('gpp.tipopago', '=', 6)
            ->where('gtp.orden', '=', '001')
            ->where('gpp.fechapago', '<', $fecha)
            ->groupBy('gpp.id_unico')
            ->orderBy('gpr.codigo_catastral', 'ASC')
            ->orderBy('gpp.fechapago')
            ->get();
foreach ($data as $row){
    $valor   = \Predial\DetalleFacturaPredial::obtnerValorTotal($row->factura);
    $minAnno = \Predial\Http\Controllers\PrescritoController::obtenerPrimerAnnoFactura($row->factura);
    $maxAnno = \Predial\Http\Controllers\PrescritoController::obtenerUltimoAnnoFactura($row->factura);;
    $xconsec = \Predial\Http\Controllers\PrescritoController::obtenerCodigoResolucion($row->predio);
    $pdf->SetFont('Arial', '', 8);
    $pdf->SetWidths(array(40, 90, 75, 35, 25, 40, 30));
    $pdf->SetAligns(array('R', 'L', 'L', 'R', 'R', 'R', 'R'));
    $pdf->Row(array($row->codCas, $row->nompr, $row->dirpr, $xconsec, $row->fecha, number_format($valor->valor, 2, ',', '.'), $minAnno." - ".$maxAnno));
    $pdf->Ln(5);
}
$pdf->Output();
exit;