<?php
$PageSecurity = 15;
$title = _('Leave 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];
$typeArr=array('e'=>'Yearly Expire','c'=>'Carry Forward');
function selectcode()
{
global $db;
$SQL ="select leavecode from leavemaster";
$result=DB_query($SQL,$db,$ErrMsg);
while($resrow=DB_fetch_array($result))
{
echo '<option value='.$resrow[leavecode].'>'.$resrow[leavecode].'</option>';
}
}

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

function CheckleaveCode($leavecode)
{
global $db;
$SQL ="select leavecode,leavename,leavetype from leavemaster where leavecode='".$leavecode."'";	
$result=DB_query($SQL,$db,$ErrMsg);
$resrow=DB_fetch_array($result);
if(isset($resrow['leavecode']))
	{
		return false;
	} 
	else 
    {
			return true;
  }
}

if(!$_POST['edit'] and $_REQUEST['act']=='edit' and $_GET['id']!='')
{
$SQL="select leavecode,leavename,leavetype,leave_flag,max_leave from leavemaster where id='".$_GET['id']."'";
$result=DB_query($SQL,$db,$ErrMsg);
$resrow=DB_fetch_array($result);
$_POST['leavecode']=$resrow['leavecode'];
$_POST['leavename']=$resrow['leavename'];
$_POST['leavetype']=$resrow['leavetype'];
$_post['leaveflag']=$resrow['leave_flag'];
$_POST['leave_month']=$resrow['max_leave'];

}
if($_POST['edit'] && $_REQUEST['act'] == 'edit' and $_GET['id']!='')
{
$SQL="update leavemaster set leavecode='".$_POST['leavecode']."' , leavename='".$_POST['leavename']."', leavetype='".$_POST['leavetype']."',leave_flag='".$_POST['leaveflag']."',max_leave='".$_POST['leave_month']."' where id='".$_GET['id']."' ";
$result = DB_query($SQL,$db,$ErrMsg);
prnMsg( _(' Leave  Code Has Updated!'),'success');
echo "<script>location.href='index.php?route=LeaveMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
unset($_POST);
}
if(isset($_POST['add']) and !empty($_POST['leavecode']) and !empty($_POST['leavename']) and $_GET['act']!='edit')
{
	if(CheckleaveCode($_POST['leavecode']))
	{
      $SQL = "INSERT into leavemaster(id, leavecode, leavename,leavetype,leave_flag,max_leave) values ('','".$_POST['leavecode']."','".$_POST['leavename']."','".$_POST['leavetype']."','".$_post['leaveflag']."','".$_POST['leave_month']."')";	
		$InsResult=DB_query($SQL,$db,$ErrMsg);
		prnMsg( _('A New Leave Code Has Inserted!'),'success');
		echo "<script>location.href='index.php?route=LeaveMaster&modid=".$_SESSION['modid']."&frmid=".$_SESSION['frmid']."'</script>";
		unset($_POST);
	}
	else
     { 
			prnMsg( _('This Leave 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>Leave Master</h1>

<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td width="13%" align="right">Leave Code</td>
	<td width="20%"><input name="leavecode" type="text" id="leavecode" value="<?php echo $_POST['leavecode']?>" /></td>
	<td width="13%" align="right">Leave Name</td>
	<td width="20%"><input name="leavename" type="text" id="leavename" value="<?php echo $_POST['leavename']?>" /></td>
	<td width="13%" align="right">Leave Type</td>
	<td nowrap="nowrap"><input type="radio" name="leavetype" value="e" <?php if($_POST['leavetype']=='e') {?>checked <?php } ?>>Yearly Expired
	<input type="radio" name="leavetype" value="c" <?php if($_POST['leavetype']=='c') {?>checked<?php } ?>>Carry Forward</td>
</tr>
<tr class="row1">
	<td align="right">Leave Flag</td>
	<td><select name="leaveflag"  id="leaveFlag"  >
	<option value="0" <?php if($_post['leaveflag']=='0') { ?> selected <?php }?> >No</option>
	<option value="1" <?php if($_post['leaveflag']=='1') { ?> selected <?php }?>>Yes</option>
	</select>
	</td>
	<td align="right">Max Leave</td>
	<td><input name="leave_month" type="text" id="leave_month" value="<?php echo $_POST['leave_month']?>" /></td>
	<td colspan="2"></td>
</tr>
<tr class="row1">
  <td colspan="6" align="center">
	<?php if($_GET['act']=='edit'){?>
	<input type="submit" name="edit"  value="Edit" class="search" /> 
	<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"/> <input type="submit" name="cancel"  value="Cancel" class="search"/><?php } }?></td>
  </tr>
</table>
</form>
<br>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" class="thborder">
<tr>
	<th width="10%">Leave Code</th>
	<th>Leave Name</th>
	<th width="12%">Leave Type</th>
	<th width="8%">Max Leave /month</th>
	<th width="6%" class="text-center">Edit</th>
	<th width="6%" class="text-center">Delete</th>
</tr>
<?php
$SQL ="select * from leavemaster";	
$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['leavecode']; ?>"  /></td>
	<td><input type="text" readonly="" class="input_read" value="<?php echo $resrow['leavename']; ?>"  /></td>
	<td><input type="text" readonly="" class="input_read" value="<?php echo $typeArr[$resrow['leavetype']]; ?>"  /></td>
	<td><input type="text" readonly="" class="input_read" value="<?php echo $resrow['max_leave']; ?>"  /></td>
	<td align="center"><?php if($_SESSION['frmEdit']=='1'){?><a href="index.php?route=LeaveMaster&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" height="15" width="15" title="Edit Document" /></a><?php }else{?><img src="images/icon-edit.png" height="15" width="15" title="No Permission to Edit Document" style="opacity:0.6;" /><?php } ?></td>
	<td align="center"><?php if($_SESSION['frmDelete']=='1'){?><a href="index.php?route=LeaveMaster&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" height="14" width="14" title="Delete Document" /></a><?php } else{?><img src="images/Icon_Delete.png" height="14" width="14" title="No Permission to Delete Document" style="opacity:0.6;" /><?php }?></td>
</tr>
<?php }?>
</table>
</div>
</div>
<?php 
include('includes/footer.inc');
?>
<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";
}*/
</style>