<?php

class EKPDF extends TCPDF {

//Page header
    function Header() {
        $data = $this->getHeaderData();
        $this->company = $data['title'];
        $this->category = $data['string'];
        //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);
               }
        }
        $category = array(1 => "Internal invoice", 2 => "Purchase", 3 => "Claim", 4 => "Advance", 5 => "Personal claim");
        //helvetica bold 15
        $this->SetFont('helvetica', 'B', 15);
        //Move to the right
        $this->Cell(60);
        $this->SetTextColor(68, 151, 242);
        //Title
        $this->Cell(60, 10, $category[$this->category], "B", 1, 'C');
        //Line break
        $this->Ln(20);
    }


//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('Expense memos'));
$pdf->SetSubject(t('Expense memos'));
$pdf->SetKeywords(t('Expense memos') );


// 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);


$left_margin = 1;
$col1_width = 20;
$col2_width = 70;
$col3_width = 20;
$col4_width = 60;
$col5_width = 45;
$pdf->SetLineStyle(['width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => [128, 128, 128]]);
Foreach($data as $key => $arr) {
    
    
    //$pdf->setHeaderData($arr['main']['entity'], $arr['main']['category']);

    // set default header data
    $pdf->SetHeaderData('','',$arr['main']['entity'], $arr['main']['category']);
    $pdf->AddPage('P','A4');
    $amount = 0;
        $pdf->SetTextColor(0, 0, 0);
        $pdf->SetFont('helvetica', 'B', 11);
        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, t('FROM'), 0, 0);
        $pdf->Cell($col2_width, 5, '', 0, 0);
        $pdf->Cell($col3_width, 5, t('TO'), 0, 0);
        $pdf->Cell($col4_width, 5, '', 0, 1);
        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, t('Payee') . ':', 0, 0);
        $pdf->SetFont('helvetica', 'B', 9);
        $pdf->Cell($col2_width, 5, $arr['main']['entity']['name'], 0, 0);
        $pdf->SetFont('helvetica', 'B', 11);
        $pdf->Cell($col3_width, 5, t('Company') . ':', 0, 0);
        $pdf->SetFont('helvetica', 'B', 9);
        $pdf->Cell($col4_width, 5, $arr['main']['entity_to']['name'], 0, 1);

        
        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, '', 0, 0);

        $pdf->Cell($col2_width, 5, isset($arr['main']['entity']['reg_number']) ? 
                '(' . $arr['main']['entity']['reg_number'] . ')' : '', 0, 0);

        $pdf->Cell($col3_width, 5, '', 0, 0);
        $pdf->Cell($col4_width, 5, isset($arr['main']['entity_to']['reg_number']) ? 
                '(' . $arr['main']['entity_to']['reg_number'] . ')' : '', 0, 1);

        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, '', 0, 0);

        $pdf->Cell($col2_width, 5, isset($arr['main']['entity']['address1']) ? 
                $arr['main']['entity']['address1'] : '', 0, 0);

        $pdf->Cell($col3_width, 5, '', 0, 0);
        $pdf->Cell($col4_width, 5, $arr['main']['entity_to']['address1'], 0, 1);

        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, '', 0, 0);

        $pdf->Cell($col2_width, 5, isset($arr['main']['entity']['address2']) ? 
                $arr['main']['entity']['address2'] : '', 0, 0);

        $pdf->Cell($col3_width, 5, '', 0, 0);
        $pdf->Cell($col4_width, 5, $arr['main']['entity_to']['address2'], 0, 1);

        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, '', 0, 0);

        $pdf->Cell($col2_width, 5, $arr['main']['entity']['postcode'] . ' ' . $arr['main']['entity']['city'], 0, 0);

        $pdf->Cell($col3_width, 5, '', 0, 0);
        $pdf->Cell($col4_width, 5, $arr['main']['entity_to']['postcode'] . ' ' . $arr['main']['entity_to']['city'], 0, 1);

        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, '', 0, 0);

        $pdf->Cell($col2_width, 5, isset($arr['main']['entity']['country']) ? 
                $arr['main']['entity']['country'] : '', 0, 0);

        $pdf->Cell($col3_width, 5, '', 0, 0);
        $pdf->Cell($col4_width, 5, $arr['main']['entity_to']['country2'], 0, 1);

        if ($arr['main']['entity']['telephone'] != '') {
            $t1 = t('Tel') . ':' . $arr['main']['entity_to']['telephone'];
        }
        if ($arr['main']['entity']['fax'] != '') {
            $t1 .= ' ' . t('Fax') . ':' . $arr['main']['entity']['fax'];
        }
        if ($arr['main']['entity_to']['telephone'] != '') {
            $t1 = t('Tel') . ':' . $arr['main']['entity_to']['telephone'];
        }
        if ($arr['main']['entity_to']['fax'] != '') {
            $t1 .= ' ' . t('Fax') . ':' . $arr['main']['entity_to']['fax'];
        }

        $pdf->Cell($left_margin, 5, '', 0, 0);
        $pdf->Cell($col1_width, 5, '', 0, 0);
        $pdf->SetFont('helvetica', 'B', 8);

        $pdf->Cell($col2_width, 5, $t1, 0, 0);

        $pdf->SetFont('helvetica', '', 10);
        $pdf->Cell($col3_width, 5, '', 0, 0);
        $pdf->SetFont('helvetica', 'B', 8);
        $pdf->Cell($col4_width, 5, $t2, 0, 1);

        $pdf->SetFont('helvetica', '', 10);
        if ($arr['main']['entity']['contact']) {
            $pdf->Cell($left_margin, 5, t('Contact') . ':', 0, 0);
            $pdf->Cell($col1_width, 5, '', 0, 0);
            $pdf->SetFont('helvetica', 'B', 9);
            $pdf->Cell($col2_width, 5, $arr['main']['entity']['contact'], 0, 0);
        } else {
            $pdf->Cell($left_margin, 5, '', 0, 0);
            $pdf->Cell($col1_width, 5, '', 0, 0);
            $pdf->SetFont('helvetica', 'B', 9);
            $pdf->Cell($col2_width, 5, '', 0, 0);
        }
        $pdf->SetFont('helvetica', '', 10);
        $pdf->Cell($col3_width, 5, t('Contact') . ':', 0, 0);
        $pdf->SetFont('helvetica', 'B', 9);
        $pdf->Cell($col4_width, 5, $arr['main']['entity_to']['contact'], 0, 1);


        $pdf->Ln(2);


        $pdf->SetFont('helvetica', '', 10);
        $pdf->Cell(185, 3, '', 'LTR', 1);
        $pdf->Cell($left_margin, 5, '', 'L', 0);
        $pdf->Cell(50, 5, t('Document ref.') . ':', 0, 0);
        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->Cell(134, 5, $arr['main']['serial'], 'R', 1);
        $pdf->Cell($left_margin, 5, '', 'L', 0);
        $pdf->Cell(50, 5, '', 0, 0);
        $pdf->SetFont('helvetica', 'B', 10);

        $pdf->Cell(134, 5, $arr['main']['mission'], 'R', 1);

        $pdf->SetFont('helvetica', '', 10);
        $pdf->Cell($left_margin, 5, '', 'L', 0);
        $pdf->Cell(50, 5, t('Project reference') . ':', 0, 0);
        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->Cell(134, 5, $arr['main']['client_card']['name'], 'R', 1);

        $pdf->SetFont('helvetica', '', 10);
        $pdf->Cell($left_margin, 5, '', 'L', 0);
        $pdf->Cell(50, 5, '', 0, 0);
        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->Cell(134, 5, $arr['main']['pcode'], 'R', 1);

        $pdf->SetFont('helvetica', '', 10);
        $pdf->Cell($left_margin, 5, '', 'L', 0);
        $pdf->Cell(50, 5, t('Document created on') . ':', 0, 0);
        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->Cell(134, 5, $arr['main']['date'], 'R', 1);
        $pdf->Cell(185, 3, '', 'LBR', 1);

        if ($arr['main']['status'] > 1) {

            $paid = drupal_get_path('module', 'ek_finance') . "/art/paid.png";
            if (file_exists($paid)) {
                $pdf->Image($paid, 90, 95, 20);
            }
        }

        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->Ln(15);
        $pdf->Cell(50, 5, t('Category'), 1, 0);
        $pdf->Cell(80, 5, t('Description'), 1, 0);
        $pdf->Cell(20, 5, t('Amount'), 1, 0);
        $pdf->Cell(20, 5, t('Currency'), 1, 0);
        $pdf->Cell(15, 5, t('Receipt'), 1, 1);
        $pdf->Ln(2);
        $pdf->SetFont('helvetica', '', 8);


        Foreach($arr['lines'] as $l => $line) {

            $pdf->Cell(50, 5, $line['aid'] . ' ' . $line['aname'], 0, 0);
            $pdf->Cell(80, 5, $line['description'], 0, 0);
            $pdf->Cell(20, 5, number_format($line['amount'], 2), 0, 0, 'R');
            $pdf->Cell(20, 5, $arr['main']['currency'], 0, 0);
            $pdf->Cell(15, 5, $line['receipt'], 0, 1);

            $amount = $amount + $line['amount'];
        }

        $pdf->Ln(2);
        $pdf->SetFont('helvetica', 'B', 8);
        $pdf->Cell(130, 5, t('TOTAL') . ':', 1, 0);
        $pdf->Cell(20, 5, number_format($amount, 2), 1, 0, 'R');
        $pdf->Cell(20, 5, $arr['main']['currency'], 1, 0, 'L');

        $pdf->Ln(15);

        $pdf->SetFont('helvetica', 'B', 10);
        $pdf->Cell(50, 5, t('Comment') . ': ', 0, 0);
        $pdf->SetFont('helvetica', '', 8);
        $pdf->MultiCell(130, 5, $arr['main']['comment'], 0, 1);

        //STAMP============================================================ 

        if ($stamp == "2") {
            $copy = drupal_get_path('module', 'ek_finance') . "/art/copy.png";
            if (file_exists($copy)) {
                $pdf->Image($copy, 150, 10, 40);
            }
        } elseif ($stamp == "1") {
            $original = drupal_get_path('module', 'ek_finance') . "/art/original.png";
            if (file_exists($original)) {
                $pdf->Image($original, 150, 10, 40);
            }
        }
        $auth = explode('|', $arr['main']['auth']);
        if ($auth[0] == 1) {
            $na = drupal_get_path('module', 'ek_finance') . "/art/authopending.jpeg";
            if (file_exists($na))
                $pdf->Image($na, 130, 100, 50);
        }
        if ($auth[0] == 3) {
            $na = drupal_get_path('module', 'ek_finance') . "/art/notapproved.jpeg";
            if (file_exists($na))
                $pdf->Image($na, 130, 95, 50);
        }

        //SIGNATURE============================================================ 
        if ($signature == 1) {
            $pdf->Cell(100);
            if (isset($arr['main']['entity']['sign'])) {
                $sign = \Drupal::service('file_system')->realpath($arr['main']['entity']['sign']);
                $pdf->Image($sign, 20, 210, 50);
            }
        }
}
