<?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>List of Container</TITLE>
	<style>
		#t1 th {
			font-size : 13px;
		}
		#t1 td {
			font-size : 13px;
		}
	</style>
	</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);
}					

if ($voyage_type != ''){
	$tempvtype = explode(',', $voyage_type);
	$voyage_type = '-1';
	foreach($tempvtype as $tempv)	
		$voyage_type .= ",'".$tempv."'";
}

if ($voyage_year!='' && $voyage_number != ''){
	$queryheader = "select vessel_id, BOL_type, manifest_auto, voyage_number,date_format(received_date,'%d/%m/%Y') from manifest where voyage_year = '$voyage_year' and voyage_number = '$voyage_number' "; 
		if ($manifest_auto!='') 		$queryheader .= " and manifest_auto = '$manifest_auto' ";
		if ($billlading!='') 			$queryheader .= " and BOL_identification = '$billlading' ";
		if ($voyage_destination!='') 	$queryheader .= " and voyage_destination = '$voyage_destination' ";
		if ($bol_number!='') 			$queryheader .= " and voyage_BOLnumber = '$bol_number' ";
		if ($voyage_type!='') 			$queryheader .= " and BOL_type in ($voyage_type) ";
		if ($voyage_pagenumber!='') 	$queryheader .= " and BOL_pagenumber = '$voyage_pagenumber' ";
		if ($portofloading!='') 		$queryheader .= " and Portofloading = '$portofloading' ";
		if ($portofdischarge!= '') 		$queryheader .= " and PortofDischarge='$portofdischarge' ";
		if ($localagent_code!='') 		$queryheader .= " and localagent_code = '$localagent_code' ";//echo $queryheader;
		if ($withempty == 1)			$queryheader .= " and manifest_emptyContainer = '$withempty' ";
	$queryheader .= " order by vessel_id, BOL_type ";	
		if ($vOrderby == '1') 			$queryheader .= " , localagent_code ";
} else 
	$queryheader = "select null, null, '$manifest_auto', null ";
