<?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>Manifest Report</TITLE>
	</HEAD>
	
	<body>";

$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);
}					
$querycomp = "select upper(company_name) from setup ";
$rescomp = mysql_query($querycomp, $connection);
$rwcomp = mysql_fetch_row($rescomp);
$compname = $rwcomp[0];

$pagenbr = 1;

rep_header($voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection);

$tempport = '';
$tempbol = '';
$numrows = 7;
$totweight = 0;



 echo "<table border=0 cellpadding=0 cellspacing=0 width='100%'>
		<col width='2%'><col width='7%'><col width='17%'><col width='17%'>
		<col width='3%'><col width='5%'><col width='16%'><col width='5%'><col width='28%'>"; 

$querydt = "select manifest.manifest_auto, PortofDischarge, agent.agent1 as agentname, manifest.BOL_identification as BOL_id,
			Container, packages, Container_Weight, if(ifnull(CTRFlag,1)=1,'1','') as CTRFlag, descriptiongood, 
			if(ifnull(CTRFlag,1) = 1, 'Container', '') as conttype, 
			if(if ( ifnull(type_id,'') = '', substr(container_type, 1,1),substr(type_id, 1,1))=2, 2200, if(if ( ifnull(type_id,'') = '', substr(container_type, 1,1),substr(type_id, 1,1))=4, 4000,'')) as tare,
			ifnull(replace(Container_Weight,',',''),0) weightnbr, ifnull(manifest_emptyContainer,0) as emptyContainer, Portofloading
			from manifestdt
			inner join manifest on manifest.manifest_auto = manifestdt.manifest_manifestauto
			left join agent on agent.code = manifest.localagent_code
			inner join manifestdesc on manifestdesc.manifest_manifestauto = manifest.manifest_auto and manifestdesc.detailsequence = manifestdt.detailsequence
			left join container on container.cntr_code = manifestdt.container
			where manifest.voyage_number=$voyage_number and manifest.voyage_year=$voyage_year and manifest.bol_type='$voyage_type'
			order by manifest.date, if(manifest.BOL_type='E',PortofDischarge,Portofloading) ";
if ($vOrderby == '1') 
	$querydt .= " , manifest.localagent_code ";
$querydt .= " , manifestdt.detailsequence, manifest.manifest_auto ";//echo $querydt;
$resdt = mysql_query($querydt, $connection);
while($rwdt = mysql_fetch_array($resdt)){
	extract($rwdt);
	
	if ($voyage_type == 'E')
		$groupByPort = $PortofDischarge;
	else $groupByPort = $Portofloading;

	if ($tempport != $groupByPort){
		echo "<tr align='left'>
				<td></td><td  colspan='8' style='font-size:13px'>$groupByPort</td><td></td>
			</tr>";
		$numrows++;	
		$pagenbr = returnPageNbr($numrows, $pagenbr);
		chkRows($numrows, $voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection);
	}
	if ($tempbol != $BOL_id){
		echo "<tr align='left'>
				<td></td><td style='font-size:13px'>$BOL_id</td><td colspan='7' style='font-size:13px'>$agentname</td>
			 </tr>";
		$numrows++;	
		$pagenbr = returnPageNbr($numrows, $pagenbr);
		chkRows($numrows, $voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection);
	}
	if($CTRFlag == 1){
		echo "<tr>
			<td></td><td colspan='8'>&nbsp;</td>
		 </tr>";
		$numrows++;
		$pagenbr = returnPageNbr($numrows, $pagenbr);
		chkRows($numrows, $voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection);
	} 
	if ($emptyContainer == 1){
		$Container_Weight = $tare;
		$weightnbr		  = $tare;
		$tare = '';
	}
	echo "<tr align='left'><td></td>
			<td></td><td></td>
			<td style='font-size:13px'>$Container</td><td style='font-size:13px'>$CTRFlag</td>
			<td style='font-size:13px'>$conttype</td><td style='font-size:13px'>$descriptiongood</td>
			<td style='font-size:13px'>$Container_Weight</td><td style='font-size:13px'>";
	if ($tare != '') echo "TARE $tare";
	echo "	</td>
		 </tr>";
	if ($weightnbr == '') $weightnbr = 0;
	$totweight = $totweight + $weightnbr;	 
	$numrows++;
	$pagenbr = returnPageNbr($numrows, $pagenbr);
	chkRows($numrows, $voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection);
	 
	$tempport = $groupByPort;
	$tempbol = $BOL_id;
}
echo "</table>";
echo "<div style='page-break-after: always;'>&nbsp;</div>";
$pagenbr++;
rep_header($voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection);

