<?php
$PageSecurity = 15;
function CheckdesigCode($desigcode,$deptid)
{
global $db;
$SQL ="select desigcode,designame from desigmaster where desigcode ='".$desigcode."' and mst_departmentid='".$deptid."' and CompanyId='".$_SESSION['ACGCompanyId']."'";	
$result=DB_query($SQL,$db,$ErrMsg);
$resrow=DB_fetch_array($result);
if(isset($resrow['desigcode'])){return false;} else {return true;}
}

if(isset($_GET['id']) and $_GET['act']=='del')
{
$SQL="delete from desigmaster where id='".$_GET['id']."'";
$ErrMsg = _('The Designation Master  record could not be deleted because');
$result = DB_query($SQL,$db,$ErrMsg);
prnMsg( _(' Designation Code Has Deleted!'),'success');
echo "<script>location.href='index.php?route=HRModule/DesigMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
}

if(!$_POST['edit'] and $_REQUEST['act']=='edit' and $_GET['id']!='')
{
$SQL="select mst_departmentid,desigcode,designame from desigmaster where id='".$_GET['id']."'";
$result=DB_query($SQL,$db,$ErrMsg);
$resrow=DB_fetch_array($result);
$_POST['desigcode']=$resrow[desigcode];
$_POST['designame']=$resrow[designame];
$_POST['mst_departmentid']=$resrow['mst_departmentid'];
}

if($_POST['edit'] && $_REQUEST['act'] == 'edit' and $_GET['id']!='')
{
	 $sql_check_code="select count(*) from desigmaster where desigcode='".$_REQUEST['desigcode']."' and id!='".$_GET['id']."' ";
	 $result_check_code=DB_query($sql_check_code,$db);
	 $rs_check_code=DB_fetch_array($result_check_code);
	 $count_row=$rs_check_code[0];
   if($count_row<1)
   {

		$SQL="update desigmaster set mst_departmentid='".$_POST['mst_departmentid']."', desigcode='".$_POST['desigcode']."' , designame='".$_POST['designame']."', CompanyId='".$_SESSION['ACGCompanyId']."' where id='".$_GET['id']."' ";
		$result = DB_query($SQL,$db,$ErrMsg);
		prnMsg( _('Designation Code Updated!'),'success');
		echo "<script>location.href='index.php?route=HRModule/DesigMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
		unset($_POST);
   }
   else
   {
   	 prnMsg( _('This Designation Code Already Exist please Enter New Group Code!'),'error');
   }		
}

if(isset($_POST['add']) and !empty($_POST['desigcode']) and !empty($_POST['designame']) and $_GET['act']!='edit')
{
	if(CheckdesigCode($_POST['desigcode'],$_POST['mst_departmentid']))
	{
		 $SQL = "INSERT into desigmaster (id, mst_departmentid, desigcode, designame, CompanyId) 
				values ('','".$_POST['mst_departmentid']."','".$_POST['desigcode']."','".$_POST['designame']."', '".$_SESSION['ACGCompanyId']."')";	
		$InsResult=DB_query($SQL,$db,$ErrMsg);
		prnMsg( _('A New Designation Code Inserted!'),'success');
		echo "<script>location.href='index.php?route=HRModule/DesigMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
		unset($_POST);
	}
	else
	{	
	prnMsg( _('This Designation Code Already Exist please Enter New Group Code!'),'error');
	}	
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<form method="post" action="">
<h1>Designation Master</h1>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right">Department</td>
    <td width="20%"><select name="mst_departmentid" type="text" id="mst_departmentid">
	<?php
		$dept_sql = "SELECT id,deptname FROM deptmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."'";
		$dept_result = DB_query($dept_sql,$db);
		while($deptRow = DB_fetch_array($dept_result)){
	?>
		<option value="<?php echo $deptRow['id']?>" <?php echo ($deptRow['id']==$_POST['mst_departmentid']) ?"selected" :"" ;?>><?php echo $deptRow['deptname']?></option>
	<?php } ?></select></td>
    <td width="13%" align="right">Designation Code</td>
    <td width="20%"><input name="desigcode" type="text" id="desigcode" autocomplete="off" value="<?php echo $_POST['desigcode']?>" onkeyup="leftTrim(this)" /></td>
    <td width="13%" align="right">Designation Name</td>
    <td><input name="designame" type="text" id="designame" onkeyup="leftTrim(this)" autocomplete="off" value="<?php echo $_POST['designame']?>" /></td>
  </tr>
  <tr class="row1">
    <td colspan="6" align="center">
<?php if($_GET['act']=='edit'){?>
<input type="submit" name="edit"  value="Update" class="search" onclick="return validate();" />
<input type="hidden" name='uiqeid' value="<?php echo $_GET['id']?>" />
<?php }else{ 
if($_SESSION['frmAdd']=='1'){
?><input type="submit" name="add"  value="Accept"  class="search" onclick="return validate();" /> <input type="reset" name="cancel" class="search"  value="Reset" /><?php } }?>
 </td>
    </tr>
</table>
</form>
<br>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
<tr>
	<th width="12%">Department</th>
	<th width="12%">Designation Code</th>
	<th>Designation Name</th>
	<th width="6%" class="text-center">Edit</th>
	<th width="6%" class="text-center">Delete</th>
</tr>
<?php
$SQL ="select DG.id, DG.desigcode, DG.designame, DP.deptname from desigmaster DG LEFT JOIN deptmaster DP ON DP.id=DG.mst_departmentid WHERE DG.CompanyId='".$_SESSION['ACGCompanyId']."'";	
$result=DB_query($SQL,$db,$ErrMsg); 
while($resrow=DB_fetch_array($result)){?>
<tr>
	<td><input type="text" readonly="" class="input_read" value="<?php echo $resrow['deptname']?>"  /></td>
	<td><input type="text" readonly="" class="input_read" value="<?php echo $resrow['desigcode']?>"  /></td>
	<td><input type="text" readonly="" class="input_read" value="<?php echo $resrow['designame']?>"  /></td>
	<td align="center">
	<?php if($_SESSION['frmEdit']=='1'){?>
	<a href="index.php?route=HRModule/DesigMaster&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/DesigMaster&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" /></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>
<?php
include('includes/footer.inc');
?>
<style type="text/css" >
/*.button{ background-color:#4D767D;
color: #FFFFFF;
height:25px;
margin:0px 5px 0px 5px;
border:1px solid #000000;
}
.link{ color:#9900CC; font-family:"Comic Sans MS";
}*/
</style>
<script type="text/javascript">
	
	function validate()
	{
		if($("#desigcode").val()=='')
		{
			alert("Please Enter Designation Code");
			return false;
			$("#desigcode").focus();
        }
       else if($("#designame").val()=='')
		{
			alert("Please Enter Designation Name");
			return false;
			$("#designame").focus();
        }

	}


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

</script>