<?php
//error_reporting(0);
ini_set("session.auto_start", 0);
require_once("fpdf1/fpdf.php");



$data= '';
class FPDF_AutoWrapTable extends FPDF {
  	private $data = array();
  	private $options = array(
  		'filename' => '',
  		'destinationfile' => '',
  		'paper_size'=>'F4',
  		'orientation'=>'P'
  	);
  	
  	function __construct($data = array(), $options = array()) {
    	parent::__construct();
    	$this->data = $data;
    	$this->options = $options;
	}
	
	function Header(){
		if ( $this->PageNo() !== 1 ) {
            $h = 18;
		$left = 40;
		$top = 80;	
		#tableheader
		$this->SetFillColor(200,200,200);	
		$left = $this->GetX();
		$this->SetFont("", "B", 12);
		$this->Cell(25,$h,'NO',1,0,'L',true);
		$this->SetX($left += 25); $this->Cell(50, $h, 'BL', 1, 0, 'C',true);
		$this->SetX($left += 50); $this->Cell(200, $h, 'SHIPPER', 1, 0, 'C',true);
		//$this->SetX($left += 150); $this->Cell(150, $h, 'NOTIFY PARTY', 1, 0, 'C',true);
		$this->SetX($left += 200); $this->Cell(200, $h, 'CONSIGNEE', 1, 0, 'C',true);		
		$this->SetX($left += 200); $this->Cell(35, $h, 'PKG', 1, 0, 'C',true);
		$this->SetX($left += 35); $this->Cell(55, $h, 'VOL', 1, 0, 'C',true);
		$this->SetX($left += 55); $this->Cell(55, $h, 'WEIGHT', 1, 0, 'C',true);
		$this->SetX($left += 55); $this->Cell(160, $h, 'ITEM DESCRIPTION', 1, 0, 'C',true);
		$this->SetX($left += 150); $this->Cell(110, $h, 'REMARK', 1, 1, 'C',true);
        }
		
	}
	
	function Footer()
{
    // Go to 1.5 cm from bottom
    $this->SetY(-20);
	//$this->Image('img/cargologo.jpg',15,890,40,40);
	$this->Image('img/cargologo.jpg',15,540,50,50);
	 $this->SetFont('Arial','',7);
	$this->SetX(320); $this->Cell(0, -10, '** THIS IS A COMPUTER GENERATED MANIFEST, NO SIGNATURE IS REQUIRED **', 0, 0,'L');
	$this->SetX(390); $this->Cell(0, 15, 'SOLUTION BY WWW.WEBIP.LK 0715118719', 0, 0,'L');
    // Select Arial italic 8
    $this->SetFont('Arial','',8);
	
    // Print centered page number
    $this->Cell(0,10,'Page '.$this->PageNo(),0,0,'R');
	
}
	
