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/Form_16_report.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Form_16_report  extends Action_controller{
	
	public function __construct(){
		parent::__construct('form_16_report');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$role_info   = $this->db->query("CALL sp_a_run ('SELECT','SELECT * FROM `cw_category` where trans_status = 1  and prime_category_id !=1')");
		$role_result = $role_info->result();
		$role_info->next_result();
		$process_role[""] = "---- Select Role ----";
		foreach($role_result as $for){
			$role_id       = $for->prime_category_id;
			$category_name = $for->category_name;
			$process_role[$role_id] = $category_name;
		}
		$data['process_role'] = $process_role;
		$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;
		$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['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();
		$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');
		$field_type   = $this->input->get('input_field_type');
		$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];
			$field_type  = $field_type[$i];
			if(($db_cond) && ($db_value)){
				$search_count++;
				if((int)$field_type === 4){
					$search_val = date("Y-m-d",strtotime($db_value));
				}else{
					$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();	
		$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));
	}
	
	//start to form generation
	public function emp_suggest(){
		$search_term  = $this->input->post_get('term');
		$final_qry = 'select employee_code,emp_name from cw_employees where trans_status = 1 and employee_code like "'.$search_term.'%"';
		$final_data   = $this->db->query("CALL sp_a_run ('SELECT','$final_qry')");
		$final_result = $final_data->result();
		$final_data->next_result();
		foreach($final_result as $rslt){
			$employee_code = $rslt->employee_code;
			$emp_name      = $rslt->emp_name;
			$suggestions[] = array('value' => $employee_code, 'label' => "$employee_code - $emp_name");
		}
		if(empty($suggestions)){
			$suggestions[] = array('value' => "0", 'label' => "No data found for this search");
		}
		echo json_encode($suggestions);
	}
	
	public function form_generate(){
		$process_type        = (int)$this->input->post('process_type');
		$process_emp_code    = $this->input->post('process_emp_id');
		$process_role        = (int)$this->input->post('process_role');
		$process_mode        = $this->input->post('process_mode');
		$logged_id           = $this->session->userdata('logged_id');		
		$today_date          = date("Y-m-d H:i:s");
		$fin_id              = $this->financial_info[0]->prime_financial_setting_id;
		$start_date          = $this->financial_info[0]->start_date;
		$end_date            = $this->financial_info[0]->end_date;
		$start_date_year 	 = explode('-', $start_date);
		$end_date_year 		 = explode('-', $end_date);
		$get_year 			 = $start_date_year[0].'-'.substr($end_date_year[0], 2, 3);
		
		if($process_type === 1){
			$cond_query  = ' and emp_code = "'.$process_emp_code.'" ';
		}elseif($process_type === 2){
			$cond_query    = ' and emp_role = "'.$process_role.'"';
			// $cond_query    = ' and emp_role = "1"';
		}else{
			$cond_query     = "";
		}
		
		//Form 24Q Excel Generations
		if($process_mode == 2){
			$excel_rslt = $this->get_24Q_excel_data();
			$sts = 1;
			echo json_encode(array('success' => TRUE, 'excel_rslt' => $excel_rslt,'sts' => $sts));
			exit(0);
		}else{
			$sts = 2;
			//incharge name and designation info
			$incharge_data_qry = 'select * from cw_employees inner join cw_financial_setting on cw_financial_setting.incharge_employee=cw_employees.employee_code inner join cw_position on cw_position.prime_position_id = cw_employees.designation where cw_employees.trans_status =1 and cw_employees.termination_status = 0 and cw_employees.role != 1 and prime_financial_setting_id = "'.$fin_id.'"';
			$incharge_data        = $this->db->query("CALL sp_a_run ('SELECT','$incharge_data_qry')");
			$incharge_data_result = $incharge_data->result();
			$incharge_data->next_result();
			$incharge_name         = $incharge_data_result[0]->emp_name;
			$incharge_designation  = $incharge_data_result[0]->designation;
			$incharge_father_name  = $incharge_data_result[0]->father_name;
			$incharge_place        = $incharge_data_result[0]->place;
			$incharge_father_name  = $incharge_data_result[0]->father_name;
			$sign_date             = date("d/m/Y");
			
			$td_class= "border-right: solid 1px black; border-left: solid 1px black;";
			//need to start to process
			$form_temp_qry   = 'select * from `cw_form16_template` inner join cw_form_sixteen on cw_form_sixteen.prime_form_sixteen_id=cw_form16_template.prime_form_sixteen_id where cw_form_sixteen.trans_status = 1 and fin_set_id = "'.$fin_id.'" and cw_form16_template.prime_form_sixteen_id !=1 order by tax_display_order asc';
			$form_temp_data    = $this->db->query("CALL sp_a_run ('SELECT','$form_temp_qry')");
			$form_temp_result  = $form_temp_data->result();
			$form_temp_data->next_result();
			
			$tax_process_exit_qry   = 'select count(*) as rslt_count from `cw_tax_calculation` where trans_status = 1 and fin_set_id = "'.$fin_id.'"';
			$tax_process_exit_data    = $this->db->query("CALL sp_a_run ('SELECT','$tax_process_exit_qry')");
			$tax_process_exit_result  = $tax_process_exit_data->result();
			$tax_process_exit_data->next_result();
			$tax_rslt_count = $tax_process_exit_result[0]->rslt_count;
			if((int)$tax_rslt_count === 0){
				echo json_encode(array('success' => False, 'msg' => "Tax is not processed for this finanncial year!"));
				exit(0);
			}else{
				$form_exit_qry   = 'select count(*) as form_rslt_count from `cw_form16_template` where trans_status = 1 and fin_set_id = "'.$fin_id.'"';
				$form_exit_data    = $this->db->query("CALL sp_a_run ('SELECT','$form_exit_qry')");
				$form_exit_result  = $form_exit_data->result();
				$form_exit_data->next_result();
				$form_rslt_count = $form_exit_result[0]->form_rslt_count;
				if((int)$form_rslt_count === 0){
					echo json_encode(array('success' => False, 'msg' => "Form Template is not created for this finanncial year!"));
					exit(0);
				}else{
					$process_emp_qry   = 'select * from `cw_tax_calculation`  inner join cw_employees  on cw_tax_calculation.emp_code=cw_employees.employee_code where fin_set_id = "'.$fin_id.'" and cw_tax_calculation.trans_status =1 '.$cond_query.'';
					$process_emp_data    = $this->db->query("CALL sp_a_run ('SELECT','$process_emp_qry')");
					$process_emp_result  = $process_emp_data->result();
					$process_emp_data->next_result();
					$tax_data = array();
					$pdf_data = array();
					foreach($process_emp_result as $emp_rslt => $val){
						$emp_code = $val->emp_code;
						$emp_name = $val->emp_name;
						foreach($val as $emp_key =>$emp_val){
							$tax_data[$emp_key] = $emp_val;
						}
						$tr_line ="<tr><td style='width:70%;$td_class'></td><td style='text-align:center;width:10%;$td_class'>Rs.</td><td style='text-align:center;width:10%;$td_class'>Rs.</td><td style='text-align:center;width:10%;$td_class'>Rs.</td></tr>";
						foreach($form_temp_result as $form_tmp_rslt){
							$form_id      = $form_tmp_rslt->prime_form_sixteen_id;
							$tax_act      = $form_tmp_rslt->tax_act;
							$form16_key   = $form_tmp_rslt->form16_key;
							$form16_value = $form_tmp_rslt->form16_value;
							$tax_value    = $tax_data[$form16_value];
							if($form16_value === 'taxsubsec_1'){
								$tax_value    = $tax_data['hra'];
							}
							if($tax_value > 0){
								$tax_value = "Rs. ".$tax_value;
							}
							if(strstr($form16_value, 'tot')){
								$tr_line     .="<tr><td style='width:70%;$td_class'>$tax_act</td><td style='text-align:right;width:10%;$td_class'></td><td style='text-align:right;width:10%;$td_class'></td><td style='text-align:right;width:10%;$td_class'>$tax_value</td></tr>";
							}elseif((int)$form_id === 7){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 7){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 15){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 16){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 17){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 18){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 19){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 46){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 47){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 48){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 49){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((int)$form_id === 50){
								if(empty($tax_value) || $tax_value =="0.00"){
									$tr_line .= "";
								}
							}elseif((strstr($form16_value, 'taxsubsec')) || (strstr($form16_value, 'otherin')) || (strstr($form16_value, 'gross_17'))){
								$tr_line     .="<tr><td style='width:40%;$td_class'>$tax_act</td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'>$tax_value</td><td style='text-align:right;width:20%;$td_class'></td></tr>";
							}elseif((int)$form_id === 34){
								$tr_line     .="<tr><td style='width:40%;$td_class'>$tax_act</td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'></td></tr>";
								$tr_line    .="<tr><td style='width:40%;$td_class'></td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'>Gross Amount</td><td style='text-align:right;width:20%;$td_class'>Deductible Amount</td></tr>";
							}elseif((int)$form_id === 42){
								$tr_line     .="<tr><td style='width:40%;$td_class'>$tax_act</td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'></td></tr>";
								$tr_line    .="<tr><td style='width:40%;$td_class'></td><td style='text-align:right;width:20%;$td_class'>Gross Amount</td><td style='text-align:right;width:20%;$td_class'>Qualifying Amount</td><td style='text-align:right;width:20%;$td_class'>Deductible Amount</td></tr>";
							}else{
								$tr_line     .="<tr><td style='width:40%;$td_class'>$tax_act</td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'></td><td style='text-align:right;width:20%;$td_class'>$tax_value</td></tr>";
							}
						}
						$table_data = "
						<h3 style='margin:10px;font-size:14px;text-align: center;'>FORM NO. 16<br/>&#8220;Part B (Annexure)&#8221;<br/>DETAILS OF SALARY PAID AND ANY OTHER INCOME AND TAX DEDUCTED</h3>
						<table style='width:100%;border-collapse: collapse;border-spacing: 0;border-color: #CCCCCC !important;border: 0px;text-align: left;font-size: 12px;background-color: #FFFFFF;' border='1' cellspacing='0' cellpadding='4'>
						<tbody>
							$tr_line
						</tbody>
						</table>
						<p>I  $incharge_name,  Son of $incharge_father_name  working in the capacity of  $incharge_designation do hereby  certify that the information given above is true, complete and correct and is based on the books of account, documents, TDS statements, and other available records.</p>
						<p><b>Place:</b> $incharge_place</p>
						<p><b>Date:</b> $sign_date</p>
						<p style='margin-left:40% !important;'>Signature of the person  responsible for deduction of tax</p>
						<p style='margin-left:40% !important;'>Full name: <b>$incharge_name</b></p>
						<p style='margin-left:40% !important;'>Designation: <b>$incharge_designation</b></p>";
						$table_data = "<!DOCTYPE html><html> <body><p style='margin-top:0px;text-align:right;'>$emp_code - $emp_name</p>".$table_data."</body></html>";
						$pdf_data[$emp_code] = $table_data;
					}
				}
			}
		}
		$count = count($pdf_data);
		echo json_encode(array('success' => TRUE, 'pdf_data' => $pdf_data,'count' => $count,'sts'=>$sts,'get_year'=>$get_year));
	}
	
	public function generate_pdf(){
		$employee_code  = $this->input->post('code');
		$content        = $this->input->post('html');
		$get_year  		= $this->input->post('get_year');
		$set_paper 		= "portrait";
		$folder 		= "form_16B/form_16B_".$get_year;
		$path 			= $this->common_generate_pdf($content,$folder,$employee_code,$set_paper,'A4','','');
		echo $path;
	}
	
	public function get_24Q_excel_data(){
		$fin_id = $this->financial_info[0]->prime_financial_setting_id;
		$start_date = date('d/m/Y', strtotime($this->financial_info[0]->start_date));
		$end_date   = date('d/m/Y', strtotime($this->financial_info[0]->end_date));
		$financial_date = $start_date."--".$end_date;
		$filename  = dirname(__FILE__)."/php_excel/PHPExcel/IOFactory.php";
		include($filename);
		$excel2 = PHPExcel_IOFactory::createReader('Excel5');
		$excel2 = $excel2->load('./excel_write/FORM24QA.xls');//default excel template
		$excel_sheet = $excel2->setActiveSheetIndex(0);
		$high_column  = $excel_sheet->getHighestColumn();
		$hign_col_num = PHPExcel_Cell::columnIndexFromString($high_column);
		$col_name = array();
		for($i=0; $i<$hign_col_num; $i++){
			$letter = $this->getNameFromNumber($i);
			$col_name[] = $letter;
		}
		$headings = $excel_sheet->rangeToArray('A3:' . $high_column . 3, NULL, TRUE,FALSE);
		$head_arr = array_combine($headings[0],$col_name);
		//collect the data from tax and employee master
		
		$emp_tax_data_qry = 'select * from cw_tax_calculation inner join cw_employees on cw_employees.employee_code = cw_tax_calculation.emp_code where cw_tax_calculation.trans_status =1 and fin_set_id="'.$fin_id.'"';
		$emp_tax_data    = $this->db->query("CALL sp_a_run ('SELECT','$emp_tax_data_qry')");
		$emp_tax_result  = $emp_tax_data->result();
		$emp_tax_data->next_result();
		$emp_tax_count  = $emp_tax_data->num_rows();
		
		$form24_temp_data_qry = 'select * from cw_form24q_template where cw_form24q_template.trans_status =1';
		$form24_temp_data    = $this->db->query("CALL sp_a_run ('SELECT','$form24_temp_data_qry')");
		$form24_temp_result  = $form24_temp_data->result();
		$form24_temp_data->next_result();
		
		$form24_col = array();
		foreach($form24_temp_result as $form24_rslt){
			$form24q_mapping = $form24_rslt->form24q_mapping;
			$form24q_value   = $form24_rslt->form24q_value;
			if((int)$form24q_mapping > 0){
				$form24_col[$form24q_mapping] = $form24q_value;
			}
		}
		$j = 5;
		$i = 1;
		foreach($emp_tax_result as $emp_tax_rslt){
			$emp_code    = $emp_tax_rslt->emp_code;
			$emp_name    = $emp_tax_rslt->emp_name;
			$pan_number  = $emp_tax_rslt->pan_number;
			$slab_type   = $emp_tax_rslt->slab_type;
			
			if((int)$slab_type === 1){
				$slab_type_val = "O";
			}elseif((int)$slab_type === 2){
				$slab_type_val = "SC";
			}else{
				$slab_type_val = "SSC";
			}
			$excel2->getActiveSheet()->setCellValue("A$j", "$i").";";//default serial number is fixed in excel column.
			$excel2->getActiveSheet()->setCellValue("B$j", "$pan_number").";";//default pan number is fixed in excel column.
			$excel2->getActiveSheet()->setCellValue("C$j", "$emp_name").";";//default employee name is fixed in excel column.
			$excel2->getActiveSheet()->setCellValue("D$j", "$slab_type_val").";";//default slab type is fixed in excel column.
			$excel2->getActiveSheet()->setCellValue("E$j", "$financial_date").";";//default financial setting date is fixed in excel column.
			foreach($form24_col as $form24_key => $form24_val){
				$head_val = $head_arr['('.$form24_key.')'];
				$tax_data_qry = 'select '.$form24_val.' from cw_tax_calculation where cw_tax_calculation.trans_status =1 and fin_set_id="'.$fin_id.'" and emp_code = "'.$emp_code.'"';
				$tax_data  = $this->db->query("CALL sp_a_run ('SELECT','$tax_data_qry')");
				$tax_data_result  = $tax_data->result();
				$tax_data->next_result();
				$tax_value = $tax_data_result[0]->$form24_val;
				$excel2->getActiveSheet()->setCellValue("$head_val$j", "$tax_value")->getStyle("$head_val$j")->getNumberFormat()->setFormatCode('0.00').";";
			}
			$excel2->getActiveSheet()->setCellValue("AW$j", "$emp_code").";";//default employee code is fixed in excel column. 
			$j++;
			$i++;
		}
		$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel5');
		$objWriter->save('./excel_write/ABC_COMPANY_FORM24QA.xls');//dynamic company
		$excel_path = './excel_write/ABC_COMPANY_FORM24QA.xls';
		return $excel_path;
	}
	
	//ABC generator for excel column values
	public function getNameFromNumber($num){
		$numeric = $num % 26;
		$letter = chr(65 + $numeric);
		$num2 = intval($num / 26);
		if ($num2 > 0) {
			return $this->getNameFromNumber($num2 - 1) . $letter;
		} else {
			return $letter;
		}
	}
}
?>