<?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");
$path="select manifest_path from setup";
$res = mysql_query($path,$connection);
if($rows = mysql_fetch_array($res)){
	$filepath=$rows[0];
}
$var = array();
$addtofile = array();
$fieldname = array();
$file1=$_GET['file1'];
$fileall=$_GET['file1'];
$showCont = $_GET['showCont'];

$writemode=$_GET['writemode'];
//$file1=$_GET['file1'];
if($file1=='Sndbofl1'){$field='Sndbofl1';}
if($file1=='Sndbofl3'){$field='Sndoftl3';}
if($file1=='Sndbofl4'){$field='sndbofl4';}
if($file1=='Sndbofl2'){$field='Sndbofl2';}

$voyage_year = ereg_replace("'", "\'", $voyage_year);
$voyage_number = ereg_replace("'", "\'", $voyage_number);
$voyage_destination = ereg_replace("'", "\'", $voyage_destination);
$bol_number = ereg_replace("'", "\'", $bol_number);
$voyage_type = ereg_replace("'", "\'", $voyage_type);
$voyage_pagenumber = ereg_replace("'", "\'", $voyage_pagenumber);
$Localagent = ereg_replace("'", "\'", $Localagent);

//////////////////////////////////////////////////////////
/*
	$q = "select fieldlength,fieldname1 from Sndbofl  where fieldfamily='$field' ";
	$res = mysql_query($q,$connection);
	//echo $q;
	$j=0;
	
while($r = mysql_fetch_row($res)){
	
	
	//	$newvessel_no = str_pad($vessel_no + 1, 4, 0, STR_PAD_LEFT);
		
	$var[$r[1]] = $r[0];
$fieldname[$j]=$r[1];
$j++;
}*/
////////////////////////////////////////////////
/*
$q = "select fieldname1 from Sndbofl where fieldfamily='Sndbofl1' ";
	$res = mysql_query($q,$connection);
while($r = mysql_fetch_row($res)){
	
	
	//	$newvessel_no = str_pad($vessel_no + 1, 4, 0, STR_PAD_LEFT);
		
echo $r[0].",&nbsp;";
}
*/

//$path=$filepath."/".$file1;



