<?php
$PageSecurity = 2;
$title = _('View/Search Employee') ;
include('includes/SQL_CommonFunctions.inc');
$_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($_REQUEST['id']!='' && $_REQUEST['action']=='delete')
{
	$GetDocSQL=DB_query("select document,image from empmaster where id='".$_REQUEST['id']."'",$db);
	$GetDocName=DB_fetch_array($GetDocSQL);
	
	$ExpDoc=explode(",",$GetDocName['document']);
	for($i=0;$i<count($ExpDoc);$i++)
	{
		$pthunlink='uploaded_docs/'.$ExpDoc[$i];
		unlink($pthunlink);
	}
	
	$pthimgunlik='uploaded_images/'.$GetDocName['image'];
	unlink($pthimgunlik);
	
	
	$sql1="Delete from empmaster where id='".$_REQUEST['id']."'";
	$result1=DB_query($sql1,$db);
	$sql2="Delete from employee_address where emp_id='".$_REQUEST['id']."'";
	$result2=DB_query($sql2,$db);
	$sql3="Delete from emp_salary where emp_id='".$_REQUEST['id']."'";
	$result3=DB_query($sql3,$db);
	$sql4="Delete from emp_leave where emp_id='".$_REQUEST['id']."'";
	$result4=DB_query($sql4,$db);
	$condition="employee='".$_REQUEST['id']."'";
	$s->DeleteRecord('daily_attendance',  $condition);
	
	$s->pageLocation("index.php?route=employee_list".$rurl);
}

if($_REQUEST['userid'])
{
	$data_userid['userid']=$_REQUEST['userid'];
	$data_userid['password']= CryptPass($_POST['password']);
	$data_userid['companyid']=$_REQUEST['companyId'];
	$data_userid['mst_dpt_heirarchy']=$_REQUEST['heirarchy'];
	if($_REQUEST['assigned_sites']!='0' && $_REQUEST['assigned_sites']!='')
	{
	$data_userid['assigned_sites']=implode(",",$_REQUEST['assigned_sites']);
	 
	}
	else
	{
		$data_userid['assigned_sites']=$_REQUEST['assigned_sites'];	 
	}
	
	$data_userid['mst_site']=implode(",",$_REQUEST['mst_site']);	
	
	$data_userid['dashboard_assign']=$_REQUEST['dashboard'];
    $data_userid['department']=$_REQUEST['department'];
    $data_userid['designation']=$_REQUEST['designation'];
	$data_userid['fullaccess']=8;
	$data_userid['theme']='professional';
	$data_userid['language']='en_US';
	$data_userid['Type']='user';
  	$data_userid['realname']=$_REQUEST['realname'];
	$data_userid['rolepid']=$_REQUEST['rolepid'];
	//echo "<pre>";
	//print_r($data_userid);
	$row_insert= $s->RecordInsert('www_users', $data_userid);
	$lastid=$s->GetLastRecord('www_users',  "1=1",'id');	
	
	$updateemp=DB_query("update empmaster set mst_www_userid='".$lastid."' where id='".$_REQUEST['EmpId']."'",$db);
	$s->pageLocation("index.php?route=employee_list".$rurl);
}
?>

<script type="text/javascript" language="javascript">
function check()
{
	 var site=document.getElementById("site").value;	  
	 if(site=='000')
	{alert('Please select site'); 
	document.getElementById("site").focus();
	return false;}	
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<form action="" method="post">
<h1>Employee List</h1>
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
    <td width="12%" align="right">Employee Code</td>
    <td width="35%"><input name="code" type="text" value=""  /></td>
    <td width="20%" align="right">Employee Department</td>
    <td colspan="3"><select name="dept" >  
      <option value="000">---Select---</option>
      <?php 
	$query2=DB_query("select * from deptmaster",$db);
	while($result2=DB_fetch_array($query2))
	{
	?>
      <option value="<?php echo $result2['deptcode'];?>"><?php echo $result2['deptname'];?></option>
      <?php } ?>
    </select></td>
</tr>
  <tr class="row1">
    <td align="right">Employee Name</td>
    <td><input name="name" type="text" value="" /></td>
    <td align="right">Employee Designation</td>
    <td colspan="3"><select name="desig" >
      <option value="000">---Select---</option>
      <?php 
	$query3=DB_query("select * from desigmaster",$db);
	while($result3=DB_fetch_array($query3))
	{   
	?>
      <option value="<?php echo $result3['desigcode'];?>"><?php echo $result3['designame'];?></option>
      <?php } ?>
    </select></td>
  </tr>
  <tr class="row1">
    <td align="right">Branch</td>
    <td><select name="site"  id="site">
<!--      <option value="000">---Select---</option>-->
      <?php 
	  if($_SESSION['AssignedBranch']!=''){ 
	$query1=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);}
	else
	{$query1=DB_query("select loccode, locationname from locations",$db);}
	while($result1=DB_fetch_array($query1))
	{
	?>
      <option value="<?php echo $result1['loccode'];?>"><?php echo $result1['locationname'];?></option>
      <?php } ?>
    </select></td>
    <td align="right">DOB From</td>
    <td><input name="doj_from" type="text" value="" size="12" class="datepicker"  /></td>
    <td align="right">To</td>
    <td><input name="doj_to" type="text" value="" size="12" class="datepicker" /></td>
  </tr>
  <tr class="row1">
    <td colspan="6" style="text-align:center;"><input type="submit" name="search" class="search" value="Search" onclick="return check();"/></td>
    </tr>
