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/wealth_cafsindia_com/application/controllers/Life_insurance.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Life_insurance  extends Base_controller{
	
	public function __construct(){
		parent::__construct('life_insurance');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
		$this->collect_base_info();
	}
	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$data['table_headers']= $this->xss_clean(get_dbtable_headers($this->table_info));
		$data['fliter_list']  = $this->fliter_list;
		$data['link_info']    = $this->quick_link;
		$this->load->view("$this->control_name/manage",$data);
	}
	
	//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;	
		
		//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];	
		
		//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;
					}					
				}
			}
		}
		$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;
		
		$renewal_query  = "select * from cw_life_insurance_renewal where renewal_policy_id = '$form_view_id' and trans_status = '1'";
		$renewal_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$renewal_query.'")');
		$renewal_result = $renewal_info->result();
		$renewal_count  = (int)$renewal_info->num_rows();
		$renewal_info->next_result();
		$data['renewal_premium_mode'] = (int)$renewal_result[0]->premium_mode;
		$data['renewal_count']        = $renewal_count-1;
		$this->load->view("$this->control_name/form",$data);
	}
	
	//IMPORT FILE VIEW INFORMATION
	public function import(){
		$data['module_id']     = $this->control_name;		
		$excel_format_qry = 'select prime_excel_format_id,excel_name from cw_util_excel_format where excel_module_id = "'.$this->control_name.'" and trans_status = 1';
		$excel_format   = $this->db->query("CALL sp_a_run ('SELECT','$excel_format_qry')");
		$excel_result    = $excel_format->result();
		$excel_format->next_result();
		$excel_format_drop[""] = "---- Excel Format ----";
		foreach($excel_result as $excel){
			$prime_excel_format_id = $excel->prime_excel_format_id;
			$excel_name            = $excel->excel_name;
			$excel_format_drop[$prime_excel_format_id] = $excel_name;
		}
		$data['excel_format_drop'] = $excel_format_drop;
		
		$this->load->view("$this->control_name/import",$data);
	}
	
	//LOAD PAGE TABLE VIEW WITH DATA BASED ON SEARCH FILTERS
	public function search(){
		$search            = $this->input->get('search');
		$limit             = $this->input->get('limit');
		$offset            = $this->input->get('offset');
		$sort              = $this->input->get('sort');
		$order             = $this->input->get('order');
		$fliter_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'];
			$condition_id     = $label_id."_cond";
			$label_id         = "search_".$label_id."_cond";
			
			if($field_isdefault === 1){
				$column_name = $this->prime_table .".$label_id";
			}else
			if($field_isdefault === 2){
				$column_name = $this->cf_table .".$label_id";
			}
			
			if($field_type === 4){
				$search_val = $_GET[$label_id];
				$search_val = date('Y-m-d',strtotime($search_val));
				$condition  = trim(implode(",",$_GET[$condition_id]));
			}else
			if(((int)$field_type === 5) || ((int)$field_type === 7)){
				$search_val = trim(implode(",",$_GET[$label_id]));
				$condition  = "in";
			}else{
				$search_val = trim(implode(",",$_GET[$label_id]));
				$condition  = trim(implode(",",$_GET[$condition_id]));
			}
			
			if(($condition) && ($search_val)){
				if($condition === "LIKE"){ $search_val = "$search_val%"; }
				$column_name = str_replace("search_",'',$column_name);
				$column_name = str_replace("_cond",'',$column_name);
				if(((int)$field_type === 5) || ((int)$field_type === 7)){
					$search_val  = str_replace(",",'","',$search_val);					
					$fliter_query .= ' and '.$column_name." ". $condition .' ("'.$search_val.'")'; 
				}else{
					$fliter_query .= ' and '.$column_name." ". $condition .' "'.$search_val.'"'; 
				}				
			}
		}
		
		$common_search = "";
		if($search){
			$count=0;
			foreach($this->form_info as $setting){
				$prime_form_id   = $setting->prime_form_id;
				$field_type      = $setting->field_type;
				$pick_list       = $setting->pick_list;
				$pick_table      = $setting->pick_table;
				$pick_list_type  = $setting->pick_list_type;
				$input_view_type = (int)$setting->input_view_type;
				$auto_prime_id      = $setting->auto_prime_id;
				$auto_dispaly_value = $setting->auto_dispaly_value;
				$label_id        = strtolower(str_replace(" ","_",$setting->label_name));
				$field_isdefault    = (int)$setting->field_isdefault;
				if($field_isdefault === 1){
					$pick_sel_table = "$this->prime_table";
				}else
				if($field_isdefault === 2){
					$pick_sel_table = "$this->cf_table";
				}
				$other_label_id = $pick_sel_table.".".$label_id;
				
				if(($input_view_type === 1) || ($input_view_type === 2)){
					if((int)$field_type === 4){
						if(strtotime($search)){
							$search_val = date('Y-m-d',strtotime($search));
							$common_search .= ' or '. $other_label_id .' like "'.$search_val.'%"';
						}
					}else
					if(((int)$field_type === 5) || ((int)$field_type === 7)){
						if((int)$pick_list_type === 1){
							$column_name = explode(",",$pick_list);
							$column_name = $column_name[1];
						}else{
							$column_name = $pick_table."_value";
						}
						$pick_query_as  = $pick_table."_".$prime_form_id;
						$label_id       = "$pick_query_as.$column_name";
						$common_search .= ' or '. $label_id .' like "'.$search.'%"';
					}else
					if((int)$field_type === 9){
						$pick_query_as  = $pick_table."_".$prime_form_id;
						$label_id       = "$pick_query_as.$auto_dispaly_value";
						$common_search .= ' or '. $label_id .' like "'.$search.'%"';
					}else{
						if(!strtotime($search)){
							$common_search .= ' or '. $other_label_id .' like "'.$search.'%"';
						}
					}
				}
			}
			if($common_search){
				$common_search = ltrim($common_search,' or ');
				$common_search = " and ($common_search)";
			}
		}
				
		if(!$sort){ $sort = $this->prime_table.".".$this->prime_id; }
		if(!$order){ $order = "asc";  }
		
		$select_info   = str_replace("@SELECT",$this->select_query,$this->base_query);
		$search_query  = $select_info. $this->pick_query;
		
		//ADDED BASIC,FILTER,COMMON QUERY HERE 
		$basic_query = "";
		if($this->table_search_info){
			$basic_query = $this->table_search_info;
		}
		$search_query .= " where $this->prime_table.trans_status = 1 $basic_query $fliter_query $common_search";
		$search_query .= " ORDER BY  $sort $order";
		$search_query .= " LIMIT  $offset,$limit";
		
		//FETCH RECORDS DATA
		$search_data   = $this->db->query("CALL sp_a_run ('SELECT','$search_query')");
		$search_result = $search_data->result();
		$num_rows      = $search_data->num_rows();
		$search_data->next_result();
		$data_rows     = array();
		foreach($search_result as $search){
			$renewal_policy_id = $search->prime_life_insurance_id;
			$is_exist_qry  = 'SELECT count(*) as rslt_count FROM cw_life_insurance_renewal where renewal_policy_id = "'.$renewal_policy_id.'" and trans_status = 1 ';
			$is_exist_data = $this->db->query("CALL sp_a_run ('SELECT','$is_exist_qry')");
			$exist_rslt    = $is_exist_data->result();
			$is_exist_data->next_result();
			$renewal_count = $exist_rslt[0]->rslt_count;
			$search->renewal_count = $renewal_count;
			$data_rows[]=get_dbdata_row($search,$this->table_info,$this);
		}
		//$data_rows=$this->xss_clean($data_rows);
		
		//FETCH RECORDS COUNT
		$count_info    = "count(*) as rslt_count";
		$base_query    = str_replace("@SELECT",$count_info,$this->base_query);
		$search_query  = $base_query. $this->pick_query;
		$search_query .= " where $this->prime_table.trans_status = 1 $basic_query $fliter_query $common_search";
		$count_data    = $this->db->query("CALL sp_a_run ('SELECT','$search_query')");
		$count_result  = $count_data->result();
		$count_data->next_result();
		echo json_encode(array('total'=>$count_result[0]->rslt_count,'rows'=>$data_rows));
	}
	
	//SAVE MODEL DATA TO DATA BASE
	public function save(){
		$unq_chk         = array();
		$prime_qry_key   = "";
		$prime_qry_value = "";
		$prime_upd_query = "";
		$cf_qry_key      = "";
		$cf_qry_value    = "";
		$cf_upd_query    = "";	
		$cf_has          = false;
		$form_id         = (int)$this->input->post($this->prime_id);
		$form_post_data  = array();	
		foreach($this->form_info as $setting){
			$field_type      = $setting->field_type;
			$input_view_type = (int)$setting->input_view_type;
			$label_id        = strtolower(str_replace(" ","_",$setting->label_name));
			$field_isdefault = $setting->field_isdefault;
			$unique_field    = (int)$setting->unique_field;
			$view_name       = $setting->view_name;
			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){
				$value = date('Y-m-d',strtotime($value));
			}
			
			if(($input_view_type === 1) || ($input_view_type === 2)){
				if((int)$field_isdefault === 1){
					$prime_qry_key     .= $label_id.",";
					$prime_qry_value   .= '"'.$value.'",';
					$prime_upd_query   .= $label_id.' = "'.$value.'",';
					if($unique_field === 1){
						$prime_unq_chk = $label_id.'= "'.$value.'"';
						$query = "select count(*) as rslt_count from $this->prime_table where $this->prime_id != $form_id and $prime_unq_chk";
						$unq_chk[] = array('label_id'=>$label_id,'view_name'=>$view_name,'query'=>$query,);
					}
				}else{
					$cf_has = true;
					$cf_qry_key     .= $label_id.",";
					$cf_qry_value   .= '"'.$value.'",';
					$cf_upd_query   .= $label_id.' = "'.$value.'",';
					if($unique_field === 1){
						$cf_unq_chk = $label_id.'= "'.$value.'"';
						$query = "select count(*) as rslt_count from $this->cf_table where $this->prime_id != $form_id and $cf_unq_chk";
						$unq_chk[] = array('label_id'=>$label_id,'view_name'=>$view_name,'query'=>$query,);
					}
				}
			}
		}
		$rslt_count = 0;
		$can_process = array();
		foreach($unq_chk as $unq_rslt){
			$query       = $unq_rslt['query'];
			$label_id    = $unq_rslt['label_id'];
			$view_name   = $unq_rslt['view_name'];
			$unq_info    = $this->db->query("CALL sp_a_run ('RUN','$query')");
			$unq_result  = $unq_info->result();
			$unq_info->next_result();
			if($unq_result){
				$rslt_count = (int)$unq_result[0]->rslt_count;
				if($rslt_count !== 0){
					$can_process[] = $view_name." already exist";
				}
			}
		}
		if(count($can_process) > 0){
			$can_process  = array_values($can_process);			
			$can_process  = implode(",<br/>", $can_process);
			echo json_encode(array('success' => false, 'message' => $can_process,));
		}else{
			$created_on = date("Y-m-d h:i:s");
			if((int)$form_id === 0){
				$prime_qry_key     .= "trans_created_by,trans_created_date";
				$prime_qry_value   .= '"'.$this->logged_id.'",'.'"'.$created_on.'"';
				$prime_insert_query = "insert into $this->prime_table ($prime_qry_key) values ($prime_qry_value)";
				$insert_info        = $this->db->query("CALL sp_a_run ('INSERT','$prime_insert_query')");
				$insert_result      = $insert_info->result();
				$insert_info->next_result();
				$insert_id = $insert_result[0]->ins_id;
				
				$cf_qry_key     .= "trans_created_by,trans_created_date";
				$cf_qry_value   .= '"'.$this->logged_id.'",'.'"'.$created_on.'"';
				$cf_insert_query = 'insert into ' .$this->cf_table. ' ('.$this->prime_id.','.$cf_qry_key.') values ("'.$insert_id.'",'.$cf_qry_value.')';
				$insert_info     = $this->db->query("CALL sp_a_run ('INSERT','$cf_insert_query')");
				$insert_info->next_result();
				
				$this->renewal_save($insert_id);
				echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id));
			}else{
				$prime_upd_query    .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
				$prime_update_query  = 'UPDATE '. $this->prime_table .' SET '. $prime_upd_query .' WHERE '. $this->prime_id .' = "'. $form_id .'"';
				$this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')");
				
				$cf_upd_query    .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
				$cf_update_query  = 'UPDATE '. $this->cf_table .' SET '. $cf_upd_query .' WHERE '. $this->prime_id .' = "'. $form_id .'"';
				$this->db->query("CALL sp_a_run ('UPDATE','$cf_update_query')");
				
				$this->renewal_save($form_id);
				echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'insert_id' => $form_id));
			}
		}
	}	
	//UPDATE STATUS TO DELETE IN MODULE PRIMARY TABLE
	public function delete(){
		$delete_ids = implode(",",$this->xss_clean($this->input->post('ids')));
		$created_on = date("Y-m-d h:i:s");
		$prime_upd_query    .= 'trans_deleted_by = "'. $this->logged_id .'",trans_deleted_date = "'.$created_on.'"';
		$prime_update_query  = 'UPDATE '. $this->prime_table .' SET trans_status = 0,'. $prime_upd_query .' WHERE '. $this->prime_id .' in ('. $delete_ids .')';
		if($this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')")){
			echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to delete"));
		}
	}
	
	//UPDATE STATUS TO DELETE FOR UPLOAD FILES or DOCUMENTS
	public function remove_file(){
		$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"));
		}
	}
	
	/* UDY CUSTOME BLOCK - START */
	//GET GST VAL 
	public function get_gst_val(){
		$product      = $this->input->post('product');
		$issued_date  = $this->input->post('issued_date');
		$issued_date  = new DateTime($issued_date);
		$issued_date  = $issued_date->format("Y-m-d");
		if(($product !== "") && ($issued_date !== "")){
			$gst_info = $this->fetch_gst_info($product,$issued_date);
			if(!$gst_info){
				echo json_encode(array('success' => false, 'message' => "Please contact admin to updated your product information"));
			}else{
				$gst            = $gst_info->gst;
				$additional_gst = $gst_info->additional_gst;
				$rider_gst      = $gst_info->rider_gst;
				echo json_encode(array('success' => true, 'gst' => $gst, 'additional_gst' => $additional_gst, 'rider_gst' => $rider_gst));
			}
		}else{
			echo json_encode(array('success' => false, 'message' => "Please select Company, Product and Issued Date"));
		}
		//echo "UDY :: $company_name :: $product :: $issued_date";
	}
	//GET GST VAL 
	public function fetch_gst_info($product,$issued_date){
		$issued_date  = new DateTime($issued_date);
		$issued_date  = $issued_date->format("Y-m-d");
		$fetch_qry    = 'SELECT start_date,end_date,gst,additional_gst,rider_gst FROM cw_product inner join cw_product_type on cw_product.product_type = cw_product_type.prime_product_type_id where cw_product.trans_status = 1 and  cw_product.prime_product_id = "'.$product.'" and end_date >= date("'.$issued_date.'")';
		$fetch_info   = $this->db->query("CALL sp_a_run ('SELECT','$fetch_qry')");
		$fetch_result = $fetch_info->result();		
		$fetch_info->next_result();
		if($fetch_result){
			return $fetch_result[0];
		}else{
			return false;
		}
	}
	//GET CUSTOMER INFORMATION
	public function fetch_cust_info(){
		$cust_id   = $this->input->post('cust_id');
		$fetch_qry    = 'SELECT * FROM cw_customer inner join cw_customer_cf on cw_customer.prime_customer_id = cw_customer_cf.prime_customer_id  where cw_customer.prime_customer_id = "'.$cust_id.'"';
		$fetch_info   = $this->db->query("CALL sp_a_run ('SELECT','$fetch_qry')");
		$fetch_result = $fetch_info->result();
		$fetch_info->next_result();
		$dob = date('d-m-Y',strtotime($fetch_result[0]->dob));		
		echo json_encode(array('success' => true, 'dob' => $dob));
	}
	
	//FETCH RENEWAL INFORMATION 
	public function renewal($policy_id){
		$policy_query   = str_replace("@SELECT",$this->select_query,$this->base_query);
		$policy_query   = $policy_query. $this->pick_query;
		$policy_query  .= " where $this->prime_table.trans_status = 1 and $this->prime_table.$this->prime_id = '$policy_id'";
		$policy_info    = $this->db->query('CALL sp_a_run ("SELECT","'.$policy_query.'")');
		$policy_result  = $policy_info->result();
		$policy_info->next_result();
		$data['policy_id']      = $policy_id;
		$data['policy_result']  = $policy_result[0];
		$data['next_renewal']   = $this->next_renewal_data($policy_id);
		$data['renewal_result'] =  $this->renewal_data($policy_id);
		$this->load->view("$this->control_name/renewal",$data);
	}
	
	//SAVE FIRST RENEWAL DATA
	public function renewal_save($renewal_policy_id){		
		$is_exist_qry  = 'SELECT count(*) as rslt_count FROM cw_life_insurance_renewal where renewal_policy_id = "'.$renewal_policy_id.'" and trans_status = 1 ';
		$is_exist_data = $this->db->query("CALL sp_a_run ('SELECT','$is_exist_qry')");
		$exist_rslt    = $is_exist_data->result();
		$is_exist_data->next_result();
		
		$policy_query   = str_replace("@SELECT","*",$this->base_query);
		$policy_query .= " where $this->prime_table.trans_status = 1 and $this->prime_table.$this->prime_id = '$renewal_policy_id'";
		$policy_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$policy_query.'")');
		$policy_result = $policy_info->result();
		$policy_info->next_result();
		$issued_date     = $policy_result[0]->issued_date;
		$ppt             = $policy_result[0]->ppt;
		$premium_mode    = $policy_result[0]->premium_mode;
		$premium_amount  = $policy_result[0]->premium_amount;
		$gst             = $policy_result[0]->gst;
		$rider           = $policy_result[0]->rider;
		$rider_yes       = $policy_result[0]->rider_yes;
		$rider_premium   = $policy_result[0]->rider_premium;
		$rider_gst       = $policy_result[0]->rider_gst;
		$total_premium   = $policy_result[0]->total_premium;
		
		$renewal_mode_list = array("1"=>"+1 year","2"=>"+1 months","3"=>"+3 months","4"=>"+6 months","5"=>"+1 year");
		$next_due      = $renewal_mode_list[$premium_mode];
		$issued_date   = new DateTime($issued_date);
		$due_date      = $issued_date->format("Y-m-d");
		$next_due_date = $issued_date->modify("$next_due");
		$next_due_date = $next_due_date->format("Y-m-d");
		
		$logged_id    = $this->session->userdata('logged_id');
		$created_on   = date("Y-m-d h:i:s");
		if((int)$exist_rslt[0]->rslt_count === 0){
			$renewal_qry  = 'INSERT INTO cw_life_insurance_renewal (renewal_policy_id, renewal_mode,renewal_due_date,renewal_premium_amount,renewal_gst,renewal_rider,renewal_rider_yes,renewal_rider_premium,renewal_rider_gst,renewal_total_premium,renewal_next_due_date,trans_created_by, trans_created_date) VALUES ("'.$renewal_policy_id.'","'.$premium_mode.'","'.$due_date.'","'.$premium_amount.'","'.$gst.'","'.$rider.'","'.$rider_yes.'","'.$rider_premium.'","'.$rider_gst.'","'.$total_premium.'","'.$next_due_date.'","'.$logged_id.'","'.$created_on.'")';
			$this->db->query("CALL sp_a_run ('RUN','$renewal_qry')");
		}else
		if((int)$exist_rslt[0]->rslt_count === 1){
			$renewal_upd_qry  = 'UPDATE  cw_life_insurance_renewal SET renewal_mode = "'.$premium_mode.'",renewal_due_date = "'.$due_date.'",renewal_premium_amount = "'.$premium_amount.'",renewal_gst = "'.$gst.'",renewal_rider = "'.$rider.'",renewal_rider_yes = "'.$rider_yes.'",renewal_rider_premium = "'.$rider_premium.'",renewal_rider_gst = "'.$rider_gst.'",renewal_total_premium = "'.$total_premium.'",renewal_next_due_date = "'.$next_due_date.'",trans_updated_by = "'.$logged_id.'",trans_updated_date = "'.$created_on.'" where renewal_policy_id = "'.$renewal_policy_id.'"';
			$this->db->query("CALL sp_a_run ('RUN','$renewal_upd_qry')");
		}
	}
	
	//SAVE NEXT RENEWAL DATA
	public function renewal_update(){
		$prime_renewal_id         = (int)$this->input->post('prime_renewal_id');
		$renewal_policy_id        = (int)$this->input->post('renewal_policy_id');
		$renewal_mode             = (int)$this->input->post('renewal_mode');
		$renewal_due_date         =  date('Y-m-d',strtotime($this->input->post('renewal_due_date')));
		$renewal_premium_amount   = $this->input->post('renewal_premium_amount');
		$renewal_gst              = $this->input->post('renewal_gst');
		$renewal_rider            = $this->input->post('renewal_rider');
		$renewal_rider_yes        = $this->input->post('renewal_rider_yes');
		$renewal_rider_premium    = $this->input->post('renewal_rider_premium');
		$renewal_rider_gst        = $this->input->post('renewal_rider_gst');
		$renewal_total_premium    = $this->input->post('renewal_total_premium');
		$renewal_next_due_date    =  date('Y-m-d',strtotime($this->input->post('renewal_next_due_date')));
		$renewal_note             = $this->input->post('renewal_note');
		$logged_id                = $this->session->userdata('logged_id');
		$created_on = date("Y-m-d h:i:s");
		
		$is_exist_qry  = 'SELECT count(*) as rslt_count FROM cw_life_insurance_renewal where renewal_policy_id = "'.$renewal_policy_id.'" and trans_status = 1 ';
		$is_exist_data = $this->db->query("CALL sp_a_run ('SELECT','$is_exist_qry')");
		$exist_rslt    = $is_exist_data->result();
		$is_exist_data->next_result();
		$rslt_count    = (int)$exist_rslt[0]->rslt_count;
		
		if($prime_renewal_id === 0){
			$is_exist_qry  = 'SELECT count(*) as rslt_count FROM cw_life_insurance_renewal where renewal_policy_id = "'.$renewal_policy_id.'" and renewal_due_date = "'.$renewal_due_date.'" and trans_status = 1 ';
			$is_exist_data = $this->db->query("CALL sp_a_run ('SELECT','$is_exist_qry')");
			$exist_rslt    = $is_exist_data->result();
			$is_exist_data->next_result();
			if((int)$exist_rslt[0]->rslt_count === 0){
				$renewal_qry  = 'INSERT INTO cw_life_insurance_renewal (renewal_policy_id, renewal_mode,renewal_due_date,renewal_premium_amount,renewal_gst,renewal_rider,renewal_rider_yes,renewal_rider_premium,renewal_rider_gst,renewal_total_premium,renewal_next_due_date,renewal_note,trans_created_by, trans_created_date) VALUES ("'.$renewal_policy_id.'","'.$renewal_mode.'","'.$renewal_due_date.'","'.$renewal_premium_amount.'","'.$renewal_gst.'","'.$renewal_rider.'","'.$renewal_rider_yes.'","'.$renewal_rider_premium.'","'.$renewal_rider_gst.'","'.$renewal_total_premium.'","'.$renewal_next_due_date.'","'.$renewal_note.'","'.$logged_id.'","'.$created_on.'")';
				$this->db->query("CALL sp_a_run ('RUN','$renewal_qry')");
				$next_renewal = $this->next_renewal_data($renewal_policy_id);
				$renewal_list = $this->renewal_data($renewal_policy_id);
				echo json_encode(array('success' => true, 'message' => "Renewal successfully added", 'renewal_list' => $renewal_list, 'next_renewal' => $next_renewal));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Renewal already exist"));
			}
		}else{
			$renewal_upd_qry  = 'UPDATE  cw_life_insurance_renewal SET renewal_due_date = "'.$renewal_due_date.'",renewal_mode = "'.$renewal_mode.'",renewal_premium_amount = "'.$renewal_premium_amount.'",renewal_gst = "'.$renewal_gst.'",renewal_rider = "'.$renewal_rider.'",renewal_rider_yes = "'.$renewal_rider_yes.'",renewal_rider_premium = "'.$renewal_rider_premium.'",renewal_rider_gst = "'.$renewal_rider_gst.'",renewal_total_premium = "'.$renewal_total_premium.'",renewal_next_due_date = "'.$renewal_next_due_date.'",renewal_note = "'.$renewal_note.'",trans_updated_by = "'.$logged_id.'",trans_updated_date = "'.$created_on.'" where prime_renewal_id = "'.$prime_renewal_id.'"';
			$this->db->query("CALL sp_a_run ('RUN','$renewal_upd_qry')");
			$next_renewal = $this->next_renewal_data($renewal_policy_id);
			$renewal_list = $this->renewal_data($renewal_policy_id);
			echo json_encode(array('success' => true, 'message' => "Renewal successfully updated", 'renewal_list' => $renewal_list,'next_renewal' => $next_renewal));
		}
	}
	
	//EDIT RENEWAL
	public function renewal_edit(){
		$prime_renewal_id = (int)$this->input->post('prime_renewal_id');
		$renewal_query  = "select *,DATE_FORMAT(renewal_due_date, '%d-%m-%Y') AS renewal_due_date  from cw_life_insurance_renewal where prime_renewal_id = '$prime_renewal_id' and trans_status = '1'";
		$renewal_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$renewal_query.'")');
		$renewal_result = $renewal_info->result();
		$renewal_info->next_result();
		echo json_encode(array('success' => TRUE, 'renewal_rslt' => $renewal_result[0]));
	}
	
	//REMOVE RENEWAL
	public function renewal_remove(){
		$prime_renewal_id  = (int)$this->input->post('prime_renewal_id');
		$renewal_policy_id = (int)$this->input->post('renewal_policy_id');
		$logged_id     = $this->session->userdata('logged_id');		
		$today_date = date("Y-m-d h:i:s");
		$final_qry = 'UPDATE cw_life_insurance_renewal SET trans_updated_by = "'.$logged_id.'",trans_updated_date = "'.$today_date.'" , trans_status = 0 WHERE prime_renewal_id = "'.$prime_renewal_id.'"';
		$this->db->query("CALL sp_a_run ('SELECT','$final_qry')");
		$next_renewal = $this->next_renewal_data($renewal_policy_id);
		$renewal_list = $this->renewal_data($renewal_policy_id);
		echo json_encode(array('success' => TRUE, 'message' => "Remove Successfully",'renewal_list' => $renewal_list,'next_renewal' => $next_renewal));
	}
	
	//CANCEL UPDATED UI
	public function renewal_cancel(){
		$renewal_policy_id = (int)$this->input->post('renewal_policy_id');
		$next_renewal = $this->next_renewal_data($renewal_policy_id);
		$renewal_list = $this->renewal_data($renewal_policy_id);
		echo json_encode(array('success' => TRUE, 'message' => "Cancelled Successfully",'renewal_list' => $renewal_list,'next_renewal' => $next_renewal));
	}
	
	//GET NEXT DUE DATE
	public function update_due_date(){
		$renewal_mode     = $this->input->post('renewal_mode');
		$renewal_due_date = $this->input->post('renewal_due_date');
		
		$renewal_mode_list = array("1"=>"+1 year","2"=>"+1 months","3"=>"+3 months","4"=>"+6 months","5"=>"+1 year");
		$next_due      = $renewal_mode_list[$renewal_mode];
		$issued_date   = new DateTime($renewal_due_date);
		$next_due_date = $issued_date->modify("$next_due");
		$next_due_date = $next_due_date->format("d-m-Y");
		echo json_encode(array('success' => TRUE,'next_due_date' => $next_due_date));
	}
	/****************************************
	UDY :: REMOVED DUE TO PRODUCT TYPE BASED GST
	//GET GST VALUE
	public function get_renewal_gst(){
		$gst_query = "SELECT COLUMN_DEFAULT as default_val FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='cafs_wealth' AND `TABLE_NAME` IN ('cw_life_insurance') and COLUMN_NAME = 'gst'";
		$gst_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$gst_query.'")');
		$gst_result = $gst_info->result();
		$gst_info->next_result();
		return  $gst_result[0]->default_val/2;
	}
	****************************************/
	//GET NEXT RENEWAL DATA
	public function next_renewal_data($renewal_policy_id){
		$policy_query  = str_replace("@SELECT","*",$this->base_query);
		$policy_query .= " where $this->prime_table.trans_status = 1 and $this->prime_table.$this->prime_id = '$renewal_policy_id'";
		$policy_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$policy_query.'")');
		$policy_result = $policy_info->result();
		$policy_info->next_result();		
		$product       = $policy_result[0]->product;		
		
		$renewal_query  = "select prime_renewal_id,renewal_policy_id,renewal_mode,renewal_due_date,renewal_premium_amount,renewal_gst,renewal_rider,renewal_rider_yes,renewal_rider_premium,renewal_rider_gst,renewal_total_premium,renewal_next_due_date,renewal_note from cw_life_insurance_renewal where renewal_policy_id = '$renewal_policy_id' and trans_status = '1' order by prime_renewal_id DESC LIMIT 0,1";
		$renewal_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$renewal_query.'")');
		$renewal_result = $renewal_info->result();
		$renewal_info->next_result();
		
		$next_renewal          = $renewal_result[0];
		$renewal_mode_list     = array("1"=>"+1 year","2"=>"+1 months","3"=>"+3 months","4"=>"+6 months","5"=>"+1 year");
		$next_due              = $renewal_mode_list[$next_renewal->renewal_mode];
		$issued_date           = new DateTime($next_renewal->renewal_next_due_date);
		$next_renewal->renewal_due_date      = $issued_date->format("d-m-Y");
		$next_due_date                       = $issued_date->modify("$next_due");
		$next_renewal->renewal_next_due_date = $next_due_date->format("d-m-Y");
		
		$gst_info = $this->fetch_gst_info($product,$next_renewal->renewal_due_date);
		$next_renewal->renewal_gst           = $gst_info->additional_gst;
		$next_renewal->renewal_rider_gst     = $gst_info->rider_gst;
		
		$renewal_total_premium  = 0;
		if((int)$next_renewal->renewal_rider === 1){
			$gst_val   = $next_renewal->renewal_premium_amount * $next_renewal->renewal_gst / 100;
			$rider_val = $next_renewal->renewal_rider_premium * $next_renewal->renewal_rider_gst / 100;
			$next_renewal->renewal_total_premium = round($next_renewal->renewal_premium_amount+$gst_val+$next_renewal->renewal_rider_premium+$rider_val);
		}else
		if((int)$next_renewal->renewal_rider === 2){
			$gst_val = $next_renewal->renewal_premium_amount * $next_renewal->renewal_gst / 100;
			$next_renewal->renewal_total_premium = round($next_renewal->renewal_premium_amount+$gst_val);
		}
		
		$date_now   = new DateTime();
		$date_now   = $date_now->modify("+1 months");
		$check_date = new DateTime($next_renewal->renewal_due_date);
		$next_renewal->btn_sts = true;
		if($check_date > $date_now) {
			$next_renewal->btn_sts = false;
		}
		$next_renewal->ppt_sts = $this->check_ppt_sts($renewal_policy_id);
		return $next_renewal;
	}
	
	public function check_ppt_sts($renewal_policy_id){
		$policy_query  = str_replace("@SELECT","*",$this->base_query);
		$policy_query .= " where $this->prime_table.trans_status = 1 and $this->prime_table.$this->prime_id = '$renewal_policy_id'";
		$policy_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$policy_query.'")');
		$policy_result = $policy_info->result();
		$policy_info->next_result();
		
		$issued_date     = $policy_result[0]->issued_date;
		$ppt             = $policy_result[0]->ppt;
		$premium_mode    = $policy_result[0]->premium_mode;
		$issued_date     = new DateTime($issued_date);
		$start_date      = $issued_date->format("Y-m-d");
		$maturity_end    = $issued_date->modify("+$ppt year");
		$maturity_end    = $maturity_end->format("Y-m-d");
		$maturity_start  = new DateTime($maturity_end);
		$maturity_start  = $maturity_start->modify("-1 year");
		$maturity_start  = $maturity_start->format("Y-m-d");
		
		$renewal_mode_list     = array("1"=>"12","2"=>"1","3"=>"3","4"=>"6","5"=>"12");
		$ppt_query   = "SELECT renewal_mode,count(*) as ppt  FROM cw_life_insurance_renewal WHERE renewal_policy_id = '$renewal_policy_id' and (renewal_due_date BETWEEN '$maturity_start' AND '$maturity_end') and trans_status = '1' GROUP by renewal_mode";
		$ppt_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$ppt_query.'")');
		$ppt_result = $ppt_info->result();
		$ppt_info->next_result();
		$paid_count = 0;
		foreach($ppt_result as $rslt){
			$renewal_mode = $rslt->renewal_mode;
			$ppt          = $rslt->ppt;
			$month        = $renewal_mode_list[$renewal_mode];
			$paid_count   += (int)$ppt*(int)$month;
		}
		if($paid_count < 12){
			return true;
		}else{
			return false;
		}
	}	
	//GET RENEWAL HISTORY
	public function renewal_data($renewal_policy_id){
		$policy_query  = str_replace("@SELECT","*",$this->base_query);
		$policy_query .= " where $this->prime_table.trans_status = 1 and $this->prime_table.$this->prime_id = '$renewal_policy_id'";
		$policy_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$policy_query.'")');
		$policy_result = $policy_info->result();
		$policy_info->next_result();		
		$sum_assured   = $policy_result[0]->sum_assured;
		$issued_date   = $policy_result[0]->issued_date;
		$ppt           = $policy_result[0]->ppt;
		$maturity_end   = new DateTime($issued_date);
		$maturity_end  = $maturity_end->modify("+$ppt year");
		
		$renewal_query  = "select * from cw_life_insurance_renewal where renewal_policy_id = '$renewal_policy_id' and trans_status = '1' order by prime_renewal_id DESC";
		$renewal_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$renewal_query.'")');
		$renewal_result = $renewal_info->result();
		$renewal_info->next_result();
		$renewal_mode_list      = array(""=>"---- Renewal Mode ----","1"=>"Single","2"=>"Monthly","3"=>"Quarterly","4"=>"Semi Annual","5"=>"Yearly");
		$renewal_rider_list     = array("1"=>"Yes","2"=>"No");
		$renewal_rider_yes_list = array("1"=>"Double Accident Benefit","2"=>"Premium Waiver Benefit","3"=>"Term Rider","4"=>"Hospital Cash","5"=>"Critical Illness Cover","6"=>"Total Partial Disablity","7"=>"Family Income Rider");
		$tr_line = "";
		$count = 0;
		$rslt_count = count($renewal_result);
		foreach($renewal_result as $rslt){
			$count++;
			$prime_renewal_id       = $rslt->prime_renewal_id;
			$renewal_policy_id      = $rslt->renewal_policy_id;
			$renewal_mode           = $rslt->renewal_mode;
			$renewal_due_date       = date('d-m-Y',strtotime($rslt->renewal_due_date));
			$renewal_premium_amount = $rslt->renewal_premium_amount;
			$renewal_gst            = $rslt->renewal_gst;
			$renewal_rider          = $rslt->renewal_rider;
			$renewal_rider_yes      = $rslt->renewal_rider_yes;
			$renewal_rider_premium  = $rslt->renewal_rider_premium;
			$renewal_rider_gst      = $rslt->renewal_rider_gst;
			$renewal_total_premium  = $rslt->renewal_total_premium;			
			$renewal_next_due_date  = date('d-m-Y',strtotime($rslt->renewal_next_due_date));
			$renewal_note           = $rslt->renewal_note;
			
			$renewal_mode           = $renewal_mode_list[$renewal_mode];
			$renewal_rider          = $renewal_rider_list[$renewal_rider];
			$renewal_rider_yes      = $renewal_rider_yes_list[$renewal_rider_yes];
			$edit_btn   = "<a class='btn btn-edit btn-xs row_btn' onclick = renewal_edit('$prime_renewal_id');>Edit</a>";
			$remove_btn = "<a class='btn btn-danger btn-xs row_btn' onclick = renewal_remove('$prime_renewal_id');>Delete</a>";
			$td_btn = "";
			if((int)$count === 1){
				$td_btn = "$edit_btn $remove_btn";
			}
			if($renewal_rider === "No"){
				$renewal_rider_yes     = "-";
				$renewal_rider_premium = "-";
				$renewal_rider_gst     = "-";
			}			
			$paying_year = $this->paying_year($issued_date,$rslt->renewal_due_date);
			
			$date_now   = new DateTime();
			if($date_now > $maturity_end){
				$td_btn = "";
			}
		
			$tr_line .= "<tr>
							<td>$paying_year</td>
							<td>$renewal_mode</td>
							<td style='white-space:nowrap;'>$renewal_due_date</td>
							<td>$sum_assured</td>
							<td>$renewal_premium_amount</td>
							<td>$renewal_gst</td>
							<td>$renewal_rider</td>
							<td>$renewal_total_premium</td>
							<td style='white-space:nowrap;'>$renewal_next_due_date</td>
							<td>$renewal_note</td>
							<td style='white-space:nowrap;'>$td_btn</td>
						</tr>";
		}
		/*
		
		<td>$renewal_rider_yes</td>
		<td>$renewal_rider_premium</td>
		<td>$renewal_rider_gst</td>							
		
		<th>Rider Yes</th>
		<th>Rider Premium</th>
		<th>Rider GST</th>
		*/
		$renewal_list = "<table id='renewal_list' class='table table-bordered' style='background-color: #FFFFFF; box-shadow: none;'>
							<thead>
								<tr>
									<th>PY</th>
									<th>Mode</th>
									<th>Due</th>
									<th>Sum Assured</th>
									<th>Premium</th>
									<th>GST</th>
									<th>Rider</th>
									<th>Total Premium</th>
									<th>Next Due</th>
									<th>Note</th>
									<th>Option</th>
								</tr>
							</thead>
							<tbody>
								$tr_line
							</tbody>
						</table>";
		return $renewal_list;
	}
	public static function paying_year($start_date, $end_date){
		$start_date  = new DateTime($start_date);
		$end_date    = new DateTime($end_date);
		$tot_month   =  $start_date->diff($end_date)->m + ($start_date->diff($end_date)->y*12);		
		$paying_year = floor($tot_month/12)+1;
		return $paying_year;
	}
	
	public function product_info($form_view_id=-1){
		//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];	
		$form_view         = $view_result[0];
		$life_assured_name = $form_view->life_assured_name;
		$product           = $form_view->product;
		$dob               = $form_view->dob;
		$term              = $form_view->term;
		$ppt               = $form_view->ppt;
		$sum_assured       = $form_view->sum_assured;
		$premium_mode      = $form_view->premium_mode;
		$premium_amount    = $form_view->premium_amount;
		$death_benefit     = $form_view->death_benefit;		
		$premium_mode_array  = array(1=>1,2=>12,3=>4,4=>2,5=>1);		
		
		$now = new DateTime();
		$end = new DateTime($dob);
		$interval = $now->diff($end);
		$age =  $interval->format('%y');
		$age = (int)$age - 1;
		$eligibility_query  = 'SELECT * FROM cw_product_eligibility_information where prime_product_id = "'.$product.'" and ppt = "'.$ppt.'" and policy_term = "'.$term.'" and trans_status = 1';
		$eligibility_data   = $this->db->query("CALL sp_a_run ('SELECT','$eligibility_query')");
		$eligibility_result = $eligibility_data->result();
		$eligibility_count  = $eligibility_data->num_rows();
		$eligibility_data->next_result();
		if((int)$eligibility_count === 1){
			$product_eligibility_id = $eligibility_result[0]->prime_product_eligibility_information_id;
			$header_query  = 'SELECT DISTINCT(illustration_header) as illustration_header,table_header,assured FROM cw_illustration_formula inner join cw_illustration_table on prime_illustration_table_id = illustration_header where prime_eligibility_information_id = "'.$product_eligibility_id.'" and cw_illustration_formula.trans_status = 1';
			$header_data   = $this->db->query("CALL sp_a_run ('SELECT','$header_query')");
			$header_result = $header_data->result();
			$header_data->next_result();
			$header_list = array("policy_year" => "Policy Year","age" => "Age","annualized_premium"  => "Annualized Premium","sum_assured"   => "Sum Assured");
			foreach($header_result as $header){
				$illustration_header  = $header->illustration_header;
				$assured              = $header->assured;
				$table_header         = str_replace(" ","_",strtolower($header->table_header));
				$header_list[$assured][$table_header] = 0;
			}
			
			$trans_array    = array();
			$formaula_array = array();
			for($process_year=1;$process_year<=$term;$process_year++){
				$age++;
				if($process_year === 1){
					$gst_val       = $premium_amount * 4.50 / 100;
					$annualized_premium  = round(($premium_amount+$gst_val) * $premium_mode_array[$premium_mode]);
				}else{
					$gst_val       = $premium_amount * 2.25 / 100;
					$annualized_premium  = round(($premium_amount+$gst_val) * $premium_mode_array[$premium_mode]);
				}				
				
				$illustration_query  = 'SELECT * FROM cw_illustration_formula inner join cw_illustration_table on prime_illustration_table_id = illustration_header where prime_eligibility_information_id = "'.$product_eligibility_id.'" and year = "'.$process_year.'" and cw_illustration_formula.trans_status = 1';
				$illustration_data   = $this->db->query("CALL sp_a_run ('SELECT','$illustration_query')");
				$illustration_result = $illustration_data->result();
				$illustration_result = $illustration_data->result();
				$illustration_data->next_result();
				
				foreach($illustration_result as $illustration){
					$table_header        = str_replace(" ","_",strtolower($illustration->table_header));
					$illustration_header = $illustration->illustration_header;
					$year                = $illustration->year;
					$assured             = $illustration->assured;
					$formula             = $illustration->formula;

					$formula =  str_replace("@sum_assured@","$sum_assured",strtolower($formula));
					$formula =  str_replace("@premium@","$premium_amount",strtolower($formula));
					$formula =  str_replace("@death_benifit@","$death_benefit",strtolower($formula));
					$for_year_list[$assured][$table_header] = $formula;
				}
				$header_list['policy_year'] = $process_year;
				$header_list['age'] = $age;
				if($process_year <= $ppt){
					$header_list['annualized_premium'] = $annualized_premium;
				}else{
					$header_list['annualized_premium'] = 0;
				}
				
				$header_list['sum_assured'] = $sum_assured;
				$trans_array[$process_year] = $header_list;
				if($for_year_list){
					$formaula_array[$process_year] = $for_year_list;
				}
				$for_year_list = "";
			}
			$data['header_list']  = $header_list;
			$data['product_info'] = $this->create_formula_file($formaula_array,$trans_array);
		}else{
			$data['product_info'] = "No data available";
		}		
		$this->load->view("$this->control_name/product_info",$data);
	}
	/*CREATE FORMULA FILE - START*/
	function create_formula_file($formaula_array,$trans_array){
		$file_list = "";
		foreach($formaula_array as $paying_year => $formula_list){
			foreach($formula_list as $assured_key => $list){				
				foreach($list as $key => $final){
					$file_list .= "\n\t".'$trans_array['.$paying_year.']['.$assured_key.']["'.$key.'"] = '.$final.";";
				}
			}
		}
		$file_list = $file_list."\n\t".' return $trans_array;'."\n";
		$logged_id    = $this->session->userdata('logged_id');
		$dynamic_file_name= "product_info_".$logged_id.".php";
		unlink("$dynamic_file_name");
		$fname = 'product_info($trans_array){';
		$code = "<?php \n function $fname $file_list}\n?>";
		fopen("$dynamic_file_name", "w");
		file_put_contents("$dynamic_file_name",$code);
		require_once("$dynamic_file_name");
		$fianl_result_array = product_info($trans_array);
		unlink("$dynamic_file_name");
		return $fianl_result_array;
	}
	/*CREATE FORMULA FILE - END*/
	
	public function policy_no_exist(){
		$policy_no     = trim($this->input->post('policy_no'));
		$exist_query  = 'SELECT count(*) as policy_count FROM cw_life_insurance where trans_status = 1 and policy_no = "'.$policy_no.'"';
		$exist_data   = $this->db->query("CALL sp_a_run ('SELECT','$exist_query')");
		$exist_result = $exist_data->result();
		$exist_data->next_result();
		if((int)$exist_result[0]->policy_count > 0){
			echo json_encode(array('success' => false, 'message' => "Policy number already exist"));
		}else{
			echo json_encode(array('success' => true, 'message' => "New Policy number"));
		}
	}
	
	public function calculate_premium(){
		$premium_amount = $this->input->post('premium_amount');
		$gst            = $this->input->post('gst');
		$rider_premium  = $this->input->post('rider_premium');
		$rider_gst      = $this->input->post('rider_gst');
		$rider          = (int)$this->input->post('rider');
		$total_premium  = 0;
		if($rider === 1){
			$gst_val       = $premium_amount * $gst / 100;
			$rider_val     = $rider_premium * $rider_gst / 100;
			$total_premium = round($premium_amount+$gst_val+$rider_premium+$rider_val);
		}else
		if($rider === 2){
			$gst_val       = $premium_amount * $gst / 100;
			$total_premium = round($premium_amount+$gst_val);
		}
		echo json_encode(array('success' => true, 'total_premium' =>$total_premium));
	}
	
	public function ecs_info(){
		$search_term    = $this->input->get('account_number');
		$proposer_name  = (int)$this->input->get('proposer_name');
		if($proposer_name > 0){
			$bank_query     = 'select bank_account_number,cw_bank.prime_bank_id,cw_bank.bank_name,bank_branch from cw_customer_bank_account inner join cw_bank on prime_bank_id = cw_customer_bank_account.bank_name where prime_customer_id like "'.$proposer_name.'%"';
			$bank_data   = $this->db->query("CALL sp_a_run ('SELECT','$bank_query')");
			$bank_result = $bank_data->result();
			$bank_data->next_result();
			foreach($bank_result as $result){
				$bank_account_number  = $result->bank_account_number;
				$prime_bank_id        = $result->prime_bank_id;
				$bank_name            = $result->bank_name;
				$bank_branch          = $result->bank_branch;
				$suggestions[] = array('label' =>"$bank_name - $bank_account_number ", 'bank_account_number' => $bank_account_number, 'prime_bank_id' => $prime_bank_id, 'bank_name' => $bank_name, 'bank_branch' => $bank_branch);
			}
			if(empty($suggestions)){
				$suggestions[] = array('value' => "0", 'label' => "No data found for this search");
			}			
		}else{
			$suggestions[] = array('value' => "0", 'label' => "Please select proposer in policy entry");
		}
		echo json_encode($suggestions);
	}
	//COMPANY LIST
	public function get_company_list(){
		$company_query  = "SELECT DISTINCT(prime_company_id),company_name FROM cw_product INNER join cw_company on prime_company_id = product_company where cw_product.trans_status = '1' and product_category = '1'";
		$company_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$company_query.'")');
		$company_result = $company_info->result();
		$company_info->next_result();
		echo json_encode(array('success' => TRUE, 'company_result' => $company_result));
	}
	//PRODUCT LIST
	public function get_product_list(){
		$company = $this->input->post('company');
		$product_query  = "SELECT prime_product_id,product_name FROM `cw_product` where product_category = '1' and product_company = '$company' and trans_status = '1'";
		$product_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$product_query.'")');
		$product_result = $product_info->result();
		$product_info->next_result();
		echo json_encode(array('success' => TRUE, 'product_result' => $product_result));
	}
	/* UDY CUSTOME BLOCK  - END*/
}
?>