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

//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, ifnull(Module_TicketSupport,0) as Module_TicketSupport 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, ifnull(project_ticket,0) as project_ticket, ifnull(alwaysClearPage,0) as alwaysClearPage from setup";
$result= mysql_query($query_setup,$connection) or die( mysql_error());
while($rowsetup=mysql_fetch_array($result)){
	extract($rowsetup);
}

if($alwaysClearPage == 1) $pageclear=1;
else $pageclear =0;

$HideMenu    	= $_POST["HideMenu"]; 				if ($HideMenu == ""){			$HideMenu 			= $_GET["HideMenu"];}   
$mode   	 	= $_GET["mode"]; 					if ($mode == ""){				$mode 				= $_POST["mode"];}   
$modedt   	 	= $_GET["modedt"]; 					if ($modedt == ""){				$modedt 			= $_POST["modedt"];}   
$message 	 	= $_GET["message"]; 				if ($message == ""){			$message 			= $_POST["message"];}   
$ProjectId  	= $_GET["ProjectId"]; 				if ($ProjectId == ""){			$ProjectId 			= $_POST["ProjectId"];} 

$txtKeyword  	= $_POST["txtKeyword"]; 			if ($txtKeyword == ""){			$txtKeyword 		= $_GET["txtKeyword"];}   
$perpage 	 	= $_POST["perpage"]; 				if ($perpage == ""){			$perpage 			= $_GET["perpage"];} 
$startrange	 	= $_POST["startrange"]; 			if ($startrange == ""){			$startrange 		= $_GET["startrange"];} 

$Project_Id	    	= $_POST["Project_Id"]; 		if ($Project_Id == ""){			$Project_Id 		= $_GET["Project_Id"];} 
$creation_date		= $_POST["creation_date"]; 		if ($creation_date == ""){		$creation_date 		= $_GET["creation_date"];} 
$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"];} 
$parent_code 		= $_POST["parent_code"]; 		if ($parent_code == ""){		$parent_code 		= $_GET["parent_code"];} 
$project_active		= $_POST["project_active"]; 	if ($project_active == ""){		$project_active 	= $_GET["project_active"];}
$txtCostCentCode	= $_POST["txtCostCentCode"]; 	if ($txtCostCentCode == ""){	$txtCostCentCode 	= $_GET["txtCostCentCode"];} 

$ProjectDt_Id	    		= $_POST["ProjectDt_Id"]; 				if ($ProjectDt_Id == ""){				$ProjectDt_Id 			 = $_GET["ProjectDt_Id"];} 
$ProjectDt_Name				= $_POST["ProjectDt_Name"]; 			if ($ProjectDt_Name == ""){				$ProjectDt_Name 		 = $_GET["ProjectDt_Name"];} 
$ProjectDt_NumberofSeats  	= $_POST["ProjectDt_NumberofSeats"]; 	if ($ProjectDt_NumberofSeats == ""){	$ProjectDt_NumberofSeats = $_GET["ProjectDt_NumberofSeats"];} 
$ProjectDt_View 			= $_POST["ProjectDt_View"]; 			if ($ProjectDt_View == ""){				$ProjectDt_View 		 = $_GET["ProjectDt_View"];} 
$ProjectDt_Floor 			= $_POST["ProjectDt_Floor"]; 			if ($ProjectDt_Floor == ""){			$ProjectDt_Floor 		 = $_GET["ProjectDt_Floor"];} 
$ProjectDt_Smoking			= $_POST["ProjectDt_Smoking"]; 			if ($ProjectDt_Smoking == ""){			$ProjectDt_Smoking 		 = $_GET["ProjectDt_Smoking"];} 

$project_email 		= $_POST["project_email"]; if ($project_email == ""){$project_email = $_GET["project_email"];} 
$project_clientcode 		= $_POST["project_clientcode"]; if ($project_clientcode == ""){$project_clientcode = $_GET["project_clientcode"];} 
$project_client 		= $_POST["project_client"]; if ($project_client == ""){$project_client = $_GET["project_client"];} 
$project_job 		= $_POST["project_job"]; if ($project_job == ""){$project_job = $_GET["project_job"];} 
$project_position 		= $_POST["project_position"]; if ($project_position == ""){$project_position = $_GET["project_position"];} 

