<?php

class EKPDF extends TCPDF {

//Page header
    public function Header() {
        $data = $this->getHeaderData();
        $this->company = $data['title'];
        $format = $data['text_color'];
        if ($this->company->logo != '') {
            if (file_exists($this->company->logo)) {
                $logo = \Drupal::service('file_url_generator')->generateAbsoluteString($this->company->logo);
                $this->Image($logo, $format['header']['logo_x'], $format['header']['logo_y'], $format['header']['logo_z']);
            }
        }
        $this->SetTextColor($format['header']['color']['red'], $format['header']['color']['green'], $format['header']['color']['blue']);
        $lm = $format['header']['left_margin'];
        $b = $format['header']['border'];
        $h1 = $format['header']['col_1'];
        $h2 = $format['header']['col_2'];
        $h3 = $format['header']['col_3'];
        $h4 = $format['header']['col_4'];
        $font1 = $format['header']['font'];
        $font2 = $format['header']['font'] - 2;
        $font3 = $format['header']['font'] - 4;
        $this->Ln(20);
        $this->SetFont('helvetica', '', $font1);
        $this->Cell($lm);
        $this->Cell($h1 + $h2);
        $this->Cell($h3 + $h4, 5, $this->company->name, $b, 1);
        if ($this->company->reg_number) {
            $this->SetFont('helvetica', '', $font3);
            $this->Cell($lm);
            $this->Cell($h1 + $h2);
            $this->Cell($h3 + $h4, 5, '(' . $this->company->reg_number . ')', $b, 1);
        }
        $this->SetFont('helvetica', '', $font2);
        $this->Cell($lm);
        $this->Cell($h1 + $h2);
        $this->Cell($h3 + $h4, 4, $this->company->address1, $b, 1);
        if ($this->company->address2) {
            $this->Cell($lm);
            $this->Cell($h1 + $h2);
            $this->Cell($h3 + $h4, 4, $this->company->address2, $b, 1);
        }

        if ($this->company->postcode != '' || $this->company->city) {
            $n = '';
            if ($this->company->postcode) {
                $n = $this->company->postcode;
            }
            if ($this->company->city) {
                $n = $this->company->city . ", " . $n;
            }
            $this->Cell($lm);
            $this->Cell($h1 + $h2);
            $this->Cell($h3 + $h4, 4, $n, $b, 1);
        }
        if ($this->company->state != '' || $this->company->country) {
            $n = '';
            if ($this->company->country) {
                $n = $this->company->country;
            }
            if ($this->company->state) {
                $n = $this->company->state . ", " . $n;
            }
            $this->Cell($lm);
            $this->Cell($h1 + $h2);
            $this->Cell($h3 + $h4, 4, $n, $b, 1);
        }

        $this->SetFont('helvetica', '', $font3);

        if ($this->company->telephone <> '') {
            $this->Cell($lm);
            $this->Cell($h1 + $h2);
            $this->Cell($h3 + $h4, 3, t("tel:") . $this->company->telephone, $b, 1);
        }
        if ($this->company->fax <> '') {
            $this->Cell($lm);
            $this->Cell($h1 + $h2);
            $this->Cell($h3 + $h4, 4, t("fax:") . $this->company->fax, $b, 1);
        }
        //Line break
        $this->Ln(1);
    }

    // Page footer
    public function Footer() {

        $data = $this->getHeaderData();
        $this->company = $data['title'];
        $this->bank = $data['string'];
        $format = $data['text_color'];
        $lm = $format['footer']['left_margin'];
        $b = $format['footer']['border'];
        $f1 = $format['footer']['col_1'];
        $f2 = $format['footer']['col_2'];
        $f3 = $format['footer']['col_3'];
        $f4 = $format['footer']['col_4'];
        $font1 = $format['footer']['font'];

        $this->SetTextColor($format['footer']['color']['red'], $format['footer']['color']['green'], $format['footer']['color']['blue']);
        $this->SetY($format['footer']['w_page']);
        $this->SetFont('helvetica', '', $font1);
        if ($this->company->address3 != "") {
            $c = $this->company->address3;
            if ($this->company->address4 <> '') {
                $c .= ", " . $this->company->address4;
            }
            if ($this->company->postcode2 <> '') {
                $c .= ", " . $this->company->postcode2;
            }
            if ($this->company->city2 <> '') {
                $c .= ", " . $this->company->city2;
            }
            if ($this->company->country2 <> '') {
                $c .= ", " . $this->company->country2;
            }
            $this->Cell(0, 4, t('Correspondence address'), 'T', 1, 'C');

            $this->Cell(0, 3, $c, $b, 1, 'C');
            $t = "";
            if ($this->company->telephone2 <> '') {
                $t .= t('Tel') . ': ' . $this->company->telephone2 . " ";
            }
            if ($this->company->fax2 <> '') {
                $t .= t('Fax') . ':' . $this->company->fax2;
            }
            $this->Cell(0, 3, $t, $b, 1, 'C');
        }
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 5, t('Page') . ' ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, 1, 'R');
    }

}

