<?php

session_start();


if($_SESSION['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']) 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);
}

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

//echo 'mainmode='.$mainmode; exit;
    
$Acctype_code=ereg_replace("'", "\'", $Acctype_code);
$Acctype_name=ereg_replace("'", "\'", $Acctype_name);
$ledger_code=ereg_replace("'", "\'", $ledger_code);
$project_code=ereg_replace("'", "\'", $project_code);
$Acctype_SameLength=ereg_replace("'", "\'", $Acctype_SameLength);
		
if(empty($project_code))
  $project_code= 'null' ;
else
  $project_code="'$project_code'";
  
  

if(empty($ledger_code))
  $ledger_code= 'null' ;
else
  $ledger_code="'$ledger_code'";
  
if(empty($ledger_number))
  $ledger_number= 'null' ;
else
  $ledger_number="'$ledger_number'";  

$Acctype_code=trim($Acctype_code);

if($mainmode=='insert' ||  $mainmode=='edit' ){
	
	if ($Acctype_increment =='') {	
		fbegin();
		$Acctype_increment = get_nextsequence('Acctype_increment','',$connection);
		fcommit();
		
		$query="Insert into Acctype (Acctype_increment,Acctype_code,Acctype_name, Acctype_ledger, project_code, Acctype_SameLength, Acctype_BelongsToGroup, Acctype_Type,DepreciationPer, Owner_Branch)
				values ('".$Acctype_increment."','".$Acctype_code."', '".$Acctype_name."',  $ledger_code, $project_code, '".$Acctype_SameLength."', $FI_BelongsToGrp, '".$AccountType."','".$deprec_perc."', '".$Setup_Branch."')";

		$result  = mysql_query($query, $connection);
		//$Acctype_increment= mysql_insert_id($connection);
		$message = mysql_error();
		
		if ($result) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'I','ACCTYPE','',$Acctype_code,$Setup_Branch);
			}
		}
		
	} else {
		$query=" Update Acctype Set ".
		       " Acctype_code = '".$Acctype_code."', ".
			   " Acctype_name = '".$Acctype_name."', ".
			   " Acctype_ledger = $ledger_code, ".
			   " project_code = $project_code, ".
			   " Acctype_SameLength = '".$Acctype_SameLength."' ,".
			   " Acctype_Type='".$AccountType."', ".
			    " DepreciationPer='".$deprec_perc."' ".
			   " Where Acctype_increment = '".$Acctype_increment."'";
				
		$result  = mysql_query($query, $connection);
		$message = mysql_error();
		
		if ($result) {
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'U','ACCTYPE','',$Acctype_code,$Setup_Branch);
			}
		}
	}

}

if($actionmode=='submitsetup'){

	if ($AccTypeSetupInc =='') {	
		$querydt=" Insert into AcctypeSetup (Type_Code,Type_name, GeneratedType_Name, Ledger_Number, Ledger_activity) ".
				 " values ('".$Acctype_code."', '".$Acctype_name."', '".$type_name."',  $ledger_number, $ledger_activity)";
					 
		$result  = mysql_query($querydt, $connection);
		$AccTypeSetupInc= mysql_insert_id($connection);
		$message = mysql_error();		 
	} else {
		$querydt=" Update AcctypeSetup Set ".
				 " Type_Code = '".$Acctype_code."', ".
				 " Type_name = '".$Acctype_name."', ".
				 " GeneratedType_Name = '".$type_name."', ".
				 " Ledger_Number = $ledger_number, ".
				 " Ledger_activity = $ledger_activity ".
				 " Where AccTypeSetupInc = '".$AccTypeSetupInc."' ";
					 
		$result  = mysql_query($querydt, $connection);
		$message = mysql_error();		 
	}
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','ACCTYPE','',$Acctype_code,$Setup_Branch);
		}
	}
}



else if($actionmode=='deletesetup' && $AccTypeSetupInc !='')
{
	$query  = "select Type_Code from AcctypeSetup where AccTypeSetupInc='".$AccTypeSetupInc."'";
	$result = mysql_query($query, $connection);
	$row    = mysql_fetch_array($result);
	$AccTypeCode = $row[0];
	
	$query="delete from AcctypeSetup where AccTypeSetupInc='".$AccTypeSetupInc."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','ACCTYPE','',$AccTypeCode,$Setup_Branch);
		}
	}

}

if($mainmode=='delete')
{	
	$query  = "select Acctype_code from Acctype where Acctype_increment='".$AccTypeInc."'";
	$result = mysql_query($query, $connection);
	$row    = mysql_fetch_array($result);
	$AccTypeCode = $row[0];
	
	
	$query="delete from Acctype where Acctype_increment='".$AccTypeInc."'";
	//echo$query;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	$query="delete from AcctypeSetup where Type_Code='".$AccTypeCode."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'D','ACCTYPE','',$AccTypeCode,$Setup_Branch);
		}
	}

}

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

header ("Location: ./Form_Acctype.php?Acctype_increment=$Acctype_increment&mainmode=editmode&message=".$message);

mysql_close($connection);

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


?>