<?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_TransporterLogisticsApp 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'];
$tlId 					= $_POST['tlId'];						if ($tlId == '') 					$tlId 					= $_GET['tlId'];
$creationDate 			= $_POST['creationDate'];				if ($creationDate == '') 			$creationDate 			= $_GET['creationDate'];
$tlProjectCode 			= $_POST['tlProjectCode'];				if ($tlProjectCode == '') 			$tlProjectCode 			= $_GET['tlProjectCode'];
$tlDestinationId 		= $_POST['tlDestinationId'];			if ($tlDestinationId == '') 		$tlDestinationId 		= $_GET['tlDestinationId'];
$tlTerminalId 			= $_POST['tlTerminalId'];				if ($tlTerminalId == '') 			$tlTerminalId 			= $_GET['tlTerminalId'];

$modedt 				= $_GET['modedt'];
$rowId 					= $_GET['rowId'];
$cType 					= $_GET['cType'];
$tlCntrNo 				= $_GET['tlCntrNo'];
$tlCntrSize 			= $_GET['tlCntrSize'];
$tlTruckNo 				= $_GET['tlTruckNo'];
$tlTransporterId 		= $_GET['tlTransporterId'];
$tlPickUpPntId	 		= $_GET['tlPickUpPntId'];
$tlStatus		 		= $_GET['tlStatus'];
$tlOutPortD 			= $_GET['tlOutPortD'];
$tlOutPortT 			= $_GET['tlOutPortT'];
$tlArrivalClientD		= $_GET['tlArrivalClientD'];
$tlArrivalClientT		= $_GET['tlArrivalClientT'];
$tlOffloadD				= $_GET['tlOffloadD'];
$tlOffloadT				= $_GET['tlOffloadT'];
$tlDropEmptyD			= $_GET['tlDropEmptyD'];
$tlDropEmptyT			= $_GET['tlDropEmptyT'];
$tlTimeTDOD				= $_GET['tlTimeTDOD'];
$tlTimeTDOT				= $_GET['tlTimeTDOT'];
$tlDaysReach			= $_GET['tlDaysReach'];
$tlDaysOffload			= $_GET['tlDaysOffload'];
$tlDaysDrop				= $_GET['tlDaysDrop'];
$tlDriverName			= $_GET['tlDriverName'];
$tlDriverPhone			= $_GET['tlDriverPhone'];
$tlCardWaybillReturnD	= $_GET['tlCardWaybillReturnD'];
$tlCardWaybillReturnT	= $_GET['tlCardWaybillReturnT'];

$message = '';
$ErrorExists = 0;

if ($tlOutPortT == '') $tlOutPortT = "12:00";
if ($tlOutPortD != '') $tlOutPortD = $tlOutPortD." ".$tlOutPortT.":00";

if ($tlArrivalClientT == '') $tlArrivalClientT = "12:00";
if ($tlArrivalClientD != '') $tlArrivalClientD = $tlArrivalClientD." ".$tlArrivalClientT.":00";

if ($tlOffloadT == '') $tlOffloadT = "12:00";
if ($tlOffloadD != '') $tlOffloadD = $tlOffloadD." ".$tlOffloadT.":00";

if ($tlDropEmptyT == '') $tlDropEmptyT = "12:00";
if ($tlDropEmptyD != '') $tlDropEmptyD = $tlDropEmptyD." ".$tlDropEmptyT.":00";

if ($tlTimeTDOT == '') $tlTimeTDOT = "12:00";
if ($tlTimeTDOD != '') $tlTimeTDOD = $tlTimeTDOD." ".$tlTimeTDOT.":00";

if ($tlCardWaybillReturnT == '') $tlCardWaybillReturnT = "12:00";
if ($tlCardWaybillReturnD != '') $tlCardWaybillReturnD = $tlCardWaybillReturnD." ".$tlCardWaybillReturnT.":00";

if ($mainmode == 'edit' && $tlId != ''){
	$queryup = "update logistic_transport set LT_Date = ".GetSQLValueString(formatDate($creationDate),'text').", 
						LT_ProjectCode = ".GetSQLValueString($tlProjectCode,'text').", 
						LT_DestinationId = ".GetSQLValueString($tlDestinationId,'int').", 
						LT_TerminalId = ".GetSQLValueString($tlTerminalId,'int')."
				where LT_Id = $tlId ";
	$result = mysql_query($queryup, $connection);			
	$message = mysql_error();
}

if ($modedt == 'editDetail' && $tlId != '' && $rowId != ''){
	$queryupdt = "update logistic_transportdt set LTdt_ContainerNo = ".GetSQLValueString($tlCntrNo,'text').", 
						LTdt_TruckNo = ".GetSQLValueString($tlTruckNo,'text').", 
						LTdt_TransporterId = ".GetSQLValueString($tlTransporterId,'int').", 
						LTdt_PickUpPointId = ".GetSQLValueString($tlPickUpPntId,'int').", 
						LTdt_Status = ".GetSQLValueString($tlStatus,'text').", 
						LTdt_OutofPortDate = ".GetSQLValueString($tlOutPortD,'text').", 
						LTdt_ArrivalatClientDate = ".GetSQLValueString($tlArrivalClientD,'text').", 
						LTdt_OffLoadDate = ".GetSQLValueString($tlOffloadD,'text').", 
						LTdt_DroppedEmptyDate = ".GetSQLValueString($tlDropEmptyD,'text').", 
						LTdt_TdoDate = ".GetSQLValueString($tlTimeTDOD,'text').", 
						LTdt_DaysToReachClient = ".GetSQLValueString($tlDaysReach,'double').", 
						LTdt_DaysToOffload = ".GetSQLValueString($tlDaysOffload,'double').", 
						LTdt_DaysToDropEmpty = ".GetSQLValueString($tlDaysDrop,'double').",
						LTdt_DriverName = ".GetSQLValueString($tlDriverName,'text').",
						LTdt_DriverPhone = ".GetSQLValueString($tlDriverPhone,'text').",
						LTdt_CardWaybillReturnDate = ".GetSQLValueString($tlCardWaybillReturnD,'text')."
				where LTdt_id = $rowId ";//echo$queryupdt;
	$resdt = mysql_query($queryupdt, $connection);
	$message = mysql_error();
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawTransporterDt($cType, $tlId, $connection);
	exit;
}