// create new PDF document
// custom formats
// set default doc data
if (isset($custom)) {
    $format_doc = $custom['doc'];
    $format_header = $custom['header'];
    $format_footer = $custom['footer'];
    $format_feature = $custom['feature'];
    $format_body = $custom['body'];
} else {
    $format_doc = ['orientation' => 'P', 'format' => 'A4', 'margin_left' => 15,
        'margin_top' => 60, 'margin_right' => 15, 'margin_bottom' => 25,
        'margin_header' => 5, 'margin_footer' => 10,];

    // set default header data
    $format_header = ['left_margin' => 2, 'col_1' => 50, 'col_2' => 50,
        'col_3' => 50, 'col_4' => 50, 'border' => 0, 'logo_x' => 20, 'logo_y' => 10,
        'logo_z' => 43, 'color' => ['red'=>128,'green'=>128,'blue'=>128], 'font' => 12];

    // set default footer data
    $format_footer = ['left_margin' => 2, 'col_1' => 15, 'col_2' => 30,
        'col_3' => 60, 'col_4' => 80, 'border' => 0, 'w_data' => -45, 'w_page' => -25,
        'color' => ['red'=>128,'green'=>128,'blue'=>128], 'font' => 8];

    // set format for feature
    $format_feature = ['left_margin' => 1, 'col_1' => 50, 'col_2' => 50,
        'col_3' => 50, 'col_4' => 50, 'border' => 0,'color' => ['red' => 128,'green' => 128,'blue' => 128],  
        'colortitle' => ['red' => 120,'green' => 150,'blue' => 190],'font' => 14,
        'stamp_x' => 110, 'stamp_y' => 90, 'stamp_z' => 35,];

    // set format for body
    $format_body = ['left_margin' => 1, 'col_1' => 7, 'col_2' => 110,
        'col_3' => 20, 'col_4' => 20, 'col_5' => 25, 'border' => 0, 'cut' => 230,
        'font' => 10, 'color' => ['red'=> 128,'green' => 128,'blue' => 128],
        'fillcolor' => ['red'=> 238,'green'=> 238,'blue'=> 238]];
}
$pdf = new EKPDF($format_doc['orientation'], PDF_UNIT, $format_doc['format'], true, 'UTF-8', false);

// set document information
$no = array_reverse(explode("-", $head->serial));
$pdf->SetCreator($company->name);
$pdf->SetAuthor(\Drupal::currentUser()->getAccountName());
$pdf->SetTitle(strtolower($head->title) . ' ' . $no[0]);
$pdf->SetSubject(strtolower($head->title));
$pdf->SetKeywords(strtolower($head->title) . ", " . $head->serial);

$pdf->SetHeaderData('', '', $company, $bank, ['header' => $format_header, 'footer' => $format_footer]);

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

// set margins
$pdf->SetMargins($format_doc['margin_left'], $format_doc['margin_top'], $format_doc['margin_right']);
$pdf->SetHeaderMargin($format_doc['margin_header']);
$pdf->SetFooterMargin($format_doc['margin_footer']);

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

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

// add a page
$pdf->AddPage($format_doc['orientation'], $format_doc['format']);


$lm = $format_feature['left_margin'];
$b = $format_feature['border'];
$c1 = $format_feature['col_1'];
$c2 = $format_feature['col_2'];
$c3 = $format_feature['col_3'];
$c4 = $format_feature['col_4'];
$font1 = $format_feature['font'];
$font2 = $format_feature['font'] - 4;
$font3 = $format_feature['font'] - 6;
$pdf->SetLineStyle(['width' => 0.1, 'color' => [$format_body['color']['red'], $format_body['color']['green'], $format_body['color']['blue']]]);


// HIGHLIGHT =====================================================
$pdf->SetFont('helvetica', 'B', $font1);
$pdf->Cell($lm);
$pdf->Cell($c1 + $c2);
$pdf->SetTextColor($format_feature['colortitle']['red'], $format_feature['colortitle']['green'], $format_feature['colortitle']['blue']);
$pdf->Cell($c3, 10, strtoupper($head->title), $b, 1);
$pdf->SetTextColor($format_feature['color']['red'], $format_feature['color']['green'], $format_feature['color']['blue']);
$pdf->SetTextColor(53, 53, 53);
$pdf->SetFont('helvetica', 'B', $font2);
$pdf->Cell($lm);
$pdf->Cell($c1 + $c2);
$pdf->Cell($c3, 6, $head->serial, $b, 1);