//echo $queryheader;exit;
$tempvessel ="*";
$tempbol ="*";
$i=0;
$resheader = mysql_query($queryheader, $connection);
while ($rwheader = mysql_fetch_row($resheader)){
$vessel_id = $rwheader[0];
$receiveddate= $rwheader[4];
if ($rwheader[1] == 'I')
	$boltype = "IMPORT";
else if ($rwheader[1] == 'E')
	$boltype = "EXPORT"; 
else if ($rwheader[1] == 'S')
	$boltype = "TRANSHIPMENT"; 
Else if ($rwheader[1] == 'T')
	$boltype = "FREEZONE"; 
$manifest_auto = $rwheader[2];

$voynbr = $rwheader[3];

if ($tempvessel != $vessel_id && $tempbol != $boltype){
		
		if ($i>0){	
						
			echo "<tr><td colspan='10'><hr></td></tr>";
						
			$querytype = "select distinct substr(type_id, 1,2)
						from container
						order by type_id";
			$i = 0;			

			$restype = mysql_query($querytype, $connection);
			 while ($rwtype = mysql_fetch_array($restype)){
				if ($sumContainer['F'][$rwtype[0]] != 0){
					
					$sumWeight[$cntr_type1][$rwtype[0]] = number_format($sumWeight[$cntr_type1][$rwtype[0]],3);	
					if ($i == 0) echo "<tr height='20px'><td></td><td><b>FULL: </b></td>";
					if ($i > 0){
						echo "<tr><td></td><td></td>";
					}
					echo "<td colspan='2'> <b>".$sumContainer['F'][$rwtype[0]]." X ".$rwtype[0]."'</b></td><td></td>
						  <td colspan='6'> <b>".$sumWeight['F'][$rwtype[0]]." Kgs</b></td>";
					echo "</tr>";	  
					$i++;
				}

			}

			$i = 0;
			$restype = mysql_query($querytype, $connection);
			 while ($rwtype = mysql_fetch_array($restype)){
				if ($sumContainer['E'][$rwtype[0]] != 0){
					
					$sumWeight[$cntr_type1][$rwtype[0]] = number_format($sumWeight[$cntr_type1][$rwtype[0]],3);	
					if ($i == 0) echo "<tr height='20px'><td></td><td><b>Empty: </b></td>";
					if ($i > 0){
						echo "<tr><td></td><td></td>";
					}
					echo "<td colspan='2'> <b>".$sumContainer['E'][$rwtype[0]]." X ".$rwtype[0]."'</b></td><td></td>
						  <td colspan='6'> <b>".$sumWeight['E'][$rwtype[0]]." Kgs</b></td>";
					echo "</tr>";	  
					$i++;
				}

			}	
			echo "<tr height='15px'><td colspan='10'></td></tr>";

			$Sumtotal['weight'] = number_format($Sumtotal['weight'],3);				
			echo "<tr height='30px' style='background-color: lightgray;'><td></td><td><b>TOTAL: <b/></td>
						<td colspan='2'><b>".$Sumtotal['container']." Containers</b></td>
						<td></td>
						<td colspan='5'><b>".$Sumtotal['weight']." Kgs</td>
				  </tr>";	
			echo "</table>";
			echo "<div style='page-break-after: always;'></div>";
			unset($sumContainer);
			unset($sumWeight);
			unset($Sumtotal);
			$i=0;
		}
		if ($rwheader[1] =='E')  { $reportname="LOAD LIST" ;} else {$reportname="DISCHARGE LIST";} 
		echo "<table border=0 cellpadding=0 cellspacing=0 height='30px' width='850px'><col width='300px><col width='350px'><col width='200px'>
			<thead><tr ><th>".($voynbr != ''?"VOY NO. $voynbr &nbsp;&nbsp; $vessel_id":'')."</th>
				<th align='center' style='border-bottom: 1px dashed black;'><span style='font-size: 18px; font-family: sans-serif;'>$reportname</span></th>
				<th>$boltype  $receiveddate</th></tr></thead>
			</table>";
		
		echo "<table id = 't1' border=0 cellpadding=0 cellspacing=0 height='30px' width='900px'><col width='100px'><col width='80px'><col width='50px'><col width='110px'>
						<col width='100px'><col width='70px'><col width='90px'><col width='150px'><col width='40px'><col width='120px'>";
		echo "<tr height='30px' ><td colspan='10'></td></tr>";
		echo "<tr height='30px' ><th>Container</th><th>Size/Type</th><th>F/E</th><th>POL</th><th>Dest.</th><th>GWT(kg)<br>(TARE)</th><th>Line</th><th>EX M/V</th><th>Voy. No.</th><th>Remark</th></tr>";	
		echo "<tr height='30px' ><td colspan='10'><hr style='border-top: 1px solid black;'></td></tr>";		
	}


	

$query = "select container, type_id, manifestdt.voyage_destination,ifnull(replace(Container_Weight,',',''),0), line_operator, manifestdt.vessel_id, 
		date_format(date,'%d/%m/%Y'),bctc_remark,Portofloading, manifestdt.BOL_type, localagent_code, agent.agent1, if(ifnull(manifest_emptyContainer,0) = '1', 'E','F'), substr(type_id, 1,2) as cntr_type,
		manifestdt.voyage_number, Container_Weight, if(substr(type_id, 1,2)=20, 2200, if(substr(type_id, 1,2)>=40, 4000,'')) as tare, agent2.agent1
		from manifestdt
		inner join manifest on manifest.manifest_auto = manifestdt.manifest_manifestauto
		inner join manifestdesc on manifestdt.manifest_manifestauto=manifestdesc.manifest_manifestauto and  manifestdesc.detailsequence=manifestdt.detailsequence
		left join container on cntr_code = manifestdt.container
		left join agent on agent.code = manifest.localagent_code
		left join agent as agent2  on agent2.code = manifest.containeragent_code
		where ifnull(CTRFlag,'') != '*' ";	

if ($manifest_auto!='') 		$query .= " and manifest.manifest_auto = '$manifest_auto' ";
if ($billlading!='') 			$query .= " and manifestdt.BOL_identification = '$billlading' ";
if ($voyage_year!='') 			$query .= " and manifestdt.voyage_year = '$voyage_year' ";
if ($voyage_number!='') 		$query .= " and manifestdt.voyage_number = '$voyage_number' ";
if ($voyage_destination!='') 	$query .= " and manifestdt.voyage_destination = '$voyage_destination' ";
if ($bol_number!='') 			$query .= " and manifestdt.voyage_BOLnumber = '$bol_number' ";
if ($voyage_type!='') 			$query .= " and manifestdt.BOL_type in ($voyage_type) ";
if ($voyage_pagenumber!='') 	$query .= " and manifestdt.BOL_pagenumber = '$voyage_pagenumber' ";
if ($portofloading!='') 		$query .= " and Portofloading = '$portofloading' ";
if ($portofdischarge!= '') 		$query .= " and PortofDischarge='$portofdischarge' ";
if ($localagent_code!='') 		$query .= " and manifest.localagent_code = '$localagent_code' ";
 
if ($cntrtype == '1')			$query .= " and ifnull(manifest_emptyContainer,0) = '0' ";
else if ($cntrtype == '2')		$query .= " and ifnull(manifest_emptyContainer,0) = '1' ";

if ($vOrderby == '1') 			$query .= " order by manifest.localagent_code ";
else {
	if ($grpby == 1)			$query .= " order by Portofloading ";
	else if ($grpby == 2) 		$query .= " order by manifestdt.BOL_type ";
	else if ($grpby == 3) 		$query .= " order by localagent_code ";
	else				 		$query .= " order by container ";
}
//echo $query;exit;
$result = mysql_query($query, $connection);
$total = array();
$Sumtotal = array();

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

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

$temp_grpby = '**'; 
$groupby = '';
$ct = 0;
while ($rw = mysql_fetch_array($result)){
	
	if ($grpby == 1)		$groupby = $rw[8];
	else if ($grpby == 2) 	$groupby = $rw[9];
	else if ($grpby == 3) 	$groupby = $rw[10];
	
	if ($grpby != '' && ($temp_grpby != $groupby)){
		/* if($ct != 0) 
			echo "<tr height='30px'><td align='left' ><b>GROUP BY $groupby TOTAL </b></td>
					<td align='center'>".$grpbytotal['container']." CONTAINER(S) FULL &nbsp;&nbsp;&nbsp; ".number_format($grpbytotal['weight'],2)." Kgs</td></tr>";
	 */
		$grpbytotal['container'] = 0;
		$grpbytotal['weight'] = 0;
		echo "<tr height='30px' ><td colspan='10'></td></tr>
				<tr height='30px' style='background-color: lightgray;'><td colspan='10'><b>Group By : $groupby</b></td></tr>";
	}
	if ($rw[12] == 'E')
		$tareWeight = $rw[16];
	else $tareWeight = $rw[15];
	
	echo "<tr height='20px' ><td>$rw[0]</td><td align='center'>$rw[1]</td><td align='center'>$rw[12]</td><td align='center'>$rw[8]</td><td align='center'>$rw[2]</td>
			<td>$tareWeight</td><td align='center'>$rw[17]</td><td align='center'>$rw[5] $rw[6]</td><td align='center'>$rw[14]</td>
			<td align='center'>$rw[7]</td></tr>";	
	
	$temp_grpby = $groupby;
	$grpbytotal['container'] = $grpbytotal['container'] + 1;
	$grpbytotal['weight'] = $grpbytotal['weight'] + $rw[3];
	
	$cntr_type1 = $rw[12];
	$cntr_type2 = $rw[13];
	
	$sumContainer[$cntr_type1][$cntr_type2] = $sumContainer[$cntr_type1][$cntr_type2] + 1;
	$sumWeight[$cntr_type1][$cntr_type2] = $sumWeight[$cntr_type1][$cntr_type2] + ($rw[12] == 'E'?$rw[16]:$rw[3]);
	
	$Sumtotal['container'] = $Sumtotal['container'] + 1;
	$Sumtotal['weight'] = $Sumtotal['weight'] + ($rw[12] == 'E'?$rw[16]:$rw[3]);
	
	$ct++;
}

/* if ($grpby != '')
	echo "<tr height='30px'><td align='left' ><b>GROUP BY $groupby TOTAL </b></td>
			<td align='center'>".$grpbytotal['container']." CONTAINER(S) FULL &nbsp;&nbsp;&nbsp; ".number_format($grpbytotal['weight'],2)." Kgs</td></tr>"; */



	
	$tempvessel = $vessel_id; 
	$tempbol = $boltype;
	$i++;
}

