<?php
//call the FPDF library
//require('fpdf181/fpdf.php');
$this->load->library('Fpdf_Master');

//$today = date('Y-m-d'));
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

//$tt =  $user;
class PDF extends FPDF
{
    
    protected $B = 0;
protected $I = 0;
protected $U = 0;
protected $HREF = '';
// Page header
function Header()
{
    // Logo
    $this->Image('assets/images/meslogo.png',8,10,-300);
    // Arial bold 15
    $this->SetFont('Arial','B',16);
    // Move to the right
    $this->Cell(110);
    // Title
    $this->Cell(30,10,'MES ENGINEERING (PVT) LTD',0,0,'L');
     $this->Ln(5);
     $this->Cell(114);
     $this->SetFont('Arial','',10);
    $this->Cell(30,10,'124/163/B,Giriulla road,Miriiama-11200, Sri Lanka',0,0,'L');
    
    $this->Ln(5);
     $this->Cell(124);
     $this->SetFont('Arial','',10);
    $this->Cell(30,10,'033 3141126, 071 669 2410, 077 368 8672',0,0,'L');
    
    $this->Ln(5);
     $this->Cell(124);
     $this->SetFont('Arial','',10);
    $this->Cell(30,10,'info@meseniineerini.lk, meslk@yahoo.com',0,0,'L');
     $this->Ln(5);
     $this->Cell(163);
     $this->SetFont('Arial','',10);
    $this->Cell(30,10,'www.meseniineerini.lk',0,0,'R');
    // Line break
    $this->Ln(15);
   $this->Line(5,40,205,40); 
    
}

// Page footer
function Footer()
{
    
$this->Line(205,280,5,280); 
    
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    
  
    
   
    // Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'',0,0,'R');
    
}

  function SetDash($black=null, $white=null)
    {
        if($black!==null)
            $s=sprintf('[%.3F %.3F] 0 d',$black*$this->k,$white*$this->k);
        else
            $s='[] 0 d';
        $this->_out($s);
    }
    
function WriteHTML($html)
{
    // HTML parser
    $html = str_replace("\n",' ',$html);
    $a = preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
    foreach($a as $i=>$e)
    {
        if($i%2==0)
        {
            // Text
            if($this->HREF)
                $this->PutLink($this->HREF,$e);
            else
                $this->Write(5,$e);
        }
        else
        {
            // Tag
            if($e[0]=='/')
                $this->CloseTag(strtoupper(substr($e,1)));
            else
            {
                // Extract attributes
                $a2 = explode(' ',$e);
                $tag = strtoupper(array_shift($a2));
                $attr = array();
                foreach($a2 as $v)
                {
                    if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
                        $attr[strtoupper($a3[1])] = $a3[2];
                }
                $this->OpenTag($tag,$attr);
            }
        }
    }
}

function OpenTag($tag, $attr)
{
    // Opening tag
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyle($tag,true);
    if($tag=='A')
        $this->HREF = $attr['HREF'];
    if($tag=='BR')
        $this->Ln(5);
}

function CloseTag($tag)
{
    // Closing tag
    if($tag=='B' || $tag=='I' || $tag=='U')
        $this->SetStyle($tag,false);
    if($tag=='A')
        $this->HREF = '';
}

function SetStyle($tag, $enable)
{
    // Modify style and select corresponding font
    $this->$tag += ($enable ? 1 : -1);
    $style = '';
    foreach(array('B', 'I', 'U') as $s)
    {
        if($this->$s>0)
            $style .= $s;
    }
    $this->SetFont('',$style);
}

function PutLink($URL, $txt)
{
    // Put a hyperlink
    $this->SetTextColor(0,0,255);
    $this->SetStyle('U',true);
    $this->Write(5,$txt,$URL);
    $this->SetStyle('U',false);
    $this->SetTextColor(0);
}

}
$html = '

    * Please get a receipt to your payments (cash/cheques)<br>
    * All payments must be settle ----------- days';
