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

$action = $_GET['act'];
if($action == "list"){

if(isset($_GET['employ_id']))
{

	$employ_id = $_GET['employ_id'];
	$where['is_blocked']	= 0;
	//$deductions  = $db->fetchAll("payroll_benifits_deductions",$where); 
	$deductions  = $db->fetchQuery("select id,e_id,e_fname,name,overtime_status,over_time,description,unit,deduction_benefit,amount from employee as emp join payroll_benifits_deductions as deduct on emp.e_id = deduct.emp_id where deduct.emp_id='$employ_id' and deduct.is_blocked = 0"); 
	if(count($deductions) == 0)
	{
		$deductions  = $db->fetchQuery("select e_id,overtime_status,over_time from employee as emp where e_id='$employ_id'"); 
	}
	
}
	else
	{
		$where['is_blocked']	= 0;
	//$deductions  = $db->fetchAll("payroll_benifits_deductions",$where); 

	$deductions  = $db->fetchQuery("select id,e_id,e_fname,name,overtime_status,over_time,description,unit,deduction_benefit,amount from employee as emp join payroll_benifits_deductions as deduct on emp.e_id = deduct.emp_id and deduct.is_blocked = 0"); 
	
	}
	echo '{"deductions":'.json_encode($deductions).'}';	
	
}


elseif($action == "remove"){

	$did	= $_GET['did'];
	//$set['is_blocked']			= 1;
	//$db->Update('payroll_benifits_deductions',$set,$where);	
	$db->ExecuteSQL("delete from payroll_benifits_deductions where id = '$did'");
	$db->ExecuteSQL("delete from payroll_user_deductions where deduction_id	 = '$did'");


}

elseif($action == "insert"){
	
	$vers['emp_id'] 		= $_POST['member_id'];
	$vers['name'] 			= $_POST['deductionTitle'];
	$vers['description'] 	= $_POST['deductionDescription'];
	$vers['deduction_benefit']= $_POST['type'];
	$vers['amount']			= $_POST['amount'];
	$vers['unit']			= $_POST['unit'];
	
	$db->Insert($vers,'payroll_benifits_deductions');	
	
	$vars['id']	= $lastid = $db->LastInsertID();
	
	$dedins['employee_id']	= $_POST['member_id'];
	$dedins['deduction_id']	= $lastid;
	$dedins['is_blocked']	= 0;
	
	$db->Insert($dedins,'payroll_user_deductions');	
	
	$memberid = $_POST['member_id'];
	$getemp = $db->fetchQuery("select e_fname,e_lname from employee where e_id ='$memberid'");
	$efname = $getemp[0]['e_fname'];
		
	$vers['emp_name'] = $efname;
	
	echo '{"deduction":'.json_encode($vers).'}';
	
}






elseif($action == "update"){
	$vers['name'] 			= $_POST['deductionTitle'];
	$vers['type']= $_POST['type'];
	$where['id']	= $_POST['deduct_ID'];
	$db->Update('earning_deduction',$vers,$where);
}




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

elseif($action == "removeded"){
	$where['employee_id']	= $_GET['emp_id'];
	$where['deduction_id']	= $_GET['deduction_id'];

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

}

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

	$deductions = $db->Insert($vars,'payroll_user_deductions');
	
}


?>