<?php include_once("../class/class.MySQL.php");
$db	=new MySQL();
session_start();
$action = $_GET['act'];

if($action == "newgrat"){
	
	$vars['e_id'] 			= $_POST['empid'];
	$vars['j_date'] 	    = $_POST['jdate'];
	$vars['depart']         = $_POST['depart'];
	$vars['emstatus']	  		= $_POST['estats'];
	$vars['amount']			= $_POST['amounts'];
	$vars['net_amnt']       = $_POST['netamnt'];
	$vars['r_date']			= $_POST['rdate'];
	$vars['desig']			= $_POST['desigs'];
	$vars['basic']			= $_POST['bsal'];
	$vars['deduct']			= $_POST['damount'];
	$vars['remark']			= $_POST['remarks'];		
	$db->Insert($vars,'tbl_gratuity');
	
	$vars['id']				=$db->LastInsertID();
	
	echo '{"leaves":'.json_encode($vars).'}';


}
if($action == "lists"){


//////////////////////////////user rights checking//////////////////////////////////////

$userid=$_SESSION['admin_id'];

$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  m.menu_name='Salary' and m.menu_id=s.menu_id and s.s_menu_name='gratuity'"));

$view_rights=mysql_fetch_array(mysql_query("select * from tbl_user_rights where user_id='$userid' and s_submenu_id='$sub_rights[s_id]'"));


$sub_rights_edit=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  m.menu_name='Salary' and m.menu_id=s.menu_id and s.s_menu_name='gratuity'"));

$view_rights_edit=mysql_fetch_array(mysql_query("select * from tbl_user_rights where user_id='$userid' and s_submenu_id='$sub_rights_edit[s_id]'"));

//////////////////////////////user rights checking//////////////////////////////////////


/*  $arr=array();

	//$grats  = $db->fetchQuery("SELECT * FROM  tbl_gratuity a,employee b  where  a.e_id = b.e_id "); 
	$grats  = $db->fetchQuery("SELECT * FROM  employee where e_id!=1 order by e_fname asc"); 
	
	foreach($grats as $grat)
	{
	mysql_query("SELECT SUM(lossofpay_leaves) as lossofpay_leaves FROM  payroll_salary_pay where userid='$grat[e_id]' ");
	}
  
	echo '{"grats":'.json_encode($grats).'}';*/
	
		//$grats  = $db->fetchQuery("SELECT * FROM  tbl_gratuity a,employee b  where  a.e_id = b.e_id "); 
		$arr=array();
   $i=1;
   if($_SESSION['usertype']=='Superadmin')
   {
	$grats  = $db->fetchQuery("SELECT * FROM  employee where user_type!='Superadmin' order by e_fname asc"); 
	$num_row_emp  = mysql_num_rows(mysql_query("SELECT * FROM  employee where user_type!='Superadmin' order by e_fname asc")); 
	}
	elseif( $_SESSION['branchadmin']=='Y') {
		$grats  = $db->fetchQuery("SELECT * FROM  employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']." order by e_fname asc"); 
		$num_row_emp  = mysql_num_rows(mysql_query("SELECT * FROM  employee where user_type!='Superadmin' and branchid=".$_SESSION['branch_id']." order by e_fname asc")); 
	}
	else
	{
	   if($view_rights['view_right']==0)
	   {
	    $grats  = $db->fetchQuery("SELECT * FROM  employee where e_id='$userid' order by e_fname asc"); 
	    $num_row_emp  = mysql_num_rows(mysql_query("SELECT * FROM  employee where user_type!='Superadmin' order by e_fname asc")); 
		}
		else
		{
		$grats  = $db->fetchQuery("SELECT * FROM  employee where e_id!=1 order by e_fname asc"); 
	    $num_row_emp  = mysql_num_rows(mysql_query("SELECT * FROM  employee where user_type!='Superadmin' order by e_fname asc")); 
		}	
	}
	
	 $yrdat=date("Y");
	 $tot_grat_amnt=0;
	foreach($grats as $grat)
	{
	   $resi_date=date("d-m-Y", strtotime($yrdat."-12-31"));
	   $your_date = strtotime($yrdat."-12-31");
	
     $datediff =  $your_date - strtotime($grat['date_join']);
      $datediff =  floor($datediff/(60*60*24));
	  
	  	$lops=mysql_fetch_array(mysql_query("SELECT SUM(lossofpay_leaves) as lossofpay_leaves FROM  payroll_salary_pay where userid='$grat[e_id]' "));
		
		$sel_pay_id=mysql_fetch_array(mysql_query("SELECT * FROM  payroll_salary_pay where userid='$grat[e_id]' order by pay_period desc "));
		
		
		
		$sel_basic=mysql_fetch_array(mysql_query("SELECT * FROM  payroll_salary_pay_details where payment_id='$sel_pay_id[id]' order by payment_id asc "));
		
		//echo $sel_basic['amount'];
		
	if($lops['lossofpay_leaves']=='')
	{
	$lop_dyas_new=0;
	}
	else
	{
		$lop_dyas_new=$lops['lossofpay_leaves'];
	}
	$tot_days_work=$datediff-$lop_dyas_new;
	
	
	   $num_yrs= floor($tot_days_work/365);
	   
	   $num_yrs_wrk= $tot_days_work/365;
	   
	   $num_yrs_grtfnal=round($num_yrs_wrk,1);
	
	//gratuity year select
	
			if($num_yrs_grtfnal > 5)
	{
	
		$grat_yer=mysql_fetch_array(mysql_query("SELECT  *  FROM  gratuity_setyear where (year_from ='5' || year_to >= '5') order by id desc"));
	}
	else
	{
	$grat_yer=mysql_fetch_array(mysql_query("SELECT  *  FROM  gratuity_setyear where year_from <= '$num_yrs_grtfnal' and year_to >= '$num_yrs_grtfnal' order by id desc"));
	}
	
	//echo "SELECT SUM(lossofpay_leaves) as lossofpay_leaves FROM  payroll_salary_pay where userid='$grat[e_id]' ".'<br />';


	
	//$basic_per_mn=$grat['emp_basic_pay'];
	$basic_per_mn=$sel_basic['amount'];
	$basic_per_yr=round($basic_per_mn * 12);
	$basic_per_day=round($basic_per_yr/365);
	
	$grat_chk_amnt=$basic_per_yr*2;
	
	$grat_amnt=$basic_per_day * $grat_yer['num_days']* $num_yrs_grtfnal;
	if($grat_amnt > $grat_chk_amnt)
	{
	$grat_amnt=$grat_chk_amnt;
	}
	
	
	$grat_record_val=mysql_query("SELECT  *  FROM  gratuity_table where e_id='$grat[e_id]'");
	$grat_num_row=mysql_num_rows($grat_record_val);
	$grat_record_set=mysql_fetch_array($grat_record_val);
	
	if($grat_num_row > 0)
	{
	
	   $your_date = strtotime($grat_record_set['resigned_dat']);
	
     $datediff =  $your_date - strtotime($grat['date_join']);
      $datediff =  floor($datediff/(60*60*24));
	  
	  	$lops=mysql_fetch_array(mysql_query("SELECT SUM(lossofpay_leaves) as lossofpay_leaves FROM  payroll_salary_pay where userid='$grat[e_id]' "));
	if($lops['lossofpay_leaves']=='')
	{
	$lop_dyas_new=0;
	}
	else
	{
		$lop_dyas_new=$lops['lossofpay_leaves'];
	}
	$tot_days_work=$datediff-$lop_dyas_new;
	
	
	   $num_yrs= floor($tot_days_work/365);
	   
	   $num_yrs_wrk= $tot_days_work/365;
	   
	   $num_yrs_grtfnal=round($num_yrs_wrk,1);
	
	//gratuity year select
	
		if($num_yrs_grtfnal > 5)
	{
	
		$grat_yer=mysql_fetch_array(mysql_query("SELECT  *  FROM  gratuity_setyear where (year_from ='5' || year_to >= '5') order by id desc"));
	}
	else
	{
	$grat_yer=mysql_fetch_array(mysql_query("SELECT  *  FROM  gratuity_setyear where year_from <= '$num_yrs_grtfnal' and year_to >= '$num_yrs_grtfnal' order by id desc"));
	}
	
	
	//$basic_per_mn=$grat['emp_basic_pay'];
	$basic_per_mn=$sel_basic['amount'];
	$basic_per_yr=round($basic_per_mn * 12);
	$basic_per_day=round($basic_per_yr/365);
	
	$basic_per_day_grt=round($basic_per_yr/365);
	
	$grat_chk_amnt=$basic_per_yr*2;
	
	$grat_amnt=$basic_per_day * $grat_yer['num_days']* $num_yrs_grtfnal;
	if($grat_amnt > $grat_chk_amnt)
	{
	$grat_amnt=$grat_chk_amnt;
	}
	
	
	
	  $tot_grat_amnt=$grat_amnt+$tot_grat_amnt;
	  $arr[$i]['lop_day']=$lop_dyas_new; 
	  $arr[$i]['resig_dat']=date("d-m-Y", strtotime($grat_record_set['resigned_dat'])); 
	  $arr[$i]['e_fname']= $grat['e_fname'];
	  $arr[$i]['e_id']= $grat['e_id'];
	  $arr[$i]['date_join']= $grat['date_join'];
	  $arr[$i]['emp_basic_pay']=$sel_basic['amount'];//$grat['emp_basic_pay'];
	  $arr[$i]['num_days']= $grat_yer['num_days'];
	  $arr[$i]['datjoin_resi_work']= $grat_record_set['days_work'];
	  $arr[$i]['total_work_dys']=$tot_days_work;
	  $arr[$i]['total_work_yr']= $num_yrs_wrk;
	  $arr[$i]['basic_per_yr']= $basic_per_yr;
	  $arr[$i]['basic_per_day']= $basic_per_day;
	  $arr[$i]['num_yrsss']= $num_yrs;
	  $arr[$i]['num_yrs_grtfnal']= $num_yrs_grtfnal;
	  $arr[$i]['grat_amnt']= $grat_amnt;
	  $arr[$i]['tot_amnt_emp']= $tot_grat_amnt;
	  $arr[$i]['num_row_emp']= $num_row_emp;
	  $arr[$i]['user_id']= $userid;
	  $arr[$i]['edit_riht']= $view_rights_edit['edit_right'];
	}
	else
	{
	$tot_grat_amnt=$grat_amnt+$tot_grat_amnt;
	  $arr[$i]['lop_day']=$lop_dyas_new; 
	  $arr[$i]['resig_dat']=$resi_date; 
	  $arr[$i]['e_fname']= $grat['e_fname'];
	  $arr[$i]['e_id']= $grat['e_id'];
	  $arr[$i]['date_join']= $grat['date_join'];
	  $arr[$i]['emp_basic_pay']=$sel_basic['amount']; //$grat['emp_basic_pay'];
	  $arr[$i]['num_days']= $grat_yer['num_days'];
	  $arr[$i]['datjoin_resi_work']= $datediff;
	  $arr[$i]['total_work_dys']= $tot_days_work;
	  $arr[$i]['total_work_yr']= $num_yrs_wrk;
	  $arr[$i]['basic_per_yr']= $basic_per_yr;
	  $arr[$i]['basic_per_day']= $basic_per_day;
	  $arr[$i]['num_yrsss']= $num_yrs;
	  $arr[$i]['num_yrs_grtfnal']= $num_yrs_grtfnal;
	  $arr[$i]['grat_amnt']= $grat_amnt;
	  $arr[$i]['tot_amnt_emp']= $tot_grat_amnt;
	  $arr[$i]['num_row_emp']= $num_row_emp;
	  $arr[$i]['user_id']= $userid;
	  $arr[$i]['edit_riht']= $view_rights_edit['edit_right'];
	}
	  
	  	$i++;
	}

 // echo $i;
	echo '{"grats":'.json_encode($arr).'}';

}
?>