</table>

</form>
<?php if($_SESSION['frmAdd']='1'){?>
<p align="left"><a href="index.php?route=EmpMaster_new&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>"><input type="button" value="Add New Employee" class="search" /></a></p> <?php } ?>
<div class="overpanel">
<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
  	<th rowspan="2" width="6%">Image</th>
    <th rowspan="2" width="12%">Code</th>
    <th rowspan="2">Employee Name</th>
    <th rowspan="2" width="12%">Site</th>
    <th rowspan="2" width="7%">Date of Birth</th>
    <th rowspan="2" width="7%">Date of Joining</th>
    <th rowspan="2" width="12%">Department</th>
    <th rowspan="2" width="12%">Desgination</th>
	<!--<th class="tabledata">Employee Type</th>-->
	<th colspan="3" style="text-align:center;" class="tabledata">Action</th>
</tr>
<tr>
	<th width="3%">View</th>
	<th width="3%">Edit</th>
	<th width="3%">Delete</th>
</tr>
<?php 
if($_REQUEST['search']!='')
{
	$filter=array();
 	$sql="select * from empmaster";
 	if($_REQUEST['code']!='')
	$filter[]="emp_code like '%".$_REQUEST['code']."%' ";
	if($_REQUEST['name']!='')
	$filter[]="emp_name like '%".$_REQUEST['name']."%' ";
	if($_REQUEST['site']!='000')
	$filter[]="emp_site = '".$_REQUEST['site']."' ";
	if($_REQUEST['dept']!='000')
	$filter[]="department = '".$_REQUEST['dept']."' ";
	if($_REQUEST['desig']!='000')
	$filter[]="designation = '".$_REQUEST['desig']."' ";
	if($_REQUEST['doj_from']!='' && $_REQUEST['doj_to']!='')
	$filter[]="doj between '".FormatDateForSQL($_REQUEST['doj_from'])."' and '".FormatDateForSQL($_REQUEST['doj_to'])."'";
	
	if($filter!='')
	{
		$sql=$sql." where ".implode(" and ",$filter);
	}
//echo $sql;
}
else
{
	$sql="SELECT * FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' ORDER BY emp_code ASC";
}

$result=DB_query($sql,$db);
$i=1;
if(DB_num_rows($result)>0)
{
	while($myrow=DB_fetch_array($result))
	{
	if($myrow['image']!='')
	{
		$images=$myrow['image'];
	}
	else
	{
		if($myrow['sex']=='M')
		{
			$images='default.png';
		}
		else
		{
			$images='default-girl.png';
		}
	}
?>  
  <tr>
  	<td><img src="uploaded_images/<?php echo $images;?>" width="45" height="45" id="image1" /></td>
    <td><input type="text" readonly="" class="input_read" value="<?php echo $myrow['emp_code'];?>"  /></td>
    <td><input type="text" readonly="" class="input_read" value="<?php echo $myrow['emp_name'];?>"  /></td>
    <td><input type="text" readonly="" class="input_read" value="<?php 
	$site_sql=DB_query("Select locationname from locations where loccode='".$myrow['emp_site']."'",$db);
	$site_row=DB_fetch_array($site_sql);
	echo $site_row['locationname'];?>"  /></td>
    <td><input type="text" readonly="" class="input_read" value="<?php echo ConvertSQLDate($myrow['dob']);?>"  /></td>
    <td><input type="text" readonly="" class="input_read" value="<?php echo ConvertSQLDate($myrow['doj']);?>"  /></td>
    <td><input type="text" readonly="" class="input_read" value="<?php
	$dept_sql=DB_query("Select deptname from deptmaster where id='".$myrow['department']."'",$db);
	$dept_row=DB_fetch_array($dept_sql);
	echo $dept_row['deptname'];?>"  /></td>
    <td><input type="text" readonly="" class="input_read" value="<?php 
	$desig_sql=DB_query("Select designame from desigmaster where id='".$myrow['designation']."'",$db);
	$desig_row=DB_fetch_array($desig_sql);
	echo $desig_row['designame'];?>"  /></td>
	<!--<td><?php //if($myrow['mst_www_userid']==''){echo 'Employee'.'<input type="button" name="makeemp" id="makeemp_'.$i.'" value="Make User" class="search" />';}else{echo 'User/Employee';} ?>
	<input type="hidden" id="empId_<?php echo $i; ?>" value="<?php echo $myrow['id'];?>" />
	</td>-->
	<td align="center"><a href="index.php?route=view_employee&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id'];?>&action=edit"><img src="images/search.gif" width="15" title="View Document" /></a></td>
	<td align="center">
	<?php if($_SESSION['frmEdit']=='1'){?>
	<a href="index.php?route=EmpMaster_Edit&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id'];?>&action=edit"><img src="images/icon-edit.png" width="15" title="Edit Document" /></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=employee_list&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id']?>&action=delete" onClick="return confirm('Are you sure you want to delete All Record of This employee(Including Attendance )?');"><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 $i++;} 
} else {?>
<tr><td colspan="10" align="center"><font color="#FF0000">No Record Found</font></td></tr>
<?php } ?>
</table>
</div>
<div id="div_id" style="display:none;"></div>
</div>
</div>

<script type="text/javascript" language="javascript">
$.noConflict();
$(document).ready(function(){	
		//var cnt=document.getElementById("count").value;
		$('input[name=makeemp]').click(function(){
		var tt=this.id;
		var tt1=tt.split("_");
		var tt2=tt1[1];
		var EmpId=document.getElementById("empId_"+tt2).value;
		
	//	var LeaveType=document.getElementById("LeaveType_"+tt2).value;
		$("#div_id").dialog({
				title:'Create User',
				modal: true,
				resizable: false,
				width:600,
				height:600,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				dialogClass: 'main-dialog-class',
				buttons: {
					Save: function()
					{
						$("#myform").closest("form").submit();
					},
					Exit: function()
					{
						$(this).dialog("close");
					}
				}
				
			});
		MakeEmpAsUser(EmpId);
		});
	});

function MakeEmpAsUser(EmpId)
{
	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)
	{	 
		document.getElementById("div_id").innerHTML=xmlhttp.responseText.split("__");		
	}
	}
	xmlhttp.open("GET","MakeEmpAsUsr.php?EMPID="+EmpId,true);
	xmlhttp.send();
}