$querytot = "select substr(type_id, 1,2), count(cntr_code), sum(ifnull(Container_Weight,0)), contdesc
			from(
				select if(ifnull(type_id,'') = '', substr(container_type, 1,2),substr(type_id, 1,2)) as type_id, manifestdt.container as cntr_code, 
				if(ifnull(manifest_emptyContainer,0) = 1,if( if(ifnull(type_id,'') = '', substr(container_type, 1,1),substr(type_id, 1,1))=2, 2200, if(if(ifnull(type_id,'') = '', substr(container_type, 1,1),substr(type_id, 1,1))=4, 4000,'')),
				ifnull(replace(Container_Weight,',',''),0)) as Container_Weight,
				if(ifnull(manifest_emptyContainer,0) = 1,'EMPTY','FULL') as contdesc
				from manifestdt
				inner join manifest on manifest.manifest_auto = manifestdt.manifest_manifestauto
				left join container on container.cntr_code = manifestdt.container
				inner join manifestdesc on manifestdesc.manifest_manifestauto = manifestdt.manifest_manifestauto
				and manifestdesc.detailsequence = manifestdt.detailsequence
				where manifest.voyage_number=$voyage_number and manifest.voyage_year=$voyage_year and manifest.bol_type='$voyage_type'
				and (ifnull(ctrflag,'') = '' or detailtype=1)
			) as Q
			group by substr(type_id, 1,2), contdesc; "; //echo $querytot;
$restot = mysql_query($querytot, $connection);			

$totalCont = 0;
$totalWeight = 0;
echo "<table border=0 cellpadding=0 cellspacing=0 width='100%'>
		<col width='8%'><col width='17%'><col width='18%'>
		<col width='3%'><col width='5%'><col width='22%'><col width='5%'><col width='22%'>"; 
while ($rwtot = mysql_fetch_row($restot)){
	echo "<tr>
			<td></td><td></td>
			<td></td><td>".$rwtot[1]."</td>
			<td colspan='2' style='font-size:13px'>Container(s) $rwtot[0]' $rwtot[3]</td>
			<td style='font-size:13px'>".number_format($rwtot[2],2)."</td><td></td>
		 </tr>";
	$totalCont +=  $rwtot[1];
	//$totalWeight +=  $rwtot[2];
}	

echo "<tr align='30px'>
		<td></td><td colspan='2'> &nbsp;</td>
		<td valign='bottom' style='border-bottom: 1px dashed black;'></td>
		<td colspan='2'></td>
		<td valign='bottom' style='border-bottom: 1px dashed black;'></td><td></td>
	 </tr>";
echo "<tr align='30px'>
		<td></td><td colspan='2'> &nbsp;</td>
		<td ></td>
		<td colspan='2'></td>
		<td ></td><td></td>
	 </tr>";	
$queryalpha = "SELECT Alpha_NumeralOnly ('ENGLISH', $totalCont) As AlphaAmount,  Alpha_NumeralOnly ('ENGLISH', $totweight) As AlphaAmount2 FROM Dual";	 
$resalpha = mysql_query($queryalpha, $connection);
$rowalpha = mysql_fetch_row($resalpha);

echo "<tr align='30px'>
		<td></td><td colspan='2' style='font-size:13px'>SAY: NBRE OF CONTAINERS: $rowalpha[0]</td>
		<td>$totalCont</td>
		<td colspan='2'></td>
		<td style='font-size:13px'>".number_format($totweight,2)." </td><td></td>
	 </tr>";
	 
