<?php

session_start();


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

include("config.inc.php");
include("function.inc.php");
include("Functions_Synchro.php");
//echo $LedgerGrp_Type;exit;

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



$message     = $_POST["message"]; if ($message == ""){$message = $_GET["message"];}  
$LedgerType     = $_POST["LedgerType"]; if ($LedgerType == ""){$LedgerType = $_GET["LedgerType"];}  
$LedgerGroupId     = $_POST["LedgerGroupId"]; if ($LedgerGroupId == ""){$LedgerGroupId = $_GET["LedgerGroupId"];}  


if($config['log_file'] == 1){
$connection_history = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) or die("Could not connect to DB");
mysql_select_db($config['dbName_history'],$connection_history) or die("Could not find DB");
}

$query_user_name = "select User_Name from ".$_SESSION['dbLabel'].".users where user_account='".$_SESSION['useraccount']."'";
$result_user_name = mysql_query($query_user_name, $connection);
$rows_user_name = mysql_fetch_array($result_user_name);
extract($rows_user_name);
//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);
}

//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);
}
  
$LedgerGrp_CreationDate = $_POST['LedgerGrp_CreationDate']; if($LedgerGrp_CreationDate == '') $LedgerGrp_CreationDate = $_GET['LedgerGrp_CreationDate'];
$LedgerGrp_Code = $_POST['LedgerGrp_Code']; if($LedgerGrp_Code == '') $LedgerGrp_Code = $_GET['LedgerGrp_Code'];
$LedgerGrp_Name = $_POST['LedgerGrp_Name']; if($LedgerGrp_Name == '') $LedgerGrp_Name = $_GET['LedgerGrp_Name'];
$project_code = $_POST['project_code']; if($project_code == '') $project_code = $_GET['project_code'];
$project_name = $_POST['project_name']; if($project_name == '') $project_name = $_GET['project_name'];
$txtKeyword = $_POST['txtKeyword']; if($txtKeyword == '') $txtKeyword = $_GET['txtKeyword'];

    
		$LedgerGrp_Code=ereg_replace("'", "\'", $LedgerGrp_Code);
		$LedgerGrp_Name=ereg_replace("'", "\'", $LedgerGrp_Name);
		$project_code=ereg_replace("'", "\'", $project_code);
		
if (!empty($LedgerGrp_CreationDate))	{
	$LedgerGrp_CreationDate  = explodeword($LedgerGrp_CreationDate );
	$LedgerGrp_CreationDate   = "'$LedgerGrp_CreationDate '";

}	
else {$LedgerGrp_CreationDate = 'null';}



if(empty($project_code))
  $project_code= "NULL";
else
  $project_code="'$project_code'";

if(empty($Setup_Branch))
  $Setup_Branch= "NULL" ;
else
  $Setup_Branch="'$Setup_Branch'";



$LedgerGrp_Code=trim($LedgerGrp_Code);



if($mode=='insert'){
		
	$query="Insert into LedgerGrp (LedgerGrp_Code,LedgerGrp_Name,project_code,LedgerGrp_BelongsToGrp, LedgerGrp_Type, LedgerGrp_CreationDate, Owner_Branch)
			values ('".$LedgerGrp_Code."', '".$LedgerGrp_Name."', $project_code, $FI_BelongsToGrp, '$LedgerType', $LedgerGrp_CreationDate, ".$Setup_Branch.")";

	//echo$query;	exit;	
	
	$result  = mysql_query($query, $connection);
	
	if($config['log_file'] == 1){
							
		$query_history="insert into ledgergrp (LedgerGrp_Code,LedgerGrp_Name,project_code,LedgerGrp_BelongsToGrp, LedgerGrp_Type, LedgerGrp_CreationDate, Owner_Branch,	LedgerGrp_Date, LedgerGrp_operator_id, LedgerGrp_schema, LedgerGrp_status)
						values ('".$LedgerGrp_Code."', '".$LedgerGrp_Name."', $project_code, $FI_BelongsToGrp,$LedgerType, $LedgerGrp_CreationDate, ".$Setup_Branch.",
						now(), '".$User_Name."', '".$_SESSION['dbName']."', 'I')";
		$result_history  = mysql_query($query_history, $connection_history);
		$message = mysql_error().$query_history;
	}
	$LedgerGrp_Id = mysql_insert_id($connection);
	$message = mysql_error();

	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','LEDGERGRP','',$LedgerGrp_Code,$Setup_Branch);
		}
	}
}

if($mode=='delete')
{	
	$query="delete from LedgerGrp where LedgerGrp_Id='".$LedgerGroupId."' and LedgerGrp_Type='".$LedgerType."'";
	//echo$query;exit;
	
	$select_history ="select LedgerGrp_Id,LedgerGrp_Type,LedgerGrp_Code,LedgerGrp_Name from  LedgerGrp";
		$result_history  = mysql_query($select_history, $connection);
		$rows=mysql_fetch_row($result_history);
		
	$LedgerGroupId=$rows[0];
	$LedgerType=$rows[1];
	$LedgerGrp_Code=$rows[2];
	$LedgerGrp_Name=$rows[3];
							
	if($config['log_file'] == 1){
							
		$query_history="insert into ledgergrp (LedgerGrp_Type,LedgerGrp_Code,LedgerGrp_Name, LedgerGrp_Date, LedgerGrp_operator_id, LedgerGrp_schema, LedgerGrp_status)
							values ( '".$LedgerType."','".$LedgerGrp_Code."','".$LedgerGrp_Name."',	now(), '".$User_Name."', '".$_SESSION['dbName']."', 'D')";
		$result_history  = mysql_query($query_history, $connection_history);
		//or die(mysql_error($query_history));
		$message = mysql_error();
	}	
	$result  = mysql_query($query, $connection);
	$message = mysql_error();

	
}


if($mode=='edit'){
	$query_update=" update LedgerGrp set LedgerGrp_Code='".$LedgerGrp_Code."', ".
				  "	LedgerGrp_Name='".$LedgerGrp_Name."' , ".
				  " LedgerGrp_CreationDate = $LedgerGrp_CreationDate ,".
				  "	project_code = $project_code ".
				  "	where LedgerGrp_Id='".$LedgerGrp_Id."' and LedgerGrp_Type='".$LedgerType."'";
	//echo $query_update;exit;		
if($config['log_file'] == 1){
							
							$query_history="insert into ledgergrp ( LedgerGrp_Code,LedgerGrp_Type,
							LedgerGrp_Name, LedgerGrp_CreationDate,project_code ,
							LedgerGrp_Date, LedgerGrp_operator_id, LedgerGrp_schema, LedgerGrp_status)
							values ( '".$LedgerGrp_Code."','".$LedgerType."','".$LedgerGrp_Name."',
							$LedgerGrp_CreationDate ,$project_code,
							now(), '".$User_Name."', '".$_SESSION['dbName']."', 'U')";
						$result_history  = mysql_query($query_history, $connection_history);
						$message = mysql_error();
						}
	
	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();

	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','LEDGERGRP','',$LedgerGrp_Code,$Setup_Branch);
		}
	}
}

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

if($mode=='delete')			   
	header ("Location: ./Form_LedgerGroup.php?mode=insert&LedgerType=".$LedgerType."&message=".$message);
else
	header ("Location: ./Form_LedgerGroup.php?LedgerGrp_Id=$LedgerGrp_Id&mode=editmode&LedgerType=".$LedgerType."&message=".$message);

mysql_close($connection);




?>