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

$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_WaybillLogisticsApp 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'];
$lwId 					= $_POST['lwId'];						
$ltdtId 				= $_POST['ltdtId'];		
$lwDate 				= $_POST['lwDate'];				
$lwTime 				= $_POST['lwTime'];			
$lwCntrSize 			= $_POST['lwCntrSize'];		
$lwProjectCode 			= $_POST['lwProjectCode'];	
$lwContainerNb 			= $_POST['lwContainerNb'];	
$lwClientCode 			= $_POST['lwClientCode'];	
$lwClientRef 			= $_POST['lwClientRef'];	
$lwDeliveryAddr 		= $_POST['lwDeliveryAddr'];	
$lwPhoneNumber 			= $_POST['lwPhoneNumber'];	
$lwBLNumber 			= $_POST['lwBLNumber'];	
$lwDsl 					= $_POST['lwDsl'];	
$LW_Remark 				= $_POST['LW_Remark'];	


$message = '';
$ErrorExists = 0;

if ($lwTime == '') $lwTime = "12:00";
if ($lwDate != '') $lwDate = $lwDate." ".$lwTime.":00";



if ($mainmode == 'insert' || $mainmode == 'edit'){
	$checkClient = "select 1 from clients where ledger_number = '$lwClientCode' ";
	$resClient = mysql_query($checkClient, $connection);
	$numrow = mysql_num_rows($resClient);
	if ($numrow == 0){
		$ErrorExists = 1;
		$message = "Client doesn\'t exist! Check for Client Code!";
	}
	
	$checkProject = "select 1
					from logistic_transportdt
					inner join logistic_transport on logistic_transport.LT_Id = logistic_transportdt.LT_Id
					where LT_ProjectCode = '$lwProjectCode' and LTdt_id = $ltdtId ";
	$resProject = mysql_query($checkProject, $connection);				
	$numrow = mysql_num_rows($resProject);
	if ($numrow == 0){
		$ErrorExists = 1;
		$message = "File Code is Incorrect!";
	}
	
	
}

if ($mainmode == 'insert' && $ErrorExists == 0 && $ltdtId != ''){ 
	$query = "insert into Logistic_Waybill (LW_date, client_code, project_code, LW_ContainerSize, LW_ContainerNb, LW_BLNumber, LW_DeliveryAddress, LW_ContactPhone, LTdt_id, LW_Remark, LW_DSLNumber, LW_ClientReference)
			values 
			(".GetSQLValueString($lwDate,'text').", ".GetSQLValueString($lwClientCode,'text').", ".GetSQLValueString($lwProjectCode,'text').", 
			".GetSQLValueString($lwCntrSize,'text').", ".GetSQLValueString($lwContainerNb,'text').", ".GetSQLValueString($lwBLNumber,'text').",
			".GetSQLValueString($lwDeliveryAddr,'text').", ".GetSQLValueString($lwPhoneNumber,'text').", ".GetSQLValueString($ltdtId,'int').",
			".GetSQLValueString($lwRemark,'text').", ".GetSQLValueString($lwDsl,'text').", ".GetSQLValueString($lwClientRef,'text')."	
			)"; 
	$result = mysql_query($query, $connection);
	if($result){
		$lwId = mysql_insert_id();
		if ($lwCntrSize == '20')
			$ref = get_nextsequence('Reference_LogisticWaybill20Ft','',$connection);	
		else if ($lwCntrSize == '40')
			$ref = get_nextsequence('Reference_LogisticWaybill40Ft','',$connection);	
		
		$queryup = "update Logistic_Waybill set LW_Reference = '$ref' where LW_id = $lwId ";
		$result = mysql_query($queryup, $connection);
		
		$mainmode = 'edit';
		$printRep = 1;
		
	} else {
		$message = mysql_error();
		$ErrorExists = 1;
		$printRep = 0;
	}
}

if ($mainmode == 'edit' && $ErrorExists == 0 && $ltdtId != '' && $lwId != ''){
	$query = "update Logistic_Waybill set
				LW_date = ".GetSQLValueString($lwDate,'text').", client_code = ".GetSQLValueString($lwClientCode,'text').", 
				project_code = ".GetSQLValueString($lwProjectCode,'text').", LW_ContainerSize = ".GetSQLValueString($lwCntrSize,'text').", 
				LW_ContainerNb = ".GetSQLValueString($lwContainerNb,'text').", LW_BLNumber = ".GetSQLValueString($lwBLNumber,'text').",
				LW_DeliveryAddress = ".GetSQLValueString($lwDeliveryAddr,'text').", LW_ContactPhone = ".GetSQLValueString($lwPhoneNumber,'text').", 
				LTdt_id = ".GetSQLValueString($ltdtId,'int').", LW_Remark = ".GetSQLValueString($lwRemark,'text').", LW_DSLNumber = ".GetSQLValueString($lwDsl,'text').", 
				LW_ClientReference = ".GetSQLValueString($lwClientRef,'text')."			
			where LW_id = $lwId	
			";
	$result = mysql_query($query, $connection);
	if($result){
		$mainmode = 'edit';
		$printRep = 1;
		
	} else {
		$message = mysql_error();
		$ErrorExists = 1;
		$printRep = 0;
	}
} 

if ($mainmode == 'delete' && $ErrorExists == 0 && $lwId != ''){
	$query = "delete from Logistic_Waybill where LW_id = $lwId	";
	$result = mysql_query($query, $connection);
	if($result){
		$lwId='';
		$ltdtId = '';
		$mainmode = '';
		
	} else {
		$message = mysql_error();
		$ErrorExists = 1;
		$printRep = 0;
	}
}


echo "<form name='lwAction' method='POST' action='Form_WaybillLogistics.php'>
	<input type='hidden' name='mainmode' value='$mainmode'>
	<input type='hidden' name='modedt' value='$modedt'>
	<input type=hidden name='message' value='$message'>
	<input type=hidden name='lwId' value='$lwId'>
	<input type=hidden name='ltdtId' value='$ltdtId'>
	<input type=hidden name='printRep' value='$printRep'>
	</form>";
echo "<script> 
		document.lwAction.submit();
	 </script>";
	
?>