	public function rptDetailData () {
		
		
		//
		$border = 0;
		$this->AddPage();
		$this->SetAutoPageBreak(true,60);
		$this->AliasNbPages();
		$left = 350;
		$left1 = 50;
		
	
		$this->Ln(110);
		$this->SetFont("", "BU", 16);
		$this->SetX($left+50); $this->Cell(40, 15, 'CARGO MANIFEST', 0, 1,'L');
//$this->line(10);
//$this->SetFont("", "U", 12);
//$this->SetX($left); $this->Cell(70, 20, 'CARGO MANIFEST', 0, 1,'L');
$this->SetFont("", "", 11);
$this->SetX($left-320); $this->Cell(70, 20, 'SHIPPER :- SERENDIB FREIGHT SERVICES, P.O. BOX 1977, AJMAN, UNITED ARAB EMIRATES. TELE NO : +971 556948298 EMAIL : serendibuae@gmail ', 0, 0,'L');
$this->Ln(15);
//$this->SetFont("", "", 10);
//$this->SetX($left-100); $this->Cell(70, 20, 'Serandib Cargo & Goods Packaging, P.O. Box 1977, Ajman, United Arab Emirates. Tele No : +971 556948298 Email : serendibuae@gmail.com ', 0, 0,'L');
//$this->Ln(15);
$this->SetFont("", "", 11);
$this->SetX($left-320); $this->Cell(70, 20, 'CONSIGNEE :- '.strtoupper($dtt['agent_name'])." ".strtoupper($dtt['adrs']).". TEL: ".$dtt['phone1'], 0, 0,'L');
$this->Ln(15);
$this->SetFont("", "", 10);
$this->SetX($left-320); $this->Cell(70, 20, "DESCRIPTION OF GOODS: PERSONAL EFFECTS AND HOUSE HOLDS GOODS", 0, 1,'L');
		
//$this->SetX($left); $this->Cell(70, 20, $dtt['agent_name']." ".$dtt['adrs'].". Tele: ".$dtt['phone1'], 0, 1,'L');

$this->Ln(30);

$this->SetFont("", "B", 12);
	//$this->Cell(40,5,' ','LTR',0,'L',0);   // empty cell with left,top, and right borders
//$this->Cell(50,15,'Words Here',1,0,'L',0);
////$this->Cell(50,15,'Words Here',1,0,'L',0);
//$this->Cell(40,15,'Words Here','LR',1,'C',0);  // cell with left and right borders
$this->Cell(75,17,'DATE',1,0,'C', 0);
$this->Cell(145,17,'CONTAINER NO',1,0,'C',0);
$this->Cell(115,17,'VESSEL',1,0,'C',0);
$this->Cell(80,17,'VOYAGE',1,0,'C',0);

$this->Cell(80,17,'SEAL NO',1,0,'C',0);
$this->Cell(75,17,'ETA DATE',1,0,'C',0);
$this->Cell(75,17,'ETD DATE',1,0,'C',0);
//$this->Cell(100,17,'PAGES',1,0,'C',0);
$this->Cell(75,17,'TOT PKG.',1,0,'C',0);
$this->Cell(80,17,'TOT VOL.',1,0,'C',0);

$this->Cell(80,17,'TOT WT.',1,1,'C',0);
$this->Cell(75,17,$dt['ctr_date'],1,0,'C', 0);
$this->Cell(145,17,strtoupper($dt['container']),1,0,'C',0);
$this->Cell(115,17,strtoupper($dt['vessel']),1,0,'C',0);
$this->Cell(80,17,strtoupper($dt['voyage']),1,0,'C',0);

$this->Cell(80,17,$dt['seal_num'],1,0,'C',0);
$this->Cell(75,17,$dt['ETA'],1,0,'C',0);
$this->Cell(75,17,$dt['ETD'],1,0,'C',0);
//$this->Cell(100,17,'PAGES',1,0,'C',0);
$this->Cell(75,17,$dt['tot_pcs'],1,0,'C',0);
$this->Cell(80,17,$dt['tot_vol'],1,0,'C',0);

$this->Cell(80,17,$dt['tot_weight'],1,1,'C',0);


//$this->Ln(4);

		//$this->SetFont("", "", 9);
		//$this->SetX($left); $this->Cell(0, 20, 'Alamat: Jl. Purbalingga No.30 Sarilamak, Kecamatan Harau, Kabupaten Lima Puluh Kota', 0, 1,'C');
		//$this->SetFont("", "", 15);
		//$this->SetX($left); $this->Cell(0, 1, '________________________________________________________________', 0, 1,'C');
		//$this->SetX($left); $this->Cell(0, 2, '________________________________________________________________', 0, 1,'C');
		//$this->Ln(10);
		//$this->Ln(10);
		//$this->SetFont("", "B", 12);
		//$this->SetX($left); $this->Cell(0, 20, 'LAPORAN DATA PEGAWAI', 0, 1,'C');
		$this->Ln(7);
		
		$h = 18;
		$left = 40;
		$top = 80;	
		#tableheader
		$this->SetFillColor(200,200,200);	
		$left = $this->GetX();
		$this->SetFont("", "B", 12);
		$this->Cell(25,$h,'NO',1,0,'L',true);
		$this->SetX($left += 25); $this->Cell(50, $h, 'BL', 1, 0, 'C',true);
		$this->SetX($left += 50); $this->Cell(200, $h, 'SHIPPER', 1, 0, 'C',true);
		//$this->SetX($left += 150); $this->Cell(150, $h, 'NOTIFY PARTY', 1, 0, 'C',true);
		$this->SetX($left += 200); $this->Cell(200, $h, 'CONSIGNEE', 1, 0, 'C',true);		
		$this->SetX($left += 200); $this->Cell(35, $h, 'PKG', 1, 0, 'C',true);
		$this->SetX($left += 35); $this->Cell(55, $h, 'VOL', 1, 0, 'C',true);
		$this->SetX($left += 55); $this->Cell(55, $h, 'WEIGHT', 1, 0, 'C',true);
		$this->SetX($left += 55); $this->Cell(160, $h, 'ITEM DESCRIPTION', 1, 0, 'C',true);
		$this->SetX($left += 150); $this->Cell(110, $h, 'REMARK', 1, 1, 'C',true);

		//$this->Ln(20);
		
		$this->SetFont('Arial','',11);
		$this->SetWidths(array(25,50,200,200,35,55,55,150,110));
		$this->SetAligns(array('C','L','L','L','C','C','C','L'));
		//$this->SetFontStyle(array('B','B','B','B','B','B','B','B'));
		$no = 1; $this->SetFillColor(255);


		

		#data dari database
		
        $sqls = mysqli_query($con, "SELECT * FROM serendib WHERE deleted_by='' AND `bl_num` in (SELECT `bl_num` FROM `package` WHERE `man_id`='$man_id' and deleted='' GROUP BY `bl_num` ASC ) ORDER BY `bl_num` ASC");
		while ($data = mysqli_fetch_array($sqls)) 
        {
$nwbl = $data['bl_num'];
         //$repeat = mysqli_query($con, "SELECT COUNT(`pkg_id`) as tot,SUM(`volume`) as vol,SUM(`weight`) as we, `description` FROM package WHERE //`bl_num`='$nwbl' and `man_id`='$man_id' GROUP BY `description` ASC");
		 
		 $repeat = mysqli_query($con, "SELECT `pkg_id`,`pkg`,`volume`,`weight`, `description` FROM package WHERE `bl_num`='$nwbl' AND `deleted`!='yes' and `man_id`='$man_id'" );
	 $det = ""; $pkg = 0; $vl=0; $we =0;
          while ($rw = mysqli_fetch_array($repeat)) {
			$pkg+=$rw['pkg'];  $vl+=$rw['volume']; $we+=$rw['weight'];
						$det = $det.$rw['description'].", ";	
 			}
			
		if($data['delpoint']=="Colombo"){$del = "";}else{$del = $data['delpoint'];}
		
		if($data['totincmb']!="0.00"){ $rem = "Col Pay-".$data['totincmb']."\n".$data['remarks']."\n".$del."\n".$data['remain']; 
		}else{ $rem = $data['remarks']."\n".$del."\n".$data['remain'];}


		if($data['noty_name']!=""){
			$noty = $data['noty_name']."\n".$data['noty_add']."\n".$data['noty_mob'];
		}else{
			$noty = $data['c_fname']." ".$data['c_lname']."\n".$data['c_line1'].$data['c_line2'].$data['c_city']."\n".$data['c_mob']." / ".$data['c_fix'];
		}	
		
			
          $this->Row(

            array($no++,
            $data['bl_num'], 
            strtoupper($data['s_fname']." ".$data['s_lname']."\n".$data['s_line1']." ".$data['s_line2']." ".$data['s_city']."\n".$data['s_mob']), 
            //$noty, 
			strtoupper($data['c_fname']." ".$data['c_lname']."\n".$data['c_line1']." ".$data['c_line2']." ".$data['c_city']."\n".$data['c_mob']." / ".$data['c_fix']), 
            $pkg, 
			$vl,
			$we,
			$det,                                                                                            
            strtoupper($rem)
          ));
        }

		# untuk menuliskan nama bulan dengan format Indonesia
		$bln = array(
		  '01' => 'Januari',
		  '02' => 'Februari',
		  '03' => 'Maret',
		  '04' => 'April',
		  '05' => 'Mei',
		  '06' => 'Juni',
		  '07' => 'Juli',
		  '08' => 'Agustus',
		  '09' => 'September',
		  '10' => 'Oktober',
		  '11' => 'November',
		  '12' => 'Desember'
		);

		#tabel footer
		//$this->Ln(20);
		//$this->SetFont("", "", 9);
		//$this->SetX(400); $this->Cell(0, 15, 'Payakumbuh Barat, '.date('d').' '.$bln[date('m')].' '.date('Y').'', 0, 1,'C');
		//$this->SetX(400); $this->Cell(0, 15, 'DIREKTUR', 0, 1,'C');
		//$this->Ln(40);
		//$this->SetX(400); $this->Cell(0, 20, 'ARIE, S.Kom, M.Kom', 0, 1,'C');
	}

