<?php
$PageSecurity = 15;
$title = _('Department Master') ;
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmAdd']=$frmpermission[0];
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];echo "<br>";
if(!empty($_GET['id']) and $_REQUEST['act']=='del')
{
$SQL="delete from deptmaster_hierarchy where id='".$_GET['id']."'";
$ErrMsg = _('The record could not be deleted because');
$result = DB_query($SQL,$db,$ErrMsg);
prnMsg( _(' Department Code Has Deleted!'),'success');
echo "<script>location.href='index.php?route=Setup/Dept-Hierarchy&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
}

function CheckdeptCode($deptcode)
{
global $db;
$SQL ="select deptcode,deptname from deptmaster_hierarchy where deptcode ='".$deptcode."'";	
$result=DB_query($SQL,$db,$ErrMsg);
$resrow=DB_fetch_array($result);
if(isset($resrow['deptcode'])){return false;} else {return true;}
}

if(!$_POST['edit'] and $_REQUEST['act']=='edit')
{
$SQL="select * from deptmaster_hierarchy where id='".$_GET['id']."'";
$result=DB_query($SQL,$db,$ErrMsg);
$resrow=DB_fetch_array($result);
$_POST['deptcode'] = $resrow['deptcode'];
$_POST['deptname'] = $resrow['deptname'];
  $_POST['parentid'] = $resrow['parentid'];

}

if($_POST['edit'] && $_REQUEST['act'] == 'edit')
{
$SQL="update deptmaster_hierarchy set deptcode = '".$_POST['deptcode']."', deptname='".$_POST['deptname']."',parentid='".$_POST['parentid']."' ,CompanyId='".$_SESSION['ACGCompanyId']."' where id='".$_GET['id']."'";
$result = DB_query($SQL,$db,$ErrMsg);
prnMsg( _(' Department Code Updated!'),'success');
echo "<script>location.href='index.php?route=Setup/Dept-Hierarchy&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
unset($_POST);
}

if(isset($_POST['add'])  and isset($_POST['deptname']) and $_GET['act']!='edit')
{
 
	$chk_sql_dept=DB_fetch_array(DB_query("select * from deptmaster_hierarchy where deptname='".$_REQUEST['deptname']."'",$db));
	

	if($_REQUEST['deptname']!=$chk_sql_dept['deptname'])
	{
	
	//if(CheckdeptCode($_POST['deptcode']))
	//{
	 $SQL = "INSERT into deptmaster_hierarchy (id, deptcode, deptname,parentid,CompanyId) 
					 values('', '".$_POST['deptcode']."', '".$_POST['deptname']."','".$_POST['parentid']."','".$_SESSION['ACGCompanyId']."' )";	
		
		
		$InsResult=DB_query($SQL,$db);
		prnMsg( _('A New Deptmartment Code Has been Inserted!'),'success');
		echo "<script>location.href='index.php?route=Setup/Dept-Hierarchy&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
		//unset($_POST);
	//}
	//else
	//{	
	//prnMsg( _('This Department Code All Ready Exists. Please Enter A New Code!'),'error');
	//}	
	}
	else
	{
		prnMsg( _('Deptmartment Name is Allready Exists!'),'error');
	}
}

/*if(isset($_REQUEST['deptname']))
{
	  $type=$_REQUEST['deptname'];
	  $sql='select deptname from deptmaster_hierarchy where deptname="'.$type.'" order by deptname desc limit 1';
	  $result = DB_query($sql,$db);
	  if($myrow['deptname']!='')
	  {
	  while($myrow=DB_Fetch_array($result))
	  {
	  	$deptCode123=explode(" ",$myrow['deptname']);
	      for($i=0;$i<count($deptCode123);$i++)
	{
	    $prefix_type[$i]=substr($deptCode123[$i], 0, 3);
	}
	 
		$prefix_type1=implode("-",$prefix_type);	
	    $final_string=$prefix_type1."0001";	
		}
	  	
	  }
	  
else 
{
	   $deptCode123=explode(" ",$type);
	 
	   for($i=0;$i<count($deptCode123);$i++)
	    {
		
	    $prefix_type[$i]=substr($deptCode123[$i], 0, 3);
	    }
	 
		$prefix_type1=implode("-",$prefix_type);	
	    $final_string=$prefix_type1."0001";	
		
}
}*/
?>

<h2 align="center"><strong>Roles Creation </strong></h2>
<form method="post" action="">
<table width="50%" border="0" align="center">