$query = "select issued, date_format(date, '%d %M %Y'),date_format(received_date, '%d %M %Y')
		  from manifest
		  where voyage_number=$voyage_number and voyage_year=$voyage_year and bol_type='$voyage_type'
		  limit 1 ";
$result = mysql_query($query, $connection);
$row = mysql_fetch_row($result);

echo "<tr align='30px'>
		<td></td><td colspan='2'>&nbsp;</td>
		<td></td>
		<td colspan='2'></td>
		<td></td><td></td>
	 </tr>";	 
echo "<tr align='30px'>
		<td></td><td colspan='2' style='font-size:13px'>SAY: TOAL KILOS: $rowalpha[1]</td>
		<td></td>
		<td colspan='2' style='font-size:13px'>$compname</td>
		<td></td><td></td>
	 </tr>";	
echo "<tr align='30px'>
		<td></td><td colspan='2'></td>
		<td></td>
		<td colspan='2' style='font-size:13px'>$row[0]</td>
		<td></td><td></td>
	 </tr>";	
echo "<tr align='30px'>
		<td></td><td colspan='2'></td>
		<td></td>
		<td colspan='2' style='font-size:13px'>$row[1]</td>
		<td></td><td></td>
	 </tr>";		 
echo "</table>";	 

function returnPageNbr($numrows, $pagenbr){
	if (!($numrows%48)){
		$pagenbr++;
		
	}
	return $pagenbr;
}

function chkRows($numrows, $voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection){
	if (!($numrows%48)){
		echo "</table>";
		echo "<div style='page-break-after: always;'></div>";
		
		rep_header($voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection);
		
		echo "<table border=0 cellpadding=0 cellspacing=0 width='100%'>
		<col width='2%'><col width='7%'><col width='17%'><col width='17%'>
		<col width='3%'><col width='5%'><col width='16%'><col width='5%'><col width='28%'>"; 
		if ($pagenbr > 1) echo "<tr height='5px'><td></td></tr>";
	} 
	return $pagenbr;
}
function rep_header($voyage_year, $voyage_number, $voyage_type, $pagenbr, $connection){
	
	$query = "select manifest_auto, issued, vessel_id, date_format(date, '%d %M %Y'),date_format(received_date, '%d %M %Y')
			  from manifest
			  where voyage_number=$voyage_number and voyage_year=$voyage_year and bol_type='$voyage_type'
			  limit 1 "; //echo $query;
	$result = mysql_query($query, $connection);
	$row = mysql_fetch_row($result);
	
	$queryvess = "select vessel_flag from vessel where vessel_description = '$row[2]' ";
	$resvess = mysql_query($queryvess, $connection);
	$rwvess = mysql_fetch_row($resvess);
	
	echo "<table border=0 cellpadding=0 cellspacing=0 width='100%'><col width='25%'><col width='25%'><col width='22%'><col width='28%'>";
	if ($pagenbr > 1) echo "<tr height='10px'><td></td></tr>";
	echo "<tr height='50px'><td colspan='4'>&nbsp;</td></tr>";
	echo "<tr><td></td><td></td><td>$row[1] &nbsp;&nbsp;&nbsp; </td><td>PAGE $pagenbr</td></tr>";
	echo "<tr><td colspan='4'>&nbsp;</td></tr>";
	
	echo "</table>";
	
	echo "<table border=0 cellpadding=0 cellspacing=0 width='100%'>
		<col width='3%'><col width='20%'><col width='11%'><col width='13%'><col width='17%'><col width='35%'>";
	echo "<tr height='18px'><td></td><td colspan='5'>&nbsp;</td></tr>";
	echo "<tr><td></td><td>$row[2] VOY. $voyage_number</td><td>$row[4]</td><td>$rwvess[0]</td><td>GHARIB SHIPPING AGENCY</td><td></td></tr>";
	echo "<tr height='40px'><td></td><td colspan='5'>&nbsp;</td></tr>";
	echo "<tr height='20px'><td></td><td colspan='5'>&nbsp;</td></tr>";
	echo "</table>";

}	
print <<< HERE

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


				 
				  
				  