<?php

session_start();


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

include("config.inc.php");

/// E Reg replace for single quote
$financial_label = ereg_replace("'", "\'", $financial_label);


$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");


if( $FinancialTot_bgcolor == '') $FinancialTot_bgcolor = "#FFFFFF";
if( $FinancialTot_fontcolor == '') $FinancialTot_fontcolor = "#000000";
if( $FinancialTot_fontsize == '') $FinancialTot_fontsize = 0;
if( $financial_relatedPer == '') $financial_relatedPer = "NULL";



/// Insert into Label Header
if($mode=='insert'){

	$query_insertheader="insert into FinancialTotLabel (FinancialTot_Label, FinancialTot_Level, FinancialTot_LevelCode,FinancialTot_Sign,
						FinancialTot_ShowResult, FinancialTot_bgcolor, FinancialTot_fontcolor, FinancialTot_fontsize)
	values ('".$financial_label."','1','01',".$financialtot_sign.",".$financialtot_showresult.", 
			'".$FinancialTot_bgcolor."','".$FinancialTot_fontcolor."','".$FinancialTot_fontsize."')";
	
	$result  = mysql_query($query_insertheader, $connection);
	$message = mysql_error();	
		
	if($message==''){
		$financial_code = mysql_insert_id();
	}
	if ($financial_code!='' or $financial_code!=0){
		$query="update FinancialTotLabel set FinancialTot_TreeCode = '".$financial_code."'
				where FinancialTot_Code = '".$financial_code."'";
		$result  = mysql_query($query, $connection);
		$message = mysql_error();	
	}	
}

// Edit main label
if($mode=='edit'){
	$query_updateheader="update FinancialTotLabel set FinancialTot_Label='".$financial_label."',
						
						FinancialTot_Sign=".$financialtot_sign.",
						FinancialTot_ShowResult=".$financialtot_showresult.",
						FinancialTot_bgcolor = '".$FinancialTot_bgcolor."', FinancialTot_fontcolor = '".$FinancialTot_fontcolor."', 
						FinancialTot_fontsize = '".$FinancialTot_fontsize."', FinancialTot_RelatedPer = ".$financial_relatedPer."
						where FinancialTot_code='".$financial_code."'";
				
	$result  = mysql_query($query_updateheader, $connection);
	$message = mysql_error();
}

/// Insert detail to main label+generating level code
if($mode=='insertdt'){
	$queryorder = "select ifnull(max(FinancialTot_order),0) from financialtotlabel
				where FinancialTot_RelatedCode = $financial_relatedcode ";
	$resorder = mysql_query($queryorder,$connection);
	$roworder = mysql_fetch_row($resorder);
	$order = $roworder[0]+1;
	
	$sub_query = " Select FinancialTot_Level, FinancialTot_LevelCode, FinancialTot_TreeCode ".
				 " From FinancialTotLabel ".
				 " Where FinancialTot_Code = '".$financial_relatedcode."'";
	$sub_result   = mysql_query($sub_query, $connection);
	$sub_row      = mysql_fetch_row($sub_result);

	$vFinancialTotLevel    = $sub_row[0] + 1;
	$vFinancialTotTreeCode = $sub_row[2];

	$sub_query1 = " Select Lpad(ifNull(Min(Substr(FinancialTot_LevelCode,length(FinancialTot_LevelCode)-1)),99) - 3, 2, '0') ".
					  " From FinancialTotLabel ".
					  " Where FinancialTot_Level = '".$vFinancialTotLevel."' ".
					  " And FinancialTot_TreeCode = '".$vFinancialTotTreeCode."' ";
	
	$sub_result1 = mysql_query($sub_query1, $connection);
	$sub_row1    = mysql_fetch_row($sub_result1);
	$vFinancialTotLevelCode = $sub_row[1].$sub_row1[0];	

	if (empty($financial_relatedcode)) $financial_relatedcode = 'Null';
	else $financial_relatedcode = "'".$financial_relatedcode."'";
	
	$query="Insert into FinancialTotLabel (FinancialTot_Label, FinancialTot_RelatedCode, FinancialTot_Level,FinancialTot_TreeCode, FinancialTot_LevelCode,FinancialTot_Sign,
			FinancialTot_ShowResult, FinancialTot_bgcolor, FinancialTot_fontcolor, FinancialTot_fontsize)
			values ('".$financial_label."',  $financial_relatedcode, '".$vFinancialTotLevel."', '".$vFinancialTotTreeCode."', '".$vFinancialTotLevelCode."',".$financialtot_sign.",
			".$financialtot_showresult.", '".$FinancialTot_bgcolor."','".$FinancialTot_fontcolor."','".$FinancialTot_fontsize."')";
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

		
}