<tr>
<th><strong>Role  Name</strong></th>
<td><input name="deptname"  type="text" id="deptname" value="<?php echo $_POST['deptname']; ?>" onblur="ShowDiv(this.value,'hid-DeptCode','Get_DeptCode.php')"   size="30"/></td>
</tr>
<tr>
<th><strong>Role  Code</strong></th>
<td>
	<div id="hid-DeptCode">
	<input name="deptcode"  type="text" id="deptcode" value="<?php echo $_POST['deptcode']; ?>" size="30"/>
	</div></td>
</tr>


 <tr>
    <th><strong>Under Role </strong> </th>
    <td><?php //  $AccountMaster->AccountGroup($MainGroupId,'MainGroupId'); 
	
 	$pcode= $MainGroupId;
 	function CategoryShow($pcode, $PcateID, $dashVal)
	{
	    global $db;
		$dashVal = $dashVal . "-";
	    $sqlCate = "select * from deptmaster_hierarchy  where  parentid = '".$PcateID."'";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
				$sqlCateSel = "select * from deptmaster_hierarchy where id  =".$rowCate['id']." ";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['id']==$pcode)
				 $px="selected";
 		?>
 				<option value="<?php echo $rowCate['id'];?>" <?php echo $px ?>><?php echo $dashVal;?>
				 <?php echo $rowCate['deptname'];?> </option>
	   <?php			
				   CategoryShow($pcode,$rowCate['id'], $dashVal);
			}
		}
	} 
	$pcode=$_POST['parentid']  ;
   ?>  
   <select name="parentid" id="parentid">	  
  <option value="000">----</option>
  <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	$sqlCate = "select * from  deptmaster_hierarchy  where    parentid   = 0 and CompanyId='".$_SESSION['ACGCompanyId']."'";
	$rsCate  = DB_query($sqlCate,$db);
	if(DB_num_rows($rsCate)>0)
	{
		while($rowCate =DB_fetch_array($rsCate))
		{
 				$sqlCateSel = "select * from deptmaster_hierarchy where parentid   =".$rowCate['id']." ";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['id']==$pcode)
				 $px="selected";
			echo $dashVal;	
		  ?>
		<option value="<?php echo $rowCate['id'];?>"<?php echo $px ?>> 
		<?php echo  $rowCate['deptname'] ;?></option>
 		<?php			
			 CategoryShow($pcode, $rowCate['id'], $dashVal);
		}
	}
?></select>	</td>
  </tr>
 <tr align="center">
 <td></td>
    <td  > 
    <?php
if($_GET['act']=='edit'){?>
<input type="submit" name="edit"  value="Edit" />
<input type="hidden" name='uiqeid' value="<?php echo $_GET['id']; ?>" />
<?php }
?>
<input type="submit" name="add"  value="Submit" />
 <?php  ?><input type="submit" name="cancel"  value="Cancel" /> </td>
 </tr>
</table>

</form>
<table width="80%" border="0" align="center">
<tr>

<th width="47%"><strong>Role Name</strong></th>
<th width="38%"><strong>Role Code</strong></th>
<th width="38%"><strong>Under Role</strong></th>

<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
<?php
$SQL ="select * from deptmaster_hierarchy where CompanyId='".$_SESSION['ACGCompanyId']."'";	
$result=DB_query($SQL,$db,$ErrMsg); 
while($resrow=DB_fetch_array($result)){?>
<tr>
	<td><?php echo $resrow['deptname']; ?></td>
<td><?php echo $resrow['deptcode']; ?></a></td>
  <td width="30%"><?php 
	   
	    $parent= $s->getField('deptmaster_hierarchy','id',$resrow['parentid'],'deptname');	 
	  if($parent!='')
	  echo $parent;
	  else{
	  echo "--Root--";
	  }
 	   ?></td>
<td>
 <a href="index.php?route=Setup/Dept-Hierarchy&amp;modid=<?php echo $_REQUEST['modid'];?>&amp;frmid=<?php echo $_REQUEST['frmid'];?>&amp;act=edit&amp;id=<?php echo $resrow['id']; ?>" class="link">Edit</a> 
</td>
<td>
 
<a href="index.php?route=Setup/Dept-Hierarchy&amp;modid=<?php echo $_REQUEST['modid'];?>&amp;frmid=<?php echo $_REQUEST['frmid'];?>&amp;act=del&amp;id=<?php echo $resrow['id']; ?>" onclick="return confirm('Are you sure you want to delete this record?')" class="link">Delete</a> </td>
</tr>
<?php }?>
</table>
 <style type="text/css" >
.button{ background-color:#666666;
color: #FFFFFF;
height:25px;
margin:0px 5px 0px 5px;
border:1px solid #359CA2;
}
.link{ color:#9900CC; font-family:"Comic Sans MS"; letter-spacing:1px
}
</style>