//fwrite($myfile,PHP_EOL );
if ($showCont == '') $showCont = 0;
if(($file1=='Sndbofl1')||($fileall=='all')){
	
	//if($fileall=='all')echo "<br>all<br>";
	//if($file1=='Sndbofl1')echo "<br>Sndbofl1<br>";
	
	if($fileall=='all'){$file1='Sndbofl1';}
	if($file1=='Sndbofl1'){$field='Sndbofl1';}
	if($file1=='Sndbofl3'){$field='Sndoftl3';}
	if($file1=='Sndbofl4'){$field='Sndbofl4';}
	if($file1=='Sndbofl2'){$field='Sndbofl2';}
	$myfile = fopen("$filepath/Export/$file1.txt", "$writemode") or die("Unable to open file!");
	
	
		$q = "select fieldlength,fieldname1 from Sndbofl  where fieldfamily='$field' ";
	$res = mysql_query($q,$connection);
	//echo $q;
	$j=0;
	
while($r = mysql_fetch_row($res)){
	
	
	//	$newvessel_no = str_pad($vessel_no + 1, 4, 0, STR_PAD_LEFT);
		
	$var[$r[1]] = $r[0];
$fieldname[$j]=$r[1];
$j++;
}
	
$query_mani="select Voyage_year, voyage_number, voyage_destination, BOL_type, 
			voyage_BOLnumber, BOL_pagenumber, voyage_descripiton, vessel_id, BOL_identification, 
			Shipper1, shipper2, shipper3, shipper4, consignee1, consignee2, consignee3, consignee4, 
			Notify1, Notify2, Notify3, Notify4, Agent_Id, Precarriage, PlaceofReceipt, Portofloading, 
			PortofDischarge, Placeofdelivery, stamp1_id, stamp2_id, stamp3_id, stamp4_id, stamp5_id,
			stamp6_id, line_operator as status1, stampdescstatus as stampdesc, BOL_freight, issued, date, Negotiable_BOL, term_of_carriage,Freight_type,
			'0' as commfreight, No_of_tues, currency,'0' as collectedcomm, tax1_desc, tax2_desc, tax3_desc, tax4_desc, tax5_desc, 
			tax6_desc, tax1_amount, tax2_amount, tax3_amount, tax4_amount, tax5_amount, 
			tax6_amount, evidence1, evidence2, evidence3, evidence4, evidence5, evidence6,
			source_agentcode, linear_meters, freetimedays, Voyage_year, voyage_number, voyage_destination,
			source_agentcode, BOL_type, voyage_BOLnumber, BOL_pagenumber  ,year, month, day, hour, minutes, seconds,
			Offernumber from manifest where 1=1";
if ($voyage_year!='') 			$query_mani=$query_mani. " and voyage_year='$voyage_year'";
if ($voyage_number!='') 		$query_mani=$query_mani. " and voyage_number='$voyage_number'";
if ($voyage_destination!='') 	$query_mani=$query_mani. " and voyage_destination='$voyage_destination'";
if ($bol_number!='') 			$query_mani=$query_mani. " and voyage_BOLnumber='$bol_number'";
if ($voyage_type!='') 			$query_mani=$query_mani. " and BOL_type='$voyage_type'";
if ($voyage_pagenumber!='') 	$query_mani=$query_mani. " and BOL_pagenumber='$voyage_pagenumber'";
if ($Localagent!='')			$query_mani=$query_mani. " and localagent_code='$Localagent'";
if ($showCont == 0)				$query_mani .= " and ifnull(manifest_emptyContainer,0) = '0' ";		
$resultquery = mysql_query($query_mani,$connection);
//echo $query_mani;exit;
$path="select manifest_path from setup";
$res = mysql_query($path,$connection);
if($rows = mysql_fetch_array($res)){
	$filepath=$rows[0];
}



$k=0;
while($result=mysql_fetch_array($resultquery)){
	//echo $var[$fieldname[$i]];
	$i=0;
	$k++;
	while($i<=81){
		
		
			 if ($fieldname[$i]=='date') 
		$result[$i]= formatdate(substr($result[$i],0,$var[$fieldname[$i]]));
	
		/*if ($fieldname[$i]=='year') {
		$year=substr($result[$i],0,4);
		$month=substr($result[$i],5,2);
		 $days=substr($result[$i],8,2);
		$hours= substr($result[$i],11,2);
		$minutes=substr($result[$i],14,2);
		$seconds=substr($result[$i],17,2);
		fwrite($myfile, "$year" );
		fwrite($myfile, "$month" );
		fwrite($myfile, "$days" );
		fwrite($myfile, "$hours" );
		fwrite($myfile, "$minutes" );
		fwrite($myfile, "$seconds" );
	//	echo "<br><br>";
		
		}*/
		
		if($fieldname[$i]=="Precarriage")
			$addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],' ');
	
	else if ((is_numeric(substr($result[$i],0,$var[$fieldname[$i]])))||($fieldname[$i]=='commfreight')||($fieldname[$i]=='collectedcomm')){
	$addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],'0',STR_PAD_LEFT);
	 }
	 else if ($fieldname[$i]=="voyage_descripiton")
		 $addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],' ',STR_PAD_LEFT);
	else 
		$addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],' ');

		//echo $fieldname[$i]." is ".$addtofile[$i]."<br>";
		// if (($fieldname[$i]!='year')&&($fieldname[$i]!='month')&&($fieldname[$i]!='day')&&
		// ($fieldname[$i]!='hour')&&($fieldname[$i]!='minutes')&&($fieldname[$i]!='seconds')){
		 fwrite($myfile, "$addtofile[$i]" );
		// }
		 
		// echo $fieldname[$i]." is ".$addtofile[$i]." nb= ".$var[$fieldname[$i]]."<br>";
		
	$i++;
	}
     if (($k!=0)||($writemode=='a'))
	{//fwrite($myfile,PHP_EOL );
	fwrite($myfile,"\r\n" );
//echo "dfgdgd";
}
	
	
	
}
}


 if(($file1=='Sndbofl3')||($fileall=='all')){
	//echo "<br> file2";
	if($fileall=='all'){$file1='Sndbofl3';}
	if($file1=='Sndbofl1'){$field='Sndbofl1';}
	if($file1=='Sndbofl3'){$field='Sndoftl3';}
	if($file1=='Sndbofl4'){$field='Sndbofl4';}
	if($file1=='Sndbofl2'){$field='Sndbofl2';}
	$myfile = fopen("$filepath/Export/$file1.txt", "$writemode") or die("Unable to open file!");
	
		$q = "select fieldlength,fieldname1 from Sndbofl  where fieldfamily='$field' ";
	$res = mysql_query($q,$connection);
	//echo $q;
	$j=0;
	
while($r = mysql_fetch_row($res)){
	
	
	//	$newvessel_no = str_pad($vessel_no + 1, 4, 0, STR_PAD_LEFT);
		
	$var[$r[1]] = $r[0];
$fieldname[$j]=$r[1];
$j++;
}
	
$query_mani="select mdt.Voyage_year, mdt.voyage_number, mdt.voyage_destination, mdt.BOL_type, 
			mdt.voyage_BOLnumber, mdt.BOL_pagenumber, mdt.detailsequence,mdt.source_agentcode,CTRFlag,Container,Marks,
			Packages,Container_Weight,CubeMeters, mdt.Voyage_year, mdt.voyage_number, mdt.voyage_destination,mdt.source_agentcode,
			mdt.BOL_type, mdt.voyage_BOLnumber, mdt.BOL_pagenumber, mdt.year,mdt.month,mdt.day,mdt.hour,mdt.minutes,mdt.seconds,detailsequence
			from manifestdt as mdt ";
$query_mani=$query_mani." inner join manifest on manifest.Manifest_auto = manifest_manifestauto ";			
								$query_mani=$query_mani." where 1=1 ";
if($voyage_year!='') 			$query_mani=$query_mani. " and mdt.voyage_year='$voyage_year'";
if($voyage_number!='') 			$query_mani=$query_mani. " and mdt.voyage_number='$voyage_number'";
if($voyage_destination!='') 	$query_mani=$query_mani. " and mdt.voyage_destination='$voyage_destination'";
if($bol_number!='') 			$query_mani=$query_mani. " and mdt.voyage_BOLnumber='$bol_number'";
if($voyage_type!='') 			$query_mani=$query_mani. " and mdt.BOL_type='$voyage_type'";
if($voyage_pagenumber!='') 		$query_mani=$query_mani. " and mdt.BOL_pagenumber='$voyage_pagenumber'";
if($Localagent!='')				$query_mani=$query_mani. " and localagent_code='$Localagent'";
if ($showCont == 0)				$query_mani .= " and ifnull(manifest_emptyContainer,0) = '0' ";		
$resultquery = mysql_query($query_mani,$connection);
//echo $query_mani;
$path="select manifest_path from setup";
$res = mysql_query($path,$connection);
if($rows = mysql_fetch_array($res)){
	$filepath=$rows[0];
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
$k=0;

while($result=mysql_fetch_array($resultquery)){
	//echo $var[$fieldname[$i]];
	$i=0;
	
	$k++;
	while($i<=27){
		
		

	
	
	 if (is_numeric(substr($result[$i],0,$var[$fieldname[$i]])))
		{
	$addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],'0',STR_PAD_LEFT);
	 }
	 else 
		 $addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],' ');
	

		
		 
		 fwrite($myfile, "$addtofile[$i]" );
		 
		 //echo $var[$fieldname[$i]]." nb of ".$fieldname[$i]." is ".$addtofile[$i]."<br>";
		
		
	$i++;
	}
	if (($k!=0)||($writemode=='a')){
	//fwrite($myfile,PHP_EOL );
	fwrite($myfile,"\r\n" );
	//echo "fffffffff";
	}
	//echo "<br><br>";
	
}
}
 if(($file1=='Sndbofl4')||($fileall=='all')){
	
	
	if($fileall=='all'){$file1='Sndbofl4';}
	if($file1=='Sndbofl1'){$field='Sndbofl1';}
	if($file1=='Sndbofl3'){$field='Sndoftl3';}
	if($file1=='Sndbofl4'){$field='Sndbofl4';}
	if($file1=='Sndbofl2'){$field='Sndbofl2';}
	$myfile = fopen("$filepath/Export/$file1.txt", "$writemode") or die("Unable to open file!");
	
		$q = "select fieldlength,fieldname1 from Sndbofl  where fieldfamily='$field' ";
	$res = mysql_query($q,$connection);
	//echo $q;
	$j=0;
	
while($r = mysql_fetch_row($res)){
	
	
	//	$newvessel_no = str_pad($vessel_no + 1, 4, 0, STR_PAD_LEFT);
		
	$var[$r[1]] = $r[0];
$fieldname[$j]=$r[1];
$j++;
}
$query_mani="select mdesc.Voyage_year, mdesc.voyage_number, mdesc.voyage_destination, mdesc.BOL_type, 
			mdesc.voyage_BOLnumber, mdesc.BOL_pagenumber, detailsequence,mdesc.source_agentcode,descriptiongood,
			mdesc.Voyage_year, mdesc.voyage_number, mdesc.voyage_destination,mdesc.source_agentcode,
			mdesc.BOL_type, mdesc.voyage_BOLnumber, mdesc.BOL_pagenumber, mdesc.year,mdesc.month,mdesc.day,mdesc.hour,mdesc.minutes,mdesc.seconds,detailsequence
			from manifestdesc as mdesc ";
$query_mani=$query_mani." left join manifest on manifest.Manifest_auto = manifest_manifestauto ";						
								$query_mani=$query_mani." where 1=1";
if($voyage_year!='') 			$query_mani=$query_mani. " and mdesc.voyage_year='$voyage_year'";
if($voyage_number!='') 			$query_mani=$query_mani. " and mdesc.voyage_number='$voyage_number'";
if($voyage_destination!='') 	$query_mani=$query_mani. " and mdesc.voyage_destination='$voyage_destination'";
if($bol_number!='') 			$query_mani=$query_mani. " and mdesc.voyage_BOLnumber='$bol_number'";
if($voyage_type!='') 			$query_mani=$query_mani. " and mdesc.BOL_type='$voyage_type'";
if($voyage_pagenumber!='') 		$query_mani=$query_mani. " and mdesc.BOL_pagenumber='$voyage_pagenumber'";
if($Localagent!='')				$query_mani=$query_mani. " and localagent_code='$Localagent'";
if ($showCont == 0)				$query_mani .= " and ifnull(manifest_emptyContainer,0) = '0' ";		
	
$resultquery = mysql_query($query_mani,$connection);
//echo $query_mani;
$path="select manifest_path from setup";
$res = mysql_query($path,$connection);
if($rows = mysql_fetch_array($res)){
	$filepath=$rows[0];
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
$k=0;

while($result=mysql_fetch_array($resultquery)){
	//echo $var[$fieldname[$i]];
	$i=0;
	$k++;
	while($i<=23){
		
		

	
	
	 if (is_numeric(substr($result[$i],0,$var[$fieldname[$i]])))
		{
	$addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],'0',STR_PAD_LEFT);
	 }
	 else 
		 $addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],' ');
	

		
		 
		 fwrite($myfile, "$addtofile[$i]" );
		 
		// echo $var[$fieldname[$i]]." nb of ".$fieldname[$i]." is ".$addtofile[$i]."<br>";
		
		
	$i++;
	}
	if (($k!=0)||($writemode=='a')){
	//fwrite($myfile,PHP_EOL );
	fwrite($myfile,"\r\n" );
	//echo "fffffffff";
	}
	
	
	//echo "<br><br>";
	
}


}



