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




	
	
	//$db->Insert($vars,'tbl_gratuity');
	
	//$vars['id']				=$db->LastInsertID();
	
	//echo '{"leaves":'.json_encode($vars).'}';



        $emps=$_GET['emp'];
		$arr=array();
   $i=1;
	$grats  = $db->fetchQuery("SELECT * FROM  employee where e_id='$emps' order by e_fname asc"); 
	$num_row_emp  = mysql_num_rows(mysql_query("SELECT * FROM  employee where e_id!=1 order by e_fname asc")); 
	
	 $yrdat=date("Y");
	 $tot_grat_amnt=0;
	foreach($grats as $grat)
	{
	
	   //$your_date = strtotime($yrdat."-12-31");
	//$your_date = strtotime($yrdat."-12-31");
	$res_date=date('d-m-Y',strtotime($_GET['dat']));
 $your_date=date('Y-m-d',strtotime($_GET['dat']));
 $your_date = strtotime($your_date);
	//date('y-m-d',strtotime($_GET['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"));
	}
	//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_yr=floor($basic_per_mn * 12);
	$basic_per_day=floor($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;
	
	$where['e_id']	= $emps;
	
	$del_grat = $db->Delete('gratuity_table',$where);
	
	$vars['e_id'] 			= $grat['e_id'];
	$vars['resigned_dat'] 	    = $res_date;
	$vars['days_work']         = $datediff;
	$vars['lop_day']	  		= $lop_dyas_new;
	$vars['total_days']			= $tot_days_work;
	$vars['total_years']       = $num_yrs_wrk;
	$vars['basic_per_yr']			= $basic_per_yr;
	$vars['basic_per_day']			= $basic_per_day;
	$vars['grat_master_day']			= $grat_yer['num_days'];
	$vars['grat_tot_amount']			= $grat_amnt;
			
	$db->Insert($vars,'gratuity_table');
	
	
	  $arr[$i]['lop_day']=$lop_dyas_new; 
	  $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']= $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;
	 
	  
	  	$i++;
	}
  
	echo '{"grats":'.json_encode($arr).'}';


?>