function FProfileMaster()
{
	myCheckboxArray = document.myform.elements["assigned_sites[]"];
	var t=myCheckboxArray.length;
	if(document.getElementById('assigned_site').checked==true)
	{	
	if(t==undefined)
	document.getElementById('assigned_sites_'+0).checked=true;		
	for (var i = 0; i <= t; i++)
	{
	document.getElementById('assigned_sites_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('assigned_sites_'+0).checked=false;		
	
	for (var i = 0; i <= t; i++)
	{
	document.getElementById('assigned_sites_'+i).checked=false;	
	}
	}
}

function getZones(str)
{	
	var cnt=document.getElementById("cnt").value;
	var arr=Array();
	for(var i=0; i<cnt; i++)
	{
	if(document.getElementById("mst_site_"+i).checked==true)
	{
	arr.push(document.getElementById("mst_site_"+i).value);
	}
	}
	//alert(arr);
	
	if(str=="")
	{
	document.getElementById('hid-zone-div').innerHTML="";
	return;
	}
	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)
	{
	document.getElementById("hid-zone-div").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","ZonePrefix.php?arr="+arr,true);
	xmlhttp.send();
}

function validate()
{
	if(document.getElementById("userid").value=='')
	{
		alert("Please Fill The UserId");
		document.getElementById("userid").focus();
		return false;
	}
	if(document.getElementById("password").value=='')
	{
		alert("Please Fill The Password");
		document.getElementById("password").focus();
		return false;
	}
	
	if(document.getElementById("heirarchy").value=='')
	{
		alert("Please Select Heirarchy");
		document.getElementById("password").focus();
		return false;
	}

	if(document.getElementById("rolepid").value=='')
	{
		alert("Please Select The Roles");
		document.getElementById("rolepid").focus();
		return false;
	}	
	
	var countbranch=document.getElementById("cnt").value;
	var chec=0;
	for(var i=0; i<countbranch;i++)
	{
		if(document.getElementById("mst_site_"+i).checked==true)
		{
			chec=1;
		}
	}
	if(chec==0)
	{
		alert("Please Check At Least One Branch");
		return false;
	}	
	
	var countzone=document.getElementById("countzone").value;
	var chkzone=0;
	for(var j=0; j<countzone;j++)
	{
		if(document.getElementById("assigned_sites_"+j).checked==true)
		{
			chkzone=1;
		}		
	}
	if(chkzone==0)
	{
		alert("Please Check At Least One Zone");
		return false;
	}	
	
	var countdash=document.getElementById("countdash").value;
	var dason=0;
	for(var m=0; m<countdash;m++)
	{
		if(document.getElementById("dashboard_"+m).checked==true)
		{
			dason=1;
		}		
	}
	
	if(dason==0)
	{
		alert("Please Select At Least One DashBoard");
		return false;
	}
}
</script>

<style>
.text{font-family:Arial, Helvetica, sans-serif; color:#000000; font-weight:bold; text-align:left;}
.short{font-family:Arial, Helvetica, sans-serif; color:#CCCCCC; font-weight:bold; text-align:center;}
.short .td{float:left; background-color:#999999; border:#000000; border-radius:5px; -moz-border-radius:5px; webkit-border-radius:5px;}
.short .div{background:#000000; color:#000000; border-bottom:#0099CC; cursor:pointer; bottom:5; volume:loud; position:absolute; quotes:none;}
</style>