if(($file1=='Sndbofl2')||($fileall=='all')){
	
	
	if($fileall=='all'){$file1='Sndbofl2';}
	if($file1=='Sndbofl1'){$field='Sndbofl1';}
	if($file1=='Sndbofl3'){$field='Sndoftl3';}
	if($file1=='Sndbofl4'){$field='Sndbofl4';}
	if($file1=='Sndbofl2'){$field='Sndbofl2';}
	$myfile = fopen("$filepath/Export/$file1.txt", "$writemode") or die("Unable to open file!");
	
		$q = "select fieldlength,fieldname1 from Sndbofl  where fieldfamily='$field' ";
	$res = mysql_query($q,$connection);
	//echo $q;
	$j=0;
	
while($r = mysql_fetch_row($res)){
	
	
	//	$newvessel_no = str_pad($vessel_no + 1, 4, 0, STR_PAD_LEFT);
		
	$var[$r[1]] = $r[0];
$fieldname[$j]=$r[1];
$j++;
}
$query_mani="select mstp.Voyage_year, mstp.voyage_number, mstp.voyage_destination, mstp.BOL_type, mstp.voyage_BOLnumber, mstp.BOL_pagenumber, 
			stampdesc1, stampdesc2, stampdesc3, stampdesc4, stampdesc5, stampdesc6, stampdesc7, stampdesc8, stampdesc9, stampdesc10,
			stampdesc11, stampdesc12, stampdesc13, stampdesc14, stampdesc15, stampdesc16, stampdesc17, stampdesc18, stampdesc19, stampdesc20,
			stampdesc21, stampdesc22, stampcode1, stampcode2, stampcode3, stampcode4,
			stampcode5, stampcode6, mstp.source_agentcode, mstp.Voyage_year, mstp.voyage_number, mstp.voyage_destination,mstp.source_agentcode,mstp.BOL_type, 
			mstp.voyage_BOLnumber, mstp.BOL_pagenumber, mstp.year, mstp.month, mstp.day,mstp.hour, mstp.minutes, mstp.seconds
			from manifeststamp as mstp ";
$query_mani=$query_mani." left join manifest on manifest.Manifest_auto = manifest_manifestauto ";									
 $query_mani=$query_mani." where 1=1";
if($voyage_year!='') 			$query_mani=$query_mani. " and mstp.voyage_year='$voyage_year'";
if($voyage_number!='') 			$query_mani=$query_mani. " and mstp.voyage_number='$voyage_number'";
if($voyage_destination!='') 	$query_mani=$query_mani. " and mstp.voyage_destination='$voyage_destination'";
if($bol_number!='') 			$query_mani=$query_mani. " and mstp.voyage_BOLnumber='$bol_number'";
if($voyage_type!='')			$query_mani=$query_mani. " and mstp.BOL_type='$voyage_type'";
if($voyage_pagenumber!='') 		$query_mani=$query_mani. " and mstp.BOL_pagenumber='$voyage_pagenumber' ";
if($Localagent!='') 			$query_mani=$query_mani. " and localagent_code='$Localagent' ";
if ($showCont == 0)				$query_mani .= " and ifnull(manifest_emptyContainer,0) = '0' ";		
	
$resultquery = mysql_query($query_mani,$connection);

$path="select manifest_path from setup";
$res = mysql_query($path,$connection);
if($rows = mysql_fetch_array($res)){
	$filepath=$rows[0];
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
$k=0;

while($result=mysql_fetch_array($resultquery)){
	//echo $var[$fieldname[$i]];
	$i=0;
	$k++;
	while($i<=48){
		
		

	
	
	 if (is_numeric(substr($result[$i],0,$var[$fieldname[$i]])))
		{$addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],'0',STR_PAD_LEFT);}
	 else 
		 $addtofile[$i]=str_pad(substr($result[$i],0,$var[$fieldname[$i]]), $var[$fieldname[$i]],' ');
	 
		 fwrite($myfile, "$addtofile[$i]" );
		 
		// echo $var[$fieldname[$i]]." nb of ".$fieldname[$i]." is ".$addtofile[$i]."<br>";

	$i++;
	}
	if (($k!=0)||($writemode=='a')){
	//fwrite($myfile,PHP_EOL );
	fwrite($myfile,"\r\n" );
	//echo "fffffffff";
	}
	
	//echo "<br><br>";
	
}


}




//echo "<br>".$file1."<br>";




header('Location: ' . $_SERVER['HTTP_REFERER']);

function formatdate($dateval){
	if($dateval != ''){
	$ndate = explode("-", $dateval);
	$dateval = $ndate[2]."/".$ndate[1]."/".$ndate[0];
}
	return $dateval;
}
?>