MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/hrms_patroniss_com/application/controllers/Offer_letter_bk03oct2020.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Offer_letter  extends Action_controller{	
	public function __construct(){
		parent::__construct('offer_letter');
		$this->collect_base_info();
	}
	
	// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
	public function index(){
		$data['quick_link']    = $this->quick_link;
		$data['table_head']    = $this->table_head;
		$data['master_pick']   = $this->master_pick;
		$data['fliter_list']   = $this->fliter_list;
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//LOAD TABEL WITH FILTERS
	public function search(){
		$draw         = $this->input->post('draw');
		$start        = $this->input->post('start');
		$per_page     = $this->input->post('length');
		$order        = $this->input->post('order');
		$order_col    = $this->input->post('columns');
		$search       = $this->input->post('search');
		$column       = $order[0]['column'];
		$order_sor    = $order[0]['dir'];
		$order_col    = $order_col[$column]['data'];
		$search       = trim($search['value']);
		$search_query = str_replace("@SELECT@",$this->select_query,$this->base_query);
		
		//ADDED BASIC,FILTER,COMMON QUERY HERE 
		$role_condition   = "";
		if($this->role_condition){
			$role_condition = $this->role_condition;
		}
		
		$fliter_query = "";
		foreach($this->fliter_list as $fliter){
			$label_id         = $fliter['label_id'];
			$label_name       = $fliter['label_name'];
			$field_isdefault  = (int)$fliter['field_isdefault'];
			$array_list       = $fliter['array_list'];
			$field_type       = (int)$fliter['field_type'];			
			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 === 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->form_info 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->master_pick[$label_id], function ($item) use ($search) {
								if (stripos($item, $search) !== false) {
									return true;
								}
								return false;
							});
							if($result){
								//$pick_key   = implode(",",array_keys($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);		
		$search_total       = $this->db->query($count_all_query);
		$search_total_info  = $search_total->result();
		$total_count        = $search_total_info[0]->allcount;
		
		$count_query        = str_replace("@SELECT@","count(*) as allcount",$this->base_query);
		$count_query       .= " where $this->prime_table.trans_status = 1 $role_condition $fliter_query $common_search";
		$search_count       = $this->db->query($count_query);
		$search_info        = $search_count->result();
		$filtered_count     = $search_info[0]->allcount;
		
		$search_query      .= " where $this->prime_table.trans_status = 1 $role_condition $fliter_query $common_search";
		//$search_query      .= " ORDER BY  $order_col $order_sor";
		$search_query      .= " ORDER BY  prime_offer_letter_id DESC";
		if((int)$per_page !== -1){
			$search_query  .= " LIMIT  $start,$per_page";
		}		
		$search_data        = $this->db->query($search_query);
		$search_result      = $search_data->result();
		//echo "search_query :: \n$search_query\n";		
		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){
		//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	
		
		//VIEW DATA
		$base_query  = str_replace("@SELECT@",$this->view_select,$this->base_query);
		$view_query  = $base_query ." where $this->prime_table.$this->prime_id = $form_view_id and $this->prime_table.trans_status = 1";
		$view_data   = $this->db->query("CALL sp_a_run ('SELECT','$view_query')");
		$view_result = $view_data->result();
		$view_data->next_result();
		$data['form_view']   = $view_result[0];	
		$department          = $view_result[0]->emp_dept;
		//AUTO COMPLTE,PICK LIST AND CONDITION
		foreach($this->form_info as $from){
			$prime_form_id      = (int)$from->prime_form_id;
			$field_type         = (int)$from->field_type;
			$pick_table         = $from->pick_table;
			$auto_prime_id      = $from->auto_prime_id;
			$auto_dispaly_value = $from->auto_dispaly_value;
			$label_id           = $from->label_name;
			if($field_type === 9){
				if($view_result[0]){
					$get_value = $view_result[0]->$label_id;
					if($get_value){
						$pick_query = 'select '.$auto_dispaly_value.' from '.$pick_table.' where '.$auto_prime_id.' = "'.$get_value.'" and trans_status = 1';
						$pick_data   = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
						$pick_result = $pick_data->result();
						$pick_data->next_result();
						$this->all_pick[$prime_form_id] = $pick_result[0]->$auto_dispaly_value;
					}					
				}
			}
			if($label_id === "employee_designation"){//edit show department based designation
				if($view_result[0]){
					if($department){
						$pick_query = 'select prime_position_id,position_name from '.$pick_table.' where department = "'.$department.'" and 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, 'prime_position_id');
							$pick_val   = array_column($pick_result, 'position_name');
							$final_pick = array_combine($pick_key, $pick_val);
							$this->all_pick[$prime_form_id] = $final_pick;
						}
					}					
				}
			}
		}
		
		$data['all_pick']       = $this->all_pick;
		$data['condition_list'] = $this->condition_list;
		
		$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_data   = $this->db->query("CALL sp_a_run ('SELECT','$view_qry')");
		$view_result = $view_data->result();
		$view_data->next_result();
		$row_view_list = array();
		foreach($view_result 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;		

		$role_based_query  = 'SELECT * FROM cw_role_base_condition WHERE  role_module_id = "'.$this->control_name.'" and FIND_IN_SET("'.$this->logged_user_role.'",role_condition_for) and trans_status = 1';
		//echo $role_based_query; die;
		$role_based_info   = $this->db->query("CALL sp_a_run ('SELECT','$role_based_query')");
		$role_based_result = $role_based_info->result();
		$role_based_info->next_result();

		$role_based_condition = array();
		foreach ($role_based_result as $key => $condition) {
			$role_based_condition[$condition->user_condition_type] = $condition->input_columns;
		}
		$data['role_based_condition'] = $role_based_condition;
		$this->load->view("$this->control_name/form",$data);
	}
	
	//SAVE MODEL DATA TO DATA BASE
	public function save(){
		$unq_chk            = array();
		$prime_qry_key      = "";
		$prime_qry_value    = "";
		$prime_upd_query    = "";
		$form_id            = (int)$this->input->post($this->prime_id);
		$category           = $this->input->post('category');
		$employee_email_id  = $this->input->post('employee_email_id');
		$hr_status          = $this->input->post('hr_status');
		$emp_code           = $this->input->post('emp_code');
		$offer_no           = $this->input->post('offer_reference_number');
		$form_post_data  = array();	
		$files_copy      = 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;		
			if((int)$field_type === 7){
				$multi_name = $label_id."[]";
				$value = trim(implode(",",array_filter($this->input->post($multi_name))));
			}else{
				$value = trim($this->input->post($label_id));
			}
					
			if((int)$field_type === 4){
				$value = date('Y-m-d',strtotime($value));
			}else
			if((int)$field_type === 13){
				$value = date('Y-m-d H:i:s',strtotime($value));
			}	
			if(($input_view_type === 1) || ($input_view_type === 2)){
				if((int)$field_isdefault === 1){
					$prime_qry_key     .= $label_id.",";
					if($label_id === 'offer_reference_number' && (int)$form_id === 0){
						$offer_reference_number = $this->generate_number();
						$prime_qry_value   .= '"'.$offer_reference_number.'",';
					}else{
						$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,);
					}
				}
			}
			if($label_id === 'rms_code'){
				$rms_code  = trim($this->input->post($label_id));
			}
			if($label_id === 'employee_name'){
				$employee_name  = trim($this->input->post($label_id));
			}
			if($label_id === 'actual_joining_date'){
				$act_join_date  =  date('Y-m-d',strtotime($this->input->post($label_id)));
			}
			if($label_id === 'emp_code'){
				$emp_code  = trim($this->input->post($label_id));
			}
			if($label_id === 'employee_mobile_number'){
				$mobile_number = trim($this->input->post($label_id));
			}
			if($label_id === 'enroll_status'){
				$enroll_status  = trim($this->input->post($label_id));
			}
			if($label_id === 'enroll_reason'){
				$multi_name    = $label_id."[]";
				$enroll_reason = array_filter($this->input->post($multi_name));
				$enroll_reason = trim(implode(",",$enroll_reason));
			}
		}
		$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($enroll_reason){
			$reason_qry = 'select cw_zct_44_id,cw_zct_44_value from cw_zct_44 where cw_zct_44_id in ('.$enroll_reason.')';
			$reason_data   = $this->db->query("CALL sp_a_run ('SELECT','$reason_qry')");
			$reason_result = $reason_data->result();
			$reason_data->next_result();
			$reason_list = array();
			foreach($reason_result as $result){
				$reason_id  = $result->cw_zct_44_id;
				$reason     = $result->cw_zct_44_value;
				$reason_list[$reason_id] = $reason;
			}
		}
		$offer_data = array();
		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){
				$table_info = '';				
				$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;
				$mail ="";
				if($employee_email_id !== ''){
					$mail_rslt = $this->send_email_directly($category,$employee_email_id,$insert_id,$offer_reference_number,$employee_name,$enroll_status,$reason_list);
					$msg = " and". $mail_rslt['msg'];
				}
				echo json_encode(array('success' => TRUE, 'message' => "Successfully added $mail", 'insert_id' => $insert_id,"table_data"=>$table_info));
			}else{				
				if((int)$enroll_status === 5 && $rms_code !== ""){
					$post_data = array('source'=>'offer','rms_code'=>$rms_code,'candidate_status'=>$enroll_status,'act_join_date'=>$act_join_date);
					$this->rms_save($post_data);
				}
				$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')");
				$mail ="";
				if($employee_email_id !== ''){
					$mail_rslt = $this->send_email_directly($category,$employee_email_id,$form_id,$offer_no,$employee_name,$enroll_status,$reason_list);
					$msg = " and". $mail_rslt['msg'];
				}
				echo json_encode(array('success' => TRUE, 'message' => "Successfully updated $msg",'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');
		$input_val      = $this->input->post('input_val');
		if($input_val){
			unlink($input_val);
		}
		$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['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 rowset_remove_file(){
		$table_name  = $this->input->post('table_name_set');
		$row_id     = (int)$this->input->post('row_id');
		$input_name     = $this->input->post('input_name');
		$view_id     = (int)$this->input->post('view_id');
		$primes_id     = (int)$this->input->post('primes_id');
		$remove_cw = str_replace("cw_","",$table_name);
		$primary_id = "prime_".$remove_cw."_id";
		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 '.$primary_id.' = "'. $row_id .'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			$row_set_edit = $this->get_row_set_data($view_id,$primes_id);
			$div_id = $row_set_edit['div_id'];
			$table_id = $row_set_edit['table_id'];
			$row_set_view = $row_set_edit['row_set_view'];
			echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'div_id' => $div_id, 'table_id' => $table_id,'row_set_view'=>$row_set_view));
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to process your request"));
		}
	}
	
	//Manually enter the employee code exit checking
	public function mobile_no_exit(){	
		$mobile_number        = $this->input->post('mobile_number');
		$mobileno_exit_qry    = 'select * from cw_employees where mobile_number = "'.$mobile_number.'"';
		$mobileno_exit_info   = $this->db->query("CALL sp_a_run ('RUN','$mobileno_exit_qry')");
		$mobileno_exit_result = $mobileno_exit_info->result();
		$mobileno_exit_info->next_result();
		$num_rows      = $mobileno_exit_info->num_rows();
		if((int)$num_rows > 0){
			echo json_encode(array('success' => FALSE, 'message' => "Mobile Number is already Exit! please enter another number"));
		}else{
		echo json_encode(array('success' => TRUE, 'message' => "Continue to fill further information"));
		}
	}
	
	public function send_email(){
		$prime_id        = $this->input->post('view_id');
		$url_path_enrol  = base_url()."enrolment";
		$cat_query  = 'SELECT category,employee_email_id,offer_reference_number,employee_name FROM cw_offer_letter WHERE prime_offer_letter_id = "'.$prime_id.'" and trans_status = 1';
		$cat_info   = $this->db->query("CALL sp_a_run ('SELECT','$cat_query')");
		$cat_result = $cat_info->result();
		$cat_info->next_result();
		$category     = $cat_result[0]->category;
		$to_email     = $cat_result[0]->employee_email_id;
		$offer_no     = $cat_result[0]->offer_reference_number;
		$employee_name     = $cat_result[0]->employee_name;
		$controller   = $this->control_name;
		if($to_email){
			$status        = 0;
			$design_query  = 'SELECT prime_print_info_id,print_design,print_info_for,print_info_module_id,print_type FROM cw_print_design inner join cw_print_info on cw_print_info.prime_print_info_id = cw_print_design.print_design_for WHERE FIND_IN_SET("'.$category.'",print_info_for) and print_info_module_id = "'.$controller.'" and cw_print_design.trans_status = 1';
			$design_info   = $this->db->query("CALL sp_a_run ('SELECT','$design_query')");
			$design_result = $design_info->result();
			$design_info->next_result();
			$print_doc_id  = $design_result[0]->prime_print_info_id;
			$print_design  = $design_result[0]->print_design;
			$print_type    = $design_result[0]->print_type;
			$data          = $this->load_design_data($print_doc_id,$print_type,$print_design,$prime_id);
			$table_data = "<!DOCTYPE html><html> <body>".$data['print_design']."</body></html>";
			$pdf_file   = $this->generate_pdf($table_data,$category,$offer_no);
			$tr_line    = '';
			//Get company name
                        $company_query  = 'SELECT company_name FROM cw_company_information WHERE trans_status = 1';
                        $company_info   = $this->db->query("CALL sp_a_run ('SELECT','$company_query')");
                        $company_result = $company_info->result();
                        $company_info->next_result();
                        if($company_result){
                                $company_name   = $company_result[0]->company_name;
                        }
			//Get Email Configuration data
			$config_query  = 'SELECT smtp_server,sender_name,bcc,port_no,sender_email,mail_username,mail_password FROM cw_mail_configurations WHERE trans_status = 1';
			$config_info   = $this->db->query("CALL sp_a_run ('SELECT','$config_query')");
			$config_result = $config_info->result();
			$config_info->next_result();
			$smtp_server   = $config_result[0]->smtp_server;
			$sender_name   = $config_result[0]->sender_name;
			//$bcc           = explode(",",$config_result[0]->bcc);
			$port_no       = $config_result[0]->port_no;
			$sender_email  = $config_result[0]->sender_email;
			$username      = $config_result[0]->mail_username;
			$password      = $config_result[0]->mail_password;
			require('./phpmailer/class.phpmailer.php');
			//try{
				$mail = new PHPMailer();
				$mail->IsSMTP();
				//$mail->SMTPDebug = 3; 
				$mail->IsSMTP();
				$mail->Host = $smtp_server; // Your SMTP PArameter
				$mail->Port = $port_no; // Your Outgoing Port
				$mail->SMTPAuth = true; // This Must Be True
				$mail->Username = $username; // Your Email Address
				$mail->Password = $password; // Your Password
				$mail->SMTPSecure = 'tls'; // Check Your Server's Connections for TLS or SSL
				$mail->From     = $sender_email;
				$mail->FromName = $sender_name;
				if($to_email){
					$mail->AddAddress($to_email);
				}
				$mail->IsHTML(true);
				$mail->Subject = "*** Welcome To Our World ***";
				$mail->Body    = "<!DOCTYPE html><html><body>
					<p>Dear $employee_name,</p>
					<p>We are pleased to extend the following offer of employment to you on behalf of $company_name.</p>
					<p>Please find the detailed offer letter in the attachments.</p>
					<p>Please ensure that you take the time to review our offer. It includes details about your compensation, benefits and terms & conditions of your anticipated employment with us.</p>
					<p>If you choose to accept please click the below link to get stared with the rest of the onboarding process and submit at your earliest convenience.</p>
					<p>Link For filling employee details $url_path_enrol</p>
					<p>We hope you will enjoy your role and make a significant contribution to overall success of $company_name.</p>
					<p>We are excited about the possibility of you joining and if you  have  any questions, please contact us.</p>
					</body></html>";
				//Attachment
				$folder = $pdf_file;
				if(file_exists($folder)){
					$mail->addAttachment($folder); // Add attachments
					$mail= $mail->Send();
					if($mail){
						$status = 1;
					}			
				}else{
					$status = 0;
				}
			/*}catch(phpmailerException $e){
				$status = 0;
				$msg = "Mail Not Sent";
				$tr_line .= "<tr><td>$category</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
			}catch(Exception $e){
				$status = 0;
				$msg = "Mail Not Sent";
				$tr_line .= "<tr><td>$category</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
			}*/
			if($status){
				echo json_encode(array('success'=>true,'message'=>"Mail Successfully Sent"));
			}else{
				echo json_encode(array('success'=>false,'message'=>"Mail Not Sent"));
			}
		}else{
			echo json_encode(array('success'=>false,'message'=>"Please Add Employee email id and Try Again!!"));
		}
	}
	
	public function load_design_data($print_doc_id,$print_type,$print_design,$view_id){
		if((int)$print_type === 4){
				$style = '';
			}else{
				$style  = "<style>
				table{
					border: 1px !important;
					border-collapse: collapse !important;
					empty-cells: show !important;
					max-width: 100% !important;
					font-size: 13px !important;
				}
				tbody {
					border: 1px !important;
					border-collapse: collapse !important; 
					empty-cells: show !important;
					max-width: 100% !important;
					font-size: 13px !important;
				}
				td, th {
					border: 1px solid #000 !important;
					font-size: 13px !important;
				}
				td.fr-thick,th.fr-thick {
					border-width: 2px !important;
				}
				table.fr-dashed-borders td, table.fr-dashed-borders th {
					border-style: dashed !important;
				}
				</style>";
			}
		$print_design  = $style."".$print_design;
		$print_design  = str_replace('~','"',$print_design);
		$block_qry    = 'select * from cw_print_block where print_block_for = "'.$print_doc_id.'" and trans_status = 1';
		$block_data   = $this->db->query("CALL sp_a_run ('SELECT','$block_qry')");
		$block_result = $block_data->result();
		$block_data->next_result();
		foreach($block_result as $block){
			$prime_print_block_id  = $block->prime_print_block_id;
			$print_block_name      = $block->print_block_name;
			$print_block_type      = (int)$block->print_block_type;
			$print_block_table     = $block->print_block_table;
			$print_block_column    = $block->print_block_column;
			$suppressed_data       = $block->suppressed_data;
			$cumulative_data       = $block->cumulative_data;
			
			$table_qry    = 'select * from cw_print_table where print_table_for_id = "'.$prime_print_block_id.'" and trans_status = 1';
			$table_data   = $this->db->query("CALL sp_a_run ('SELECT','$table_qry')");
			$table_result = $table_data->result();
			$table_data->next_result();
			$line_table_query = "";
			$cutome_table_check = array('transactions'=>'cw_transactions');
			foreach($table_result as $table){
				$line_prime_table      = $table->line_prime_table;
				$line_prime_col        = $table->line_prime_col;
				$line_join_type        = $table->line_join_type;
				$line_join_table       = $table->line_join_table;
				$line_join_col         = $table->line_join_col;
				$line_sort             = $table->line_sort;
				$module_name           = str_replace("cw_","",$line_prime_table);
				$prime_id              = "prime_".$module_name."_id";
				$cf_id                 = "prime_".$module_name."_cf_id";
				$cf_table_name         = $this->db->dbprefix($module_name."_cf");
				$join_module_name      = str_replace("cw_","",$line_join_table);
				$join_prime_id         = "prime_".$join_module_name."_id";
				$join_cf_id            = "prime_".$join_module_name."_cf_id";
				$join_cf_table_name    = $this->db->dbprefix($join_module_name."_cf");	
				if((int)$line_sort === 1){
					if($cutome_table_check[$module_name]){
						$line_prime_table = " $line_prime_table ";
					}else{
						$line_prime_table = " $line_prime_table inner join $cf_table_name on $line_prime_table.$prime_id = $cf_table_name.$prime_id ";
					}
					if($cutome_table_check[$join_module_name]){
						$line_join_table = " $line_join_table on $line_join_col = $line_prime_col";
					}else{
						$line_join_table = " $line_join_table on $line_join_col = $line_prime_col inner join  $join_cf_table_name on $line_join_table.$join_prime_id = $join_cf_table_name.$join_prime_id ";
					}
					$line_table_query .= " $line_prime_table  $line_join_type join $line_join_table"; 
				}else{
					if($cutome_table_check[$join_module_name]){
						$line_table_query .= " $line_join_type join $line_join_table on $line_join_col = $line_prime_col "; 
					}else{
						$line_table_query .= " $line_join_type join $line_join_table on $line_join_col = $line_prime_col inner join  $join_cf_table_name on $line_join_table.$join_prime_id = $join_cf_table_name.$join_prime_id "; 
					}
				}
			}
			if(!$line_table_query){
				$module_name      = str_replace("cw_","",$print_block_table);
				$prime_id         = "prime_".$module_name."_id";
				$cf_id            = "prime_".$module_name."_cf_id";
				$cf_table_name    = $this->db->dbprefix($module_name."_cf");
				$line_table_query = " $print_block_table ";
			}
			if(!$print_block_column){
				$print_block_column = "*";
			}else{
				$select_query = "";
				$select_ytd_query = "";
				$pick_query   = "";
				$map_column = explode(",",$print_block_column);
				foreach($map_column as $table_column){
					$map_column   = explode(".",$table_column);
					$table_name   = $map_column[0];
					$column 	  = $map_column[1];
					$control_name = str_replace('cw_',"",$table_name);
					if($control_name === "transactions"){
						$control_name = "employees";
					}
					$form_qry    = 'select prime_form_id,view_name,label_name,field_type,pick_list_type,pick_list,pick_table,auto_prime_id,auto_dispaly_value from cw_form_setting where prime_module_id = "'.$control_name.'" and  label_name = "'.$column.'"  and trans_status = "1"';
					$form_data   = $this->db->query("CALL sp_a_run ('SELECT','$form_qry')");
					$form_result = $form_data->result();
					$form_data->next_result();
					foreach($form_result as $form){
						$prime_form_id  = (int)$form->prime_form_id;
						$view_name      = $form->view_name;
						$label_name     = $form->label_name;
						$field_type     = (int)$form->field_type;
						$pick_list_type = (int)$form->pick_list_type;
						$pick_list      = $form->pick_list;
						$pick_table     = $form->pick_table;
						$auto_prime_id      = $form->auto_prime_id;
						$auto_dispaly_value = $form->auto_dispaly_value;
						if((int)$field_type === 4){
							$select_query .= 'DATE_FORMAT('.$table_name.'.'.$label_name.', "%d-%m-%Y") as '.$label_name.' , ';
						}else
						if(($field_type === 5) || ($field_type === 7)){
							if($pick_list_type === 1){
								$pick_list_val   = explode(",",$pick_list);
								$pick_list_val_1 = $pick_list_val[0];
								$pick_list_val_2 = $pick_list_val[1];
								
								$pick_query_as = $pick_table."_".$prime_form_id;
								$select_query .= "$pick_query_as.$pick_list_val_2 as $label_name , ";
								$pick_query .= " left join $pick_table as $pick_query_as on $pick_query_as.$pick_list_val_1 = $table_name.$label_name ";
							}else
							if($pick_list_type === 2){ 
								$pick_list_val_1 = $pick_table."_id";
								$pick_list_val_2 = $pick_table."_value";
								$pick_list_val_3 = $pick_table."_status";
								
								$pick_query_as = $pick_table."_".$prime_form_id;
								$select_query .= "$pick_query_as.$pick_list_val_2 as $label_name , ";
								$pick_query   .= " left join $pick_table as $pick_query_as on $pick_query_as.$pick_list_val_1 = $table_name.$label_name ";
							}
						}else
						if($field_type === 9){
							$pick_query_as = $pick_table."_".$prime_form_id;
							$select_query .= "$pick_query_as.$auto_dispaly_value as $label_name,";
							$pick_query .= " left join $pick_table as $pick_query_as on $pick_query_as.$auto_prime_id = $table_name.$label_name ";
						}else
						if(($field_type === 2) || ($field_type === 3)){
							$label_ytd  =	$label_name."_ytd";
							$select_ytd_query .= "sum($table_name.$label_name) as $label_ytd, ";
							$select_query .= "$table_name.$label_name , ";
						}else{
							$select_query .= "$table_name.$label_name , ";
						}
					}					
				}
			}
			$where_trans = "";
			$where_trans_info = explode(",",$print_block_table);
			foreach($where_trans_info as $trans_info){
				if($trans_info === "cw_transactions"){
					$select_query .= "cw_transactions.transactions_month , ";
				}				
				$where_trans .= "$trans_info.trans_status = 1 and ";
			}
			$where_trans = rtrim($where_trans,'and ');
			$where_qry    = 'select * from cw_print_table_where where where_for_id = "'.$prime_print_block_id.'" and trans_status = 1';
			$where_data   = $this->db->query("CALL sp_a_run ('SELECT','$where_qry')");
			$where_result = $where_data->result();
			$where_data->next_result();
			$where_condition = "";
			if($where_result){
				$where_condition = str_replace('^','"',$where_result[0]->where_condition);
				$where_condition = str_replace('@logged_id@',$view_id,$where_condition);				
				$session_date_list  = array("logged_DMY"=>"d-m-Y","logged_YMD"=>"Y-m-d","logged_MY"=>"m-Y","logged_YM"=>"Y-m","logged_Y"=>"Y"); 
				$session_query      = 'select session_value from cw_session_value where session_for = 1 and trans_status = "1"';
				$session_data       = $this->db->query("CALL sp_a_run ('SELECT','$session_query')");
				$session_result     = $session_data->result();
				$session_data->next_result();
				foreach($session_result as $rslt){
					$session_value 	   = $rslt->session_value;
					if($session_value !== "access_data"){
						$exist_val = "@".$session_value."@";
						if($session_date_list[$session_value]){
							$date_formate      = $session_date_list[$session_value];
							$saved_session_val = date($date_formate);
						}else{
							$saved_session_val = $this->session->userdata($session_value);
						}
						$where_condition  = str_replace($exist_val,$saved_session_val,$where_condition);
					}
					}
			}
			$select_query = rtrim($select_query,',');
			$select_query = rtrim($select_query,' , ');
			if((int)$cumulative_data === 1){
				$start_fin_date = $this->financial_info[0]->start_date;
				$start_fin_date = date('m-Y',strtotime($start_fin_date));
				$end_fin_date   = $this->financial_info[0]->end_date;
				$end_fin_date   = date('m-Y',strtotime($end_fin_date));
				$select_ytd_query = rtrim($select_ytd_query,',');
				$select_ytd_query = rtrim($select_ytd_query,' , ');
				$where_ytd_condition  = ' and date_format(str_to_date(transactions_month, "%m-%Y") , "%Y-%m")  >= date_format(str_to_date("'.$start_fin_date.'", "%m-%Y"), "%Y-%m") and date_format(str_to_date(transactions_month, "%m-%Y") , "%Y-%m")  <= date_format(str_to_date("'.$end_fin_date.'", "%m-%Y"), "%Y-%m")';
				$final_ytd_qry = "select $select_ytd_query from $line_table_query $pick_query  where $where_trans $where_condition  $where_ytd_condition";
				$final_ytd_data   = $this->db->query("CALL sp_a_run ('SELECT','$final_ytd_qry')");
				$final_ytd_result = $final_ytd_data->result();
				$final_ytd_data->next_result();
				foreach($final_ytd_result as $ytd_rslt){
					$map_column = explode(",",$print_block_column);
					foreach($map_column as $table_column){
						$map_column   = explode(".",$table_column);
						$ytd_column 	  = $map_column[1]."_ytd";
						$ytd_value        = $ytd_rslt->$ytd_column;
						$replace_ytd_val  = "@".$ytd_column."@";
						$print_design  = str_replace($replace_ytd_val,$ytd_value,$print_design);
					}
				}
			}
			$final_qry = "select category,$select_query from ".$line_table_query." $pick_query where $where_trans $where_condition";
			$final_data   = $this->db->query("CALL sp_a_run ('SELECT','$final_qry')");
			$final_result = $final_data->result();
			$final_data->next_result();
			$tr_line = "";
			$th_line = "";
			$count = 0;
			$assign_date_formate_list  = array("DMY"=>"d-m-Y","YMD"=>"Y-m-d","DFY"=>"d F Y","MY"=>"F-Y","YM"=>"Y-F","D"=>"d","M"=>"M","Y"=>"Y");
			$split_qry    = 'select * from cw_print_split where trans_status = 1 and split_table_info ="'.$print_doc_id.'"';
			$split_data   = $this->db->query("CALL sp_a_run ('SELECT','$split_qry')");
			$split_result = $split_data->result();
			$split_data->next_result();
			$split_array = array();
			foreach($split_result as $split){
				$split_info  = $split->split_info;
				$split_colum = $split->split_colum;
				$split_array[$split_colum] = $split_info;
			}		
			if($final_result){
				$data['print_sts'] = true;
				foreach($final_result as $rslt){
					$count++;
					$map_column = explode(",",$print_block_column);
					$td_line = "";
					foreach($map_column as $table_column){
						$map_column   = explode(".",$table_column);
						$column 	  = $map_column[1];
						$value        = $rslt->$column;
						$replace_val  = "@".$column."@";
						//amount number is changed to in words for net pays--07SEP2019
						if($column == 'net_pay'){
							$value         = $rslt->$column;
							$print_design  = str_replace($replace_val,$value,$print_design);
							$net_pay_val   = $value;
							$net_pay_words = $this->numbertowords($net_pay_val);
							$net_pay_words = strtoupper($net_pay_words);
							$print_design  = str_replace("@net_pay_words@",$net_pay_words,$print_design);
						}else
						if($column == 'employee_name'){
							$value         = ucwords($rslt->$column);
							$print_design  = str_replace($replace_val,$value,$print_design);
						}else
						if($column == 'reporting_person'){
							$value         = ucwords($rslt->$column);
							$print_design  = str_replace($replace_val,$value,$print_design);
						}else
						if($column == 'salary'){
							$value         = $rslt->$column;
							$print_design  = str_replace($replace_val,$value,$print_design);
							$salary_val   = $value;
							$salary_words = $this->numbertowords($salary_val);
							$salary_words = ucwords($salary_words);
							$print_design  = str_replace("@salary_words@",$salary_words,$print_design);
						}
						
						if($split_array[$replace_val]){
							//Process split informtion 
							$process_function = $split_array[$replace_val];
							if((int)$process_function === 1){
								$transactions_month = $final_result[0]->transactions_month;
								$employee_code      = $final_result[0]->employee_code;
								$loan_info = $this->get_loan_value($transactions_month,$employee_code);
								$print_design = str_replace($replace_val,$loan_info,$print_design);
							}
						}else{
							if($print_block_type === 1){
								$print_design = str_replace($replace_val,$value,$print_design);
								foreach($assign_date_formate_list as $key=>$formate){
									if($column == 'transactions_month'){//transactions month static updated
										$start         = "@".$key."_";
										$end           = "_".$key."@";
										$replace_val   = $start.$column.$end;
										$value         = date('Y-m-d',strtotime("01-".$rslt->$column));
										$date_value    = date_create($value);
										$replace_value = strtoupper(date_format($date_value,$formate));
										$print_design  = str_replace($replace_val,$replace_value,$print_design);
									}else{//not static month updated
										$start         = "@".$key."_";
										$end           = "_".$key."@";
										$replace_val   = $start.$column.$end;
										$replace_val   = $start.$column.$end;
										$date_value    = date_create($value);
										$replace_value = date_format($date_value,$formate);
										$print_design  = str_replace($replace_val,$replace_value,$print_design);
									}
								}
							}else
							if($print_block_type === 2){
								$td_line .= "<td style='text-align:center;'>$value</td>";
							}
							if($count === 1){
								$head_name = ucwords(str_replace("_"," ",$column));
								$th_line .= "<th style='text-align:center;'>$head_name</th>";
							}
						}
					}
					if($print_block_type === 2){
						if($count === 1){
							$th_line  = "$th_line";
						}
						$tr_line .= "<tr>$td_line</tr>";
					}
				}
				if($print_block_type === 2){
					$table_list  = "<table style='width:100%;'><thead>$th_line</thead><tbody>$tr_line</tbody></table>";
					$replce_block = "@".strtolower(str_replace(" ","_",$print_block_name))."@";
					$print_design = str_replace($replce_block,$table_list,$print_design);
				}
			}
			$data['suppressed_data'] = $suppressed_data;
		}
		$print_design = str_replace("<br>","",$print_design);
		$data['print_design'] = $print_design;
		return $data;
	}
	
	public function send_email_directly($category,$employee_email_id,$view_id,$offer_no,$employee_name,$enroll_status,$reason_list){
		$to_email        = $employee_email_id;
		$employee_name   = ucwords(strtolower($employee_name));
		$url_path_enrol  = base_url()."enrolment";
		$url_path_offer  = base_url()."offer";
		$reason_list     = implode(",",$reason_list);
		//Get company name
		$company_query  = 'SELECT company_name FROM cw_company_information WHERE trans_status = 1';
		$company_info   = $this->db->query("CALL sp_a_run ('SELECT','$company_query')");
		$company_result = $company_info->result();
		$company_info->next_result();
		if($company_result){
			$company_name   = $company_result[0]->company_name;
		}
		if((int)$enroll_status === 3){
			$design_query  = 'SELECT prime_print_info_id,print_design,print_info_for,print_info_module_id,print_type FROM cw_print_design inner join cw_print_info on cw_print_info.prime_print_info_id = cw_print_design.print_design_for WHERE FIND_IN_SET("'.$category.'",print_info_for) and print_info_module_id = "'.$this->control_name.'" and cw_print_design.trans_status = 1';
			$design_info   = $this->db->query("CALL sp_a_run ('SELECT','$design_query')");
			$design_result = $design_info->result();
			$design_info->next_result();
			$print_doc_id  = $design_result[0]->prime_print_info_id;
			$print_design  = $design_result[0]->print_design;
			$print_type    = $design_result[0]->print_type;
			$data          = $this->load_design_data($print_doc_id,$print_type,$print_design,$view_id);
			$table_data = "<!DOCTYPE html><html> <body>".$data['print_design']."</body></html>";
			$pdf_file   = $this->generate_pdf($table_data,$category,$offer_no);	
		}
		if($employee_email_id !== ''){	
			//Get Email Configuration data
			$config_query  = 'SELECT smtp_server,sender_name,bcc,port_no,sender_email,mail_username,mail_password FROM cw_mail_configurations WHERE trans_status = 1';
			$config_info   = $this->db->query("CALL sp_a_run ('SELECT','$config_query')");
			$config_result = $config_info->result();
			$config_info->next_result();
			$smtp_server   = $config_result[0]->smtp_server;
			$sender_name   = $config_result[0]->sender_name;
			//$bcc           = explode(",",$config_result[0]->bcc);
			$port_no       = $config_result[0]->port_no;
			$sender_email  = $config_result[0]->sender_email;
			$username      = $config_result[0]->mail_username;
			$password      = $config_result[0]->mail_password;
			require('./phpmailer/class.phpmailer.php');
			$rslt = array();
			try{
				$mail = new PHPMailer();
				//$mail->SMTPDebug = 3; 
				$mail->IsSMTP();
				$mail->Host = $smtp_server; // Your SMTP PArameter
				$mail->Port = $port_no; // Your Outgoing Port
				$mail->SMTPAuth = true; // This Must Be True
				$mail->Username = $username; // Your Email Address
				$mail->Password = $password; // Your Password
				$mail->SMTPSecure = 'tls'; // Check Your Server's Connections for TLS or SSL
				$mail->From     = $sender_email;
				$mail->FromName = $sender_name;
				if($to_email){		
					$mail->AddAddress($to_email);
				}
				$mail->IsHTML(true);
				if((int)$enroll_status === 3){
					$mail->Subject = "***Welcome To Our World***";
					$mail->Body    = "<!DOCTYPE html><html><body>
					<p>Dear $employee_name,</p>
					<p>We are pleased to extend the following offer of Employment/Consultent to you on behalf of $company_name.</p>
					<p>Please find the detailed offer letter in the attachments.</p>
					<p>Please ensure that you take the time to review our offer. It includes details about your compensation, benefits and terms & conditions of your anticipated employment with us.</p>
					<p>If you choose to accept please click the below link to get stared with the rest of the onboarding process and submit at your earliest convenience.</p>
					<p style='color: #050505;font-weight: 800;'>Link For filling employee details $url_path_enrol</p>
					<p>We hope you will enjoy your role and make a significant contribution to overall success of $company_name.</p>
					<p>We are excited about the possibility of you joining and if you  have  any questions, please contact us.</p>
					</body></html>";
					//Attachment
					$folder = $pdf_file;
					if(file_exists($folder)){
						$mail->addAttachment($folder); // Add attachments
						$mail= $mail->Send();
						if($mail){
							$status = 1;
							$msg = "Mail Sent Successfully";
							$rslt = array('sts'=>$status,'msg'=>$msg);
							$tr_line .= "<tr><td>$category</td><td style='color:green !important;'>$msg</td></tr>";
						}							
					}else{
						$status = 0;
						$msg = "File Not Exist";
						$rslt = array('sts'=>$status,'msg'=>$msg);
						$tr_line .= "<tr><td>$category</td><td style='color:green !important;'>$msg</td></tr>";
					}
				}else
				if((int)$enroll_status === 4){
					$mail->Subject = "Reupload your documents";
					$mail->Body    = "<!DOCTYPE html><html><body>
					<p>Dear $employee_name,</p>
					<p>Your upload files $reason_list images are not cleared, please reupload your documents in below links: $url_path_offer
					</body></html>";
					$mail= $mail->Send();
					if($mail){
						$status = 1;
						$msg = "Mail Sent Successfully";
						$rslt = array('sts'=>$status,'msg'=>$msg);
					}else{
						$status = 0;
						$msg = "File Not Exist";
						$rslt = array('sts'=>$status,'msg'=>$msg);
					}
				}else
				if((int)$enroll_status === 1){
					$mail->Subject = "Document Upload link";
					$mail->Body    = "<!DOCTYPE html><html><body>
					<p>Dear $employee_name,</p>
					<p>This is your offer reference number: $offer_no.
					<p>Please find the following link to upload your documents: $url_path_offer</p>
					</body></html>";
					$mail= $mail->Send();
					if($mail){
						$status = 1;
						$msg = "Mail Sent Successfully";
						$rslt = array('sts'=>$status,'msg'=>$msg);
					}else{
						$status = 0;
						$msg = "File Not Exist";
						$rslt = array('sts'=>$status,'msg'=>$msg);
					}
				}
			}catch(phpmailerException $e){
				$status = 0;
				$msg = "Mail Not Sent";
				$rslt = array('sts'=>$status,'msg'=>$msg);
				$tr_line .= "<tr><td>$category</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
			}catch(Exception $e){
				$status = 0;
				$msg = "Mail Not Sent";
				$rslt = array('sts'=>$status,'msg'=>$msg);
				$tr_line .= "<tr><td>$category</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
			}
			$table_info = "<table><tbody>$tr_line</tbody></table>";
			return $rslt;
		}
	}
	
	// GENERATE PDF
	public function generate_pdf($table_data,$category,$offer_no){
		// Load pdf library
        $this->load->library('pdf');
			// Load HTML content 
		$this->dompdf->loadHtml($table_data);
			// (Optional) Setup the paper size and orientation
		$this->dompdf->setPaper('A4', 'portrait');
			// Render the HTML as PDF
		$this->dompdf->render();
			// Output the generated PDF (1 = download and 0 = preview)
		$output = $this->dompdf->output();
		$folder = "./offer_letter/offer_letter_".$category;

		//Check Folder Exist
		if (!file_exists($folder)){
			mkdir($folder, 0777, true);
		}
			//Check File Exist
		if(file_exists($folder."/".$offer_no.".pdf")){
			unlink($folder."/".$offer_no.".pdf");
		}
		$file_content = file_put_contents($folder."/".$offer_no.".pdf" , $output);
		$path = $folder."/".$offer_no.".pdf";
		chmod($path, 0777, true);
		return $path;
	}
	public function generate_number(){
		$max_val = 200000;
		$max_no_qry = 'select IFNULL(MAX(offer_reference_number),0)as max_offer_number from cw_offer_letter where trans_status = 1 and offer_reference_number >= '.$max_val;
		$offer_max_data    = $this->db->query("CALL sp_a_run ('SELECT','$max_no_qry')");
		$offer_max_result  = $offer_max_data->result();
		$offer_max_data->next_result();
		$max_count         = (int)$offer_max_result[0]->max_offer_number;
		if($max_count === 0){
			$offer_reference_number = $max_val + 1;
		}else{
			$offer_reference_number = $max_count + 1;
		}
		return 	$offer_reference_number;
	}
	
	//Manually enter the employee code exit checking
	public function employee_code_exit(){	
		$employee_code  = $this->input->post('employee_code');
		$form_id        = $this->input->post('view_id');
		$emp_code_check_qry    = 'select * from cw_offer_letter where emp_code = "'.$employee_code.'" and prime_offer_letter_id !="'.$form_id.'"';
		$emp_code_check_info   = $this->db->query("CALL sp_a_run ('RUN','$emp_code_check_qry')");
		$emp_code_check_result = $emp_code_check_info->result();
		$emp_code_check_info->next_result();
		$num_rows      = $emp_code_check_info->num_rows();
		if((int)$num_rows > 0){
			echo json_encode(array('success' => FALSE, 'message' => "Employee Code already Exit! please enter another code"));
		}else{
			echo json_encode(array('success' => TRUE, 'message' => "Complete your offer"));
		}
	}
	
	public function get_position(){
		$department      = $this->input->post('department');	
		$position_qry = 'select prime_position_id,position_name from cw_position where department ="'.$department.'" and trans_status = 1';
		$position_info   = $this->db->query("CALL sp_a_run ('SELECT','$position_qry')");
		$position_result = $position_info->result();
		$position_info->next_result();
		$position_list = "<option value=''>--- Select Designation ---</option>";
		foreach($position_result as $result){
			$id        = $result->prime_position_id;
			$position  = $result->position_name;
			$position_list .= "<option value='$id'>$position</option>";
		}
		echo $position_list;
	}
	
	//candidate status to rms
	public function rms_save($post_data){
		$post_url  = "https://rms.cafsjobs.com/app/api_controller.php";
		$post_data['frm'] = "offer_save";
		$response  = $this->curl($post_url,$post_data);
		$response  = json_decode($response);
		echo $response;
	}
}
?>