<?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_Logistics.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) 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);
}


$querylog="SELECT FL_OperationLogisticApp as loginaccess, user_account, Language 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);
}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");

$mainmode 				= $_POST['mainmode'];  					if ($mainmode == '') 				$mainmode 				= $_GET['mainmode'];
$message 				= $_POST['message'];
$olId 					= $_POST['olId'];						if ($olId == '') 					$olId 					= $_GET['olId'];
$olDate 				= $_POST['olDate'];						if ($olDate == '') 					$olDate 				= $_GET['olDate'];
$olClientCode 			= $_POST['olClientCode'];				if ($olClientCode == '') 			$olClientCode 			= $_GET['olClientCode'];
$olServiceTypeId 		= $_POST['olServiceTypeId'];			if ($olServiceTypeId == '') 		$olServiceTypeId 		= $_GET['olServiceTypeId'];
$olServiceSeqId 		= $_POST['olServiceSeqId'];				if ($olServiceSeqId == '') 			$olServiceSeqId 		= $_GET['olServiceSeqId'];
$olClientRef 			= $_POST['olClientRef'];				if ($olClientRef == '') 			$olClientRef 			= $_GET['olClientRef'];
$olFreeDem		 		= $_POST['olFreeDem'];					if ($olFreeDem == '') 				$olFreeDem 				= $_GET['olFreeDem'];
$olProjectCode 			= $_POST['olProjectCode'];				if ($olProjectCode == '') 			$olProjectCode 			= $_GET['olProjectCode'];
$olStatus 				= $_POST['olStatus'];					if ($olStatus == '') 				$olStatus 				= $_GET['olStatus'];
$olDealerId				= $_POST['olDealerId'];					if ($olDealerId == '') 				$olDealerId 			= $_GET['olDealerId'];
$olTeamId				= $_POST['olTeamId'];					if ($olTeamId == '') 				$olTeamId 				= $_GET['olTeamId'];
$olShipCoId				= $_POST['olShipCoId'];					if ($olShipCoId == '') 				$olShipCoId 			= $_GET['olShipCoId'];
$olEtd					= $_POST['olEtd'];						if ($olEtd == '') 					$olEtd 					= $_GET['olEtd'];
$olTwenty				= $_POST['olTwenty'];					if ($olTwenty == '') 				$olTwenty 				= $_GET['olTwenty'];
$olForty				= $_POST['olForty'];					if ($olForty == '') 				$olForty 				= $_GET['olForty'];
$olEta					= $_POST['olEta'];						if ($olEta == '') 					$olEta 					= $_GET['olEta'];
$olRot					= $_POST['olRot'];						if ($olRot == '') 					$olRot 					= $_GET['olRot'];
$olDob					= $_POST['olDob'];						if ($olDob == '') 					$olDob 					= $_GET['olDob'];
$olDobDays				= $_POST['olDobDays'];					if ($olDobDays == '') 				$olDobDays 				= $_GET['olDobDays'];
$olTerminalId			= $_POST['olTerminalId'];				if ($olTerminalId == '') 			$olTerminalId 			= $_GET['olTerminalId'];
$olCopyDocRc			= $_POST['olCopyDocRc'];				if ($olCopyDocRc == '') 			$olCopyDocRc 			= $_GET['olCopyDocRc'];
$olDocsRcvd				= $_POST['olDocsRcvd'];					if ($olDocsRcvd == '') 				$olDocsRcvd 			= $_GET['olDocsRcvd'];
$olOblRcvd				= $_POST['olOblRcvd'];					if ($olOblRcvd == '') 				$olOblRcvd 				= $_GET['olOblRcvd'];
$olPaarRcvd				= $_POST['olPaarRcvd'];					if ($olPaarRcvd == '') 				$olPaarRcvd 			= $_GET['olPaarRcvd'];
$olAssessment			= $_POST['olAssessment'];				if ($olAssessment == '') 			$olAssessment 			= $_GET['olAssessment'];
$olCnumber				= $_POST['olCnumber'];					if ($olCnumber == '') 				$olCnumber 				= $_GET['olCnumber'];
$olSonPaid				= $_POST['olSonPaid'];					if ($olSonPaid == '') 				$olSonPaid 				= $_GET['olSonPaid'];
$olStamp1				= $_POST['olStamp1'];					if ($olStamp1 == '') 				$olStamp1 				= $_GET['olStamp1'];
$olStamp2				= $_POST['olStamp2'];					if ($olStamp2 == '') 				$olStamp2 				= $_GET['olStamp2'];
$olDutyPaid				= $_POST['olDutyPaid'];					if ($olDutyPaid == '') 				$olDutyPaid 			= $_GET['olDutyPaid'];
$olDutyNbr				= $_POST['olDutyNbr'];					if ($olDutyNbr == '') 				$olDutyNbr 				= $_GET['olDutyNbr'];
$olTerminalRatePer		= $_POST['olTerminalRatePer'];			if ($olTerminalRatePer == '') 		$olTerminalRatePer 		= $_GET['olTerminalRatePer'];
$olTerminalPaid			= $_POST['olTerminalPaid'];				if ($olTerminalPaid == '') 			$olTerminalPaid 		= $_GET['olTerminalPaid'];
$olTerminalAdd			= $_POST['olTerminalAdd'];				if ($olTerminalAdd == '') 			$olTerminalAdd 			= $_GET['olTerminalAdd'];
$olShippingRatePer		= $_POST['olShippingRatePer'];			if ($olShippingRatePer == '') 		$olShippingRatePer 		= $_GET['olShippingRatePer'];
$olShippingPaid			= $_POST['olShippingPaid'];				if ($olShippingPaid == '') 			$olShippingPaid 		= $_GET['olShippingPaid'];
$olShippingAdd			= $_POST['olShippingAdd'];				if ($olShippingAdd == '') 			$olShippingAdd 			= $_GET['olShippingAdd'];
$olExamBook				= $_POST['olExamBook'];					if ($olExamBook == '') 				$olExamBook 			= $_GET['olExamBook'];
$olExamined				= $_POST['olExamined'];					if ($olExamined == '') 				$olExamined 			= $_GET['olExamined'];
$olCustReleased			= $_POST['olCustReleased'];				if ($olCustReleased == '') 			$olCustReleased 		= $_GET['olCustReleased'];
$olShipReleased			= $_POST['olShipReleased'];				if ($olShipReleased == '') 			$olShipReleased 		= $_GET['olShipReleased'];
$olTdo					= $_POST['olTdo'];						if ($olTdo == '') 					$olTdo 					= $_GET['olTdo'];
$olLoadingCompleted		= $_POST['olLoadingCompleted'];			if ($olLoadingCompleted == '') 		$olLoadingCompleted 	= $_GET['olLoadingCompleted'];
$olDeliveryCompleted	= $_POST['olDeliveryCompleted'];		if ($olDeliveryCompleted == '') 	$olDeliveryCompleted 	= $_GET['olDeliveryCompleted'];
$olWaybRcvd				= $_POST['olWaybRcvd'];					if ($olWaybRcvd == '') 				$olWaybRcvd 			= $_GET['olWaybRcvd'];
$olContainerRcvd		= $_POST['olContainerRcvd'];			if ($olContainerRcvd == '') 		$olContainerRcvd 		= $_GET['olContainerRcvd'];
$olDobDeliver			= $_POST['olDobDeliver'];				if ($olDobDeliver == '') 			$olDobDeliver 			= $_GET['olDobDeliver'];
$olDutyDeliver			= $_POST['olDutyDeliver'];				if ($olDutyDeliver == '') 			$olDutyDeliver 			= $_GET['olDutyDeliver'];
$olInvoiceIssuedOn		= $_POST['olInvoiceIssuedOn'];			if ($olInvoiceIssuedOn == '') 		$olInvoiceIssuedOn 		= $_GET['olInvoiceIssuedOn'];
$olInvoiceBlanace		= $_POST['olInvoiceBlanace'];			if ($olInvoiceBlanace == '') 		$olInvoiceBlanace 		= $_GET['olInvoiceBlanace'];
$olDslRef				= $_POST['olDslRef'];					if ($olDslRef == '') 				$olDslRef 				= $_GET['olDslRef'];
$olRemark				= $_POST['olRemark'];					if ($olRemark == '') 				$olRemark 				= $_GET['olRemark'];

