<?php

session_start();

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

include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.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");

$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2 ".
             " from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}


set_time_limit(0);

$message='';

$HideMenu   = $_POST["HideMenu"]; if ($HideMenu == ""){$HideMenu = $_GET["HideMenu"];}

if($mode == "TransferFunds"){
	
	$resultvars = mysql_query("set @Result := 0, @JvResult := 0 ", $connection);
	if($txtJourId=='') $txtJourId=0;
	if($fromproject=='') $fromproject ="NULL";
	else $fromproject = "'".$fromproject."'";
	if($toproject=='') $toproject ="NULL";
	else $toproject = "'".$toproject."'";
	if($fromcostcent=='') $fromcostcent ="NULL";
	else $fromcostcent = "'".$fromcostcent."'";
	if($tocostcent=='') $tocostcent ="NULL";
	else $tocostcent = "'".$tocostcent."'";
	$sub_query = " call TransferFunds('".$fromledger."', '".$toledger."', ".$fromproject.", '".$toproject."', '".$fromcostcent."', '".$tocostcent."', ".
				 "                    '".$transamount."', '".$transcurr."', '".insertMySQLDate($transdate)."', '".$transdesc."', 0, '". $_SESSION['dbName']."', '". $_SESSION['dbLabel']."', ".
				 "                    '". $_SESSION['useraccount']."', '". $_SESSION['username']."', '".$txtJourId."', $fundrate, @Result, @JvResult) ";
					
	//echo $sub_query; exit;
	$sub_result = mysql_query($sub_query,$connection);
    $error = mysql_error($connection);
	
	
	$sub_query1 = " select @Result, @JvResult from dual ";
	$sub_result1 = mysql_query($sub_query1,$connection) or die(mysql_error());
	$cur1 = mysql_fetch_row($sub_result1);

	
    if ($cur1[0] == -2) {
	   $message = 'Error In Procedure TransferFunds: '.$error;
	   //frollback();
	}   
	else if  ($cur1[0] == -1)  {
	   $message = 'Error In Procedure Create_Jv: '.$error;   
	   //frollback();
	}   
	else if ($cur1[0] >= 0 && $cur1[1] >= 0) {
		$message = 'Transfer done successfully. ';	
		//fcommit();
	}	
	
}

if($mode == 'getRate'){
	$txtDate 		= $_GET['txtDate'];
	$currency_code	= $_GET['currency_code'];
	$ndate = explode('/', $txtDate);
	$dateFormat = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	
	$queryrate = "select curdt_rate from curdt 
			where currency_code='$currency_code' and date_format(curdt_date,'%Y-%m-%d') <= '$dateFormat'
			order by date_format(curdt_date,'%Y-%m-%d') desc limit 1; ";
	$resrate = mysql_query($queryrate,$connection);
	$rwrate = mysql_fetch_row($resrate);
	$fundrate = $rwrate[0];
	echo "[BRK]";
	echo $fundrate;
	exit;
}

$message = ereg_replace(' ','%20',$message);
$message = ereg_replace('#','%23',$message);
$message = ereg_replace('&','%26',$message);

//echo $mode.$message; exit;

header ("Location: ./Form_TransferFunds.php?message=".$message);



?>