MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //home/cafsindia/hrms_allyindian_com/application_bk/controllers/Income_matching.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Income_matching  extends Action_controller{
	
	public function __construct(){
		parent::__construct('income_matching');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$this->page_info();
		$data['encKey']                  = $this->generateKey();	
		$data['module_sts']              = (int)$this->module_sts;
		$data['quick_link']              = $this->quick_link;
		$data['pick_list']               = $this->pick_list;
		$data['form_info']               = $this->form_info;
		$data['table_head']              = $this->table_head;
		$data['fliter_list']             = $this->fliter_list;
		$data['freeze_list']             = $this->freeze_list;
		//Get all the Earning Columns without tax column
		$get_match_columns = 'SELECT prime_form_id,prime_module_id,CONCAT(label_name," - ",view_name) as view_name,label_name FROM `cw_form_setting` WHERE prime_module_id = "employees" and transaction_type IN (2,3)';
		$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();
		if($match_column_result){
			$pick_key   = array_column($match_column_result, "label_name");
			$pick_val   = array_column($match_column_result, "view_name");
			$match_pick = array_combine( $pick_key, $pick_val);
			$match_pick = array("" => "---- Select Column ----") + $match_pick;
		}
		$data['earning_columns']   = $match_pick;

		$this->load->view("$this->control_name/manage",$data);
	}
	
	//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' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
				exit(0);
			}
		}
		//VIEW INFO FUNCTION
		$this->view_info($form_view_id);
		$data['encKey']                 = $this->generateKey();
		$data['module_sts']             = (int)$this->module_sts;
		//VIEW, FORM INPUT
		$data['view_info']              = $this->view_info;
		$data['form_info']              = $this->form_info;
		$data['formula_result']         = $this->formula_result;//for label color change
		$data['role_based_condition']   = $this->role_based_condition;//for label name readonly options
		$data['all_pick']               = $this->pick_list;//all pick result
		
		//DEPENDENT AUTO PICKLIST
		$data['auto_pick']              = $this->depen_auto_list;//dependent auto result			
		$data['condition_list']         = $this->form_condition_list;

		//VIEW DATA
		$base_query          = str_replace("@SELECT@",$this->view_select,$this->base_query);
		$form_view_qry       = $base_query .' where '.$this->prime_table.'.'.$this->prime_id.' = '.$form_view_id.' and '.$this->prime_table.'.trans_status = 1';
		$row_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_pro_qry        = [];
		$view_pro_qry[]      = array("return"=>"form_view","qry"=>$form_view_qry);
		$view_pro_qry[]      = array("return"=>"row_view_list","qry"=>$row_view_qry);	

		$view_info_rslt      = $this->run_multi_qry($view_pro_qry);
		$form_view_rslt      = $view_info_rslt->rslt->form_view[0];			
		$row_view_rslt       = $view_info_rslt->rslt->row_view_list;

		$data['form_view']   = $form_view_rslt;
		
		$row_view_list       = array();
		foreach($row_view_rslt 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;
		$form_arr = $this->get_form_columns($form_view_id);
		$data['earning_columns_withtax'] = $form_arr['earning_columns_withtax'];
		$data['earning_columns']         = $form_arr['earning_columns'];
		$data['ded_columns']             = $form_arr['ded_columns'];

		$data['edit_id']                 = $form_view_id;
		//FOR DEPENDENT
		$data['get_depend_prime_id']     = $this->get_depend_fun();
		$data['depend_label_id']   	     = $this->depend_label_fun();
		$this->load->view("$this->control_name/form",$data);
	}
	
	//IMPORT FILE VIEW INFORMATION
	public function import(){
		$data['encKey']        = $this->generateKey();
		$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);
	}
	
	//LOAD PAGE TABLE VIEW WITH DATA BASED ON SEARCH FILTERS
	public function search(){
		$dec_data         = $this->cryptoDecrypt($_POST['Payload']);
		$_POST            = $dec_data['data'];
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....'));
			exit(0);
		}		
		//SEARCH INFO FUNCTION
		$this->search_info();
		$module_sts       = (int)$this->module_sts;
		if(!$module_sts){
			echo json_encode(array('success' => FALSE, 'message' => "Search  Info Query Process Error..!"));
			exit(0);
		}else{
			$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);
		
			$search_result    = array();
			//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_name;
				$field_isdefault  = (int)$fliter->field_isdefault;
				$field_type       = (int)$fliter->field_type;
				$prime_form_id 	  = $fliter->prime_form_id;

				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 === 9){
							$search_id     = 'filter_'.$label_id.'_hidden_'.$prime_form_id;
							$search_val    = $this->input->post("$search_id");
							$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->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 = "$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->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);
				}
			}
			$count_all_query    = str_replace("@SELECT@","count(*) as allcount",$this->base_query);
			$count_query        = $count_all_query.' where financial_setting_id = '.$this->financial_info[0]->prime_financial_setting_id.' and '.$this->prime_table.'.trans_status = 1 '.$role_condition.$fliter_query.$common_search;

			$search_query      .= " where financial_setting_id = ".$this->financial_info[0]->prime_financial_setting_id." and $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_pro_qry     = [];
			$search_pro_qry[]   = array("return"=>"total_count","qry"=>$count_all_query);
			$search_pro_qry[]   = array("return"=>"filtered_count","qry"=>$count_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     = $search_info_rslt->rslt->filtered_count[0]->allcount;
			
			$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));	
		}
	}
	
	//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' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
			exit(0);
		}
		//SAVE INFO FUNCTION
		$this->save_info();	
		$module_sts          = (int)$this->module_sts;
		if(!$module_sts){
			echo json_encode(array('success' => FALSE, 'message' => "Save Info Query Process Error...!"));
			exit(0);
		}else{
			$unq_chk         = array();
			$prime_qry_key   = "";
			$prime_qry_value = "";
			$prime_upd_query = "";
			//Decrypt Form id
			$form_id         = $this->input->post($this->prime_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' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
					exit(0);
				}
			}else{
				$form_id = 0;
			}
			$previous_data   = $this->get_previous_view($form_id);
			$previous_data   = $previous_data[0];
			$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;
				$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));
				}else
				if((int)$field_type === 10){//File Upload
					$value 		 		 = $value;
					$remove_upload_fname = "old_".$label_id;
					$remove_upload_file  = $this->input->post($remove_upload_fname);
					if($value !== $remove_upload_file){
						unlink($remove_upload_file);  
					}
				}
				if($label_id === 'earnings'){
					$earnings  = trim($this->input->post($label_id));
				}
				if($label_id === 'order_no'){
					$order_no  = trim($this->input->post($label_id));
				}
				
				
			//For replace special character
				$value = str_replace('"',"~",$value);
				$value = str_replace("'","`",$value);	
			
			//Sanitize SQL InJection chars
			$value = $this->sanitize_input($value, $field_type);

			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){
					if(!$this->check_earning_already_exists($earnings)){
						if(!$this->check_order_already_exists($order_no)){
							$prime_qry_key     .= "financial_setting_id,trans_created_by,trans_created_date";
					$prime_qry_value   .= '"'.$this->financial_info[0]->prime_financial_setting_id.'","'.$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;	
							$send_mail_status = "";
							if((int)$this->email_count > 0){
								$send_mail_status = $this->dynamic_mail_sent($insert_id,$pick_array,$previous_data);
							}			
							echo json_encode(array('success' => TRUE, 'message' => "Successfully added $send_mail_status", 'insert_id' => $insert_id));
						}else{
							echo json_encode(array('success' => False, 'message' => "Order no ALready Exists"));
						}
					}else{
						echo json_encode(array('success' => False, 'message' => "Earning ALready Exists"));
					}
				}else{
					if(!$this->check_earning_already_exists($earnings,$form_id)){
						if(!$this->check_order_already_exists($order_no,$form_id)){
							$prime_upd_query    .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
							$prime_update_query  = 'UPDATE '. $this->prime_table .' SET '. $prime_upd_query .' WHERE financial_setting_id = "'.$this->financial_info[0]->prime_financial_setting_id.'" and '. $this->prime_id .' = "'. $form_id .'"';
							$this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')");
							$send_mail_status = "";
							if((int)$this->email_count > 0){
								$send_mail_status = $this->dynamic_mail_sent($form_id,$pick_array,$previous_data);
							}
							echo json_encode(array('success' => TRUE, 'message' => "Successfully updated $send_mail_status",'insert_id' => $form_id));
						}else{
							echo json_encode(array('success' => False, 'message' => "Order no ALready Exists"));
						}
					}else{
						echo json_encode(array('success' => False, 'message' => "Earning ALready Exists"));
					}
				}
			}
		}
	}
	
	//CHECK EARNING IS ALREADY EXISTS FOR INCOME MATCHING
	public function check_earning_already_exists($earnings,$form_id = -1){
		$is_exist_qry  = 'SELECT * FROM cw_income_matching where earnings = "'.$earnings.'" and trans_status = 1 and financial_setting_id = "'.$this->financial_info[0]->prime_financial_setting_id.'"';
		if((int)$form_id > 0){
			$is_exist_qry .= ' and prime_income_matching_id != "'.$form_id.'" ';
		}
		$is_exist_data = $this->db->query("CALL sp_a_run ('SELECT','$is_exist_qry')");
		$exist_count   = $is_exist_data->num_rows();
		$is_exist_data->next_result();
		if((int)$exist_count > 0){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	//CHECK ORDER IS ALREADY EXISTS FOR INCOME MATCHING
	public function check_order_already_exists($order_no,$form_id = -1){
		$is_exist_qry  = 'SELECT * FROM cw_income_matching where order_no = "'.$order_no.'" and trans_status = 1 and financial_setting_id = "'.$this->financial_info[0]->prime_financial_setting_id.'"';
		if((int)$form_id > 0){
			$is_exist_qry .= " and prime_income_matching_id != $form_id";
		}
		$is_exist_data = $this->db->query("CALL sp_a_run ('SELECT','$is_exist_qry')");
		$exist_count   = $is_exist_data->num_rows();
		$is_exist_data->next_result();
		if((int)$exist_count > 0){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	//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' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
			exit(0);
		}
		$delete_ids          = implode(",",$this->xss_clean($this->input->post('delete_ids')));
		$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"));
		}
	}
	
	//UPDATE STATUS TO DELETE FOR UPLOAD FILES or DOCUMENTS
	public function remove_file(){
		//Encryption
		$encString     = file_get_contents('php://input');
		$_POST         = $this->cryptoDecrypt($encString);
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
			exit(0);
		}
		$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"));
		}
	}
	
	//Sheet Name display in import page
	public function sheet_name(){
		$file_path  = $this->input->post('file_path');
		$filename = dirname(__FILE__)."/php_excel/PHPExcel/IOFactory.php";
		include($filename);
		$excel_obj   = \PhpOffice\PhpSpreadsheet\IOFactory::load($file_path);
		$sheet_count = $excel_obj->getSheetCount();
		$sheet_name = array();
		for($i= 0; $i< $sheet_count; $i++){
			$sheet        = $excel_obj->getSheet($i);
			$sheet_name[] = $sheet->getTitle();
		}
		echo json_encode(array('sheet_name' =>$sheet_name));
	}
	
	public function matching_check(){
		$encString         = file_get_contents('php://input');
		$_POST             = $this->cryptoDecrypt($encString);
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
			exit(0);
		}
		$matching_components  = $this->input->post('matching_components');
		$match_query   = 'select count(*) as rslt_count from cw_income_matching where trans_status = 1 and matching_components = "'.$matching_components.'"';
		$match_info    = $this->db->query("CALL sp_a_run ('SELECT','$match_query')");
		$match_result  = $match_info->result();
		$match_info->next_result();
		$match_count  = $match_result[0]->rslt_count;
		if($match_count > 0){
			echo json_encode(array('success' => FALSE, 'message' => "Already Components is Matched, Choose another one?"));
		}else{
			echo json_encode(array('success' => TRUE, 'message' => "Matching components is choosed"));
		}
	}
	
	//examption check 
	public function examption_check(){
		$encString         = file_get_contents('php://input');
		$_POST             = $this->cryptoDecrypt($encString);
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Url Expired.. Please refresh the page and try again....','table_data' => ""));
			exit(0);
		}
		$excemption_component  = $this->input->post('excemption_component');
		$excem_match_query   = 'select count(*) as rslt_count from cw_income_matching where trans_status = 1 and excemption_component = "'.$excemption_component.'"';
		$excem_match_info    = $this->db->query("CALL sp_a_run ('SELECT','$excem_match_query')");
		$excem_match_result  = $excem_match_info->result();
		$excem_match_info->next_result();
		$excem_match_count  = $excem_match_result[0]->rslt_count;
		if($excem_match_count > 0){
			echo json_encode(array('success' => FALSE, 'message' => "Already Examption is applied, Choose another one?"));
		}else{
			echo json_encode(array('success' => TRUE, 'message' => "Examption components is choosed"));
		}
	}
	
	//Earning components check
	public function earning_check(){
		$earning_component  = $this->input->post('earning_component');
		$earn_match_query   = 'select count(*) as rslt_count from cw_income_matching where trans_status = 1 and formula like  "%'.$earning_component.'%"';
		$earn_match_info    = $this->db->query("CALL sp_a_run ('SELECT','$earn_match_query')");
		$earn_match_result  = $earn_match_info->result();
		$earn_match_info->next_result();
		$earn_match_count  = $earn_match_result[0]->rslt_count;
		if($earn_match_count > 0){
			echo json_encode(array('success' => FALSE, 'message' => "Already Components is Mapped, Choose another one?"));
		}else{
			echo json_encode(array('success' => TRUE, 'message' => "Can Process"));
		}
	}
	public function get_form_columns($form_view_id){
		$form_arr = array();
		// Check Exist Earned column
		$not_in_prime_id 	 = "";
		if((int)$form_view_id > 0){
			$not_in_prime_id = ' and cw_income_matching.prime_income_matching_id = "'.$form_view_id.'"';			
		}
		$exist_matching_qry  = 'SELECT group_concat(earnings) as exist_col from cw_income_matching where trans_status = 1 '.$not_in_prime_id.'';
		$exist_matching_info = $this->db->query("CALL sp_a_run ('SELECT','$exist_matching_qry')");
		$exist_matching_rslt = $exist_matching_info->result();
		$exist_matching_info->next_result();
		$exist_col 		     = $exist_matching_rslt[0]->exist_col;
		$exist_column        = "";
		if($exist_col){
			$exist_column    = implode('","',explode(',', $exist_col));
		}
		$exist_qry           = ' and label_name not in("'.$exist_column.'")';
		if((int)$form_view_id > 0){
			$exist_qry       = ' and label_name in("'.$exist_column.'")';
		}
		$get_earn_columns = 'SELECT prime_form_id,prime_module_id,CONCAT(label_name," - ",view_name) as view_name,label_name FROM `cw_form_setting` WHERE prime_module_id = "employees" and taxable_check = 1 and transaction_type = 2 '.$exist_qry.'';
		$earn_column_info   = $this->db->query("CALL sp_a_run ('SELECT','$get_earn_columns')");
		$earn_column_result = $earn_column_info->result();
		$earn_column_info->next_result();
		if($earn_column_result){
			$pick_key   = array_column($earn_column_result, "label_name");
			$pick_val   = array_column($earn_column_result, "view_name");
			$earn_pick = array_combine( $pick_key, $pick_val);
			$earn_pick = array("" => "---- Select Column ----") + $earn_pick;
		}
		$form_arr['earning_columns_withtax']   = $earn_pick;

		//Get all the Earning Columns without tax column
		$get_match_columns = 'SELECT prime_form_id,prime_module_id,CONCAT(label_name," - ",view_name) as view_name,label_name FROM `cw_form_setting` WHERE prime_module_id = "employees" and transaction_type = 2 and taxable_check != 1';
		$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();
		if($match_column_result){
			$pick_key   = array_column($match_column_result, "label_name");
			$pick_val   = array_column($match_column_result, "view_name");
			$match_pick = array_combine( $pick_key, $pick_val);
			$match_pick = array("" => "---- Select Column ----") + $match_pick;
		}
		$form_arr['earning_columns']   = $match_pick;

		//Get All Deduction Component
		$get_ded_columns = 'SELECT prime_form_id,prime_module_id,CONCAT(label_name," - ",view_name) as view_name,label_name FROM `cw_form_setting` WHERE prime_module_id = "employees" and transaction_type = 3';
		$ded_column_info   = $this->db->query("CALL sp_a_run ('SELECT','$get_ded_columns')");
		$ded_column_result = $ded_column_info->result();
		$ded_column_info->next_result();
		if($ded_column_result){
			$pick_key   = array_column($ded_column_result, "label_name");
			$pick_val   = array_column($ded_column_result, "view_name");
			$ded_pick = array_combine( $pick_key, $pick_val);
			$ded_pick = array("" => "---- Select Column ----") + $ded_pick;
		}
		$form_arr['ded_columns']   = $ded_pick;

		return $form_arr;
	}
}
?>