//// Edit label detail
if($mode=='editdt'){
		
	$query_previousrelated="select FinancialTot_RelatedCode from FinancialTotLabel
							where FinancialTot_code='".$financial_code."'";
						//	echo$query_previousrelated;exit;
	$result_previous  = mysql_query($query_previousrelated, $connection);
	$row_prv=mysql_fetch_row($result_previous);
	$prv_related=$row_prv[0];
	
	//echo$prv_related.''.$financial_relatedcode;exit;
	
	if($prv_related!=$financial_relatedcode){
		$sub_query = " Select FinancialTot_Level, FinancialTot_LevelCode, FinancialTot_TreeCode ".
				 " From FinancialTotLabel ".
				 " Where FinancialTot_Code = '".$financial_relatedcode."'";
			//	 echo$sub_query;exit;
		$sub_result   = mysql_query($sub_query, $connection);
		$sub_row      = mysql_fetch_row($sub_result);

		$vFinancialTotLevel    = $sub_row[0] + 1;
		$vFinancialTotTreeCode = $sub_row[2];
		
		$sub_query1 = " Select Lpad(ifNull(Min(Substr(FinancialTot_LevelCode,length(FinancialTot_LevelCode)-1)),99) - 3, 2, '0') ".
					  " From FinancialTotLabel ".
					  " Where FinancialTot_Level = '".$vFinancialTotLevel."' ".
					  " And FinancialTot_TreeCode = '".$vFinancialTotTreeCode."' ";
	
		$sub_result1 = mysql_query($sub_query1, $connection);
		$sub_row1    = mysql_fetch_row($sub_result1);
		$vFinancialTotLevelCode = $sub_row[1].$sub_row1[0];
	//echo$vFinancialTotLevelCode;exit;
	
		if($vFinancialTotLevelCode!=''){
			$query_updateheader="update FinancialTotLabel set FinancialTot_Label='".$financial_label."',
						FinancialTot_LevelCode='".$vFinancialTotLevelCode."',
						FinancialTot_RelatedCode='".$financial_relatedcode."',
						FinancialTot_Sign=".$financialtot_sign.",
						FinancialTot_ShowResult=".$financialtot_showresult.",
						FinancialTot_bgcolor = '".$FinancialTot_bgcolor."', FinancialTot_fontcolor = '".$FinancialTot_fontcolor."', 
						FinancialTot_fontsize = '".$FinancialTot_fontsize."', FinancialTot_RelatedPer = ".$financial_relatedPer."
						where FinancialTot_code='".$financial_code."'";
				
			$result  = mysql_query($query_updateheader, $connection);
			$message = mysql_error();
		}
	}
	
	else{
		$queryNewOrder = "select financialtot_code, FinancialTot_LevelCode,FinancialTot_order from financialtotlabel
					where FinancialTot_RelatedCode = '$financial_relatedcode' and FinancialTot_order='$FinancialTot_order';";
		$resNewOrder = mysql_query($queryNewOrder,$connection);
		$rowNewOrder = mysql_fetch_row($resNewOrder);
		$newtotCode = $rowNewOrder[0];
		$newtotLvlCode = $rowNewOrder[1];
		$newtotOrder = $rowNewOrder[2];
		
		$queryOldOrder = "select FinancialTot_LevelCode,FinancialTot_order from financialtotlabel where financialtot_code = '$financial_code' ";
		$resOldOrder = mysql_query($queryOldOrder,$connection);
		$rowOldOrder = mysql_fetch_row($resOldOrder);
		$oldtotLvlCode = $rowOldOrder[0];
		$oldtotOrder = $rowOldOrder[1];
		
		if($oldtotOrder != $FinancialTot_order){
			$queryupnew = "update financialtotlabel set FinancialTot_LevelCode = '$oldtotLvlCode',FinancialTot_order = '$oldtotOrder' where financialtot_code = '$newtotCode' ";
			$resupnew = mysql_query($queryupnew,$connection);
			//echo $queryupnew;echo"<br>";
			$queryupcur = "update financialtotlabel set FinancialTot_LevelCode = '$newtotLvlCode',FinancialTot_order = '$newtotOrder' where financialtot_code = '$financial_code' ";
			$resupcur = mysql_query($queryupcur,$connection);
			//echo $queryupcur;
		}
		
		$query_updateheader="update FinancialTotLabel set FinancialTot_Label='".$financial_label."',
							FinancialTot_Sign=".$financialtot_sign.",
							FinancialTot_ShowResult=".$financialtot_showresult.",
							FinancialTot_bgcolor = '".$FinancialTot_bgcolor."', FinancialTot_fontcolor = '".$FinancialTot_fontcolor."', 
							FinancialTot_fontsize = '".$FinancialTot_fontsize."', FinancialTot_RelatedPer = ".$financial_relatedPer."
							where FinancialTot_code='".$financial_code."'";
					
		$result  = mysql_query($query_updateheader, $connection);
		$message = mysql_error();
	}
}

