File: /home/cafsindia/cpaqua.cafsinfotech.in/dump/ZRM/application/controllers/Monthly_input_mapping.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Monthly_input_mapping extends Action_controller{
public function __construct(){
parent::__construct('monthly_input_mapping');
if(!$this->Appconfig->isAppvalid()){
redirect('config');
}
}
// LOAD PAGE WITH TABLE DATA
public function index(){
$db_name = $this->config->item("db_name");
$prime_table_query = $this->db->query("SELECT COLUMN_NAME AS label_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA ='$db_name' and TABLE_NAME = 'cw_monthly_input_mapping' and COLUMN_NAME NOT IN ('trans_created_by','trans_created_date','trans_updated_by','trans_updated_date','trans_deleted_by','trans_deleted_date','trans_status','prime_monthly_input_mapping_id')");
$prime_table_col = $prime_table_query->result();
$data['table_head'] = $prime_table_col;
$data['count_type_arr'] = $this->get_time_office_components();
$this->load->view("$this->control_name/manage",$data);
}
//LOAD MODEL PAGE VIEW WITH DATA
public function view($form_view_id=-1){
$mi_component_arr = $this->get_time_office_components();
$mi_input_qry ='SELECT time_office_component from cw_monthly_input_mapping where trans_status = 1 and cw_monthly_input_mapping.prime_monthly_input_mapping_id != "'.$form_view_id.'"';
$mi_input_info = $this->db->query("CALL sp_a_run ('SELECT','$mi_input_qry')");
$mi_input_result = $mi_input_info->result_array();
$mi_input_info->next_result();
$mi_input_arr = array_reduce($mi_input_result, function ($result, $arr) {
$result[$arr['time_office_component']] = $arr['time_office_component'];
return $result;
}, array());
$result = array_diff_key($mi_component_arr,$mi_input_arr);
$data['time_office_component_list'] = array(""=>"---- Select Column----") + $result;
// print_r($data['time_office_component_list']); die;
$get_match_columns = 'SELECT prime_form_id,prime_module_id,label_name,view_name FROM cw_form_setting WHERE prime_module_id = "employees" and (deduction_month_check = 1 or earn_month_check=1) and transaction_type in (2,3) and cw_form_setting.label_name not in (select mi_component from cw_monthly_input_mapping where cw_monthly_input_mapping.trans_status = 1 and cw_monthly_input_mapping.prime_monthly_input_mapping_id != "'.$form_view_id.'")';
$match_column_info = $this->db->query("CALL sp_a_run ('SELECT','$get_match_columns')");
$match_column_result = $match_column_info->result();
$match_column_info->next_result();
$match_columns[""] = "---- Select Column ----";
foreach($match_column_result as $match_column){
$prime_form_id = $match_column->prime_form_id;
$label_name = $match_column->label_name;
$column_value = $match_column->view_name;
$match_column_name = ucwords(str_replace("_"," ",$column_value));
$match_columns[$this->xss_clean($label_name)] = $this->xss_clean($match_column_name);
}
$data['match_columns'] = $match_columns;
//Get Row data
$previous_data_query = 'SELECT * from cw_monthly_input_mapping where trans_status = 1 and prime_monthly_input_mapping_id = "'.$form_view_id.'"';
$previous_data = $this->db->query("CALL sp_a_run ('SELECT','$previous_data_query')");
$previous_result = $previous_data->result();
$num_rows = $previous_data->num_rows();
$previous_data->next_result();
$data['mapping_result'] = $previous_result;
$this->load->view("$this->control_name/form",$data);
}
//LOAD PAGE TABLE VIEW WITH DATA BASED ON SEARCH 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'];
$search = trim($search['value']);
$filter_count = count($fliter_label);
$fliter_query = "";
$common_search = "";
if($search){
$common_search .= " and cw_monthly_input_mapping.mi_component like '%".$search."%'";
}
$search_total = $this->db->query("SELECT count(*) as allcount from cw_monthly_input_mapping where trans_status = 1");
$search_total_info = $search_total->result();
$total_count = $search_total_info[0]->allcount;
$search_count = $this->db->query("SELECT count(*) as allcount from cw_monthly_input_mapping where trans_status = 1 $common_search");
$search_info = $search_count->result();
$filtered_count = $search_info[0]->allcount;
$search_query ='SELECT prime_monthly_input_mapping_id,time_office_component,view_name as mi_component from cw_monthly_input_mapping inner join cw_form_setting on cw_form_setting.label_name = cw_monthly_input_mapping.mi_component where cw_monthly_input_mapping.trans_status = 1 and cw_form_setting.trans_status = 1 and prime_module_id ="employees"';
$search_query .= "$common_search 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 json_encode(array("draw" => intval($draw),"recordsTotal" => $total_count,"recordsFiltered" => $filtered_count,"data" => $search_result));
}
//SAVE MODEL DATA TO DATA BASE
public function save($form_id){
$prime_qry_key = "";
$prime_qry_value = "";
$prime_upd_query = "";
$this->prime_table = "cw_monthly_input_mapping";
$this->prime_id = "prime_monthly_input_mapping_id";
$exist_qry = 'select * from cw_monthly_input_mapping where time_office_component = "'.$this->input->post('time_office_component').'" and mi_component = "'.$this->input->post('mi_component').'" and trans_status = 1';
$exist_data = $this->db->query("CALL sp_a_run ('SELECT','$exist_qry')");
$exist_result = $exist_data->result();
$num_rows = $exist_data->num_rows();
$exist_data->next_result();
$id = $exist_result[0]->prime_monthly_input_mapping_id;
if(((int)$num_rows > 0) && ($id !== $form_id)){
echo json_encode(array('success' => false, 'message' => 'Already Exist'));
}else{
$created_on = date("Y-m-d h:i:s");
if((int)$form_id === -1){
$prime_qry_key .= "time_office_component,mi_component,trans_created_by,trans_created_date";
$prime_qry_value .= '"'.$this->input->post('time_office_component').'","'.$this->input->post('mi_component').'","'.$this->logged_id.'",'.'"'.$created_on.'"';
$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;
echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id));
}else{
$prime_upd_query .= 'time_office_component = "'. $this->input->post('time_office_component') .'",mi_component = "'. $this->input->post('mi_component') .'",trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
$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')");
echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'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')")){
$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{
$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;
}
}
}
?>