// DATE ==========================================================
$pdf->SetFont('helvetica', '', $font2);
$pdf->Cell($lm);
$pdf->Cell($c1 + $c2);
$pdf->Cell($c3, 5, t("Date") . ":", $b, 0);
$pdf->Cell($c4, 5, $head->date, $b, 1, 'L');

// CLIENT ======================================================== 
$pdf->Ln(1);

$pdf->SetFont('helvetica', '', $font2);
$pdf->Cell($lm);
$pdf->Cell($c1, 5, utf8_decode($client->name), $b, 1);

if ($client->reg) {
    $pdf->Cell($lm);
    $pdf->SetFont('helvetica', '', $font3);
    $pdf->Cell($c1, 3, '(' . $client->reg . ')', $b, 1);
}

$pdf->SetFont('helvetica', '', $font2);
$pdf->Cell($lm);

$pdf->Cell($c1, 5, $client->address, $b, 1);

if ($client->address2 != '') {
    $pdf->SetFont('helvetica', '', $font2);
    $pdf->Cell($lm);
    $pdf->Cell($c1, 5, $client->address2, $b, 1);
}
$p = '';
if ($client->postcode != '' || $client->city) {
    $p = '';
    if ($client->city) {
        $p = $client->city;
    }
    if ($client->postcode) {
        $p = $client->postcode . ", " . $p;
    }
    $pdf->Cell($lm);
    $pdf->Cell($c1, 5, $p, $b, 1);
}

if ($client->state != '' || $client->country) {
    $p = '';
    if ($client->country) {
        $p = $client->country;
    }
    if ($client->state) {
        $p = $client->state . ", " . $p;
    }
    $pdf->Cell($lm);
    $pdf->Cell($c1, 5, $p, $b, 1);
}


if (isset($client_card->contact_name)) {
    $pdf->Ln(2);
    $pdf->SetFont('helvetica', 'U', $font2);
    $pdf->Cell($lm);
    $pdf->Cell($c1, 5, t("Attention to") . ":", $b, 0, 'L');
    $pdf->SetFont('helvetica', 'B', $font2);
    $pdf->Cell($c2, 5, $client_card->salutation . " " . $client_card->contact_name, $b, 1, 'L');
}

// COMMENT ========================================================
$pdf->Ln(2);
if ($head->comment != '') {
    $pdf->Cell($lm);
    $pdf->SetFont('helvetica', 'U', $font2);
    $pdf->Cell($c1, 5, t("Comments") . ':', $b, 1, 'L');
    $pdf->SetFont('helvetica', 'I', $font3);
    $pdf->Cell($lm);
    $pdf->MultiCell(0, 5, $head->comment, $b, 'L');
}
// PROJECT ========================================================
$pdf->Ln(2);
if ($head->pcode != "n/a") {
    $pdf->SetFont('helvetica', 'B', $font3);
    $pdf->Cell($lm);
    $pdf->Cell($c1, 5, t("Project ref.") . " " . $head->pcode, $b, 1);
}

//BODY OF purchase =================================================

$i = 0;  //count item each rows;
$ii = 0; //count groups
$iii = 0; //count group item rows
$cut = $format_body['cut'];
$lm = $format_body['left_margin'];
$b1 = $format_body['col_1'];
$b2 = $format_body['col_2'];
$b3 = $format_body['col_3'];
$b4 = $format_body['col_4'];
$b5 = $format_body['col_5'];
$font1 = $format_body['font'];
$font2 = $format_body['font'] - 2;
$font3 = $format_body['font'] - 3;
$pdf->SetFillColor($format_body['fillcolor']['red'], $format_body['fillcolor']['green'], $format_body['fillcolor']['blue']);
$pdf->SetTextColor($format_body['color']['red'], $format_body['color']['green'], $format_body['color']['blue']);
$pdf->SetDrawColor($format_body['color']['red'], $format_body['color']['green'], $format_body['color']['blue']);
$pdf->Ln(2);
$pdf->SetFont('helvetica', 'B', $font3);
$pdf->Cell($lm);
$pdf->Cell($b1, 5, t('No.'), 1, 0);
$pdf->Cell($b2, 5, t('Description'), 1, 0);
$pdf->Cell($b3, 5, t('Unit Price'), 1, 0);
$pdf->Cell($b4, 5, t('Quantity'), 1, 0);
$pdf->Cell($b5, 5, t('Amount in') . " " . $head->currency, 1, 1);