$olProductionDesc		= $_POST['olProductionDesc'];			if ($olProductionDesc == '') 		$olProductionDesc 		= $_GET['olProductionDesc'];
$olBLNbr				= $_POST['olBLNbr'];					if ($olBLNbr == '') 				$olBLNbr 				= $_GET['olBLNbr'];
$olFormMValidate		= $_POST['olFormMValidate'];			if ($olFormMValidate == '') 		$olFormMValidate 		= $_GET['olFormMValidate'];
$olFormMApproved		= $_POST['olFormMApproved'];			if ($olFormMApproved == '') 		$olFormMApproved 		= $_GET['olFormMApproved'];
$olInsuranceRcvd		= $_POST['olInsuranceRcvd'];			if ($olInsuranceRcvd == '') 		$olInsuranceRcvd 		= $_GET['olInsuranceRcvd'];
$olInsuranceRqst		= $_POST['olInsuranceRqst'];			if ($olInsuranceRqst == '') 		$olInsuranceRqst 		= $_GET['olInsuranceRqst'];
$olMFNumber				= $_POST['olMFNumber'];					if ($olMFNumber == '') 				$olMFNumber 			= $_GET['olMFNumber'];
$olOBLFrom				= $_POST['olOBLFrom'];					if ($olOBLFrom == '') 				$olOBLFrom 				= $_GET['olOBLFrom'];
$olPaarNbr				= $_POST['olPaarNbr'];					if ($olPaarNbr == '') 				$olPaarNbr 				= $_GET['olPaarNbr'];
$olPaarRqsted			= $_POST['olPaarRqsted'];				if ($olPaarRqsted == '') 			$olPaarRqsted 			= $_GET['olPaarRqsted'];
$olPFIDate				= $_POST['olPFIDate'];					if ($olPaarRqsted == '') 			$olPaarRqsted 			= $_GET['olPaarRqsted'];
$olRegularDocRcvd		= $_POST['olRegularDocRcvd'];			if ($olRegularDocRcvd == '') 		$olRegularDocRcvd 		= $_GET['olRegularDocRcvd'];
$olRegularDocRqst		= $_POST['olRegularDocRqst'];			if ($olRegularDocRqst == '') 		$olRegularDocRqst 		= $_GET['olRegularDocRqst'];
$olRotNbr				= $_POST['olRotNbr'];					if ($olRotNbr == '') 				$olRotNbr 				= $_GET['olRotNbr'];
$olVesselName			= $_POST['olVesselName'];				if ($olVesselName == '') 			$olVesselName 			= $_GET['olVesselName'];

