<?php

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

$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);
}


print <<<HERE
<html>
<head><title>Taahod</title></head>
<style>
.header1 {	
	  font-size: 18px;
}

.header2 {	
	  font-size: 16px;
}

@media print 
{
   @page
   {
    size: landscape;
  }
}
</style>
<body>

HERE;

$query = "select company_name from setup";
$res = mysql_query($query, $connection);
$rw = mysql_fetch_row($res);
$compname = $rw[0];

$queryheader = "select distinct vessel_id, date_format(received_date,'%d/%m/%Y'), Portofloading from manifest where voyage_year = '$voyage_year' and voyage_number = '$voyage_number' and BOL_type = '$voyage_type' ";
$resheader   = mysql_query($queryheader, $connection);
$rowheader   = mysql_fetch_row($resheader);
$vessel  	 = $rowheader[0];
$manfdate  	 = $rowheader[1];
$pol	  	 = $rowheader[2];

if ($voyage_type=='I') $voyagetype="Import";
if ($voyage_type=='E') $voyagetype="Export";
if ($voyage_type=='T') $voyagetype="FreeZone";
if ($voyage_type=='S') $voyagetype="Transhipment";


$bottom = "border-bottom: 1px solid black";
$top    = "border-top: 1px solid black";
$right  = "border-right: 1px solid black";

$pagenb = 1;

rep_header($vessel, $voyage_number,$manfdate, $pol, $pagenb, $compname); 

$linenbr = 5;

$query = "select manifestdt.container as container, ifnull(type_id,container_type) as cntr_size
			from manifestdt
			inner join manifest on manifest.Manifest_auto = manifestdt.manifest_manifestauto
			left join container on container.cntr_code = manifestdt.container
			where manifest.voyage_year = '$voyage_year' and manifest.voyage_number = '$voyage_number' and manifest.BOL_type = 'E'
			and (ifnull(ctrflag,'') = '' or detailtype=1) ";
if ($localagent_code != '')
	$query .= " and 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' ";		
$query .= "	order by container";
		

$result = mysql_query($query, $connection);
echo "<table cellpadding=0 cellspacing=0 width=100%>
			<col width=3%><col width=15%><col width=7%><col width=20%><col width=10%>
			<col width=10%><col width=5%><col width=10%><col width=10%><col width=10%>";
$i=1;	
$cntr_type = array();		
while ($row = mysql_fetch_array($result)){
	$linenbr++;
	
	if (!($linenbr%20)){
		echo "</table>";
		echo "<div style='page-break-after:always'></div>";
		$pagenb++;
		rep_header($vessel, $voyage_number,$manfdate, $pol, $pagenb, $compname); 
		echo "<table cellpadding=0 cellspacing=0 width=100%>
			<col width=3%><col width=15%><col width=7%><col width=20%><col width=10%>
			<col width=10%><col width=5%><col width=10%><col width=10%><col width=10%>";
		$linenbr = 5;	
	}
	$querydt = "select manifestdt.container, date_format(manifest.Manifest_date,'%d/%m/%Y'), if(ifnull(manifest_emptyContainer,0) = 0, 'FULL','EMPTY') as cntr_type, manifest.vessel_id as vessel, manifest.voyage_number as voynbr
				from manifestdt
				inner join manifest on manifest.Manifest_auto = manifestdt.manifest_manifestauto
				where manifest.voyage_year = '$voyage_year'  and manifest.BOL_type <> 'E' and manifestdt.container = '$row[0]'
				and (ifnull(ctrflag,'') = '' or detailtype=1)
				order by container, manifest.Manifest_date desc limit 1 ";
				
	$resultdt = mysql_query($querydt, $connection);			
	$rowdt = mysql_fetch_row($resultdt);
	
	echo "<tr height='25px'>
			<td align='right' style='$bottom;$right;padding-right:5px;'><span class='header2'>$i</span></td>
			<td align='left' style='$bottom;$right;padding-left:5px;'><span class='header2'>$row[0]</span></td>
			<td align='left' style='$bottom;$right;padding-left:5px;'><span class='header2'>$row[1]</span></td>
			<td align='left' style='$bottom;$right;padding-left:5px;'><span class='header2'>$rowdt[3] $rowdt[4]</span></td>
			<td align='center' style='$bottom;$right;'><span class='header2'>$rowdt[1]</span></td>
			<td align='left' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'><span class='header2'>$rowdt[2]</span></td>
			<td align='left' style='$bottom;$right;'></td>
			<td align='left' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'><span class='header2'></span></td>
		 </tr>";	 
	$i++;	 
	
	if ($rowdt[2] == 'FULL')
		$cntr_type['F'][$row[1]] += 1;
	
	if ($rowdt[2] == 'EMPTY')
		$cntr_type['E'][$row[1]] += 1;
	
	
}	


