<?

//error_reporting(0);

  /*
  Notes: 210.001mm x 297.00mm
  */

  $pubuserpkey=$_GET['userid'];

  //include_once('../includes.php');
  echo '';  include_once('../dbincludes.php');
  include_once('../lib/lib_general.php');
  define('FPDF_FONTPATH','../lib/fpdf/font/');
  require('../lib/fpdf/fpdf.php');

  include_once('../models/userModel.php');
  $myuserModel=new userModel;

  $user_data = $myuserModel->getdata_user($pubuserpkey);

  include_once('../models/reportModel.php');
  $myreportModel=new reportModel;

  $header_data = $myreportModel->loadheader_mbl($pubuserpkey);
  $report_data = $myreportModel->loaddata_mbl($pubuserpkey);

  //$projectname = $header_data['hdr_projectname'];
  
/*  
//penjara temp solution
 if($header_data['hdr_projectname'] == "PENJARA TEGAR LEMBAH KLANG - PHASE 1")

{
echo "
<iframe src =\"penjara.pdf\" width=\"100%\" height=\"100%\">
  <p>Your browser does not support iframes.</p>
</iframe>
";
} */
// end penjara temp solution

  class PDF extends FPDF {
  public $datatype;
    // Page header
	public function yus_color($datatype){
	 // color yus
      if($datatype == 1 ){
	  $this->SetTextColor(4,180,4);
      }else if($datatype == 2 ){
	  $this->SetTextColor(255,0,0);
	  }else if($datatype == 3 ){
	  $this->SetTextColor(8,8,138);
	  }else if($datatype == 4 ){
	  $this->SetTextColor(56,39,11);
	  }else{
	  $this->SetTextColor(0,0,0);
	  }
	}
	
	
    function Header() {

      global $header_data;
      global $newsection;
      global $datatype;

      if ($this->PageNo()==1) {

        // zikay logo..
        $this->Image('../gfx/zikayheader.png',110,10,70,20);	//390, 87);

        // title
        $this->SetXY(118,35);
        $this->SetFont('Arial','B',9);
        $this->Cell(60,5,'MATERIAL BUDGET LIST REPORT','B',1,'C');

        // project info
        $this->SetXY(10,45);
        $this->SetFont('Arial','B',7);
        $this->Cell(30,5,'PROJECT TITLE',0,0);
        $this->SetX(40);
        $this->Cell(5,5,':',0,0);
        $this->SetX(50);
        $this->Cell(200,5,$header_data['hdr_projectname'],0,0);

        // date filter
        $this->SetXY(10,50);
        $this->SetFont('Arial','B',7);
        $this->Cell(30,5,'DATE',0,0);
        $this->SetX(40);
        $this->Cell(5,5,':',0,0);
        $this->SetX(50);
        $this->Cell(25,5,$header_data['hdr_datefrom'],1,0);
        $this->SetX(80);
        $this->Cell(5,5,'TO',0,0);
        $this->SetX(90);
        $this->Cell(25,5,$header_data['hdr_dateto'],1,0);

        //Line break
        $this->Ln(10);	//15

      }

      if ($newsection==true) {

        $this->SetFont('Arial','B',6);
        if ($datatype==1) {
		 
          //$this->Cell(30,6,'MATERIAL BUDGET LIST',0,0);
          $this->Cell(30,6,'BUDGETED MATERIAL',0,0);
		 
		  
        } else if ($datatype==2) {
          //$this->Cell(30,6,'VARIANCE ORDER LISTED',0,0);
           $this->Cell(30,6,'BUDGETED MATERIAL EXCEED',0,0);
        } else if ($datatype==3) {
          //$this->Cell(30,6,'VARIANCE ORDER NOT LISTED',0,0);
           $this->Cell(30,6,'NON-BUDGETED MATERIAL - VO',0,0);
        } else if ($datatype==4) {
          //$this->Cell(30,6,'VARIANCE ORDER CLIENT',0,0);
          $this->Cell(30,6,'NON-BUDGETED MATERIAL - SUBCON',0,0);
		 
        }
		
		//tambahan
		else if ($datatype==8) { $this->Cell(30,6,'TRANSPORT',0,0); }
		else if ($datatype==9) { 
			$this->Cell(30,6,'SUMMARY REPORT',0,0); 
			$this->Ln();
			return;
			}
		//tambahan tamat
		  
        $this->Ln();
      }
	  
		
      
      $this->SetFont('Arial','B',6);
      $this->SetX(10);
	  
	  $this->SetTextColor(0,0,0);//test

      $header=array('ITEM','MATERIAL','UNIT','QTY','RATE','AMOUNT','PO NO','QTY','RATE','AMOUNT','QTY','RATE','AMOUNT','QTY','RATE','AMOUNT');

	  
	 
      //Column widths
      $w=array(10,42,16,16,16,16,16,16,16,16,16,16,16,16,16,16);
     
      //Headers
      $this->Cell(68,5,'',1,0,'C');
      $this->Cell(48,5,'A',1,0,'C');
      $this->Cell(64,5,'B',1,0,'C');
      $this->Cell(48,5,'C',1,0,'C');
      $this->Cell(48,5,'(B - C)',1,1,'C');
      $this->Cell(68,5,'',1,0,'C');
      $this->Cell(48,5,'MATERIAL BUDGET LIST',1,0,'C');
      $this->Cell(64,5,'PURCHASE ORDER',1,0,'C');
      $this->Cell(48,5,'DELIVERED',1,0,'C');
      $this->Cell(48,5,'BALANCE DELIVERED',1,1,'C');
      for($i=0;$i<count($header);$i++)
	      
          $this->Cell($w[$i],5,$header[$i],1,0,'C');
      $this->Ln();

    }

    // Page footer
    function Footer() {

      global $user_data;

      $this->Line(10,194,290,194);

      // printer and date (1.5 cm from bottom)..
      $this->SetXY(10,-15);
      $this->SetFont('Arial','',7);
      $this->Cell(0,5,'Print Date : '.unconverttounixslash(getdatestamp()).' - Printed By : '.$user_data['staff_name'],0,0,'L');

      // page no (1.5 cm from bottom)..
      $this->SetXY(-15,-15);
      $this->SetFont('Arial','',7);
      $this->Cell(0,5,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }

    function PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt) {
    
      //Column widths
      $w=array(10,42,16,16,16,16,16,16,16,16,16,16,16,16,16,16);
    
	 
	  
	  
	  $this->SetFont('Arial','B',5);
	  
	 
      $this->Cell($w[0],5,'',1,0);
      $this->Cell($w[1],5,'TOTAL',1,0);
      $this->Cell($w[2],5,'',1,0,'C');
      $this->Cell($w[3],5,number_format($totalmblqty,4),1,0,'R');
      $this->Cell($w[4],5,'',1,0,'R');
      $this->Cell($w[5],5,number_format($totalmblamt,2),1,0,'R');
      $this->Cell($w[6],5,'',1,0);
      $this->Cell($w[7],5,number_format($totalpoqty,4),1,0,'R');
      $this->Cell($w[8],5,'',1,0,'R');
      $this->Cell($w[9],5,number_format($totalpoamt,2),1,0,'R');
      $this->Cell($w[10],5,number_format($totaldoqty,4),1,0,'R');
      $this->Cell($w[11],5,'',1,0,'R');
      $this->Cell($w[12],5,number_format($totaldoamt,2),1,0,'R');
      $this->Cell($w[13],5,number_format($totalbalqty,4),1,0,'R');
      $this->Cell($w[14],5,'',1,0,'R');
      $this->Cell($w[15],5,number_format($totalbalamt,2),1,0,'R');
      $this->Ln();
      
    }

    function PrintGrandTotals($grandmblamt,$grandpoamt,$granddoamt,$grandbalamt) {

      //Column widths
      $w=array(10,42,16,16,16,16,16,16,16,16,16,16,16,16,16,16);
 
      $this->SetFont('Arial','B',5);
      $this->Cell($w[0],5,'',1,0);
      $this->Cell($w[1],5,'GRAND TOTAL',1,0);
      $this->Cell($w[2],5,'',1,0,'C');
      $this->Cell($w[3],5,'',1,0,'R');
      $this->Cell($w[4],5,'',1,0,'R');
      $this->Cell($w[5],5,number_format($grandmblamt,2),1,0,'R');
	  //$this->Cell($w[5],5,'ghaghagha',1,0,'R');

      $this->Cell($w[6],5,'',1,0);
      $this->Cell($w[7],5,'',1,0,'R');
      $this->Cell($w[8],5,'',1,0,'R');
      $this->Cell($w[9],5,number_format($grandpoamt,2),1,0,'R');
      $this->Cell($w[10],5,'',1,0,'R');
      $this->Cell($w[11],5,'',1,0,'R');
      $this->Cell($w[12],5,number_format($granddoamt,2),1,0,'R');
      $this->Cell($w[13],5,'',1,0,'R');
      $this->Cell($w[14],5,'',1,0,'R');
      $this->Cell($w[15],5,number_format($grandbalamt,2),1,0,'R');
      $this->Ln();

    }

  }

  $newsection = true;
  $datatype = 1;

  $pdf=new PDF('L','mm','A4');
  $pdf->Open();
  $pdf->AliasNbPages();
  $pdf->AddPage('L','A4');
  $pdf->SetFont('Arial','',8);

  //Column widths
  $w=array(10,42,16,16,16,16,16,16,16,16,16,16,16,16,16,16);

  $pdf->SetFont('Courier','',6);
  
  //Kai: Sum total for MBL Budgeted materials with data no > 1
  $myprevSub = $report_data[0]['data_itemgroup'];
  $tsand = false;
  $counter = -1;
  foreach ($report_data as $row)
  {
  	//For reference purpose
  	$counter++;
  	
  	//calculation only applies to Budgeted
  	if ($datatype > 1)
  		break;
  	
  	//Kai: Sand is tricky, if data_itemunit is different then it must be T-Sand
  	if ($row['data_itemgroup'] == 'SAND' && strtoupper($row['data_itemunit']) == 'TONNE' && !$tsand)
  	{
  		$storedData[$myprevSub]['data_mblquantity'] = $totalmblqty;
  		$storedData[$myprevSub]['data_mblrate'] = $totalmblrate;
  		$storedData[$myprevSub]['data_mblsum'] = $totalmblamt;
  		$storedData['grandTotalMbl'] += $totalmblamt;
  		$totalmblqty = 0;	
  		$totalmblrate = 0;
  		$totalmblamt = 0;
  		$tsand = true;
  		$myprevSub = 'T-SAND';
  	}
  	
  	if ($tsand && $row['data_itemgroup'] == 'SAND')
  	{
  		$row['data_itemgroup'] = 'T-SAND';
  		$report_data[$counter]['data_itemno'] += 1;
  	}
  	
  		
  	if ( $myprevSub != $row['data_itemgroup'] )
  	{	
  		$storedData[$myprevSub]['data_mblquantity'] = $totalmblqty;
  		$storedData[$myprevSub]['data_mblrate'] = $totalmblrate;
  		$storedData[$myprevSub]['data_mblsum'] = $totalmblamt;
  		$storedData['grandTotalMbl'] += $totalmblamt;
  		$totalmblqty = 0;	
  		$totalmblrate = 0;
  		$totalmblamt = 0;
  	}	
  	
  	//Kai: only add total if these conditions
  	if ( 
  			( $row['data_itemno'] == 1 && $myprevName != $row['data_itemname'] ) 
  			|| ( $row['data_itemno'] >= 1 && $row['data_mblamount'] != $myprevMblAmt ) 
  		)
  	{
  		$totalmblqty += $row['data_mblquantity'];
  		$totalmblamt += $row['data_mblquantity'] * $row['data_mblrate'];
  	}	
  	$totalmblrate = $row['data_mblrate'];
  		
  	$myprevSub = $row['data_itemgroup'];
  	$myprevName = $row['data_itemname'];
  	$myprevMblAmt = $row['data_mblamount'];
  	
  }
  //End calculation
  
  $grandmblamt=0;
  $grandpoamt=0;
  $granddoamt=0;
  $grandbalamt=0;

  $totalmblqty=0;
  $totalmblamt=0;
  $totalpoqty=0;
  $totalpoamt=0;
  $totaldoqty=0;
  $totaldoamt=0;
  $totalbalqty=0;
  $totalbalamt=0;

  $tsand = false;
  $rowCounter = -1;
  foreach($report_data as $row) {
  	$rowCounter++;
  	
    if ($datatype!=$row['data_itemtype']) {
	  
      $pdf->PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
      $grandmblamt += $totalmblamt;
      $grandpoamt += $totalpoamt;
      $granddoamt += $totaldoamt;
      $grandbalamt += $totalbalamt;
      
	  //Additional For Summary
	  $grandmblamt_[$datatype]=$grandmblamt;
      $grandpoamt_[$datatype]=$grandpoamt;
      $granddoamt_[$datatype]=$granddoamt;
      $grandbalamt_[$datatype]=$grandbalamt;
	  
      if ($datatype == 1)
	  	$pdf->PrintGrandTotals($storedData['grandTotalMbl'],$grandpoamt,$granddoamt,$grandbalamt);
	  else 
	  	$pdf->PrintGrandTotals($grandmblamt,$grandpoamt,$granddoamt,$grandbalamt);
      
      $grandmblamt=0;
      $grandpoamt=0;
      $granddoamt=0;
      $grandbalamt=0;

      $totalmblqty=0;
      $totalmblamt=0;
      $totalpoqty=0;
      $totalpoamt=0;
      $totaldoqty=0;
      $totaldoamt=0;
      $totalbalqty=0;
      $totalbalamt=0;

      $prevsub="";
      $prevsubsub="";

      $newsection=true;
      $datatype=$row['data_itemtype'];
      $pdf->AddPage('L','A4');
    } else {
      $newsection=false;
    }
	
    //$pdf->Cell(150,5,$prevsubsub,1);
    //$pdf->ln();
    
    //Kai: Output total, if name is not same. to seperate T-Sand, and datatype == 1 and itemno
    if (
    $prevsub!=$row['data_itemgroup'] 
    || ($row['data_itemgroup']=='SAND' && strtoupper($row['data_itemunit'])=='TONNE' && !$tsand)
    || ($datatype == 1 && $row['is_lumpsump'] == 0 && $row['data_itemname'] != $prevsubsub)
    || ($datatype > 1 && $row['data_itemname'] != $prevsubsub)
    ) {
    	if ($prevsubsub!="") {
      	
      	//Kai: We already calculated datatype == 1 total on top, please use this instead
      	if ($datatype == 1)
      	{
	      	if (strtoupper($prevsub)=='SAND' && $tsand) 
	      	{
	      		$prevsub = "T-SAND";
	      	}
	      	if ($report_data[$rowCounter - 1]['is_lumpsump'] == 0)
	      	{
	      	$pdf->PrintTotals($report_data[$rowCounter - 1]['data_mblquantity'],$report_data[$rowCounter - 1]['data_mblamount'],$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
	      	//$pdf->PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
	      	}
	      	else
      		$pdf->PrintTotals($storedData[$prevsub]['data_mblquantity'],$storedData[$prevsub]['data_mblsum'],$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
      	}
        else
           // if($totalpoqty > 0){ //yusr cancel filter
      		$pdf->PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
            //}else{}
      	$grandmblamt += $totalmblamt;
        $grandpoamt += $totalpoamt;
        $granddoamt += $totaldoamt;
        $grandbalamt += $totalbalamt; 
        	      		
	    $totalmblqty=0;
	    $totalmblamt=0;
	    $totalpoqty=0;
	    $totalpoamt=0;
	    $totaldoqty=0;
	    $totaldoamt=0;
	    $totalbalqty=0;
	    $totalbalamt=0;
      }
    }
    
    //Kai: output first row
    if ($prevsub!=$row['data_itemgroup'] 
    || ($row['data_itemgroup']=='SAND' && strtoupper($row['data_itemunit'])=='TONNE' && !$tsand)
    ) { 
        
      if($row['data_itemgroup']=='SAND' && strtoupper($row['data_itemunit'])=='TONNE' && !$tsand)
      //Kai Sand already output, now output sand instead
      $tsand = true;


      $pdf->SetFont('Arial','B',5);
      $pdf->Cell($w[0],5,'',1);
      $pdf->Cell($w[1],5,$row['data_itemgroup'],1);
 	
      //Kai: Output total here, if datatype == 1 and is lump sum item. output total on top
      if ( $datatype == 1 && $row['is_lumpsump'] == 1 )
      {
      	if (strtoupper($row['data_itemgroup'])=='SAND' && $tsand) 
      	{
      		$itemgroup = "T-SAND";
      		$prevsubsub = ""; //Kai: reset this so that it display correct;
      	}
      	else
      	{ 
      		$itemgroup = $row['data_itemgroup'];
      	}
      	
      	$pdf->Cell($w[2],5,$row['data_itemunit'],1,0,'C');
 	    $pdf->Cell($w[3],5,number_format($storedData[$itemgroup]['data_mblquantity'],4),1,0,'R');
	    $pdf->Cell($w[4],5,number_format($storedData[$itemgroup]['data_mblrate'],2),1,0,'R');
		$pdf->Cell($w[5],5,number_format($storedData[$itemgroup]['data_mblsum'],2),1,0,'R');     		
      } else {
      	$pdf->Cell($w[2],5,'',1,0,'C');
	    $pdf->Cell($w[3],5,'',1,0,'R');
	  	$pdf->Cell($w[4],5,'',1,0,'R');
	    $pdf->Cell($w[5],5,'',1,0,'R');
      }
      $pdf->SetFont('Arial','',5);
      $pdf->Cell($w[6],5,'',1);
      $pdf->SetFont('Arial','',5);	// C6
      $pdf->Cell($w[7],5,'',1,0,'R');
      $pdf->Cell($w[8],5,'',1,0,'R');
      $pdf->Cell($w[9],5,'',1,0,'R');
      $pdf->Cell($w[10],5,'',1,0,'R');
      $pdf->Cell($w[11],5,'',1,0,'R');
      $pdf->Cell($w[12],5,'',1,0,'R');
      $pdf->Cell($w[13],5,'',1,0,'R');
      $pdf->Cell($w[14],5,'',1,0,'R');
      $pdf->Cell($w[15],5,'',1,0,'R');
      $pdf->Ln();
     
    }
    //Kai: end top summary display
 

	//Edited by Abejali
	$e_Width=$w[1];
	$e_ItemName=str_ireplace("\'", "'", $row['data_itemname']);
	$e_ItemName=str_ireplace('\"', '"', $e_ItemName);
	$e_ItemLen=strlen($e_ItemName);
	if($e_ItemLen==0){
		$nb=1;
	}
	else{
		$nb=ceil($e_ItemLen/$e_Width);
	}
	$e_Height=5*$nb;
	//-End
	
    //Kai: only add total if these conditions
  	if ( 
  			( $row['data_itemno'] == 1 && $prevsubsub != $row['data_itemname'] && $datatype == 1 ) 
  			|| ( $row['data_itemno'] >= 1 && $row['data_mblamount'] != $myprevMblAmt && $datatype == 1 ) 
  		)
  	{
  		$totalmblqty += $row['data_mblquantity'];
  		$totalmblamt += $row['data_mblquantity'] * $row['data_mblrate'];
  	}
  	$myprevMblAmt = $row['data_mblamount'];
    
    //kai: if $datatype not 1, then add row by row
    if ( $datatype > 1 ) {
	  $totalmblqty += $row['data_mblquantity'];
	  $totalmblamt += $row['data_mblamount'];	
    }
	
	$pdf->SetFont('Arial','',5);
    if ( ( $prevsubsub!=$row['data_itemname'] && $datatype == 1 ) || $datatype > 1 ) {
     // if($row['data_porate'] != 0){ // yusr cancel filter
	  $pdf->Cell($w[0],$e_Height,$row['data_itemno'],1,0,'C');
	  //}else{}
	  //Added By Abejali
	  $x=$pdf->GetX();
	  $y = $pdf->GetY();
	  $pdf->SetXY($x, $y+($nb*1.5));
	  
	
	 if($row['data_porate'] != 0){ // yusr cancel filter
		$pdf->MultiCell($w[1], 2,$e_ItemName,0,'L');
	  }else{
	    $pdf->MultiCell($w[1], 2,$e_ItemName." (Cancel) ",0,'L'); //cancel 12-11
	  }
	  
	  
	  $pdf->SetXY($x, $y);
	  $pdf->Cell($w[1], $e_Height, '', 1); 
	  //-End
	  
	  //Kai: if item is lump sump don't output item unit.
	  if ( $row['is_lumpsump']==0 && $datatype == 1 )
	  {
	  	$pdf->Cell($w[2],$e_Height,$row['data_itemunit'],1,0,'C');
	  }
	  else
	  { 
	  	$pdf->Cell($w[2],$e_Height,'',1);
	  }
	  	
	} else {
      $pdf->Cell($w[0],$e_Height,'',1);
      $pdf->Cell($w[1],$e_Height,'',1);
      $pdf->Cell($w[2],$e_Height,'',1);
    }
    $pdf->SetFont('Arial','',5);	
    
    //Kai: Don't copy amount over if lump sum, output MBL value
    //Kai: if datatype not MBL, output only
    if ( ( $row['is_lumpsump'] == 0 && $prevsubsub!=$row['data_itemname'] && $datatype == 1 ) 
    	|| ( $datatype > 1 ) 
    )
    {
    	if ($datatype == 1)
    	{
    	$pdf->Cell($w[3],$e_Height,number_format($row['data_mblquantity'],4),1,0,'R');
    	$pdf->Cell($w[4],$e_Height,number_format($row['data_mblrate'],2),1,0,'R');
    	$pdf->Cell($w[5],$e_Height,number_format($row['data_mblamount'],2),1,0,'R');
    	} else {
		    if($row['data_porate'] != 0){ //yusr filter cancel mbl qty
			$pdf->Cell($w[3],$e_Height,number_format($row['data_mblquantity'],4),1,0,'R');
		    $pdf->Cell($w[4],$e_Height,number_format($row['data_mblrate'],2),1,0,'R');
			$pdf->Cell($w[5],$e_Height,number_format($row['data_mblamount'],2),1,0,'R');
			
			#$pdf->Cell($w[3],$e_Height,"",1,0,'R');
			#$pdf->Cell($w[4],$e_Height,"",1,0,'R');
			#$pdf->Cell($w[5],$e_Height,"",1,0,'R');
			}
    	}
    } else {
     	$pdf->Cell($w[3],$e_Height,"",1,0,'R');
    	$pdf->Cell($w[4],$e_Height,"",1,0,'R');
    	$pdf->Cell($w[5],$e_Height,"",1,0,'R');   	
    }
    
    $pdf->SetFont('Arial','',5);
    //Kai: if no po don't display
    if($row['data_pono'] != 'no po')
    	$pdf->Cell($w[6],$e_Height,$row['data_pono'],1,0,'C');
    else
    {
    	$pdf->Cell($w[6],$e_Height,'',1,0,'C');
    	//Kai: if no po, set rate to mbl rate
    	$row['data_deliveredrate'] = $row['data_mblrate'];
    	$row['data_porate'] = $row['data_mblrate'];
    	$row['data_balancerate'] = $row['data_mblrate'];
    }
    	
    $pdf->SetFont('Arial','',5);	// C6
    
    //Kai: if no po don't display
    if ($row['data_pono'] != 'no po')
    {
	    	
	    $pdf->Cell($w[7],$e_Height,number_format($row['data_poquantity'],4),1,0,'R');
	    $pdf->Cell($w[8],$e_Height,number_format($row['data_porate'],2),1,0,'R');
	    $pdf->Cell($w[9],$e_Height,number_format($row['data_poamount'],2),1,0,'R');
		   
    } else {
    	$pdf->Cell($w[7],$e_Height,'0',1,0,'R');
    	$pdf->Cell($w[8],$e_Height,$row['data_porate'],1,0,'R');
    	$pdf->Cell($w[9],$e_Height,'0',1,0,'R');
    }
	//if($row['data_porate'] != 0){ //yusr filter cancel vo
    $pdf->Cell($w[10],$e_Height,number_format($row['data_deliveredquantity'],4),1,0,'R');
    $pdf->Cell($w[11],$e_Height,number_format($row['data_deliveredrate'],2),1,0,'R');
    $pdf->Cell($w[12],$e_Height,number_format($row['data_deliveredamount'],2),1,0,'R');
    $pdf->Cell($w[13],$e_Height,number_format($row['data_balancequantity'],4),1,0,'R');
    $pdf->Cell($w[14],$e_Height,number_format($row['data_balancerate'],2),1,0,'R');
    $pdf->Cell($w[15],$e_Height,number_format($row['data_balanceamount'],2),1,0,'R');
    //}else{}
	$pdf->Ln();

    $totalpoqty += $row['data_poquantity'];
    $totalpoamt += $row['data_poamount'];
    $totaldoqty += $row['data_deliveredquantity'];
    $totaldoamt += $row['data_deliveredamount'];
    $totalbalqty += $row['data_balancequantity'];
    $totalbalamt += $row['data_balanceamount'];

    $prevsub = $row['data_itemgroup'];
    $prevsubsub = $row['data_itemname'];
  }

	//Last Calculation
    $grandmblamt += $totalmblamt;
	$grandpoamt += $totalpoamt;
    $granddoamt += $totaldoamt;
    $grandbalamt += $totalbalamt;
	//Additional For Summary
	$grandmblamt_[$datatype]=$grandmblamt;
    $grandpoamt_[$datatype]=$grandpoamt;
    $granddoamt_[$datatype]=$granddoamt;
    $grandbalamt_[$datatype]=$grandbalamt;

      
	  $pdf->PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
	  $pdf->PrintGrandTotals($grandmblamt,$grandpoamt,$granddoamt,$grandbalamt);
	  
	  //Column widths 
	  $newsection = true;
	  $datatype=9;
	  
	  $pdf->AddPage('L','A4');
	  //$pdf->SetXY(10,45);
      $pdf->SetFont('Arial','B',5);
      //$w=array(60,60,60);
	  $w=array(40,20,40,20,40,20);
	  $pdf->Cell($w[0],5,'BUDGETED MATERIAL:',1,0,'TL');
      $pdf->Cell($w[1],5,number_format($grandmblamt_[1],2),1,0,'R');
	  $pdf->Cell($w[2],5,'ACTUAL PURCHASED:',1,0,'TL');
      $pdf->Cell($w[3],5,number_format($grandpoamt_[1],2),1,0,'R');
	  $pdf->Cell($w[4],5,'ACTUAL DELIVERED:',1,0,'TL');
      $pdf->Cell($w[5],5,number_format($granddoamt_[1],2),1,0,'R');
	  $pdf->Ln();
  
 	  $pdf->Cell($w[0],5,'BUDGETED MATERIAL - EXCEEDED:',1,0,'TL');
      $pdf->Cell($w[1],5,number_format($grandmblamt_[2],2),1,0,'R');
	  $pdf->Cell($w[2],5,'ACTUAL PURCHASED:',1,0,'TL');
      $pdf->Cell($w[3],5,number_format($grandpoamt_[2],2),1,0,'R');
	  $pdf->Cell($w[4],5,'ACTUAL DELIVERED:',1,0,'TL');
      $pdf->Cell($w[5],5,number_format($granddoamt_[2],2),1,0,'R');
	  $pdf->Ln();
   
      $pdf->Cell($w[0],5,'NON BUDGETED MATERIAL - VO:',1,0,'TL');
	  $pdf->Cell($w[1],5,number_format($grandmblamt_[3],2),1,0,'R');
      $pdf->Cell($w[2],5,'ACTUAL PURCHASED:',1,0,'TL');
	  $pdf->Cell($w[3],5,number_format($grandpoamt_[3],2),1,0,'R');
      $pdf->Cell($w[4],5,'ACTUAL DELIVERED:',1,0,'TL');
	  $pdf->Cell($w[5],5,number_format($granddoamt_[3],2),1,0,'R');
      $pdf->Ln();

	  $pdf->Cell($w[0],5,'NON BUDGETED MATERIAL - SUBCON:',1,0,'TL');
      $pdf->Cell($w[1],5,number_format($grandmblamt_[4],2),1,0,'R');
	  $pdf->Cell($w[2],5,'ACTUAL PURCHASED:',1,0,'TL');
      $pdf->Cell($w[3],5,number_format($grandpoamt_[4],2),1,0,'R');
	  $pdf->Cell($w[4],5,'ACTUAL DELIVERED:',1,0,'TL');
      $pdf->Cell($w[5],5,number_format($granddoamt_[4],2),1,0,'R');
	  $pdf->Ln();	
		
	  $pdf->Cell($w[0],5,'GRAND TOTAL',0,0,'TL');
      $pdf->Cell($w[1],5,number_format($grandmblamt_[1]+$grandmblamt_[2]
					+$grandmblamt_[3]+$grandmblamt_[4],2),1,0,'R');
	  $pdf->Cell($w[2],5,'',0,0,'TL');
      $pdf->Cell($w[3],5,number_format($grandpoamt_[1]+$grandpoamt_[2]
					+$grandpoamt_[3]+$grandpoamt_[4],2),1,0,'R');
	  $pdf->Cell($w[4],5,'',0,0,'TL');
      $pdf->Cell($w[5],5,number_format($granddoamt_[1]+$granddoamt_[2]
					+$granddoamt_[3]+$granddoamt_[4],2),1,2,'R');
	  $pdf->Ln(10);	


	  //Section 2
	  //$pdf->AddPage('L','A4');
	  //$pdf->SetXY(10,45);
	  $pdf->SetFont('Arial','B',5);
      $w=array(40,20,40,20,40,20);
	  $pdf->Cell($w[0],5,'TOTAL BUDGETED MATERIAL',1,0,'TL');
      $pdf->Cell($w[1],5,number_format($grandmblamt_[1],2),1,0,'R');
      $pdf->Cell($w[2],5,'TOTAL ACTUAL PURCHASED',1,0,'TL');
      $pdf->Cell($w[3],5,number_format($grandpoamt_[1],2),1,0,'R');
	  $pdf->Cell($w[4],5,'TOTAL ACTUAL DELIVERED',1,0,'TL');
      $pdf->Cell($w[5],5,number_format($granddoamt_[1],2),1,0,'R');
	  $pdf->Ln();

		
      $gtM=$grandmblamt_[2]+$grandmblamt_[3];
	  $gtP=$grandpoamt_[2]+$grandpoamt_[3];
	  $gtD=$granddoamt_[2]+$granddoamt_[3];

     
	 
	  $pdf->Cell($w[0],5,'TOTAL MATERIALS (EXE + VO)',1,0,'TL');
      $pdf->Cell($w[1],5,number_format($gtM,2),1,0,'R');
      $pdf->Cell($w[2],5,'TOTAL ACTUAL PURCHASED (EXE + VO)',1,0,'TL');
      $pdf->Cell($w[3],5,number_format($gtP,2),1,0,'R');
	  $pdf->Cell($w[4],5,'TOTAL ACTUAL DELIVERED (EXE + VO)',1,0,'TL');
      $pdf->Cell($w[5],5,number_format($gtD,2),1,0,'R');
	  $pdf->Ln();

	  //$pdf->SetFillColor(220,50,50);
	  $pdf->SetFillColor(245,245,245);
	  $pdf->Cell($w[0],5,'',1,0,'TL',True);
      $pdf->Cell($w[1],5,number_format($grandmblamt_[1]+$gtM,2),1,0,'R',True);
      $pdf->Cell($w[2],5,'',1,0,'TL',True);
      $pdf->Cell($w[3],5,number_format($grandpoamt_[1]+$gtP,2),1,0,'R',True);
	  $pdf->Cell($w[4],5,'',1,0,'TL',True);
      $pdf->Cell($w[5],5,number_format($granddoamt_[1]+$gtD,2),1,0,'R',True);
	  $pdf->Ln();	

	  // yusr 27-6-2011
	  if($grandmblamt_[1] == 0){
	  $pM = 0;
	  }else if($grandpoamt_[1] == 0){
	  $pP = 0;
	  }else if($granddoamt_[1] == 0){
	  $pD = 0;
	  }else{
	  $pM=number_format((($grandmblamt_[1]-$gtM)/$grandmblamt_[1])*100,2).'%';
	  $pP=number_format((($grandpoamt_[1]-$gtP)/$grandpoamt_[1])*100,2).'%';
	  $pD=number_format((($granddoamt_[1]-$gtD)/$granddoamt_[1])*100,2).'%';
	  }

	  $pdf->Cell($w[0],5,'PERCENTAGE',1,0,'R');
      $pdf->Cell($w[1],5,$pM,1,0,'R');
      $pdf->Cell($w[2],5,'PERCENTAGE',1,0,'R');
      $pdf->Cell($w[3],5,$pP,1,0,'R');
	  $pdf->Cell($w[4],5,'PERCENTAGE',1,0,'R');
      $pdf->Cell($w[5],5,$pD,1,0,'R');
	  $pdf->Ln();


  
  $pdf->Output();
  exit;


  /* For outputting to a file..
  $file = basename(tempnam('.', 'tmp'));
  rename($file, $file.'.pdf');
  $file .= '.pdf';
  $pdf->Output($file, 'F');
  header('Location: '.$file);
  */
  
  // tambahan
  //$this->Ln(10);
  /*
  $pdf=new PDF('L','mm','A4');
  $pdf->Open();
  $pdf->AliasNbPages();
  $pdf->AddPage('L','A4');
  $pdf->SetFont('Arial','',8);

  $w=array(10,42,16,16,16,16,16,16,16,16,16,16,16,16,16,16);
 // $this->Cell(30,6,'BUDGETED MATERIAL EXCEED',0,0);
  */
  
  //Closure line
  //$pdf->Cell(array_sum($w),0,'','T');

  // output..

  
?>