$olFileHandledClearing	= $_POST['olFileHandledClearing'];		if ($olFileHandledClearing == '') 	$olFileHandledClearing 	= $_GET['olFileHandledClearing'];
$olContactName		 	= $_POST['olContactName'];				if ($olContactName == '') 			$olContactName 			= $_GET['olContactName'];
$olContactPhone		 	= $_POST['olContactPhone'];				if ($olContactPhone == '') 			$olContactPhone 		= $_GET['olContactPhone'];
$olDutyRcvdPaid		 	= $_POST['olDutyRcvdPaid'];				if ($olDutyRcvdPaid == '') 			$olDutyRcvdPaid 		= $_GET['olDutyRcvdPaid'];
$olDestinationId		= $_POST['olDestinationId'];			if ($olDestinationId == '') 		$olDestinationId 		= $_GET['olDestinationId'];
$olLclNbr		 		= $_POST['olLclNbr'];					if ($olLclNbr == '') 				$olLclNbr 				= $_GET['olLclNbr'];
$olRoroShipment		 	= $_POST['olRoroShipment'];				if ($olRoroShipment == '') 			$olRoroShipment 		= $_GET['olRoroShipment'];
$olShipmentType		 	= $_POST['olShipmentType'];				if ($olShipmentType == '') 			$olShipmentType 		= $_GET['olShipmentType'];

$olFormMRqstdate		= $_POST['olFormMRqstdate'];			if ($olFormMRqstdate == '') 		$olFormMRqstdate 		= $_GET['olFormMRqstdate'];
$olClearingStatus		= $_POST['olClearingStatus'];			if ($olClearingStatus == '') 		$olClearingStatus 		= $_GET['olClearingStatus'];
$olRepo		 			= $_POST['olRepo'];						if ($olRepo == '') 					$olRepo 				= $_GET['olRepo'];
$olExamSchedule		 	= $_POST['olExamSchedule'];				if ($olExamSchedule == '') 			$olExamSchedule 		= $_GET['olExamSchedule'];
$olTdoExpiry		 	= $_POST['olTdoExpiry'];				if ($olTdoExpiry == '') 			$olTdoExpiry 			= $_GET['olTdoExpiry'];

// Remark table
$modedt		 			= $_GET['modedt'];
$remId		 			= $_GET['remId'];
$lrType		 			= $_GET['lrType'];
$lrRemark		 		= $_GET['lrRemark'];
$lrDate		 			= $_GET['lrDate'];
$lrtime		 			= $_GET['lrtime'];
if ($lrtime == '') $lrtime = "12:00";
if ($lrDate != '') $lrDate = $lrDate." ".$lrtime.":00";

// Container
$cntr_type              = $_GET['cntr_type'];
$contId                 = $_GET['contId'];
$lcRemark               = $_GET['lcRemark'];