echo "</table>";			

echo "<table cellpadding=0 cellspacing=0 width=100%><col width=10%><col width=20%><col width=10%><col width=5%><col width=55%>";
echo "<tr height='20px'><td></td><td></td><td></td><td></td><td></td></tr>";

$title = "TOTAL FULL CNTRS:";
if (!empty($cntr_type['F'])){
	foreach ($cntr_type['F'] as $k => $val) {
		echo "<tr><td></td><td align='left'><span class='header2'>$title</td><td>CT. $k</td><td>$val</td><td></td></tr>";
		$title = "";
	}
	echo "<tr height='20px'><td></td><td></td><td></td><td></td><td></td></tr>";
}
$title = "TOTAL EMPTY CNTRS:";
if (!empty($cntr_type['E'])){
	foreach ($cntr_type['E'] as $k => $val) {
		echo "<tr><td></td><td align='left'><span class='header2'>$title</td><td>CT. $k</td><td>$val</td><td></td></tr>";
		$title = "";
	}
	
}
echo "</table>";

function rep_header($vessel, $voyage_number,$manfdate, $pol, $pagenb, $compname) {
	$bottom = "border-bottom: 1px solid black";
	$top    =  "border-top: 1px solid black";
	$right  = "border-right: 1px solid black";

	echo "<table cellpadding=0 cellspacing=0 width=100%><col width=70%><col width=30%>";
	echo "<tr><td colspan='2'><span class='header1'>$compname / $pol</span></td></tr>";
	echo "<tr height='20px'><td></td><td align='center'><span class='header1'>PAGE $pagenb</span></td></tr>";
	echo "<tr><td colspan='2'><span class='header1'>$vessel VOY.$voyage_number DATE $manfdate </span></td></tr>";
	echo "<tr height='10px'><td></td><td align='center'></td></tr>";
	echo "</table>";
	
	echo "<table cellpadding=0 cellspacing=0 width=100%>
			<col width=3%><col width=15%><col width=7%><col width=20%><col width=10%>
			<col width=10%><col width=5%><col width=10%><col width=10%><col width=10%>";
	echo "<tr>
			<td align='center' style='$top;$right;'></td>
			<td align='center' style='$top;$right;'><span class='header2'>CONTAINERS<BR>MARKS/NUMBERS</span></td>
			<td align='center' style='$top;$right;'><span class='header2'>SIZE</span></td>
			<td align='center' style='$top;$right;'><span class='header2'>IMPORT VESSEL</span></td>
			<td align='center' style='$top;$right;'><span class='header2'>IMPORT DATE</span></td>
			<td align='center' style='$top;$right;'><span class='header2'>IMPORT MANIFEST NUMBER</span></td>
			<td align='center' style='$top;$right;'><span class='header2'>F/E EXP.</span></td>
			<td colspan='2' align='center' style='$top;$right;'><span class='header2'>FOR CUSTOM'S USER PROVE LOAD AFTER CHECKING<BR>---PARC--------------QUAY---</span></td>
			<td align='center' valign='middle' style='$top;$right;'><span class='header2'>FOR PORT<BR>USE TAX<BR><hr style='border-top: 1px dashed black;'></span></td>
		 </tr>";
	echo "<tr>
			<td align='center' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'></td>
			<td align='center' style='$bottom;$right;'><span class='header2'>CUSTOM AGENT</span></td>
			<td align='center' style='$bottom;$right;'><span class='header2'>CUSTOM AGENT</span></td>
			<td align='center' style='$bottom;$right;'><span class='header2'>DAY SUM</span></td>
		 </tr>";	 
	echo "</table>";
}
print <<< HERE

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

echo "</body>
	</html>";

?>














