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/Incident_request.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Incident_request  extends Action_controller{	
	public function __construct(){
		parent::__construct('incident_request');
		$this->collect_base_info();
	}
	
	// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
	public function index(){
		$data['quick_link']    = $this->quick_link;
		$data['table_head']    = json_decode(json_encode($this->get_table_head()));
		// INCIDENT STATUS
		$this->db->select('prime_incident_status_id,incident_status');
		$this->db->from('cw_incident_status');
		$this->db->where('cw_incident_status.trans_status', 1);
		$incident_status_rslt = $this->db->get()->result();
		foreach($incident_status_rslt as $incident_rslt){
			$prime_incident_status_id  = $incident_rslt->prime_incident_status_id;
			$incident_name             = $incident_rslt->incident_status;
			$incident_status[$prime_incident_status_id] = $incident_name;
		}
		$data['incident_status'] = $incident_status;
		
		// INVESTIGATION STATUS
		$this->db->select('prime_investigation_risk_status_id,investigation_risk_status');
		$this->db->from('cw_investigation_risk_status');
		$this->db->where('cw_investigation_risk_status.trans_status', 1);
		$incident_status_rslt = $this->db->get()->result();
		foreach($incident_status_rslt as $rlst){
			$prime_investigation_risk_status_id  = $rlst->prime_investigation_risk_status_id;
			$investigation_risk_status           = $rlst->investigation_risk_status;
			$investigation_status[$prime_investigation_risk_status_id] = $investigation_risk_status;
		}
		$data['investigation_status'] = $investigation_status;
		
		$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));
		$incident_status           = $this->input->post('incident_status');
		$investigation_status      = $this->input->post('investigation_status');
		$incident_category_filter  = $this->input->post('incident_category_filter');
		$search_query = 'select prime_incident_request_id,incident_category,incident_no,incident_description,incident_date,emp1.employee_name as reporter_name,reporting_date,emp2.employee_name as reporting_to,cw_incident_status.incident_status as incident_status,cw_investigation_risk_status.investigation_risk_status as investigation_status from cw_incident_request left join cw_employees emp1 on emp1.prime_employees_id = cw_incident_request.reporter_name join cw_employees emp2 on emp2.prime_employees_id = cw_incident_request.reporting_to join cw_incident_status on cw_incident_status.prime_incident_status_id = cw_incident_request.incident_status left join cw_investigation_risk_status on cw_investigation_risk_status.prime_investigation_risk_status_id = cw_incident_request.investigation_status  where cw_incident_request.trans_status = 1';
		$search       = trim($search['value']);
		if($search){
			if(strtotime($search)){
				$search_val = date('Y-m-d',strtotime($search));
				$common_search .= ' or incident_date like "'.$search_val.'%" or reporting_date like "'.$search_val.'%"';
			}else{
				$common_search .= ' or incident_no like "'.$search.'%" or reporter_name like "'.$search.'%"';
			}
		}
		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(incident_date, "%Y-%m-%d") BETWEEN \''.$from_date.'\' AND \''.$to_date.'\'';
		}	
		
		if($incident_status[0] > 0){
			$incident_status  =  '"'.implode('","',$incident_status).'"';
			$common_search   .= " and cw_incident_request.incident_status IN ($incident_status)";
		}
		
		if($investigation_status[0] > 0){
			$investigation_status  =  '"'.implode('","',$investigation_status).'"';
			$common_search        .= " and cw_incident_request.investigation_status IN ($investigation_status)";
		}
		if($incident_category_filter[0] > 0){
			$incident_category_filter  =  '"'.implode('","',$incident_category_filter).'"';
			$common_search        .= " and cw_incident_request.incident_category IN ($incident_category_filter)";
		}
		
		$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 $common_search";
		$search_count       = $this->db->query($count_query);
		$search_info        = $search_count->result();
		$filtered_count     = $search_info[0]->allcount;
		if((int)$per_page !== -1){
			$search_query  .= " $common_search LIMIT  $start,$per_page";
		}
		$search_data        = $this->db->query($search_query);
		$search_result      = $search_data->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){
		$select_query    = "select cw_incident_request.incident_no,incident_category,incident_description,incident_date,emp6.employee_name as employee_name,cw_incident_request.employee_name as employee_id,employee_dept,phone_number,cw_incident_request.email_id,address,cw_incident_request.reporter_name,cw_incident_request.reporting_date,cw_incident_request.reporting_to,cw_incident_request.incident_status,prime_incident_request_id,incident_type,police_case_no,police_notified,location_of_incident,description_of_incident,attach_document,witness_incident,witness_name,witness_phone_number,witness_address,individual_injured,description_individual,treatment_provided,treatment_type,treatment_evidence,corrective_type,audit_number,specification,q_m_s_element,details_of_problem,problem_identified_date,problem_identified_by as problem_identified_by_id,emp1.employee_name as problem_identified_by,problem_knowledged_by as problem_knowledged_by_id,emp2.employee_name as problem_knowledged_by,problem_agreed_date_of_response,problem_date_followed_up,problem_extension_requested_by as problem_extension_requested_by_id,emp3.employee_name as problem_extension_requested_by,correction_taken_to_resolve_problem,correction_taken_by as correction_taken_by_id,emp4.employee_name as correction_taken_by,correction_taken_date,root_cause_of_problem,root_cause_completed_by as root_cause_completed_by_id,emp5.employee_name as root_cause_completed_by,root_cause_completed_date,date_time,place,weather_road_conditions,drivers_name,registration_number,other_drivers_name,other_id_number,make_of_vehicle,other_registration_number,other_driver_address,other_telephone_no,person_reported_to,reported_person_tel_no,police_station,police_case_number,police_statement
		from cw_incident_request join cw_employees emp6 on emp6.prime_employees_id = cw_incident_request.employee_name left join cw_employees emp1 on emp1.prime_employees_id = cw_incident_request.problem_identified_by left join cw_employees emp2 on emp2.prime_employees_id = cw_incident_request.problem_knowledged_by left join cw_employees emp3 on emp3.prime_employees_id = cw_incident_request.problem_extension_requested_by left join cw_employees emp4 on emp4.prime_employees_id = cw_incident_request.correction_taken_by left join cw_employees emp5 on emp5.prime_employees_id = cw_incident_request.root_cause_completed_by  where cw_incident_request.trans_status = 1 and prime_incident_request_id = $form_view_id";
		$select_data        = $this->db->query("CALL sp_a_run ('SELECT','$select_query')");
		$select_result      = $select_data->result();
		$select_data->next_result();
		$data['incident_request'] = $select_result;
		
		// EMPLOYEE DEPARTMENT
		$this->db->select('prime_employee_department_id,employee_department_name');
		$this->db->from('cw_employee_department');
		$this->db->where('cw_employee_department.trans_status', 1);
		$employee_dept_rslt = $this->db->get()->result();
		$employee_dept[""] = "--- Select ---";
		foreach($employee_dept_rslt as $dept_rslt){
			$prime_employee_department_id  = $dept_rslt->prime_employee_department_id;
			$employee_department_name      = $dept_rslt->employee_department_name;
			$employee_dept[$prime_employee_department_id] = $employee_department_name;
		}
		$data['employee_dept'] = $employee_dept;
		
		// ALL EMPLOYEE
		$this->db->select('prime_employees_id,employee_name');
		$this->db->from('cw_employees');
		$this->db->where('cw_employees.trans_status', 1);
		$this->db->where('cw_employees.role !=', 1);
		$employee_dept_rslt = $this->db->get()->result();
		$all_employee[""] = "--- Select ---";
		foreach($employee_dept_rslt as $dept_rslt){
			$prime_employees_id                = $dept_rslt->prime_employees_id;
			$employee_name                     = $dept_rslt->employee_name;
			$all_employee[$prime_employees_id] = $employee_name;
		}
		$data['all_employee'] = $all_employee;
		
		// REPORTING EMPLOYEE
		$this->db->select('prime_employees_id,employee_name');
		$this->db->from('cw_employees');
		$this->db->where('cw_employees.trans_status', 1);
		$this->db->where_in('cw_employees.role', array(5,2));
		$employee_rslt = $this->db->get()->result();
		$reporting_to[""] = "--- Select ---";
		foreach($employee_rslt as $emp_rslt){
			$prime_employees_id  = $emp_rslt->prime_employees_id;
			$employee_name       = $emp_rslt->employee_name;
			$reporting_to[$prime_employees_id] = $employee_name;
		}
		$data['reporting_to'] = $reporting_to;
		
		// INCIDENT STATUS
		$this->db->select('prime_incident_status_id,incident_status');
		$this->db->from('cw_incident_status');
		$this->db->where('cw_incident_status.trans_status', 1);
		$incident_status_rslt = $this->db->get()->result();
		$incident_status[""] = "--- Select ---";
		foreach($incident_status_rslt as $incident_rslt){
			$prime_incident_status_id  = $incident_rslt->prime_incident_status_id;
			$incident_name             = $incident_rslt->incident_status;
			$incident_status[$prime_incident_status_id] = $incident_name;
		}
		$data['incident_status'] = $incident_status;
		
		// INCIDENT TYPE
		$this->db->select('prime_incident_type_id,incident_type as incident_name');
		$this->db->from('cw_incident_type');
		$this->db->where('cw_incident_type.trans_status', 1);
		$incident_type_rslt = $this->db->get()->result();
		$incident_type[""] = "--- Select ---";
		foreach($incident_type_rslt as $incident_rslt){
			$prime_incident_type_id  = $incident_rslt->prime_incident_type_id;
			$incident_name           = $incident_rslt->incident_name;
			$incident_type[$prime_incident_type_id] = $incident_name;
		}
		$data['incident_type'] = $incident_type;	
		
		// CORRECTIVE TYPE
		$this->db->select('prime_corrective_type_id,corrective_type as corrective_name');
		$this->db->from('cw_corrective_type');
		$this->db->where('cw_corrective_type.trans_status', 1);
		$corrective_type_rslt = $this->db->get()->result();
		$corrective_type[""] = "--- Select ---";
		foreach($corrective_type_rslt as $corrective_rslt){
			$prime_corrective_type_id  = $corrective_rslt->prime_corrective_type_id;
			$corrective_name           = $corrective_rslt->corrective_name;
			$corrective_type[$prime_corrective_type_id] = $corrective_name;
		}
		$data['corrective_type']       = $corrective_type;
		
		// DRIVERS INFORMATION
		$this->db->select('prime_employees_id,employee_name');
		$this->db->from('cw_employees');
		$this->db->where('cw_employees.role', 4);
		$this->db->where('cw_employees.trans_status', 1);
		$driver_rslt = $this->db->get()->result();
		$drivers_dropdown[""] = "--- Select ---";
		foreach($driver_rslt as $drivers){
			$prime_employees_id  = $drivers->prime_employees_id;
			$employee_name       = $drivers->employee_name;
			$drivers_dropdown[$prime_employees_id] = $employee_name;
		}
		$data['drivers_dropdown']      = $drivers_dropdown;
		
		// DRIVERS INFORMATION
		$this->db->select('cw_vehicle_master.prime_vehicle_master_id,cw_vehicle_master.register_no,cw_vehicle_type.vehicle_type');
		$this->db->from('cw_vehicle_master');
		$this->db->join('cw_vehicle_type','cw_vehicle_type.prime_vehicle_type_id = cw_vehicle_master.vehicle_type');
		$this->db->where('cw_vehicle_master.trans_status', 1);
		$driver_rslt = $this->db->get()->result();
		$ntc_vehicles[""] = "--- Select ---";
		foreach($driver_rslt as $drivers){
			$prime_vehicle_master_id  = $drivers->prime_vehicle_master_id;
			$register_no              = $drivers->register_no;
			$vehicle_type             = $drivers->vehicle_type;
			$ntc_vehicles[$prime_vehicle_master_id] = $vehicle_type."-".$register_no;
		}
		$data['ntc_vehicles']          = $ntc_vehicles;		
		$data['injured_person_table']  = $this->get_injured_person_info_table($form_view_id);			
		$data['witness_line_table']    = $this->get_witness_info_table($form_view_id);			
		$this->load->view("$this->control_name/form",$data);
	}
	
	//SAVE INCIDENT REQUEST
	public function save(){
		$prime_incident_request_id  = (int)$this->input->post('prime_incident_request_id');
		$incident_category          = $this->input->post('incident_category');
		$incident_request_data      = array(
			'incident_category'     => $incident_category,
			'incident_description'  => $this->input->post('incident_description'),
			'incident_date'         => date("Y-m-d",strtotime($this->input->post('incident_date'))),
			'employee_name'         => $this->input->post('employee_name_id'),
			'employee_dept'         => $this->input->post('employee_dept'),
			'phone_number'          => $this->input->post('phone_number'),
			'email_id'              => $this->input->post('email_id'),
			'address'               => $this->input->post('address'),
			'reporter_name'         => $this->input->post('reporter_name'),
			'reporting_date'        => date("Y-m-d",strtotime($this->input->post('reporting_date'))),
			'reporting_to'          => $this->input->post('reporting_to'),
			'incident_status'       => $this->input->post('incident_status'),
			'incident_type'         => $this->input->post('incident_type')
		);
		$update_info = $incident_request_data;
		if((int)$incident_category === 1){
			$update_info['incident_car_type']                  = $this->input->post('incident_type');
			$incident_request_data['police_notified']          = $this->input->post('police_notified');
			$incident_request_data['police_case_no']           = $this->input->post('police_case_no');
			$incident_request_data['location_of_incident']     = $this->input->post('location_of_incident');
			$incident_request_data['attach_document']          = $this->input->post('attach_document');
			$incident_request_data['witness_incident']         = $this->input->post('witness_incident');
			$incident_request_data['witness_name']             = $this->input->post('witness_name');
			$incident_request_data['witness_phone_number']     = $this->input->post('witness_phone_number');
			$incident_request_data['witness_address']          = $this->input->post('witness_address');
			$incident_request_data['individual_injured']       = $this->input->post('individual_injured');
			$incident_request_data['description_individual']   = $this->input->post('description_individual');
			$incident_request_data['treatment_provided']       = $this->input->post('treatment_provided');
			$incident_request_data['treatment_type']           = $this->input->post('treatment_type');
			$incident_request_data['treatment_evidence']       = $this->input->post('treatment_evidence');
		}else
		if((int)$incident_category === 2){
			$incident_request_data['audit_number']                        = $this->input->post('audit_number');
			$incident_request_data['specification']                       = $this->input->post('specification');
			$incident_request_data['q_m_s_element']                       = $this->input->post('q_m_s_element');
			$incident_request_data['details_of_problem']                  = $this->input->post('details_of_problem');
			$incident_request_data['problem_identified_date']             = date("Y-m-d",strtotime($this->input->post('problem_identified_date')));
			$incident_request_data['problem_identified_by']               = $this->input->post('problem_identified_by_id');
			$incident_request_data['problem_knowledged_by']               = $this->input->post('problem_knowledged_by_id');
			$incident_request_data['problem_agreed_date_of_response']     = date("Y-m-d",strtotime($this->input->post('problem_agreed_date_of_response')));
			$incident_request_data['problem_date_followed_up']            = date("Y-m-d",strtotime($this->input->post('problem_date_followed_up')));
			$incident_request_data['problem_extension_requested_by']      = $this->input->post('problem_extension_requested_by_id');
			$incident_request_data['correction_taken_to_resolve_problem'] = $this->input->post('correction_taken_to_resolve_problem');
			$incident_request_data['correction_taken_by']                 = $this->input->post('correction_taken_by_id');
			$incident_request_data['correction_taken_date']               = date("Y-m-d",strtotime($this->input->post('correction_taken_date')));
			$incident_request_data['root_cause_of_problem']               = $this->input->post('root_cause_of_problem');
			$incident_request_data['root_cause_completed_by']             = $this->input->post('root_cause_completed_by_id');
			$incident_request_data['root_cause_completed_date']           = date("Y-m-d",strtotime($this->input->post('root_cause_completed_date')));
			$incident_request_data['corrective_type']                     = $this->input->post('corrective_type');
			$update_info                                                  = $incident_request_data;
			$update_info['incident_car_type']                             = $this->input->post('corrective_type');
			
		}else
		if((int)$incident_category === 3){
			$incident_request_data['date_time']                = date('y-m-d h:m:s',strtotime($this->input->post('date_time')));
			$incident_request_data['place']                    = $this->input->post('place');
			$incident_request_data['weather_road_conditions']  = $this->input->post('weather_road_conditions');
			$incident_request_data['reported_person_tel_no']   = $this->input->post('reported_person_tel_no');
			$incident_request_data['drivers_name']             = $this->input->post('drivers_name');
			$incident_request_data['registration_number']      = $this->input->post('registration_number');
			$incident_request_data['other_drivers_name']       = $this->input->post('other_drivers_name');
			$incident_request_data['other_id_number']          = $this->input->post('other_id_number');
			$incident_request_data['make_of_vehicle']          = $this->input->post('make_of_vehicle');
			$incident_request_data['other_registration_number']= $this->input->post('other_registration_number');
			$incident_request_data['other_driver_address']     = $this->input->post('other_driver_address');
			$incident_request_data['other_telephone_no']       = $this->input->post('other_telephone_no');
			$incident_request_data['person_reported_to']       = $this->input->post('person_reported_to');
			$incident_request_data['police_station']           = $this->input->post('police_station');
			$incident_request_data['police_case_number']       = $this->input->post('police_case_number');
			$incident_request_data['police_statement']         = $this->input->post('police_statement');
		}
		if((int)$prime_incident_request_id === 0){
			$incident_no                                   = $this->get_incident_no($incident_category);
			$incident_request_data['incident_no']          = $incident_no;
			$incident_request_data['investigation_status'] = 1;
			$incident_request_data['trans_created_by']     = $this->session->userdata('logged_id');
			$incident_request_data['trans_created_date']   = date("Y-m-d H:i:s");
			if($this->db->insert('cw_incident_request',$incident_request_data)){
				$insert_id = $this->db->insert_id();
				$this->update_incident_management_status($incident_no,$update_info);
				echo json_encode(array('success' => TRUE, 'message' => "Successfully Inserted",
			'insert_id' => $insert_id,'incident_no'=>$incident_no));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Insertion failed, Please Try Again"));
			}
		}else
		if((int)$prime_incident_request_id > 0){
			$incident_request_data['trans_updated_by']   = $this->session->userdata('logged_id');
			$incident_request_data['trans_updated_date'] = date("Y-m-d H:i:s");
			$this->db->where('prime_incident_request_id',$prime_incident_request_id);
			if($this->db->update('cw_incident_request',$incident_request_data)){
				$incident_no = $this->input->post('incident_no');
				$this->update_incident_management_status($incident_no,$update_info);
				echo json_encode(array('success' => TRUE, 'message' => "Successfully Updated",'insert_id' => $prime_incident_request_id,'incident_no'=>$incident_no));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Update failed, Please Try Again",'insert_id' => $prime_incident_request_id));
			}
		}else{
			echo json_encode(array('success' => False, 'message' => "Please contact Admin.!"));
		}
	}
	
	
	//CHECK INCIDENT REQUEST EXISTS
	public function check_incident_request($prime_incident_request_id){
		$this->db->select('count(*) as num_row');
		$this->db->from('cw_incident_request');
		$this->db->where('prime_incident_request_id',(int)$prime_incident_request_id);
		$this->db->where('trans_status', 1);
		$rslt    = $this->db->get()->row();
		$num_row = $rslt->num_row;
		if((int)($num_row) === 1){
			return TRUE;
		}else{ 
			return FALSE;
		}
	} 
	
	//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_upload_file(){
		$prime_id_val  = $this->input->post('prime_id');
		$input_name    = $this->input->post('input_name');
		$table_name    = $this->prime_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"));
		}
	}
	
	//TABLE HEAD
	public function get_table_head(){
		$table_head = array(array(
						'label_name' => 'incident_date',
						'view_name'  => 'Incident Date',
						'field_type'  => 4
						),array(
						'label_name' => 'incident_no',
						'view_name'  => 'Incident No',
						'field_type'  => 1
						),array(
						'label_name' => 'incident_category',
						'view_name'  => 'Incident Category',
						'field_type'  => 1
						),array(
						'label_name' => 'incident_description',
						'view_name'  => 'Incident Description',
						'field_type'  => 1
						),array(
						'label_name' => 'reporter_name',
						'view_name'  => 'Reporter Name',
						'field_type'  => 1
						),array(
						'label_name' => 'reporting_date',
						'view_name'  => 'Reporting Date',
						'field_type'  => 4
						),array(
						'label_name' => 'reporting_to',
						'view_name'  => 'Reporting to',
						'field_type'  => 1
						),array(
						'label_name' => 'incident_status',
						'view_name'  => 'Incident Status',
						'field_type'  => 1
						),array(
						'label_name' => 'investigation_status',
						'view_name'  => 'Investigation Status',
						'field_type'  => 1
						)
					 );
		return $table_head;
	}
	
	//SEARCH EMPLOYEE LIST
	public function search_employee(){
		$search_employee_name  = $this->input->post_get('search_employee_name');
		$suggestions = array();
		if($search_employee_name){
			$this->db->select('prime_employees_id,employee_name,employee_code,department,mobile_number,email_id,residential_address');
			$this->db->from('cw_employees');
			$this->db->where('cw_employees.trans_status', 1);
			$this->db->where('cw_employees.role !=', 1);
			$this->db->group_start();
				$this->db->like('cw_employees.employee_name',$search_fault_name);
				$this->db->or_like('cw_employees.user_name',$search_fault_name);
			$this->db->group_end();	
			$search_fault_info = $this->db->get()->result();
			foreach($search_fault_info as $search_rlst){
				$prime_employees_id    = $search_rlst->prime_employees_id;
				$employee_code         = $search_rlst->employee_code;
				$employee_name         = $search_rlst->employee_name;
				$department            = $search_rlst->department;
				$mobile_number         = $search_rlst->mobile_number;
				$address               = $search_rlst->residential_address;
				$email_id              = $search_rlst->email_id;
				$label                 = "$employee_code - $employee_name";
				$suggestions[] = array('value'=>$employee_name,'label'=>$label,'prime_employees_id'=>$prime_employees_id,'department'=>$department,'mobile_number'=>$mobile_number,'email_id'=>$email_id,'address'=>$address);
			}
		}
		if(empty($suggestions)){
			$suggestions[] = array('value' => "0", 'label' => "No data found for this search");
		}
		echo json_encode($suggestions);
	}
	
	public function get_incident_no($incident_category){
		if((int)$incident_category === 1){
			$code = "IN";
		}else
		if((int)$incident_category === 2){
			$code = "CA";
		}else
		if((int)$incident_category === 3){
			$code = "AC";
		}
		$this->db->select('count(*) as rslt_count');
		$this->db->from('cw_incident_request');
		$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') . '/' . $code . $count;
		return strtoupper($uniqueCode);
	}
	
	//UPDATE RISK IN ACTION MANAGEMENT
	public function update_incident_management_status($incident_no,$update_info){
		unset($update_info['reporter_name']);
		unset($update_info['incident_type']);
		unset($update_info['corrective_type']);
		unset($update_info['reporting_date']);
		unset($update_info['reporting_to']);
		$exist_qry = "SELECT count(*) as record_count from cw_incident_management where cw_incident_management.incident_car_no = \"$incident_no\" and cw_incident_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();
		$record_count = $exist_result->record_count;
		if((int)$record_count === 0){
			$update_info['incident_car_no']       = $incident_no;
			$update_info['investigation_status']  = 1;
			$update_info['trans_created_by']      = $this->session->userdata('logged_id');
			$update_info['trans_created_date']    = date("Y-m-d H:i:s");
			$this->db->insert('cw_incident_management',$update_info);
		}else{
			$update_info['trans_updated_by']   = $this->session->userdata('logged_id');
			$update_info['trans_updated_date'] = date("Y-m-d H:i:s");
			$this->db->where('incident_car_no',$incident_no);
			$this->db->update('cw_incident_management',$update_info);
		}
	}

	//SAVE INJURED LINE
	public function save_injured_line(){
		$prime_incident_request_id             = (int)$this->input->post('prime_incident_request_id');
		$prime_injured_person_accident_line_id = $this->input->post('prime_injured_person_accident_line_id');
		$injured_data  = array(
			'prime_incident_request_id'      =>$prime_incident_request_id,
			'injured_person'                 =>$this->input->post('injured_person'),
			'injured_description'            =>$this->input->post('injured_description')
			);
		if((int)$prime_injured_person_accident_line_id === 0){
			$injured_data['trans_created_by']   = $this->session->userdata('logged_id');
			$injured_data['trans_created_date'] = date("Y-m-d H:i:s");
			if($this->db->insert('cw_injured_person_accident_line',$injured_data)){
				$insert_id  = $this->db->insert_id();
				$table_info = $this->get_injured_person_info_table($prime_incident_request_id);
				echo json_encode(array('success' => TRUE, 'message' => "Enquiry Inserted Successfully",
			'insert_id' => $insert_id,'table_info' =>$table_info));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Insertion failed, Please Try Again"));
			}
		}else
		if((int)$prime_injured_person_accident_line_id > 0){
			$enquiry_team_data['trans_updated_by']   = $this->session->userdata('logged_id');
			$enquiry_team_data['trans_updated_date'] = date("Y-m-d H:i:s");
			$this->db->where('prime_injured_person_accident_line_id',$prime_injured_person_accident_line_id);
			if($this->db->update('cw_injured_person_accident_line',$enquiry_team_data)){
				$table_info = $this->get_injured_person_info_table($prime_incident_request_id);
				echo json_encode(array('success' => TRUE, 'message' => "Enquiry Updated Successfully",'insert_id' => $prime_injured_person_accident_line_id,'table_info' =>$table_info));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Update failed, Please Try Again",'insert_id' => $prime_injured_person_accident_line_id));
			}
		}else{
			echo json_encode(array('success' => False, 'message' => "Please contact Admin.!"));
		}
	}
	
	//LIST INJURED PERSONS INFOMATION
	public function get_injured_person_info_table($prime_incident_request_id){
		$this->db->select('prime_incident_request_id,injured_person,injured_description,prime_injured_person_accident_line_id');
		$this->db->from('cw_injured_person_accident_line');
		$this->db->where('cw_injured_person_accident_line.trans_status', 1);
		$this->db->where('cw_injured_person_accident_line.prime_incident_request_id', $prime_incident_request_id);
		$send_request_info = $this->db->get()->result();
		$tble_line = '';
		foreach($send_request_info as $send_rlst){
			$prime_injured_person_accident_line_id  = $send_rlst->prime_injured_person_accident_line_id;
			$prime_incident_request_id              = $send_rlst->prime_incident_request_id;
			$injured_person                         = $send_rlst->injured_person;
			$injured_description                    = $send_rlst->injured_description;
			$tble_line .= "	<tr>
							<td>$injured_person</td>
							<td>$injured_description</td>
							<td>
								<a class='btn btn-warning btn-sm' onclick='edit_injured_person_info($prime_injured_person_accident_line_id)'>Edit</a>
								<a class='btn btn-danger btn-sm' onclick='delete_injured_person_info($prime_injured_person_accident_line_id,$prime_incident_request_id)'>Delete</a>
							</td>
						</tr>";
		}
		if($tble_line === ''){
			$tble_line .= "<tr><td colspan='3' style='text-align:center;'>No data available in table</td></tr>";
		}
		$table_data = "	<h4 class='m-t-0 header-title'><b>Enquiry Team Information List</b></h4>
						<table class='table table-striped table-bordered' id='enquiry_info_table'>
						 <thead>
							<tr>
							   <th>Injured Person Name</th>
							   <th>Description</th>
							   <th>Action</th>
							</tr>
						 </thead>
						 <tbody>
							$tble_line
						</tbody>
					</table>";
				
		return $table_data;
	}
	
	//GET INJURED PERSONS INFORMATION
	public function edit_injured_person_info(){
		$prime_injured_person_accident_line_id = (int)$this->input->post('prime_injured_person_accident_line_id');
		$this->db->select('injured_person,injured_description,prime_injured_person_accident_line_id');
		$this->db->from('cw_injured_person_accident_line');
		$this->db->where('cw_injured_person_accident_line.trans_status',1);
		$return_data = $this->db->get()->result();
		if($return_data){
			echo json_encode(array('success'=>true,'message'=>'Edit Info','update_data'=>$return_data));
		}else{
			echo json_encode(array('success'=>false,'message'=>'Please Contact Admin..!'));			
		}
	}

	//DELETE INJURED PERSONS INFORMATION
	public function delete_injured_person_info(){
		$prime_injured_person_accident_line_id = $this->input->post('prime_injured_person_accident_line_id');
		$prime_incident_request_id             = $this->input->post('prime_incident_request_id');
		$update_data['trans_deleted_by']       = $this->session->userdata('logged_id');
		$update_data['trans_deleted_date']     = date("Y-m-d H:i:s");
		$update_data['trans_status']           = 0;
		$this->db->where('prime_injured_person_accident_line_id ',$prime_injured_person_accident_line_id);
		if($this->db->update('cw_injured_person_accident_line',$update_data)){
			$table_info = $this->get_injured_person_info_table($prime_incident_request_id);
			echo json_encode(array("success" => TRUE,'message' => 'Deleted successfully','table_info'=>$table_info));
		}else{
			echo json_encode(array("success" => False,'message' => "Please Try Again"));
		}
	}
	
	//SAVE WITNESS TEAM
	public function save_witness_line(){
		$prime_incident_request_id           = (int)$this->input->post('prime_incident_request_id');
		$prime_witness_accident_line_id      = $this->input->post('prime_witness_accident_line_id');
		$witness_data  = array(
			'prime_incident_request_id' =>$prime_incident_request_id,
			'name'                      =>$this->input->post('witness_accident_name'),
			'tel_no'                    =>$this->input->post('witness_accident_tel_no'),
			'address'                   =>$this->input->post('witness_accident_address'),
			'registration_no'           =>$this->input->post('witness_accident_registration_no'),
			);
		if((int)$prime_witness_accident_line_id === 0){
			$witness_data['trans_created_by']   = $this->session->userdata('logged_id');
			$witness_data['trans_created_date'] = date("Y-m-d H:i:s");
			if($this->db->insert('cw_witness_accident_line',$witness_data)){
				$insert_id  = $this->db->insert_id();
				$table_info = $this->get_witness_info_table($prime_incident_request_id);
				echo json_encode(array('success' => TRUE, 'message' => "Enquiry Inserted Successfully",
			'insert_id' => $insert_id,'table_info' =>$table_info));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Insertion failed, Please Try Again"));
			}
		}else
		if((int)$prime_witness_accident_line_id > 0){
			$witness_data['trans_updated_by']   = $this->session->userdata('logged_id');
			$witness_data['trans_updated_date'] = date("Y-m-d H:i:s");
			$this->db->where('prime_witness_accident_line_id',$prime_witness_accident_line_id);
			if($this->db->update('cw_witness_accident_line',$witness_data)){
				$table_info = $this->get_witness_info_table($prime_incident_request_id);
				echo json_encode(array('success' => TRUE, 'message' => "Enquiry Updated Successfully",'insert_id' => $prime_witness_accident_line_id,'table_info' =>$table_info));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Update failed, Please Try Again",'insert_id' => $prime_witness_accident_line_id));
			}
		}else{
			echo json_encode(array('success' => False, 'message' => "Please contact Admin.!"));
		}
	}
	
	//LIST WITNESS INFOMATION
	public function get_witness_info_table($prime_incident_request_id){
		$this->db->select('prime_incident_request_id,name,address,tel_no,registration_no,prime_witness_accident_line_id');
		$this->db->from('cw_witness_accident_line');
		$this->db->where('cw_witness_accident_line.trans_status', 1);
		$this->db->where('cw_witness_accident_line.prime_incident_request_id', $prime_incident_request_id);
		$send_request_info = $this->db->get()->result();
		$tble_line = '';
		foreach($send_request_info as $send_rlst){
			$prime_witness_accident_line_id = $send_rlst->prime_witness_accident_line_id;
			$prime_incident_request_id      = $send_rlst->prime_incident_request_id;
			$name                           = $send_rlst->name;
			$address                        = $send_rlst->address;
			$tel_no                         = $send_rlst->tel_no;
			$registration_no                = $send_rlst->registration_no;
			$tble_line .= "	<tr>
							<td>$name</td>
							<td>$address</td>
							<td>$tel_no</td>
							<td>$registration_no</td>
							<td>
								<a class='btn btn-warning btn-sm' onclick='edit_witness_info($prime_witness_accident_line_id)'>Edit</a>
								<a class='btn btn-danger btn-sm' onclick='delete_witness_info($prime_witness_accident_line_id,$prime_incident_request_id)'>Delete</a>
							</td>
						</tr>";
		}
		if($tble_line === ''){
			$tble_line .= "<tr><td colspan='5' style='text-align:center;'>No data available in table</td></tr>";
		}
		$table_data = "	<h4 class='m-t-0 header-title'><b>Enquiry Team Information List</b></h4>
						<table class='table table-striped table-bordered' id='enquiry_info_table'>
						 <thead>
							<tr>
							   <th>Witness Name</th>
							   <th>Witness Address</th>
							   <th>Witness phone Number</th>
							   <th>Witness Registration No</th> 
							   <th>Action</th>
							</tr>
						 </thead>
						 <tbody>
							$tble_line
						</tbody>
					</table>";
				
		return $table_data;
	}
	
	//GET WITNESS INFORMATION
	public function edit_witness_info(){
		$prime_witness_accident_line_id = (int)$this->input->post('prime_witness_accident_line_id');
		$this->db->select('name as witness_accident_name,address as witness_accident_address,tel_no as witness_accident_tel_no,registration_no as witness_accident_registration_no,prime_witness_accident_line_id');
		$this->db->from('cw_witness_accident_line');
		$this->db->where('cw_witness_accident_line.prime_witness_accident_line_id',$prime_witness_accident_line_id);
		$this->db->where('cw_witness_accident_line.trans_status',1);
		$return_data = $this->db->get()->result();
		if($return_data){
			echo json_encode(array('success'=>true,'message'=>'Edit Info','update_data'=>$return_data));
		}else{
			echo json_encode(array('success'=>false,'message'=>'Please Contact Admin..!'));			
		}
	}

	//DELETE WITNESS INFORMATION
	public function delete_witness_info(){
		$prime_witness_accident_line_id    = $this->input->post('prime_witness_accident_line_id');
		$prime_incident_request_id         = $this->input->post('prime_incident_request_id');
		$update_data['trans_deleted_by']   = $this->session->userdata('logged_id');
		$update_data['trans_deleted_date'] = date("Y-m-d H:i:s");
		$update_data['trans_status']       = 0;
		$this->db->where('prime_witness_accident_line_id ',$prime_witness_accident_line_id);
		if($this->db->update('cw_witness_accident_line',$update_data)){
			$table_info = $this->get_witness_info_table($prime_incident_request_id);
			echo json_encode(array("success" => TRUE,'message' => 'Deleted successfully','table_info'=>$table_info));
		}else{
			echo json_encode(array("success" => False,'message' => "Please Try Again"));
		}
	}
}
?>