$thickness_id 		= $_POST["thickness_id"];	 	if ($thickness_id == ""){		$thickness_id 		= $_GET["thickness_id"];} 
$width_id 			= $_POST["width_id"]; 			if ($width_id == ""){			$width_id 			= $_GET["width_id"];} 
$gross_weight 		= $_POST["gross_weight"]; 		if ($gross_weight == ""){		$gross_weight 		= $_GET["gross_weight"];} 
$net_weight 		= $_POST["net_weight"]; 		if ($net_weight == ""){			$net_weight 		= $_GET["net_weight"];} 
$project_blocked 	= $_POST["project_blocked"];	if ($project_blocked == ""){	$project_blocked 	= $_GET["project_blocked"];} 
$pStartDate 		= $_POST["pStartDate"];			if ($pStartDate == ""){			$pStartDate 		= $_GET["pStartDate"];} 
$pPreventDate 		= $_POST["pPreventDate"];		if ($pPreventDate == ""){		$pPreventDate 		= $_GET["pPreventDate"];} 
$pNbrVisit 			= $_POST["pNbrVisit"];			if ($pNbrVisit == ""){			$pNbrVisit 			= $_GET["pNbrVisit"];} 
$pNbrMonth 			= $_POST["pNbrMonth"];			if ($pNbrMonth == ""){			$pNbrMonth 			= $_GET["pNbrMonth"];} 

$pState 			= $_POST["pState"];			if ($pState == ""){			$pState 		= $_GET["pState"];} 
$pLga 				= $_POST["pLga"];			if ($pLga == ""){			$pLga 			= $_GET["pLga"];} 
$pRegion 			= $_POST["pRegion"];		if ($pRegion == ""){		$pRegion 		= $_GET["pRegion"];} 
$pAddress 			= $_POST["pAddress"];		if ($pAddress == ""){		$pAddress 		= $_GET["pAddress"];} 
$pBranchMan 		= $_POST["pBranchMan"];		if ($pBranchMan == ""){		$pBranchMan 	= $_GET["pBranchMan"];} 
$pManagerPhone 		= $_POST["pManagerPhone"];	if ($pManagerPhone == ""){	$pManagerPhone 	= $_GET["pManagerPhone"];} 
$pInfoType 			= $_POST["pInfoType"];		if ($pInfoType == ""){		$pInfoType 		= $_GET["pInfoType"];} 
$pInfoCounter 		= $_POST["pInfoCounter"];	if ($pInfoCounter == ""){	$pInfoCounter 	= $_GET["pInfoCounter"];} 

$pIP 				= $_POST["pIP"];			if ($pIP == ""){			$pIP 			= $_GET["pIP"];} 
$pSubnet 			= $_POST["pSubnet"];		if ($pSubnet == ""){		$pSubnet 		= $_GET["pSubnet"];} 
$pGateway 			= $_POST["pGateway"];		if ($pGateway == ""){		$pGateway 		= $_GET["pGateway"];} 



$project_code = ereg_replace("'", "\'", $project_code);
$project_name = ereg_replace("'", "\'", $project_name);
$parent_code = ereg_replace("'", "\'", $parent_code);

if($txtfromMNo == '') $txtfromMNo = "NULL";
else $txtfromMNo = "'".$txtfromMNo."'";

if($txtBaNo == '') $txtBaNo = "NULL";
else $txtBaNo = "'".$txtBaNo."'";

if($txtBankName == '') $txtBankName = "NULL";
else $txtBankName = "'".$txtBankName."'";

if($txtLcNo == '') $txtLcNo = "NULL";
else $txtLcNo = "'".$txtLcNo."'";

if($project_email == '') $project_email = "NULL";
else $project_email = "'".$project_email."'";

if($project_clientcode == '') $project_clientcode = "NULL";
else $project_clientcode = "'".$project_clientcode."'";

if($project_job == '') $project_job = "NULL";
else $project_job = "'".$project_job."'";

if($project_position == '') $project_position = "NULL";
else $project_position = "'".$project_position."'";

if($thickness_id == '') $thickness_id = "NULL";
else $thickness_id = "'".$thickness_id."'";

if($width_id == '') $width_id = "NULL";
else $width_id = "'".$width_id."'";

if($gross_weight == '') $gross_weight = "NULL";
else $gross_weight = "'".$gross_weight."'";

if($net_weight == '') $net_weight = "NULL";
else $net_weight = "'".$net_weight."'";