$i = 0;
$pdf->SetFillColor($format_body['fillcolor']['red'], $format_body['fillcolor']['green'], $format_body['fillcolor']['blue']);
$grandtotal = 0;
$subtotal = 0;
foreach ($items as $detail) {
    if ($detail['item'] != '') {
        if ($detail['item'] === "sub_total" || $detail['item'] === "[sub total]") {
            //insert sub total only
            $pdf->SetFont('helvetica', 'B', $font2);
            $pdf->Cell($lm);
            $pdf->Cell($b1);
            $pdf->Cell($b2 + $b3 + $b4, 5, t('Sub total'), 'T', "", 'R', 0);
            $pdf->Cell($b5, 5, number_format($subtotal, 2), 'RT', 1, 'R', 1);
            $subtotal = 0;
        } else {
            $i++;
            $pdf->Cell($lm);
            if ($detail['total'] == 0 && $detail['value'] == 0 && $detail['quantity'] == 0) {
                $pdf->SetFont('helvetica', 'B', $font2);
                $fill = 0;
                $ii++;
                $row = $ii . '_';
                $iii = 0;
                $align = 'L';
            } else {
                $pdf->SetFont('helvetica', '', $font3);
                ($i % 2 == 0) ? $fill = 1 : $fill = 0;
                $iii++;
                $row = $iii;
                $align = 'R';
            }

            //item row number
            $pdf->Cell($b1, 5, $row, 1, 0, $align);
            //get the starting point of description
            $x = $pdf->GetX();
            $y = $pdf->GetY();

            //include text into justified cell (first column)
            if($detail['description2'] != NULL){
                $pdf->writeHTMLCell($b2,5,$x,$y,$detail['description2'],"TLR",true,$fill,1,'',0);
            } else {
                $pdf->MultiCell($b2,5, $detail['item'],"TLR",'L',$fill);
            }
            
            //get ending coordinate Y
            $y2 = $pdf->GetY();
            //calculate the new coordinate Y
            $h = $y2 - $y;
            //restart from initial coordinate for second column
            $pdf->SetXY($x, $y);
            $total = $detail['total'];
            if ($detail['value'] <> 0) {
                $pdf->Cell($b2);
                $pdf->Cell($b3, $h, number_format($detail['value'], 2), 'TLB', 0, 'R', $fill);
            } else {
                $pdf->Cell($b2);
                $pdf->Cell($b3, $h, '', 'TLB', 0, 'R', $fill);
            }

            if ($detail['quantity'] <> 0) {
                $pdf->Cell($b4, $h, $detail['quantity'], 'TLB', 0, 'C', $fill);
            } else {
                $pdf->Cell($b4, $h, '', 'TB', 0, 'C', $fill);
            }

            if ($total <> 0 && ($detail['value'] <> 0 || $detail['quantity'] <> 0)) {
                $pdf->Cell($b5, $h, number_format($total, 2), '1', 1, 'R', $fill);
            } else {
                $pdf->Cell($b5, $h, '', 'BTR', 1, 'R', $fill);
            }
            $i++;
            $grandtotal = round(($grandtotal + $total), 2);
            $subtotal = round(($subtotal + $total), 2);

            //check position for a break. Add page if bottom is reached====================
            if ($pdf->GetY() > $cut) {
                $pdf->AddPage($format_doc['orientation'], $format_doc['format']);
                $font1 = $format_feature['font'];
                $font2 = $format_feature['font'] - 4;
                $font3 = $format_feature['font'] - 6;
                
                $pdf->SetFont('helvetica', 'B', $font1);
                $pdf->Cell($lm);
                $pdf->Cell($c2 + $c3);
                $pdf->SetTextColor($format_feature['colortitle']['red'], $format_feature['colortitle']['green'], $format_feature['colortitle']['blue']);
                $pdf->Cell($c3, 10, strtoupper($head->title), 0, 1);
                $pdf->SetTextColor($format_feature['color']['red'], $format_feature['color']['green'], $format_feature['color']['blue']);
                $pdf->SetFont('helvetica', 'B', $font2);
                $pdf->Cell($lm);
                $pdf->Cell($c2 + $c3);
                $pdf->Cell($c3, 10, $head->serial, 0, 1);
                $pdf->Ln(3);

                $font3 = $format_body['font'] - 3;
                $pdf->SetFont('helvetica', 'B', $font3);
                $pdf->Cell($lm);
                $pdf->Cell($b1, 5, t('No.'), 1, 0);
                $pdf->Cell($b2, 5, t('Description'), 1, 0);
                $pdf->Cell($b3, 5, t('Unit Price'), 1, 0);
                $pdf->Cell($b4, 5, t('Quantity'), 1, 0);
                $pdf->Cell($b5, 5, t('Amount in') . " " . $head->currency, 1, 1);
            }
        }            
    }
}

