<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
include("function.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>Demurrage List</title></head>
<style>
.header1 {	
	  font-size: 16px;
}

.header2 {	
	  font-size: 14px;
}

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

HERE;

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

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

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, $base2); 

$linenbr = 3;

echo "<table cellpadding=0 cellspacing=5 width=100%>
			<col width=13%><col width=18%><col width=12%><col width=8%><col width=8%><col width=8%>
			<col width=8%><col width=5%><col width=5%><col width=5%><col width=5%><col width=5%>";
			
$query = "select if(manifest.BOL_type='E',PortofDischarge,Portofloading), manifest.BOL_identification, ledger_name, manifestdt.container, ifnull(type_id,container_type),
			if(ifnull(manifest_emptyContainer,0)=0, 'FULL', 'EMPTY'), containertrace.freetimedays, date_format(From_date, '%d-%m-%Y'), date_format(crf_date, '%d-%m-%Y'), date_format(crt_date, '%d-%m-%Y'),
			if(ifnull(crt_date,'') != '', DATEDIFF(crt_date,From_date)+1, if(ifnull(crf_date,'') != '',DATEDIFF(crf_date,From_date)+1,0) ), ifnull(type_id,container_type), if(ifnull(crt_date,'')='', 'FULL', 'EMPTY')
			from manifestdt
			inner join manifest on manifest.manifest_auto = manifestdt.manifest_manifestauto
			inner join clients on clients.ledger_number = manifest.client_code
			left join container on container.cntr_code = manifestdt.container
			inner join containertrace on containertrace.manifest_auto = manifestdt.manifest_manifestauto and container_no = manifestdt.container
			where manifest.voyage_year = $voyage_year and manifest.voyage_number = $voyage_number and (ifnull(ctrflag,'') = '' or detailtype=1) ";

if ($manifest_auto!='') 		$query .= " and manifest.manifest_auto = '$manifest_auto' ";
if ($billlading!='') 			$query .= " and manifest.BOL_identification = '$billlading' ";
if ($voyage_destination!='') 	$query .= " and manifest.voyage_destination = '$voyage_destination' ";
if ($bol_number!='') 			$query .= " and manifest.voyage_BOLnumber = '$bol_number' ";
if ($voyage_type!='') 			$query .= " and manifest.BOL_type = '$voyage_type' ";
if ($voyage_pagenumber!='') 	$query .= " and manifest.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' ";	

$query .= " order by if(manifest.BOL_type='E',PortofDischarge,Portofloading), manifest.BOL_identification ";
//echo $query;
$tempport = "*";

$result = mysql_query($query, $connection);
while ($row = mysql_fetch_row($result)){
	$demdays = 0;
	$cost	 = 0;
	if ($tempport != $row[0]){
		if (!($linenbr%20)){
			printNewPage($pagenb,$vessel, $voyage_number,$manfdate, $base2);
			$pagenb++;
		}
		echo "<tr><td align='left' colspan='11'><span class='header2' style='border-bottom:1px solid black;'>$row[0]</span></td></tr>";
		$linenbr++;
		
	}
	
	if (!($linenbr%20)){
		printNewPage($pagenb,$vessel, $voyage_number,$manfdate, $base2);
		$pagenb++;
	}	
	
	if ($row[6] < $row[10])
		$demdays = $row[10]-$row[6];
	if ($demdays>0){
		$cost = getDemurrageVal ($row[4], $demdays, $row[6], $connection);
	} 
	
	echo "<tr>
			<td align='left' ><span class='header2'>$row[1]</span></td>
			<td align='left' ><span class='header2'>$row[2]</span></td>
			<td align='left' ><span class='header2'>$row[3]</span></td>
			<td align='left' ><span class='header2'>$row[4]</span></td>
			<td align='center' ><span class='header2'>$row[7]</span></td>
			<td align='center' ><span class='header2'>$row[8]</span></td>
			<td align='center' ><span class='header2'>$row[9]</span></td>
			<td align='center' ><span class='header2'>$row[12]</span></td>
			<td align='center' ><span class='header2'>$row[10]</span></td>
			<td align='center' ><span class='header2'>$row[6]</span></td>
			<td align='center' ><span class='header2'>$demdays</span></td>
			<td align='center' ><span class='header2'>".number_format($cost,2)."</span></td>
		 </tr>";
		 
	$tempport = $row[0];	 
	$linenbr++;
		
}

echo "</table>";
/* echo "<table cellpadding=0 cellspacing=0 width=100%><col width=100%>";	
echo "<tr><td align='center'><span class='header2'>PAGE $pagenb</SPAN></td></tr>";
echo "</table>"; */			


function printNewPage($pagenb,$vessel, $voyage_number,$manfdate, $base2){
	echo "</table>";
	/* echo "<table cellpadding=0 cellspacing=0 width=100% style='position: absolute;bottom: 0;'><col width=100%>";
	echo "<tr><td align='center'><span class='header2'>PAGE $pagenb</span></td></tr>";
	echo "</table>"; */
	echo "<div style='page-break-after: always;'></div>";
	rep_header($vessel, $voyage_number,$manfdate, $base2);
	echo "<table cellpadding=0 cellspacing=5 width=100%>
			<col width=13%><col width=18%><col width=12%><col width=8%><col width=8%><col width=8%>
			<col width=8%><col width=5%><col width=5%><col width=5%><col width=5%><col width=5%>";
}

function rep_header($vessel, $voyage_number,$manfdate, $base2) {
	$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'>DEMURRAGE LIST OF $vessel V/$voyage_number - $manfdate</span></td></tr>";
	echo "<tr height='10px'><td></td><td align='center'></td></tr>";
	echo "</table>";
	
	echo "<table cellpadding=0 cellspacing=5 width=100%>
			<col width=13%><col width=18%><col width=12%><col width=8%><col width=8%><col width=8%>
			<col width=8%><col width=5%><col width=5%><col width=5%><col width=5%><col width=5%>";
	echo "<tr>
			<td align='center' style='$bottom;'><span class='header2'>B\L No.</span></td>
			<td align='center' style='$bottom;'><span class='header2'>CLIENT NAME</span></td>
			<td align='center' style='$bottom;'><span class='header2'>CNTR No.</span></td>
			<td align='center' style='$bottom;'><span class='header2'>CNTR SIZE</span></td>
			<td align='center' style='$bottom;'><span class='header2'>Received Date</span></td>
			<td align='center' style='$bottom;'><span class='header2'>CRF Date</span></td>
			<td align='center' style='$bottom;'><span class='header2'>CT Date</span></td>
			<td align='center' style='$bottom;'><span class='header2'>POS</span></td>
			<td align='center' style='$bottom;'><span class='header2'>Total Days</span></td>
			<td align='center' style='$bottom;'><span class='header2'>Free Days</span></td>
			<td align='center' style='$bottom;'><span class='header2'>Dem. Days</span></td>
			<td align='center' style='$bottom;'><span class='header2'>To Coll.($base2)</span></td>
		 </tr>";
	
	echo "</table>";
}
print <<< HERE

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

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

?>