if ($modedt == 'deleteDetail' && $rowId != ''){
	$ExistInWaybill = 0;
	$checkWaybill = "select 1 from Logistic_Waybill where  LTdt_id = $rowId ";
	$resWaybill = mysql_query($checkWaybill, $connection);
	$rownum = mysql_num_rows($resWaybill);
	
	if ($rownum > 0){
		$ExistInWaybill = 1;
		$message = "Delete is not Allowed! Already Exists in Waybill";
	}
	
	if ($ExistInWaybill == 0){
		$querydel = "delete from logistic_transportdt where LTdt_id = $rowId ";
		$resdel = mysql_query($querydel, $connection);
		$message = mysql_error();
		
		$querycnt = "select 1 from logistic_transportdt where LT_Id = $tlId ";
		$res = mysql_query($querycnt);
		$numr = mysql_num_rows($res);
		$update = "update logistic_transport set LT_SerialNo = $numr where LT_Id = $tlId ";
		$resup = mysql_query($update);
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawTransporterDt($cType, $tlId, $connection);
	echo "[BRK]";
	echo $numr;
	exit;
}
if ($modedt == 'getDays'){
	$query = "select datediff('$tlArrivalClientD', '$tlOutPortD') as daysReach,
				datediff('$tlOffloadD', '$tlOutPortD') as daysOffload,
				datediff('$tlDropEmptyD', '$tlOffloadD') as daysDrop  ";
	$result = mysql_query($query, $connection);
	$row = mysql_fetch_row($result);
	
	echo "[BRK]";
	echo $row[0];
	echo "[BRK]";
	echo $row[1];
	echo "[BRK]";
	echo $row[2];
	exit;
}
if ($mainmode == 'LoadOperation'){
	$query = "insert into logistic_transport(LT_Date, LT_ProjectCode, LT_DestinationId, LT_TerminalId, LT_OperationId)
			select now(), Project_Code, LO_DestinationId, LO_TerminalID,lo_id
			from logistic_operations
			where lo_id=$olId;";
	$result = mysql_query($query, $connection);
	if($result){
		$tlId = mysql_insert_id();
		
	} else {
		$message = mysql_error();
		$ErrorExists = 1;
	}
	if ($ErrorExists == 0 && $tlId != ''){
		$querydt = "insert into logistic_transportdt (LT_Id, LTdt_ContainerNo, LTdt_ContainerSize, LTdt_Status, LTdt_TdoDate, LTdt_ContainerId)
				SELECT '$tlId', LC_Description, LC_ContainerType, LO_Status, LO_TDODate, LC_Id
				FROM logistic_containernbr
				inner join logistic_operations on logistic_operations.lo_id = logistic_containernbr.lo_id
				where logistic_containernbr.LO_id=$olId and LC_Id not in (
					select LTdt_ContainerId from logistic_transportdt
					inner join logistic_transport on logistic_transport.LT_Id = logistic_transportdt.LT_Id
					where LT_OperationId = $olId
				)
				order by LC_ContainerType, LC_Id;";
		$resultdt = mysql_query($querydt, $connection);
		$message = mysql_error();
		$querycnt = "select 1 from logistic_transportdt where LT_Id = $tlId ";//echo$querycnt;exit;
		$res = mysql_query($querycnt);
		$numr = mysql_num_rows($res);
		$update = "update logistic_transport set LT_SerialNo = $numr where LT_Id = $tlId ";
		$resup = mysql_query($update);
	}
	if ($ErrorExists == 1){
		header("Location: ./Form_LoadOperationLogistic.php?message=$message");
		exit;
	} else {
		header("Location: ./Form_LoadOperationLogistic.php?loadPage=1&tlId=$tlId");
		exit;
	}
}

if ($mainmode == 'delete' && $tlId != ''){
	$ExistInWaybill = 0;
	$checkWaybill = "select 1 from Logistic_Waybill where  LTdt_id in (select LTdt_id from Logistic_transportdt where LT_Id = $tlId ) ";
	$resWaybill = mysql_query($checkWaybill, $connection);
	$rownum = mysql_num_rows($resWaybill);
	
	if ($rownum > 0){
		$ExistInWaybill = 1;
		$message = "Delete is not Allowed! Already Exists in Waybill";
	}
	if ($ExistInWaybill == 0){
		$query = "delete from Logistic_transport where LT_Id = $tlId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	header("Location: ./Form_SearchTransport.php?message=$message");
	exit;
}

$tlId='';
$mainmode = '';
echo "<form name='ltAction' method='POST' action='Form_TransporterLogistic.php'>
	<input type='hidden' name='mainmode' value='$mainmode'>
	<input type=hidden name='message' value='$message'>
	<input type=hidden name='tlId' value='$tlId'>
	<input type=hidden name='vTabFocus' value='$vTabFocus'>
	</form>";
echo "<script> 
		document.ltAction.submit();
	 </script>";
		
function formatDate($date){
	if($date !=''){
		$ndate = explode('/', $date);
		$date = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	}
	return $date;
	
}	
?>