<?php

session_start();


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

include("config.inc.php");
include("function.inc.php");
include("Functions_Synchro.php");


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

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Synchro,0) As Module_Synchro From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$query_setup=" Select base1 As base1, base2 as base2, Setup_Branch from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

$bank_name = ereg_replace("'", "\'", $bank_name);
$bank_branch = ereg_replace("'", "\'", $bank_branch);
$bank_tel1 = ereg_replace("'", "\'", $bank_tel1);
$bank_tel2 = ereg_replace("'", "\'", $bank_tel2);
$bank_fax = ereg_replace("'", "\'", $bank_fax);
$Address = ereg_replace("'", "\'", $Address);
$bank_email = ereg_replace("'", "\'", $bank_email);
$bank_webpage = ereg_replace("'", "\'", $bank_webpage);

if(empty($banktype))
  $banktype= 'null' ;
else
  $banktype="'$banktype'";

if(empty($bankledgertype))
  $bankledgertype= 'null' ;
else
  $bankledgertype="'$bankledgertype'";
  
if($outstanding_check=='')
	$outstanding_check=0;
 
$bank_name=trim($bank_name);
 
if($mode=='insert'){
		
	fbegin();
	$bank_Id = get_nextsequence('bank_id','',$connection);
	fcommit();
		
	$query="Insert into banks (bank_name,bank_branch,Bank_Telephone1,Bank_Telephone2,Bank_Fax,Bank_Address,Bank_Email,Bank_Website,Bank_creditlimit1,Bank_creditlimit2,bank_id, Owner_Branch)
			values ('".$bank_name."','".$bank_branch."', '".$bank_tel1."','".$bank_tel2."', '".$bank_fax."','".$Address."','".$bank_email."','".$bank_webpage."','".$Bank_creditlimit1."','".$Bank_creditlimit2."','".$bank_Id."', '".$Setup_Branch."')";

	//echo$query;			
	//exit;	
	$result  = mysql_query($query, $connection);
	$bank_id = mysql_insert_id($connection);
	if($config['log_file'] == 1){
		$transType = "I";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select bank_id from banks where  bank_name='$bank_name'";
		$resultid=mysql_query($queryid,$connection);
		$rowid=mysql_fetch_row($resultid);
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$rowid[0]', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'banks', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	//$bank_Id = mysql_insert_id($connection);
	$message = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','BANKS',$bank_Id,'',$Setup_Branch);
		}
	}
}

if($mode=='delete')
{	if($config['log_file'] == 1){
		$transType = "D";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '".$bankId."', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'banks', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	$query="delete from banks where bank_Id='".$bankId."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'D','BANKS',$bankId,'',$Setup_Branch);
		}
	}
}


if($mode=='edit'){
	$query_update=" update banks set bank_name='".$bank_name."', ".
				  "	bank_branch='".$bank_branch."' , ".
				  "	Bank_Telephone1 = '".$bank_tel1."', ".
				  " Bank_Telephone2='".$bank_tel2."',".
				  " Bank_Fax='".$bank_fax."',".
				  " Bank_Address='".$Address."',".
				  " Bank_Email='".$bank_email."',".
				  " Bank_Website='".$bank_webpage."',".
				  " Bank_creditlimit1='".$Bank_creditlimit1."',".
				  " Bank_creditlimit2='".$Bank_creditlimit2."' ".
				  
				  "	where bank_id='".$bank_Id."'";
	//echo $query_update;exit;		

	$resultedit = mysql_query($query_update, $connection);
	if($config['log_file'] == 1){
		$transType = "U";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select bank_id from banks where  bank_name='$bank_name'";
		$resultid=mysql_query($queryid,$connection);
		$rowid=mysql_fetch_row($resultid);
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType','$rowid[0]', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'banks', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	$message    = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','BANKS',$bank_Id,'',$Setup_Branch);
		}
	}
}

if($actionmode=='submitledger'){

	if ($BankLedger_ID =='') {	
		$querydt=" Insert into bankledger (Ledger_Number,Bank_InterestRate,Bank_InterestRate2,Bank_CrdLimit, Currency_Code,BankType_id,BankLedgerType_id,bankledger_outstandingcheck,Bank_ID) ".
				 " values ('".$ledger_number."', '".$interest_rate."','".$commission_rate."', '".$credit_limit."','".$currency."','".$banktype."','".$bankledgertype."','".$outstanding_check."','".$bank_Id."')";
		$result  = mysql_query($querydt, $connection);
		
		$message = mysql_error();		 
	} else {
		$querydt=" Update bankledger Set ".
				 " Ledger_Number = '".$ledger_number."', ".
				 " Bank_InterestRate = '".$interest_rate."', ".
				 " Bank_InterestRate2 = $commission_rate, ".
				 " Bank_CrdLimit = $credit_limit, ".
				 " Currency_Code = '".$currency."', ".
				 " BankType_id = $banktype, ".
				 " BankLedgerType_id = $bankledgertype, ".
				 " bankledger_outstandingcheck = $outstanding_check ".
				 " Where BankLedger_ID = '".$BankLedger_ID."' ";
		//echo$querydt;exit;			 
		$result  = mysql_query($querydt, $connection);
		$message = mysql_error();		 
	}
	$vTabFocus='Ledgers';
}
else if($actionmode=='deletebankledger'){

	$query="delete from bankledger where BankLedger_ID='".$BankLedger_ID."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	$vTabFocus='Ledgers';
}


$message = ereg_replace(' ','%20',$message);

if($mode=='delete')			   
	header ("Location: ./Form_Banks.php?mode=insert&message=".$message);
else
	header ("Location: ./Form_Banks.php?vTabFocus=$vTabFocus&bank_Id=$bank_Id&mode=editmode&message=".$message);

mysql_close($connection);

function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}
?>