<?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");
include("Functions_Synchro.php");
include("Forms_DataGrid_Manifest.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);
}

$perpage=$_POST['perpage'];
//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);
}

//Read From Module Table

$querymodule=" Select ifNull(Module_Synchro,0) As Module_Synchro From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}


$query_setup=" Select base1 As base1, base2 as base2, Setup_Branch from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

$seq=$_GET['seq'];

//echo $billoflading."fg";

$dateval= date("Y-m-d H:i:s");

$issuedon 			= formatdate($issuedon);
$manifest_date 		= formatdate($manifest_date);
$received_date		= formatdate($received_date);

if($manifest_date == '') 
	$manifest_date = "NULL";
else $manifest_date = "'".$manifest_date."'";

if($issuedon == '') 
	$issuedon = "NULL";
else $issuedon = "'".$issuedon."'";

if($received_date == '') 
	$received_date = "NULL";
else $received_date = "'".$received_date."'";

if($declaredbyforwarder == '') $declaredbyforwarder = 0;
if ($withEmptyContainer == '') $withEmptyContainer = 0;

$message="";
$seqdt = $_GET['seqdt'];

if($modemanifestdt =='insert'){
	
	if($mainmode=='insertmanifest'){
		
	$datesubs=formatdatetime(date('Y:m:d:H:i:s'));
	$year		= $datesubs[0];
	$month		= $datesubs[1];
	$day		= $datesubs[2];
	$hour		= $datesubs[3];
	$minutes	= $datesubs[4];
	$second		= $datesubs[5];
		
		
	$manifest_orderid = get_nextsequence('manifest_orderid','',$connection);	
		
		//echo "insert into manifest";
	$query="SELECT manifest_id,manifest_auto FROM manifest ORDER BY manifest_auto DESC LIMIT 1";
	$result  = mysql_query($query, $connection);
	if($row=mysql_fetch_array($result)){$manifest_id=$row[0];}
	
	$newmanifest_id =$manifest_id + 1;
		
		$query="insert into manifest(
				Manifest_date, Manifest_sysdate, Voyage_year,voyage_number,voyage_destination,voyage_BOLnumber,
				BOL_type,BOL_pagenumber,vessel_id,  BOL_identification, Shipper1, shipper2, shipper3, shipper4, 
				consignee1, consignee2, consignee3, consignee4, Notify1, Notify2, Notify3, Notify4,
				agent_id, No_of_tues,currency, 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,
				Precarriage, PlaceofReceipt, Portofloading, PortofDischarge, Placeofdelivery, 
				BOL_freight,Freight_type, issued, date, Negotiable_BOL, term_of_carriage, 
				manifest_id, Agent1_id, linear_meters, freetimedays, Offernumber,received_date,client_code,
				stamp1_id,stamp2_id,stamp3_id,stamp4_id,stamp5_id,stamp6_id,source_agentcode,
				year, month, day, hour, minutes, seconds,line_operator, localagent_code, declaredbyforwarder, manifest_emptyContainer, containeragent_code, manifest_orderid, other_remark)
 
				values($manifest_date,now(),".strformat($voyage_year).",".strformat($voyage_number).",".strformat($voyage_destination).",".strformat($bol_number).",
				".strformat($voyage_type).",".strformat($voyage_pagenumber).",".strformat($vessel_id).",
				".strformat($billoflading).",".strformat($shipper1).",".strformat($shipper2).",".strformat($shipper3).",".strformat($shipper4).",
				".strformat($consignee1).",".strformat($consignee2).",".strformat($consignee3).",".strformat($consignee4).",
				".strformat($notify1).",".strformat($notify2).",".strformat($notify3).",".strformat($notify4).",
				".strformat($agent_id).", ".strformat($nbofteus).",".strformat($currency).", ".strformat($desc1).",".strformat($desc2).",".strformat($desc3).",
				".strformat($desc4).",".strformat($desc5).",".strformat($desc6).",
				".strformat($amount1)." ,".strformat($amount2)." ,".strformat($amount3)." ,".strformat($amount4)." ,".strformat($amount5).",".strformat($amount6).",
				".strformat($evidence1).",".strformat($evidence2).",".strformat($evidence3).",".strformat($evidence4).",".strformat($evidence5).",".strformat($evidence6).",
				".strformat($precarriage).",".strformat($recieptplace).",".strformat($portlading).",".strformat($portdischarge).",".strformat($placedelivery).",
				".strformat($freight).",".strformat($freight_type).",".strformat($issuedat).",$issuedon,".strformat($nonegotiable).",".strformat($termcarriage).",$newmanifest_id,
				".strformat($carriersign).",".strformat($linearmeter).",".strformat($freetimedays).",".strformat($offer_number).",$received_date,".strformat($client_name).",
				".strformat($stamp1).",".strformat($stamp2).",".strformat($stamp3).",".strformat($stamp4).",".strformat($stamp5).",".strformat($stamp6).",".strformat($sourceagent)."
				,'$year','$month','$day','$hour','$minutes','$second', ".strformat($lineoperator).", ".strformat($localagent_code).", ".$declaredbyforwarder.", ".$withEmptyContainer.",
				".strformat($containeragent_code).", ".$manifest_orderid.", ".strformat($otherRemark)."); ";
 
 
 //echo $query;
$result  = mysql_query($query, $connection);
$lastid = mysql_insert_id($connection);
$Manifest_Auto=$lastid;
	
}
	
	$insertseq="select max(detailsequence) from manifestdt where manifest_manifestauto = '$Manifest_Auto';";
	$result  = mysql_query($insertseq, $connection);
	$row=mysql_fetch_row($result);
	$sequence=$row[0];
 
 
 $sequence=$sequence+1;
 $sequence=str_pad($sequence, 3,'0',STR_PAD_LEFT);
 $cntrexist=0;
 $containerexist="select  count(cntr_code)  from container  where cntr_code='$container'";
 $resultcntr  = mysql_query($containerexist, $connection);
 $rows=mysql_fetch_row($resultcntr);
	 if ($rows[0]>=1) $cntrexist=1;
 
