<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$perpage=$_POST['perpage'];
$modedt = $_GET['modedt'];
//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);
}

$Costcent_code = ereg_replace("'", "\'", $Costcent_code);
$Costcent_name = ereg_replace("'", "\'", $Costcent_name);

if (!empty($Costcent_CreationDate))	{
	$Costcent_CreationDate  = explodeword($Costcent_CreationDate );
	$Costcent_CreationDate   = "'$Costcent_CreationDate '";

}	
else {$Costcent_CreationDate = 'null';}

$Costcent_code=trim($Costcent_code);

if($mode=='' && $modedt=='SetCostcent1'){
	$Costcent_code = $_GET['Costcent_code'];
	$cost_id = $_GET['cost_id'];

	echo "[BRK]";
	getCostcentChild($Costcent_code, $cost_id, $connection);
	echo "[BRK]";
exit;
}


if ($mode=='' && $modedt =='saveval'){
	$Costcent_code = $_GET['Costcent_code'];
	$cost_id = $_GET['cost_id'];
	$cost_percentage = $_GET['cost_percentage'];
	$cost_number = $_GET['cost_number'];

		$queryedt="update costcent
   							set  	CostCent_Percentage=".GetSQLValueString($cost_percentage,'double').", 
   									costcent_number=".GetSQLValueString($cost_number,'double')."
   							where 	Costcent_code='".$cost_id."'";
   								
   	$resultedt=mysql_query($queryedt,$connection);
	echo "[BRK]";
	getCostcentChild($Costcent_code, $cost_id, $connection);
	echo "[BRK]";
	exit;
}

if($mode=='insert'){
		
	$query="Insert into CostCent (Costcent_code,Costcent_name,Costcent_BelongsToGroup,Costcent_CreationDate,Costcent_Deactivate,Parent_Code, Owner_Branch)
			values ('".$Costcent_code."', '".$Costcent_name."', $FI_BelongsToGrp, $Costcent_CreationDate,$Costcent_active,'".$parent_code."', '".$Setup_Branch."')";

	//echo$query;				
	
	$result  = mysql_query($query, $connection);
	$Costcent_Id = mysql_insert_id($connection);
	//$message = mysql_error();
    if($config['log_file'] == 1){
		$transType = "I";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select costcent_id from costcent where  costcent_code='$Costcent_code'";
		$resultid=mysql_query($queryid,$connection);
		$rowid=mysql_fetch_row($resultid);
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$rowid[0]', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'costcent', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','COSTCENT','',$Costcent_code,$Setup_Branch);
		}
	}
else	$message ="this code already exist ";	

}

if($mode=='delete')
{	
	$query="select Costcent_code  from Costcent where Costcent_Id='".$CostCentId."'";
	$result = mysql_query($query, $connection);
	$row=mysql_fetch_row($result );
	$costcent_codePrv=$row[0];
	 if($config['log_file'] == 1){
		$transType = "D";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);
        $queryid="select costcent_id from costcent where  costcent_code='$Costcent_code'";
		$resultid=mysql_query($queryid,$connection);
		$rowid=mysql_fetch_row($resultid);		
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType', '$rowid[0]', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'costcent', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	$query="delete from Costcent where Costcent_Id='".$CostCentId."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	$errornum= mysql_errno();
						
	if($errornum=='1451') {
		$message="Delete Not Allowed . CostCenter is used .";
		//echo $message;
	}
	else{
		if($result){
			$query="update Costcent set parent_code=null where parent_code='".$costcent_codePrv."'";
			//echo$query;exit;
			$result = mysql_query($query, $connection);
			
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'D','COSTCENT','',$costcent_codePrv,$Setup_Branch);
			}
		}
	}
}



if($mode=='edit'){
	$query="select Costcent_code  from Costcent where Costcent_Id='".$Costcent_Id."'";
	//echo$query;exit;
	$result = mysql_query($query, $connection);
	$row=mysql_fetch_row($result );
	$costcent_codePrv=$row[0];
	
	$query_update=" update CostCent set Costcent_code='".$Costcent_code."',
					Costcent_name='".$Costcent_name."',
					Costcent_CreationDate= $Costcent_CreationDate,
					Costcent_Deactivate=$Costcent_active
					where Costcent_Id='".$Costcent_Id."'";
	//echo $query_update;exit;		

	$resultedit = mysql_query($query_update, $connection);
	$message    = mysql_error();
	if($config['log_file'] == 1){
		$transType = "U";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select costcent_id from costcent where  costcent_code='$Costcent_code'";
		$resultid=mysql_query($queryid,$connection);
		$rowid=mysql_fetch_row($resultid);
		$query_insertToHistory  = "call ".$config['dbName_history'].".InsertToHistory('$transType','$rowid[0]', '".$_SESSION['useraccount']."', now(), '".$_SESSION['dbName']."', 'costcent', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	if($resultedit && $costcent_codePrv!=$Costcent_code){
		$query="update CostCent set parent_code='".$Costcent_code."' where parent_code='".$costcent_codePrv."'";
		//echo$query;exit;
		$result = mysql_query($query, $connection);
	
	}
	
	if ($resultedit) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','COSTCENT','',$Costcent_code,$Setup_Branch);
		}
	}	

}

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

if($mode=='delete')
	header ("Location: ./Form_CostCenter.php?mode=insert&message=".$message."&perpage=".$perpage);
else 
header ("Location: ./Form_CostCenter.php?Costcent_Id=$Costcent_Id&mode=editmode&message=".$message."&perpage=".$perpage);

mysql_close($connection);



print <<<HERE
</form>
</body>

HERE;


?>
		           