//create pdf object
$pdf = new PDF();

// First page
$pdf->AddPage();
//set font to arial, bold, 14pt
$pdf->SetFont('arial','B',15);
//$pdf->Line(20,55,190,55);
//Cell(width , height , text , border , end line , [align] )

//$pdf->Cell(1 ,5,'To',0,0,'L');
$pdf->Cell(182 ,5,'PURCHASE ORDER',0,1,'R');//end of line

//set font to arial, regular, 12pt
$pdf->SetFont('Arial','B',10);



//bill end
$pdf->Cell(1 ,5,'SUPPLIER',0,0);//end of line
//$pdf->Cell(130 ,5,'[Street Address]',0,0);
$pdf->Cell(59 ,5,'',0,1);//end of line

$pdf->SetFont('Arial','',10);
$pdf->Cell(130 ,5,$inv[0]->sup_company,0,0);
$pdf->Cell(25 ,5,'DATE',0,0);
$pdf->Cell(34 ,5,': '.$inv[0]->po_cdate,0,1);//end of line

$pdf->Cell(130 ,5, 	$inv[0]->sup_addres ,0,0);
$pdf->Cell(25 ,5,'PO ID',0,0);
$pdf->Cell(34 ,5,': MESPO0000'.$inv[0]->po_id,0,1);//end of line

$pdf->Cell(130 ,5,'' ,0,0);
$pdf->Cell(25 ,5,'MES REF NO',0,0);
$pdf->Cell(34 ,5,': '.$inv[0]->po_cus_qto_refno,0,1);//end of line

$pdf->Cell(130 ,5,'' ,0,0);
$pdf->Cell(25 ,5,'SUP REF NO',0,0);
$pdf->Cell(34 ,5,': '.$inv[0]->po_sup_inv_refno,0,1);//end of line

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line
//billing address
//$pdf->Cell(100 ,5,'Bill to',0,1);//end of line

//add dummy cell at beginning of each line for indentation
//$pdf->Cell(10 ,5,'',0,0);
//$pdf->Cell(90 ,5,'[Name]',0,1);

//$pdf->Cell(10 ,5,'',0,0);
//$pdf->Cell(90 ,5,'[Company Name]',0,1);

//$pdf->Cell(10 ,5,'',0,0);
//$pdf->Cell(90 ,5,'[Address]',0,1);

//$pdf->Cell(10 ,5,'',0,0);
//$pdf->Cell(90 ,5,'[Phone]',0,1);

//make a dummy empty cell as a vertical spacer
//$pdf->Cell(189 ,10,'',0,1);//end of line

//invoice contents
$pdf->SetFont('Arial','B',10);

$pdf->Cell(20 ,6,'ITEM',1,0,'C');
$pdf->Cell(80 ,6,'DESCRIPTION',1,0,'C');
$pdf->Cell(15 ,6,'UNIT',1,0,'C');
$pdf->Cell(15 ,6,'QTY',1,0,'C');
$pdf->Cell(25 ,6,'RATE',1,0,'R');
$pdf->Cell(38 ,6,'AMOUNT',1,1,'R');//end of line

$pdf->SetFont('Arial','',8);

//Numbers are right-aligned so we give 'R' after new line parameter

$i=0;
$total=0;
$itmcount=0;

foreach ($inv_itm as $post)   { 
    
$pdf->Cell(20 ,5,$post->item_id,1,0);
$pdf->Cell(80 ,5,$post->item_description ,1,0);
$pdf->Cell(15 ,5,$post->unit_symble ,1,0,'R');
$pdf->Cell(15 ,5,$post->po_itm_qty  ,1,0,'R');
$pdf->Cell(25 ,5,$post->item_selling_price,1,0,'R');
$pdf->Cell(38 ,5,$post->item_selling_price * $post->po_itm_qty,1,1,'R');//end of linetotal_price
$i= $i+1;
$total = $total + ($post->item_selling_price * $post->po_itm_qty);
$itmcount = $itmcount+$post->po_itm_qty;
}
$pdf->Ln(5);
//summary
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(21 ,5,'SUB TOTAL',0,0);
$pdf->Cell(10 ,5,$inv[0]->curr_symble,1,0);
$pdf->Cell(32 ,5,number_format($total,2),1,1,'R');//end of line