if ($project_blocked == '') $project_blocked = 0;
if ($pNbrVisit == '') 		$pNbrVisit = 0;
if ($pNbrMonth == '') 		$pNbrMonth = 0;


$perpage=$_POST['perpage'];

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

}	
else {$creation_date= 'null';}

$project_code=trim($project_code);

if ($mode=='' && $modedt =='setChild'){
	$project_code = $_GET['project_code'];
	$proj_id = $_GET['proj_id'];

	echo "[BRK]";
	docalc($project_code, $proj_id, $connection);
	echo "[BRK]";
	exit;
}

if($actionmode=='Get_ItemCode'){
		
	$project_client=$_GET["project_client"];
	$project_job=$_GET["project_job"];
	$project_position=$_GET["project_position"];

	if($parent==''){
	//$item_code = Get_AutoItemCode($subgroup_code,$group_code,$category_code, $remaininglength,$connection);
	$client = explode(' ',$project_client);
	$clientlen = sizeof($client);
	$i=0;
	while($i<$clientlen){
		$cli.= substr($client[$i], 0, 1);
		$i++;
	}

	$job = substr($project_job, 0, 3);

	if($project_position !='month')
	$pos = substr($project_position, 0, 3);
	else{
		$querymonth = "select month(now())";
		$resultmonth = mysql_query($querymonth,$connection);
		$rowmonth = mysql_fetch_array($resultmonth);
		$pos = getMonth($rowmonth[0]);
	}

	$project_codee = $cli.'-'.$job.'/'.$pos;
	}
	else{
		$project_codee = $parent;
	}
	$querycount = "select project_code from project where project_code like '".$project_codee."%'" ;
	$rescount = mysql_query($querycount,$connection);
	while($rowcount = mysql_fetch_array($rescount)){
		$len = strlen($project_codee);
		$code=substr($rowcount[0],$len+1);
		if($numberproj == ''){
			$numberproj = $code;
		}
		else if($numberproj<$code){
			$numberproj = $code;
		}

		// echo"<br>numberproj=".$numberproj;

	}

	$num=$numberproj+1;
if($parent=='')
	$project_codee.=  ' '.$num;
	else
	$project_codee.=  '.'.$num;


	echo $project_codee;
	//if ($item_descriptionf1!='' && $item_descriptionf2=='' && $Owner_Branch=='' ) 
	//echo $item_code."-".substr($item_descriptionf1, 0, 4);
	
	//if ($item_descriptionf1!='' && $item_descriptionf2!='' && $Owner_Branch=='' ) 
	//echo $item_code."-".substr($item_descriptionf1, 0, 4)."-".substr($item_descriptionf2, 0, 2);
	
	//if ($item_descriptionf1!='' && $item_descriptionf2=='' && $Owner_Branch!='' ) 
	//echo $item_code."-".substr($item_descriptionf1, 0, 4)."-".substr($Owner_Branch, 0, 2);
	
	//if ($item_descriptionf1!='' && $item_descriptionf2!='' && $Owner_Branch!='' ) 
	//echo $item_code."-".substr($item_descriptionf1, 0, 4)."-".substr($item_descriptionf2, 0, 2)."-".substr($Owner_Branch, 0, 2);
	
	
	//if ($item_descriptionf2!='' && $item_descriptionf1=='' && $Owner_Branch=='' ) 
	//echo $item_code."-".substr($item_descriptionf2, 0, 2);
	
	//if ($item_descriptionf2!='' && $item_descriptionf1!='' && $Owner_Branch=='' ) 
	//echo $item_code."-".substr($item_descriptionf1, 0, 4)."-".substr($item_descriptionf2, 0, 2);
	
	//if ($item_descriptionf2!='' && $item_descriptionf1=='' && $Owner_Branch!='' ) 
	//echo $item_code."-".substr($item_descriptionf2, 0, 2)."-".substr($Owner_Branch, 0, 2);
	
	
	
	
	//echo $item_code;
	exit;
	}