$message = '';
$ErrorExists = 0;


if ($mainmode == 'insert' || $mainmode == 'edit'){
	if ($olProjectCode != ''){
		$checkProj = "select 1 from project where project_code = ".GetSQLValueString($olProjectCode,'text')." ";
		$resProj = mysql_query($checkProj, $connection);
		$rwProj = mysql_num_rows($resProj);
		if ($rwProj == 0){
			$message = 'Insert/Edit Not Allowed! Please check For File Code!';
			$ErrorExists = 1;
		}
	}
	
	if ($olClientCode != ''){
		$checkClient = "select 1 from clients where ledger_number = ".GetSQLValueString($olClientCode,'text')." ";
		$resClient = mysql_query($checkClient, $connection);
		$rwClient = mysql_num_rows($resClient);
		if ($rwClient == 0){
			$message = 'Insert/Edit Not Allowed! Please check For Client Code!';
			$ErrorExists = 1;
		}
	}
}


if ($mainmode == 'insert' && $olId == '' && $ErrorExists == 0){
	$queryins 	= "insert into logistic_operations (LO_CreationDate, Client_code, LO_ServiceTypeId, LO_SeqId, 
							LO_ClientRef, LO_FreeDemarrage, LO_Status, 
							Project_Code, LO_DealerId, LO_TeamId, 
							LO_ShipCoId, LO_twenty, LO_Forty, 
							LO_ETDDate, LO_ETADate, LO_ROTDate, 
							LO_DOBDate, LO_DOBDays, LO_TerminalID, 
							LO_CopyDocReceivedDate, LO_AllDocReceivedDate, LO_OBLReceivedDate, 
							LO_PAARReceivedDate, LO_AssessmentDate, LO_CNumber, 
							LO_SONPaidDate, LO_Stamp1Date, LO_Stamp2Date,
							LO_DutyPaidDate, LO_DutyDays, LO_TerminalRatePerDate, 
							LO_TerminalPaidDate, LO_terminalAdditionalDate, LO_ShippingRatePerDate, 
							LO_ShippingPaidDate, LO_ShippingAdditionalDate, LO_ExaminationBookDate, 
							LO_ExaminedDate, LO_custReleasedDate, LO_ShipCoReleaseDate, 
							LO_TDODate, LO_LoadingCompleteDate, LO_DeliveryCompletedDate, 
							LO_WayBillReceivedDate, LO_ContainerCardReceivedDate, LO_DOBDeliver, 
							LO_DutyDeliver, LO_InvoiceIssuedOnDate, LO_InvoiceBalance, 
							LO_DslReference, LO_ProductionDescription, LO_BLNumber,
							LO_FormMValidateDate, LO_FormMApprovedDate, LO_InsuranceReceivedDate,
							LO_InsuranceRequestedDate, LO_MFNumber, LO_OBLReceivedFrom,
							LO_PAARNumber, LO_PAARRquestedDate, LO_PFIDate,
							LO_PFINumber, LO_RegularDocReceivedDate, LO_RegularDocRquestedDate,
							LO_ROTNumber, LO_VesselName, LO_FileHandledDate,
							LO_ContactName, LO_ContactPhoneNbr, LO_DutyRcvdPaidDate,
							LO_DestinationId, LO_LCLNumber, LO_ROROShipmentDate,
							LO_ShipmentType, LO_FormMRqstDate, LO_ClearingStatus,
							LO_RepositionDate, LO_ExamScheduleDate, LO_TDOExpiryDate,
							user_account, LO_Remark
							) 
					values 
					(".GetSQLValueString(formatDate($olDate),'text').", ".GetSQLValueString($olClientCode,'text').", ".GetSQLValueString($olServiceTypeId,'int').", ".GetSQLValueString($olServiceSeqId,'text').", 
					".GetSQLValueString($olClientRef,'text')." , ".GetSQLValueString($olFreeDem,'double').", ".GetSQLValueString($olStatus,'text').", 
					".GetSQLValueString($olProjectCode,'text')." , ".GetSQLValueString($olDealerId,'int').", ".GetSQLValueString($olTeamId,'int').", 
					".GetSQLValueString($olShipCoId,'int')." , ".GetSQLValueString($olTwenty,'double').", ".GetSQLValueString($olForty,'double').", 
					".GetSQLValueString(formatDate($olEtd),'text')." ,".GetSQLValueString(formatDate($olEta),'text')." ,".GetSQLValueString(formatDate($olRot),'text')." ,
					".GetSQLValueString(formatDate($olDob),'text')." ,".GetSQLValueString($olDobDays,'int')." ,".GetSQLValueString($olTerminalId,'int')." ,
					".GetSQLValueString(formatDate($olCopyDocRc),'text')." ,".GetSQLValueString(formatDate($olDocsRcvd),'text')." ,".GetSQLValueString(formatDate($olOblRcvd),'text')." ,
					".GetSQLValueString(formatDate($olPaarRcvd),'text')." ,".GetSQLValueString(formatDate($olAssessment),'text')." ,".GetSQLValueString($olCnumber,'text')." ,
					".GetSQLValueString(formatDate($olSonPaid),'text')." ,".GetSQLValueString(formatDate($olStamp1),'text')." ,".GetSQLValueString(formatDate($olStamp2),'text')." ,
					".GetSQLValueString(formatDate($olDutyPaid),'text')." ,".GetSQLValueString($olDutyNbr,'double')." ,".GetSQLValueString(formatDate($olTerminalRatePer),'text')." ,
					".GetSQLValueString(formatDate($olTerminalPaid),'text')." ,".GetSQLValueString(formatDate($olTerminalAdd),'text')." ,".GetSQLValueString(formatDate($olShippingRatePer),'text')." ,
					".GetSQLValueString(formatDate($olShippingPaid),'text')." ,".GetSQLValueString(formatDate($olShippingAdd),'text')." ,".GetSQLValueString(formatDate($olExamBook),'text')." ,
					".GetSQLValueString(formatDate($olExamined),'text')." ,".GetSQLValueString(formatDate($olCustReleased),'text')." ,".GetSQLValueString(formatDate($olShipReleased),'text')." ,
					".GetSQLValueString(formatDate($olTdo),'text')." ,".GetSQLValueString(formatDate($olLoadingCompleted),'text')." ,".GetSQLValueString(formatDate($olDeliveryCompleted),'text')." ,
					".GetSQLValueString(formatDate($olWaybRcvd),'text')." ,".GetSQLValueString(formatDate($olContainerRcvd),'text')." ,".GetSQLValueString($olDobDeliver,'double')." ,
					".GetSQLValueString($olDutyDeliver,'double')." ,".GetSQLValueString(formatDate($olInvoiceIssuedOn),'text')." ,".GetSQLValueString($olInvoiceBlanace,'double')." ,
					".GetSQLValueString($olDslRef,'text').", ".GetSQLValueString($olProductionDesc,'text').", ".GetSQLValueString($olBLNbr,'text').",
					".GetSQLValueString(formatDate($olFormMValidate),'text').", ".GetSQLValueString(formatDate($olFormMApproved),'text').",".GetSQLValueString(formatDate($olInsuranceRcvd),'text').",
					".GetSQLValueString(formatDate($olInsuranceRqst),'text').", ".GetSQLValueString($olMFNumber,'text').", ".GetSQLValueString($olOBLFrom,'text').",
					".GetSQLValueString($olPaarNbr,'text').", ".GetSQLValueString(formatDate($olPaarRqsted),'text').", ".GetSQLValueString(formatDate($olPFIDate),'text').", 
					".GetSQLValueString($olPFINbr,'text').", ".GetSQLValueString(formatDate($olRegularDocRcvd),'text').", ".GetSQLValueString(formatDate($olRegularDocRqst),'text').", 
					".GetSQLValueString($olRotNbr,'text').", ".GetSQLValueString($olVesselName,'text').", ".GetSQLValueString(formatDate($olFileHandledClearing),'text').",
					".GetSQLValueString($olContactName,'text').", ".GetSQLValueString($olContactPhone,'text').", ".GetSQLValueString(formatDate($olDutyRcvdPaid),'text').",	
					".GetSQLValueString($olDestinationId,'text').", ".GetSQLValueString($olLclNbr,'text').", ".GetSQLValueString(formatDate($olRoroShipment),'text').",		
					".GetSQLValueString($olShipmentType,'text').", ".GetSQLValueString(formatDate($olFormMRqstdate),'text').", ".GetSQLValueString($olClearingStatus,'text').", 
					".GetSQLValueString(formatDate($olRepo),'text').", ".GetSQLValueString(formatDate($olExamSchedule),'text').", ".GetSQLValueString(formatDate($olTdoExpiry),'text').", 
					'".$_SESSION['useraccount']."', ".GetSQLValueString($olRemark,'text')."
					)";   
	
 	$resins 	= mysql_query($queryins, $connection);
	if($resins){
		$olId = mysql_insert_id();
		if ($olServiceTypeId != ''){
			$updateSeq = "update Logistic_Services set service_seqId = ifnull(service_seqId,0)+1 where service_id = $olServiceTypeId";
			$resSeq = mysql_query($updateSeq, $connection);
		}
	}
	$message = mysql_error(); 
	
}


