<?php


//Return Date in DD/MM/YYYY format
function displayToday(){
	return strftime("%d")."/".strftime("%m")."/".strftime("%Y");
}

//Return Today's Date in YYYYMMDD format. Supplied Format: DD/MM/YYYY
function insertDate($data){
	return substr($data, 6, 4) . substr($data, 3, 2) . substr($data, 0, 2);
}

//Return Date in YYYY-MM-DD HH:MM:SS format. Supplied Format: DD/MM/YYYY
function insertMySQLDate($data){
	$data = insertDate($data);
	return substr($data, 0, 4) ."-". substr($data, 4, 2) ."-".substr($data, 6, 2)." ".strftime("%H").":".strftime("%M").":".strftime("%S");;
}
// Return the number of days between the two dates format YYYY-MM-DD:
function dateDiff ($d1, $d2) {
	return round(abs(strtotime($d1)-strtotime($d2))/86400);
}
	
//Function to add Comma to a given Number
function addComma($data){
	return number_format($data, 2, '.', ',');
}
//Method to query database and get the first row
function selectData($conn, $query){
	$result = mysql_query($query, $conn);
	$cur = mysql_fetch_row($result);		
	return $cur;
}

//Method to Insert/ Update/ Delete Record in Database
function updateData($conn, $query, $commit){
	if (mysql_query($query, $conn)){				
		return true;
	}else{
		return false;
	}
}

function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
{
	$query = "SELECT ifNull(convertcurfunc('".$FromCurr."', '".$ToCurr."', '".$FromDate."', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
	$result = selectData($connection, $query);
	
	Return	$result[0];

}

function mround($number, $precision=0) { 
    
    $precision = ($precision == 0 ? 1 : $precision);    
    $pow = pow(10, $precision); 
    
    $ceil = ceil($number * $pow)/$pow; 
    $floor = floor($number * $pow)/$pow; 
    
    $pow = pow(10, $precision+1); 
    
    $diffCeil     = $pow*($ceil-$number); 
    $diffFloor     = $pow*($number-$floor)+($number < 0 ? -1 : 1); 
    
    if($diffCeil >= $diffFloor) return $floor; 
    else return $ceil; 
} 

function mabsolute($number) {
   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");
		
	//Read From Setup Table							
	$querysetup= " select DecimalBase from setup ";
	$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
	while($rowsetup=mysql_fetch_array($resultsetup)){
		extract($rowsetup);
	}

   // jp removed Exponencial
   return  str_replace(',','',number_format(abs($number),$DecimalBase)) ;
}

function ReplaceHtmlChars($theValue){

	/// Replaces #, & and spaces with % ...
	$theValue=rawurlencode($theValue );
	
	return $theValue;
}

function Create_TransJv($PTransId, $DbName, $DbLabel, $UserAccount, $UserName) {

	include("config.inc.php");
	
	$conn = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
	mysql_select_db($DbName, $conn) or die("Could not find DB");

	fbegin();
	$resultvars = mysql_query("set @id :=0 ", $conn);

	$query = " call Create_TransAutoJv(".$PTransId.", 1, '".$DbName."', '".$DbLabel."', '".$UserAccount."', '".$UserName."', @id) ";
	//echo $query; //exit;
	$result = mysql_query($query,$conn);
    $error = mysql_error();
	
	$query1 = " select @id from dual ";
	$result1 = mysql_query($query1,$conn) or die(mysql_error());
	$cur1 = mysql_fetch_row($result1);
	

	fcommit();
	
	
    if ($cur1[0] == -2 || $cur1[0] == -1)
	   $error = 'Error In Procedure Create_TransAutoJv: '.$error;
	else if  ($error != '')  
		$error = 'Error In Function Create_TransJv: '.$error;

	Return $error;
	

}

function Create_SalesJVJv($PId, $DbName, $DbLabel, $UserAccount, $UserName, $BelongsToGrp) {

	include("config.inc.php");
	
	$conn = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
	mysql_select_db($DbName, $conn) or die("Could not find DB");

	fbegin();
	$resultvars = mysql_query("set @id :=0 ", $conn);

	$query = " call Create_SalesJVAutoJv(".$PId.", 0, '".$DbName."', '".$DbLabel."', '".$UserAccount."', '".$UserName."', '".$BelongsToGrp."', @id) ";
	//echo $query; exit;
	$result = mysql_query($query,$conn);
    $error = mysql_error();
	
	$query1 = " select @id from dual ";
	$result1 = mysql_query($query1,$conn) or die(mysql_error());
	$cur1 = mysql_fetch_row($result1);
	

	fcommit();
	
	
    if ($cur1[0] == -2)
	   $error = 'Error In Procedure Create_SalesJVAutoJv: '.$error;
	else if  ($cur1[0] == -1)  
	   $error = 'Error In Procedure Create_Jv: '.$error;   
	else if  ($error != '')  
		$error = 'Error In Function Create_SalesJVJv: '.$error;

	Return $error;
	

}
	
function Create_PurchasesJVJv($PId, $DbName, $DbLabel, $UserAccount, $UserName, $BelongsToGrp) {

	include("config.inc.php");
	
	$conn = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
	mysql_select_db($DbName, $conn) or die("Could not find DB");

	fbegin();
	$resultvars = mysql_query("set @id :=0 ", $conn);

	$query = " call Create_PurchasesJVAutoJv(".$PId.", 0, '".$DbName."', '".$DbLabel."', '".$UserAccount."', '".$UserName."', '".$BelongsToGrp."', @id) ";
	//echo $query; exit;
	$result = mysql_query($query,$conn);
    $error = mysql_error();
	
	$query1 = " select @id from dual ";
	$result1 = mysql_query($query1,$conn) or die(mysql_error());
	$cur1 = mysql_fetch_row($result1);
	

	fcommit();
	
	
    if ($cur1[0] == -2)
	   $error = 'Error In Procedure Create_PurchasesJVAutoJv: '.$error;
	else if  ($cur1[0] == -1)  
	   $error = 'Error In Procedure Create_Jv: '.$error;   
	else if  ($error != '')  
		$error = 'Error In Function Create_PurchasesJVJv: '.$error;

	Return $error;
	

}

function Generate_Chargings($PGenDate, $DbName, $DbLabel, $UserAccount, $UserName) {

	include("config.inc.php");
	
	$conn = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
	mysql_select_db($DbName, $conn) or die("Could not find DB");

	//fbegin();
	$resultvars = mysql_query("set @id :=0 ", $conn);

	$query = " call GenerateChargings('".$PGenDate."', 0, '".$DbName."', '".$DbLabel."', '".$UserAccount."', '".$UserName."', 1, @id) ";
	//echo $query; exit;
	$result = mysql_query($query,$conn);
    $error = mysql_error();
	
	$query1 = " select @id from dual ";
	$result1 = mysql_query($query1,$conn) or die(mysql_error());
	$cur1 = mysql_fetch_row($result1);
	

	//fcommit();
	
	$Result = array();
	
    if ($cur1[0] == -2)
	   $Result['error'] = 'Error In Procedure GenerateChargings: '.$error;
	else if  ($cur1[0] == -1)  
	   $Result['error'] = 'Error In Procedure Create_Jv: '.$error;   
	else if  ($error != '')  
		$Result['error'] = 'Error In Function Generate_Chargings: '.$error;
	else	
		$Result['id'] = $cur1[0];
		
	Return $Result;
	

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


?>
