<?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/NDepartmentHierarchy&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['mst_deptid'] = $resrow['mst_deptid'];

  $_POST['parentid'] = $resrow['parentid'];
    $_POST['companyid'] = $resrow['CompanyId'];


}

if($_POST['edit'] && $_REQUEST['act'] == 'edit')
{
$SQL="update deptmaster_hierarchy set deptcode = '".$_POST['deptcode']."', deptname='".$_POST['deptname']."',parentid='".$_POST['parentid']."' ,CompanyId='".$_POST['companyid']."' ,mst_deptid='".$_POST['mst_deptid']."' where id='".$_GET['id']."'";
$result = DB_query($SQL,$db,$ErrMsg);
prnMsg( _(' Department Code Updated!'),'success');
echo "<script>location.href='index.php?route=Setup/NDepartmentHierarchy&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,mst_deptid) 
					 values('', '".$_POST['deptcode']."', '".$_POST['deptname']."','".$_POST['parentid']."','".$_POST['companyid']."' ,'".$_POST['mst_deptid']."' )";	
		
		
		$InsResult=DB_query($SQL,$db);
		prnMsg( _('A New Deptmartment Code Has been Inserted!'),'success');
		echo "<script>location.href='index.php?route=Setup/NDepartmentHierarchy&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";	
		
}
}*/
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<h2 align="center"><strong>Organizational Chart  </strong></h2>
<form method="post" action="">
<table width="100%" border="0" align="center">
<tr class="row1">
<td class="tabledata"><strong>Company</strong></td>
<td><select name="companyid" id='companyid' onchange="showdept(this.value)" style="width:100%; height:100%" >
        <option value="0">--</option>
 		<?php 
		  $query=DB_query("select * from companies where 1=1",$db);
		  while($row=DB_fetch_array($query))
		  {
 		  	$p='';
			if($row['CompanyId']==$_POST['companyid'])
			{
			   $p="selected";
			}
 		 ?>
		<option value="<?php echo $row['CompanyId'] ?>" <?php echo $p; ?>><?php echo $row['CompanyName'] ?></option>
		<?php } ?>
		</select>
		</td>



<td class="tabledata"><strong>Dept</strong></td>
<td>
<select name="mst_deptid" id="mst_deptid" style="width:100%; height:100%">
  <option>Select Department</option>
  <?php $sql_dept=DB_query("select * from deptmaster  where CompanyId ='".$_SESSION['ACGCompanyId']."'",$db);
	while($dept_row=DB_fetch_array($sql_dept))	
	{	
	$s='';
	if($dept_row['id']==$_POST['mst_deptid'])
	{
		$s='selected';
	}
?>
  <option value="<?php echo $dept_row['id']; ?>" <?php echo $s;?>>
  <?php echo $dept_row['deptname']; ?>
  </option>
  <?php } ?>
</select></td>


<td class="tabledata"><strong>Role  Name</strong></td>
<td><input name="deptname"  type="text" id="deptname" value="<?php echo $_POST['deptname']; ?>" onblur="ShowDiv(this.value,'hid-DeptCode','Get_DeptCode.php')" style="width:100%; height:100%"/></td>

<td class="tabledata"><strong>Role  Code</strong></td>
<td>
	<div id="hid-DeptCode">
	<input name="deptcode"  type="text" id="deptcode" value="<?php echo $_POST['deptcode']; ?>" style="width:100%; height:100%"/>
	</div></td>




    <td class="tabledata"><strong>Under Role </strong> </td>
    <td>
	<div  id="dptcode">
	<?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" style="width:100%; height:100%">	  
  <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>

</div>
	</td>
  </tr>
</table>
<p align="center"> <?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" /> </p>
</form>
<table width="100%" border="0" align="center">
<tr class="row1">

<td width="25%" class="tabledata"><strong>Role Name</strong></td>
<td width="16%" class="tabledata"><strong>Role Code</strong></th>
<td width="19%" class="tabledata"><strong>Under Role</strong></td>
<td width="33%" class="tabledata"><strong>Department</strong></td>
<td width="3%" class="tabledata"><strong>Edit</strong></td>
<td width="4%" class="tabledata"><strong>Delete</strong></td>
</tr>
<?php $s=new db_query();
$SQL ="select * from deptmaster_hierarchy where CompanyId='".$_SESSION['ACGCompanyId']."' ORDER BY mst_deptid, parentid";	
$result=DB_query($SQL,$db,$ErrMsg); 
while($resrow=DB_fetch_array($result)){?>
<tr class="row1">
	<td><?php echo $resrow['deptname']; ?></td>
<td><?php echo $resrow['deptcode']; ?></a></td>
  <td width="19%"><?php 
	   
	    $parent= $s->getField('deptmaster_hierarchy','id',$resrow['parentid'],'deptname');	 
	  if($parent!='')
	  echo $parent;
	  else{
	  echo "--Root--";
	  }
 	   ?></td>
	   <td><?php echo $s->GetFieldValue('deptmaster',"id='".$resrow['mst_deptid']."'",'deptname'); ?></td>
<td>
 <a href="index.php?route=Setup/NDepartmentHierarchy&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/NDepartmentHierarchy&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>

<script>
function  showdept(str)
{
if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	
	var data=xmlhttp.responseText.split("__");
	document.getElementById("mst_deptid").innerHTML=data[0];
	//document.getElementById("dptcode").innerHTML=data[1];
	
	}
	}
	xmlhttp.open("GET","dept_prefix.php?type="+str,true);
	xmlhttp.send();




}
</script>