if ($mode=='' && $modedt =='saveval'){
	$project_code = $_GET['project_code'];
	$proj_id = $_GET['proj_id'];
	$proj_percentage = $_GET['proj_percentage'];
	$proj_number = $_GET['proj_number'];

	$queryup = "update project set project_percentage = ".GetSQLValueString($proj_percentage,'double').", 
				project_number = ".GetSQLValueString($proj_number,'double')." where project_code = '$proj_id' ";
	$resup = mysql_query($queryup, $connection);
	echo "[BRK]";
	docalc($project_code, $proj_id, $connection);
	echo "[BRK]";
	exit;
}
if ($mode=='' && $modedt =='addMac'){
	$macAddress 	= $_GET["macAddress"];
	$mac_id		 	= $_GET["mac_id"];
	$mactype	 	= $_GET["mactype"];
	$project_code 	= $_GET["project_code"];
	
	if ($mac_id == ''){
		$query = "insert into project_macAddress (project_code, mac_address, mac_type) values ('$project_code', '$macAddress', '$mactype') ";
		$result = mysql_query($query, $connection);
	}
	if ($mac_id != ''){
		$query = "update project_macAddress set project_code = '$project_code', mac_address = '$macAddress', mac_type = '$mactype' where mac_id = $mac_id ";
		$result = mysql_query($query, $connection);
	}
	
	echo "[BRK]";
	showMacAddress($project_code,$mactype,$connection);
	echo "[BRK]";
	
	exit;	
}

if ($mode=='' && $modedt =='addMac'){
	$mac_id		 	= $_GET["mac_id"];
	$mactype	 	= $_GET["mactype"];
	$project_code 	= $_GET["project_code"];
	
	if ($mac_id != ''){
		$query = "delete from project_macAddress where mac_id = $mac_id ";
		$result = mysql_query($query,$connection);
	}
	echo "[BRK]";
	showMacAddress($project_code,$mactype,$connection);
	echo "[BRK]";
	exit;	
}

if($mode=='insert' && $modedt !='deletedt'){
	fbegin();
	$Project_Id = get_nextsequence('Project_Id','',$connection);
	fcommit();
		
		
	$query="Insert into project (project_id, project_code,project_name,project_date, Project_BelongsToGroup,Project_Deactivate,Parent_Code, Owner_Branch, project_email,FormM_No,BA_No,Bank_Name,
			LC_No, thickness_id, width_id, gross_weight, net_weight, project_blocked, distribution_code, project_startingDate, project_preventifDate, project_nbrVisits, project_periodMonth,
			Project_state, Project_LGA, Project_region, project_address, project_BranchManager, project_BranchManagerPhone, project_infoType, project_counters,
			project_IP, project_subnet, project_gateWay,project_client,project_job,project_position) 
			values ('".$Project_Id."','".$project_code."','".$project_name."',$creation_date, $FI_BelongsToGrp,$project_active,'".$parent_code."', '".$Setup_Branch."', $project_email, $txtfromMNo,
			$txtBaNo,$txtBankName, $txtLcNo, $thickness_id, $width_id, $gross_weight, $net_weight, $project_blocked, '".$txtCostCentCode."', ".formatdate($pStartDate).", ".formatdate($pPreventDate).", $pNbrVisit, $pNbrMonth,
			".GetSQLValueString($pState,'text').", ".GetSQLValueString($pLga,'text').", ".GetSQLValueString($pRegion,'text').", ".GetSQLValueString($pAddress,'text').", ".GetSQLValueString($pBranchMan,'text').",
			".GetSQLValueString($pManagerPhone,'text').", ".GetSQLValueString($pInfoType,'text').", ".GetSQLValueString($pInfoCounter,'double').",
			".GetSQLValueString($pIP,'text').",".GetSQLValueString($pSubnet,'text').",".GetSQLValueString($pGateway,'text').",$project_clientcode,$project_job,$project_position)";		
	//echo $query;		//exit;		
	$result  = mysql_query($query, $connection);
	//$Project_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 project_id from project where  project_code='$project_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']."', 'project', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	if ($result) {
		if ($project_ticket == 1 && $Module_TicketSupport == 1){
			$queryins = "insert into project_items(project_code, item_code) 
						select '$project_code', item_code from items limit 10";
			$resins = mysql_query($queryins, $connection);			
		}
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'I','PROJECT','',$project_code,$Setup_Branch);
		}
	}
}