else if ($mainmode == 'edit' && $olId != '' && $ErrorExists == 0){
	$querySel = "select LO_ServiceTypeId, LO_SeqId from logistic_operations where LO_id = $olId ";
	$resSel = mysql_query($querySel, $connection);
	$rowSel = mysql_fetch_row($resSel);
	$prevServiceTypeId = $rowSel[0];
	
	if ($olServiceTypeId == $prevServiceTypeId){
		$olServiceSeqId = $rowSel[1];
	}
	
	$queryup 	= "update logistic_operations set 
						LO_CreationDate = ".GetSQLValueString(formatDate($olDate),'text').", Client_code = ".GetSQLValueString($olClientCode,'text').", 
						LO_ServiceTypeId = ".GetSQLValueString($olServiceTypeId,'int').", LO_SeqId = ".GetSQLValueString($olServiceSeqId,'text').", 
						LO_ClientRef = ".GetSQLValueString($olClientRef,'text').", LO_FreeDemarrage = ".GetSQLValueString($olFreeDem,'double').",
						LO_Status = ".GetSQLValueString($olStatus,'text').", Project_Code = ".GetSQLValueString($olProjectCode,'text')." , 
						LO_DealerId = ".GetSQLValueString($olDealerId,'int').", LO_TeamId = ".GetSQLValueString($olTeamId,'int').", 
						LO_ShipCoId = ".GetSQLValueString($olShipCoId,'int').", LO_twenty = ".GetSQLValueString($olTwenty,'double').", LO_Forty = ".GetSQLValueString($olForty,'double').",
						LO_ETDDate = ".GetSQLValueString(formatDate($olEtd),'text').", LO_ETADate = ".GetSQLValueString(formatDate($olEta),'text').", LO_ROTDate = ".GetSQLValueString(formatDate($olRot),'text').", 
						LO_DOBDate = ".GetSQLValueString(formatDate($olDob),'text').", LO_DOBDays = ".GetSQLValueString($olDobDays,'int').", LO_TerminalID = ".GetSQLValueString($olTerminalId,'int').", 
						LO_CopyDocReceivedDate = ".GetSQLValueString(formatDate($olCopyDocRc),'text').", LO_AllDocReceivedDate = ".GetSQLValueString(formatDate($olDocsRcvd),'text').", 
						LO_OBLReceivedDate = ".GetSQLValueString(formatDate($olOblRcvd),'text').",
						LO_PAARReceivedDate = ".GetSQLValueString(formatDate($olPaarRcvd),'text').", 
						LO_AssessmentDate = ".GetSQLValueString(formatDate($olAssessment),'text').", LO_CNumber = ".GetSQLValueString($olCnumber,'text').",	
						LO_SONPaidDate = ".GetSQLValueString(formatDate($olSonPaid),'text').", LO_Stamp1Date = ".GetSQLValueString(formatDate($olStamp1),'text').", 
						LO_Stamp2Date = ".GetSQLValueString(formatDate($olStamp2),'text').", LO_DutyPaidDate = ".GetSQLValueString(formatDate($olDutyPaid),'text').", 
						LO_DutyDays = ".GetSQLValueString($olDutyNbr,'double').", LO_TerminalRatePerDate = ".GetSQLValueString(formatDate($olTerminalRatePer),'text').",	
						LO_TerminalPaidDate = ".GetSQLValueString(formatDate($olTerminalPaid),'text').", LO_terminalAdditionalDate = ".GetSQLValueString(formatDate($olTerminalAdd),'text').", 
						LO_ShippingRatePerDate = ".GetSQLValueString(formatDate($olShippingRatePer),'text').", LO_ShippingPaidDate = ".GetSQLValueString(formatDate($olShippingPaid),'text').", 
						LO_ShippingAdditionalDate = ".GetSQLValueString(formatDate($olShippingAdd),'text').", LO_ExaminationBookDate = ".GetSQLValueString(formatDate($olExamBook),'text').",
						LO_ExaminedDate = ".GetSQLValueString(formatDate($olExamined),'text').", LO_custReleasedDate = ".GetSQLValueString(formatDate($olCustReleased),'text').", 
						LO_ShipCoReleaseDate = ".GetSQLValueString(formatDate($olShipReleased),'text').", LO_TDODate = ".GetSQLValueString(formatDate($olTdo),'text').", 
						LO_LoadingCompleteDate = ".GetSQLValueString(formatDate($olLoadingCompleted),'text').", LO_DeliveryCompletedDate = ".GetSQLValueString(formatDate($olDeliveryCompleted),'text').", 	
						LO_WayBillReceivedDate = ".GetSQLValueString(formatDate($olWaybRcvd),'text').", LO_ContainerCardReceivedDate = ".GetSQLValueString(formatDate($olContainerRcvd),'text').", 
						LO_DOBDeliver = ".GetSQLValueString($olDobDeliver,'double').", LO_DutyDeliver = ".GetSQLValueString($olDutyDeliver,'double').", 
						LO_InvoiceIssuedOnDate = ".GetSQLValueString(formatDate($olInvoiceIssuedOn),'text').", LO_InvoiceBalance = ".GetSQLValueString($olInvoiceBlanace,'double').", 
						LO_DslReference = ".GetSQLValueString($olDslRef,'text').", LO_ProductionDescription = ".GetSQLValueString($olProductionDesc,'text').",
						LO_BLNumber = ".GetSQLValueString($olBLNbr,'text').", LO_FormMValidateDate = ".GetSQLValueString(formatDate($olFormMValidate),'text').", 
						LO_FormMApprovedDate = ".GetSQLValueString(formatDate($olFormMApproved),'text').", LO_InsuranceReceivedDate = ".GetSQLValueString(formatDate($olInsuranceRcvd),'text').",
						LO_InsuranceRequestedDate = ".GetSQLValueString(formatDate($olInsuranceRqst),'text').", LO_MFNumber = ".GetSQLValueString($olMFNumber,'text').", 
						LO_OBLReceivedFrom = ".GetSQLValueString($olOBLFrom,'text').", LO_PAARNumber = ".GetSQLValueString($olPaarNbr,'text').", 
						LO_PAARRquestedDate = ".GetSQLValueString(formatDate($olPaarRqsted),'text').", LO_PFIDate = ".GetSQLValueString(formatDate($olPFIDate),'text').", 
						LO_PFINumber = ".GetSQLValueString($olPFINbr,'text').", LO_RegularDocReceivedDate = ".GetSQLValueString(formatDate($olRegularDocRcvd),'text').", 
						LO_RegularDocRquestedDate = ".GetSQLValueString(formatDate($olRegularDocRqst),'text').", LO_ROTNumber = ".GetSQLValueString($olRotNbr,'text').",
						LO_VesselName = ".GetSQLValueString($olVesselName,'text').", LO_FileHandledDate = ".GetSQLValueString(formatDate($olFileHandledClearing),'text').",				
						LO_ContactName = ".GetSQLValueString($olContactName,'text').", LO_ContactPhoneNbr = ".GetSQLValueString($olContactPhone,'text').", 
						LO_DutyRcvdPaidDate = ".GetSQLValueString(formatDate($olDutyRcvdPaid),'text').", LO_DestinationId = ".GetSQLValueString($olDestinationId,'text').",
						LO_LCLNumber = ".GetSQLValueString($olLclNbr,'text').", LO_ROROShipmentDate = ".GetSQLValueString(formatDate($olRoroShipment),'text').",
						LO_ShipmentType = ".GetSQLValueString($olShipmentType,'text').", LO_FormMRqstDate = ".GetSQLValueString(formatDate($olFormMRqstdate),'text').",
						LO_ClearingStatus = ".GetSQLValueString($olClearingStatus,'text').", LO_RepositionDate = ".GetSQLValueString(formatDate($olRepo),'text').", 
						LO_ExamScheduleDate = ".GetSQLValueString(formatDate($olExamSchedule),'text').", LO_TDOExpiryDate = ".GetSQLValueString(formatDate($olTdoExpiry),'text').",
						LO_Remark = ".GetSQLValueString($olRemark,'text')."
					where LO_id = $olId "; //echo $queryup;exit;
	$resup 		= mysql_query($queryup, $connection);
	if ($resup>0 && $olServiceTypeId != $prevServiceTypeId){
		$updateSeq = "update Logistic_Services set service_seqId = ifnull(service_seqId,0)+1 where service_id = $olServiceTypeId";
		$resSeq = mysql_query($updateSeq, $connection);
	}
	$message 	= mysql_error();	

}
else if ($mainmode == 'delete' && $olId != '' && $ErrorExists == 0){
	$ExistsInTransport = 0;
	$chkTransport = "select 1 from Logistic_transport where  LT_OperationId = $olId ";
	$resTransport = mysql_query($chkTransport, $connection);
	$numrow = mysql_num_rows($resTransport);
	
	if ($numrow > 0){
		$ExistsInTransport = 1;
		$message = "Delete Not Allowed! Operation Already Exists in Transport!";
	}
	
	if ($ExistsInTransport == 0){
		mysql_query("START TRANSACTION");
		
		$querydelRemark = "delete from Logistic_Remark where LO_id = $olId ";
		$resdelRemark		= mysql_query($querydelRemark, $connection);
		if (!$resdelRemark){
			$ErrorExists = 1;
			$message = mysql_error();
		}
		
		if ($ErrorExists == 0){
			$querydelCont = "delete from Logistic_ContainerNbr where LO_Id = $olId ";
			$resdelCont	= mysql_query($querydelCont, $connection);
			if (!$resdelCont){
				$ErrorExists = 1;
				$message = mysql_error();
			}
			
		}
		if ($ErrorExists == 0){
		  $querydel 	= "delete from logistic_operations where LO_id = $olId ";
		  $resdel		= mysql_query($querydel, $connection);
		  $message 	= mysql_error();
		}
		
		if ($ErrorExists == 0){
			mysql_query("COMMIT");
		} else {
			mysql_query("ROLLBACK");
		}
	}
}


