<?

error_reporting(0);

  /*
  Notes: 210.001mm x 297.00mm
  */

  $pubuserpkey=$_GET['userid'];

  //include_once('../includes.php');
  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 {

    // Page header
    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); }
		//tambahan tamat
		  
        $this->Ln();

      }
	  
		

      $this->SetFont('Arial','B',6);
      $this->SetX(10);

      $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[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);

  // data area..

  //for($i=1;$i<=40;$i++)
  //  $pdf->Cell(40,40,'Printing line number '.$i.$pubuserpkey,0,1);
  //$pdf->Cell(40,10,'Hello World');

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

  $pdf->SetFont('Courier','',6);

  $grandmblamt=0;
  $grandpoamt=0;
  $granddoamt=0;
  $grandbalamt=0;

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

  foreach($report_data as $row) {
    if ($datatype!=$row['data_itemtype']) {

      $pdf->PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
      $grandmblamt += $totalmblamt;
      $grandpoamt += $totalpoamt;
      $granddoamt += $totaldoamt;
      $grandbalamt += $totalbalamt;
      $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;
    }
    if ($prevsub!=$row['data_itemgroup']) {

      if ($prevsub!="") {

        $pdf->PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);

        $grandmblamt += $totalmblamt;
        $grandpoamt += $totalpoamt;
        $granddoamt += $totaldoamt;
        $grandbalamt += $totalbalamt;

      }

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

      $pdf->SetFont('Arial','B',5);
      $pdf->Cell($w[0],5,'',1);
      $pdf->Cell($w[1],5,$row['data_itemgroup'],1);
      $pdf->Cell($w[2],5,$row['data_itemunit'],1,0,'C');
      $pdf->SetFont('Arial','',5);	// C6
      $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();
    }
    $pdf->SetFont('Arial','',5);
    if ($prevsubsub!=$row['data_itemname']) {
      $pdf->Cell($w[0],5,$row['data_itemno'],1,0,'C');
      $pdf->Cell($w[1],5,$row['data_itemname'],1);
    } else {
      $pdf->Cell($w[0],5,'',1);
      $pdf->Cell($w[1],5,'',1);
    }
    $pdf->Cell($w[2],5,'',1);
    $pdf->SetFont('Arial','',5);	// C6
    if ($prevsubsub!=$row['data_itemname']) {
      $pdf->Cell($w[3],5,number_format($row['data_mblquantity'],4),1,0,'R');
      $pdf->Cell($w[4],5,number_format($row['data_mblrate'],2),1,0,'R');
      $pdf->Cell($w[5],5,number_format($row['data_mblamount'],2),1,0,'R');
    } else {
      $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,$row['data_pono'],1,0,'C');
    $pdf->SetFont('Arial','',5);	// C6
    $pdf->Cell($w[7],5,number_format($row['data_poquantity'],4),1,0,'R');
    $pdf->Cell($w[8],5,number_format($row['data_porate'],2),1,0,'R');
    $pdf->Cell($w[9],5,number_format($row['data_poamount'],2),1,0,'R');
    $pdf->Cell($w[10],5,number_format($row['data_deliveredquantity'],4),1,0,'R');
    $pdf->Cell($w[11],5,number_format($row['data_deliveredrate'],2),1,0,'R');
    $pdf->Cell($w[12],5,number_format($row['data_deliveredamount'],2),1,0,'R');
    $pdf->Cell($w[13],5,number_format($row['data_balancequantity'],4),1,0,'R');
    $pdf->Cell($w[14],5,number_format($row['data_balancerate'],2),1,0,'R');
    $pdf->Cell($w[15],5,number_format($row['data_balanceamount'],2),1,0,'R');
    $pdf->Ln();

    $totalmblqty += $row['data_mblquantity'];
    $totalmblamt += $row['data_mblamount'];
    $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'];
  }

  $pdf->PrintTotals($totalmblqty,$totalmblamt,$totalpoqty,$totalpoamt,$totaldoqty,$totaldoamt,$totalbalqty,$totalbalamt);
  $pdf->PrintGrandTotals($grandmblamt,$grandpoamt,$granddoamt,$grandbalamt);
  
  
  // 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..

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

?>