//echo "seqafter=".$sequence;
//if(($cntrexist==1)||($flag=='*') || $cntrtype == '1'){
	$querydate = "select year, month, day, hour, minutes, seconds from manifest where Manifest_Auto = $Manifest_Auto ";
	$resdate  = mysql_query($querydate, $connection);
	$rwdate = mysql_fetch_row($resdate);
	$cntrexist=1;
	
	$year		= $rwdate[0];
	$month		= $rwdate[1];
	$day		= $rwdate[2];
	$hour		= $rwdate[3];
	$minutes	= $rwdate[4];
	$second		= $rwdate[5];
		fbegin();
$query1=" insert into manifestdt ( Manifest_sysdate, Voyage_year, voyage_number, voyage_destination, 
		 voyage_BOLnumber, BOL_type, BOL_pagenumber,detailsequence,
		 Container, Marks, Packages, Container_Weight, CubeMeters,ctrflag,BOL_identification,manifest_manifestauto,
		 year, month, day, hour, minutes, seconds,source_agentcode, bctc_remark, detailtype, container_type)
		 values( now(),".strformat($voyage_year).",".strformat($voyage_number).",".strformat($voyage_destination).",
		 ".strformat($bol_number).",".strformat($voyage_type).",".strformat($voyage_pagenumber).",".strformat($sequence).",".strformat($container).",
		 ".strformat($marks).",".strformat($package).",".strformat($weight).",".strformat($measurement).",".strformat($flag).",".strformat($billoflading).",'$Manifest_Auto'
		 ,'$year','$month','$day','$hour','$minutes','$second',".strformat($sourceagent).", ".strformat($bctc_remark).", ".strformat($DetailType).", ".strformat($cntrtype)." );";

$result1  = mysql_query($query1, $connection);
	if ($result1){
		fcommit();
		$inser="insert into manifestdesc (  Manifest_sysdate, Voyage_year, voyage_number, voyage_destination, voyage_BOLnumber, BOL_type, BOL_pagenumber,
				detailsequence, descriptiongood,manifest_manifestauto,BOL_identification,
				year, month, day, hour, minutes, seconds,source_agentcode) values(now(),".strformat($voyage_year).",".strformat($voyage_number).",".strformat($voyage_destination).",
				".strformat($bol_number).",".strformat($voyage_type).",".strformat($voyage_pagenumber).",".strformat($sequence).",".strformat($txtdes).",'$Manifest_Auto',
				".strformat($billoflading).",'$year','$month','$day','$hour','$minutes','$second',".strformat($sourceagent).")";
		$result  = mysql_query($inser, $connection); 

	}
	 else{
		frollback();
	}  
