<?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, $compname, $pagenbr, $connection);

$tempagent = '';
$numrows = 6;

$topdash = "border-top: 1px dashed black";
$bottomdash = "border-bottom: 1px dashed black";

$bright = "border-right: 1px  solid black";
$bleft = "border-left: 1px  solid black";

$totalWeight = 0;
echo "<table border=0 cellpadding=0 cellspacing=0 width='880px'>
		<col width='230px'><col width='140px'><col width='120px'><col width='120px'><col width='110px'><col width='160px'>"; 
echo "<tr height='30px'>
			<td align='center' style='$bright;$bleft;$topdash;$bottomdash;'>CONTENEUR ET No. SEAL</td>
			<td align='center' style='$bright;$topdash;$bottomdash;'>No. B/L</td>
			<td align='center' style='$bright;$topdash;$bottomdash;'>FEET</td>
			<td align='center' style='$bright;$topdash;$bottomdash;'>G/F/E</td>
			<td align='center' style='$bright;$topdash;$bottomdash;'>TONNAGE</td>
			<td align='center' style='$bright;$topdash;$bottomdash;'>PORT</td>
	  </tr>";
$querydt = "select Container, manifestdt.BOL_identification, type_id, Container_Weight, ifnull(replace(Container_Weight,',','.'),0),
			PortofDischarge, detailsequence, manifest_manifestauto, localagent_code
			from manifestdt
			left join container on container.cntr_code = manifestdt.container
			inner join manifest on manifest.manifest_auto = manifest_manifestauto
			where manifest.Voyage_year=$voyage_year and manifest.voyage_number=$voyage_number and manifest.BOL_type = '$voyage_type'
			and ifnull(CTRFlag,1) <> '*'
			order by manifestdt.container; "; 
$resdt = mysql_query($querydt, $connection);
$i=0;
while($rwdt = mysql_fetch_array($resdt)){
	
	if ($rwdt[8] != $tempagent && $i!= 0){
		echo "<tr height='30px'>
				<td style='$bright;$bleft;$topdash;'></td><td style='$bright;$topdash;'></td><td style='$bright;$topdash;'></td>
				<td style='$bright;$topdash;'></td><td style='$bright;$topdash;'></td><td style='$bright;$topdash;'></td>
			</tr>";
		$rescont = getContainer($voyage_year, $voyage_number, $voyage_type, $tempagent, 0, $connection);
		while ($rwcont = mysql_fetch_array($rescont)){
			echo "<tr height='30px'>
					<td colspan='5'></td><td colspan='5'>TOTAL</td>
				</tr>";
		}
		echo "<tr height='30px'>
					<td align='center' style='$bottomdash;' colspan='6'></td>
			</tr>";
		echo "</table>";
		echo "<div style='page-break-after: always;'>&nbsp;</div>";
		$pagenbr++;
		rep_header($voyage_year, $voyage_number, $pagenbr, $connection);
		echo "<table border=0 cellpadding=0 cellspacing=0 width='880px'>
		<col width='230px'><col width='140px'><col width='120px'><col width='120px'><col width='110px'><col width='160px'>"; 
		echo "<tr height='30px'>
					<td align='center' style='$bright;$bleft;$topdash;$bottomdash;'>CONTENEUR ET No. SEAL</td>
					<td align='center' style='$bright;$topdash;$bottomdash;'>No. B/L</td>
					<td align='center' style='$bright;$topdash;$bottomdash;'>FEET</td>
					<td align='center' style='$bright;$topdash;$bottomdash;'>G/F/E</td>
					<td align='center' style='$bright;$topdash;$bottomdash;'>TONNAGE</td>
					<td align='center' style='$bright;$topdash;$bottomdash;'>PORT</td>
			  </tr>";
	}
	
	echo "<tr height='30px'>
					<td align='center' style='$bright;$bleft;'>$rwdt[0]</td>
					<td align='center' style='$bright;'>$rwdt[1]</td>
					<td align='center' style='$bright;'>$rwdt[2]</td>
					<td align='center' style='$bright;'>G/F/E</td>
					<td align='center' style='$bright;'>$rwdt[3]</td>
					<td align='center' style='$bright;'>$rwdt[5]</td>
		</tr>";
	$totalWeight = $totalWeight + $rwdt[4];	
	$numrows++;
	$tempagent = $rwdt[8];
	$i++;
}
echo "</table>"; 


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

function chkRows($numrows, $voyage_year, $voyage_number, $pagenbr, $connection){
	if (!($numrows%44)){
		echo "</table>";
		echo "<div style='page-break-after: always;'>&nbsp;</div>";
		rep_header($voyage_year, $voyage_number, $pagenbr, $connection);
		 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%'>"; 
	 
	} 
	return $pagenbr;
}
function getContainer($voyage_year, $voyage_number, $voyage_type, $tempagent, $final, $connection){
	$querycont = "select substr(type_id, 1,2), count(container)
				from manifestdt
				left join container on container.cntr_code = manifestdt.container
				inner join manifest on manifest.manifest_auto = manifest_manifestauto
				where manifest.Voyage_year=$voyage_year and manifest.voyage_number=$voyage_number
				and manifest.BOL_type = '$voyage_type' and if('$final' = '1', 1=1, ifnull(localagent_code,'') = '$tempagent')
				and ifnull(CTRFlag,1) <> '*'
				group by substr(type_id, 1,2);";
	$rescont = mysql_query($querycont, $connection);
	return $rescont;
}
function rep_header($voyage_year, $voyage_number, $voyage_type, $compname, $pagenbr, $connection){
	
	$query = "select manifest_auto, vessel_id, date_format(received_date, '%d/%M/%Y'), PortofDischarge
			  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 "<table border=0 cellpadding=0 cellspacing=0 width='880px'><col width='150px'><col width='580px'><col width='150px'>";
	echo "<tr><td colspan='2'> <u>$compname</u> </td><td>PAGE $pagenbr</td></tr>";
	echo "<tr height='30px'><td colspan='3' align='center'><u>CONTENEURS DEBARQUES</u></td></tr>";
	echo "<tr><td>Sequence: </td><td colspan='2'>($voyage_type) $row[1] VOY. $voyage_number</td></tr>";
	echo "<tr><td>Port de debarque: </td><td colspan='2'>$row[3]</td></tr>";
	echo "<tr><td>Arrive Le: </td><td colspan='2'>$row[2]</td></tr>";
	echo "</table>";
		
}	
print <<< HERE

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


				 
				  
				  