if($mode=='edit' && $modedt !='deletedt'){

	$query="select project_code  from project where Project_Id='".$Project_Id."'";
	$result = mysql_query($query, $connection);
	$row=mysql_fetch_row($result );
	$project_codePrv=$row[0];

	//echo$project_codePrv.'  '.$project_code;exit;
	
	$query_update="update  project set project_code='".$project_code."',
					project_name='".$project_name."',
					project_Date=$creation_date,
					Project_Deactivate=$project_active,
					Parent_Code='".$parent_code."', 
					project_email=$project_email,
					project_client=$project_clientcode,project_job=$project_job,project_position=$project_position,
					FormM_No = $txtfromMNo,
					BA_No = $txtBaNo,
					Bank_Name = $txtBankName,
					LC_No = $txtLcNo,
					thickness_id = $thickness_id, 
					width_id = $width_id,
					gross_weight = $gross_weight, 
					net_weight = $net_weight, 
					project_blocked = $project_blocked,
					distribution_code = ".GetSQLValueString($txtCostCentCode,'text').",
					project_startingDate = ".formatdate($pStartDate).", 
					project_preventifDate = ".formatdate($pPreventDate).", 
					project_nbrVisits = $pNbrVisit, 
					project_periodMonth = $pNbrMonth,
					
					Project_state = ".GetSQLValueString($pState,'text').",
					Project_LGA = ".GetSQLValueString($pLga,'text').",
					Project_region = ".GetSQLValueString($pRegion,'text').",
					project_address = ".GetSQLValueString($pAddress,'text').",
					project_BranchManager = ".GetSQLValueString($pBranchMan,'text').",
					project_BranchManagerPhone = ".GetSQLValueString($pManagerPhone,'text').",
					project_infoType = ".GetSQLValueString($pInfoType,'text').",
					project_counters = ".GetSQLValueString($pInfoCounter,'double').",
					project_IP = ".GetSQLValueString($pIP,'text').",
					project_subnet = ".GetSQLValueString($pSubnet,'text').",
					project_gateWay = ".GetSQLValueString($pGateway,'text')."
					where Project_Id='".$Project_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 project_id from project where  project_code='$project_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']."', 'project', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	if($resultedit && $project_codePrv!=$project_code){
		$query="update project set parent_code='".$project_code."' where parent_code='".$project_codePrv."'";
		//echo$query;exit;
		$result = mysql_query($query, $connection);
	
	}
	
	if ($resultedit) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','PROJECT','',$project_code,$Setup_Branch);
		}
	}
}

if (($mode=='insert' || $mode=='edit') && $modedt !='deletedt' && $ProjectDt_Name != '') {
	if ($ProjectDt_Id == '')
	{
		$QueryExist  = " Select Count(*) From ProjectDt Where ProjectDt_Name = '".$ProjectDt_Name."'";
		//echo $QueryExist;exit;
		$ResultExist = mysql_query($QueryExist, $connection);
		$RowExist = mysql_fetch_array($ResultExist);
		$vExists = $RowExist[0];
	
		if ($vExists == 0) {
		
			$querydt=" Insert into ProjectDt (ProjectDt_Name, ProjectDt_NumberofSeats, ProjectDt_View, ProjectDt_Floor, ProjectDt_Smoking, Project_Id) ".
					 " values ('".$ProjectDt_Name."', '".$ProjectDt_NumberofSeats."', '".$ProjectDt_View."', '".$ProjectDt_Floor."', '".$ProjectDt_Smoking."','".$Project_Id."' )";
			//echo  $querydt; exit;			
			$result  = mysql_query($querydt, $connection);
			$message = mysql_error();		 
			
		}
		else {
			$message = 'Duplicate Entry ...';		 
		}	
	} else {
		$querydt=" Update ProjectDt Set ".
				 " ProjectDt_Name = '".$ProjectDt_Name."', ".
				 " ProjectDt_NumberofSeats = '".$ProjectDt_NumberofSeats."', ".
				 " ProjectDt_View = '".$ProjectDt_View."', ".
				 " ProjectDt_Floor = '".$ProjectDt_Floor."', ".
				 " ProjectDt_Smoking = '".$ProjectDt_Smoking."' ".
				 " Where ProjectDt_Id= '".$ProjectDt_Id."' ";

		$result  = mysql_query($querydt, $connection);
		$message = mysql_error();
//echo $querydt;exit;		
		
	}
		
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','PROJECT','',$project_code,$Setup_Branch);
		}
	}
}
	
