<?php
/**
	Page type C = Costcenter
	Page type P = Project
	
	CREATE NEW COSTCENTERS/PROJECTS BASED ON ROOT COSTCENTER/PROJECT
**/


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


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

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


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

$query_setup=" Select Setup_Branch from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

$pagetype = $_POST['pagetype'];
$rootCode = $_POST['rootCode'];
$newCode  = $_POST['newCode'];
$newDesc  = $_POST['newDesc'];

$rootCode = ereg_replace("'", "\'", $rootCode);
$newCode = ereg_replace("'", "\'", $newCode);
$newDesc = ereg_replace("'", "\'", $newDesc);
 
$ErrorExists = 0;

if ($pagetype == 'C'){
	//Check if root costcenter exists
	$queryChk = "select 1 from costcent where Costcent_code like '$rootCode%' ";
	$resChk   = mysql_query($queryChk, $connection);
	$numRoot  = mysql_num_rows($resChk); 
	if ($numRoot == 0) {
		$ErrorExists = 1;
		$message = "Root Code Does Not Exist!";
	}
	
	// Check if new Costcenter Doesn't exist
	$queryChk = "select 1 from costcent where Costcent_code = '$newCode' ";
	$resChk   = mysql_query($queryChk, $connection);
	$numNew  = mysql_num_rows($resChk); 
	
	
	if ($ErrorExists == 0){
		if ($numNew == 0){
			$insertNew = "insert into costcent (Costcent_code, Costcent_name, Costcent_BelongsToGroup, Costcent_CreationDate, Owner_Branch) 
						  values ('$newCode', '$newDesc', '$FI_BelongsToGrp', now(), '".$Setup_Branch."') ";
			$result = mysql_query($insertNew, $connection);
		}
		
		$queryRep = "select replace(costcent_code,'$rootCode','$newCode'), Costcent_name from costcent where costcent_code like '$rootCode%' ";//echo $queryRep;exit;
		$resRep = mysql_query($queryRep, $connection);
		while ($rwRep = mysql_fetch_array($resRep)){
			$insNew = "insert into costcent (Costcent_code, Costcent_name, Costcent_BelongsToGroup, Costcent_CreationDate, Owner_Branch) 
						  values (".GetSQLValueString($rwRep[0],'text').", ".GetSQLValueString($rwRep[1],'text').", '$FI_BelongsToGrp', now(), '".$Setup_Branch."') ";
			$res = mysql_query($insNew, $connection);
		}
	}
} 

if ($pagetype == 'P'){
	//Check if root costcenter exists
	$queryChk = "select 1 from project where Project_Code like '$rootCode%' ";
	$resChk   = mysql_query($queryChk, $connection);
	$numRoot  = mysql_num_rows($resChk); 
	if ($numRoot == 0) {
		$ErrorExists = 1;
		$message = "Root Code Does Not Exist!";
	}
	
	// Check if new Costcenter Doesn't exist
	$queryChk = "select 1 from project where Project_Code = '$newCode' ";
	$resChk   = mysql_query($queryChk, $connection);
	$numNew  = mysql_num_rows($resChk); 
	
	
	if ($ErrorExists == 0){
		if ($numNew == 0){
			$insertNew = "insert into project (Project_Code, Project_Name, Project_BelongsToGroup, Project_Date, Owner_Branch) 
						  values ('$newCode', '$newDesc', '$FI_BelongsToGrp', now(), '".$Setup_Branch."') ";
			$result = mysql_query($insertNew, $connection);
		}
		$queryprojid = "select max(project_id) from project ";
		$resprojid  = mysql_query($queryprojid, $connection);
		$rwprojid   = mysql_fetch_row($resprojid); 
		
		
		$queryRep = "select replace(Project_Code,'$rootCode','$newCode'), Project_Name from project where Project_Code like '$rootCode%' ";//echo $queryRep;exit;
		$resRep = mysql_query($queryRep, $connection);
		while ($rwRep = mysql_fetch_array($resRep)){
			$project_id = $rwprojid[0] + 1;
			$insNew = "insert into project (Project_Code, Project_Name, Project_BelongsToGroup, Project_Date, Owner_Branch, project_id) 
						  values (".GetSQLValueString($rwRep[0],'text').", ".GetSQLValueString($rwRep[1],'text').", '$FI_BelongsToGrp', now(), '".$Setup_Branch."', $project_id) ";
			$res = mysql_query($insNew, $connection);
		}
	}
} 

if($message != '')
	header("Location: ./Form_NewCostcenter.php?pagetype=$pagetype&message=$message");
else
	header("Location: ./Form_NewCostcenter.php?pagetype=$pagetype");


exit;
?>