<?php

session_start();

if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");
 
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$mode 			= $_GET['mode'];
$depositid		= $_POST['depositid']; if ($depositid == '') $depositid = $_GET['depositid'];

$voyyear 		= $_POST['voyyear'];
$deptype 		= $_POST['deptype'];
$cnttype 		= $_POST['cnttype'];
$clientcode 	= $_POST['clientcode'];
$depamount 		= $_POST['depamount'];
$currencycode 	= $_POST['currencycode'];

if ($depamount == '') 	$depamount 	= 0;


if ($mode == 'insert'){
	$ErrorExists = 0;
	
	$sub_query = " Select Count(Ledger_Number) From Clients ".
				 " Where Ledger_Number = '".$clientcode."'";
	$sub_result = mysql_query($sub_query, $connection);
	$rw = mysql_fetch_row($sub_result);	
	if ($rw[0] == 0) {
		$ErrorExists = 1;
		$message = 'Insert not allowed. Please check for Client Code.';
	}
	
	if ($ErrorExists == 0){	
		$querychk = "select 1 from Manifest_Deposit
					 where client_code = '".$clientcode."' and voy_year = '".$voyyear."' and dep_type = '".$deptype."' and  cntr_type = '".$cnttype."' ";
		$resultchk = mysql_query($querychk, $connection);
		$nmr = mysql_num_rows($resultchk);
		
		if ($nmr == 0){	
			$queryins = "insert into Manifest_Deposit
						(voy_year, client_code, dep_amount, currency_code, dep_type, cntr_type)
						values 
						('".$voyyear."', '".$clientcode."', ".$depamount.", '".$currencycode."', '".$deptype."', '".$cnttype."' ) ";
			$resultins = mysql_query($queryins, $connection);			 
			if ($resultins)
				$message = "Record Submitted Successfully!";
			else
				$message = mysql_error($connection);
		} else 	
				$message = "Record Already Exists!";	 
	}
} 


if ($mode == 'update' && $depositid != ''){
	$ErrorExists = 0;
	
	$sub_query = " Select Count(Ledger_Number) From Clients ".
				 " Where Ledger_Number = '".$clientcode."'";
	$sub_result = mysql_query($sub_query, $connection);
	$rw = mysql_fetch_row($sub_result);	
	if ($rw[0] == 0) {
		$ErrorExists = 1;
		$message = 'Insert not allowed. Please check for Client Code.';
	}
	
	if ($ErrorExists == 0){
		$querychk = "select 1 from Manifest_Deposit
					 where client_code = '".$clientcode."' and voy_year = '".$voyyear."' and dep_type = '".$deptype."' and  cntr_type = '".$cnttype."' and deposit_id <> $depositid ";
		$resultchk = mysql_query($querychk, $connection);
		$nmr = mysql_num_rows($resultchk);
		
		if ($nmr == 0){	
			$queryup = "update Manifest_Deposit set
						voy_year = '".$voyyear."',
						client_code = '".$clientcode."', 
						dep_amount = ".$depamount.", 
						currency_code = '".$currencycode."', 
						dep_type = '".$deptype."',
						cntr_type = '".$cnttype."',
						where deposit_id = $depositid  ";
			$resultup = mysql_query($queryup, $connection);			 
			if ($resultup)
				$message = "Record Submitted Successfully!";
			else
				$message = mysql_error($connection);
		} else 	
				$message = "Record Already Exists!";
	}		
} 


if ($mode == 'delete' && $depositid != ''){
	$querydel = "delete from Manifest_Deposit where Manifest_Deposit = $depositid  ";
	$resdel = mysql_query($querydel, $connection);
	if ($resdel)
			$message = "Record Deleted Successfully!";
		else
			$message = mysql_error($connection);
} 


function strformat($str){
	if($str == '') $str = "NULL";
	else $str = "'".ereg_replace("'", "\'", trim($str))."'";
	return $str;
}

echo "<form name='depositaction' action='Form_ManifestDeposit.php'>
		<input type='hidden' name='message' value='$message'>
		<input type='hidden' name='searchOn' value=0>
	 </form>";
echo "<script>
		document.depositaction.submit();;
	 </script>";	
exit;	 
?>