<?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 				= $_GET['mainmode'];
$message 				= $_POST['message'];
$tablename 				= $_GET['tablename'];
$rowId 					= $_GET['rowId'];

$message = '';
$CodeExists = 0;

// Service Table
if ($tablename == 'service'){
	$serviceCode 	= $_GET['serviceCode'];
	$serviceDesc 	= $_GET['serviceDesc'];
	$serviceSeq 	= $_GET['serviceSeq'];
	
	$check = "select 1 from Logistic_Services where service_code = ".GetSQLValueString($serviceCode,'text')." and if ('$rowId'='', 1=1, service_id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "Service Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_Services (service_code, service_description, service_seqId) values 
					(".GetSQLValueString($serviceCode,'text').",".GetSQLValueString($serviceDesc,'text').", ".GetSQLValueString($serviceSeq,'int').") ";
		
		} else {
			$query = "update Logistic_Services set service_code = ".GetSQLValueString($serviceCode,'text').", 
												service_description = ".GetSQLValueString($serviceDesc,'text').", 
												service_seqId = ".GetSQLValueString($serviceSeq,'int')."
					 where service_id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_Services where service_id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawService($connection);	
	exit;
}


// Dealer Table
if ($tablename == 'dealer'){
	$dealerCode 	= $_GET['dealerCode'];
	$dealerDesc 	= $_GET['dealerDesc'];
	
	$check = "select 1 from Logistic_Dealer where Dealer_Code = ".GetSQLValueString($dealerCode,'text')." and if ('$rowId'='', 1=1, Dealer_Id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "Dealer Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_Dealer (Dealer_Code, Dealer_Description) values 
					(".GetSQLValueString($dealerCode,'text').",".GetSQLValueString($dealerDesc,'text').") ";
		
		} else {
			$query = "update Logistic_Dealer set Dealer_Code = ".GetSQLValueString($dealerCode,'text').", 
												Dealer_Description = ".GetSQLValueString($dealerDesc,'text')."
					 where Dealer_Id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_Dealer where Dealer_Id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawDealer($connection);	
	exit;
}

// ShipCo Table
if ($tablename == 'ship'){
	$shipCode 	= $_GET['shipCode'];
	$shipDesc 	= $_GET['shipDesc'];
	
	$check = "select 1 from Logistic_ShipCo where Ship_Code = ".GetSQLValueString($shipCode,'text')." and if ('$rowId'='', 1=1, Ship_Id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "ShipCo Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_ShipCo (Ship_Code, Ship_Description) values 
					(".GetSQLValueString($shipCode,'text').",".GetSQLValueString($shipDesc,'text').") ";
		
		} else {
			$query = "update Logistic_ShipCo set Ship_Code = ".GetSQLValueString($shipCode,'text').", 
												Ship_Description = ".GetSQLValueString($shipDesc,'text')."
					 where Ship_Id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_ShipCo where Ship_Id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawShipCo($connection);	
	exit;
}


// Team Table
if ($tablename == 'team'){
	$teamCode 	= $_GET['teamCode'];
	$teamDesc 	= $_GET['teamDesc'];
	
	$check = "select 1 from Logistic_Team where Team_Code = ".GetSQLValueString($teamCode,'text')." and if ('$rowId'='', 1=1, Team_Id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "Team Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_Team (Team_Code, Team_Description) values 
					(".GetSQLValueString($teamCode,'text').",".GetSQLValueString($teamDesc,'text').") ";
		
		} else {
			$query = "update Logistic_Team set Team_Code = ".GetSQLValueString($teamCode,'text').", 
												Team_Description = ".GetSQLValueString($teamDesc,'text')."
					 where Team_Id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_Team where Team_Id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawTeam($connection);	
	exit;
}


