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/.trash/application.1/controllers/Approval.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Approval  extends Action_controller{	
	public function __construct(){
		parent::__construct('approval');
	}
	
	// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
	public function index(){
		$data['encKey']        = $this->generateKey();
		$this->page_info();
		$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;
		$data['components']['weekoff_component']  = $this->time_setting_components();
		/* Custom Code for Weekoff Component */
		$component_query  = 'SELECT pick_table,pick_list,components FROM cw_general_setting inner join cw_form_setting on cw_form_setting.prime_form_id = cw_general_setting.components WHERE entry_parameter_type = 3 and cw_general_setting.trans_status = 1';
		$component_info   = $this->db->query("CALL sp_a_run ('SELECT','$component_query')");
		$component_result = $component_info->result();
		$component_info->next_result();		
		$pick_list   = $component_result[0]->pick_list;
		$pick_table = $component_result[0]->pick_table;
		$components = $component_result[0]->components;
		$pick_list_val   = explode(",",$pick_list);
		$pick_list_val_1 = $pick_list_val[0];
		$pick_list_val_2 = $pick_list_val[1];
		if($pick_table){
			$pick_query = "select $pick_list from $pick_table where trans_status = 1";
			$pick_data   = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
			$pick_result = $pick_data->result();
			$pick_data->next_result();
			if($pick_result){
				$pick_key   = array_column($pick_result, $pick_list_val_1);
				$pick_val   = array_column($pick_result, $pick_list_val_2);
				$final_pick = array_combine( $pick_key, $pick_val);
			}
			$this->pick_list['component_value']['array_list'] = array("" => "---- Component Value----") + $final_pick;
			$data['master_pick']   = $this->pick_list;
		}		
		$leave_financial_info      = $this->get_leave_financial_details();
		$prime_financial_id        = $leave_financial_info[0]->prime_leave_financial_year_id;

		//FUNCTION FOR USING TO MANAGE MODULE EDIT VIEW AND CANCEL BUTTON VALIDATION ARRAY GET
		$result                    = $this->request_data_arr($prime_financial_id);

		$data['leave_status_arr']  = $result['leave_status_arr'];
		$data['leave_cancel_arr']  = $result['leave_cancel_arr'];
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//FUNCTION FOR USING TO MANAGE MODULE EDIT VIEW AND CANCEL BUTTON VALIDATION ARRAY GET
	public function request_data_arr($prime_financial_id){
		$leave_status_query    = 'SELECT prime_approval_id,leave_status,cancellation_request FROM cw_approval WHERE cw_approval.trans_status = 1 and financial_setting_id = "'.$prime_financial_id.'"';

		$leave_status_info     = $this->db->query("CALL sp_a_run ('SELECT','$leave_status_query')");
		$leave_status_result   = $leave_status_info->result_array();
		$leave_status_info->next_result();
		
		$rslt                  = array();
		$leave_status_arr      = array();
		$leave_cancel_arr      = array();
		foreach($leave_status_result as $arr){
			$approval_id                    = $arr['prime_approval_id'];
			$leave_status                   = $arr['leave_status'];
			$cancel_request                 = $arr['cancellation_request'];
			$leave_status_arr[$approval_id] = $leave_status;
			$leave_cancel_arr[$approval_id] = $cancel_request;
		}
		//ALL ARRAYS ADD TO FINAL ARRAY
		$rslt['leave_status_arr']   = $leave_status_arr;
		$rslt['leave_cancel_arr']   = $leave_cancel_arr;

		return $rslt;
	}

	//LOAD TABLE WITH FILTERS
	public function search(){
		$dec_data         = $this->cryptoDecrypt($_POST['Payload']);
		$_POST            = $dec_data['data'];
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Invalid Request..'));
			exit(0);
		}	
		$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;
			}
			//CURRENT FINANCIAL YEAR ID
			$leave_financial_info = $this->get_leave_financial_details();
			$prime_financial_id   = $leave_financial_info[0]->prime_leave_financial_year_id;

			$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 '.$this->prime_table.'.trans_status = 1 AND financial_setting_id = '.$prime_financial_id.' '.$role_condition.$fliter_query.$common_search;
			
			$search_query      .= " where $this->prime_table.trans_status = 1 AND financial_setting_id = $prime_financial_id $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));		
		}
	}
	
	//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' => 'Invalid Request..','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
		
		//DEPENDENT AUTO PICKLIST
		$data['auto_pick']              = $this->depen_auto_list;//dependent auto result			
		$data['condition_list']         = $this->form_condition_list;
		
		/* Custom Code for Weekoff Component */
		$component_query  = 'SELECT pick_table,pick_list,components FROM cw_general_setting inner join cw_form_setting on cw_form_setting.prime_form_id = cw_general_setting.components WHERE entry_parameter_type = 3 and cw_general_setting.trans_status = 1';
		$component_info   = $this->db->query("CALL sp_a_run ('SELECT','$component_query')");
		$component_result = $component_info->result();
		$component_info->next_result();			
		$pick_list   = $component_result[0]->pick_list;
		$pick_table = $component_result[0]->pick_table;
		$components = $component_result[0]->components;
		$pick_list_val   = explode(",",$pick_list);
		$pick_list_val_1 = $pick_list_val[0];
		$pick_list_val_2 = $pick_list_val[1];

		$pick_query = "select $pick_list from $pick_table where trans_status = 1";
		$pick_data   = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
		$pick_result = $pick_data->result();
		$pick_data->next_result();
		if($pick_result){
			$pick_key   = array_column($pick_result, $pick_list_val_1);
			$pick_val   = array_column($pick_result, $pick_list_val_2);
			$final_pick = array_combine( $pick_key, $pick_val);
		}

		$this->pick_list[$components]['array_list'] = array("" => "---- $components ----") + $final_pick;		
		$data['weekoff_comp']   = $this->time_setting_components();		
		$data['all_pick']       = $this->pick_list;
		$data['components']     = $components;

		$this->view_select = $this->view_select.",first_level_approval,second_level_approval";
		//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
		$logged_emp_code      		   = $this->session->userdata('logged_emp_code');
		$data['empdata']               = $this->get_emp_approval_data($form_view_id);
		$data['get_depend_prime_id']   = $this->get_depend_fun();
		$data['depend_label_id']   	   = $this->depend_label_fun();	
		$data['max_allow_date']        = $this->company_info[0]->max_allow_date;
		$data['max_date_based']        = $this->company_info[0]->max_date_based;
		$data['mp_treat_time_log']     = $this->company_info[0]->mp_treat_as;
		$this->load->view("$this->control_name/form",$data);
	}
	
	//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' => 'Invalid Request..','table_data' => ""));
			exit(0);
		}
		//SAVE INFO FUNCTION
		$this->save_info();	
		$module_sts          = (int)$this->module_sts;
		$mp_treat_time_log   = $this->company_info[0]->mp_treat_as;
		if(!$module_sts){
			echo json_encode(array('success' => FALSE, 'message' => "Save Info Query Process Error...!"));
			exit(0);
		}else{
			$post_data             = array();
			$unq_chk               = array();
			$prime_qry_key         = "";
			$prime_qry_value       = "";
			$prime_upd_query       = "";
			$form_id               = $this->input->post($this->prime_id);	
			//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' => 'Invalid Request..','table_data' => ""));
					exit(0);
				}
			}else{
				$form_id = 0;
			}
			$financial_info        = $this->get_leave_financial_details();
			$prime_financial_id    = $financial_info[0]->prime_leave_financial_year_id;	
			// $permission_date       = $this->input->post('permission_date');	
			$leave_sts_approve_arr = array("first_approval_leave_status","second_approval_leave_status","leave_status","first_approval_cancel_status","second_approval_cancel_status");
			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 === 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));
				}	
				$value     = str_replace('"',"~",$value);
				$value     = str_replace("'","`",$value);
				$value     = str_replace("&","^",$value);	
				
			$value = str_replace('"',"xdbquot",$value);
			$value = str_replace("'","xquot",$value);
			$value = str_replace("&","xxamp",$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){
					  	if(!in_array($label_id,$leave_sts_approve_arr)){
							$prime_qry_key     .= $label_id.",";
							$prime_qry_value   .= '"'.$value.'",';
							$prime_upd_query   .= $label_id.' = "'.$value.'",';
							$post_data[$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,);
						}
					}
				}
				if($label_id === "request_type"){
					$request_type  = $value;
				}
				if($label_id === "from_date"){
					$from_date     = $value;
				}
				if($label_id === "to_date"){
					$to_date       = $value;
				}
				if($label_id === "cancellation_request"){
					$cancel_request      = (int)$value;
				}
				if($label_id === "permission_date"){
					$permission_date      = $value;
				}
			}

			//Generate Approved status based on approval type - START
			$approve_detail_qry   = 'select employee_code,first_level_approval,second_level_approval,request_type,leave_approve_type,first_approval_leave_status,second_approval_leave_status,leave_status,cancellation_request,first_approval_cancel_status,second_approval_cancel_status,permission_date,device_code,in_date,out_date,from_date,to_date,shift_date from cw_approval where prime_approval_id = "'.$form_id.'" and trans_status = 1';
			$approve_detail_info  = $this->db->query("CALL sp_a_run ('SELECT','$approve_detail_qry')");
			$approve_detail_rslt  = $approve_detail_info->result_array();
			$approve_detail_info->next_result();
			$logged_emp_code      = $this->logged_emp_code;
			$logged_id            = $this->logged_id;
			$employee_code        = $approve_detail_rslt[0]['employee_code'];
			$first_level          = $approve_detail_rslt[0]['first_level_approval'];
			$second_level         = $approve_detail_rslt[0]['second_level_approval'];
			$request_type         = $approve_detail_rslt[0]['request_type'];
			$approve_type         = (int)$approve_detail_rslt[0]['leave_approve_type'];
			$first_leave_status   = (int)$approve_detail_rslt[0]['first_approval_leave_status'];
			$second_leave_status  = (int)$approve_detail_rslt[0]['second_approval_leave_status'];
			$first_cancel_status  = (int)$approve_detail_rslt[0]['first_approval_cancel_status'];
			$second_cancel_status = (int)$approve_detail_rslt[0]['second_approval_cancel_status'];
			$cancel_request       = (int)$approve_detail_rslt[0]['cancellation_request'];
			$permission_date      = $approve_detail_rslt[0]['permission_date'];
			$leave_status         = $approve_detail_rslt[0]['leave_status'];
			$device_code          = $approve_detail_rslt[0]['device_code'];
			$approve_date         = "";
			if($request_type === "1" || $request_type === "3" || $request_type === "8"){
				$from_date      = date("Y-m-d",strtotime($approve_detail_rslt[0]['from_date']));
				$to_date      = date("Y-m-d",strtotime($approve_detail_rslt[0]['to_date']));
			}else
			if($request_type === "4" || $request_type === "5"){
				$request_date      = date("Y-m-d",strtotime($approve_detail_rslt[0]['permission_date']));
				$from_date         = $request_date;
				$to_date           = $request_date;
			}else
			if($request_type === "7"){
				$request_date      = date("Y-m-d",strtotime($approve_detail_rslt[0]['shift_date']));
				$from_date         = $request_date;
				$to_date           = $request_date;
			}

			//leave status update based on first and second level
			if($cancel_request === 1){ //IF CANCELLED				
				if($logged_emp_code === $first_level){
					$first_cancel_status   = (int)$this->input->post('first_approval_cancel_status');				
				}
				if($logged_emp_code === $second_level){
					$second_cancel_status  = (int)$this->input->post('second_approval_cancel_status');
				}

				if($approve_type === 1){ //ANYONE
					if($first_cancel_status === 2 || $second_cancel_status  === 2){
						$leave_status         = 4;
						$approve_date         = date("Y-m-d");
					}else
					if($first_cancel_status === 3 || $second_cancel_status  === 3){
						$leave_status         = 2;
					}
				}else
				if($approve_type === 2){ //BOTH
					if($first_cancel_status === 2 && $second_cancel_status  === 2){
						$leave_status         = 4;
						$approve_date         = date("Y-m-d");
					}else
					if($first_cancel_status === 3 && $second_cancel_status  === 3){
						$leave_status         = 2;
					}else
					if($first_cancel_status === 3 || $second_cancel_status  === 3){
						$leave_status         = 3;
					}
				}else
				if($approve_type === 3){ //ONLY FIRST LEVEL
					if($first_cancel_status === 2){
						$leave_status         = 4;
						$approve_date         = date("Y-m-d");
					}else
					if($first_cancel_status === 3){
						$leave_status         = 2;
					}
				}else
				if($approve_type === 4){ //ONLY SECOND LEVEL
					if($second_cancel_status === 2){
						$leave_status         = 4;
						$approve_date         = date("Y-m-d");
					}else
					if($second_cancel_status === 3){
						$leave_status         = 2;
					}
				}				
				$post_data['first_approval_cancel_status']  = $first_cancel_status;
				$post_data['second_approval_cancel_status'] = $second_cancel_status;
				$post_data['leave_status']                  = $leave_status;
			}else{
				//leave status update based on first and second level
				if($logged_emp_code === $first_level){
					$first_leave_status   = (int)$this->input->post('first_approval_leave_status');				
				}
				if($logged_emp_code === $second_level){
					$second_leave_status  = (int)$this->input->post('second_approval_leave_status');
				}
				if($approve_type === 1){ //ANYONE
					if($first_leave_status === 2 || $second_leave_status  === 2){
						$leave_status     = 2;
						$approve_date     = date("Y-m-d");
					}else
					if($first_leave_status === 3 || $second_leave_status  === 3){
						$leave_status     = 3;
					}
				}else
				if($approve_type === 2){ //BOTH
					if($first_leave_status === 2 && $second_leave_status  === 2){
						$leave_status     = 2;
						$approve_date     = date("Y-m-d");
					}else
					if($first_leave_status === 3 && $second_leave_status  === 3){
						$leave_status     = 3;
					}else
					if($first_leave_status === 3 || $second_leave_status  === 3){
						$leave_status     = 3;
					}
				}else
				if($approve_type === 3){ //ONLY FIRST LEVEL
					if($first_leave_status === 2){
						$leave_status     = 2;
						$approve_date     = date("Y-m-d");
					}else
					if($first_leave_status === 3){
						$leave_status     = 3;
					}
				}else
				if($approve_type === 4){ //ONLY SECOND LEVEL
					if($second_leave_status === 2){
						$leave_status     = 2;
						$approve_date     = date("Y-m-d");
					}else
					if($second_leave_status === 3){
						$leave_status     = 3;
					}
				}
				$post_data['first_approval_leave_status']  = $first_leave_status;
				$post_data['second_approval_leave_status'] = $second_leave_status;
				$post_data['leave_status']                 = $leave_status;		
				if($request_date){
					$ot_check_qry          = 'select sum(over_time) as over_time from cw_overtime_entry where employee_code="'.$employee_code.'" and entry_date = "'.$request_date.'"';
					$over_time_info        = $this->db->query($ot_check_qry);
					$over_time_rslt        = $over_time_info->result_array();
					$over_time_info->next_result();
					$over_time             = $over_time_rslt[0]->over_time;
					if($over_time > 0){
						echo json_encode(array("success"=>false,"message"=>"Overtime already exist in this date, please cancel Your previous OT Request...!"));
						exit(0);
					}
				}
			}
			$post_data['employee_code']                    = $employee_code;
			$post_data['device_code']                      = $device_code;
			$post_data['cancellation_request']             = $cancel_request;
			//Generate Approved status based on approval type - END
			$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;	
					//FUNCTION FOR USING TO MANAGE MODULE EDIT VIEW AND CANCEL BUTTON VALIDATION ARRAY GET
					$result             = $this->request_data_arr($prime_financial_id);
					$leave_status_arr   = $result['leave_status_arr'];
					$leave_cancel_arr   = $result['leave_cancel_arr'];

					echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id, 'leave_status_arr' => $leave_status_arr, 'leave_cancel_arr' => $leave_cancel_arr));
				}else{
					$prime_upd_query    .= 'first_approval_leave_status = "'.$first_leave_status.'",second_approval_leave_status = "'.$second_leave_status.'",first_approval_cancel_status = "'.$first_cancel_status.'",second_approval_cancel_status = "'.$second_cancel_status.'",leave_status = "'.$leave_status.'",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')");
					if($request_type === "1" || $request_type === "2" || $request_type === "3" || $request_type === "8"){	
						$this->approval_process($post_data,$form_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
					}else
					if($request_type === "4" || $request_type === "5"){
						$this->permission_approval($post_data,$form_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
						
					}else
					if($request_type === "6"){
						$this->shift_change_approval($post_data,$form_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
					}else
					if($request_type === "7"){
						$this->manual_punch_approval($post_data,$form_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
					}
					if($cancel_request === 1 ){ 
						$time_entry_delete_qry   = 'delete from cw_time_entry where employee_code="'.$employee_code.'" and att_date  between "'.$from_date.'" and "'.$to_date.'"';
						$del_qry_info            = $this->db->query("CALL sp_a_run ('RUN','$time_entry_delete_qry')");
						$leave_status = 4;
					}else{
						$leave_status=2;
					}
					if((int)$request_type === 7 && (int)$mp_treat_time_log === 1){						
						$time_log_rslt    = $this->time_log($post_data,$prime_financial_id,$leave_status);
						$success          = $time_entry_rslt['success'];
					}
					//CALL PROCEDURE
					/*$qry_info          = $this->db->query("CALL itsp_prcatt ('$from_date','$to_date','$employee_code')");
					$qry_result        = $qry_info->result();
					$qry_info->next_result();*/
					//FUNCTION FOR USING TO MANAGE MODULE EDIT VIEW AND CANCEL BUTTON VALIDATION ARRAY GET
					$result             = $this->request_data_arr($prime_financial_id); 
					$leave_status_arr   = $result['leave_status_arr'];
					$leave_cancel_arr   = $result['leave_cancel_arr'];
					echo json_encode(array('success' => TRUE, 'message' => "Successfully Updated.!",'insert_id' => $form_id, 'leave_status_arr' => $leave_status_arr, 'leave_cancel_arr' => $leave_cancel_arr));
				}
			}
		}
	}
	
	//Bulk Approval Process
	public function bulk_approve(){
		$encString     = file_get_contents('php://input');
		$_POST         = $this->cryptoDecrypt($encString);
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Invalid Request..','table_data' => ""));
			exit(0);
		}
		$approve_ids           = implode('","',$this->input->post('approve_ids'));
		$created_on            = date("Y-m-d H:i:s");
		$financial_info        = $this->get_leave_financial_details();
		$prime_financial_id    = $financial_info[0]->prime_leave_financial_year_id;

		//COMPANY INFORMATION DETAILS GET
		$company_info          = $this->company_info();
		$mp_treat_time_log     = $company_info[0]->mp_treat_as;

		$approve_detail_qry    = 'select * from cw_approval where prime_approval_id in ("'.$approve_ids.'") and leave_status = 1 and cancellation_request = 2 and trans_status = 1';
		$approve_detail_info   = $this->db->query("CALL sp_a_run ('SELECT','$approve_detail_qry')");
		$approve_detail_rslt   = $approve_detail_info->result_array();
		$approve_detail_info->next_result();
		
		$bulk_approve_arr      = array();
		foreach ($approve_detail_rslt as $app_key => $app_val) {
			$app_id            = $app_val['prime_approval_id'];
			$bulk_approve_arr[$app_id]  = $app_val;
		}

		if(count($bulk_approve_arr)){
			$sts                   = true;
			$log_user_role         = (int)$this->logged_user_role;
			$logged_emp_code       = $this->logged_emp_code;
			$logged_id             = $this->logged_id;
			$approval_upd_value    = ""; 
			$approved_rows         = 0;
			foreach ($bulk_approve_arr as $key => $arr_value) {
				$prime_approval_id    = $arr_value['prime_approval_id'];
				$employee_code        = $arr_value['employee_code'];
				$first_level          = $arr_value['first_level_approval'];
				$second_level         = $arr_value['second_level_approval'];
				$request_type         = $arr_value['request_type'];
				$approve_type         = (int)$arr_value['leave_approve_type'];
				$leave_status         = (int)$arr_value['leave_status'];
				$first_leave_status   = (int)$arr_value['first_approval_leave_status'];
				$second_leave_status  = (int)$arr_value['second_approval_leave_status'];
				$cancel_request       = (int)$arr_value['cancellation_request'];
				$permission_date      = $arr_value['permission_date'];
				$from_date            = $arr_value['from_date'];
				$to_date              = $arr_value['to_date'];
				$approve_date         = "";
				//leave status update based on first and second level
				if($logged_emp_code === $first_level){
					$first_leave_status   = 2;
				}
				if($logged_emp_code === $second_level){
					$second_leave_status  = 2;
				}
				if($approve_type === 1){
					if($first_leave_status === 2 || $second_leave_status  === 2){
						$leave_status         = 2;
						$approve_date         = date("Y-m-d");
					}
				}else
				if($approve_type === 2){
					if($first_leave_status === 2 && $second_leave_status  === 2){
						$leave_status         = 2;
						$approve_date         = date("Y-m-d");
					}
				}else
				if($approve_type === 3){
					if($first_leave_status === 2){
						$leave_status         = 2;
						$approve_date         = date("Y-m-d");
					}
				}else
				if($approve_type === 4){
					if($second_leave_status === 2){
						$leave_status         = 2;
						$approve_date         = date("Y-m-d");
					}
				}
				//TO CHECK TIME ENTRY TABLE
				if($leave_status === 2 && $cancel_request === 2){
					if($request_type === "1" || $request_type === "2" || $request_type === "3" || $request_type === "7" || $request_type === "8" || $request_type === "4"){
						if($request_type === "4"){
							$success          = "true";
						}else{
							if((int)$request_type === 7 && (int)$mp_treat_time_log === 1){
								$time_log_rslt    = $this->time_log($arr_value,$prime_financial_id,$leave_status);
								$success          = $time_entry_rslt['success'];
							}
						}
						if($success === "false"){
							$sts     = false;
							$msg     = $time_entry_rslt['message'];
					 	}else{
							//APPROVAL UPD QUERY
							$approval_upd_qry    = 'UPDATE cw_approval SET first_approval_leave_status = "'.$first_leave_status.'",second_approval_leave_status = "'.$second_leave_status.'",leave_status = "'.$leave_status.'",approve_date = "'.$approve_date.'",trans_updated_by = "'.$logged_id.'",trans_updated_date = "'.$created_on.'" WHERE prime_approval_id = "'.$prime_approval_id.'"';
							$approval_upd_info   = $this->db->query($approval_upd_qry);
							if($approval_upd_info){
								//TO UPDATE A REQUEST AND THIS REQUEST TYPE RELATED ENTRY MODULE
								if($request_type === "1" || $request_type === "2" || $request_type === "3" || $request_type === "8"){	
									$this->approval_process($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
								}else
								if($request_type === "4" || $request_type === "5"){
									$this->permission_approval($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
								}
								else
								if($request_type === "7"){
									$this->manual_punch_approval($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
								}
								$approved_rows++;
							}
						}
					}else
					if($request_type === "6"){
						//THIS IS NOT SAME AS OTHER REQUEST 
						//APPROVAL UPD QUERY
						$approval_upd_qry    = 'UPDATE cw_approval SET first_approval_leave_status = "'.$first_leave_status.'",second_approval_leave_status = "'.$second_leave_status.'",leave_status = "'.$leave_status.'",approve_date = "'.$approve_date.'",trans_updated_by = "'.$logged_id.'",trans_updated_date = "'.$created_on.'" WHERE prime_approval_id = "'.$prime_approval_id.'"';
						$approval_upd_info   = $this->db->query($approval_upd_qry);

						if($approval_upd_info){
							$this->shift_change_approval($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
							$approved_rows++;
						}
					}
				}else{
					//APPROVAL UPD QUERY
					$approval_upd_qry    = 'UPDATE cw_approval SET first_approval_leave_status = "'.$first_leave_status.'",second_approval_leave_status = "'.$second_leave_status.'",leave_status = "'.$leave_status.'",approve_date = "'.$approve_date.'",trans_updated_by = "'.$logged_id.'",trans_updated_date = "'.$created_on.'" WHERE prime_approval_id = "'.$prime_approval_id.'"';
					$approval_upd_info   = $this->db->query($approval_upd_qry);

					if($approval_upd_info){
						//TO UPDATE A REQUEST AND THIS REQUEST TYPE RELATED ENTRY MODULE
						if($request_type === "1" || $request_type === "2" || $request_type === "3" || $request_type === "8"){	
							$this->approval_process($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
						}else
						if($request_type === "4" || $request_type === "5"){
							$this->permission_approval($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
						}else
						if($request_type === "6"){
							$this->shift_change_approval($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
						}else
						if($request_type === "7"){
							$this->manual_punch_approval($arr_value,$prime_approval_id,$prime_financial_id,$first_leave_status,$second_leave_status,$leave_status);
						}
						$approved_rows++;
					}
				}
			}
			if($sts){
				echo json_encode(array('success' => TRUE, 'message' => "Successfully Approved.!"));
			}else{
				//ALL SELECTED ROWS ERROR
				if($approved_rows === 0){
					echo json_encode(array('success' => FALSE, 'message' => "Data Already Exist in Time Entry Table.!"));
				}else{
					echo json_encode(array('success' => FALSE, 'message' => "Successfully Approved But $msg"));
				}
			}
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to Approve. Some Data Already Approved or Could not able to Approve.!"));
		}
	}

	public function permission_approval($post_data,$form_id,$prime_financial_id,$first_approval,$second_approval,$leave_status){
		$created_on           = date("Y-m-d h:i:s");
		foreach ($post_data as $key => $value) {
			if($key === "employee_code"){
				$employee_code = $value;
			}
			if($key === "prime_request_id"){
				$prime_request_id = $value;
			}
			if($key === "leave_type"){
				$leave_type = $value;
			}
			if($key === "rejected_reason"){
				$rejected_reason = $value;
			}
			if($key === "no_of_days"){
				$no_of_days = $value;
			}
			if($key === "cancellation_request"){
				$cancellation_request = $value;
			}
			if($key === "first_approval_cancel_status"){
				$first_approval_cancel_status = $value;
			}
			if($key === "second_approval_cancel_status"){
				$second_approval_cancel_status = $value;
			}
			if($key === "permission_date"){
				$permission_date = $value;
			}
		}
		
		$permission_date_format           = date("Y-m-d",strtotime($permission_date));

		$employee_detail_qry  			  = 'select prime_employees_id,company_email_id from cw_employees where employee_code = "'.$employee_code.'" and trans_status = 1';
		$employee_detail_info  			  = $this->db->query("CALL sp_a_run ('SELECT','$employee_detail_qry')");
		$employee_detail_result  		  = $employee_detail_info->result();
		$employee_detail_info->next_result();
		$view_id 						  = $employee_detail_result[0]->prime_employees_id;
		$employee_to_mail 				  = $employee_detail_result[0]->company_email_id;
		if((int)$cancellation_request === 1){
			if((int)$leave_status === 4){
				$upd_permission_entry = 'UPDATE cw_permission_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_permission_entry.employee_code = "'.$employee_code.'" and cw_permission_entry.prime_request_id = "'.$prime_request_id.'" and cw_permission_entry.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_permission_entry')");

				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				
				$print_type_id = 31;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
				/*$information   = $this->db->query("CALL itsp_prcatt ('$permission_date_format','$permission_date_format','$employee_code')");
				$result        = $information->result();
				$information->next_result();*/
			}else{
				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				$print_type_id = 32;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}
		}else{
			if((int)$leave_status === 2){
				$upd_permission_entry   = 'UPDATE cw_permission_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_permission_entry.employee_code = "'.$employee_code.'" and cw_permission_entry.prime_request_id = "'.$prime_request_id.'" and cw_permission_entry.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_permission_entry')");
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$upd_res     = $this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");

				if($upd_res){
					//TIME ENTRY EXIST CHECK
					/*$time_entry_qry     = 'select COUNT(prime_time_entry_id) as time_entry_count from cw_time_entry where cw_time_entry.employee_code = "'.$employee_code.'" and cw_time_entry.att_date = "'.$permission_date_format.'" and cw_time_entry.trans_status = 1';
					$time_entry_info     = $this->db->query("CALL sp_a_run ('SELECT','$time_entry_qry')");
					$time_entry_result   = $time_entry_info->result();
					$time_entry_info->next_result();
					$time_entry_count    = (int)$time_entry_result[0]->time_entry_count;
					if($time_entry_count === 1){*/
						// $info   = $this->db->query("CALL itsp_perm_upd ('$permission_date_format','$permission_date_format','$employee_code')");
						//PERMISSION PROCEDURE CALL TO INSERT TIME ENTRY TABLE 
						/*$info   = $this->db->query("CALL itsp_prcatt ('$permission_date_format','$permission_date_format','$employee_code')");
						$result = $info->result();
						$info->next_result();*/
					/*}*/
					$print_type_id = 28;
					$this->send_mail($print_type_id,$form_id,$employee_to_mail);
				}
				/*if($result){
					
				}*/				
			}else
			if((int)$leave_status === 3){
				$upd_permission_entry = 'UPDATE cw_permission_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_permission_entry.employee_code = "'.$employee_code.'" and cw_permission_entry.prime_request_id = "'.$prime_request_id.'" and cw_permission_entry.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_permission_entry')");

				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",rejected_reason = "'.$rejected_reason.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");

				$print_type_id = 29;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else{
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
			}
		}
	}

	public function shift_change_approval($post_data,$form_id,$prime_financial_id,$first_approval,$second_approval,$leave_status){
		$created_on           = date("Y-m-d h:i:s");
		foreach ($post_data as $key => $value) {
			if($key === "employee_code"){
				$employee_code = $value;
			}
			if($key === "prime_request_id"){
				$prime_request_id = $value;
			}
			if($key === "shift_date"){
				$shift_date = $value;
			}
			if($key === "current_shift"){
				$current_shift = $value;
			}
			if($key === "change_shift"){
				$change_shift = $value;
			}
			if($key === "rejected_reason"){
				$rejected_reason = $value;
			}
			if($key === "cancellation_request"){
				$cancellation_request = $value;
			}
			if($key === "first_approval_cancel_status"){
				$first_approval_cancel_status = $value;
			}
			if($key === "second_approval_cancel_status"){
				$second_approval_cancel_status = $value;
			}
		}
		$employee_detail_qry  			  = 'select prime_employees_id,company_email_id from cw_employees where employee_code = "'.$employee_code.'" and trans_status = 1';
		$employee_detail_info  			  = $this->db->query("CALL sp_a_run ('SELECT','$employee_detail_qry')");
		$employee_detail_result  		  = $employee_detail_info->result();
		$employee_detail_info->next_result();
		$view_id 						  = $employee_detail_result[0]->prime_employees_id;
		$employee_to_mail 				  = $employee_detail_result[0]->company_email_id;

		if((int)$cancellation_request === 1){
			if((int)$leave_status === 4){
				$upd_shift_import = 'UPDATE cw_shift_import SET shift_name = "'.$current_shift.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_shift_import.employee_code = "'.$employee_code.'" and cw_shift_import.shift_date = "'.$shift_date.'" and cw_shift_import.trans_status = 1';
				$upd_shift_id     = $this->db->query("CALL sp_a_run ('UPDATE','$upd_shift_import')");
				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");

				//function for run a time entry because shift will update
				$this->time_entry_process($employee_code,$shift_date,$current_shift);
				//FOR EMAIL
				$print_type_id = 43;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else{
				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//FOR EMAIL
				$print_type_id = 44;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}
		}else{
			if((int)$leave_status === 2){
				$upd_shift_import = 'UPDATE cw_shift_import SET shift_name = "'.$change_shift.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_shift_import.employee_code = "'.$employee_code.'" and cw_shift_import.shift_date = "'.$shift_date.'" and cw_shift_import.trans_status = 1';
				$upd_shift_id     = $this->db->query("CALL sp_a_run ('UPDATE','$upd_shift_import')");
				$upd_request      = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");

				//function for run a time entry because shift will update
				$this->time_entry_process($employee_code,$shift_date,$change_shift);
				
				//FOR EMAIL
				$print_type_id = 40;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else
			if((int)$leave_status === 3){
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",rejected_reason = "'.$rejected_reason.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//FOR EMAIL
				$print_type_id = 41;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else{
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
			}
		}
	}
	
	public function manual_punch_approval($post_data,$form_id,$prime_financial_id,$first_approval,$second_approval,$leave_status){
		$created_on           = date("Y-m-d h:i:s");
		foreach ($post_data as $key => $value) {
			if($key === "component_value"){
				$component_value = $value;
			}
			if($key === "employee_code"){
				$employee_code = $value;
			}
			if($key === "shift_date"){
				$shift_date  = $value;
			}
			if($key === "shift_name"){
				$shift_name = $value;
			}
			if($key === "in_date"){
				$in_date = $value;
			}
			if($key === "out_date"){
				$out_date = $value;
			}
			if($key === "day_type"){
				$day_type = $value;
			}
			if($key === "in_time"){
				$in_time = $value;
			}
			if($key === "out_time"){
				$out_time = $value;
			}
			if($key === "total_time"){
				$total_time = $value;
			}
			if($key === "prime_request_id"){
				$prime_request_id = $value;
			}
			if($key === "rejected_reason"){
				$rejected_reason = $value;
			}
			if($key === "cancellation_request"){
				$cancellation_request = $value;
			}
			if($key === "first_approval_cancel_status"){
				$first_approval_cancel_status = $value;
			}
			if($key === "second_approval_cancel_status"){
				$second_approval_cancel_status = $value;
			}
		}
		$from_date_time     = $in_date." ".$in_time;
		$to_date_time       = $out_date." ".$out_time;
		$employee_detail_qry  			  = 'select prime_employees_id,company_email_id,device_code from cw_employees where employee_code = "'.$employee_code.'" and trans_status = 1';
		$employee_detail_info  			  = $this->db->query("CALL sp_a_run ('SELECT','$employee_detail_qry')");
		$employee_detail_result  		  = $employee_detail_info->result();
		$employee_detail_info->next_result();
		$view_id 						  = $employee_detail_result[0]->prime_employees_id;
		$employee_to_mail 				  = $employee_detail_result[0]->company_email_id;
		$device_code     				  = $employee_detail_result[0]->device_code;
		if((int)$cancellation_request === 1){
			if((int)$leave_status === 4){
				$upd_manual_punch_entry = 'UPDATE cw_manual_punch_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_manual_punch_entry.employee_code = "'.$employee_code.'" and cw_manual_punch_entry.prime_request_id = "'.$prime_request_id.'" and cw_manual_punch_entry.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_manual_punch_entry')");

				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				
				$del_time_log_qry   = 'DELETE FROM cw_time_log WHERE cw_time_log.user_id = "'.$device_code.'" and cw_time_log.device_id in ("MAN01","MAN02") and log_date in ("'.$from_date_time.'","'.$to_date_time.'")';
				$this->db->query("CALL sp_a_run ('RUN','$del_time_log_qry')");
				//FOR EMAIL
				$print_type_id = 49;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else{
				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//FOR EMAIL
				$print_type_id = 50;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}
		}else{
			if((int)$leave_status === 2){
				$upd_manual_punch_entry = 'UPDATE cw_manual_punch_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_manual_punch_entry.employee_code = "'.$employee_code.'" and cw_manual_punch_entry.prime_request_id = "'.$prime_request_id.'" and cw_manual_punch_entry.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_manual_punch_entry')");
				 
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//FOR EMAIL
				$print_type_id = 46;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else
			if((int)$leave_status === 3){
				//Manual punch upd query
				$upd_manual_punch_entry = 'UPDATE cw_manual_punch_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_manual_punch_entry.employee_code = "'.$employee_code.'" and cw_manual_punch_entry.prime_request_id = "'.$prime_request_id.'" and cw_manual_punch_entry.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_manual_punch_entry')");

				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",rejected_reason = "'.$rejected_reason.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//FOR EMAIL
				$print_type_id = 47;
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else{
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
			}
		}
	}
	public function approval_process($post_data,$form_id,$prime_financial_id,$first_approval,$second_approval,$leave_status){
		$created_on           = date("Y-m-d h:i:s");
		foreach ($post_data as $key => $value) {
			if($key === "employee_code"){
				$employee_code = $value;
			}
			if($key === "prime_request_id"){
				$prime_request_id = $value;
			}
			if($key === "request_type"){
				$request_type = $value;
			}
			if($key === "leave_type"){
				$leave_type = $value;
			}
			if($key === "rejected_reason"){
				$rejected_reason = $value;
			}
			if($key === "from_date"){
				$from_date = $value;
			}
			if($key === "from_date_type"){
				$from_date_type = $value;
			}
			if($key === "to_date"){
				$to_date = $value;
			}
			if($key === "to_date_type"){
				$to_date_type = $value;
			}
			if($key === "no_of_days"){
				$no_of_days = $value;
			}
			if($key === "cancellation_request"){
				$cancellation_request = $value;
			}
			if($key === "first_approval_cancel_status"){
				$first_approval_cancel_status = $value;
			}
			if($key === "second_approval_cancel_status"){
				$second_approval_cancel_status = $value;
			}
		}
		$leave_creation_qry    = 'SELECT leave_opening,intervening_holidays,leave_name from cw_leave_creation where cw_leave_creation.trans_status = 1 and prime_leave_creation_id = "'.$leave_type.'"';
		$leave_creation_info   = $this->db->query("CALL sp_a_run ('SELECT','$leave_creation_qry')");
		$leave_creation_result = $leave_creation_info->result();
		$leave_creation_info->next_result();	
		$leave_opening  	   = $leave_creation_result[0]->leave_opening;
		$leave_name  		   = $leave_creation_result[0]->leave_name;
		$leave_name 		   = strtolower($leave_name);
		$pending_column 	   = "pending_".$leave_name;
		$used_column 	   	   = "used_".$leave_name;

		$employee_detail_qry  			  = 'select prime_employees_id,company_email_id from cw_employees where employee_code = "'.$employee_code.'" and trans_status = 1';
		$employee_detail_info  			  = $this->db->query("CALL sp_a_run ('SELECT','$employee_detail_qry')");
		$employee_detail_result  		  = $employee_detail_info->result();
		$employee_detail_info->next_result();
		$view_id 						  = $employee_detail_result[0]->prime_employees_id;
		$employee_to_mail 				  = $employee_detail_result[0]->company_email_id;
		$print_type_id                    = '';
		if((int)$cancellation_request === 1){
			if((int)$leave_status === 4){
				if((int)$leave_opening === 1){
					$leave_opening_qry 			= 'select '.$used_column.' from cw_leave_opening where employee_code = "'.$employee_code.'" and financial_setting_id = "'.$prime_financial_id.'" and trans_status = 1';
					$leave_opening_info   		= $this->db->query("CALL sp_a_run ('SELECT','$leave_opening_qry')");
					$leave_opening_result    	= $leave_opening_info->result();
					$leave_opening_info->next_result();
					$used   					= $leave_opening_result[0]->$used_column;
					$leave_total 				= $used - $no_of_days;

					$upd_leave_opening = 'UPDATE cw_leave_opening SET '.$used_column.' = "'.$leave_total.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_leave_opening.employee_code = "'.$employee_code.'" and financial_setting_id = "'.$prime_financial_id.'" and cw_leave_opening.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_leave_opening')");
				}
				if((int)$request_type === 1){
					$upd_leave_entry = 'UPDATE cw_leave_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_leave_entry.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_leave_entry.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_leave_entry')");
					//FOR EMAIL
					$print_type_id = 25;
				}else
				if((int)$request_type === 3 || (int)$request_type === 8){
					$upd_on_duty_entry = 'UPDATE cw_on_duty_entry SET on_duty_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_on_duty_entry.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_on_duty_entry.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_on_duty_entry')");
					//FOR EMAIL
					$print_type_id = 37;
				}
				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//EMAIL SEND FUNCTION
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}
			else{
				$upd_request = 'UPDATE cw_request SET first_approval_cancel_status = "'.$first_approval_cancel_status.'",second_approval_cancel_status = "'.$second_approval_cancel_status.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//FOR EMAIL
				if((int)$request_type === 1){
					$print_type_id = 26;
				}else
				if((int)$request_type === 3 || (int)$request_type === 8){
					$print_type_id = 38;
				}
				//EMAIL SEND FUNCTION
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}
		}else{			
			if((int)$leave_status === 2){
				if((int)$leave_opening === 1){
					$leave_opening_qry 			= 'select '.$used_column.','.$pending_column.' from cw_leave_opening where employee_code = "'.$employee_code.'" and financial_setting_id = "'.$prime_financial_id.'" and trans_status = 1';
					$leave_opening_info   		= $this->db->query("CALL sp_a_run ('SELECT','$leave_opening_qry')");
					$leave_opening_result    	= $leave_opening_info->result();
					$leave_opening_info->next_result();
					$used   					= $leave_opening_result[0]->$used_column;
					$pending    				= $leave_opening_result[0]->$pending_column;
					$leave_total 				= $used + $no_of_days;
					$pending_total 				= $pending - $no_of_days;

					$upd_leave_opening = 'UPDATE cw_leave_opening SET '.$used_column.' = "'.$leave_total.'",'.$pending_column.' = "'.$pending_total.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_leave_opening.employee_code = "'.$employee_code.'" and financial_setting_id = "'.$prime_financial_id.'" and cw_leave_opening.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_leave_opening')");
				}
				if((int)$request_type === 1){
					$upd_leave_entry = 'UPDATE cw_leave_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_leave_entry.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_leave_entry.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_leave_entry')");
					//FOR EMAIL
					$print_type_id = 22;
				}else
				if((int)$request_type === 3 || (int)$request_type === 8){
					$upd_on_duty_entry = 'UPDATE cw_on_duty_entry SET on_duty_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_on_duty_entry.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_on_duty_entry.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_on_duty_entry')");
					//FOR EMAIL
					$print_type_id = 34;
				}
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				
				//EMAIL SEND FUNCTION
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else
			if((int)$leave_status === 3){
				if((int)$leave_opening === 1){
					$leave_opening_qry 			= 'select '.$pending_column.' from cw_leave_opening where employee_code = "'.$employee_code.'" and financial_setting_id = "'.$prime_financial_id.'" and trans_status = 1';
					$leave_opening_info   		= $this->db->query("CALL sp_a_run ('SELECT','$leave_opening_qry')");
					$leave_opening_result    	= $leave_opening_info->result();
					$leave_opening_info->next_result();
					$pending    				= $leave_opening_result[0]->$pending_column;
					$pending_total 				= $pending - $no_of_days;

					$upd_leave_opening = 'UPDATE cw_leave_opening SET '.$pending_column.' = "'.$pending_total.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_leave_opening.employee_code = "'.$employee_code.'" and financial_setting_id = "'.$prime_financial_id.'" and cw_leave_opening.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_leave_opening')");
				}

				if((int)$request_type === 1){
					$upd_leave_entry = 'UPDATE cw_leave_entry SET leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_leave_entry.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_leave_entry.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_leave_entry')");
					//FOR EMAIL
					$print_type_id = 23;
				}else
				if((int)$request_type === 3 || (int)$request_type === 8){
					$upd_on_duty_entry = 'UPDATE cw_on_duty_entry SET on_duty_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_on_duty_entry.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_on_duty_entry.trans_status = 1';
					$this->db->query("CALL sp_a_run ('UPDATE','$upd_on_duty_entry')");
					//FOR EMAIL
					$print_type_id = 35;
				}
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",rejected_reason = "'.$rejected_reason.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
				//EMAIL SEND FUNCTION
				$this->send_mail($print_type_id,$form_id,$employee_to_mail);
			}else{
				
				$upd_request = 'UPDATE cw_request SET first_approval_leave_status = "'.$first_approval.'",second_approval_leave_status = "'.$second_approval.'",leave_status = "'.$leave_status.'",trans_updated_by = "'.$this->logged_id.'", trans_updated_date = "'.$created_on.'" WHERE cw_request.employee_code = "'.$employee_code.'" and prime_request_id = "'.$prime_request_id.'" and cw_request.trans_status = 1';
				$this->db->query("CALL sp_a_run ('UPDATE','$upd_request')");
			}
		}
	}
	public function view_approval_status($value){
		$logged_emp_code      	 = $this->session->userdata('logged_emp_code');
		$data['logged_emp_code'] = $logged_emp_code;
			//CURRENT FINANCIAL YEAR ID
		$financial_info          = $this->get_leave_financial_details();
		$prime_financial_id      = $financial_info[0]->prime_leave_financial_year_id;
		
		$emp_code_qry            = 'select employee_code from cw_approval where prime_approval_id = "'.$value.'" and trans_status = 1';
		$emp_code_info           = $this->db->query("CALL sp_a_run ('SELECT','$emp_code_qry')");
		$emp_code_result  	     = $emp_code_info->result();
		$emp_code_info->next_result();
		$emp_code                = $emp_code_result[0]->employee_code;

		$leave_qry 				= 'select emp_name from cw_employees where cw_employees.employee_code = "'.$emp_code.'" and cw_employees.trans_status = 1';
		$leave_info       		= $this->db->query("CALL sp_a_run ('SELECT','$leave_qry')");
		$leave_result  			= $leave_info->result();
		$leave_info->next_result();
		$emp_name 				= $leave_result[0]->emp_name;
		$data['emp_name']       = $emp_name;
		$leave_approval_qry 	= 'select employee_code,cw_request_type.request_type,applied_on,from_date,to_date,no_of_days,in_time,out_time,current_shift,change_shift,cw_leave_status.leave_status from cw_approval inner join cw_leave_status on cw_leave_status.prime_leave_status_id = cw_approval.leave_status inner join cw_request_type on cw_request_type.prime_request_type_id = cw_approval.request_type where cw_approval.employee_code = "'.$emp_code.'" and cw_approval.trans_status = 1 and cw_approval.financial_setting_id = "'.$prime_financial_id.'" ORDER BY applied_on DESC';
		$leave_approval_info    = $this->db->query("CALL sp_a_run ('SELECT','$leave_approval_qry')");
		$leave_approval_result  = $leave_approval_info->result();
		$leave_approval_info->next_result();

		$data['leave_approval_result'] = $leave_approval_result;
		$this->load->view("$this->control_name/view_approval_status",$data);
		
	}

	//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' => 'Invalid Request..','table_data' => ""));
			exit(0);
		}
		$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;
				foreach($prime_module_id as $check_modules){
					$table_name          = "cw_".$check_modules;
					$select_table        = "$table_name.$label_name[$i]";
					$select_where        = " and $table_name.$label_name[$i] in ($delete_ids)";
					$check_module_query  = 'SELECT '.$select_table.' from '.$table_name.' WHERE '.$table_name.'.trans_status = 1 '.$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){
						$table_names  .= "$check_modules,";
						$can_process   = False;
						$delete_status = False;
					}
					$i++;
				}				
			}
			$table_names = rtrim($table_names,",");
			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{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to delete, This value is already used in $table_names 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(){
		//Encryption
		$encString     = file_get_contents('php://input');
		$_POST         = $this->cryptoDecrypt($encString);
		if(!$_POST){
			echo json_encode(array('success' => false,'message' => 'Invalid Request..','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"));
		}
	}
	
	//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);
	}
	public function send_mail($print_type_id,$view_id,$employee_to_mail){
		$email_data     	  = $this->load_email_data($print_type_id,$view_id);
		$body_content   	  = "<!DOCTYPE html><html> <body>".$email_data['print_design']."</body></html>";
		$email_design_query   = 'select prime_print_info_id,email_design,print_info_for,print_info_module_id,print_type,email_subject,email_bcc,to_email from cw_email_design inner join cw_print_info on cw_print_info.prime_print_info_id = cw_email_design.email_design_for where find_in_set("'.$this->logged_role.'",print_info_for) and print_info_module_id = "'.$this->control_name.'" and cw_print_info.trans_status = 1 and print_type = "'.$print_type_id.'"';

		$email_design_info    = $this->db->query("CALL sp_a_run ('SELECT','$email_design_query')");
		$email_design_result  = $email_design_info->result();
		$email_design_info->next_result();
		$email_design_content = $email_design_result[0]->email_design;
		$mail_subject         = $email_design_result[0]->email_subject;
		$email_bcc 			  = $email_design_result[0]->email_bcc;
		$to_email 			  = $email_design_result[0]->to_email;
		$email_returns = $this->send_email_notification($to_email,$employee_to_mail,'',$email_bcc,$mail_subject,$body_content,'');
	}

	//DR CODE
	function getDatesFromRange($start, $end, $format = 'Y-m-d') {
      
	    // Declare an empty array
	    $array = array();
	      
	    // Variable that store the date interval
	    // of period 1 day
	    $interval = new DateInterval('P1D');
	  
	    $realEnd = new DateTime($end);
	    $realEnd->add($interval);
	  
	    $period = new DatePeriod(new DateTime($start), $interval, $realEnd);
	  
	    // Use loop to store date into array
	    foreach($period as $date) {                 
	        $array[] = $date->format($format); 
	    }
	  
	    // Return the array elements
	    return $array;
	}
	public function weekOfMonth($date) {
	    //Get the first day of the month.
	    $firstOfMonth = strtotime(date("Y-m-01", $date));
	    //Apply above formula.
	    return $this->weekOfYear($date) - $this->weekOfYear($firstOfMonth) + 1;
	}
	public function weekOfYear($date) {
	    $weekOfYear = intval(date("W", $date));
	    if (date('n', $date) == "1" && $weekOfYear > 51) {
	        // It's the last week of the previos year.
	        $weekOfYear = 0;    
	    }
	    return $weekOfYear;
	}
}
?>