//CLOSE =====================================================  
$pdf->Cell($lm + $b1);
$pdf->Cell($b2 + $b3 + $b4 , 1, "", "T", 0);

$pdf->Ln(2);
$pdf->SetFont('helvetica', 'B', $font2);
$pdf->Cell($lm + $b1 + $b2);
$pdf->Cell($b3 + $b4, 5, t('Total') . " " . $head->currency, 1, 0);
$pdf->Cell($b5, 5, number_format($grandtotal, 2), 1, 1, 'R');

if ($head->taxvalue > 0) {
    if ($items['taxable'] > 0) {
        $pdf->Ln(2);
        $pdf->Cell($lm + $b1 + $b2);
        $pdf->Cell($b3 + $b4, 5, $head->tax . " " . $head->taxvalue . "%", 1, 0);
        $pdf->Cell($b5, 5, number_format($items['taxamount'], 2), 1, 1, 'R');
        $pdf->Ln(2);
        $pdf->Cell($lm + $b1 + $b2);
        $pdf->Cell($b3 + $b4, 5, t('total with tax') . " " . $head->currency, 1, 0);
        $pdf->Cell($b5, 5, number_format($items['taxamount'] + $grandtotal, 2), 1, 1, 'R');
    }
}

//ADD AMOUNT IN WORDS -----------------------------------------------
$resultinwords = new Drupal\ek_sales\NumberToWord();
$word = $resultinwords->en(round($items['taxamount'] + $grandtotal, 2));
$pdf->Ln(2);
$pdf->SetFont('helvetica', 'I', 8);
$pdf->Cell($lm + $b1 + $b2 + $b3 + $b4 + $b5 , 5, $head->currency . ' ' . $word,0, 1, 'R');


// stamps ==============================================================
if ($stamp == "2") {

    $copy = drupal_get_path('module', 'ek_sales') . "/art/copy.png";
    if (file_exists($copy)) {
        $pdf->Image($copy,$format_feature['stamp_x'], $format_feature['stamp_y'], $format_feature['stamp_z']);
    }
} elseif ($stamp == "1") {
    $original = drupal_get_path('module', 'ek_sales') . "/art/original.png";
    if (file_exists($original)) {
        $pdf->Image($original,$format_feature['stamp_x'], $format_feature['stamp_y'], $format_feature['stamp_z']);
    }
}

//SIGNATURE============================================================ 
$x = $pdf->GetX();
$y = $pdf->GetY();
$adj = ($s_pos > 0) ? $s_pos : 40;
$bottom = ($y + $adj) > 268 ? 268 : $y + $adj;
if ($signature == 1) {
     if(isset($company->sign)) {        
            if(file_exists($company->sign)) {
            $sign = \Drupal::service('file_url_generator')->generateAbsoluteString($company->sign);
            $pdf->Image($sign, 20, $bottom - 20, $format_feature['stamp_z']);
        }
    }

    $pdf->SetXY(20, $bottom);
    $pdf->Cell(60, 3, t("Authorized signatory"), 'T', 1);

} elseif($signature == 2) {
        // computer signature
        $pdf->SetXY(20,$bottom);
        $pdf->SetFont('helvetica','B',9);
        $pdf->Cell(60,3,t("Electronic purchase – no signature required") ,'',1);
}





//Receipt printing =====================================================
if ($head->status == 1 && $stamp != 2 && $stamp != 1) {
    $pdf->Ln(2);
    $pdf->Cell($lm);
    $pdf->SetTextColor(239, 58, 68);
    $pdf->SetFont('helvetica', 'B', 16);
    $pdf->Cell($c1 + $c2, 8, t('PAID'), 'LTR', 1);
    $pdf->Cell($lm);
    $pdf->SetFont('helvetica', '', 8);
    $pdf->Cell($c1, 7, t('date') . ':', 'L', 0);
    $pdf->Cell($c2, 5, $head->pdate, 'R', 1);
    $pdf->Cell($lm);
    $pdf->Cell($c1, 7, 'by:', 'L', 0);
    $pdf->Cell($c2, 5, $company->name, 'R', 1);
    $pdf->Cell($lm);
    $pdf->Cell($c1 + $c2, 5, '', 'LBR', 1, 'R');
}

