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/ntc_cafsinfotech_in_bk/application/controllers/Risk_and_hazard_management.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Risk_and_hazard_management  extends Action_controller{	
	public function __construct(){
		parent::__construct('risk_and_hazard_management');
		$this->collect_base_info();
	}
	
	// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
	public function index(){
		$data['quick_link']    = $this->quick_link;
		$data['table_head']    = $this->table_head;
		$data['master_pick']   = $this->master_pick;
		$data['fliter_list']   = $this->fliter_list;
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//LOAD TABEL WITH FILTERS
	public function search(){
		$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'];
		$start_date   = str_replace("/","-",$this->input->post('start_date'));
		$end_date     = str_replace("/","-",$this->input->post('end_date'));
		$from_date    = date('Y-m-d',strtotime($start_date));
		$to_date      = date('Y-m-d',strtotime($end_date));
		$search       = trim($search['value']);
		$search_query = str_replace("@SELECT@",$this->select_query,$this->base_query);
		//ADDED BASIC,FILTER,COMMON QUERY HERE 
		$role_condition   = "";
		if($this->role_condition){
			$role_condition = $this->role_condition;
		}
		$fliter_query = "";
		foreach($this->fliter_list as $fliter){
			$label_id         = $fliter['label_id'];
			$label_name       = $fliter['label_name'];
			$field_isdefault  = (int)$fliter['field_isdefault'];
			$array_list       = $fliter['array_list'];
			$field_type       = (int)$fliter['field_type'];			
			if($field_isdefault === 1){
				$column_name = $this->prime_table .".$label_id";				
				$search_val  = $this->input->post("$label_id");
				if($search_val){
					if($field_type === 4){
						$search_val = date('Y-m-d',strtotime($search_val));
						$fliter_query .= " and $column_name = '$search_val'";
					}else
					if(($field_type === 5) || ($field_type === 7)){
						$search_val    = trim(implode(",",$search_val));
						$fliter_query .= " and $column_name in ($search_val)";  
					}else
					if($field_type === 13){
						$search_val = date('Y-m-d H:i:s',strtotime($search_val));
						$fliter_query .= " and $column_name = '$search_val'";
					}else{
						$fliter_query .= " and $column_name LIKE '$search_val%'";
					}
				}
			}
		}
		
		$common_search = "";
		if($search){
			foreach($this->form_info 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 = "$this->prime_table.$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->master_pick[$label_id], 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.')';
							}
						}
					}
				}
			}
		}
		if($common_search){
			$common_search = ltrim($common_search,' or ');
			$common_search = " and ($common_search)";
		}
		if($from_date != '1970-01-01' && $to_date != '1970-01-01'){
			$common_search .= ' and DATE_FORMAT(cw_risk_and_hazard_management.date, "%Y-%m-%d") BETWEEN \''.$from_date.'\' AND \''.$to_date.'\'';
		}
		$count_all_query    = str_replace("@SELECT@","count(*) as allcount",$this->base_query);		
		$search_total       = $this->db->query($count_all_query);
		$search_total_info  = $search_total->result();
		$total_count        = $search_total_info[0]->allcount;
		
		$count_query        = str_replace("@SELECT@","count(*) as allcount",$this->base_query);
		$count_query       .= " where $this->prime_table.trans_status = 1 $role_condition $fliter_query $common_search";
		$search_count       = $this->db->query($count_query);
		$search_info        = $search_count->result();
		$filtered_count     = $search_info[0]->allcount;
		
		$search_query      .= " where $this->prime_table.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_data        = $this->db->query($search_query);
		$search_result      = $search_data->result();
		//echo "search_query :: \n$search_query\n";		
		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){
		//VIEW, FORM INPUT
		$data['view_info']      = $this->view_info;
		$data['form_info']      = $this->form_info;	
		
		//VIEW DATA
		$base_query  = str_replace("@SELECT@",$this->view_select,$this->base_query);
		$view_query  = $base_query ." where $this->prime_table.$this->prime_id = $form_view_id and $this->prime_table.trans_status = 1";
		$view_data   = $this->db->query("CALL sp_a_run ('SELECT','$view_query')");
		$view_result = $view_data->result();
		$view_data->next_result();
		$data['form_view']   = $view_result[0];	
		
		//AUTO COMPLTE,PICK LIST AND CONDITION
		foreach($this->form_info as $from){
			$prime_form_id      = (int)$from->prime_form_id;
			$field_type         = (int)$from->field_type;
			$pick_table         = $from->pick_table;
			$auto_prime_id      = $from->auto_prime_id;
			$auto_dispaly_value = $from->auto_dispaly_value;
			$label_id           = $from->label_name;
			if($field_type === 9){
				if($view_result[0]){
					$get_value = $view_result[0]->$label_id;
					if($get_value){
						$pick_query = 'select '.$auto_dispaly_value.' from '.$pick_table.' where '.$auto_prime_id.' = "'.$get_value.'" and trans_status = 1';
						$pick_data   = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
						$pick_result = $pick_data->result();
						$pick_data->next_result();
						$this->all_pick[$prime_form_id] = $pick_result[0]->$auto_dispaly_value;
					}					
				}
			}
		}
		$data['all_pick']       = $this->all_pick;
		$data['condition_list'] = $this->condition_list;
		
		$view_qry    = 'select * from cw_form_view_setting where  prime_view_module_id = "'.$this->control_name.'" and  form_view_type = "3" and trans_status = 1';
		$view_data   = $this->db->query("CALL sp_a_run ('SELECT','$view_qry')");
		$view_result = $view_data->result();
		$view_data->next_result();
		$row_view_list = array();
		foreach($view_result as $view){
			$prime_form_view_id   = $view->prime_form_view_id;
			$row_set_data = $this->get_row_set_data($prime_form_view_id,$form_view_id);
			$row_view_list[$prime_form_view_id] = $row_set_data;
		}
		$data['row_view_list']   = $row_view_list;
		
		$this->load->view("$this->control_name/form",$data);
	}
	
	//SAVE MODEL DATA TO DATA BASE
	public function save(){
		$unq_chk             = array();
		$prime_qry_key       = "";
		$prime_qry_value     = "";
		$prime_sts_qry_key   = "";
		$prime_sts_qry_value = "";
		$prime_upd_query     = "";
		$prime_sts_upd_query = "";
		$cf_qry_key          = "";
		$cf_qry_value        = "";
		$cf_upd_query        = "";	
		$cf_has              = false;
		$form_id             = (int)$this->input->post($this->prime_id);
		$form_post_data      = array();	
		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;
			if($label_id !== 'risk_no' && $label_id !== 'action_status'){
				if($label_id === 'risk_status'){
					$risk_status = trim($this->input->post($label_id));
				}				
				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){
					$value = date('Y-m-d',strtotime($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)){
					if((int)$field_isdefault === 1){
						$prime_qry_key     .= $label_id.",";
						$prime_qry_value   .= '"'.$value.'",';
						$prime_upd_query   .= $label_id.' = "'.$value.'",';
						if($unique_field === 1){
							$prime_unq_chk = $label_id.'= "'.$value.'"';
							$query = "select count(*) as rslt_count from $this->prime_table where $this->prime_id != $form_id and $prime_unq_chk";
							$unq_chk[] = array('label_id'=>$label_id,'view_name'=>$view_name,'query'=>$query,);
						}
					}
				}
			}
		}
		$rslt_count = 0;
		$can_process = array();
		foreach($unq_chk as $unq_rslt){
			$query       = $unq_rslt['query'];
			$label_id    = $unq_rslt['label_id'];
			$view_name   = $unq_rslt['view_name'];
			$unq_info    = $this->db->query("CALL sp_a_run ('RUN','$query')");
			$unq_result  = $unq_info->result();
			$unq_info->next_result();
			if($unq_result){
				$rslt_count = (int)$unq_result[0]->rslt_count;
				if($rslt_count !== 0){
					$can_process[] = $view_name." already exist";
				}
			}
		}
		if(count($can_process) > 0){
			$can_process  = array_values($can_process);			
			$can_process  = implode(",<br/>", $can_process);
			echo json_encode(array('success' => false, 'message' => $can_process,));
		}else{
			$created_on = date("Y-m-d h:i:s");
			if((int)$form_id === 0){
				$risk_no            = $this->get_risk_no();
				$prime_qry_key     .= "risk_no,trans_created_by,trans_created_date";
				$prime_qry_value   .= '"'.$risk_no.'","'.$this->logged_id.'",'.'"'.$created_on.'"';
				if((int)$risk_status === 2){
					$prime_qry_key     .= ",action_status";
					$prime_qry_value   .= ',1';
				}
				$prime_insert_query = "insert into $this->prime_table ($prime_qry_key) values ($prime_qry_value)";
				$insert_info        = $this->db->query("CALL sp_a_run ('INSERT','$prime_insert_query')");
				$insert_result      = $insert_info->result();
				$insert_info->next_result();
				$insert_id = $insert_result[0]->ins_id;	
				$this->update_risk_status($risk_status,$insert_id);		
				echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id));
			}else{
				if($this->check_action_status($form_id)){
					$prime_upd_query    .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
					if((int)$risk_status === 2){
						$prime_upd_query     .= ",action_status = 1";
					}
					$prime_update_query  = 'UPDATE '. $this->prime_table .' SET '. $prime_upd_query .' WHERE '. $this->prime_id .' = "'. $form_id .'"';
					$this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')");
					$this->update_risk_status($risk_status,$form_id);
					echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'insert_id' => $form_id));
				}else{
					echo json_encode(array('success' => FALSE, 'message' => "Action Management On Process",'insert_id' => $form_id));
				}
			}
		}
	}
	
	//UPDATE STATUS TO DELETE IN MODULE PRIMARY TABLE
	public function delete(){
		$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;
				$select_table            = '';
				$select_label            = '';
				$select_trans_status     = '';
				$select_where            = '';
				foreach($prime_module_id as $check_modules){
					$table_name            = "cw_".$check_modules;
					$table_rename          = $table_name."_$i";
					$select_table         .= "$table_rename.$label_name[$i],";
					$select_label         .= " $table_name $table_rename,";
					if((int)$i === 0){
						$select_trans_status  .= "( $table_rename.trans_status = 1";
						$select_where         .= " and ($table_rename.$label_name[$i] in ($delete_ids)";
					}else{
						$select_trans_status  .= " and $table_rename.trans_status = 1";
						$select_where         .= " or $table_rename.$label_name[$i] in ($delete_ids)";
					}
					$i++;
				}
				$select_trans_status .= ")";
				$select_where        .= ")";
				$select_table         = rtrim($select_table,',');
				$select_label         = rtrim($select_label,',');
				$check_module_query  .= 'SELECT '.$select_table.' from '.$select_label.' WHERE '.$select_trans_status.' '.$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){
					$can_process   = False;
					$delete_status = False;
				}
			}
			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')")){
					$prime_table_query   = 'SELECT * from '. $this->prime_table .'';
					$prime_table_info    = $this->db->query("CALL sp_a_run ('SELECT','$prime_table_query')");
					$row_count           = $prime_table_info->num_rows();
					$prime_table_info->next_result();
					$alter_query         = 'ALTER TABLE '. $this->prime_table .' AUTO_INCREMENT = '. $row_count .'';
					$this->db->query("CALL sp_a_run ('RUN','$alter_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{
			$modules = ucwords($check_table_rlst->prime_module_id);
			echo json_encode(array('success' => FALSE, 'message' => "Unable to delete, This value is already used in $modules 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;
		}
	}
	
	//UPDATE STATUS TO DELETE FOR UPLOAD FILES or DOCUMENTS
	public function remove_file(){
		$prime_id_val  = $this->input->post('prime_id_val');
		$is_defult     = (int)$this->input->post('is_defult');
		$input_name     = $this->input->post('input_name');
		$table_name = '';
		if($is_defult === 1){
			$table_name = $this->prime_table;
		}else
		if($is_defult === 2){
			$table_name = $this->cf_table;
		}
		if($table_name){
			$created_on    = date("Y-m-d h:i:s");
			$set_query     = $input_name .' = "" ,trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
			$update_query  = 'UPDATE '.$table_name .' SET '. $set_query .' WHERE '. $this->prime_id .' = "'. $prime_id_val .'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			echo json_encode(array('success' => TRUE, 'message' => "Successfully updated"));
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to process your request"));
		}
	}
	
	//IMPORT FILE VIEW INFORMATION
	public function import(){
		$data['module_id']     = $this->control_name;		
		$excel_format_qry = 'select prime_excel_format_id,excel_name from cw_util_excel_format where excel_module_id = "'.$this->control_name.'" and trans_status = 1';
		$excel_format   = $this->db->query("CALL sp_a_run ('SELECT','$excel_format_qry')");
		$excel_result    = $excel_format->result();
		$excel_format->next_result();
		$excel_format_drop[""] = "---- Excel Format ----";
		foreach($excel_result as $excel){
			$prime_excel_format_id = $excel->prime_excel_format_id;
			$excel_name            = $excel->excel_name;
			$excel_format_drop[$prime_excel_format_id] = $excel_name;
		}
		$data['excel_format_drop'] = $excel_format_drop;
		
		$this->load->view("$this->control_name/import",$data);
	}
	
	//UPDATE RISK IN ACTION MANAGEMENT
	public function update_risk_status($risk_status,$prime_id = -1){
		if((int)$risk_status === 2){
			$exist_qry = 'SELECT cw_risk_and_hazard_management.prime_risk_and_hazard_management_id,cw_risk_and_hazard_management.risk_no,cw_risk_and_hazard_management.risk_owner,cw_risk_and_hazard_management.date,cw_risk_and_hazard_management.risk_description,cw_risk_and_hazard_management.phone_number,cw_risk_and_hazard_management.email_address,cw_risk_and_hazard_management.address from cw_risk_and_hazard_management join cw_action_management on cw_action_management.risk_incident_car_no = cw_risk_and_hazard_management.risk_no where prime_risk_and_hazard_management_id = "'.$prime_id.'" and cw_risk_and_hazard_management.trans_status = 1';
			$exist_data   = $this->db->query("CALL sp_a_run ('SELECT','$exist_qry')");
			$exist_count  = $exist_data->num_rows();		
			$exist_data->next_result();
			$action_category = 1;
			$created_on      = date("Y-m-d h:i:s");
			$logged_id       = $this->logged_id;
			if((int)$exist_count === 0){
				$insert_query = "INSERT INTO cw_action_management(action_category,risk_incident_car_no,employee_name,phone_number,email,address,trans_created_by,trans_created_date,action_creation_date,action_status,description,risk_incident_car_date) SELECT \"$action_category\",risk_no,risk_owner,phone_number,email_address,address,\"$logged_id\",\"$created_on\",\"$created_on\",1,risk_description,date FROM cw_risk_and_hazard_management WHERE cw_risk_and_hazard_management.prime_risk_and_hazard_management_id = '\"$prime_id\"'";
				$insert_info         = $this->db->query("CALL sp_a_run ('RUN','$insert_query')");
				$insert_info->next_result();
			}else{
				$exist_result     = $exist_data->row();	
				$risk_no          = $exist_result->risk_no;
				$risk_owner       = $exist_result->risk_owner;
				$phone_number     = $exist_result->phone_number;
				$email_address    = $exist_result->email_address;
				$address          = $exist_result->address;
				$risk_description = $exist_result->risk_description;
				$date             = date('Y-m-d',strtotime($exist_result->date));
				$update_query = "UPDATE cw_action_management SET cw_action_management.employee_name = \'$risk_owner\',cw_action_management.phone_number = \'$phone_number\',cw_action_management.email = \'$email_address\',cw_action_management.address = \'$address\',cw_action_management.action_category = \'$action_category\',cw_action_management.action_status = 1,cw_action_management.description = \'$risk_description\',cw_action_management.risk_incident_car_date = \'$date\',cw_action_management.trans_updated_by = \'$logged_id\',cw_action_management.trans_updated_date = \'$created_on\' WHERE  cw_action_management.risk_incident_car_no = \'$risk_no\'";
				$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			}
		}
	}
	
	//GET EMPLOYEE INFORMATION
	public function get_emp_info(){
		$risk_owner  = $this->input->post('risk_owner');
		$this->db->select('residential_address as address,mobile_number as phone_number,email_id as email_address');
		$this->db->from('cw_employees');
		$this->db->where('cw_employees.trans_status', 1);
		$this->db->where('cw_employees.prime_employees_id ', $risk_owner);
		$emp_info     = $this->db->get()->result();
		echo json_encode(array('success'=>true,'message'=>$emp_info));
	}
	
	//GET RATING AND MATRIX UI
	public function get_rate_matrix_ui(){
		$consequence                 = $this->input->post('consequence');
		$consequence_rating          = $this->input->post('consequence_rating');
		$likelihood_rating           = $this->input->post('likelihood_rating');
		$likelihood_impact           = $this->input->post('likelihood_impact');
		$consequence_rating          = '';
		$likelihood_rating           = '';
		$prime_consequence_rating_id = '';
		$prime_likelihood_rating_id  = '';
		if($consequence){
			$this->db->select('consequence_rating,prime_consequence_rating_id');
			$this->db->from('cw_consequence_rating');
			$this->db->where_in('cw_consequence_rating.prime_consequence_rating_id', $consequence);
			$this->db->where('cw_consequence_rating.trans_status', 1);
			$this->db->order_by("consequence_rating", "ASC");
			$this->db->limit(1,0);
			$consequence_rating_rlst     = $this->db->get()->row();
			$consequence_rating          = $consequence_rating_rlst->consequence_rating;
			$prime_consequence_rating_id = $consequence_rating_rlst->prime_consequence_rating_id;
		}
		if($likelihood_impact){
			$this->db->select('likelihood_rating,prime_likelihood_rating_id');
			$this->db->from('cw_likelihood_rating');
			$this->db->where_in('cw_likelihood_rating.prime_likelihood_rating_id', $likelihood_impact);
			$this->db->where('cw_likelihood_rating.trans_status', 1);
			$this->db->order_by("likelihood_rating", "DESC");
			$this->db->limit(1,0);
			$likelihood_rating_rslt     = $this->db->get()->row();
			$likelihood_rating          = $likelihood_rating_rslt->likelihood_rating;
			$prime_likelihood_rating_id = $likelihood_rating_rslt->prime_likelihood_rating_id;
		}
		$sts       = false;
		$matrix_ui = '' ;
		if($likelihood_rating && $consequence_rating){
			$matrix_ui = $this->risk_matrix_ui($likelihood_rating,$consequence_rating);
			$sts       = true;
		}
		echo json_encode(array('success'=>true,'consequence_rating' => $consequence_rating,	'likelihood_rating' => $likelihood_rating,'ui_status'=> $sts,'risk_matrix' => $matrix_ui));
	}
	
	//RATE MATRIX UI DESIGN
	public function risk_matrix_ui($likelihood_rating,$consequence_rating){
		$array                       = range('A', 'Z');
		$likelihood_val = array_keys($array, $likelihood_rating);
		$likelihood_val = $likelihood_val[0];
		if((int)$consequence_rating > 0){
			$consequence_rating = (int)$consequence_rating - 1;
		}
		$check_id       = "$likelihood_val$consequence_rating";
		$risk_rate     = array();
		array_push($risk_rate,array('III','III','II','II','I','I','I'));
		array_push($risk_rate,array('IV','III','III','II','II','I','I'));
		array_push($risk_rate,array('IV','IV','III','III','II','II','I'));
		array_push($risk_rate,array('IV','IV','IV','III','III','II','II'));
		array_push($risk_rate,array('V','IV','IV','III','III','II','II'));
		array_push($risk_rate,array('V','V','IV','IV','III','III','II'));
		$tr_line = '';
		$i       = 0;
		for($a = 4;$a >= 0;$a--){
			$tr_line .= '<tr class="w10">';
			$j        = 0;
			if($a === 4){
				$tr_line .= '<td rowspan="7" class="border-a t-center h100p label_text " style="color: blue;font-weight: 700;font-size: 14px;border:1px solid #000;white-space: nowrap;max-width: 15px;">LIKELIHOOD RATING</td>';
			}
			for($b = 7;$b >=0;$b--){
				
				$style = "";
				$class_style = "";
				if($b === 7){
					$value = $array[$a];
					$style = "background-color: #f2f2f2;";
				}else{
					if($b <= 6){
						$value = $risk_rate[$i][$j];
						$j++;
					}
					if("td_$check_id" == "td_$a$b"){
						if($value == 'I'){
							$class_style = "red-c";
						}else
						if($value == 'II'){
							$class_style = "yellow-c";
						}else
						if($value == 'III'){
							$class_style = "blue-c";
						}else
						if($value == 'IV'){
							$class_style = "green-c";
						}else
						if($value == 'V'){
							$class_style = "grey-c";
						}						
					}
					
				}
				
				$tr_line .= "<td id='td_$a$b' class='form-label $class_style' style='border:1px solid #000;$style' >$value</td>";
			}
			$tr_line .= '</tr>';
			if($a <= 3){
				$i++;
			}
		}
		$table_info = "<div style='background-color:#FFFFFF;padding:8px;width:80%;' class='table-responsive' ><h4 class='block_head'>Risk Matrix Table</h4>
							<table class='table'  id='risk_matrix_table' >
								<tbody>
									$tr_line
									<tr>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'></td>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'>7</td>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'>6</td>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'>5</td>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'>4</td>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'>3</td>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'>2</td>
										<td class='form-label' style='border:1px solid #000;background-color: #f2f2f2;'>1</td>
									</tr>
									<tr>
										<td class='form-label' colspan='8' style='color: blue;txt-align:center; font-weight: 700; font-size: 20px;border:1px solid #000;'>CONSEQUENCE RATING</td>
									</tr>
								</tbody>
							</table>
						 </div>";
		return $table_info;
	}
	
	//GET RISK NUMBER
	public function get_risk_no(){
		$this->db->select('count(*) as rslt_count');
		$this->db->from('cw_risk_and_hazard_management');
		$this->db->where('trans_status', 1);
		$result      = $this->db->get()->row();
		$rslt_count  = (int)$result->rslt_count;
		$count       = $rslt_count + 1 ;
		$count       = str_pad($count, 4, '0', STR_PAD_LEFT);
		$uniqueCode  = date('Y') . '/RS'. $count;
		return strtoupper($uniqueCode);
	}
	
	//CHECK ACTION Status
	public function check_action_status($prime_id){
		$exist_qry = 'SELECT count(*) as result_count from cw_risk_and_hazard_management join cw_action_management on cw_action_management.risk_incident_car_no = cw_risk_and_hazard_management.risk_no where prime_risk_and_hazard_management_id = "'.$prime_id.'" and cw_risk_and_hazard_management.trans_status = 1';
		$exist_data   = $this->db->query("CALL sp_a_run ('SELECT','$exist_qry')");
		$exist_result = $exist_data->row();	
		$exist_data->next_result();
		$result_count = (int)$exist_result->result_count;
		if($result_count === 0){
			return true;
		}else{
			$exist_qry = 'SELECT count(*) as result_count from cw_risk_and_hazard_management join cw_action_management on cw_action_management.risk_incident_car_no = cw_risk_and_hazard_management.risk_no where prime_risk_and_hazard_management_id = "'.$prime_id.'" and cw_action_management.action_status = 1 and cw_risk_and_hazard_management.trans_status = 1';
			$exist_data   = $this->db->query("CALL sp_a_run ('SELECT','$exist_qry')");
			$exist_result = $exist_data->row();	
			$exist_data->next_result();
			$result_count = (int)$exist_result->result_count;
			if($result_count === 1){
				return true;
			}else{
				return false;
			}
		}
	}
}
?>