<?php


session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");

echo "<HTML><HEAD><TITLE>Cargo Manifest</TITLE>
	</HEAD>
	
	<body>";

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		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);
}					

echo "<table border=0 cellpadding=0 cellspacing=0 height='30px' width='850px'><col width='250px><col width='350px'><col width='250px'>
	<thead><tr ><th></th>
	<th align='center' style='border-bottom: 1px dashed black;'><span style='font-size: 20px; font-family: sans-serif;'>CARGO MANIFEST</span></th>
	<th></th></tr></thead>
	</table>";

$query = "select manifest_auto, vessel_id, voyage_number, date_format(date,'%d/%m/%Y'), Portofloading, PortofDischarge, BOL_type, localagent_code from manifest
		  where 1=1 ";	

if ($manifest_auto!='') 		$query .= " and manifest_auto = '$manifest_auto' ";
if ($billlading!='') 			$query .= " and BOL_identification = '$billlading' ";
if ($voyage_year!='') 			$query .= " and voyage_year = '$voyage_year' ";
if ($voyage_number!='') 		$query .= " and voyage_number = '$voyage_number' ";
if ($voyage_destination!='') 	$query .= " and voyage_destination = '$voyage_destination' ";
if ($bol_number!='') 			$query .= " and voyage_BOLnumber = '$bol_number' ";
if ($voyage_type!='') 			$query .= " and BOL_type = '$voyage_type' ";
if ($voyage_pagenumber!='') 	$query .= " and BOL_pagenumber = '$voyage_pagenumber' ";
if ($portofloading!='') 		$query .= " and Portofloading = '$portofloading' ";
if ($localagent_code!='') 		$query .= " and localagent_code = '$localagent_code' ";
if ($portofdischarge!='') 		$query .= " and PortofDischarge = '$portofdischarge' ";
 
if ($grpby == 1)				$query .= " order by Portofloading ";
else if ($grpby == 2) 			$query .= " order by BOL_type ";
else if ($grpby == 3) 			$query .= " order by localagent_code ";

$result = mysql_query($query, $connection);
$total = array();
$Sumtotal = array();

$total['container'] = 0;
$total['weight'] = 0;

$grpbytotal['container'] = 0;
$grpbytotal['weight'] = 0;

$Sumtotal['container'] = 0;
$Sumtotal['weight'] = 0;

