
<?php   
$PageSecurity = 1;
include('includes/AccountGroupMaster_Class.php');
include('includes/PaginationClass.php');
include('includes/fun-HRClass.php');
include('includes/fun-php.php');
$table='bil_chamber';

if($_REQUEST['Save'] && $_REQUEST['action']!='Edit')
{
$data['chamber']=$_REQUEST['Chamber'];
$data['chamber_code']=$_REQUEST['chamber_code'];
$data['CompanyId']=$_SESSION['ACGCompanyId'];
$data['Created_date']=date('Y-m-d');
$data['Created_by']=$_SESSION['UserID'];
$s->RecordInsert($table,$data);
//$s->pageLocation('index.php?route=Voucher_Master&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}
	
if(!$_REQUEST['Save'] &&   $_REQUEST['action']=='Edit')
{
	$condition="id='".$_REQUEST['VoucherType']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
	$chamber_code=$RData['chamber_code'];
	$Chamber=$RData['chamber'];
	
}	
if($_REQUEST['Save'] && $_REQUEST['action']=='Edit')
{
 $data['chamber']=$_REQUEST['Chamber'];
 $data['chamber_code']=$_REQUEST['chamber_code'];
 $data['Modified_date']=date('Y-m-d');
 $data['Modified_by']=$_SESSION['UserID'];
  $cond="id='".$_REQUEST['VoucherType']."'";
	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	if(!$row)
	{
	   die('Error'.DB_error());
	}
  prnMsg( _('Record Updated successfully'),'success');
}

if($_REQUEST['action']=='del')
{
	$condition="id='".$_REQUEST['VoucherType']."'";
	$s->DeleteRecord($table,$condition);
	prnMsg( _('Record deleted successfully'),'success');
	$s->pageLocation('index.php?route=Billing/Masters/chamber_master&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
}
if($_REQUEST['employee_code']!='')
{
    $emp_weekly_off=$s->GetFieldValue('empmaster'," id='".$_REQUEST['employee_code']."' " ,'emp_weekly_off');
   $month=$_REQUEST['month'];
   $years=$_REQUEST['years'];
    $emp_id=$_REQUEST['employee_code'];
    $dateofjoining=$s->GetFieldValue('empmaster'," id='".$_REQUEST['employee_code']."' " ,'doj');
    
  $workingdays= cal_days_in_month(CAL_GREGORIAN, $month, $years);	
 
if($month==1){ $mnthName='January';}
elseif($month==2){ $mnthName='February'; }
elseif($month==3){ $mnthName='March'; }
elseif($month==4){ $mnthName='April'; }
elseif($month==5){ $mnthName='May'; }
elseif($month==6){ $mnthName='June'; }
elseif($month==7){ $mnthName='July'; }
elseif($month==8){ $mnthName='August'; }
elseif($month==9){ $mnthName='September'; }
elseif($month==10){ $mnthName='October'; }
elseif($month==11){ $mnthName='November'; }
elseif($month==12){ $mnthName='December'; }

if($emp_weekly_off==1){ $week='Monday';}
elseif($emp_weekly_off==2){ $week='Tuesday'; }
elseif($emp_weekly_off==3){ $week='Wednesday'; }
elseif($emp_weekly_off==4){ $week='Thursday'; }
elseif($emp_weekly_off==5){ $week='Friday'; }
elseif($emp_weekly_off==6){ $week='Saturday'; }
elseif($emp_weekly_off==7){ $week='Sunday'; }
//echo $week;
$ts = strtotime($mnthName.' '.$years);
$startDate=date('Y-m-01', $ts);
$endDate=date('Y-m-t', $ts);

$startDateNew = strtotime( 'next '.$week, strtotime($startDate) );
$endDateNew=date('Y-m-d', strtotime($endDate. ' + 1 days'));
$endDateNew=strtotime($endDateNew. ' + 1 days');

// Output all Tuesdays prior to our end date
while ($startDateNew < $endDateNew) {

      $weeklypfArray[]= date('Y-m-d', $startDateNew );

    // Get next Tuesday relative to last Tuesday
    $startDateNew = strtotime( 'next '.$week, $startDateNew );
}

//print_r($weeklypfArray);
if($weeklypfArray)
{
    $_SESSION['WEEKLYOFF']=$weeklypfArray;
    $weekOffStr=implode("','", $weeklypfArray);
}
// calculating holiday
$holidaySql="select start_date, end_date from holidays where month(start_date)='".$month."' and published='Y'  
	        and  year(start_date)='".$years."'";
$holidaySqlExec = DB_query($holidaySql,$db);
while($RowData = DB_fetch_array($holidaySqlExec))
{
    $startDates=$RowData['start_date'];
    $endDates=$RowData['end_date'];
    $holidaysArray[]=dateRange($startDates,$endDates);
}

if($holidaysArray)
    {
    $holidayStr=multi_implode($holidaysArray, ',');
    $holidayArray=explode(',', $holidayStr);
    }
    
}
if($holidayArray)
{
    $_SESSION['HOLIDAY']=$holidayArray;
    $holidayStr=implode("','", $holidayArray);
}
$_SESSION['DATES']=$dates=$weekOffStr."','".$holidayStr;

if($_REQUEST['proceed']!='')
{
 	 $s->pageLocation($target."&month=".$_REQUEST['month']."&years=".$_REQUEST['years']."&site=".$_REQUEST['site']."&empcode=".$_REQUEST['employee_code']);
         echo $_REQUEST['empcode'];
}


?>
<link rel="stylesheet" href="css/new_mxerp1.css">
<script src="js/jquery10.js" type="text/javascript"></script>
<script src="js/settings.js" type="text/javascript"></script>
<script>
function check_selectedEmployee()
{
	if(document.getElementById("site").value=='0')
	{
		alert("Please Select The Branch First !");
		return false;
	}
	if(document.getElementById("employee_code").value=='')
	{
		alert("Please Select The Employee First !");
		return false;
	}
	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)
	{
		var chkemp=xmlhttp.responseText;
		if(chkemp=='1')
		{
			alert("You Cannot Proceed As DOJ Is Greater Than Current Month");
			return false;
		}
		else
		{
			document.myform.submit();
		}
	 //return false;
		//document.getElementById("div_id").innerHTML=xmlhttp.responseText;
	}
	}
	var current_month=parseInt(document.getElementById("month").value);
	var current_years=parseInt(document.getElementById("years").value);
	var EmpId=document.getElementById("employee_code").value;
	xmlhttp.open("GET","CheckDateOfJoiningForEmp.php?EMPID="+EmpId+"&CurrSAMonth="+current_month+"&CurrSAYear="+current_years,true);
	xmlhttp.send();
}
</script>
<?php
// WO CALCULATE