/// Delete detail
if($mode=='deletedt'){
	fbegin();
	
		$query_delete="delete from FinancialTotLabel where FinancialTot_RelatedCode='".$financial_code."'";
		$result  = mysql_query($query_delete, $connection);
		$message = mysql_error();

		if($result){
			$query_delete="delete from FinancialTotLabel where FinancialTot_code='".$financial_code."'";
			$result  = mysql_query($query_delete, $connection);
			$message = mysql_error();
		}
		
		if($message=='')
		fcommit();
		else {
			frollback();
			$message=' Delete Not Allowed';
		}
	
}

/// Delete main label (related null)
if($mode=='deletetop'){
	fbegin();
	$query_delete="delete from FinancialTotLabel where FinancialTot_treeCode='".$financial_code."'";
	
	$result  = mysql_query($query_delete, $connection);
	$message = mysql_error();

	if($result){
		$query_delete="delete from FinancialTotLabel where FinancialTot_code='".$financial_code."'";
		$result  = mysql_query($query_delete, $connection);
		$message = mysql_error();
	}
	if($message=='')
		fcommit();
	else {
		frollback();
		$message='Label Cannot be Deleted';
	}

}

if($balanceType == '') $balanceType = "NULL";

if($backgroundgcolor == '') $backgroundgcolor = "#FFFFFF";
if($fontsize == '') $fontsize = 15;
if($isContinuous == '') $isContinuous = 0;	

/// Insert ledger 
if($modedt=='insert'){
	$projectcode = "".$projectcode;
	$costcentcode = "".$costcentcode;
	
	if($projectcode == '') $projectcode = "NULL";
	else $projectcode = "'".$projectcode."'";
	if($costcentcode == '') $costcentcode = "NULL";
	else $costcentcode = "'".$costcentcode."'";
	
	if($dbcd == '') $dbcd = "NULL";
	else $dbcd = "'".$dbcd."'";
	
	$querydt= " Insert into FinancialTemplate ( Ledger_Number, Financial_order, FinancialTot_Code, Ledger_ShowDT, project_code, costcent_code, dbcd, balanceType, ".
			" Financial_bgcolor,Financial_fontcolor,Financial_fontsize,Financial_isContinuous) ".
				  " values ( '".$ledgerNbr."',  '".$Financial_order."', '".$financialdt_code."', '".$Ledger_ShowDt."', $projectcode, $costcentcode, $dbcd, $balanceType, ".
			"	'".$backgroundgcolor."', '".$fontcolor."', '".$fontsize."', '".$isContinuous."' )";
						 
	
	$resultdt  = mysql_query($querydt, $connection);
	$FinancialTemplateID= mysql_insert_id($connection);
	$message = mysql_error();	
}

/// Edit Ledger
if ($modedt== 'edit'){
	$projectcode = "".$projectcode;
	$costcentcode = "".$costcentcode;
	
	if($projectcode == '') $projectcode = "NULL";
	else $projectcode = "'".$projectcode."'";
	if($costcentcode == '') $costcentcode = "NULL";
	else $costcentcode = "'".$costcentcode."'";
	
	if($dbcd == '') $dbcd = "NULL";
	else $dbcd = "'".$dbcd."'";
	
	$querydt= " Update financialtemplate Set 
			  Ledger_Number = '".$ledgerNbr."', 
			  Financial_order = '".$Financial_order."', 
			  Ledger_ShowDt = '".$Ledger_ShowDt."',
			  project_code = $projectcode, 
			  costcent_code	= $costcentcode,
			  dbcd = $dbcd	,
			  balanceType = $balanceType,		
			  Financial_bgcolor = '".$backgroundgcolor."',		
			  Financial_fontcolor = '".$fontcolor."',		
			  Financial_fontsize = '".$fontsize."'	,	
			  Financial_isContinuous ='".$isContinuous."' 
			  Where FinancialTemplate_ID = '".$FinancialTemplate_ID."' ";
		
	
	$resultdt  = mysql_query($querydt, $connection);
	$message = mysql_error();
}


/// Delete detail ledger
if ($modedt == 'delete'){
	
	$query="delete from FinancialTemplate where FinancialTemplate_ID='".$FinancialTemplate_ID."'";
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
}

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

if($mode=='insert' || $mode=='edit'  || $mode=='deletedt' || $mode=='insertdt' || $mode=='editdt' || $mode=='deletetop')			   
	header ("Location: ./Form_FinancialAccounts.php?message=".$message);

else if($modedt=='insert' || $modedt=='edit' || $modedt=='delete')
	header ("Location: ./Form_FinancialAccountsDetailLedgers.php?FinancialTemplateID=$FinancialTemplateID&financialdt_code=$financialdt_code&modedt=insert&refreshpage=1&message=".$message);

mysql_close($connection);

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