if($mode=='delete')
{		
	$query="select project_code  from project where Project_Id='".$ProjectId."'";
	$result = mysql_query($query, $connection);
	$row=mysql_fetch_row($result );
	$project_codePrv=$row[0];
	 if($config['log_file'] == 1){
		$transType = "D";
		$company = explode("_", $_SESSION['dbName']);
		$currCompany = strtoupper($company[0]);	
		$queryid="select project_id from project where  project_code='$project_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']."', 'project', '$currCompany')";
		//echo $query_insertToHistory;exit;
		$result_insertToHistory = mysql_query($query_insertToHistory, $connection);
	}
	$query="delete from project where Project_Id='".$ProjectId."'";
	//echo$query;exit;
   	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	$errornum= mysql_errno();
						
	if($errornum=='1451') {
		$message="Delete Not Allowed . Project is used .";
		//echo $message;
	}
	else{
		if($result){
			$del2 = "delete from warehouseProject where project_code = '".$project_codePrv."' ";
			$res2 = mysql_query($del2, $connection);
			
			$query="update project set parent_code=null where parent_code='".$project_codePrv."'";
			//echo$query;exit;
			$result = mysql_query($query, $connection);
			
			if ($Module_Synchro == 1) {
				InsertSynchroLoc($connection,'D','PROJECT','',$project_codePrv,$Setup_Branch);
			}
		}
	
	}
}

if($modedt=='deletedt' && $ProjectDt_Id !='')
{
	$query = "select project_code from project inner join projectdt on project.project_id = projectdt.project_id where ProjectDt_Id='".$ProjectDt_Id."'";
	$result=mysql_query($query,$connection);
	$rows=mysql_fetch_row($result);
	$project_code=$rows[0];
	
	$query="delete from ProjectDt where ProjectDt_Id='".$ProjectDt_Id."'";
	//echo$query;exit;
	$result  = mysql_query($query, $connection);
	$message = mysql_error();
	
	if ($result) {
		if ($Module_Synchro == 1) {
			InsertSynchroLoc($connection,'U','PROJECT','',$project_code,$Setup_Branch);
		}
	}
}

$warehousecode 	= $_GET["warehousecode"];
$wp_id 			= $_GET["wp_id"];	


if ($modedt == 'insertWarehouse'){
	$chkquery = " select 1 from warehouseProject where project_code = '".$project_code."' and warehouse_code = '".$warehousecode."' ";
	$chkres = mysql_query($chkquery, $connection);
	$numchk = mysql_num_rows($chkres);
	
	if ($numchk == 0){
		$queryins = " insert into warehouseProject (project_code, warehouse_code) values ('".$project_code."', '".$warehousecode."') ";
		$resins = mysql_query($queryins, $connection);
	} else {
		$msg = "Warehouse Code $warehousecode Already Exists for $project_code!";
	}
	if ($mode=='insert'){
		echo "[BRK]";
		echo "";
		echo "[BRK]";
		echo $Project_Id;
	} else {
		echo "[BRK]";
		echo $msg;
		echo "[BRK]";
		drawProjectWarehouse($project_code, $connection);
	}
	exit;
}

if ($modedt == 'updateWarehouse' && $wp_id != ''){
	$chkquery = " select 1 from warehouseProject where project_code = '".$project_code."' and warehouse_code = '".$warehousecode."' and wp_id <> '$wp_id' ";
	$chkres = mysql_query($chkquery, $connection);
	$numchk = mysql_num_rows($chkres);
	
	if ($numchk == 0){
		$queryins = " update warehouseProject set project_code = '".$project_code."', warehouse_code = '".$warehousecode."' where  wp_id = '$wp_id' ";
		$resins = mysql_query($queryins, $connection);
	}
	else {
		$msg = "Warehouse Code $warehousecode Already Exists for $project_code!";
	}
	echo "[BRK]";
	echo $msg;
	echo "[BRK]";
	drawProjectWarehouse($project_code, $connection);
	exit;
}

if ($modedt == 'removeWarehouse' && $wp_id != ''){
	$query = "delete from warehouseProject where wp_id = '$wp_id' ";
	$res = mysql_query($query, $connection);
	$msg = mysql_error($res);
	echo "[BRK]";
	echo $msg;
	echo "[BRK]";
	drawProjectWarehouse($project_code, $connection);
	exit;	
}