// Terminal Table
if ($tablename == 'terminal'){
	$terminalCode 	= $_GET['terminalCode'];
	$terminalDesc 	= $_GET['terminalDesc'];
	
	$check = "select 1 from Logistic_Terminal where Terminal_Code = ".GetSQLValueString($terminalCode,'text')." and if ('$rowId'='', 1=1, Terminal_Id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "Terminal Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_Terminal (Terminal_Code, Terminal_Description) values 
					(".GetSQLValueString($terminalCode,'text').",".GetSQLValueString($terminalDesc,'text').") ";
		
		} else {
			$query = "update Logistic_Terminal set Terminal_Code = ".GetSQLValueString($terminalCode,'text').", 
												Terminal_Description = ".GetSQLValueString($terminalDesc,'text')."
					 where Terminal_Id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_Terminal where Terminal_Id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawTerminal($connection);	
	exit;
}
// Destination Table
if ($tablename == 'destination'){
	$destinationCode 	= $_GET['destinationCode'];
	$destinationDesc 	= $_GET['destinationDesc'];
	
	$check = "select 1 from Logistic_Destination where Destination_Code = ".GetSQLValueString($destinationCode,'text')." and if ('$rowId'='', 1=1, Destination_Id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "Terminal Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_Destination (Destination_Code, Destination_Description) values 
					(".GetSQLValueString($destinationCode,'text').",".GetSQLValueString($destinationDesc,'text').") ";
		
		} else {
			$query = "update Logistic_Destination set Destination_Code = ".GetSQLValueString($destinationCode,'text').", 
												Destination_Description = ".GetSQLValueString($destinationDesc,'text')."
					 where Destination_Id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_Destination where Destination_Id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawDestination($connection);	
	exit;
}

// Transporter Table
if ($tablename == 'transporter'){
	$transporterCode 	= $_GET['transporterCode'];
	$transporterDesc 	= $_GET['transporterDesc'];
	
	$check = "select 1 from Logistic_Transporter where Transporter_Code = ".GetSQLValueString($transporterCode,'text')." and if ('$rowId'='', 1=1, Transporter_Id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "Terminal Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_Transporter (Transporter_Code, Transporter_Description) values 
					(".GetSQLValueString($transporterCode,'text').",".GetSQLValueString($transporterDesc,'text').") ";
		
		} else {
			$query = "update Logistic_Transporter set Transporter_Code = ".GetSQLValueString($transporterCode,'text').", 
												Transporter_Description = ".GetSQLValueString($transporterDesc,'text')."
					 where Transporter_Id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_Transporter where Transporter_Id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawTransporter($connection);	
	exit;
}

// Transporter Table
if ($tablename == 'pickup'){
	$pickupCode 	= $_GET['pickupCode'];
	$pickupDesc 	= $_GET['pickupDesc'];
	
	$check = "select 1 from Logistic_PickUp where PickUp_Code = ".GetSQLValueString($pickupCode,'text')." and if ('$rowId'='', 1=1, PickUp_Id <> '$rowId' )";
	$res = mysql_query($check, $connection);
	$rownum = mysql_num_rows($res);
	if ($rownum > 0){
		$CodeExists = 1;	
		$message = "Terminal Code Already Exists!";
	}
	
	if ($mainmode == 'save' && $CodeExists == 0){
		if ($rowId == ''){
			$query = "insert into Logistic_PickUp (PickUp_Code, PickUp_Description) values 
					(".GetSQLValueString($pickupCode,'text').",".GetSQLValueString($pickupDesc,'text').") ";
		
		} else {
			$query = "update Logistic_PickUp set PickUp_Code = ".GetSQLValueString($pickupCode,'text').", 
												PickUp_Description = ".GetSQLValueString($pickupDesc,'text')."
					 where PickUp_Id = $rowId ";
			
		}
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	if ($mainmode == 'delete'){
		$query = "delete from Logistic_PickUp where PickUp_Id = $rowId ";
		$result = mysql_query($query, $connection);
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	drawPickUp($connection);	
	exit;
}

echo "[BRK]";
echo $message;
echo "[BRK]";
echo "Table Not Fount";
exit;

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