MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //home/cafsindia/hrms_patroniss_com/application/controllers/Gratuity_view.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Gratuity_view  extends Base_controller{
	
	public function __construct(){
		parent::__construct('gratuity_view');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
			}
		$this->collect_base_info();
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$role_info   = $this->db->query("CALL sp_a_run ('SELECT','SELECT * FROM `cw_category` where trans_status = 1 and prime_category_id !=1')");
		$role_result = $role_info->result();
		$role_info->next_result();
		$process_role[""] = "---- Select Role ----";
		foreach($role_result as $for){
			$role_id       = $for->prime_category_id;
			$category_name = $for->category_name;
			$process_role[$role_id] = $category_name;
		}
		$data['process_role'] = $process_role;
		
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//employee code view
	public function emp_suggest(){
		$search_term  = $this->input->post_get('term');
		$final_qry = 'select employee_code,emp_name from cw_employees where trans_status = 1 and employee_code like "'.$search_term.'%"';
		$final_data   = $this->db->query("CALL sp_a_run ('SELECT','$final_qry')");
		$final_result = $final_data->result();
		$final_data->next_result();
		foreach($final_result as $rslt){
			$employee_code = $rslt->employee_code;
			$emp_name      = $rslt->emp_name;
			$suggestions[] = array('value' => $employee_code, 'label' => "$employee_code - $emp_name");
		}
		if(empty($suggestions)){
			$suggestions[] = array('value' => "0", 'label' => "No data found for this search");
		}
		echo json_encode($suggestions);
	}
	
	//06-12-2019 gratuity;
	public function check_gratuity(){
		$process_type    = $this->input->post('process_type');
		$employee_code   = $this->input->post('process_emp_id');
		$process_role    = $this->input->post('process_role');
		$gratuity_date   = date('Y-m-d' ,strtotime($this->input->post('gratuity_date')));
		
		$employee_query_info   = 'select cw_employees.employee_code,cw_employees.emp_name,cw_employees.date_of_joining,cw_category.category_name as role,prime_category_id from cw_employees join cw_category on cw_category.prime_category_id = cw_employees.role where cw_employees.trans_status = 1 and cw_employees.role != 1 ';
		if((int)$process_type === 1){
			$employee_query_info .= " and cw_employees.employee_code = \"$employee_code\"";
		}else
		if((int)$process_type === 2){
			$employee_query_info .= " and cw_employees.role = $process_role";
		}	
		$employee_query_info   = $this->db->query("CALL sp_a_run ('SELECT','$employee_query_info')");
		$employee_query_rlst   = $employee_query_info->result();
		$employee_query_info->next_result();
		$emp_result = array_map(function($emp){
			$emp_array['employee_data'] = $emp;
			$emp_array['employee_code'] = $emp->employee_code;
			return $emp_array;
		}, $employee_query_rlst);
		$emp_result    = array_column($emp_result,'employee_data','employee_code');
		$employee_code = array_column($emp_result,'employee_code');
		$return_data   = $this->get_gratuity($employee_code,$emp_result,$gratuity_date);
		$tble_line = '';
		foreach($employee_query_rlst as $rlst){
			$employee_code      = $rlst->employee_code;
			$emp_name           = $rlst->emp_name;
			$role               = $rlst->role;
			$role_id            = $rlst->prime_category_id;
			$date_of_joining    = date('d-m-Y' ,strtotime($rlst->date_of_joining));
			$doj                = $rlst->date_of_joining;
			$return_data        = $return_data[$employee_code];
			$tot_year           = $return_data['tot_year'];
			if((int)$tot_year > 5){
				$gratuity_amt       = $return_data['gratuity_amt'];
				$tble_line .= "	<tr>
									<td>$role</td>
									<td>$employee_code</td>
									<td>$emp_name</td>
									<td>$date_of_joining</td>
									<td>$tot_year</td>
									<td>$gratuity_amt</td>
								</tr>";
			}
		}
		if($tble_line === ''){
			$tble_line .= "<tr><td colspan='6'> No Data Found</td>";
		}
		$table_data = "	<h4 class='m-t-0 header-title'><b>Gratuity Information List</b></h4>
						<table class='table table-striped table-bordered' id='gratuity_info'>
						 <thead>
							<tr>
							   <th>Employee Role</th>
							   <th>Employee Code</th>
							   <th>Employee Name</th>
							   <th>DOJ</th>
							   <th>No. of years</th>
							   <th>Gratuity Amount</th>
							</tr>
						 </thead>
						 <tbody>
							$tble_line
						</tbody>
					</table>";
		 
		echo json_encode(array("success" => TRUE,'message' => $table_data));
	}
	
	//get gratuity details
	public function get_gratuity($employee_code,$emp_result,$gratuity_date){
		$employe_code_str = '"'.implode('","',$employee_code).'"';
		//get gratuity settings
		$check_eligibilty_qry    =  'select working_days,number_of_years,pay_days,year_rounding,formula_detail,formula_rounding,category from cw_gratuity where trans_status = 1';
		$check_eligibilty_data   = $this->db->query("CALL sp_a_run ('SELECT','$check_eligibilty_qry')");
		$check_eligibilty_result = $check_eligibilty_data->result();
		$check_eligibilty_data->next_result();
		$eligibilty_result = array_map(function($result){
			$return_data['eligibilty_result']['working_days']      = $result->working_days;
			$return_data['eligibilty_result']['number_of_years']   = $result->number_of_years;
			$return_data['eligibilty_result']['pay_days']          = $result->pay_days;
			$return_data['eligibilty_result']['year_rounding']     = $result->year_rounding;
			$return_data['eligibilty_result']['formula_rounding']  = $result->formula_rounding;
			$return_data['eligibilty_result']['category']          = $result->category;
			$gratuity_formula                                      = $result->formula_detail;
			$gratuity_formula                                      =  str_replace("@","",$gratuity_formula);
			$return_data['eligibilty_result']['gratuity_formula']  = "(".$gratuity_formula.") as gratuity";
			$return_data['eligibilty_result']['gratuity_formula']  = $gratuity_formula;
			$return_data['gratuity_formula']                       = $gratuity_formula;
			$return_data['category']                               = $result->category;
			return $return_data;
		}, $check_eligibilty_result);
		$gratuity_formula     = implode(',',array_column($eligibilty_result,'gratuity_formula'));
		$eligibilty_result    = array_column($eligibilty_result,'eligibilty_result','category');
		$grat_formula_amt_qry =  'select '.$gratuity_formula.',employee_code from cw_transactions where trans_status =1 and employee_code IN ('.$employe_code_str.') group by employee_code order by transactions_month desc';
		$grat_formula_amt_data   = $this->db->query("CALL sp_a_run ('SELECT','$grat_formula_amt_qry')");
		$grat_formula_amt_result = $grat_formula_amt_data->result();
		$grat_formula_amt_data->next_result();
		$grat_amt_result = array_map(function($result){
			$return_data['grat_formula_amt_result'] = $result;
			$return_data['employee_code']           = $result->employee_code;
			return $return_data;
		}, $grat_formula_amt_result);
		$grat_amt_result    = array_column($grat_amt_result,'grat_formula_amt_result','employee_code');
		$return_grt_data   = array();
		foreach($employee_code as $code){
			$emp_rlst           = $emp_result[$code];
			$role_id            = $emp_rlst->prime_category_id;
			$date_of_joining    = date('d-m-Y' ,strtotime($emp_rlst->date_of_joining));
			$doj                = $emp_rlst->date_of_joining;
			$doj         	    = strtotime($doj);
			$last_day    	    = strtotime($gratuity_date);
			$eligib_rslt        = $eligibilty_result[$role_id];
			$year_rounding      = $eligib_rslt['year_rounding'];
			$working_days       = $eligib_rslt['working_days'];
			$number_of_years    = $eligib_rslt['number_of_years'];
			$pay_days           = $eligib_rslt['pay_days'];
			$formula_rounding   = $eligib_rslt['formula_rounding'];
			$gratuity_formula   = $eligib_rslt['gratuity_formula'];
			//find year difference 
			$tot_year  =  (($last_day - $doj)/60/60/24)/365;
			$year_val  = round($tot_year,2);
			$gratuity_list = array();
			$year_rounding_list   = array(1=>"Actual",2=>"Normal",3=>"Greater",4=>"Lesser");
			if((int)$year_rounding === 1){
				$tot_year = $year_val;
			}else
			if((int)$year_rounding === 2){
				$tot_year = round($year_val);
			}else
			if((int)$year_rounding === 3){
				$tot_year = ceil($year_val);
			}else 
			if((int)$year_rounding === 4){
				$tot_year = floor($year_val);
			}
			//gratuity calculations for given formula and employee code
			if($tot_year >= $number_of_years){
				$grat_formula_amt = $grat_amt_result[$code]->$gratuity_formula;
				$gratuity_value   = (($grat_formula_amt * $pay_days)/$working_days) * $tot_year;
				$gratuity_amt     = $this->rounding_amount($gratuity_value,$formula_rounding);
			}else{
				$gratuity_amt = 0;
			}
			$return_grt_data[$code] = array('tot_year' => $tot_year,'gratuity_amt' => $gratuity_amt);
		}			
		return $return_grt_data;
	}
	
	///rounding value for default functions
	public function rounding_amount($result, $round_mode){
		$result       = round($result,2);
		if(($round_mode == 0.5) || ($round_mode == 1)){
			$final_result = round($result/$round_mode, 0)* $round_mode;
		}else
		if($round_mode == '>1'){
			$final_result = ceil($result);
		}else
		if($round_mode == '<1'){
			$final_result = floor($result);
		}else
		if($round_mode == '<0.5'){
			$rslt        = explode('.', $result);
			$int_value   =  $rslt[0];
			$point_value =  $rslt[1];
			if((int)$point_value > 50){
				$final_result = $int_value.'.50';
			}else{
				$final_result = ceil($result);
			}
		}else
		if($round_mode == '>0.5'){
			$rslt        = explode('.', $result);
			$int_value   =  $rslt[0];
			$point_value =  $rslt[1];
			if((int)$point_value >= 50){
				$final_result = round($result);
			}else{
				$final_result = $int_value.'.50';
			}
		}else
		if(((int)$round_mode == 5) || ((int)$round_mode == 10) || ((int)$round_mode == 50) || ((int)$round_mode == 100)){
			$final_result = (ceil($result)% $round_mode === 0) ? ceil($result) : round(($result+ $round_mode/2)/ $round_mode)*$round_mode;
		}else	
		if($round_mode == 0.1){
			$final_result = round($result, 2);
		}
		return $final_result;
	}
}
?>