$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(21 ,5,'DISCOUNT',0,0);
$pdf->Cell(10 ,5,$inv[0]->curr_symble,1,0);
$pdf->Cell(32 ,5,$inv[0]->po_discount,1,1,'R');//end of line
//$pdf->Cell(32 ,5,$post->discount.'%'.number_format($post->dis_price,2),1,1,'R');//end of line



if($inv[0]->sup_tax!=0 ){
    
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(21 ,5,'VAT 10%',0,0);
$pdf->Cell(10 ,5,$inv[0]->curr_symble,1,0);
$pdf->Cell(32 ,5,$inv[0]->po_vat,1,1,'R');//end of line

$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(21 ,5,'NBT 5%',0,0);
$pdf->Cell(10 ,5,$inv[0]->curr_symble,1,0);
$pdf->Cell(32 ,5,$inv[0]->po_nbt,1,1,'R');//end of line
    
}

if($inv[0]->sup_type=="Foreign" ){
    
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(21 ,5,'BANKING',0,0);
$pdf->Cell(10 ,5,$inv[0]->curr_symble,1,0);
$pdf->Cell(32 ,5,$inv[0]->po_banking,1,1,'R');//end of line

$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(21 ,5,'SHIPPING',0,0);
$pdf->Cell(10 ,5,$inv[0]->curr_symble,1,0);
$pdf->Cell(32 ,5,$inv[0]->po_shipping,1,1,'R');//end of line
    
}




$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(21 ,5,'TOTAL',0,0);
$pdf->Cell(10 ,5,$inv[0]->curr_symble,1,0);
$pdf->Cell(32 ,5,$inv[0]->po_final_tot,1,1,'R');//end of line

$pdf->Ln(10);
$pdf->Cell(25 ,5,'TOTAL ITEMS: ',0,0);//end of line
//$pdf->Cell(130 ,5,'[Street Address]',0,0);
$pdf->Cell(30 ,5,$i,0,0);//end of line
$pdf->Cell(30 ,5,'TOTAL PRODUCTS: ',0,0);//end of line
$pdf->Cell(50 ,5,$itmcount,0,1);//end of line
//$pdf->AliasNbPages();
//$pdf->AddPage();

$pdf->Ln(5);
$pdf->Cell(30 ,5,'REMARKS :',0,0);//end of line

$pdf->Ln(30);
//$pdf->Cell(150 ,5,'---------------------',0,0);//end of line
//$pdf->Cell(130 ,5,'[Street Address]',0,0);
//$pdf->Cell(30 ,5,'----------------------',0,1);//end of line
$pdf->Cell(150 ,5,'TERMS & CONOITIONS',0,0);//end of line
//$pdf->Cell(30 ,5,'    (Authorized)',0,1);//end of line

$pdf->Ln(10);
$pdf->SetFont('Arial','',8);


$pdf->WriteHTML($html);
$pdf->Ln(10);

 $pdf->SetFont('Arial','',8);
    $pdf->Cell(10 ,5,'Print On',0,0);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(20 ,5,' : '.date('Y-m-d'),0,0,'B');
    $pdf->SetFont('Arial','',8);
    $pdf->Cell(10 ,5,'Print By',0,0);
    $pdf->SetFont('Arial','B',8);
    $pdf->Cell(34 ,5,'demo user ',0,1,'B');//end of line

//You can now easily print text mixing different styles: <b>bold</b>, <i>italic</i>,
//<u>underlined</u>, or <b><i><u>all at once</u></i></b>!<br><br>You can also insert links on
//text, such as <a href="http://www.fpdf.org">www.fpdf.org</a>, or on an image: click on the logo.';
$pdf->Output();
?>