<?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);
	$GetDocName['document'];
	if($GetDocName['document']!=''){
	$ExpDoc=explode(",",$GetDocName['document']);
	for($i=0;$i<count($ExpDoc);$i++)
	{
		$pthunlink='uploaded_docs/'.$ExpDoc[$i];
		unlink($pthunlink);
	}
    }
    
	if($GetDocName['image']!=''){
	$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('labor_daily_attendance',  $condition);
	prnMsg(_('Record deleted successfully'),'success');
	$s->pageLocation("index.php?route=HRModule/Master/labour_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">
$.noConflict();
function check()
{
	 var site=document.getElementById("site").value;	  
	 if(site=='000')
	{alert('Please select Project Name'); 
	document.getElementById("site").focus();
	return false;}	 
}
</script>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Labor List</h1>
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
    <td width="13%"><strong>Labor Code</strong></td>
    <td width="35%"><input name="code" type="text" value="<?php echo $_REQUEST['code']; ?>" /></td>
    <td width="13%"><strong>Labor Group</strong></td>
    <td colspan="3"><select name="dept">  
      <option value="000">---Select---</option>
      <?php 
	$query2=DB_query("select * from deptmaster order by deptname ASC",$db);
	while($result2=DB_fetch_array($query2))
	{
	?>
      <option value="<?php echo $result2['id'];?>" <?php if($result2['id']==$_REQUEST['dept'])echo "selected";  ?> ><?php echo $result2['deptname'];?></option>
      <?php } ?>
    </select></td>
</tr>
  <tr class="row1">
    <td><strong>Labor Name</strong></td>
    <td><input name="name" type="text" value="<?php echo $_REQUEST['name']; ?>" size="25" /></td>
    <td><strong>Labor Assign Position</strong></td>
    <td colspan="3"><select name="desig">
      <option value="000">---Select---</option>
      <?php 
	$query3=DB_query("select * from desigmaster order by designame ASC",$db);
	while($result3=DB_fetch_array($query3))
	{   
	?>
      <option value="<?php echo $result3['id'];?>" <?php if($result3['id']==$_REQUEST['desig'])echo "selected";  ?>><?php echo $result3['designame'];?></option>
      <?php } ?>
    </select></td>
  </tr>
  <tr class="row1">
    <td width="13%"><strong>Project Name</strong></td>
    <td width="35%"><select name="site" id="site">
      <option value="000">---Select---</option>
      <?php 
	  
	$query1=DB_query("Select id, project_name from manufacturing_advice  where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
	
	while($result1=DB_fetch_array($query1))
	{
	?>
      <option value="<?php echo $result1['id'];?>" <?php if($result1['id']==$_REQUEST['site'])echo "selected";  ?>><?php echo $result1['project_name'];?></option>
      <?php } ?>
    </select></td>
    <td width="13%"><strong>DOJ From Date</strong></td>
    <td width="15%"><input name="doj_from" type="text" value="<?php echo $_REQUEST['doj_from']; ?>" class="datepicker" /></td>
    <td width="6%"><strong>To Date</strong></td>
    <td><input name="doj_to" type="text" value="<?php echo $_REQUEST['doj_to']; ?>"  class="datepicker" /></td>
  </tr>
  <tr class="row1">
  	<td><strong>Employment Type</strong></td>
  	<td><select id="employment_type" name="employment_type">
			<option value="Worker"<?php if($_REQUEST['employment_type']=='Worker')echo "selected";  ?> >Worker</option>
	  </select></td>
	<td colspan="4"></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=HRModule/Master/labour_master&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>"><input type="button" value="Add New Labor" class="search" /></a></p> <?php } ?>
<table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#808080 1px solid; border-collapse:collapse;">
<tr>
  	<th width="4%" rowspan="2"><strong>Image</strong></th>
    <th width="10%" rowspan="2"><strong>Code</strong></th>
    <th rowspan="2"><strong>Labor Name</strong></th>
    <th width="10%" rowspan="2"><strong>Project Name</strong></th>
    <th width="6%" rowspan="2"><strong>DOB</strong></th>
    <th width="6%" rowspan="2"><strong>DOJ</strong></th>
    <th width="16%" rowspan="2"><strong>Group</strong></th>
    <th width="16%" rowspan="2"><strong>Assign Position</strong></th>
     <th width="16%" rowspan="2"><strong>Document Attached</strong></th>
	<!--<th width="10%" rowspan="2"><strong>User Status</strong></th>-->
	<th colspan="3" style="text-align:center;"><strong>Action</strong></th>
</tr>
<tr>
	<th width="4%" style="text-align:center;"><strong>View</strong></th>
	<th width="4%" style="text-align:center;"><strong>Edit</strong></th>
	<th width="4%" style="text-align:center;"><strong>Delete</strong></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 '".str_replace('/','-', FormatDateForSQL($_REQUEST['doj_from']))."' and '".str_replace('/','-',FormatDateForSQL($_REQUEST['doj_to']))."'";
	if($_REQUEST['employment_type']!='000')
	$filter[]="employment_type = '".$_REQUEST['employment_type']."' ";
	
	if($filter!='')
	{
    $sql=$sql." where ".implode(" and ",$filter) ."ORDER BY emp_name ASC";
	}
  //echo $sql;
}
else
{
	$sql="SELECT * FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."' AND employment_type='Worker' ORDER BY emp_name ASC";
}

$result=DB_query($sql,$db);
$i=1;
if(DB_num_rows($result)>0)
{
	while($myrow=DB_fetch_array($result))
	{
	if($myrow['image']!='')
	{
		$imagesUser=$myrow['image'];
	}
	else
	{
		if($myrow['sex']=='M')
		{
			$images='default.png';
		}
		else
		{
			$images='default-girl.png';
		}
	}
	$src='uploaded_images/'.$myrow['image'];
	if(!is_file($src)){
		$src='uploaded_images/noimage.jpg';
	}
?>  
  <tr bgcolor="#fff">
  	<td style="text-align:center;"><img src="<?php echo $src; ?>" height="50" id="image1" class="emp-img" /></td>
    <td><?php echo $myrow['emp_code'];?></td>
    <td><?php echo $myrow['emp_name'];?></td>
    <td><?php 
	echo $s->getField("manufacturing_advice","id",$myrow['emp_site'],"project_name");?></td>
    <td><?php echo ConvertSQLDate($myrow['dob']);?></td>
    <td><?php echo ConvertSQLDate($myrow['doj']);?></td>
    <td><?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><?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 $documents=explode(',',$myrow['document']);
    //print_r($documents);
    $count=count($documents);
    for($i=0;$i<$count;$i++){
    ?>
    <a href="uploaded_docs/<?php echo $documents[$i]; ?>"><?php echo $documents[$i]; ?></a>
    <?php	
    } 
    ?>
	</td>
        <!--<td><?php 
        //$cnt=DB_query("select * from www_users where empid='".$myrow['id']."' and companyid='".$_SESSION['ACGCompanyId']."' ",$db); if(DB_num_rows($cnt)>0) {$user=DB_fetch_assoc($cnt); echo $user['userid']; }else{echo '--';} ?>
	<input type="hidden" id="empId_<?php //echo $i; ?>" value="<?php //echo $myrow['id'];?>" />
	</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="text" id="empId_<?php //echo $i; ?>" value="<?php //echo $myrow['id'];?>" />
	</td>-->
	<td style="text-align:center;"><a href="index.php?route=HRModule/Master/view_labour&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id'];?>&action=view"><img src="images/search.gif" height="15" width="15" title="View Document" /></a></td>
	<td style="text-align:center;">
	<?php if($_SESSION['frmEdit']=='1'){?>
	<a href="index.php?route=HRModule/Master/labour_master_edit&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $myrow['id'];?>&action=edit"><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.5;" /><?php } ?></td>
	<td style="text-align:center;"><?php if($_SESSION['frmDelete']=='1'){?>
	<a href="index.php?route=HRModule/Master/labour_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 Related Record?');"><img src="images/Icon_Delete.png" height="15" width="15" title="Delete Document" /></a><?php }else { ?><img src="images/Icon_Delete.png" height="15" width="15" title="No Permission to Delete Document" style="opacity:0.5;" /><?php } ?></td>
  </tr>
  <?php $i++;} 
} else {?>
<tr class="row1"><td colspan="10"><h3 align="center"><strong><font color="#FF0000">No Record Found</font></strong></h3></td></tr>
<?php } ?>
</table>
<div id="div_id" style="display:none;"></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>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