// Remark tab
if ($modedt == 'saveRemark' && $olId != ''){
	
	if ($ErrorExists == 0){
		if ($remId == ''){
			$queryRem 	= "insert into Logistic_Remark (LO_id, LR_Type, LR_Remark, LR_RemarkDate) values 
							($olId, '$lrType', ".GetSQLValueString($lrRemark,'text').", ".GetSQLValueString($lrDate,'text')."  ); ";
			$resRem 	= mysql_query($queryRem, $connection);
			$message 	= mysql_error();
		} else {
			$queryRem 	= "update Logistic_Remark set LO_id = $olId, LR_Type = '$lrType', LR_Remark = ".GetSQLValueString($lrRemark,'text').",
								LR_RemarkDate = ".GetSQLValueString($lrDate,'text')." 
						   where LR_id = $remId; ";
			$resRem 	= mysql_query($queryRem, $connection);
			$message 	= mysql_error();
		}
	
		
	}
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	echo $olId;	
	echo "[BRK]";
	echo drawRemarkTable ($olId, $connection);			
	
	exit;
}
else if ($modedt == 'deleteRemark' && $olId != ''){
	
	if ($ErrorExists == 0){
		if ($remId != ''){
			$queryRem 	= "delete from Logistic_Remark where LR_id = $remId; ";
			$resRem 	= mysql_query($queryRem, $connection);
			$message 	= mysql_error();
		} 
	}
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	echo drawRemarkTable ($olId, $connection);			
	
	exit;
}	