	public function printPDF () {
				
		if ($this->options['paper_size'] == "F4") {
			$a = 8.3 * 72; //1 inch = 72 pt
			$b = 13.0 * 72;
			$this->FPDF($this->options['orientation'], "pt", array($a,$b));
		} else {
			$this->FPDF($this->options['orientation'], "pt", $this->options['paper_size']);
		}
		
	    $this->SetAutoPageBreak(false);
	    $this->AliasNbPages();
	    $this->SetFont("helvetica", "B", 10);
	    //$this->AddPage();
		
		
		
		
		
	    $this->rptDetailData();
			    
	    $this->Output($this->options['filename'],$this->options['destinationfile']);
  	}
  	
  	private $widths;
	private $aligns;

	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}

	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}

	function Row($data)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		$h=15*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x,$y,$w,$h);
			//Print the text
			$this->MultiCell($w,15,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}
		//Go to the next line
		$this->Ln($h);
	}

	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			
			$this->AddPage($this->CurOrientation);
	
	}

	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}
} //end of class

//pilihan
$options = array(
	'filename' => '', //nama file penyimpanan, kosongkan jika output ke browser
	'destinationfile' => '', //I=inline browser (default), F=local file, D=download
	'paper_size'=>'F4',	//paper size: F4, A3, A4, A5, Letter, Legal
	'orientation'=>'L' //orientation: P=portrait, L=landscape
);

$tabel = new FPDF_AutoWrapTable($data, $options);
$tabel->printPDF();
?>