<?php


class EKPDF extends TCPDF {

    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] * 20 / $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->telephone,0,1);
        }
        
    }

    //Page footer
    function Footer(){
        //Position at 1.5 cm from bottom
        $this->SetY(-15);
        //helvetica italic 8
        $this->SetFont('helvetica','I',8);
        //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('Voucher') . ' ' . $line->id);
$pdf->SetSubject(t('Voucher'));
$pdf->SetKeywords(t('Voucher'));

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

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

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, 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->SetTitle($fileName);

$pdf->AddPage('P','A4');
$pdf->SetLineStyle(['width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => [128, 128, 128]]);
$pdf->Ln(35);
$m = 20;
$pdf->SetDrawColor(128, 128, 128);
$pdf->SetFont('helvetica','',11);
    $pdf->Cell($m,5);
    $pdf->Cell(50,5, t('Cash Transaction No') ,1,0);
    $pdf->Cell(80,5, $line->id ,1,1);
$pdf->SetFont('helvetica','',8);   
    $pdf->Cell($m,5);
    $pdf->Cell(50,5, t('Date') ,1,0);
    $pdf->Cell(80,5, $line->pay_date ,1,1);
    $pdf->Cell($m,5);
    $pdf->Cell(50,5, t('Account') ,1,0);
    $pdf->Cell(80,5, $employee ,1,1);
    $pdf->Cell($m,5);
    $pdf->Cell(50,5,t("Amount (local currency)") . ' -  ' . $line->type,1,0);
    $pdf->Cell(80,5, $line->amount . ' - ' .  $line->currency ,1,1);
    $pdf->Cell($m,5);
    $pdf->Cell(50,5,t('Amount in base currency') ,1,0);
    $pdf->Cell(80,5, $line->cashamount . ' ' . $baseCurrency ,1,1);
    $pdf->Cell($m,5);
    $pdf->Cell(50,5, t('Comment') ,1,0);
    $pdf->MultiCell(80,5, $line->comment ,1,1);      
        
$pdf->Ln(20);
    $pdf->Cell(80);
    $pdf->Cell(50,3,t('Received by').':' ,'T',1);
    $pdf->Cell(80);
    $pdf->Cell(50,3,t('Date').':' ,0, 1);
$pdf->Ln(10);
$pdf->SetFont('helvetica','',7);
$pdf->SetTextColor(60,60,60);
$pdf->Cell($m,5);
$pdf->Cell(40,3,"printed " . date('Y-m-d') ,0,0);
