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/cloud_cafsinfotech_in/application/controllers/Other_income_entry.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Other_income_entry  extends Action_controller{
	
	public function __construct(){
		parent::__construct('other_income_entry');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		//PAGE INFO FUNCTION
		$this->page_info();
		$data['module_sts']    = (int)$this->module_sts;
		$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;
		$data['link_info']      = $this->quick_link;
		$data['financial_info'] = $this->financial_info;
		$category_info   = $this->db->query("CALL sp_a_run ('SELECT','SELECT * FROM `cw_category` where trans_status = 1 and prime_category_id !=1')");
		$category_result = $category_info->result();
		$category_info->next_result();
		$category_list[""] = "---- Category For ----";
		foreach($category_result as $for){
			$cat_id                 = $for->prime_category_id;
			$category_name          = $for->category_name;
			$category_list[$cat_id] = $category_name;
		}
		$data['category_list']  = $category_list;
		
		$data['table_headers']  = $this->xss_clean(get_dbtable_headers($this->table_info));
		$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;
		$data['module_id']     = $this->control_name;
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//LOAD MODEL PAGE VIEW WITH DATA
	public function view($form_view_id=-1){
		//VIEW INFO FUNCTION
		$this->view_info($form_view_id);
		$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;
		//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['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(){
		//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{
			$search       = $this->input->get('search');
			$limit        = $this->input->get('limit');
			$offset       = $this->input->get('offset');
			$sort         = $this->input->get('sort');
			$order        = $this->input->get('order');
			$fliter_label = $this->input->get('fliter_label');
			$fliter_type  = $this->input->get('fliter_type');
			$filter_cond  = $this->input->get('filter_cond');
			$fliter_val   = $this->input->get('fliter_val');
			$filter_count = count($fliter_label);
			$fliter_query = "";
			$search_count = 0;
			for($i=0;$i<=(int)$filter_count;$i++){
				$db_name     = $fliter_label[$i];
				$table_name  = $fliter_type[$i];
				$db_cond     = $filter_cond[$i];
				$db_value    = $fliter_val[$i];
				if(($db_cond) && ($db_value)){
					$search_count++;
					$search_val = $db_value;
					if($db_cond === "LIKE"){ $search_val = "$db_value%"; }
					if((int)$table_name === 1){ $fliter_query .= ' and '. $this->prime_table .".". $db_name ." ". $db_cond .' "'.$search_val.'"'; }
					if((int)$table_name === 2){ $fliter_query .= ' and '. $this->cf_table .".". $db_name ." ". $db_cond .' "'.$search_val.'"'; }
				}			
			}
			
			$common_search = "";
			if($search){
				$count=0;
				foreach($this->form_info as $setting){
					$prime_form_id   = $setting->prime_form_id;
					$field_type      = $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){
						$pick_sel_table = "$this->prime_table";
					}else
					if($field_isdefault === 2){
						$pick_sel_table = "$this->cf_table";
					}
					$other_label_id = $pick_sel_table.".".$label_id;
					
					if(($input_view_type === 1) || ($input_view_type === 2)){
						if((int)$field_type === 4){
							if(strtotime($search)){
								$search_val = date('Y-m-d',strtotime($search));
								$common_search .= ' or '. $other_label_id .' like "'.$search_val.'%"';
							}
						}else
						if(((int)$field_type === 5) || ((int)$field_type === 7)){
							if((int)$pick_list_type === 1){
								$column_name = explode(",",$pick_list);
								$column_name = $column_name[1];
							}else{
								$column_name = $pick_table."_value";
							}
							$pick_query_as  = $pick_table."_".$prime_form_id;
							$label_id       = "$pick_query_as.$column_name";
							$common_search .= ' or '. $label_id .' like "'.$search.'%"';
						}else
						if((int)$field_type === 9){
							$pick_query_as  = $pick_table."_".$prime_form_id;
							$label_id       = "$pick_query_as.$auto_dispaly_value";
							$common_search .= ' or '. $label_id .' like "'.$search.'%"';
						}else{
							if(!strtotime($search)){
								$common_search .= ' or '. $other_label_id .' like "'.$search.'%"';
							}
						}
					}
				}
				if($common_search){
					$common_search = ltrim($common_search,' or ');
					$common_search = " and ($common_search)";
				}
			}
					
			if(!$sort){ $sort = $this->prime_table.".".$this->prime_id; }
			if(!$order){ $order = "asc";  }
			
			$select_info   = str_replace("@SELECT",$this->select_query,$this->base_query);
			$search_query  = $select_info. $this->pick_query;
			
			//ADDED BASIC,FILTER,COMMON QUERY HERE 
			$basic_query = "";
			if($this->table_search_info){
				$basic_query = $this->table_search_info;
			}
			$search_query .= " where $this->prime_table.trans_status = 1 $basic_query $fliter_query $common_search";
			$search_query .= " ORDER BY  $sort $order";
			$search_query .= " LIMIT  $offset,$limit";
			
			//FETCH RECORDS DATA
			$search_data   = $this->db->query("CALL sp_a_run ('SELECT','$search_query')");
			$search_result = $search_data->result();
			$num_rows      = $search_data->num_rows();
			$search_data->next_result();
			$data_rows     = array();
			foreach ($search_result as $search){
				$data_rows[]=get_dbdata_row($search,$this->table_info,$this->print_info,$this);
			}
			//$data_rows=$this->xss_clean($data_rows);
			
			//FETCH RECORDS COUNT
			$count_info    = "count(*) as rslt_count";
			$base_query    = str_replace("@SELECT",$count_info,$this->base_query);
			$search_query  = $base_query. $this->pick_query;
			$search_query .= " where $this->prime_table.trans_status = 1 $basic_query $fliter_query $common_search";
			$count_data    = $this->db->query("CALL sp_a_run ('SELECT','$search_query')");
			$count_result  = $count_data->result();
			$count_data->next_result();
			echo json_encode(array('total'=>$count_result[0]->rslt_count,'rows'=>$data_rows));
		// }
	}
	
	//SAVE MODEL DATA TO DATA BASE
	public function save(){
		//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 = "";
			$cf_qry_key      = "";
			$cf_qry_value    = "";
			$cf_upd_query    = "";	
			$cf_has          = false;
			$form_id         = (int)$this->input->post($this->prime_id);
			$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 === 8){//textbox only
					$value = str_replace('"',"~",$value);
					$value = str_replace("'","`",$value);
					$value = str_replace("&","^",$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(($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,);
						}
					}else{
						$cf_has = true;
						$cf_qry_key     .= $label_id.",";
						$cf_qry_value   .= '"'.$value.'",';
						$cf_upd_query   .= $label_id.' = "'.$value.'",';
						if($unique_field === 1){
							$cf_unq_chk = $label_id.'= "'.$value.'"';
							$query = "select count(*) as rslt_count from $this->cf_table where $this->prime_id != $form_id and $cf_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){
					$prime_qry_key     .= "trans_created_by,trans_created_date";
					$prime_qry_value   .= '"'.$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;
					
					$cf_qry_key     .= "trans_created_by,trans_created_date";
					$cf_qry_value   .= '"'.$this->logged_id.'",'.'"'.$created_on.'"';
					$cf_insert_query = 'insert into ' .$this->cf_table. ' ('.$this->prime_id.','.$cf_qry_key.') values ("'.$insert_id.'",'.$cf_qry_value.')';
					$insert_info     = $this->db->query("CALL sp_a_run ('INSERT','$cf_insert_query')");
					$insert_info->next_result();
					
					echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id));
				}else{
					$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 '. $this->prime_id .' = "'. $form_id .'"';
					$this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')");
					
					$cf_upd_query    .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
					$cf_update_query  = 'UPDATE '. $this->cf_table .' SET '. $cf_upd_query .' WHERE '. $this->prime_id .' = "'. $form_id .'"';
					$this->db->query("CALL sp_a_run ('UPDATE','$cf_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;
		}
	}
	
	//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"));
		}
	}
	
	//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   = PHPExcel_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));
	}
	
	//category based employee list -- 27AUG2019
	public function get_employee_list(){
		$category = $this->input->post('category');
		$emp_query  = 'SELECT emp_name,employee_code FROM cw_employees WHERE role = "'.$category.'" and trans_status =1';
		$emp_info   = $this->db->query("CALL sp_a_run ('SELECT','$emp_query')");
		$emp_result = $emp_info->result();
		$emp_info->next_result();
		$emp_list = "<option value=''>---- Select Employee ----</option>";
		foreach($emp_result as $result){
			$name      = $result->emp_name;
			$emp_code  = $result->employee_code;
			$emp_list .= "<option value='$emp_code'>$emp_code - $name</option>";
		}
		echo $emp_list;
	}
	
	public function save_section(){
		$other_income_data = json_decode($this->input->post('other_income_data'));
		$created_on = date("Y-m-d H:i:s");
		$finacial_setting_id = $this->financial_info[0]->prime_financial_setting_id;
		$prime_qry_key   = '';
		$prime_qry_value = '';
		if(!empty($other_income_data)){
			foreach($other_income_data as $entry_data){
				$column_name      = $entry_data->name;
				$column_value     = $entry_data->value;
				if($column_name === "effective_month"){
					$column_value     = $entry_data->value;
					//$column_value = date('Y-m-d',strtotime($column_value));
					$month_exit   = $column_value;
				}
				if($column_name === "emp_code"){
					$emp_code = $column_value;
				}
				$prime_qry_key   .= $column_name.",";
				$prime_qry_value .= '"'.$column_value.'",';

				$update_qry_key     .= $column_name.",";
				$update_qry_value   .= '"'.$column_value.'",';
				$update_upd_query   .= $column_name.' = "'.$column_value.'",';
				
			}
			$tax_process_exit_qry   = 'select count(*) as rslt_count from cw_tax_calculation where emp_code="'.$emp_code.'" and process_month="'.$month_exit.'" and trans_status =1';
			$tax_process_exit_info   = $this->db->query("CALL sp_a_run ('SELECT','$tax_process_exit_qry')");
			$tax_process_exit_result = $tax_process_exit_info->result();
			$tax_process_exit_info->next_result();
			$tax_exit_count = $tax_process_exit_result[0]->rslt_count;
			if((int)$tax_exit_count === 1){
				echo json_encode(array('success' => false, 'message' => "Already Tax Proceed, Please Delete the tax process?"));
				exit();
			}else{
				$exit_emp_qry   = 'select count(*) as rslt_count from cw_other_income_entry where financial_setting_id ="'.$finacial_setting_id.'" and emp_code="'.$emp_code.'" and effective_month="'.$month_exit.'" and trans_status =1';
				$exit_emp_info   = $this->db->query("CALL sp_a_run ('SELECT','$exit_emp_qry')");
				$exit_emp_result = $exit_emp_info->result();
				$exit_emp_info->next_result();
				$rslt_count = $exit_emp_result[0]->rslt_count;
				if((int)$rslt_count === 0){
					$prime_qry_key     .= "financial_setting_id,trans_created_by,trans_created_date";
					$prime_qry_value   .= '"'.$finacial_setting_id.'","'.$this->logged_id.'",'.'"'.$created_on.'"';
					$dec_entry_insert_qry = "insert into cw_other_income_entry ($prime_qry_key) values ($prime_qry_value)";				
					$dec_entry_insert_info        = $this->db->query("CALL sp_a_run ('INSERT','$dec_entry_insert_qry')");
					$dec_entry_insert_result      = $dec_entry_insert_info->result();
					$dec_entry_insert_info->next_result();
					echo json_encode(array('success' => TRUE, 'message' => "Successfully added your Other Income Entry"));
				}else{
					$update_upd_query     .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
					$update_query  = 'UPDATE cw_other_income_entry SET '. $update_upd_query .' WHERE financial_setting_id = "'. $finacial_setting_id .'" and emp_code = "'. $emp_code .'" and effective_month = "'. $month_exit .'"';
					$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
					echo json_encode(array('success' => TRUE, 'message' => "Successfully Updated your Other Income Entry"));
				}
			}
		}
	}
	
	public function update_section(){
		$category            = $this->input->post('category');
		$emp_code            = $this->input->post('emp_code');
		$effective_month     = $this->input->post('effective_month');
		//$effective_month     = "01-".$effective_month;
		//$effective_month_val = date('Y-m-d', strtotime($effective_month));
		
		$tax_section_qry  = 'select cw_income_type.income_type,income_description,other_income_column from cw_other_income inner join cw_income_type on cw_income_type.prime_income_type_id =cw_other_income.income_type where cw_other_income.trans_status = 1 order by cw_income_type.income_type';
		$tax_section_info   = $this->db->query("CALL sp_a_run ('SELECT','$tax_section_qry')");
		$tax_section_result = $tax_section_info->result();
		$tax_section_info->next_result();
		$tr_line = "";
		
		foreach($tax_section_result as $tax_rslt){
				$income_description   =  $tax_rslt->income_description;
				$income_type          =  $tax_rslt->income_type;
				$other_income_column  =  $tax_rslt->other_income_column;
				$finacial_setting_id  = $this->financial_info[0]->prime_financial_setting_id;				
				$collect_dec_entry_qry   = 'select '.$other_income_column.' from cw_other_income_entry where financial_setting_id ="'.$finacial_setting_id.'" and emp_code="'.$emp_code.'" and effective_month="'.$effective_month.'" and trans_status =1';
				$collect_dec_entry_info   = $this->db->query("CALL sp_a_run ('SELECT','$collect_dec_entry_qry')");
				$collect_dec_entry_result = $collect_dec_entry_info->result();
				$collect_dec_entry_info->next_result();
				if($collect_dec_entry_result){
					$other_income_column_val  = $collect_dec_entry_result[0]->$other_income_column;	
				}else{
					$other_income_column_val = 0.00;
				}
									
				$tr_line .= "<tr><td>".$income_type."</td><td>".$income_description."</td><td><input class='form-control number' type='text' id='".$other_income_column."' name='".$other_income_column."' value ='$other_income_column_val'/></td></tr>";
		}
		$table_info = "<table class='table table-bordered'>
								<thead>
									<tr>
										<th>Income Type</th>
										<th>Income Description</th>
										<th>Yearly Value</th>
									</tr>
								</thead>
								<tbody>								
									$tr_line
								</tbody>
						   </table>";
		echo $table_info;		
	}
	
	public function delete_section(){
		$category            = $this->input->post('category');
		$emp_code            = $this->input->post('emp_code');
		$effective_month     = $this->input->post('effective_month');
		
		$tax_process_exit_qry   = 'select count(*) as rslt_count from cw_tax_calculation where emp_code="'.$emp_code.'" and process_month="'.$effective_month.'" and trans_status =1';
		$tax_process_exit_info   = $this->db->query("CALL sp_a_run ('SELECT','$tax_process_exit_qry')");
		$tax_process_exit_result = $tax_process_exit_info->result();
		$tax_process_exit_info->next_result();
		$tax_exit_count = $tax_process_exit_result[0]->rslt_count;
		if((int)$tax_exit_count === 1){
			echo json_encode(array('success' => false, 'message' => "Already Tax Proceed, Please Delete the tax process?"));
			exit();
		}else{
			$created_on = date("Y-m-d H:i:s");
			$finacial_setting_id = $this->financial_info[0]->prime_financial_setting_id;
			$update_upd_query     .= 'trans_deleted_by = "'. $this->logged_id .'",trans_deleted_date = "'.$created_on.'"';
			if($effective_month){
				$update_query  = 'UPDATE cw_other_income_entry SET trans_status = 0, '. $update_upd_query .' WHERE financial_setting_id = "'. $finacial_setting_id .'" and emp_code = "'. $emp_code .'" and effective_month = "'. $effective_month.'"';
				$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
				echo json_encode(array('success' => true, 'message' => "Deleted your data successfully!!!"));
			}
		}
	}
	public function excel($module_id,$excel_format){
		$excel_format_qry = 'select income_description as view_name,excel_line_column_name,excel_line_value from cw_util_excel_format_line left join cw_other_income on cw_other_income.other_income_column = cw_util_excel_format_line.excel_line_column_name where excel_line_module_id = "'.$module_id.'" and prime_excel_format_id ="'.$excel_format.'" and cw_util_excel_format_line.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();		
		//require_once APPPATH."/third_party/PHPExcel.php";
		require_once APPPATH."/controllers/php_excel/PHPExcel.php";
		$obj = new PHPExcel();		
		//Set the first row as the header row
		foreach($excel_result as $excel){
			$excel_line_column_name = $excel->view_name;
			if(!$excel_line_column_name){
				$excel_line_column_name = str_replace('_', ' ', $excel->excel_line_column_name);
			}
			$excel_line_value       = $excel->excel_line_value;
			$obj->getActiveSheet()->setCellValue($excel_line_value."1", $excel_line_column_name);
		}		

		// Rename worksheet name
		 $filename= $module_id.".xls"; //save our workbook as this file name
		 ob_end_clean();
		 header('Content-Type: application/vnd.ms-excel'); //mime type
		 header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		 header('Cache-Control: max-age=0'); //no cache
		 ob_end_clean();
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		 //if you want to save it as .XLSX Excel 2007 format
		 $objWriter = PHPExcel_IOFactory::createWriter($obj, 'Excel5');
		 //force user to download the Excel file without writing it to server's HD
		 $objWriter->save('php://output');
		echo json_encode(array('success' => TRUE, 'output' => $excelOutput));
	}
	public function save_import(){
		$module_id        = $this->control_name;
		$process_month    = $this->input->post('effective_month');
		$excel_format     = $this->input->post('excel_format');
		$excel_file_path  = $this->input->post('excel_file_path');
		$excel_sheet_name = $this->input->post('excel_sheet_name');
		$excel_start_row  = $this->input->post('excel_start_row');
		$excel_end_row    = $this->input->post('excel_end_row');
		$logged_id        = $this->session->userdata('logged_id');
		$import_type      = $this->input->post('import_type');	
		$today_date       = date("Y-m-d h:i:s");
		$import_query = 'insert into cw_month_import (module_id,excel_format,excel_file_path,excel_sheet_name,excel_start_row,excel_end_row,process_month,trans_created_by,trans_created_date) value ("'.$module_id.'","'.$excel_format.'","'.$excel_file_path.'","'.$excel_sheet_name.'","'.$excel_start_row.'","'.$excel_end_row.'","'.$process_month.'","'.$logged_id.'","'.$today_date.'")';
		$import_info   = $this->db->query("CALL sp_a_run ('INSERT','$import_query')");
		$import_result = $import_info->result();
		$import_info->next_result();
		$import_id = $import_result[0]->ins_id;
		echo $this->do_excel_oi_entry_import($import_id,$import_type,$process_month);
	}
	//IMPORT DATA FROM FILE PATH
	public function do_excel_oi_entry_import($import_id,$import_type,$process_month){
		$filename = dirname(__FILE__)."/php_excel/PHPExcel/IOFactory.php";
		include($filename);
		if($import_id < 0){
			return json_encode(array('success' => false, 'message' => "Invalid file upload"));
		}
		$excel_path_qry    = 'select * from cw_month_import where import_id = "'.$import_id.'"';
		$excel_path_info   = $this->db->query("CALL sp_a_run ('SELECT','$excel_path_qry')");
		$excel_path_result = $excel_path_info->result();
		$excel_path_info->next_result();
		if(!$excel_path_result){
			return json_encode(array('success' => false, 'message' => "Invalid file upload"));
		}else{
			$this->prime_table  = "cw_".$this->control_name;
			$excel_file_path    = $excel_path_result[0]->excel_file_path;
			$module_id          = $this->control_name;
			$excel_format       = $excel_path_result[0]->excel_format;			
			$excel_sheet_name   = (int)$excel_path_result[0]->excel_sheet_name;
			$excel_row_start    = (int)$excel_path_result[0]->excel_start_row;
			$excel_row_end      = (int)$excel_path_result[0]->excel_end_row;
			$process_month      = $excel_path_result[0]->process_month;
		

			$format_qry 	= 'select * from cw_util_excel_format where prime_excel_format_id = "'.$excel_format.'" and cw_util_excel_format.trans_status = 1';
			$format_info    = $this->db->query("CALL sp_a_run ('SELECT','$format_qry')");
			$format_rslt    = $format_info->result();
			$format_info->next_result();
			if(!$format_rslt){
				return json_encode(array('success' => false, 'message' => "Please add excel format before import"));
			}else{
				$exist_column_name 	= explode(",",$format_rslt[0]->exist_column_name);

				$finance_yr_arr 	= $this->financial_info;
				$finance_yr_id 		= $finance_yr_arr[0]->prime_financial_setting_id;

				$get_role_qry 		= 'select role,employee_code from cw_employees where trans_status = 1';
				$get_role_info      = $this->db->query("CALL sp_a_run ('SELECT','$get_role_qry')");
				$get_role_rslt 		= $get_role_info->result_array();
				$get_role_info->next_result();
				$get_role_rslt 		= array_reduce($get_role_rslt, function ($result, $arr) {
					    $result[$arr['employee_code']] = $arr['role'];
					    return $result;
					}, array());
				// echo "<pre>";
				// print_r($get_role_rslt);die;


				// $column_qry 	= 'SELECT TABLE_NAME as table_name,COLUMN_NAME as column_name  FROM `INFORMATION_SCHEMA`.`COLUMNS`  WHERE `TABLE_SCHEMA`="'.$this->config->item("db_name").'" AND `TABLE_NAME` IN ("cw_other_income_entry") AND COLUMN_NAME NOT LIKE "%trans%" AND COLUMN_NAME NOT IN ("prime_other_income_entry_id")';
				// $column_info    = $this->db->query("CALL sp_a_run ('SELECT','$column_qry')");
				// $column_rslt 	= $column_info->result();
				// $column_info->next_result();
				// echo "<pre>";
				// print_r($column_rslt);die;




				$excel_format_qry 	= 'select excel_line_column_name,excel_line_value from cw_util_excel_format_line where excel_line_module_id = "'.$module_id.'" and prime_excel_format_id = "'.$excel_format.'" and cw_util_excel_format_line.trans_status = 1 order by prime_excel_format_line_id ASC';
				// echo "$excel_format_qry";die;

				// $get_column_qry 	= 'SELECT prime_other_income_id,other_income_column,UPPER(cw_income_type.income_type) as income_type FROM `cw_other_income` inner join cw_income_type on cw_income_type.prime_income_type_id = cw_other_income.income_type WHERE cw_other_income.trans_status = 1 and cw_income_type.trans_status = 1';
				// echo "get_column_qry ::$get_column_qry";die;
				$excel_format        = $this->db->query("CALL sp_a_run ('SELECT','$excel_format_qry')");
				$excel_format_result = $excel_format->result();
				$excel_format->next_result();
				// echo "<pre>";
				// print_r($excel_format_result);die;
				if(!$excel_format_result){
					return json_encode(array('success' => false, 'message' => "Please map excel cell column before import"));
				}else{
					try{
						$excel_obj = PHPExcel_IOFactory::load($excel_file_path);
					}catch(Exception $e){
						die('Error loading file "' . pathinfo($excel_file_path, PATHINFO_BASENAME). '": ' . $e->getMessage());
						return json_encode(array('success' => false, 'message' => "Invalid file or path"));
					}
					$sheet           	= $excel_obj->getSheet($excel_sheet_name);
					if($excel_row_end){
						$total_rows 	= $excel_row_end;
					}else{
						$total_rows 	= $sheet->getHighestRow();
					}
					$highest_column  	= $sheet->getHighestColumn();
					$status_array	    = array();
					$invalid_cat        = array();
					$formula_process    = array();
					//Unique field check
					$err_column_tabview  = array();
					$array_uniq 		 = array();
					foreach ($excel_format_result as $key => $value) {
						$excel_line_column_name = $value->excel_line_column_name;
						$excel_line_value 		= $value->excel_line_value;
						$get_cell_value 		= $sheet->rangeToArray("$excel_line_value$excel_row_start:$excel_line_value$total_rows", NULL, TRUE, TRUE, TRUE);
						$common_multi_cell_value = $sheet->rangeToArray("$excel_line_value$excel_row_start:$excel_line_value$total_rows", NULL, TRUE, TRUE, TRUE);
						$i = $excel_row_start;
						foreach($common_multi_cell_value as $common_value){
							foreach($common_value as $col_key =>$col_value){
								if(empty($col_value) && !is_numeric($col_value)){
									$err_column_array['error']["$excel_line_value$i"] = $view_name;
									$msg_line = "columns are empty and invalid data is present please check it?";
									$err_column_tabview['error']["$excel_line_value$i"]  = $view_name." ".$msg_line;
								}else
								if($excel_line_column_name === "emp_code" || $excel_line_column_name === "user_id"){
									$emp_code_qry = 'select count(*) as rslt_count from cw_employees where trans_status = 1 and employee_code = "'.$col_value.'"';
									$emp_data  = $this->db->query("CALL sp_a_run ('SELECT','$emp_code_qry')");
									$emp_data_result = $emp_data->result();
									$emp_data->next_result();
									$rslt_count = $emp_data_result[0]->rslt_count;
									if((int)$rslt_count === 0){
										$err_column_array['error']["$excel_line_value$i"] = $view_name;
										$msg_line = "is not exit in employee master please check it?";
										$err_column_tabview['error']["$excel_line_value$i"]  = $view_name." ".$msg_line;
									}
								}
								$i++;
							}
						}						
					}
					// echo "<pre>";
					// print_r($err_column_tabview);
					// die;
					$err_column_count = count($err_column_array['error']);
					$err_column       = implode(",",(array_unique($err_column_array['error'])));
					if((int)$err_column_count > 0){
						$table_info = $this->get_excel_error_ui($err_column_tabview);
						echo json_encode(array('success'=>false,'message'=>"Column Wise Error",'table_info'=>$table_info));
						exit();
					}
					for($row =$excel_row_start; $row <= $total_rows; $row++){
						$prime_column_val = "";
						$prime_cell_val   = "";
						$exist_val        = "";
						$prime_upd_query  = "";
						$status_info = array();
						$status_info["Excel Row"] = $row;
						$sts = 1;
						foreach($excel_format_result as $excel_info){
							$excel_line_column_name = $excel_info->excel_line_column_name;
							$excel_line_value       = $excel_info->excel_line_value;
							$get_cell_value         = trim($sheet->getCell("$excel_line_value$row")->getCalculatedValue());
							
								if($excel_line_column_name === 'emp_code'){
									$prime_column_val .= $excel_line_column_name.",";
									$prime_cell_val   .= '"'.$get_cell_value.'",';
									$employee_code 	   = $get_cell_value;
								}else{
									$prime_column_val .= $excel_line_column_name.",";
									$prime_cell_val   .= '"'.$get_cell_value.'",';
									$prime_upd_query  .= $excel_line_column_name.' = "'.$get_cell_value.'",';
								}
								
								if(empty($exist_column_name)) {
									$exist_val .= $excel_line_column_name.' = "'.$get_cell_value.'" and ';
								}else{
									if(in_array($excel_line_column_name,$exist_column_name)){
										$exist_val .= $excel_line_column_name.' = "'.$get_cell_value.'" and ';
									}
								}
							}
						// }

						// echo "$exist_val<br>";die;
						// echo "$prime_column_val<br>";
						// echo "$prime_cell_val<br>";die;
						// die;
						if((int)$sts !== 0){
						//GET Employee id
							// $emp_query = 'select * from cw_employees where employee_code = "'.$employee_code.'" and trans_status =1';
							// echo "$emp_query<br>";
							// $emp_info   = $this->db->query("CALL sp_a_run ('SELECT','$emp_query')");
							// $emp_result = $emp_info->result();
							// $emp_info->next_result();
							// $employees_id   = $emp_result[0]->prime_employees_id;
							// $emp_name       = $emp_result[0]->emp_name;
							// $doj            = $emp_result[0]->date_of_joining;
							// $esi_elig       = $emp_result[0]->esi_eligibility;
							// $pf_elig        = $emp_result[0]->pf_eligibility;
							// $lwf_location   = $emp_result[0]->lwf_location;
							// $tax_location   = $emp_result[0]->professional_tax_location;
							// die;
							// if(!$employees_id){
							// 	return json_encode(array('success' => false, 'message' => "Employee Code Not Exist.. Please Map the Employee Code Correctly"));
							// }else{
								//only role and employee code is checking
								if($prime_column_val){
									$prime_id    = "prime_".$module_id."_id";
									// echo "$prime_id<br>";
									$exist_val   = "and ".rtrim($exist_val," and ");
									$exist_query = "select count(*) as exist_count,$prime_id from ".$this->prime_table." where ".$this->prime_table.".trans_status =1 and effective_month = \"$process_month\" $exist_val";
									// echo "$exist_query<br>";
									$exist_info   = $this->db->query("CALL sp_a_run ('RUN','$exist_query')");
									$exist_result = $exist_info->result();
									$exist_info->next_result();
									$exist_count  = $exist_result[0]->exist_count;
									$created_on   = date("Y-m-d H:i:s");
									$role 		  = $get_role_rslt[$employee_code];
									$upd_prime_id = $exist_result[0]->$prime_id;
									if((int)$import_type === 1){
										if((int)$exist_count === 0){
											$prime_column_val .= "category,financial_setting_id,effective_month,trans_created_by,trans_created_date";
											$prime_cell_val   .= '"'.$role.'","'.$finance_yr_id.'","'.$process_month.'","'.$this->logged_id.'",'.'"'.$created_on.'"';
											$prime_column_val  = rtrim($prime_column_val,",");
											$prime_cell_val    = rtrim($prime_cell_val,",");
											$prime_query       = "insert into $this->prime_table ($prime_column_val) VALUES ($prime_cell_val)";
											$insert_info   = $this->db->query("CALL sp_a_run ('INSERT','$prime_query')");
											$insert_result = $insert_info->result();
											$insert_info->next_result();
											$insert_id = $insert_result[0]->ins_id;
										}
										$cat_sts 	= 0;
									}else
									if((int)$import_type === 2){
										if((int)$exist_count === 1){
										$upd_query = 'UPDATE '.$this->prime_table.' SET '.$prime_upd_query.'trans_updated_by = "'.$this->logged_id.'",trans_updated_date = "'.$created_on.'" , trans_status = 1 WHERE '.$prime_id.' = "'.$upd_prime_id.'"';
										$this->db->query("CALL sp_a_run ('RUN','$upd_query')");
										}
										$cat_sts 	= 1;
									}
								}
							}
						}
					}
					if($cat_sts){
						echo json_encode(array('success'=>true,'message'=>"Successfully updated file imported"));
					}else{
						echo json_encode(array('success'=>true,'message'=>"Successfully file imported"));
					}
				}
			}
	}
	//ERROR COLUMN AND ROW DISPLAY FOR INVALID DATAS
	public function get_excel_error_ui($err_column_tabview){
		$table_info = "";
		$th_line = "";
		$tr_line = "";
		foreach($err_column_tabview as $err_column){
			foreach($err_column as $key => $value){
				$tr_line .= "<tr><td>$key</td><td style='color:#ff0303 !important;'>$value</td></tr>";
			}
		}
		$table_info = "<table class='table table-bordered'>
		<thead>
		<tr>
		<th>Row and Column</th>
		<th>Input Column</th>
		</tr>
		</thead>
		<tbody>
		$tr_line
		</tbody>
		</table>";
		return $table_info;
	}
}
?>