//}
if($mainmode=='insertmanifest'){
echo $lastid;
echo "[BRK]";
ShowDeliveryDtGrid($connection,'Manifest_Dt',$Manifest_Auto,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],'ManifestDt.DeliveryDt_ID');
echo "[BRK]";
echo $cntrexist;
echo "[BRK]";
}
else {
	
	echo "<input type='hidden' name='cntrexist' value='$cntrexist' id='cntrexist'>";
	ShowDeliveryDtGrid($connection,'Manifest_Dt',$Manifest_Auto,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],'ManifestDt.DeliveryDt_ID');

}

}
else if ($modemanifestdt=='delete'){
	
	$query="select Voyage_year,voyage_number, voyage_destination, voyage_BOLnumber, voyage_descripiton, BOL_type, BOL_pagenumber,
	vessel_id, BOL_identification, detailsequence
	from manifestdt where auto='$manifestdtauto'";
	$result  = mysql_query($query, $connection);
	//echo $query;
	$resultselect=mysql_fetch_row($result);
		$voyage_year=$resultselect[0];
		$voyage_number=$resultselect[1];
		$voyage_destination=$resultselect[2];
		$voyage_BOLnumber=$resultselect[3];
		$voyage_descripiton=$resultselect[4];
		$BOL_type=$resultselect[5];
		$BOL_pagenumber=$resultselect[6];
		$vessel_id=$resultselect[7];
		$BOL_identification=$resultselect[8];
		$detailsequence=$resultselect[9];
		
		
	$query="delete  from manifestdesc where manifest_manifestauto='$Manifest_Auto' and detailsequence='$detailsequence'";
	$result  = mysql_query($query, $connection);
	$query="delete from manifestdt where auto='$manifestdtauto'";
	$result  = mysql_query($query, $connection);
	//echo $query;
	
	//echo $query;
	//echo $query;
	ShowDeliveryDtGrid($connection,'Manifest_Dt',$Manifest_Auto,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],'ManifestDt.DeliveryDt_ID');
//echo "[BRK]";
	
	
}
else if($modemanifestdt=='edit'){
	
	$cntrexist=0;
	$containerexist="select  count(cntr_code)  from container  where cntr_code='$container'";
	$resultcntr  = mysql_query($containerexist, $connection);
	$rows=mysql_fetch_row($resultcntr);
	 if ($rows[0]>=1) $cntrexist=1;

 
// if(($cntrexist==1)||($flag=='*') || $cntrtype == '1'){
	 $cntrexist=1;
	 
	 fbegin();
	$query = " update manifestdt set Container=".strformat($container).", Marks=".strformat($marks).", Packages=".strformat($package).", Container_Weight=".strformat($weight).",
					CubeMeters=".strformat($measurement)." , Voyage_year=".strformat($voyage_year)." , voyage_number=".strformat($voyage_number)." ,  
					voyage_destination=".strformat($voyage_destination)." ,  voyage_BOLnumber=".strformat($bol_number).", BOL_type=".strformat($voyage_type)." , 
					BOL_pagenumber=".strformat($voyage_pagenumber).",BOL_identification=".strformat($billoflading).",manifest_manifestauto='$Manifest_Auto',
					source_agentcode=".strformat($sourceagent).", detailsequence = ".strformat($seqdt).", bctc_remark = ".strformat($bctc_remark).",
					detailtype = ".strformat($DetailType).", container_type = ".strformat($cntrtype)."
				where auto='$manifestdtauto';";
//echo $query;
  $result  = mysql_query($query, $connection);
 
if ($result){
	fcommit();			
	$query=" update manifestdesc set descriptiongood=".strformat($txtdes).",source_agentcode=".strformat($sourceagent)." ,Voyage_year=".strformat($voyage_year)." , 
						voyage_number=".strformat($voyage_number)." ,  voyage_destination=".strformat($voyage_destination)." , voyage_BOLnumber=".strformat($bol_number).", 
						BOL_type=".strformat($voyage_type)." , BOL_pagenumber=".strformat($voyage_pagenumber).",BOL_identification=".strformat($billoflading).",
						manifest_manifestauto='$Manifest_Auto', detailsequence = ".strformat($seqdt)."
			where  detailsequence='$seq' and   manifest_manifestauto='$Manifest_Auto';";
	$result  = mysql_query($query, $connection);


}
else {
	frollback();
}
	 ///echo $query;
//}
echo "<input type='hidden' name='cntrexist' value='$cntrexist' id='cntrexist'>";
 ShowDeliveryDtGrid($connection,'Manifest_Dt',$Manifest_Auto,$_SESSION['gdl_parameters'],$_SESSION['gdl_arrayaccess'],'ManifestDt.DeliveryDt_ID');
//echo "[BRK]";
}




function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}




mysql_close($connection);



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




function formatdatetime($dateval){
	if($dateval != ''){
	$ndate = explode(":", $dateval);
	
}
	return $ndate;
}

 function strformat($str){
	if($str == '')
		$str = "NULL";
	else
		$str = "'".ereg_replace("'", "\'", $str)."'";
		
	return $str;	
}


?>
		           