<?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($_SESSION['dbName'],$connection) or die("Could not find DB");

//User Access
$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp 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);
}
$queryBase= "select BASE1, BASE2, Setup_Branch from setup";
$resultBase=mysql_query($queryBase, $connection);
$rowBase=mysql_fetch_array($resultBase);
extract($rowBase);

//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);
}


//echo 'mainmode='.$mainmode1; exit;
    
$Curency_code=ereg_replace("'", "\'", $Currency_code);
$Curency_name=ereg_replace("'", "\'", $Currency_name);

if (!empty($Currency_CreationDate))	{
	$Currency_CreationDate  = explodeword($Currency_CreationDate );
	$Currency_CreationDate   = "'$Currency_CreationDate '";

}	
else {$Currency_CreationDate = 'null';}

if (!empty($Curdt_date))	{
	$Curdt_date  = explodeword($Curdt_date);
	$Curdt_date  = "'$Curdt_date'";

}	
else {$Curdt_date= 'null';}

$Currency_code=trim($Currency_code);


if(($mainmode1=='insert' ||  $mainmode1=='edit') && $actionmode1 !='deletedt' && $actionmode!='delete'){
	$QueryExist  = " Select Count(*) From Currency Where Currency_code = '".$Currency_code."' ";
	$ResultExist = mysql_query($QueryExist, $connection);
	$RowExist = mysql_fetch_array($ResultExist);
	$vExists = $RowExist[0];


	if ($vExists == 0 && $mainmode1=='insert') {	
	
		fbegin();
		$Currency_Id = get_nextsequence('currency_id','',$connection);
		fcommit();
		
		$query="Insert into Currency(currency_id,Currency_code,Currency_name, Currency_nature,Currency_CreationDate, Owner_Branch)
				values ('".$Currency_Id."',upper('".$Currency_code."'), upper('".$Currency_name."'), $Currency_nature,$Currency_CreationDate, '".$Setup_Branch."')";
		//echo$query;exit;
		$result  = mysql_query($query, $connection);
		//$Currency_Id = mysql_insert_id($connection);
		$message = mysql_error();
		
		 if($config['log_file'] == 1){
		$transType = "I";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select currency_id from currency where  currency_code='$Currency_code'";
		$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']."', 'currency', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
		
		if ($result) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'I','CURRENCY','',$Currency_code,$Setup_Branch);
			}
		}
	}
	
	if($mainmode1=='edit') {
		$query=" Update Currency Set ".
		       " Currency_code = '".$Currency_code."', ".
			   " Currency_name = '".$Currency_name."', ".
			   " Currency_nature = '".$Currency_nature."' ".
			   " Where Currency_Id ='".$Currency_Id."'";
		//echo $query;exit;
		
		$result  = mysql_query($query, $connection);
		$message = mysql_error();
		if($config['log_file'] == 1){
		$transType = "U";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select currency_id from currency where  currency_code='$Currency_code'";
		$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']."', 'currency', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
		if ($result) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'U','CURRENCY','',$Currency_code,$Setup_Branch);
			}
		}
	}
	
	if ($Curdt_date != '' && $Curdt_rate != '' && $Curdt_parity != '' && $Currency_code != '') {
		if ($Curdt_increment == '')
		{
			$QueryExist  = " Select Count(*) From Curdt Where Currency_code = '".$Currency_code."' And Curdt_date = $Curdt_date";
			//echo$QueryExist;exit;
			$ResultExist = mysql_query($QueryExist, $connection);
			$RowExist = mysql_fetch_array($ResultExist);
			$vExists = $RowExist[0];
	
			if ($vExists == 0) {
				fbegin();
				$CurrDt_Id = get_nextsequence('curdt_id','',$connection);
				fcommit();
		
				$querydt=" Insert into curdt(Currency_code, Curdt_date, Curdt_rate, Curdt_parity,currency_id,curdt_id) ".
						 " values ('".$Currency_code."', $Curdt_date, '".$Curdt_rate."', '".$Curdt_parity."', '".$Currency_Id."','".$CurrDt_Id."' )";
				//echo$querydt;exit;	 
				$result  = mysql_query($querydt, $connection);
				$message = mysql_error();		 
			 
			
				/*if ($Currency_code == $BASE1){
					fbegin();
					$CurrDt_Id = get_nextsequence('curdt_id','',$connection);
					fcommit();
					
					$querydt=" Insert into curdt(Currency_code, Curdt_date, Curdt_rate, Curdt_parity,currency_id,curdt_id) ".
							 " values ('".$BASE2."', $Curdt_date, '".$Curdt_parity."', '".$Curdt_rate."','".$Currency_Id."','".$CurrDt_Id."')";
							 
					$result  = mysql_query($querydt, $connection);
					$message = mysql_error();		
				
				}*/
			}
			else {
				$message = 'Duplicate Entry ...';		 
			}	
		} else {
			$querydt=" Update curdt Set ".
					 " Curdt_date = $Curdt_date, ".
					 " Curdt_rate = '".$Curdt_rate."', ".
					 " Curdt_parity = '".$Curdt_parity."' ".
					 " Where Curdt_id= '".$Curdt_increment."' ";

			$result  = mysql_query($querydt, $connection);
			$message = mysql_error();		 
		
		}
		
		if ($result) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'U','CURRENCY','',$Currency_code,$Setup_Branch);
			}
		}
	}	
	
//echo $query."<br>".$querydt;	exit;	
			  
}

/*
if($mainmode1=='post' && $Currency_code !='')
{
	$query=" Update Currency Set ".
	       " Currency_code = '".$Currency_code."', ".
		   " Currency_name = '".$Currency_name."', ".
		   " Currency_nature = $Currency_nature ,".
		   " Currency_CreationDate = $Currency_CreationDate ".
		   " Where Currency_Id = '".$Currency_Id."'";
				
		//echo $query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
}	*/	


if($actionmode1=='deletedt' && $Curdt_increment !='')
{
	$query = "select Currency_code from curdt where Curdt_id='".$Curdt_increment."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$Currency_code=$rows[0];
	
	$query="delete from curdt where Curdt_id='".$Curdt_increment."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','CURRENCY','',$Currency_code,$Setup_Branch);
		}
	}
}

if($actionmode=='delete'){	
	$query = "select Currency_code from Currency where Currency_Id='".$CurrencyId."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$Currency_code=$rows[0];
	
	if($config['log_file'] == 1){
		$transType = "D";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select currency_id from currency where  currency_code='$Currency_code'";
		$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']."', 'currency', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	
	$query="delete from curdt where Currency_Id='".$CurrencyId."'";
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	$query="delete from Currency where Currency_Id='".$CurrencyId."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'D','CURRENCY','',$Currency_code,$Setup_Branch);
		}
	}
	
}


$message = ereg_replace(' ','%20',$message);
if($actionmode=='delete')
	header ("Location: ./Form_Currency.php?mainmode=insert&message=".$message);
else 
header ("Location: ./Form_Currency.php?Currency_Id=$Currency_Id&mainmode=edit&message=".$message);

mysql_close($connection);

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

?>