?>
<div class="body_contain">
    <div class="tab_div">
       <form action="" method="post" name="myform">
<table width="100%" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td class="tabledata1"><strong>For Month</strong></td>
	<td><select name="month" id="month" style="width:150px;">
	<?php $amonth=array(1=>'jan','feb','march','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec');
	  for($i=1;$i<=count($amonth) ;$i++)
	  {
	   $p='';
	   if($_REQUEST['month']==$i)
	   $p='selected';
	   
	    echo "<option value='".$i."' $p>$amonth[$i]</option>";
	  }
	?></select></td>
	<td class="tabledata1"><strong>Year</strong></td>
	<td><select name="years" id="years" style="width:150px;">
	<?php for($i=date('Y');$i>=2000;$i--){
	  $p='';
	   if($_REQUEST['years']==$i)
	   $p='selected';
	    echo "<option value='".$i."' $p>$i</option>";
	  }?>
	  </select></td>
	<td class="tabledata1"><strong>Branch</strong></td>
	<td><select name="site" id="site" style="width:150px;" onchange="ShowDiv(this.value,'hid_lstemp','EmpCodePrefix.php')">
	<option value="0">Select</option>
	<?php 
	$site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
	while($site_row=DB_fetch_array($site_sql))
	{
	?>
	<option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
<?php }?>
	</select></td>
    <td class="tabledata1"><strong>Employee List</strong></td>
	<td><div id='hid_lstemp'>
	<select name="employee_code" id="employee_code"  style="width:150px;">
		<option value="">Select</option>
	<?php
	$query="Select  * from empmaster where emp_site='".$_REQUEST['site']."' order by emp_name ";	
	$sql_emp=DB_query($query,$db);
	 while($rowEmp=DB_fetch_array($sql_emp)){?>
	<option value="<?php echo $rowEmp['id'] ?>" <?php echo ($_REQUEST['employee_code']==$rowEmp['id']?'selected':'') ?>><?php echo $rowEmp['emp_name'] ?></option>
	 <?php }?>
	</select>
 	</div>
	</td>
	<td><input type="button" name="proceed" class="search" value="proceed" onclick="return check_selectedEmployee()" /></td>
  </tr>
