<?php
@session_start();
include_once("../class/class.MySQL.php");
$db	=new MySQL();

$action = $_GET['act'];

if($action == "list"){

$resultdsg = mysql_query("SELECT * FROM designation join department on designation.departs_id =department.depart_id  order by department.depart_name , depart_rank ASC");
$rowsd = mysql_num_rows($resultdsg);

	//$where['is_blocked'] = 0;
	//$leaves  = $db->fetchAll("designation"); 
	

	
		
	if($rowsd > 0)
	{
	$leaves = $db->fetchQuery("SELECT * FROM designation join department on designation.departs_id =department.depart_id  order by department.depart_name , depart_rank ASC");
	}
	else
	{
	$leaves  = $db->fetchAll("designation"); 
	}
	
		$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='Designation' and m.menu_id=s.menu_id "));
	
	$userid=$_SESSION['admin_id'];

	$where['user_id']	= $userid;
	$where['s_submenu_id']	= $sub_rights['s_id'];
	
	if(count($leaves) > 0){
	
	if($userid==1)
	{
	$leaves[count($leaves)-1]['permissions'][0] = array("user_id"=>$userid);
	}
	else
	{
	$leaves[count($leaves)-1]['permissions'] =  $db->fetchAll("tbl_user_rights",$where);
	}
	}
	
	//print_r($leaves);
	echo '{"leaves":'.json_encode($leaves).'}';
	
}elseif($action == "update"){

}elseif($action == "removeded"){

	$where['desig_id']	= $_GET['did'];
	
	$deductions = $db->Delete('designation',$where);
	

}
elseif($action == "insert"){
$resultdsg = mysql_query("SELECT * FROM designation where desig_name='$_POST[designationame]' and departs_id='$_POST[department_name]'");
$rowsd = mysql_num_rows($resultdsg);
if($rowsd > 0)
{

$vars['desig_name']='Already Saved Data';
echo '{"leaves":'.json_encode($vars).'}';
}
else
{

	$arrys = join(',',$_POST['checkadd']); 
	$vars['desig_name'] 	 = $_POST['designationame'];
	$vars['departs_id'] 	 = $_POST['department_name'];
	$vars['depart_sub_name'] = $_POST['sub_department'];
	$vars['depart_rank'] 	 = $_POST['department_rank'];
	$vars['desig_rights']=$arrys;
	$db->Insert($vars,'designation');
	
	$vars['id']		=$db->LastInsertID();
	$dsgid=$db->LastInsertID();
		//$arrys=$_POST['checkadd'];
		
	//foreach($arrys as $item) {}
    $varsd['designation']=$dsgid;
	$varsd['desig_right']=$arrys;
    $db->Insert($varsd,'tbl_designation_right');
    // to know what's in $item
    //echo '<pre>'; var_dump($item);
   
	//Insert role rights
/*	$menu_list=$db->fetchQuery("SELECT * FROM menu order by sort_order");
	foreach($menu_list as $menu_items)
	{
		if(isset($_POST['checkadd'.($menu_items['menu_id']).''))
		{
			//$subvars['desig_name'] 	 = $_POST['designationame'];
			//$db->Insert($vars,'designation');
		}
	}*/
echo '{"leaves":'.json_encode($vars).'}';	
}	
	
	
}
elseif($action == "emp"){

	$where['employee_id']	= $_GET['emp_id'];
	$where['is_blocked']	= 0;
	$leaves = $db->fetchAll('payroll_user_leave',$where);
	 echo '{"leaves":'.json_encode($leaves).'}';
}
elseif($action == "removeded"){
	$where['employee_id']	= $_GET['emp_id'];
	$where['leave_id']	= $_GET['leave_id'];

	$deductions = $db->Delete('payroll_user_leave',$where);

}
elseif($action == "addtoemp"){
	 $vars['employee_id']	= $_GET['emp_id'];
	 $vars['leave_id']	= $_GET['leave_id'];

	$leaves = $db->Insert($vars,'payroll_user_leave');
	
}elseif($action == "newRequest"){
	$vars['employee_id']	= $_GET['emp_id'];
	$vars['day_from']		= date('Y-m-d',strtotime($_POST['day_from']));
	 $vars['day_to']		= date('Y-m-d',strtotime($_POST['day_to']));
	 
	 $day_from = date('Y-m-d',strtotime($_POST['day_from'])); // or your date as well
     $day_to = date('Y-m-d',strtotime($_POST['day_to']));
	// echo "SELECT * FROM payroll_holiday_calendar WHERE MONTH(date)='$day_from' and YEAR(date)='$day_to'";
	  $lastMtnHolidays = $db->fetchQuery("SELECT * FROM payroll_holiday_calendar WHERE MONTH(date)='$day_from' and YEAR(date)='$day_to'");
		 $holidays=array();
		 foreach($lastMtnHolidays as $lastMtnHolidays){
			$holidays[]=$lastMtnHolidays['date'];
		 }
	 
	// print_r($holidays);
     $datediff = $db->getWorkingDays($day_from,$day_to,$holidays);
	 
	$vars['no_of_days']	= $datediff;//floor($datediff/(60*60*24));
	$vars['leave_id']	= $_POST['leave_id'];
	$vars['reason']		= $_POST['reason'];
	
	
	$vars['status']		=1;
	$db->Insert($vars,'payroll_user_leave_requests');

}elseif($action == "getLeaves"){
	$emp_id = $_GET['emp_id'];
	
	$leaves = $db->fetchQuery("SELECT * FROM payroll_user_leave A LEFT JOIN payroll_leave B ON A.leave_id = B.lid WHERE A.employee_id='$emp_id'");

	echo '{"leaves":'.json_encode($leaves).'}';

}elseif($action == "loadRequests"){
	$leaves = $db->fetchQuery("SELECT * FROM payroll_user_leave_requests A 
								LEFT JOIN employee B ON A.employee_id = B.e_id 
								LEFT JOIN payroll_leave C ON A.leave_id = C.lid");

	echo '{"leaves":'.json_encode($leaves).'}';
}elseif($action == "changeStatus"){

	$set['status'] = $_GET['set'];
	$where['id'] = $_GET['reqid'];
	$db->Update('payroll_user_leave_requests',$set,$where);
	
}


?>