if ($modedt == 'addItem'){
	$project_code 	= $_GET['project_code'];
	$item_id 		= $_GET['item_id'];
	$itemCode 		= $_GET['itemCode'];
	$itemQty 		= $_GET['itemQty'];
	
	$ErrorExist = 0;
	
	$queryItem 	= "select 1 from items where item_code = '".ereg_replace("'", "\'", trim($itemCode))."' ";
	$resItem 	= mysql_query($queryItem, $connection);
	$numRow 	= mysql_num_rows($resItem);
	if ($numRow == 0) {
		$ErrorExist = 1;
		$message = "Insert not Allowed! Check for Item Code!";
	}
	
	// Insert Item Code
	if ($item_id == '' && $ErrorExist == 0){
		$queryChk = "select 1 from project_items where project_code = '$project_code' and item_code = '".ereg_replace("'", "\'", trim($itemCode))."' ";
		$resChk 	= mysql_query($queryChk, $connection);
		$numRow 	= mysql_num_rows($resChk);
		if ($numRow == 0) {
			$queryins = "insert into project_items (project_code, item_code, projetItems_qty) 
						values
						('$project_code',  '".ereg_replace("'", "\'", trim($itemCode))."', '$itemQty')";
			$resins = mysql_query($queryins, $connection);			
		} else {
			$ErrorExist = 1;
			$message = "Insert not Allowed! Item Already Exists!";
		}
	}
	
	// Update Item Code
	else if ($item_id != '' && $ErrorExist == 0){
		$queryChk = "select 1 from project_items where project_code = '$project_code' and item_code = '".ereg_replace("'", "\'", trim($itemCode))."' and projetItems_id <> $item_id  ";
		$resChk 	= mysql_query($queryChk, $connection);
		$numRow 	= mysql_num_rows($resChk);
		if ($numRow == 0) {
			$queryup = "update project_items set 
							project_code = '$project_code', item_code = '".ereg_replace("'", "\'", trim($itemCode))."', 
							projetItems_qty = '$itemQty'
							where projetItems_id = $item_id ";
			$resup = mysql_query($queryup, $connection);			
		} else {
			$ErrorExist = 1;
			$message = "Insert not Allowed! Item Already Exists!";
		}
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	draw_ProjectItems($project_code, '', $connection);
	exit;
}

if ($modedt == 'deleteItem'){
	$project_code 	= $_GET['project_code'];
	$item_id 		= $_GET['item_id'];
	if ($item_id != ''){
		$query = "delete from project_items where projetItems_id = $item_id ";
		$res = mysql_query($query, $connection);	
		$message = mysql_error();
	}
	echo "[BRK]";
	echo $message;
	echo "[BRK]";
	draw_ProjectItems($project_code, '', $connection);
	exit;
}

if ($modedt == 'addSerial'){
	$serialid 		= $_GET['serialid'];
	$projectItemId 	= $_GET['projectItemId'];
	$serialNbr 		= $_GET['serialNbr'];
	
	if ($serialid == ''){
		$queryins = "insert into projectItems_Serial (serial_number, projectItem_id) values ('$serialNbr','$projectItemId') ";
		$resins = mysql_query($queryins, $connection);
	} 
	else {
		$queryup = "update projectItems_Serial set serial_number = '$serialNbr', projectItem_id = $projectItemId
					where serial_id = $serialid ";
		$resup = mysql_query($queryup, $connection);		
	}
	echo "[BRK]";
	serialNumberGrid ('', $projectItemId,$connection);
	exit;
	
}
if ($modedt == 'deleteSerial'){
	$serialid 		= $_GET['serialid'];
	$projectItemId 	= $_GET['projectItemId'];
	$querydel = "delete from projectItems_Serial where serial_id = $serialid ";
	$resdel = mysql_query($querydel, $connection);	
	echo "[BRK]";
	serialNumberGrid ('', $projectItemId,$connection);
	exit;
}
$message = ereg_replace(' ','%20',$message);


if($mode=='delete')
	header ("Location: ./Form_Project.php?mode=insert&HideMenu=".$HideMenu."&message=".$message."&perpage=".$perpage."&pageclear=".$pageclear);
else 
	header ("Location: ./Form_Project.php?Project_Id=$Project_Id&project_code=$project_code&project_name=$project_name&mode=editmode&HideMenu=".$HideMenu."&message=".$message."&perpage=".$perpage."&pageclear=".$pageclear);

mysql_close($connection);


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

function getMonth($month){
	return date('M', mktime(0,0,0,$month,1));
  }

function formatdate($dateval){
	if($dateval != ''){
	$ndate = explode("/", $dateval);
	$dateval = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$dateval = "'".$dateval."'";
	} else $dateval = "NULL";
	return $dateval;
}
print <<<HERE
</form>
</body>

HERE;


?>
		           