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/Health_insurance.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Health_insurance  extends Base_controller{
	
	public function __construct(){
		parent::__construct('health_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;
			if((int)$prime_form_view_id === 49){ //UDY CUSTOME BLOCK
				$row_set_data = $this->get_members_save_data($prime_form_view_id,$form_view_id);
			}else{
				$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;
		
		$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";  }
		
		$this->select_query   .= ",".$this->prime_table .".current_policy_no";
		$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_health_insurance_id;
			$is_exist_qry  = 'SELECT count(*) as rslt_count FROM cw_health_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 CUSTOM BLOCK - START*/
	public function get_policy_to(){
		$policy_from   = $this->input->post('policy_from');
		$policy_from  = new DateTime($policy_from);
		$policy_from  = $policy_from->modify("+1 year");
		$policy_to    = $policy_from->modify("-1 day");
		$policy_to    = $policy_to->format("d-m-Y");
		echo json_encode(array('success' => true, 'policy_to' => $policy_to));
	}
	public function get_policy_covered_year(){		
		$policy_from  = $this->input->post('policy_from');
		$policy_to    = $this->input->post('policy_to');
		
		$policy_from = new DateTime($policy_from);
		$policy_from = $policy_from->format("Y-m-d");
		
		$policy_to = new DateTime($policy_to);
		$policy_to = $policy_to->modify("+1 day");
		$policy_to = $policy_to->format("Y-m-d");

		
		$date_parts1    = explode("-", $policy_from);
		$date_parts2    = explode("-", $policy_to);
		$start_date     = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date       = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
		$diff           = abs($end_date - $start_date);
		$diff = $diff+1;
		if((int)$diff === 365){
			$covered_year  = 1;
		}else{
			$covered_year  = floor($diff / 365.25);
		}
		echo json_encode(array('success' => true, 'diff' => $diff ,'covered_year' => $covered_year));
	}
	
	//SAVE MEMBERS COVERED
	public function members_save(){
		$view_id         = $this->input->post('view_id');
		$module_id       = $this->input->post('module_id');
		$row_prime_id    = (int)$this->input->post('row_prime_id');
		$row_label_name  =  $this->input->post('row_label_name');
		$prime_id        = (int)$this->input->post('prime_id');
		
		$table_name      = $module_id."_".$row_label_name;
		$table_prime     = "prime_".$table_name."_id";
		$table_name      = $this->db->dbprefix($table_name);
		
		$prime_qry_key   = "prime_".$module_id."_id,";
		$prime_qry_value = '"'.$prime_id.'",';
		$prime_upd_query = "";

		$form_qry  = 'select * from cw_form_setting where prime_module_id = "'.$module_id.'" and  input_for = "'.$view_id.'" and  field_show = "1" 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 $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;
			if((int)$field_type === 7){
				$multi_name = $label_id."[]";
				$value = implode(",",$this->input->post($multi_name));
			}else{
				$value = $this->input->post($label_id);
			}			
			if((int)$field_type === 4){
				$value = date('Y-m-d',strtotime($value));
			}			
			$prime_qry_key     .= $label_id.",";
			$prime_qry_value   .= '"'.$value.'",';
			$prime_upd_query   .= $label_id.' = "'.$value.'",';
		}
		$created_on = date("Y-m-d h:i:s");
		if((int)$row_prime_id === 0){
			$prime_qry_key     .= "trans_created_by,trans_created_date";
			$prime_qry_value   .= '"'.$this->logged_id.'",'.'"'.$created_on.'"';
			$prime_insert_query = "insert into $table_name ($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;
			$row_set_data = $this->get_members_save_data($view_id,$prime_id);
			echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id, 'row_set_data' => $row_set_data));
		}else{
			$prime_upd_query    .= 'trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
			$prime_update_query  = "UPDATE $table_name SET ". $prime_upd_query .' WHERE '. $table_prime .' = "'. $row_prime_id .'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')");
			$row_set_data = $this->get_members_save_data($view_id,$prime_id);
			echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'insert_id' => $row_prime_id,'row_set_data' => $row_set_data));
		}
	}
	
	//MEMBERS COVERED REMOVE DATA
	public function row_set_remove(){
		$row_id          = (int)$this->input->post('row_id');
		$view_id         = (int)$this->input->post('view_id');
		$table_name      = $this->input->post('table_name');
		$prime_id        = $this->input->post('prime_id');
		$table_prime_id  =	 "prime_".$table_name."_id";
		$table_name      = $this->db->dbprefix($table_name);
		$logged_id     = $this->session->userdata('logged_id');		
		$today_date = date("Y-m-d h:i:s");
		$final_qry = 'UPDATE '.$table_name.' SET trans_updated_by = "'.$logged_id.'",trans_updated_date = "'.$today_date.'" , trans_status = 0 WHERE '.$table_prime_id.' = "'.$row_id.'"';
		$this->db->query("CALL sp_a_run ('SELECT','$final_qry')");
		$row_set_data = $this->get_members_save_data($view_id,$prime_id);
		echo json_encode(array('success' => TRUE, 'msg' => "Remove Successfully",'row_set_data' => $row_set_data));
	}
	
	//GET MEMBERS COVERED DATA
	public function get_members_save_data($view_id,$prime_id){
		$members_covered = $this->members_covered_sum($prime_id);
		$policy_type   = (int)$members_covered['policy_type'];
		$sum_assured   = (int)$members_covered['sum_assured'];
		$total_premium = $members_covered['total_premium'];
		
		$view_qry    = 'select * from cw_form_view_setting where prime_form_view_id = "'.$view_id.'" and 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();
		$prime_form_view_id   = $view_result[0]->prime_form_view_id;
		$prime_view_module_id = $view_result[0]->prime_view_module_id;
		$form_view_label_name = $view_result[0]->form_view_label_name;
		
		$div_id       	 = $form_view_label_name."_div_".$prime_form_view_id;
		$table_id        = $form_view_label_name."_tbl_".$prime_form_view_id;
		$table_name      = $this->control_name."_".$form_view_label_name;
		$row_prime_id    = "prime_".$table_name."_id";
		$table_name      =  $this->db->dbprefix($table_name);
		$table_prime_id  = "prime_".$this->control_name."_id";
		
		$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 = "'.$this->control_name.'" and  input_for = "'.$prime_form_view_id.'" and  input_view_type = "3" and table_show = "1" 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();
		$table_head = array();
		$thead_line = "";
		$select_query = "$table_name.$row_prime_id,$table_name.$table_prime_id,";
		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{
				$select_query .= "$table_name.$label_name , ";
			}
			
			if((int)$policy_type === 1){
				$table_head[] = $label_name;
				$thead_line  .= "<th>$view_name</th>";
			}else
			if((int)$policy_type === 2){
				if(($label_name === "mem_sum_assured") || ($label_name === "total_premium")){
					// UDY FIND ALTERNATE
				}else{
					$table_head[] = $label_name;
					$thead_line  .= "<th>$view_name</th>";
				}
			}			
		}
		$thead = "<tr>$thead_line<th>Option</th></tr>";
		
		$select_query = rtrim($select_query,',');
		$select_query = rtrim($select_query,' , ');
		$final_qry    = "select $select_query from $table_name $pick_query " .' where '.$table_name.'.'.$table_prime_id.' = "'.$prime_id.'" and '.$table_name.'.trans_status = "1" order by abs('.$table_name.'.'.$row_prime_id.') desc';
		$row_data     = $this->db->query("CALL sp_a_run ('SELECT','$final_qry')");
		$row_result   = $row_data->result();
		$row_data->next_result();
		$tr_line = "";
		
		foreach($row_result as $data){			
			$td_line = "";
			$count = 0;
			foreach($table_head as $label){
				$count++;
				$value = $data->$label;
				$td_line .= "<td>$value</td>";
			}
			$row_id   = $data->$row_prime_id;
			$tab_name = $this->control_name."_".$form_view_label_name;
			$edit_btn   = "<a class='btn btn-edit btn-xs row_btn' onclick = row_set_edit('$row_id','$tab_name','$prime_form_view_id');>Edit</a>";
			$remove_btn = "<a class='btn btn-danger btn-xs row_btn' onclick = row_set_remove('$row_id','$tab_name','$prime_form_view_id','$prime_id');>Delete</a>";
			$tr_line .= "<tr>$td_line<td>$edit_btn $remove_btn</td></tr>";
		}
		
		$end_date  = new DateTime();
		$end_date  = $end_date->format("Y-m-d");
		$fetch_qry    = 'SELECT start_date,end_date,gst,additional_gst,rider_gst FROM cw_product_type where cw_product_type.trans_status = 1 and  cw_product_type.product_type_category = 2 and end_date >= date("'.$end_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){
			$gst_val =  $fetch_result[0]->gst_val;
		}
		$tax_per     = (floatval($gst_val) / 100) + 1;
		$net_premium = round(floatval($total_premium) /floatval($tax_per),2);
		$gst_amount  = round(floatval($total_premium) - floatval($net_premium),2);
			
		$total_tr = "";
		
		if($policy_type === 1){	
			$col_span = (int)$count - 1;		
			$total_tr = "<tr>
							<td colspan='$col_span' style='text-align:right;font-weight:bold;color:#00b0eb;'>Net Premium</td>
							<td colspan='2' style='font-weight:bold;'>$net_premium</td>
						</tr>
						<tr>
							<td colspan='$col_span' style='text-align:right;font-weight:bold;color:#00b0eb;'>GST</td>
							<td colspan='2' style='font-weight:bold;'>$gst_amount</td>
						</tr>
						<tr>
							<td colspan='$col_span' style='text-align:right;font-weight:bold;color:#00b0eb;'>Total Premium</td>
							<td colspan='2' style='font-weight:bold;'>$total_premium</td>
						</tr>";
		}else
		if($policy_type === 2){
			$col_span = (int)$count - 1;
			$total_tr = "<tr>
							<td colspan='$col_span' style='text-align:right;font-weight:bold;color:#00b0eb;'>Sum Assured</td>
							<td colspan='2' style='font-weight:bold;'>$sum_assured</td>
						</tr>
						<tr>
							<td colspan='$col_span' style='text-align:right;font-weight:bold;color:#00b0eb;'>Net Premium</td>
							<td colspan='2' style='font-weight:bold;'>$net_premium</td>
						</tr>
						<tr>
							<td colspan='$col_span' style='text-align:right;font-weight:bold;color:#00b0eb;'>GST</td>
							<td colspan='2' style='font-weight:bold;'>$gst_amount</td>
						</tr>
						<tr>
							<td colspan='$col_span' style='text-align:right;font-weight:bold;color:#00b0eb;'>Total Premium</td>
							<td colspan='2' style='font-weight:bold;'>$total_premium</td>
						</tr>";
		}
		$row_set_view = "<table id='$table_id' class='table table-bordered' style='background-color: #FFFFFF; box-shadow: none;'>
							<thead>$thead</thead>
							<tbody>$tr_line $total_tr</tbody>
						</table>";
		return array('div_id' => $div_id, 'table_id' => $table_id,'row_set_view'=>$row_set_view);
	}
	
	//MEMBERS COVERED SUM
	public function members_covered_sum($prime_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 = '$prime_id'";
		$policy_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$policy_query.'")');
		$policy_result = $policy_info->result();
		$policy_info->next_result();
		$policy_type = (int)$policy_result[0]->policy_type;		
		$total_premium = 0;
		if($policy_type === 1){
			$members_query  = "SELECT IFNULL(SUM(sum_assured),0) AS mem_sum_assured,IFNULL(SUM(total_premium),0) AS total_premium FROM cw_health_insurance_members_covered inner join cw_sum_assured on prime_sum_assured_id = mem_sum_assured where prime_health_insurance_id = '$prime_id' and cw_health_insurance_members_covered.trans_status = '1'";
			$members_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$members_query.'")');
			$members_result = $members_info->result();
			$members_info->next_result();
			$total_premium   = $members_result[0]->total_premium;
			$mem_sum_assured = $members_result[0]->mem_sum_assured;
			
			$sum_assured_qry    = "SELECT * FROM cw_sum_assured where sum_assured = '$mem_sum_assured' and trans_status = '1'";
			$sum_assured_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$sum_assured_qry.'")');
			$sum_assured_result = $sum_assured_info->result();
			$sum_assured_count  = (int)$sum_assured_info->num_rows();
			$sum_assured_info->next_result();
			if($sum_assured_count === 0){
				$insert_query = "INSERT INTO cw_sum_assured(sum_assured) VALUES ('$mem_sum_assured')";
				$insert_info  = $this->db->query('CALL sp_a_run ("INSERT","'.$insert_query.'")');
				$insert_result      = $insert_info->result();
				$insert_info->next_result();
				$sum_assured = $insert_result[0]->ins_id;
				
				$insert_query = "INSERT INTO cw_sum_assured_cf(prime_sum_assured_id) VALUES ('$sum_assured')";
				$insert_info  = $this->db->query('CALL sp_a_run ("INSERT","'.$insert_query.'")');
				$insert_result      = $insert_info->result();
				$insert_info->next_result();
			}else{
				$sum_assured = $sum_assured_result[0]->prime_sum_assured_id;
			}
			$update_query = 'UPDATE cw_health_insurance SET sum_assured = "'.$sum_assured.'",premium = "'.$total_premium.'" WHERE prime_health_insurance_id = "'.$prime_id.'"';
			$update_info  = $this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			$update_info->next_result();
			
			$is_exist_qry     = 'SELECT * FROM cw_health_insurance_renewal where renewal_policy_id = "'.$prime_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();			
			$exist_rslt_count = (int)$is_exist_data->num_rows();
			$is_exist_data->next_result();
			if(($exist_rslt_count === 0) || ($exist_rslt_count === 1)){
				$prime_renewal_id = $exist_rslt[0]->prime_renewal_id;
				$update_query = 'UPDATE cw_health_insurance_renewal SET renewal_sum_assured = "'.$sum_assured.'",renewal_premium_amount = "'.$total_premium.'" WHERE prime_renewal_id = "'.$prime_renewal_id.'"';
				$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			}
		}else{
			$sum_assured   = (int)$policy_result[0]->sum_assured;
			$sum_assured_qry    = "SELECT * FROM cw_sum_assured where prime_sum_assured_id = '$sum_assured' and trans_status = '1'";
			$sum_assured_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$sum_assured_qry.'")');
			$sum_assured_result = $sum_assured_info->result();
			$sum_assured_info->next_result();
			$sum_assured   = $sum_assured_result[0]->sum_assured;
			$total_premium = (int)$policy_result[0]->premium;
		}
		return array('policy_type' => $policy_type, 'sum_assured' => $sum_assured, 'total_premium' => $total_premium);
	}
	
	//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 = '2'";
		$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 = '2' 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));
	}
	
	//MEMBERS INFORMATION
	public function get_cust_info(){
		$cust_id       = $this->input->post('cust_id');
		$prime_id_val  = $this->input->post('prime_id_val');
		$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 = $fetch_result[0]->dob;
		
		$policy_query  = str_replace("@SELECT","*",$this->base_query);
		$policy_query .= " where $this->prime_table.trans_status = 1 and $this->prime_table.$this->prime_id = '$prime_id_val'";
		$policy_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$policy_query.'")');
		$policy_result = $policy_info->result();
		$policy_info->next_result();
		$inception_date = $policy_result[0]->inception_date;
			
		$current_date  = new DateTime();
		$current_date  = $current_date->format("Y-m-d");
		$dob_date      = new DateTime($dob);
		$dob_date      = $dob_date->format("Y-m-d");
		$date_parts1   = explode("-", $current_date);
		$date_parts2   = explode("-", $dob_date);
		$start_date    = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date      = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
		$diff          = abs($end_date - $start_date);
		if((int)$diff === 365){
			$age  = 1;
		}else{
			$age  = floor($diff / 365.25);
		}
		
		$dob_date       = new DateTime();
		$dob_date       = $dob_date->format("Y-m-d");
		$inception_date = new DateTime($inception_date);
		$inception_date = $inception_date->format("Y-m-d");
		$date_parts1    = explode("-", $dob_date);
		$date_parts2    = explode("-", $inception_date);
		$start_date     = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date       = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
		$diff           = abs($end_date - $start_date);
		if((int)$diff === 365){
			$inception_age  = 1;
		}else{
			$inception_age  = floor($diff / 365.25);
		}
		$age_at_start   = (int)$age - (int)$inception_age;
		$date_of_birth  = new DateTime($dob);
		$date_of_birth  = $date_of_birth->format("d-m-Y");
		echo json_encode(array('success' => true, 'date_of_birth' => $date_of_birth, 'age_at_start' => $age_at_start));
	}
	
	//FETCH RENEWAL INFORMATION 
	public function renewal($policy_id){
		$policy_query   = str_replace("@SELECT",$this->select_query .',cw_health_insurance.current_policy_no',$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];
		
		$sum_assured_qry    = "SELECT * FROM cw_sum_assured where trans_status = '1'";
		$sum_assured_info   = $this->db->query('CALL sp_a_run ("SELECT","'.$sum_assured_qry.'")');
		$sum_assured_result = $sum_assured_info->result();
		$sum_assured_info->next_result();		
		$renewal_sum_assured[""] = "---- Sum Assured ----";
		foreach($sum_assured_result as $rslt){
			$renewal_sum_assured[$rslt->prime_sum_assured_id] = $rslt->sum_assured;
		}
		$data['renewal_sum_assured_list'] = $renewal_sum_assured;
		$data['next_renewal']   = $this->next_renewal_data($policy_id);
		$data['renewal_result'] =  $this->renewal_data($policy_id);	
		
		$data['view_info']      = $this->view_info;
		$data['form_info']      = $this->form_info;
		//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;
			$input_for       = (int)$setting->input_for;
			$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($input_for === 49){
				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;
			if((int)$prime_form_view_id === 49){ //UDY CUSTOME BLOCK
				$row_set_data = $this->get_members_save_data($prime_form_view_id,$policy_id);
			}else{
				$row_set_data = $this->get_row_set_data($prime_form_view_id,$policy_id);
			}			
			$row_view_list[$prime_form_view_id] = $row_set_data;
		}
		$data['row_view_list']   = $row_view_list;
		
		$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_health_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();
		$renewal_from_date      = $policy_result[0]->policy_from;
		$renewal_to_date        = $policy_result[0]->policy_to;
		$renewal_policy_no      = $policy_result[0]->policy_no;
		$renewal_premium_amount = $policy_result[0]->premium;
		$renewal_sum_assured    = $policy_result[0]->sum_assured;
		$renewal_covered_year   = $policy_result[0]->covered_year;
		$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_health_insurance_renewal (renewal_policy_id, renewal_from_date,renewal_to_date,renewal_covered_year,renewal_policy_no,renewal_premium_amount,renewal_sum_assured,trans_created_by, trans_created_date) VALUES ("'.$renewal_policy_id.'","'.$renewal_from_date.'","'.$renewal_to_date.'","'.$renewal_covered_year.'","'.$renewal_policy_no.'","'.$renewal_premium_amount.'","'.$renewal_sum_assured.'","'.$logged_id.'","'.$created_on.'")';
			$this->db->query("CALL sp_a_run ('RUN','$renewal_qry')");
			
			$update_query = 'UPDATE cw_health_insurance SET current_policy_no = "'.$renewal_policy_no.'" WHERE prime_health_insurance_id = "'.$renewal_policy_id.'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
		}else
		if((int)$exist_rslt[0]->rslt_count === 1){
			$renewal_upd_qry  = 'UPDATE  cw_health_insurance_renewal SET renewal_from_date = "'.$renewal_from_date.'",renewal_to_date = "'.$renewal_to_date.'",renewal_covered_year = "'.$renewal_covered_year.'",renewal_policy_no = "'.$renewal_policy_no.'",renewal_premium_amount = "'.$renewal_premium_amount.'",renewal_sum_assured = "'.$renewal_sum_assured.'",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')");
			$update_query = 'UPDATE cw_health_insurance SET current_policy_no = "'.$renewal_policy_no.'" WHERE prime_health_insurance_id = "'.$renewal_policy_id.'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
		}
	}
	
	//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_from_date        =  date('Y-m-d',strtotime($this->input->post('renewal_from_date')));
		$renewal_to_date          =  date('Y-m-d',strtotime($this->input->post('renewal_to_date')));
		$renewal_covered_year     = $this->input->post('renewal_covered_year');
		$renewal_policy_no        = $this->input->post('renewal_policy_no');
		$renewal_premium_amount   = $this->input->post('renewal_premium_amount');
		$renewal_sum_assured      = $this->input->post('renewal_sum_assured');
		$renewal_ncp              = $this->input->post('renewal_ncp');
		$renewal_ncp_amount       = $this->input->post('renewal_ncp_amount');
		$renewal_note             = $this->input->post('renewal_note');
		$logged_id                = $this->session->userdata('logged_id');
		$created_on = date("Y-m-d h:i:s");	
		
			
		if($prime_renewal_id === 0){
			$is_exist_qry  = 'SELECT count(*) as rslt_count FROM cw_health_insurance_renewal where renewal_policy_id = "'.$renewal_policy_id.'" and renewal_policy_no = "'.$renewal_policy_no.'" 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_health_insurance_renewal (renewal_policy_id, renewal_from_date,renewal_to_date,renewal_covered_year,renewal_policy_no,renewal_premium_amount,renewal_sum_assured,renewal_ncp,renewal_ncp_amount,renewal_note,trans_created_by, trans_created_date) VALUES ("'.$renewal_policy_id.'","'.$renewal_from_date.'","'.$renewal_to_date.'","'.$renewal_covered_year.'","'.$renewal_policy_no.'","'.$renewal_premium_amount.'","'.$renewal_sum_assured.'","'.$renewal_ncp.'","'.$renewal_ncp_amount.'","'.$renewal_note.'","'.$logged_id.'","'.$created_on.'")';
				$this->db->query("CALL sp_a_run ('RUN','$renewal_qry')");
				
				$is_exist_qry  = 'SELECT IFNULL(sum(renewal_covered_year),0) as renewal_covered_year  FROM cw_health_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();
				$tot_covered_year = $exist_rslt[0]->renewal_covered_year;
				$update_query = 'UPDATE cw_health_insurance SET current_policy_no = "'.$renewal_policy_no.'",covered_year = "'.$tot_covered_year.'" WHERE prime_health_insurance_id = "'.$renewal_policy_id.'"';
				$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
				
				$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", 'next_renewal' => $next_renewal, 'renewal_list' => $renewal_list));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Renewal already exist"));
			}
		}else{
			$renewal_upd_qry  = 'UPDATE  cw_health_insurance_renewal SET renewal_from_date = "'.$renewal_from_date.'",renewal_to_date = "'.$renewal_to_date.'",renewal_covered_year = "'.$renewal_covered_year.'",renewal_policy_no = "'.$renewal_policy_no.'",renewal_premium_amount = "'.$renewal_premium_amount.'",renewal_sum_assured = "'.$renewal_sum_assured.'",renewal_ncp = "'.$renewal_ncp.'",renewal_ncp_amount = "'.$renewal_ncp_amount.'",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')");
			
			$is_exist_qry  = 'SELECT IFNULL(sum(renewal_covered_year),0) as renewal_covered_year  FROM cw_health_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();
			$tot_covered_year = $exist_rslt[0]->renewal_covered_year;


			$update_query = 'UPDATE cw_health_insurance SET current_policy_no = "'.$renewal_policy_no.'",covered_year = "'.$tot_covered_year.'" WHERE prime_health_insurance_id = "'.$renewal_policy_id.'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			$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", 'next_renewal' => $next_renewal, 'renewal_list' => $renewal_list));
		}
	}
	//GET NEXT RENEWAL DATA
	public function next_renewal_data($renewal_policy_id){
		$renewal_query  = "select * from cw_health_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];		
		$policy_from                      = new DateTime($next_renewal->renewal_to_date);
		$from_date                        = $policy_from->modify("+1 day");
		$next_renewal->renewal_from_date  = $from_date->format("d-m-Y");
		$policy_to                        = $policy_from->modify("+1 year");
		$policy_to                        = $policy_to->modify("-1 day");
		$next_renewal->renewal_to_date    = $policy_to->format("d-m-Y");
		$next_renewal->renewal_policy_no  = "";
		$next_renewal->renewal_ncp        = 2;
		$next_renewal->renewal_ncp_amount = "";
		$next_renewal->renewal_note       = "";
		$date_now   = new DateTime();
		$date_now   = $date_now->modify("+1 months");
		$check_date = new DateTime($next_renewal->renewal_from_date);
		$next_renewal->btn_sts = true;
		if($check_date > $date_now) {
			$next_renewal->btn_sts = false;
		}
		
		
		$policy_from = new DateTime($next_renewal->renewal_from_date);
		$policy_from = $policy_from->format("Y-m-d");		
		$policy_to   = new DateTime($next_renewal->renewal_to_date);
		$policy_to   = $policy_to->modify("+1 day");
		$policy_to   = $policy_to->format("Y-m-d");
		
		$date_parts1    = explode("-", $policy_from);
		$date_parts2    = explode("-", $policy_to);
		$start_date     = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
		$end_date       = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
		$diff           = abs($end_date - $start_date);
		if((int)$diff === 365){
			$covered_year  = 1;
		}else{
			$covered_year  = floor($diff / 365.25);
		}		
		$next_renewal->renewal_covered_year = $covered_year;
		return $next_renewal;
	}
	//EDIT RENEWAL
	public function renewal_edit(){
		$prime_renewal_id = (int)$this->input->post('prime_renewal_id');
		$renewal_query  = "select *,DATE_FORMAT(renewal_from_date, '%d-%m-%Y') AS renewal_from_date,DATE_FORMAT(renewal_to_date, '%d-%m-%Y') AS renewal_to_date  from cw_health_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_health_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_to_date(){
		$policy_from      = $this->input->post('renewal_from_date');	
		$policy_from      = new DateTime($policy_from);
		$policy_from      = $policy_from->modify("+1 year");
		$policy_to        = $policy_from->modify("-1 day");
		$renewal_to_date  = $policy_to->format("d-m-Y");
		echo json_encode(array('success' => TRUE,'renewal_to_date' => $renewal_to_date));
	}
	//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();		
		$policy_from   = $policy_result[0]->policy_from;
	
		$renewal_query  = "select * from cw_health_insurance_renewal where renewal_policy_id = '$renewal_policy_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();
		$tr_line = "";
		$count = 0;
		$rslt_count = count($renewal_result);
		$renewal_ncb_list = array('1'=>"Yes",'2'=>"No");
		foreach($renewal_result as $rslt){
			$count++;
			$prime_renewal_id        = $rslt->prime_renewal_id;
			$renewal_policy_id       = $rslt->renewal_policy_id;
			$renewal_from_date       = date('d-m-Y',strtotime($rslt->renewal_from_date));
			$renewal_to_date         = date('d-m-Y',strtotime($rslt->renewal_to_date));
			$renewal_policy_no       = $rslt->renewal_policy_no;
			$renewal_premium_amount  = $rslt->renewal_premium_amount;
			$renewal_sum_assured     = $rslt->renewal_sum_assured;
			$renewal_ncp             = $rslt->renewal_ncp;
			$renewal_ncp_amount      = $rslt->renewal_ncp_amount;
			$renewal_note            = $rslt->renewal_note;			
			$renewal_ncp             = $renewal_ncb_list[$renewal_ncp];
			
			$sum_assured_qry     = "SELECT * FROM cw_sum_assured where prime_sum_assured_id = '$renewal_sum_assured' and trans_status = '1'";
			$sum_assured_info    = $this->db->query('CALL sp_a_run ("SELECT","'.$sum_assured_qry.'")');
			$sum_assured_result  = $sum_assured_info->result();
			$sum_assured_info->next_result();
			$renewal_sum_assured = $sum_assured_result[0]->sum_assured;
			
			$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($rslt_count === $count){
				$td_btn = "$edit_btn $remove_btn";
			}
			if($count === 1){
				$td_btn = "";
			}
			$tr_line .= "<tr>
							<td>$count</td>
							<td style='white-space:nowrap;'>$renewal_from_date</td>
							<td style='white-space:nowrap;'>$renewal_to_date</td>
							<td>$renewal_policy_no</td>
							<td>$renewal_sum_assured</td>
							<td>$renewal_premium_amount</td>
							<td>$renewal_ncp</td>
							<td>$renewal_ncp_amount</td>
							<td>$renewal_note</td>
							<td style='white-space:nowrap;'>$td_btn</td>
						</tr>";
		}
		$renewal_list = "<table id='renewal_list' class='table table-bordered' style='background-color: #FFFFFF; box-shadow: none;'>
							<thead>
								<tr>
									<th>Paying Year</th>
									<th>Policy From</th>
									<th>Policy To</th>
									<th>Policy No</th>
									<th>Sum Assured</th>
									<th>Premium Amount</th>
									<th>NCP</th>
									<th>NCP Amount</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 policy_no_exist(){
		$policy_no     = trim($this->input->post('policy_no'));
		$exist_query  = 'SELECT count(*) as policy_count FROM cw_health_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{
			$exist_query  = 'SELECT count(*) as policy_count FROM cw_health_insurance_renewal where trans_status = 1 and  renewal_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 in renewal"));
			}else{
				echo json_encode(array('success' => true, 'message' => "New Policy number"));
			}			
		}
	}
	/* UDY CUSTOM BLOCK - END*/
}
?>