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/.trash/application.1/controllers/Employment_confirmation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Employment_confirmation  extends Action_controller{	
	public function __construct(){
		parent::__construct('employment_confirmation');
	}
	
	// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
	public function index(){
		//PAGE INFO FUNCTION
		// $this->page_info();
		$data['encKey']        = $this->generateKey();	
		$data['module_sts']    = (int)$this->module_sts;
		$data['quick_link']    = $this->quick_link;
		$data['form_info']     = $this->form_info;

		//TABLE HEAD CREATE STATICALLY BASED ON EMPLOYEE MASTER
		$table_head_rslt       = $this->emp_form_sel_qry();
		$this->table_head      = $table_head_rslt;
		$data['table_head']    = $this->table_head;

		//PICKLIST CREATE FUNCTION
		$this->pick_list_data($table_head_rslt);
		$data['pick_list']     = $this->pick_list;
		// $data['fliter_list']   = $this->fliter_list;
		// $data['freeze_list']   = $this->freeze_list;
		$this->load->view("$this->control_name/manage",$data);
	}

	//FORM SETTING EMPLOYEE MODULE SELECT QUERY GET FUNCTION
	public function emp_form_sel_qry(){
		// cw_form_setting.label_name,cw_form_setting.view_name,cw_form_setting.field_type,cw_form_setting.date_type
		$table_head_qry        = 'select * from cw_form_setting where cw_form_setting.prime_module_id = "employees" and cw_form_setting.label_name in ("prime_employees_id","employee_code","emp_name","date_of_joining","conf_date","conf_sts","ext_period") and cw_form_setting.trans_status = "1" ORDER BY input_for,field_sort asc';
		// ,"department","designation","team","tl_report","manager_report"
		// 
		$table_head_info       = $this->db->query("CALL sp_a_run ('SELECT','$table_head_qry')");
		$table_head_rslt       = $table_head_info->result();
		$table_head_info->next_result();
		return $table_head_rslt;
	}
	//FORM SETTING EMPLOYEE MODULE SELECT QUERY GET FUNCTION

	//VIEW SETTING EMPLOYEE MODULE SELECT QUERY GET FUNCTION
	public function emp_view_sel_qry(){
		$table_head_qry        = 'select DISTINCT cw_form_view_setting.* from cw_form_view_setting where prime_view_module_id = "employees" and cw_form_view_setting.form_view_show = "1" and cw_form_view_setting.trans_status = "1" ORDER BY cw_form_view_setting.form_view_sort asc';
		$table_head_info       = $this->db->query("CALL sp_a_run ('SELECT','$table_head_qry')");
		$table_head_rslt       = $table_head_info->result();
		$table_head_info->next_result();
		return $table_head_rslt;
	}
	//VIEW SETTING EMPLOYEE MODULE SELECT QUERY GET FUNCTION
	//PIKCLIST DATA GET FUNCTION
	public function pick_list_data($table_head_rslt){
		$pro_pick_list        = array();
		foreach($table_head_rslt as $key => $value){
			$field_type       = (int)$value->field_type;
			$table_show       = (int)$value->table_show;
			$search_show      = (int)$value->search_show;
			$input_view_type  = (int)$value->input_view_type;	

			if(($field_type === 5) || ($field_type === 7) || ($field_type === 9)){
				$pro_pick_list[$key]  = $value;
			}
		}
		//PICKLIST CALL FUNCTION
		$this->get_pick_list_qry($pro_pick_list);
	}
	//PIKCLIST DATA GET FUNCTION

	//LOAD TABEL WITH FILTERS
	public function search(){
		$dec_data         = $this->cryptoDecrypt($_POST['Payload']);
		$_POST            = $dec_data['data'];
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Invalid Request..'));
			exit(0);
		}		
		//SEARCH INFO FUNCTION
		$this->search_info();
		$module_sts       = (int)$this->module_sts;
		
		$draw         = $this->input->post('draw');
		$start        = $this->input->post('start');
		$per_page     = $this->input->post('length');
		$order        = $this->input->post('order');
		$order_col    = $this->input->post('columns');
		$search       = $this->input->post('search');
		$column       = $order[0]['column'];
		$order_sor    = $order[0]['dir'];
		$order_col    = $order_col[$column]['data'];
		$search       = trim($search['value']);
		$search_query = str_replace("@SELECT@",$this->select_query,$this->base_query);

		//-------------------------- DR STATIC CODE START ------------------------------------------
		$order_col          = str_replace("prime_employment_confirmation_id","prime_employees_id",$order_col);
		//TABLE HEAD CREATE STATICALLY BASED ON EMPLOYEE MASTER
		$table_head_rslt    = $this->emp_form_sel_qry();
		$this->table_head   = $table_head_rslt;

		//PICKLIST ARRAY GET FUNCTION 
		$this->pick_list_data($table_head_rslt);
		$data['pick_list']  = $this->pick_list;
		//--------------------------- DR STATIC CODE END ------------------------------------------
		$search_result      = array();
		//ADDED BASIC,FILTER,COMMON QUERY HERE 
		$role_condition     = "";
		if($this->role_condition){
			$role_condition = $this->role_condition;
		}
		
		$fliter_query  = "";
		$common_search = "";
		if($search){
			foreach($this->table_head as $setting){
				$prime_form_id      = $setting->prime_form_id;
				$field_type         = (int)$setting->field_type;
				$pick_list          = $setting->pick_list;
				$pick_table         = $setting->pick_table;
				$pick_list_type     = $setting->pick_list_type;
				$input_view_type    = (int)$setting->input_view_type;
				$auto_prime_id      = $setting->auto_prime_id;
				$auto_dispaly_value = $setting->auto_dispaly_value;
				$label_id           = strtolower(str_replace(" ","_",$setting->label_name));
				$field_isdefault    = (int)$setting->field_isdefault;
				if($field_isdefault === 1){
					if(($input_view_type === 1) || ($input_view_type === 2)){
						$search_label = "cw_employees.$label_id";
						$search_val   = "";
						if($field_type === 4){ // having issues in date search
							if(strtotime($search)){
								$search_val = date('Y-m-d',strtotime($search));
								$common_search .= ' or '. $search_label .' like "%'.$search_val.'%"';
							}
						}else
						if(($field_type === 5) || ($field_type === 7) || ($field_type === 9)){
							$result = array_filter($this->pick_list[$label_id]['array_list'] ?? [], function ($item) use ($search) {
								if (stripos($item, $search) !== false) {
									return true;
								}
								return false;
							});
							if($result){
								$pick_key = implode('", "', array_keys($result ?? []) ?? []);
								$common_search .= ' or '. $search_label .' in("'.$pick_key.'")';
							}
						}else{
							$common_search .= ' or '. $search_label .' like "%'.$search.'%"';
						}
					}
				}
			}
			if($common_search){
				$common_search = ltrim($common_search,' or ');
				$common_search = " and ($common_search)";
				$common_search = str_replace("(,","(",$common_search);
				$common_search = str_replace("()","(0)",$common_search);
			}
		}
		//GET DATE FORMATION FOR CHECK IN QUERY
		$today_date         = date("Y-m-d");
		$pre_mon_date       = date("Y-m-d",strtotime("-1 month",strtotime($today_date)));
		$nxt_mon_date       = date("Y-m-d",strtotime("+1 month",strtotime($today_date)));
	
		$total_count        = 0;
		$filtered_count     = 0;

		//ALL COUNT SELECT QUERY

		// cw_employees.conf_date >= \"$pre_mon_date\" and cw_employees.conf_date <= \"$today_date\" 
		$count_all_query    = "SELECT count(*) as allcount FROM cw_employees where cw_employees.conf_date >= \"$today_date\" and cw_employees.conf_date <= \"$nxt_mon_date\" and cw_employees.conf_sts = 1 and cw_employees.termination_status = 0 and cw_employees.prime_employees_id != 1 and cw_employees.trans_status = 1 $role_condition $fliter_query $common_search";

		//DATA GET SELECT QUERY
		$search_query       = "SELECT * FROM cw_employees where cw_employees.conf_date >= \"$today_date\" and cw_employees.conf_date <= \"$nxt_mon_date\" and cw_employees.conf_sts = 1 and cw_employees.termination_status = 0 and cw_employees.prime_employees_id != 1 and cw_employees.trans_status = 1 $role_condition $fliter_query $common_search";
		$search_query      .= " ORDER BY $order_col $order_sor";
		if((int)$per_page !== -1){
			$search_query  .= " LIMIT  $start,$per_page";
		}		
		$search_pro_qry     = [];
		$search_pro_qry[]   = array("return"=>"total_count","qry"=>$count_all_query);
		$search_pro_qry[]   = array("return"=>"search_result","qry"=>$search_query);

		$search_info_rslt   = $this->run_multi_qry($search_pro_qry);
		$total_count        = $search_info_rslt->rslt->total_count[0]->allcount;
		$filtered_count     = $total_count;
		
		$search_result      = json_decode(json_encode($search_info_rslt->rslt->search_result),true);

		if($search_result === null || $search_result === ''){
			$search_result  = [];
		}

		echo json_encode(array("draw" => intval($draw),"recordsTotal" => $total_count,"recordsFiltered" => $filtered_count,"data" => $search_result));	
	}
	
	//LOAD MODEL PAGE VIEW WITH DATA
	public function view($form_view_id=-1){
		$data['primeId']     = $form_view_id;
		if($form_view_id !== -1){
			//Decrypt prime id from URL
			$decRslt          = $this->cryptoDecrypt(base64_decode(urldecode($form_view_id)));
			$form_view_id     = $decRslt['prime_id'];
			if(!$form_view_id){
				echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
				exit(0);
			}
		}
		$data['encKey']       = $this->generateKey();
		//VIEW INFO FUNCTION
		// $this->view_info($form_view_id);
		//VIEW, FORM INPUT
		//-------------------------- DR STATIC CODE START ------------------------------------------
		//TABLE HEAD CREATE STATICALLY BASED ON EMPLOYEE MASTER
		
		//VIEW INFO QUERY GET FUNCTION
		$table_view_rslt   = $this->emp_view_sel_qry();
		$this->view_info   = $table_view_rslt;

		//FORM INFO QUERY GET FUNCTION
		$table_head_rslt   = $this->emp_form_sel_qry();
		$this->form_info   = $table_head_rslt;

		//PICKLIST ARRAY GET FUNCTION 
		$this->pick_list_data($table_head_rslt);
		$data['pick_list']  = $this->pick_list;
		$data['view_info']              = $this->view_info;
		$data['form_info']              = $this->form_info;
		$data['all_pick']               = $this->pick_list;//all pick result

		//SELECT EMPLOYEE LABEL NAME
		// $base_query          = str_replace("@SELECT@",$this->view_select,$this->base_query);
		$sel_label_arr       = array_column($this->form_info,"label_name");
		$sel_label_col       = implode(",",$sel_label_arr);

		$form_view_qry       = 'SELECT prime_employees_id,'.$sel_label_col.' from cw_employees where cw_employees.prime_employees_id = '.$form_view_id.' and cw_employees.trans_status = 1';		
		
		$form_view_info      = $this->db->query("CALL sp_a_run ('SELECT','$form_view_qry')");
		$form_view_rslt      = $form_view_info->result();
		$form_view_info->next_result();

		$data['form_view']   = $form_view_rslt[0];
		//--------------------------- DR STATIC CODE END ------------------------------------------
		$this->load->view("$this->control_name/form",$data);
	}
	
	//SAVE MODEL DATA TO DATA BASE
	public function save(){
		//Encryption
		$encString      = file_get_contents('php://input');
		$_POST          = $this->cryptoDecrypt($encString);
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
			exit(0);
		}
		//SAVE INFO FUNCTION
		$unq_chk         = array();
		$prime_qry_key   = "";
		$prime_qry_value = "";
		$prime_upd_query = "";
		$form_id         = $this->input->post("prime_employees_id");
		if($form_id !== '-1'){ //Decryption
			$decRslt     = $this->cryptoDecrypt(base64_decode(urldecode($form_id)));
			$form_id     = (int)$decRslt['prime_id'];
			if(!$form_id){
				echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
				exit(0);
			}
		}else{
			$form_id = 0;
		}
		//FORM INFO QUERY GET FUNCTION
		$table_head_rslt   = $this->emp_form_sel_qry();
		$this->form_info   = $table_head_rslt;

		foreach($this->form_info as $setting){
			$field_type      = $setting->field_type;
			$input_view_type = (int)$setting->input_view_type;
			$label_id        = strtolower(str_replace(" ","_",$setting->label_name));
			$field_isdefault = $setting->field_isdefault;
			$unique_field    = (int)$setting->unique_field;
			$view_name       = $setting->view_name;
			$date_type       = $setting->date_type;
			
			if((int)$field_type === 5){
				$value = trim($this->input->post($label_id));
				$pick_array[$label_id]  = $value;
			}else	
			if((int)$field_type === 7){
				$multi_name = $label_id."[]";
				$value = trim(implode(",",$this->input->post($multi_name) ?? []));
			}else{
				$value = trim($this->input->post($label_id));
			}
			
			if((int)$field_type === 4){
				if((int)$date_type === 1){
					$value = date('Y-m-d',strtotime($value));
				}else{
					$value = $value;
				}
			}else
			if((int)$field_type === 13){
				$value = date('Y-m-d H:i:s',strtotime($value));
			}
			
			if(($input_view_type === 1) || ($input_view_type === 2)){
				// && ($label_id === "conf_date" || $label_id === "conf_sts")
				if((int)$field_isdefault === 1){
					if($label_id === "employee_code"){
						$employee_code   = $value;
					}
					if($label_id === "conf_date"){
						$conf_date       = $value;
					}
					if($label_id === "conf_sts"){
						$conf_sts        = (int)$value;
					}
					if($label_id === "ext_period"){
						$ext_prd         = (int)$value;
					}
				}
			}
		}
		$created_on      = date("Y-m-d h:i:s");
		$today           = date("Y-m-d");

		//INSERT AND UPDATE QUERY CREATION
		if($conf_sts === 2){
			$prime_qry_key     = "employee_code,conf_sts,ext_period,conf_date,";
			$prime_qry_value   = '"'.$employee_code.'","'.$conf_sts.'","'.$ext_prd.'","'.$conf_date.'",';
			$prime_upd_query   = 'conf_sts = "'.$conf_sts.'",';
		}else
		if($conf_sts === 3){
			$ext_prd_qry      = 'SELECT prime_extend_period_id,extend_period from cw_extend_period WHERE cw_extend_period.trans_status = 1 ';
			$ext_prd_info     = $this->db->query("CALL sp_a_run ('SELECT','$ext_prd_qry')");
			$ext_prd_rlst     = $ext_prd_info->result_array();
			$ext_prd_info->next_result();
			$ext_prd_arr      = array_column($ext_prd_rlst,"extend_period","prime_extend_period_id");
			$ext_prd_mon      = $ext_prd_arr[$ext_prd];
			if($ext_prd_mon){
				$conf_date    = date("Y-m-d",strtotime("+ $ext_prd_mon",strtotime($conf_date)));
			}

			$prime_qry_key    = "employee_code,conf_sts,ext_period,conf_date,";
			$prime_qry_value  = '"'.$employee_code.'","'.$conf_sts.'","'.$ext_prd.'","'.$conf_date.'",';
			$prime_upd_query  = 'conf_sts = "'.$conf_sts.'",ext_period = "'.$ext_prd.'",conf_date = "'.$conf_date.'",';
		}
		//CONFIRMSTION STATUS BASED VALIDATION
		if($conf_sts === 1){
			echo json_encode(array('success' => FALSE, 'message' => "Please Choose Confiramtion Status is Cnfirm or Extend.!"));
			exit(0);	
		}else{
			$prime_upd_query  .= 'trans_updated_by = "'.$this->logged_id.'",trans_updated_date = "'.$created_on.'"';
			$emp_upd_query     = 'UPDATE cw_employees SET '.$prime_upd_query.' WHERE prime_employees_id = "'. $form_id.'"';
			$emp_upd_rslt      = $this->db->query("CALL sp_a_run ('UPDATE','$emp_upd_query')");

			//INSERT A EMPLOYEE CONFIRAMTION DETAILS IN EMPLOYEE CONFIRMATION TABLE 
			if($emp_upd_rslt && $form_id){
				$prime_qry_key     .= "prime_employees_id,trans_created_by,trans_created_date";
				$prime_qry_value   .= '"'.$form_id.'","'.$this->logged_id.'","'.$created_on.'"';
				$prime_ins_query    = "insert into $this->prime_table ($prime_qry_key) values ($prime_qry_value)";
				$insert_info        = $this->db->query("CALL sp_a_run ('INSERT','$prime_ins_query')");
				$insert_result      = $insert_info->result();
				$insert_info->next_result();
				$insert_id          = (int)$insert_result[0]->ins_id;	
				if($insert_id){
					echo json_encode(array('success' => TRUE, 'message' => "Successfully Updated.!",'insert_id' => $form_id));
				}
			}	
		}
	}
	
	//UPDATE STATUS TO DELETE IN MODULE PRIMARY TABLE
	public function delete(){
		//Encryption
		$encString      = file_get_contents('php://input');
		$_POST          = $this->cryptoDecrypt($encString);
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
			exit(0);
		}
		$delete_ids    = implode(",",$this->input->post('delete_ids') ?? []);
		$can_process   = TRUE;
		$delete_status = FALSE;
		if($this->check_delete_status()){
			$delete_status = TRUE;
			$check_table_query  = 'SELECT GROUP_CONCAT(prime_module_id) as prime_module_id,GROUP_CONCAT(label_name) as label_name from cw_form_setting WHERE pick_table = "'. $this->prime_table .'" and  trans_status = 1 ';
			$check_table_info   = $this->db->query("CALL sp_a_run ('SELECT','$check_table_query')");
			$check_table_rlst   = $check_table_info->row();
			$check_table_info->next_result();
			if($check_table_rlst->prime_module_id){
				$prime_module_id         = explode(",",$check_table_rlst->prime_module_id);
				$label_name              = explode(",",$check_table_rlst->label_name);
				$i                       = 0;
				foreach($prime_module_id as $check_modules){
					$table_name          = "cw_".$check_modules;
					$select_table        = "$table_name.$label_name[$i]";
					$select_where        = " and $table_name.$label_name[$i] in ($delete_ids)";
					$check_module_query  = 'SELECT '.$select_table.' from '.$table_name.' WHERE '.$table_name.'.trans_status = 1 '.$select_where.' LIMIT 0,1'; 
					$check_module_info   = $this->db->query("CALL sp_a_run ('SELECT','$check_module_query')");
					$values_count        = $check_module_info->num_rows();
					$check_module_info->next_result();
					if((int)$values_count > 0){
						$table_names  .= "$check_modules,";
						$can_process   = False;
						$delete_status = False;
					}
					$i++;
				}				
			}
			$table_names = rtrim($table_names,",");
			if($delete_status){
				$delete_query  = 'DELETE FROM '. $this->prime_table .'  WHERE '. $this->prime_id .' in ('. $delete_ids .')';
				if($this->db->query("CALL sp_a_run ('RUN','$delete_query')")){
					$row_set_query   = 'SELECT form_view_label_name from cw_form_view_setting where form_view_type = "3" and prime_view_module_id = "'. $this->control_name .'" and trans_status = 1';
					$row_set_info    = $this->db->query("CALL sp_a_run ('SELECT','$row_set_query')");
					$row_count       = (int)$row_set_info->num_rows();
					$row_set_info->next_result();
					if($row_count !== 0){
						$row_set_result         = $row_set_info->result();
						$delete_table_name      = '';
						$delete_table_condition = '';
						foreach($row_set_result as $row_set){
							$row_set_table_name      = "cw_".$this->control_name."_".$row_set->form_view_label_name;
							$delete_table_name      .= "$row_set_table_name,";
							$delete_table_condition .= " $row_set_table_name.$this->prime_id  in ('$delete_ids') and";
						}
						$delete_table_name           = rtrim($delete_table_name,',');
						$delete_table_condition      = rtrim($delete_table_condition,'and');
						$delete_row_set_query  = 'DELETE FROM '. $delete_table_name .'  WHERE '. $delete_table_condition.'';
						$this->db->query("CALL sp_a_run ('RUN','$delete_row_set_query')");						
					}
					$can_process = False;
				}
				
			}
		}
		if($can_process){
			$created_on = date("Y-m-d h:i:s");
			$prime_upd_query    .= 'trans_deleted_by = "'. $this->logged_id .'",trans_deleted_date = "'.$created_on.'"';
			$prime_update_query  = 'UPDATE '. $this->prime_table .' SET trans_status = 0,'. $prime_upd_query .' WHERE '. $this->prime_id .' in ('. $delete_ids .')';
			if($this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')")){
				echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Unable to delete"));
			}
		}else
		if($delete_status){
			echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to delete, This value is already used in $table_names modules"));
		}
	}
	
	//CHECK UNIQUE FIELD STATUS
	public function check_delete_status(){
		$check_delete_query  = 'SELECT GROUP_CONCAT(unique_field) as unique_field from cw_form_setting WHERE prime_module_id = "'. $this->control_name .'" and  trans_status = 1 ';
		$check_delete_info   = $this->db->query("CALL sp_a_run ('SELECT','$check_delete_query')");
		$check_delete_rlst   = $check_delete_info->row();
		$check_delete_info->next_result();
		$unique_info         = explode(",",$check_delete_rlst->unique_field);
		if(in_array('1', $unique_info ?? [])){
			return TRUE;
		}else{
			return FALSE;
		}
	}

}
?>