<?php

/**
	vOthers = 1 -> Report Bahri
	vOthers = 0 -> Report Manifest

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

print <<< HERE

<HTML>
<HEAD>
<TITLE>Manifest </TITLE>

<style type="text/css">

/*
	body {
    font-size:9.5px;
}			  
	*/			  

 .classbody{
	 font-size:9px;
	 //color:red;
 }
.spacebeforegrid{
	height:232px;
}
.manifestgrid{
	height:290px;
}
		.classtitles{
			font-size:7px;
		}	 


.grid{
	position:absolute;
	
	 top: 1080px;
	
	//border:solid 1px black;
	width:100%;

	
}		



.classbody2{
	font-size:13;
}	

.classbody3{
	font-family: "Courier New", Courier, "Lucida Sans Typewriter", "Lucida Typewriter", monospace;
	font-size:9;
}	

.break { 

  page-break-after: always; 
}


@page {
  // padding 2cm;
    margin-bottom: 3cm;    
}
</style>


</HEAD>

<body>
HERE;

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);
}					
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

	
$now=current_server_time($connection);
$dt=split(" ",$now[0]);
$printdate=split("-",$dt[0]);


if ($vOthers == 1) $pagenbr = 1;
else $pagenbr = '';

$query="select vessel_id, BOL_identification, Voyage_year, voyage_number, voyage_destination, voyage_BOLnumber,
		 voyage_descripiton, BOL_type, BOL_pagenumber, Shipper1, shipper2, shipper3, shipper4, consignee1, consignee2, consignee3,
		 consignee4, Notify1, Notify2, Notify3, Notify4, Portofloading, PortofDischarge,DATE_FORMAT(Manifest_date, '%d/%m/%Y'),  BOL_freight,issued, 
		 upper(tax1_desc), upper(tax2_desc), upper(tax3_desc), upper(tax4_desc), upper(tax5_desc) ,upper(tax6_desc), tax1_amount, tax2_amount, tax3_amount, tax4_amount, tax5_amount, tax6_amount,
		 Manifest_Auto,DATE_FORMAT(date, '%d/%m/%Y'),currency, localagent_code, localagent.agent1, agentman.agent1, ifnull(manifest_emptyContainer,0)
		 Freight_type, other_remark from manifest
		 left join agent as localagent on localagent.code = localagent_code
		 left join agent as agentman on agentman.BL_code = Agent_Id or agentman.agent1 = Agent_Id
		 where 1=1  ";
if ($manifest_auto!='') 		$query=$query. " and manifest_auto = '$manifest_auto' ";
if ($billlading!='') 			$query=$query. " and BOL_identification = '$billlading' ";
if ($voyage_year!='') 			$query=$query. " and voyage_year = '$voyage_year' ";
if ($voyage_number!='') 		$query=$query. " and voyage_number = '$voyage_number' ";
if ($voyage_destination!='') 	$query=$query. " and voyage_destination = '$voyage_destination' ";
if ($bol_number!='') 			$query=$query. " and voyage_BOLnumber = '$bol_number' ";
if ($voyage_type!='') 			$query=$query. " and BOL_type = '$voyage_type' ";
if ($voyage_pagenumber!='') 	$query=$query. " and BOL_pagenumber = '$voyage_pagenumber' ";
if ($portofdischarge!= '') 		$query=$query. " and PortofDischarge = '$portofdischarge' ";
if ($portofloading != '')		$query=$query. " and Portofloading = '$portofloading' "; 
if ($localagent_code!= '') 		$query=$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  Voyage_year, voyage_number, manifest.date, if(BOL_type='E',PortofDischarge,Portofloading), if(BOL_type='E',Portofloading,PortofDischarge) ";
if ($vOrderby == '1') 
	$query .= " , localagent_code ";
$query .= " , BOL_identification ";
$queryresult=mysql_query($query,$connection);

//echo $query;

$nmrow = 0;		
$temppod = '**';	
$countAllCntr = 0;
$countAllCntrPort = 0;
$splitnbr = 22;
	$i=0;
	//echo "<div class=nobottom>";
