<?php
$PageSecurity = 15;
$title = _('Department Master') ;
if(!empty($_GET['id']) and $_REQUEST['act']=='del')
{
$SQL="delete from deptmaster 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=HRModule/DeptMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
}

function CheckdeptCode($deptcode)
{
global $db;
$SQL ="select deptcode,deptname from deptmaster where deptcode ='".$deptcode."' and CompanyId='".$_SESSION['ACGCompanyId']."'";	
$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 where id='".$_GET['id']."'";
$result=DB_query($SQL,$db,$ErrMsg);
$resrow=DB_fetch_array($result);
$_POST['deptcode'] = $resrow['deptcode'];
$_POST['deptname'] = $resrow['deptname'];
}

if($_POST['edit'] && $_REQUEST['act'] == 'edit')
{
	 $sql_check_dept="select count(*) from deptmaster where deptcode='".$_REQUEST['deptcode']."' and  id!='".$_GET['id']."'";
	 $result_check_dept=DB_query($sql_check_dept,$db);
     $rs_check_dept=DB_fetch_array($result_check_dept);
     $count_rows=$rs_check_dept[0];
    
    if($count_rows>0)
    {
      prnMsg( _('This Department Code Already Exists. Please Enter A New Code!'),'error');
    }
    else
    { 
    	 $SQL="update deptmaster set deptcode = '".$_POST['deptcode']."', deptname='".$_POST['deptname']."' where id='".$_GET['id']."'";
         $result = DB_query($SQL,$db,$ErrMsg);
        prnMsg( _(' Department Code Updated!'),'success');
         echo "<script>location.href='index.php?route=HRModule/DeptMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
         unset($_POST);
    }     
}

if(isset($_POST['add']) and isset($_POST['deptcode']) and isset($_POST['deptname']) and $_GET['act']!='edit')
{
	if(CheckdeptCode($_POST['deptcode']))
	{
		$SQL = "INSERT into deptmaster (id, deptcode, deptname,CompanyId) 
					 values('', '".$_POST['deptcode']."', '".$_POST['deptname']."','".$_SESSION['ACGCompanyId']."')";	
		
		$InsResult=DB_query($SQL,$db,$ErrMsg);
		prnMsg( _('A New Deptmartment Code Has been Inserted!'),'success');
		echo "<script>location.href='index.php?route=HRModule/DeptMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
		unset($_POST);
	}
	else
	{	
	prnMsg( _('This Department Code Already Exists. Please Enter A New Code!'),'error');
	}	
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<form method="post" action="">
<h1>Department Master</h1>
	<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
		<tr class="row1">
		    <td width="15%" align="right">Department Code</td>
			<td width="35%"><input name="deptcode" type="text" id="deptcode" value="<?php echo $_POST['deptcode']; ?>"  onkeyup="leftTrim(this)" autocomplete="off"/></td>
			<td width="15%" align="right">Department Name</td>
			<td><input name="deptname" type="text" id="deptname" value="<?php echo $_POST['deptname']; ?>"  autocomplete="off" onkeyup="leftTrim(this)"/></td>
		</tr>
		<tr class="row1">
		  	<td colspan="4" align="center"><?php
			if($_GET['act']=='edit'){?><input type="submit" name="edit" class="search" value="Update" onclick="return validate();" /> <input type="hidden" name='uiqeid' class="search" value="<?php echo $_GET['id']; ?>" />
			<?php }else{ 
			if($_SESSION['frmAdd']=='1'){?>
			<input type="submit" name="add" class="search" value="Submit" onclick="return validate();" /> <input type="reset" name="cancel" class="search"  value="Reset" /><?php }} ?>
			</td>
	  	</tr>
	</table>
</form>
<br>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" class="thborder">
	<tr>
	    <th width="20%">Department Code</th>
	    <th>Department Name</th>
	    <th width="6%" class="text-center">Edit</th>
	    <th width="6%" class="text-center">Delete</th>
	</tr>
	<?php
	$SQL ="select * from deptmaster where companyId='".$_SESSION['ACGCompanyId']."'";	
	$result=DB_query($SQL,$db,$ErrMsg); 
	while($resrow=DB_fetch_array($result)){?>
	<tr>
		<td><?php echo $resrow['deptcode']; ?><!--<input type="text" readonly="" class="input_read" value="<?php //echo $resrow['deptcode']; ?>"  />--></td>
		<td><?php echo $resrow['deptname']; ?><!--<input type="text" readonly="" class="input_read" value="<?php //echo $resrow['deptname']; ?>"  />--></td>
		<td align="center">
		<?php if($_SESSION['frmEdit']=='1'){?>
		<a href="index.php?route=HRModule/DeptMaster&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&act=edit&id=<?php echo $resrow['id']; ?>" class="link"><img src="images/icon-edit.png" width="15" title="Edit" /></a><?php } else {?><img src="images/icon-edit.png" width="15" style="opacity:0.6;" title="No Permission to Edit Document" /><?php }?></td>
		<td align="center">
		<?php if($_SESSION['frmDelete']=='1') {?>
		<a href="index.php?route=HRModule/DeptMaster&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&act=del&id=<?php echo $resrow['id']; ?>" onclick="return confirm('Are you sure you want to delete this record?')" class="link"><img src='images/Icon_Delete.png' width="15" title="Delete Document" /></a><?php }else {?><img src='images/Icon_Delete.png' width="15" style="opacity:0.6;" title="No Permission to Delete Document" /><?php }?></td>
	</tr>
	<?php } ?>
</table>

</div>
</div>
<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 type="text/javascript">
	
	function validate()
	{
		if($("#deptcode").val()=='')
		{
			alert("Please Enter Department Code");
			$("#deptcode").focus();
			return false;
		}
		else if($("#deptname").val()=='')
		{
			alert("Please Enter Department Name");
			$("#deptname").focus();
			return false;
		}


	}

	function leftTrim(element)
{
	if(element)
		element.value=element.value.replace(/^\s+/,"");
}

</script>