$temp_grpby = '**'; 
$ct = 0;
while ($rw = mysql_fetch_array($result)){
	$manifest_auto = $rw[0];
	
	$total['container'] = 0;
	$total['weight'] = 0;
	if ($grpby == 1)		$groupby = $rw[4];
	else if ($grpby == 2) 	$groupby = $rw[6];
	else if ($grpby == 3) 	$groupby = $rw[7];
	
	
	if ($grpby != '' && ($temp_grpby != $groupby)){
		echo "<table border=0 cellpadding=0 cellspacing=0 width='850px'><col width='400px'><col width='450px'>";
		if($ct != 0) 
			echo "<tr height='30px'><td align='left' ><b>GROUP BY $groupby TOTAL CARGO MANIFEST </b></td>
					<td align='center'>".$grpbytotal['container']." CONTAINER(S) FULL &nbsp;&nbsp;&nbsp; ".number_format($grpbytotal['weight'],2)."</td></tr>";
	
		$grpbytotal['container'] = 0;
		$grpbytotal['weight'] = 0;
		echo "<tr height='30px' ><td colspan='2'></td></tr>
				<tr height='30px' style='background-color: lightgray;'><td colspan='2'><b>Group By : $groupby</b></td></tr>";
		echo "</table>";		
	}
	
	echo "<table border=0 cellpadding=0 cellspacing=0 width='850px'><col width='400px'><col width='450px'>";
	echo "<tr height='30px'><td>VESSEL: $rw[1] VOY. No.: $rw[2]</td><td>SAILING DATE: $rw[3]</td></tr>";
	echo "<tr height='30px'><td>LOADIN PORT: $rw[4]</td><td>PORT OF DISCHARGING: $rw[5]</td></tr>";
	echo "<tr><td colspan='2'><hr style='border-top: 1px dashed black;'></td></tr>";
	echo "</table>";
	
	echo "<table border=0 cellpadding=0 cellspacing=0 width='850px'><col width='150px'><col width='200px'><col width='400px'><col width='100px'>";
	echo "<tr><td align='center'>B/L NUMBERS</td><td align='center'>CONTAINER</td><td align='center'>MARK & DESCRIPTIONS</td><td align='center'>KILOS</td></tr>";
	echo "<tr><td colspan='4'><hr style='border-top: 1px dashed black;'></td></tr>";

	
	$querydt = "select manifestdt.BOL_identification, Container, Marks, descriptiongood, ifnull(replace(Container_Weight,',','.'),0), Container_Weight
				from manifestdt
				inner join manifestdesc on manifestdt.manifest_manifestauto=manifestdesc.manifest_manifestauto
				and  manifestdesc.detailsequence=manifestdt.detailsequence
				where manifestdt.manifest_manifestauto = $manifest_auto
				order by manifestdt.detailsequence ";
	$resdt = mysql_query($querydt, $connection);
	$temp_bol = '';
	while($rwdt = mysql_fetch_array($resdt)){
		
		echo "<tr><td align='center'>";
		if ($temp_bol != $rwdt[0])
			echo $rwdt[0];
		echo "</td><td align='center'>$rwdt[1]</td><td align='center'>$rwdt[2] $rwdt[3]</td><td align='center'>".$rwdt[5]."</td></tr>";
		
		if($rwdt[4] == '') $rwdt[4] = 0;
		
		$total['container'] = $total['container'] + 1;
		$total['weight'] = $total['weight'] + $rwdt[4];
		
		$grpbytotal['container'] = $grpbytotal['container'] + 1;
		$grpbytotal['weight'] = $grpbytotal['weight'] + $rwdt[4];
		
		$temp_bol = $rwdt[0];
	}	
	echo "<tr><td colspan='4'><hr style='border-top: 1px solid black;'></td></tr>";
	echo "<tr height='30px'><td align='left' colspan='2'>TOTAL CARGO MANIFEST $rw[4]</td><td align='center'>".$total['container']." CONTAINER(S) </td><td align='center'>".number_format($total['weight'],2)."</td></tr>";
	echo "<tr height='30px'><td colspan='4'><hr style='border-top: 2px double black;'></td></tr>";
	
	echo "</table>";
	$Sumtotal['container'] = $Sumtotal['container'] + $total['container'];
	$Sumtotal['weight'] = $Sumtotal['weight'] + $total['weight'];
	
	$temp_grpby = $groupby;
	$ct++;
}
	
echo "<table border=0 cellpadding=0 cellspacing=0 width='850px'><col width='150px'><col width='200px'><col width='400px'><col width='100px'>";
if($grpby != '')
	echo "<tr height='30px'><td align='left' colspan='2'><b>GROUP BY $groupby TOTAL CARGO MANIFEST </b></td><td align='center'>".$grpbytotal['container']." CONTAINER(S) </td><td align='center'>".number_format($grpbytotal['weight'],2)."</td></tr>";

echo "<tr ><td colspan='4'><hr style='border-top: 2px double black;'></td></tr>";
echo "<tr height='30px'><td align='left' colspan='2'><b>FINAL TOTAL</b></td><td align='center'>".$Sumtotal['container']." CONTAINER(S) </td><td align='center'>".number_format($Sumtotal['weight'],2)."</td></tr>";
	
echo "</table>";
print <<< HERE

<script language=javascript>
if (typeof(window.print) != 'undefined')
{
    window.print();
}
</script>
</BODY>
</HTML>
HERE;
?>
	


				 
				  
				  