echo "<table border =0 width=100% cellspacing=0 cellpadding=0>";
	while($row=mysql_fetch_array($queryresult)){
	$vessel_id		= $row[0];
	$BOL_identification = $row[1];
	$Voyage_year	= $row[2];
	$voyage_number	= $row[3];
	$BOL_type 		= $row[5];
	$BOL_pagenumber = $row[8];
	$shipper1		= $row[9];
	$shipper2		= $row[10];
	$shipper3		= $row[11];
	$shipper4		= $row[12];
	$consignee1		= $row[13];
	$consignee2		= $row[14];
	$consignee3		= $row[15];
	$consignee4		= $row[16];
	$Notify1		= $row[17];
	$Notify2		= $row[18];
	$Notify3		= $row[19];
	$Notify4		= $row[20];
	$Portofloading	= $row[21];
	$PortofDischarge= $row[22];
	$Manifest_date	= $row[23];
	$BOL_freight	= $row[24];
	$issued			= $row[25];
	$desc1			= $row[26];
	$desc2			= $row[27];
	$desc3			= $row[28];
	$desc4			= $row[29];
	$desc5			= $row[30];
	$desc6			= $row[31];
	$amount1		= $row[32];
	$amount2		= $row[33];
	$amount3		= $row[34];
	$amount4		= $row[35];
	$amount5		= $row[36];
	$amount6		= $row[37];
	$Manifest_Auto	= $row[38];
	$saileddate		= $row[39];
	$currency		= $row[40];
	$localagent_code= $row[41];
	$localagent		= $row[42];
	$agentname		= $row[43];
	$emptyContainer = $row[44];
	$otherRemark	= $row[45];
	$groupbyPort = $PortofDischarge;
	
	if ( $BOL_type == 'E')
		$groupbyPort = $PortofDischarge;
	else 
		$groupbyPort = $Portofloading;
	//echo $voyage_type; exit;
	if ($voyage_type=='I') $voyagetype="IMPORT";
    if ($voyage_type=='E') $voyagetype="EXPORT";
    if ($voyage_type=='T') $voyagetype="FREEZONE";
    if ($voyage_type=='S') $voyagetype="TRANSHIPMENT";
	
	if ($withprice == 0 && $withlocalprice == 0)
		$title = 'Cargo';
	else $title = 'Freight';
	
	if($temppod != $groupbyPort){
		if ($vOthers == 1 && $countAllCntrPort > 0){
			echo "<tr height='30px'><td align='center' style='border-top: 1px solid black;border-bottom:1px solid black;'>
				<span class='classbody2'><b>".$countAllCntrPort." Containers &nbsp;&nbsp;&nbsp;&nbsp; Total Weight: &nbsp; ".$AllWeightPort." Kgs</b></span>
				</td></tr>";
			
		} else if ($vOthers != 1 ) {
			$querytype = "select distinct substr(type_id, 1,2)
				from container
				order by type_id";
			
			$restype = mysql_query($querytype, $connection);
			while ($rwtype = mysql_fetch_array($restype)){
				if ($cntrcount[$rwtype[0]] != '' && $cntrcount[$rwtype[0]] != 0)
				
				echo "<tr height='30px'><td align='center'>
				<span class='classbody2'><b>".$cntrcount[$rwtype[0]]." X ".$rwtype[0]."' Containers &nbsp;&nbsp;&nbsp;&nbsp; Total Weight: &nbsp; ".$weightbyport[$rwtype[0]]." Kgs</b></span>
				</td></tr>";
			}
		}
		$weightbyport = array();
		$cntrcount = array(); 
		$countAllCntrPort = 0;
		$AllWeightPort = 0;
	
	}  
	if($temppod != $groupbyPort){
		
				
		if ($i>0){
			echo "<tr><td>";
			echo "<div class='break'>&nbsp; </div>";
			echo "&nbsp;</td></tr>";
		}
		manifestheader($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr,$voyagetype);
		if ($vOthers == 1) $pagenbr += 1;
		$nmrow = 5;
	}
	$querydt = "select Container, Marks, Packages, descriptiongood, Container_Weight, CubeMeters,manifestdt.auto,manifestdt.detailsequence,ctrflag,
			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) as weightnbr, if ( ifnull(type_id,'') = '', substr(container_type, 1,2),substr(type_id, 1,2)), CTRFlag, detailtype
			from manifestdt
			inner join manifestdesc on manifestdt.manifest_manifestauto=manifestdesc.manifest_manifestauto
			and manifestdesc.detailsequence=manifestdt.detailsequence
			left join container on container.cntr_code = manifestdt.container
			where manifestdt.manifest_manifestauto='$Manifest_Auto'
			group by manifestdt.auto";//echo $querydt;echo "<br><br>";
			
	echo "<tr><td>
				<table border=0 width=100% cellspacing=0>
					<col width=150>
					<col width=150>
					<col width=150>
					<col width=100>
					<col width=250>
					<col width=100>
					<col width=100>
					<col width=150>
					<col width=100>";		
	
	echo "
		<tr>
			<td valign=top> <span class='classbody2'>$BOL_identification</span></td>
			<td  colspan=6>
				<table width=100%>";
	if ($vOthers == 1){
		echo "<tr><td><span class='classbody2'>".nl2br($otherRemark)."</span></td></tr>";
		$nmrow = $nmrow + 1;
		if ((($nmrow%$splitnbr) == 0) && $vOthers == 1){
			splitPage ($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr);
			if($vOthers == 1) $pagenbr += 1;
			$nmrow = 5;
		}
	}	
	else {
		if ($shipperType == 0){
		echo "	
				<tr><td><span class='classbody2'>A) $shipper1</span></td></tr>
				<tr><td><span class='classbody2'>$shipper2</span></td></tr>
				<tr><td><span class='classbody2'>$shipper3</span></td></tr>
				<tr><td><span class='classbody2'>$shipper4</span></td></tr>
				<tr><td><span class='classbody2'>B) $consignee1</span></td></tr>
				<tr><td><span class='classbody2'>$consignee2</span></td></tr>
				<tr><td><span class='classbody2'>$consignee3</span></td></tr>
				<tr><td><span class='classbody2'>$consignee4</span></td></tr>
				<tr><td><span class='classbody2'>C) $Notify1</span></td></tr>
				<tr><td><span class='classbody2'>$Notify2</span></td></tr>
				<tr><td><span class='classbody2'>$Notify3</span></td></tr>
				<tr><td><span class='classbody2'>$Notify4</span></td></tr>
			";
			$nmrow = $nmrow + 3;
			if ($shipper2 != '')
				$nmrow = $nmrow + 1;	
			if ($shipper3 != '')
				$nmrow = $nmrow + 1;	
			if ($shipper4 != '')
				$nmrow = $nmrow + 1;
			if ($consignee2 != '')
				$nmrow = $nmrow + 1;	
			if ($consignee3 != '')
				$nmrow = $nmrow + 1;	
			if ($consignee4 != '')
				$nmrow = $nmrow + 1;
			if ($Notify2 != '')
				$nmrow = $nmrow + 1;	
			if ($Notify3 != '')
				$nmrow = $nmrow + 1;	
			if ($Notify4 != '')
				$nmrow = $nmrow + 1;	
				
		} 
		else if ($shipperType == 1) {
			echo "	
				<tr><td><span class='classbody2'>$localagent</span></td></tr>
				<tr><td><span class='classbody2'>CN: Order</span></td></tr>
				";
			$nmrow = $nmrow + 3;	
		} 
		else if ($shipperType == 2) {
			echo "	
				<tr><td><span class='classbody2'>$agentname</span></td></tr>
				<tr><td><span class='classbody2'>CN: Order</span></td></tr>
				";
			$nmrow = $nmrow + 3;	
		}
	}
	
	echo "		</table>
			</td>";
			
		if ($BOL_freight =='PREPAID' && $vOthers != 1)   echo "<td valign=top><span class='classbody2'>$BOL_freight AT $issued</span></td></tr>";
		if ($BOL_freight =='COLLECT' && $vOthers != 1)   echo "<td></td><td valign=top><span class='classbody2'>$BOL_freight AT $PortofDischarge</span></td></tr>";
			
				$resultdt=mysql_query($querydt,$connection);
				$numdt = mysql_num_rows($resultdt);
				$tt = 0;
				$emptyWeight = 0;
				$c = 1;
				while($rowdt=mysql_fetch_array($resultdt)){
					$container = $rowdt[0];
					$marks = $rowdt[1];
					$Packages = $rowdt[2];
					$descriptiongood = $rowdt[3];
					$Container_Weight = $rowdt[4];
					$CubeMeters = $rowdt[5];
					$tare = $rowdt[9];
					
					if ($emptyContainer == 1)
						$weightnbr = $tare;
					else 
						$weightnbr = $rowdt[10];
					$cntrtype = $rowdt[11];
					$ctrflag = $rowdt[12];
					
					$weightbyport[$cntrtype] = $weightbyport[$cntrtype] + $weightnbr;
					$cntrcount[$cntrtype] = $cntrcount[$cntrtype] + 1;
					
					if ($rowdt[13] == 1 || $ctrflag == '' ){
						$countAllCntrPort = $countAllCntrPort + 1;
						$countAllCntr	  = $countAllCntr + 1;
					}
					$AllWeightPort = $AllWeightPort + $weightnbr;
				 if ($emptyContainer == 1){
					if ($ctrflag == '' || $rowdt[13] == 1){
						if ($c == 1)
							echo "<tr><td></td><td></td>";
						echo "<td colspan='2'><span class='classbody2'>$container</span></td>";
						if (!($c%3)){
							echo "<td colspan='6'></td></tr>";
							$nmrow++;
							if ((($nmrow%$splitnbr) == 0) && $vOthers == 1){
								splitPage ($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr);
								if($vOthers == 1) $pagenbr += 1;
								$nmrow = 5;
							}
							echo "<tr><td></td><td></td>";
							$nmrow++;
						}
						$c++;
					}
						
				} else {				
					echo "<tr>
							<td></td><td></td>
							<td><span class='classbody2'>$container</span></td>
							<td><span class='classbody2'>$Packages</span></td>
							<td><span class='classbody2'>$descriptiongood</span></td>
							<td><span class='classbody2'>$Container_Weight</span></td>
							<td><span class='classbody2'>$tare</span></td>
							<td></td>
							<td></td>			
						</tr>";
					$nmrow++;
				}
					$totweight = $totweight + $weightnbr;
					$emptyWeight = $emptyWeight + $weightnbr;
					
					$tt++;
					
				if ((($nmrow%$splitnbr) == 0) && $numdt != $tt && $vOthers == 1){
					splitPage ($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr);
					if($vOthers == 1) $pagenbr += 1;
					$nmrow = 5;
				} 	
			}
				if ($emptyWeight > 0 && $emptyContainer == 1){
					$emptyWeight = number_format($emptyWeight,2);			
					echo "<tr><td></td><td></td><td></td><td colspan='6' align='center'><span class='classbody2'><b>Total Weight: $emptyWeight Kgs<b/></span></td></tr>";
					$nmrow++;
					if ((($nmrow%$splitnbr) == 0) && $vOthers == 1){
						splitPage ($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr);
						if($vOthers == 1) $pagenbr += 1;
						$nmrow = 5;
					}
				}
				echo "</table>
			</td></tr>";
			
			if($withprice==1){
		echo "<tr>
				<td >
					<table width=100% border=0>
						<col width=150>
						<col width=150>
						<col width=150>
						<col width=100>
						<col width=100>
						<col width=100>
						<col width=100>
						<col width=150>
						<col width=100>";
					echo "<tr height=10 ><td colspan=8 style='border-top:solid 1px black'></td></tr>";
					echo "	<tr><td colspan=5 align=right><span class='classbody2'>$desc1</span></td>";
					if ($amount1!=0)echo "<td><span class='classbody2'> &nbsp;$amount1 &nbsp; $currency </span></td><td colspan=2 align=left></td></tr>
					<tr><td colspan=5 align=right><span class='classbody2'>$desc2</span></td>";
					if ($amount2!=0)echo "<td><span class='classbody2'> &nbsp;$amount2 &nbsp; $currency </span></td><td colspan=2 align=left></td></tr>
					<tr><td colspan=5 align=right><span class='classbody2'>$desc3</span></td>";
					if ($amount3!=0)echo "<td><span class='classbody2'> &nbsp;$amount3  &nbsp; $currency  </span></td><td colspan=2 align=left></td></tr>
					<tr><td colspan=5 align=right><span class='classbody2'>$desc4</span></td>";
					if ($amount4!=0)echo "<td><span class='classbody2'> &nbsp;$amount4 &nbsp; $currency  </span></td><td colspan=2 align=left></td></tr>
					<tr><td colspan=5 align=right><span class='classbody2'>$desc5</span></td>";
					if ($amount5!=0)echo "<td><span class='classbody2'> &nbsp;$amount5  &nbsp; $currency </span></td><td colspan=2 align=left></td></tr>
					<tr><td colspan=5 align=right><span class='classbody2'>$desc6</span></td>";
					if ($amount6!=0)echo "	<td><span class='classbody2'> &nbsp;$amount6 &nbsp; $currency </span></td><td align=left colspan=2></td></tr>";
	
	
					$amounttotal=floatval($amount1)+floatval($amount2)+floatval($amount3)+floatval($amount4)+floatval($amount5)+floatval($amount6);
					echo "<tr><td   colspan=5 align=right ><span class='classbody2'>TOTAL:</span></td>
					<td ".($amounttotal > 0?"style='border-top:solid 1px black'":'')."><span class='classbody2'> &nbsp;$amounttotal &nbsp; $currency</span></td><td colspan=2></td></tr>
					</table>";
				$allAmountPrice = $allAmountPrice + ($amounttotal>0?$amounttotal:0);
				$priceCur = $currency;
			echo "</td></tr>";}
			
			
			
			if ($withlocalprice==1){
				$totalamount = 0;
				$amountquery="select upper(item_name), sum(tax_amount * quantity), currencycode from manifestdtl where manifest_manifestauto='$Manifest_Auto' group by item_name, ifnull(currencycode,'')   ";
				$queryres=mysql_query($amountquery,$connection);
								
				echo "<tr>
				
				<td     >
				<table border=0 width=100%>
				
				<col width=150>
						<col width=150>
						<col width=150>
						<col width=100>
						<col width=100>
						<col width=100>
						<col width=100>
						<col width=150>
						<col width=100>";
				while ($row=mysql_fetch_array($queryres)){
					echo "	<tr><td colspan=5 align=right><span class='classbody2'>$row[0]</span></td>";
					echo "<td><span class='classbody2'> &nbsp;$row[1] &nbsp; $row[2] </span></td><td colspan=2 align=left></td></tr>";
					$totalamount = $totalamount + $row[1];
				}
				
				echo "<tr><td  colspan=5 align=right><span class='classbody2'>TOTAL:</span></td>
					<td ".($totalamount > 0?"style='border-top:solid 1px black'":'')."><span class='classbody2'> &nbsp;$totalamount&nbsp; $currency </span></td><td colspan=2></td></tr>
					</table></td></tr>";
					
				$allAmountLocal = $allAmountLocal + ($totalamount>0?$totalamount:0);	
				$localCur = $currency;
			}
			
		$temppod = $groupbyPort;
		$i++;
		
	}	
	if ($vOthers == 1 && $countAllCntrPort > 0 ){
		echo "<tr><td>
					<table border=0 width=100% cellspacing=0>
						<col width=150>
						<col width=150>
						<col width=150>
						<col width=100>
						<col width=250>
						<col width=100>
						<col width=100>
						<col width=150>
						<col width=100>";	
		echo "<tr height='30px'><td align='center' style='border-top: 1px solid black;'><span class='classbody2'><b>".$countAllCntrPort." Containers &nbsp;&nbsp;&nbsp;&nbsp; Total Weight: &nbsp; ".number_format($AllWeightPort,2)." Kgs</b></span></td></tr>";	
		echo "</table></td></tr>";				
		$nmrow++;
		if ((($nmrow%$splitnbr) == 0) && $vOthers == 1){
			splitPage ($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr);
			if($vOthers == 1) $pagenbr += 1;
			$nmrow = 5;
		}	
	} else if ($vOthers != 1) {
		if(!empty($cntrcount)){
			
			echo "<tr><td>
					<table border=0 width=100% cellspacing=0>
						<col width=150>
						<col width=150>
						<col width=150>
						<col width=100>
						<col width=250>
						<col width=100>
						<col width=100>
						<col width=150>
						<col width=100>";	
			$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 ($cntrcount[$rwtype[0]] != '' && $cntrcount[$rwtype[0]] != 0)
				
				echo "<tr height='30px'><td align='center'><span class='classbody2'><b>".$cntrcount[$rwtype[0]]." X ".$rwtype[0]."' Containers &nbsp;&nbsp;&nbsp;&nbsp; Total Weight: &nbsp; ".number_format($weightbyport[$rwtype[0]],2)." Kgs</b></span></td></tr>";
			}
			$weightbyport = array();
			$cntrcount = array(); 
			echo "</table></td></tr>";
		
		}
	}
	$querypkg = "select sum(uExtractNumberFromString(Packages)) 
				from manifestdt inner join manifest on manifest.manifest_auto = manifest_manifestauto
				where ifnull(Packages,'') != '' ";
	if ($manifest_auto!='') 		$querypkg=$querypkg. " and manifest.manifest_auto = '$manifest_auto' ";
	if ($billlading!='') 			$querypkg=$querypkg. " and manifest.BOL_identification = '$billlading' ";
	if ($voyage_year!='') 			$querypkg=$querypkg. " and manifest.voyage_year = '$voyage_year' ";
	if ($voyage_number!='') 		$querypkg=$querypkg. " and manifest.voyage_number = '$voyage_number' ";
	if ($voyage_destination!='') 	$querypkg=$querypkg. " and manifest.voyage_destination = '$voyage_destination' ";
	if ($bol_number!='') 			$querypkg=$querypkg. " and manifest.voyage_BOLnumber = '$bol_number' ";
	if ($voyage_type!='') 			$querypkg=$querypkg. " and manifest.BOL_type = '$voyage_type' ";
	if ($voyage_pagenumber!='') 	$querypkg=$querypkg. " and manifest.BOL_pagenumber = '$voyage_pagenumber' ";
	if ($portofdischarge!= '') 		$querypkg=$querypkg. " and manifest.PortofDischarge = '$portofdischarge' ";
	if ($portofloading != '')		$querypkg=$querypkg. " and Portofloading = '$portofloading' "; 
	if ($localagent_code!= '') 		$querypkg=$querypkg. " and manifest.localagent_code = '$localagent_code' ";	
	if ($cntrtype == '1')			$querypkg .= " and ifnull(manifest_emptyContainer,0) = '0' ";
	else if ($cntrtype == '2')		$querypkg .= " and ifnull(manifest_emptyContainer,0) = '1' ";	
									
	$respkg = mysql_query($querypkg, $connection);
	$rwpkg = mysql_fetch_row($respkg);
	$nbrpkg = $rwpkg[0];
	echo "<tr>
	<td>
	<table width=100% border=0 cellspacing=0>
	<col width='230px'>
	<col width='200px'>
	<col width='270px'>
	<tr height=10px><td style='border-top:solid 1px black;' colspan='3' ></td></tr>";
	$nbcontainer="select count(container), substr(type_id, 1,2)
				from manifestdt
				inner join container on container.cntr_code = container
				inner join manifest on manifest.manifest_auto = manifest_manifestauto
				where ifnull(ctrflag,'')!='*' ";
	if ($manifest_auto!='') 		$nbcontainer=$nbcontainer. " and manifest.manifest_auto = '$manifest_auto' ";
	if ($billlading!='') 			$nbcontainer=$nbcontainer. " and manifest.BOL_identification = '$billlading' ";
	if ($voyage_year!='') 			$nbcontainer=$nbcontainer. " and manifest.voyage_year = '$voyage_year' ";
	if ($voyage_number!='') 		$nbcontainer=$nbcontainer. " and manifest.voyage_number = '$voyage_number' ";
	if ($voyage_destination!='') 	$nbcontainer=$nbcontainer. " and manifest.voyage_destination = '$voyage_destination' ";
	if ($bol_number!='') 			$nbcontainer=$nbcontainer. " and manifest.voyage_BOLnumber = '$bol_number' ";
	if ($voyage_type!='') 			$nbcontainer=$nbcontainer. " and manifest.BOL_type = '$voyage_type' ";
	if ($voyage_pagenumber!='') 	$nbcontainer=$nbcontainer. " and manifest.BOL_pagenumber = '$voyage_pagenumber' ";
	if ($portofdischarge!= '') 		$nbcontainer=$nbcontainer. " and manifest.PortofDischarge = '$portofdischarge' ";
	if ($portofloading != '')		$nbcontainer=$nbcontainer. " and Portofloading = '$portofloading' ";
	if ($localagent_code!= '') 		$nbcontainer=$nbcontainer. " and manifest.localagent_code = '$localagent_code' ";		
	if ($cntrtype == '1')			$nbcontainer .= " and ifnull(manifest_emptyContainer,0) = '0' ";
	else if ($cntrtype == '2')		$nbcontainer .= " and ifnull(manifest_emptyContainer,0) = '1' ";	
									$nbcontainer=$nbcontainer. " group by substr(type_id, 1,2) ";
	//echo $nbcontainer;
	if ($vOthers == 1){
		echo "<tr>
				<td ></td>
				<td ><span class='classbody2'>$countAllCntr Containers</span></td>
				<td ><span class='classbody2'></span></td>
				<tr>";
	} else {
		$resultnb=mysql_query($nbcontainer,$connection);
		while ($rownb=mysql_fetch_array($resultnb)){
			echo "<tr>
				<td ></td>
				<td ><span class='classbody2'>$rownb[0] Containers $rownb[1]</span></td>
				<td ><span class='classbody2'></span></td>
				<tr>";
			}
	}
	$totweight = number_format($totweight,2);	
	echo "<tr>
	<td ></td>
	<td ><span class='classbody2'>";
	if ($nbrpkg != '' && $vOthers != 1) echo $nbrpkg." PKGS"; 
	echo "</span></td>
	<td ><span class='classbody2'>".$totweight." KGS</span></td>
	<tr>"; 
	if($withprice==1){
		echo "<tr><td></td><td></td><td><span class='classbody2'>Total: $allAmountPrice $priceCur</span></td></tr>";
	}
	if ($withlocalprice==1){
		echo "<tr><td></td><td></td><td><span class='classbody2'>Total: $allAmountLocal $localCur</span></td></tr>";
	}
	echo "<tr><td style=' border-bottom:solid 1px black'></td><td style=' border-bottom:solid 1px black'></td><td style=' border-bottom:solid 1px black'></td></tr>";
	echo "</table></td></tr>";					
	echo "</table>";