echo "<tr><td colspan='10'><hr></td></tr>";
			
$querytype = "select distinct substr(type_id, 1,2)
			from container
			order by type_id";
$i = 0;			

$restype = mysql_query($querytype, $connection);
 while ($rwtype = mysql_fetch_array($restype)){
	if ($sumContainer['F'][$rwtype[0]] != 0){
		
		$sumWeight[$cntr_type1][$rwtype[0]] = number_format($sumWeight[$cntr_type1][$rwtype[0]],3);	
		if ($i == 0) echo "<tr height='20px'><td></td><td><b>FULL: </b></td>";
		if ($i > 0){
			echo "<tr><td></td><td></td>";
		}
		echo "<td colspan='2'> <b>".$sumContainer['F'][$rwtype[0]]." X ".$rwtype[0]."'</b></td><td></td>
			  <td colspan='6'> <b>".$sumWeight['F'][$rwtype[0]]." Kgs</b></td>";
		echo "</tr>";	  
		$i++;
	}

}

$i = 0;
$restype = mysql_query($querytype, $connection);
 while ($rwtype = mysql_fetch_array($restype)){
	if ($sumContainer['E'][$rwtype[0]] != 0){
		
		$sumWeight[$cntr_type1][$rwtype[0]] = number_format($sumWeight[$cntr_type1][$rwtype[0]],3);	
		if ($i == 0) echo "<tr height='20px'><td></td><td><b>Empty: </b></td>";
		if ($i > 0){
			echo "<tr><td></td><td></td>";
		}
		echo "<td colspan='2'> <b>".$sumContainer['E'][$rwtype[0]]." X ".$rwtype[0]."'</b></td><td></td>
			  <td colspan='6'> <b>".$sumWeight['E'][$rwtype[0]]." Kgs</b></td>";
		echo "</tr>";	  
		$i++;
	}

}	
echo "<tr height='15px'><td colspan='10'></td></tr>";

$Sumtotal['weight'] = number_format($Sumtotal['weight'],3);				
echo "<tr height='30px' style='background-color: lightgray;'><td></td><td><b>TOTAL: <b/></td>
			<td colspan='2'><b>".$Sumtotal['container']." Containers</b></td>
			<td></td>
			<td colspan='5'><b>".$Sumtotal['weight']." Kgs</td>
	  </tr>";	
echo "</table>";
print <<< HERE

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


				 
				  
				  