<?php

class EKPDF extends TCPDF {

    //Page header
    function Header(){   
        $data = $this->getHeaderData();
        $this->company = $data['title'];
        //Logo
        if ($this->company->logo != '') {
               if(file_exists($this->company->logo)) {
                   $logo = \Drupal::service('file_url_generator')->generateAbsoluteString($this->company->logo);
                   $info = getimagesize($logo);
                    $h = $info[1] * 35 / $info[0];
                    $this->Image($logo, 25, 10, $h);
               }
        }
        $this->Ln(5);
        $this->SetFont('helvetica','',12);
        $this->Cell(100);
        $this->Cell(50,5,$this->company->name,0,1);
        $this->SetFont('helvetica','',8);
        if ($this->company->reg_number) {
            $this->Cell(100);
            $this->Cell(8,3,"(".$this->company->reg_number .")",0,1);
        }
        if ($this->company->address1) {
            $this->Cell(100);    
            $this->Cell(60,4, $this->company->address1,0,1);
        }
        if ($this->company->address2) {
             $this->Cell(100);    
            $this->Cell(60,4, $this->company->address2,0,1);
        }
        $next = '';
        if ($this->company->postcode) {
            $next .= $this->company->postcode;
        }
        if($this->company->city){
            $next .= " " . $this->company->city;
        }
        if($this->company->country){
            $next .= ", " . $this->company->country;
        }
        $this->Cell(100);
        $this->Cell(50,4, $next ,0,1);
            
        if ($this->company->telephone) {
            $this->Cell(100);
            $this->Cell(8,3,t("tel:"). $this->company->telephone,0,1);
        }
        if ($this->company->fax) {
            $this->Cell(100);
            $this->Cell(8,4,t("fax:"). $this->company->fax,0,1);
        } 

        
    }

    //Page footer
    function Footer(){
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        //Page number
        $this->Cell(0,5, t('Page') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, 1, 'R');
    }
}

// create new PDF document
$pdf = new EKPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator($company->name);
$pdf->SetAuthor(\Drupal::currentUser()->getAccountName());
$pdf->SetTitle(t('Reconciliation report'));
$pdf->SetSubject(t('Reconciliation report'));
$pdf->SetKeywords(t('Reconciliation report'));

// set default header data
$pdf->SetHeaderData('','',$company,'');

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 50, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//$pdf=new PDF();
$pdf->SetTitle($fileName);
$pdf->AddPage('P','A4');
$col1_width=110;
$col2_width=36;
$col3_width=36;
$col4_width=36;
$left_margin=1;

// header details

// document head
$pdf->Ln(1);
$pdf->SetFont('helvetica','B',11);
$pdf->SetTextColor(68, 151, 242);
$pdf->Cell($col1_width,5, t('RECONCILIATION REPORT') ,0,1);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetLineStyle(['width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => [128, 128, 128]]);

$pdf->Ln(5);    
$pdf->SetFont('helvetica','B',8);
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5, t('Account reconciled'),0,0);
$pdf->SetFont('helvetica','',8);
$pdf->Cell($col2_width,5, $aid . ' ' . $aname ,0,1);
$pdf->SetFont('helvetica','B',8);
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5,t("Credits"),0,0);
$pdf->SetFont('helvetica','',8); 
$pdf->Cell($col2_width,5,number_format($data[0][0],2),0,1);
$pdf->SetFont('helvetica','B',8); 
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5,t("Debits"),0,0);
$pdf->SetFont('helvetica','',8);   
$pdf->Cell($col2_width,5,number_format($data[0][1],2),0,1);
$pdf->SetFont('helvetica','B',8);
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5,t("Opening balance") . ":",0,0);
$pdf->SetFont('helvetica','',8);      
$pdf->Cell($col2_width,5,number_format(abs($data[0][2]),2),0,1);
$pdf->SetFont('helvetica','B',8); 
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5,t("Reconciled balance") . ":",0,0);
$pdf->SetFont('helvetica','',8);      
$pdf->Cell($col2_width,5,number_format($data[0][3],2),0,1); 
$pdf->SetFont('helvetica','B',8);
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5,t("Difference") . ":",0,0);
$pdf->SetFont('helvetica','',8);       
$pdf->Cell($col2_width,5,number_format($data[0][4],2),0,1);  
$pdf->SetFont('helvetica','B',8); 
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5,t("Date of reconcliation") .":",0,0);
$pdf->SetFont('helvetica','',8);        
$pdf->Cell($col2_width,5,$data[0][5],0,1);  
$pdf->Ln(5);     

$pdf->SetFont('helvetica','B',10);  
$pdf->Cell($left_margin);
$pdf->Cell($col1_width,5, t('Transactions') ,1,0);
$pdf->Cell($col2_width,5, t('Credits'),1,0,'C');
$pdf->Cell($col3_width,5, t('Debits') ,1,1,'C');  

$pdf->SetFont('helvetica','',8);   
$total_cr = 0;
$total_db = 0;
$pdf->SetLineStyle(['width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 1, 'color' => [128, 128, 128]]);
for ($x=1;$x<=count($data);$x++) {
    
        if(isset($data[$x][5])){

            if($data[$x][8] == 1) {
                $error = '* ';
            } else {
                $error = '';
            }

            if (strlen($data[$x][6])>45) {
                $this_line = substr($data[$x][6],0,45)."...";} 
            else {
                $this_line = $data[$x][6];
            }

            $this_line = $error . $data[$x][1]." - " . $data[$x][7] . ' ' . $this_line;
            $pdf->Cell($left_margin);
            $pdf->Cell($col1_width,5,$this_line,'LR',0);
            $amount = $data[$x][4]." ".number_format($data[$x][5],2);
            if ($data[$x][3]== "Credit" || $data[$x][3]== "credit") {
                $pdf->Cell($col2_width,5,$amount,'R',0,'R');
                $pdf->Cell($col3_width,5,'','R',1);
                $total_cr=$total_cr+$data[$x][5];
            }
            if ($data[$x][3]=="Debit" || $data[$x][3]=="debit") {
                $pdf->Cell($col2_width,5,'','R',0);
                $pdf->Cell($col3_width,5,$amount,'R',1,'R');    
                $total_db=$total_db+$data[$x][5];
            }
        }
} 
$pdf->Ln(1); 
$pdf->SetFont('helvetica','B',8); 
$pdf->SetLineStyle(['width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => [128, 128, 128]]);
    $pdf->Cell($left_margin);
    $pdf->Cell($col1_width,5, t("Total"),1,0, 'R');
    $pdf->Cell($col2_width,5,$data[1][4]." ".number_format($total_cr,2),1,0,'R');
    $pdf->Cell($col3_width,5,$data[1][4]." ".number_format($total_db,2),1,1,'R');  
         

$pdf->Ln(5);    
$pdf->SetFont('helvetica','',8); 
$date = date("F j, Y, g:i a");
    $pdf->Cell($col1_width,5, t('Printed by') . ' ' . $userName ,0,1);
    $pdf->Cell($col1_width,5,$date,0,1);