<?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'];
$lfId 					= $_POST['lfId'];						if ($lfId == '') 					$lfId 					= $_GET['lfId'];
$lfDate 				= $_POST['lfDate'];				
$lfTime 				= $_POST['lfTime'];			
$lfCntrSize 			= $_POST['lfCntrSize'];		
$lfProjectCode 			= $_POST['lfProjectCode'];	
$lfContainerNb 			= $_POST['lfContainerNb'];	
$lfClientCode 			= $_POST['lfClientCode'];

$message = '';
$ErrorExists = 0;

if ($lfTime == '') $lfTime = "12:00";
if ($lfDate != '') $lfDate = $lfDate." ".$lfTime.":00";



if ($mainmode == 'insert' || $mainmode == 'edit'){
	$checkClient = "select 1
					from clients
					where ledger_number = '$lfClientCode' ";
	$resClient = mysql_query($checkClient, $connection);
	$numrow = mysql_num_rows($resClient);
	if ($numrow == 0){
		$ErrorExists = 1;
		$message = "Client Code is Incorrect!";
	}
	
	$checkProject = "select 1
					from project
					where project_code = '$lfProjectCode' ";
	$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 ){ 
	$query = "insert into Logistic_ClosingFile (LCF_Date, LCF_ProjectCode, LCF_ClientCode, LCF_ContainerSize, LCF_ContainerNumber)
			values 
			(".GetSQLValueString($lfDate,'text').", ".GetSQLValueString($lfProjectCode,'text').", ".GetSQLValueString($lfClientCode,'text').", 
			".GetSQLValueString($lfCntrSize,'text').", ".GetSQLValueString($lfContainerNb,'text')."	
			)"; 
	$result = mysql_query($query, $connection);
	if($result){
		$lfId = mysql_insert_id();
		
		$mainmode = 'edit';
		$printRep = 1;
		
	} else {
		$message = mysql_error();
		$ErrorExists = 1;
		$printRep = 0;
	}
}

if ($mainmode == 'edit' && $ErrorExists == 0  && $lfId != ''){
	$query = "update Logistic_ClosingFile set
			LCF_Date = ".GetSQLValueString($lfDate,'text').", LCF_ProjectCode = ".GetSQLValueString($lfProjectCode,'text').", 
			LCF_ClientCode = ".GetSQLValueString($lfClientCode,'text').", LCF_ContainerSize = ".GetSQLValueString($lfCntrSize,'text').", 
			LCF_ContainerNumber = ".GetSQLValueString($lfContainerNb,'text')."
			where LCF_Id = $lfId	
			";
	$result = mysql_query($query, $connection);
	if($result){
		$mainmode = 'edit';
		$printRep = 1;
		
	} else {
		$message = mysql_error();
		$ErrorExists = 1;
		$printRep = 0;
	}
} 
if (($mainmode == 'edit' || $mainmode == 'insert') && $ErrorExists == 0 && $lfId != ''){
	
	$querydoc = "select LDI_Id from Logistic_DocumentInfo ";
	$resdoc = mysql_query($querydoc, $connection);
	while ($rwdoc = mysql_fetch_array($resdoc)){
		$ldId = $rwdoc[0];
		$org = 'original-'.$ldId;
		$copy = 'copy-'.$ldId;
		$detailId = 'detailId-'.$ldId;
		
		if ($$org == '')  $$org  = 0;
		if ($$copy == '') $$copy = 0;
		
		if ($$detailId == ''){
			$querydt = "insert into Logistic_ClosingFiledt (LCF_Id, Document_id, Chk_Original, Chk_Copy) values (".$lfId.", ".$ldId.",".$$org.",".$$copy.")";
			$resdt = mysql_query($querydt, $connection);
		} else if ($$detailId != ''){
			$querydt = "update Logistic_ClosingFiledt set Document_id = ".$ldId.", Chk_Original = ".$$org.", Chk_Copy = ".$$copy." 
						where LCFdt_id = ".$$detailId." ";
			$resdt = mysql_query($querydt, $connection);
		}
	
	}
}

if ($mainmode == 'delete' && $ErrorExists == 0 && $lfId != ''){
	$query = "delete from Logistic_ClosingFile where LCF_Id = $lfId	";
	$result = mysql_query($query, $connection);
	if($result){
		$query = "delete from Logistic_ClosingFiledt where LCF_Id = $lfId	";
		$result = mysql_query($query, $connection);
	} else {
		$message = mysql_error();
	}
	header("Location: ./Form_SearchClosingFileLogistic.php?message=$message");
	exit;
}

echo "<form name='lfAction' method='POST' action='Form_ClosingFileLogistic.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='lfId' value='$lfId'>
	<input type=hidden name='printRep' value='$printRep'>
	</form>";
echo "<script> 
		document.lfAction.submit();
	 </script>";
	
?>