<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PDF extends FPDF{
    // Page header
    function Header(){
        if ($this->PageNo() == 1){
            $this->setFont('Arial','I',9);
            $this->setFillColor(255,255,255);
            $this->cell(90,6,'',0,0,'L',1); 
            $this->cell(100,6,"Printed date : " . date('d-M-Y'),0,1,'R',1); 
            $this->Line(10,$this->GetY(),200,$this->GetY());
        }else{
            $this->setFont('Arial','I',9);
            $this->setFillColor(255,255,255);
            $this->cell(90,6,"Laporan Laba Rugi",0,0,'L',1); 
            $this->cell(100,6,"Printed date : " . date('d-M-Y'),0,1,'R',1); 
        }
    }

    function Content($laba_rugi_data, $bln, $tahun, $profile){
        // Kop Laporan
        $this->Ln(3);
        $this->setFont('Arial','B',12);
        $this->setFillColor(255,255,255);
        $this->cell(0,6,$profile['perusahaan'],0,1,'C',1); 
        $this->cell(0,6,'Laporan Laba Rugi',0,1,'C',1); 
        $this->cell(0,6,'Periode '.getBulan($bln).' '.$tahun,0,1,'C',1); 
        $this->Ln(2);

        $this->SetLineWidth(0.8);
        $this->Line(10,$this->GetY(),200,$this->GetY());
        $this->SetLineWidth(0);

        $this->setFont('Arial','B',11);
        $this->setFillColor(255,255,255);
        $this->cell(0,6,'Pendapatan',0,1,'L',1); 

        $this->SetLineWidth(0.5);
        $this->Line(10,$this->GetY(),200,$this->GetY());

        $sum_pendapatan_operasional_debit = 0;
        $sum_pendapatan_operasional_kredit = 0;
        if(isset($laba_rugi_data[0][410]))
        {
            foreach ($laba_rugi_data[0][410] as $key => $row)
            {
                if($row['saldo'] < 0) {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);   
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_pendapatan_operasional_kredit += $row['saldo']; 
                } else {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);  
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_pendapatan_operasional_debit += $row['saldo'];  
                }
            }
        }

        $sum_pendapatan_nonoperasional_debit = 0;
        $sum_pendapatan_nonoperasional_kredit = 0;
        if(isset($laba_rugi_data[0][420]))
        {
            foreach ($laba_rugi_data[0][420] as $key => $row)
            {
                if($row['saldo'] < 0) {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);  
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_pendapatan_nonoperasional_kredit += $row['saldo']; 
                } else {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);   
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_pendapatan_nonoperasional_debit += $row['saldo'];  
                }
            }
        } 
        $total_pendapatan = -(($sum_pendapatan_operasional_debit + $sum_pendapatan_nonoperasional_debit) + ($sum_pendapatan_operasional_kredit + $sum_pendapatan_nonoperasional_kredit));

        $this->Line(10,$this->GetY(),200,$this->GetY());
        $this->SetLineWidth(0);

        $this->setFont('Arial','B',10);
        $this->setFillColor(255,255,255);  
        $this->cell(20,6,'',0,0,'L',1); 
        $this->cell(135,6,'Total Pendapatan',0,0,'L',1); 
        $this->cell(35,6,'Rp. '.number_format(abs($total_pendapatan),0,',','.'),0,1,'R',1); 

        $this->SetLineWidth(0.5);
        $this->Line(10,$this->GetY(),200,$this->GetY());
        $this->SetLineWidth(0);

        $this->setFont('Arial','B',11);
        $this->setFillColor(255,255,255);  
        $this->cell(0,6,'Beban',0,1,'L',1); 

        $this->SetLineWidth(0.5);
        $this->Line(10,$this->GetY(),200,$this->GetY());
        
        $sum_beban_operasional_debit = 0;
        $sum_beban_operasional_kredit = 0;
        if(isset($laba_rugi_data[0][510]))
        {
            foreach ($laba_rugi_data[0][510] as $key => $row)
            {   
                if($row['saldo'] > 0) {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);   
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_beban_operasional_debit += $row['saldo']; 
                } else {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);   
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_beban_operasional_kredit += $row['saldo'];  
                }
            }
        }

        $sum_beban_nonoperasional_debit = 0;
        $sum_beban_nonoperasional_kredit = 0;
        if(isset($laba_rugi_data[0][520]))
        {
            foreach ($laba_rugi_data[0][520] as $key => $row)
            {
                if($row['saldo'] > 0) {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);   
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_beban_nonoperasional_debit += $row['saldo']; 
                } else {
                        $this->setFont('Arial','',9);
                        $this->setFillColor(255,255,255);   
                        $this->cell(20,6,$row['noakun'],0,0,'C',1);
                        $this->cell(100,6,$row['nama'],0,0,'L',1);
                        $this->cell(35,6,'Rp. '.number_format(abs($row['saldo']),0,',','.'),0,0,'R',1);
                        $this->cell(35,6,'',0,0,'C',1);
                        $this->Ln();
                    $sum_beban_nonoperasional_kredit += $row['saldo'];  
                }
            }
        }

        $total_beban = ($sum_beban_operasional_debit + $sum_beban_nonoperasional_debit) + ($sum_beban_operasional_kredit + $sum_beban_nonoperasional_kredit);
        $this->Line(10,$this->GetY(),200,$this->GetY());
        $this->SetLineWidth(0);

        $this->setFont('Arial','B',10);
        $this->setFillColor(255,255,255);  
        $this->cell(155,6,'Total Beban',0,0,'L',1); 
        $this->cell(35,6,'Rp. '.number_format(abs($total_beban),0,',','.'),0,1,'R',1); 

        $this->SetLineWidth(0.5);
        $this->Line(10,$this->GetY(),200,$this->GetY());
        $this->SetLineWidth(0);

        $total_laba_rugi = $total_pendapatan - $total_beban;
        if($total_laba_rugi >= 0) {
            $this->setFont('Arial','B',10);
            $this->setFillColor(255,255,255);  
            $this->cell(155,6,'Laba Bersih',0,0,'L',1); 
            $this->cell(35,6,'Rp. '.number_format($total_laba_rugi,0,',','.'),0,0,'R',1); 
        } else {
            $this->setFont('Arial','B',10);
            $this->setFillColor(255,255,255);  
            $this->cell(155,6,'Rugi Bersih',0,0,'L',1); 
            $this->cell(35,6,'Rp. '.number_format(abs($total_laba_rugi),0,',','.'),0,0,'R',1); 
        }
        $this->Ln();
        $this->SetLineWidth(0.8);
        $this->Line(10,$this->GetY(),200,$this->GetY());
        $this->SetLineWidth(0);



    }

    // Page footer
    function Footer(){
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        //buat garis horizontal
        $this->Line(10,$this->GetY(),200,$this->GetY());
        //Arial italic 9
        $this->SetFont('Arial','I',9);
        $this->Cell(0,10,'Copyright@'.date('Y'),0,0,'L');
        //nomor halaman
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
    }
}

// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->setTitle('Laporan Laba Rugi '.$profile['perusahaan'],true);
$pdf->Content($laba_rugi_data, $bln, $tahun, $profile);
$pdf->Output('laporan-laba-rugi['.date('d-M-Y').'].pdf', 'I');