// Container tab
if ($modedt == 'saveContainer' && $olId != ''){
	if ($contId == ''){
		$querydt = "insert into Logistic_ContainerNbr (LC_Description, LC_ContainerType, LO_Id) values (".GetSQLValueString($lcRemark,'text').", '$cntr_type', $olId) ";
		$resdt = mysql_query($querydt, $connection);
		$message = mysql_error();
	} else {
		$querydt = "update Logistic_ContainerNbr set LC_Description = ".GetSQLValueString($lcRemark,'text').", LC_ContainerType = '$cntr_type', LO_Id = $olId
		           where LC_Id = $contId ";
		$resdt = mysql_query($querydt, $connection);
		$message = mysql_error();
		
	}
}
else if ($modedt == 'deleteContainer' && $olId != ''){
	if ($contId != ''){
		$querydt = "delete from Logistic_ContainerNbr where LC_Id = $contId ";
		$resdt = mysql_query($querydt, $connection);
		$message = mysql_error();
		
	}
}

if (($modedt == 'showContainer' || $modedt == 'saveContainer' || $modedt == 'deleteContainer')&& $olId != ''){
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	echo $olId;	
	echo "[BRK]";
	drawContainerTable ($olId, $olTwenty, '20', $connection);		
	echo "[BRK]";
	drawContainerTable ($olId, $olForty, '40', $connection);
	exit;
}
$mainmode 	= '';
$olId	= '';
$vTabFocus = '';
echo "<form name='olAction' method='POST' action='Form_OperationsLogistics.php'>
	<input type='hidden' name='mainmode' value='$mainmode'>
	<input type=hidden name='message' value='$message'>
	<input type=hidden name='olId' value='$olId'>
	<input type=hidden name='vTabFocus' value='$vTabFocus'>
	</form>";
echo "<script> 
		document.olAction.submit();
	 </script>";
		
function formatDate($date){
	if($date !=''){
		$ndate = explode('/', $date);
		$date = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	}
	return $date;
	
}	
?>