<?php

session_start();

include("config.inc.php");

header("Content-type: text/html; charset=".$_SESSION['encodingmode']);

if($_SESSION['dbName']==""){
header("location:../index.php");
}






$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536)	or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, Setup_vatno, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, ".
			 " Company_Name, Company_NameF, Company_Memo, Company_MemoF, Company_Capital, Company_CapitalF, Header_color as HeaderColor, Background_Color as BackgroundColor ".
			 " from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
echo "<html></head><title>$crfttype Report</title>
	<style>
	.styleh1 {
		font-size: 24px;
		font-weight: bold;
	}
	.styleh2 {
		font-weight: bold;
		font-size: 17px;
	}
	
	</style>
	</head><body>";

$query = " select distinct reference, extra_info, consignee1, BL_number, Portofloading, manifest.vessel_id, voy_number, PortofDischarge, date_format(From_date, '%d/%m/%Y'), CRFT_Header.manifest_auto,
		   date_format(To_Date, '%d/%m/%Y')
		   from CRFT_Header
		   inner join manifest on manifest.manifest_auto = CRFT_Header.manifest_auto
		   left join containertrace on manifest_manifestauto = manifest.manifest_auto 
		   where crft_id = $crft_id ";	
$result = mysql_query($query, $connection);
$rw = mysql_fetch_row($result);		   
$ref 			= $rw[0];
$extra_info 	= $rw[1];
$consignee		= $rw[2];
$BL_number		= $rw[3];
$POL 			= $rw[4];
$vessel			= $rw[5];
$voy_number		= $rw[6];
$POD			= $rw[7];
$fromdate		= $rw[8];
$manifest_auto	= $rw[9];
$todate			= $rw[10];

//header
echo "<table border='0' cellpadding=0 cellspacing=0 width='800px'>";
echo "<tr height='30px'><td><span class='styleh1'><u>".strtoupper($Company_Name)."</u></span></td></tr>";
echo "<tr height='30px'><td align=right><span class='styleh2'>No. B.S.: &nbsp; $ref</span></td></tr>";
echo "<tr height='40px'><td align=right></td></tr>";
echo "<tr height='30px'><td align=left><span class='styleh3'>".nl2br($extra_info)."</span></td></tr>";
echo "<tr height='20px'><td align=right></td></tr>";
echo "</table>";

echo "<table border='0' cellpadding=0 cellspacing=0 width='800px'><col width='400px'><col width='400px'>";
echo "<tr height='30px'>
			<td colspan='1'><span class='styleh3'><b>MESSRS: </b>".$consignee."</span></td>
			<td> <span class='styleh3'><b>B/L No.:</b> ".$BL_number." - ".$POL." </span></td>
	  </tr>";
echo "<tr height='30px'>
			<td colspan='1'><span class='styleh3'><b>M/V: </b>".$vessel." VOY. ".$voy_number."</span></td>
			<td align='center'><span class='styleh3'><b>AT ".$POD." &nbsp;&nbsp;</b> ".$fromdate."</span></td>
	  </tr>";	 
echo "<tr><td colspan='2'><hr></td></tr>";	  
echo "</table>";

//detail

echo "<table border='0' cellpadding=0 cellspacing=0 width='800px'><col width='250px'><col width='550px'>";
$querydt = "select 'C',Container, if( ifnull(container_type,'') = '', type_id, container_type), detailsequence
			from manifestdt
			left join container on container.cntr_code = manifestdt.Container
			where manifestdt.manifest_manifestauto = $manifest_auto and (ifnull(ctrflag,'') <> '*' or detailtype = 1)
			and crft_id = $crft_id
			union
			select 'S', Container, null, detailsequence
			from manifestdt
			where manifestdt.manifest_manifestauto = $manifest_auto and detailtype = 2
			order by detailsequence ";
//echo $querydt;exit;
$resdt = mysql_query($querydt, $connection);
$i=1;
$containername = "";
$containertype = "";
$containersize = "";
while ($rwdt = mysql_fetch_array($resdt)){
	if ($rwdt[0] == 'C' && $i > 1){
		echo "<tr height='20px'>
				<td><span class='styleh4'>".$containername."</span></td>
				<td align='left'><span class='styleh4'>".$containersize."</span></td>
			</tr>";	
		$containername = "";
		$containersize = "";		
	}

	 $containername = $containername."&nbsp;&nbsp;&nbsp;&nbsp;".$rwdt[1];
	 $containersize = $containersize." ".$rwdt[2];
	$i++;
}

if ($containername != ''){
	echo "<tr height='20px'>
				<td><span class='styleh4'>".$containername."</span></td>
				<td align='left'><span class='styleh4'>".$containersize."</span></td>
		  </tr>";	
$i++;	  
}

for($j=32;$j>$i;$j--)
	echo "<tr height='20px'><td colspan='2'>&nbsp;</td></tr>";

echo "<tr height='30px'><td colspan='2'>&nbsp;</td></tr>";
echo "<tr height='20px'><td colspan='2'><span class='styleh2'>UP TO ".$todate."</span></td></tr>";
echo "<tr height='30px'><td colspan='2'>&nbsp;</td></tr>";
echo "</table>";

//footer
echo "<table border='0' cellpadding=1 cellspacing=0 width='800px'><col width='275px'><col width='150px'><col width='25px'><col width='150px'><col width='25px'><col width='150px'><col width='25px'>";
echo "<tr height='30px'>
			<td><span class='styleh2'>".strtoupper($Company_Name)."</span></td>
			<td align='center' style='border-bottom: 1px solid black;'><span class='styleh2'>Receiver</span></td><td></td>
			<td align='center' style='border-bottom: 1px solid black;'><span class='styleh2'>Office</span></td><td></td>
			<td align='center' style='border-bottom: 1px solid black;'><span class='styleh2'>Terminal</span></td><td></td>
	 </tr>";
echo "<tr height='30px'><td colspan='7'>&nbsp;</td></tr>";	
echo "<tr height='10px'><td colspan='7'>&nbsp;</td></tr>";	 
echo "<tr height='30px'>
			<td colspan='7'><span class='styleh2'>$crfttype RECEIVED IN GOOD CONDITION \nWITH SEAL(S) INTACT</span></td>
			
	 </tr>";	 
echo "</table>";
echo "
</body>
</html>";

print <<< HERE

<script language=javascript>

    window.print();

</script>
</BODY>
</HTML>
HERE;
?>