</table>
           <br/>
<?php
if($_REQUEST['employee_code']!='')
{
?>
<table width="100%" cellspacing="0" cellpadding="3" border="1">
    <tr class="row1">
        <td><strong>Sr No.</strong></td>
        <td><strong>Employee Code</strong></td>
        <td><strong>Employee Name</strong></td>
        <td><strong>Created</strong></td>
        <td><strong>Taken</strong></td>
        <td><strong>Pending</strong></td>
       <!-- <td><strong>Action</strong></td>-->
    </tr>
    <?php 
    if($dates)
    {
    $sql="select * from daily_attendance where pres_abs='P' and attendance_date in ('$dates') and employee='".$emp_id."' GROUP BY  employee";
     }
     $i=0;
       $sqlExec = DB_query($sql,$db);
       while($RowData = DB_fetch_array($sqlExec))
       { 
          $sqlNum="select * from daily_attendance where pres_abs='P' and attendance_date in ('$dates') and employee='".$emp_id."'";
          $sqlNumExec = DB_query($sqlNum,$db);
           $created=DB_num_rows($sqlNumExec);
           
           $sqlTaken="select * from daily_attendance where pres_abs='P' and attendance_date in ('$dates') and employee='".$emp_id."' and comp_status='1'";
          $sqlTakenExec = DB_query($sqlTaken,$db);
           $taken=DB_num_rows($sqlTakenExec);
           $pending=$created-$taken;
    ?>
    <tr class="row">
        <td><?php echo ($i+1); ?></td>
        <td><?php echo $RowData['e_code']; ?></td>
        <td><?php echo $s->GetFieldValue('empmaster'," id='".$RowData['employee']."' " ,'emp_name'); ?></td>
        <td><?php echo $created; ?></td>
       <td><?php if($taken==0){ echo $taken;} else { ?><a onclick="popup('comp_apply_view.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $RowData['employee'];?>&mainid=<?php echo $RowData['id'];?>',800,400)" href="#" title="Click To See The Details" style="color:#00CC66;"><?php echo $taken; ?></a><?php } ?></td>
        <td><?php if($pending>0) { ?><a onclick="popup('comp_apply.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $RowData['employee'];?>&mainid=<?php echo $RowData['id'];?>',800,400)" style="color:#FF0000;" title="Apply For Leave" href="#" ><?php echo $pending; ?></a><?php } else { ?><?php echo $pending; ?><?php } ?></td>
        <!--<td><?php if($pending>0) { ?><a onclick="popup('comp_apply.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $RowData['employee'];?>&mainid=<?php echo $RowData['id'];?>',800,400)" href="#" title="Apply For Leave">Apply</a><?php } else { ?>No Leave Left<?php } ?></td>-->
    </tr>
    
       <?php $i++; } ?>
	   
</table>
<?php } ?>
</form> 
    </div>
</div>