//	echo "<div>";
	

function current_server_time($connection){

  $string="Select date_format(NOW(),'%Y-%m-%d %h:%i %p') ";
  $result=mysql_query($string,$connection) or die(mysql_error());
  $now=mysql_fetch_row($result);

  return $now;
}

function manifestheader($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr,$voyagetype){
	echo "<tr >
			<td colspan='9'>
				<table border=0 width=100% cellspacing=0 cellpadding=0>
					<col width=30%>
					<col width=40%>
					<col width=30%>
					<tr height=40px>
						<td valign=top><b >$title Manifest</b></td>
						<td align=center>".($vOthers == '1'?'':'<b>GHARIB SHPPING AGENCY</b>
						<br><b>Beirut Lebanon</b>')."
						</td>
						<td><span class='classbody2' >".($vOthers == '1'?"<b>PAGE $pagenbr</b>":'')."</span></td>
					</tr>
				</table>
			</td>
		</tr>";
	echo "<tr>";
			echo "<td colspan='9' >
					<table width=100% border=0>";
						echo "<col width=80>
						<col width=150>
						<col width=400>
						<col width=100>
						<col width=100>
						<col width=100>";
						echo "<tr>
								<td ><span class='classbody2' ><b>Vessel</b></span></td>
								<td align='left'><span class='classbody2' ><b>$vessel_id</b></span></td>
								<td align=right><span class='classbody2' ></span></td>
								
								<td ><span class='classbody2' ><b>Port Of Loading</b></span></td>
								<td align=left><span class='classbody2'><b>$Portofloading </b></span></td>
						</tr>";
						echo "<tr>
								<td><span class='classbody2' align=right><b>Voyage</b></span></td>
								<td  align=left><span class='classbody2'><b>$voyage_number </b></span></td>
								
								<td align=center ><span class='classbody2' > <b> Sailed &nbsp; &nbsp; &nbsp;
								$saileddate </b></span></td>
								<td><span class='classbody2' ><b>Port Of Discharge</b></span></td>
								<td align=left><span class='classbody2'><b>$PortofDischarge &nbsp; &nbsp; &nbsp; $voyagetype </b></span></td>
						</tr>
					</table>
				</td>
	</tr>";
	echo "<tr><td colspan='9'>
				<table border=0 width=100% cellspacing=0>
					<col width=150>
					<col width=150>
					<col width=150>
					<col width=100>
					<col width=250>
					<col width=100>
					<col width=100>
					<col width=150>
					<col width=100>
				
					<tr height='30px'>
						<td style='border-top:solid 1px black; border-bottom:solid 1px black'><span class='classbody2'><b>B/LN0</b></span></td>
						<td style='border-top:solid 1px black ; border-bottom:solid 1px black'><span class='classbody2'><b>Shipper/consignee/Notify</b></span></td>
						<td style='border-top:solid 1px black ; border-bottom:solid 1px black'><span class='classbody2'><b>Marks & numbers</b></span></td>
						<td style='border-top:solid 1px black; border-bottom:solid 1px black'><span class='classbody2'><b>Packing </b></span></td>
						<td style='border-top:solid 1px black; border-bottom:solid 1px black'><span class='classbody2'><b>Description Of Goods </b></span></td>
						<td style='border-top:solid 1px black; border-bottom:solid 1px black'><span class='classbody2'><b>Weight </b></span></td>
						<td style='border-top:solid 1px black; border-bottom:solid 1px black'><span class='classbody2'><b>Tare </b></span></td>
						<td style='border-top:solid 1px black; border-bottom:solid 1px black'><span class='classbody2'><b>".($vOthers == 1?'':'Prepaid')."</b></span></td>
						<td  style='border-top:solid 1px black; border-bottom:solid 1px black'><span class='classbody2'><b>".($vOthers == 1?'':'to be Collect')."</b></span></td>
					</tr>";
	echo "</table></td></tr>";
		
}/* 
function chkrw ($title,$vessel_id,$voyage_number,$PortofDischarge, $nmrow){
	echo "<tr><td>";
	echo "<div class='break'>&nbsp;</div>";
	echo "&nbsp;</td></tr>";
	echo "</table>
	</td></tr>";
	manifestheader($title,$vessel_id,$voyage_number,$PortofDischarge);
	$nmrow = $nmrow + 6;	
	echo "<tr><td>
<table border=0 width=100% cellspacing=0>
	<col width=150>
	<col width=150>
	<col width=150>
	<col width=100>
	<col width=250>
	<col width=100>
	<col width=100>
	<col width=150>
	<col width=100>";
} */

function splitPage ($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr){
	echo "<tr><td>";
	echo "<div class='break'>&nbsp;</div>";
	echo "&nbsp;</td></tr>";
	echo "</table>
	</td></tr>";
	manifestheader($title,$vessel_id,$voyage_number,$PortofDischarge, $Portofloading, $saileddate, $vOthers, $pagenbr);
		
	echo "<tr><td><table border=0 width=100% cellspacing=0>
	<col width=150>	<col width=150>	<col width=150>	<col width=100>
	<col width=250>	<col width=100>	<col width=100>	<col width=150>
	<col width=100>";
}
print <<< HERE

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


				 
				  
				  