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/login_cafsindia_com/application/controllers/Health_bk02AUG2025.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Health  extends Action_controller{	
	public function __construct(){
		parent::__construct('health');
		$this->collect_base_info();
	}
	
	// LOAD PAGE QUICK LINK,FILTERS AND TABLE HEADERS
	public function index(){
		$data['quick_link']     = $this->quick_link;
		$data['table_head']     = $this->table_head;
		$data['master_pick']    = $this->master_pick;
		$data['fliter_list']    = $this->fliter_list;
		
		$data['login_rslt_arr'] = $this->log_based_operation();
		$this->load->view("$this->control_name/manage",$data);
	}
	public function log_based_operation(){
		$login_qry 		        = 'select prime_health_id,login_date,login_status from cw_health where trans_status = 1'; 
		$login_data   	        = $this->db->query("CALL sp_a_run ('SELECT','$login_qry')");
		$login_rslt 	        = $login_data->result_array();
		$login_data->next_result();
		$login_rslt             = array_reduce($login_rslt, function($result, $arr){			
		    $result[$arr['prime_health_id']] = $arr;
		    return $result;
		}, array());

		return $login_rslt; 
	}
	//LOAD TABEL WITH FILTERS
	public function search(){
		$draw         = $this->input->post('draw');
		$start        = $this->input->post('start');
		$per_page     = $this->input->post('length');
		$order        = $this->input->post('order');
		$order_col    = $this->input->post('columns');
		$search       = $this->input->post('search');
		$column       = $order[0]['column'];
		$order_sor    = $order[0]['dir'];
		$order_col    = $order_col[$column]['data'];
		$search       = trim($search['value']);
		$search_query = str_replace("@SELECT@",$this->select_query,$this->base_query);
		
		//ADDED BASIC,FILTER,COMMON QUERY HERE 
		$role_condition   = "";
		if($this->role_condition){
			$role_condition = $this->role_condition;
		}
		
		$fliter_query = "";
		foreach($this->fliter_list as $fliter){
			$label_id         = $fliter['label_id'];
			$label_name       = $fliter['label_name'];
			$field_isdefault  = (int)$fliter['field_isdefault'];
			$array_list       = $fliter['array_list'];
			$field_type       = (int)$fliter['field_type'];			
			if($field_isdefault === 1){
				$column_name = $this->prime_table .".$label_id";				
				$search_val  = $this->input->post("$label_id");
				if($search_val){
					if($field_type === 4){
						$search_val = date('Y-m-d',strtotime($search_val));
						$fliter_query .= " and $column_name = '$search_val'";
					}else
					if(($field_type === 5) || ($field_type === 7)){
						$search_val    = trim(implode(",",$search_val));
						$fliter_query .= " and $column_name in ('$search_val')";  
					}else
					if($field_type === 13){
						$search_val = date('Y-m-d H:i:s',strtotime($search_val));
						$fliter_query .= " and $column_name = '$search_val'";
					}else{
						$fliter_query .= " and $column_name LIKE '$search_val%'";
					}
				}
			}
		}
		
		$common_search = "";
		if($search){
					foreach($this->form_info as $setting){
						$prime_form_id   = $setting->prime_form_id;
						$field_type      = (int)$setting->field_type;
						$pick_list       = $setting->pick_list;
						$pick_table      = $setting->pick_table;
						$pick_list_type  = $setting->pick_list_type;
						$input_view_type = (int)$setting->input_view_type;
						$auto_prime_id      = $setting->auto_prime_id;
						$auto_dispaly_value = $setting->auto_dispaly_value;
						$label_id        = strtolower(str_replace(" ","_",$setting->label_name));
						$field_isdefault    = (int)$setting->field_isdefault;
						if($field_isdefault === 1){					
							if(($input_view_type === 1) || ($input_view_type === 2)){
								$search_label = "$this->prime_table.$label_id";
								$search_val   = "";
								if($field_type === 4){ // having issues in date search
									if(strtotime($search)){
										$search_val = date('Y-m-d',strtotime($search));
										$common_search .= ' or '. $search_label .' like "'.$search_val.'%"';
									}
								}else
								if(($field_type === 5) || ($field_type === 7) || ($field_type === 9)){
									$result = array_filter($this->master_pick[$label_id], function ($item) use ($search) {
										if (stripos($item, $search) !== false) {
											return true;
										}
										return false;
									});
									if($result){
										$pick_key = implode('", "', array_keys($result));
										$common_search .= ' or '. $search_label .' in("'.$pick_key.'")';
									}
								}
								else{
									$common_search .= ' or '. $search_label .' like "'.$search.'%"';
								}
							}
						}
					}
				
		                        if($common_search){
		                                $common_search = ltrim($common_search,' or ');
		                                $common_search = " and ($common_search)";
						$common_search = str_replace("(,","(",$common_search);
						$common_search = str_replace("()","(0)",$common_search);
		                        }
				}
		$count_all_query    = str_replace("@SELECT@","count(*) as allcount",$this->base_query);		
		$search_total       = $this->db->query($count_all_query);
		$search_total_info  = $search_total->result();
		$total_count        = $search_total_info[0]->allcount;
		
		$count_query        = str_replace("@SELECT@","count(*) as allcount",$this->base_query);
		$count_query       .= " where $this->prime_table.trans_status = 1 $role_condition $fliter_query $common_search";
		$search_count       = $this->db->query($count_query);
		$search_info        = $search_count->result();
		$filtered_count     = $search_info[0]->allcount;
		
		$search_query      .= " where $this->prime_table.trans_status = 1 $role_condition $fliter_query $common_search";
		$search_query      .= " ORDER BY  $order_col $order_sor";
		if((int)$per_page !== -1){
			$search_query  .= " LIMIT  $start,$per_page";
		}		
		$search_data        = $this->db->query($search_query);
		$search_result      = $search_data->result();
		//echo "search_query :: \n$search_query\n";		
		echo json_encode(array("draw" => intval($draw),"recordsTotal" => $total_count,"recordsFiltered" => $filtered_count,"data" => $search_result));		
	}
	
	//LOAD MODEL PAGE VIEW WITH DATA
	public function view($form_view_id=-1){
		//VIEW, FORM INPUT
		$data['view_info']      	= $this->view_info;
		$data['form_info']      	= $this->form_info;	
		$data['form_view_script']   = (int)$form_view_id;	
		
		//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];
		
		// FANILY COUNT AND TABLE COUNT GETTING FOR ON LOAD VALIDATION -> GV START
		// FAMILY COUNT GETTING QRY
		$family_count_qry     ='SELECT cw_family_size.family_count FROM cw_family_size INNER JOIN cw_health ON cw_health.family_size = cw_family_size.prime_family_size_id WHERE prime_health_id =  "'. $form_view_id .'"';
		$family_count_info    = $this->db->query("CALL sp_a_run ('SELECT','$family_count_qry')");
		$family_count_rlst    = $family_count_info->result();
		$family_count_info->next_result();
		$family_count         = (int)$family_count_rlst[0]->family_count;
		$data['family_count'] = $family_count;

		$insured_person_table_qry   = 'SELECT COUNT(*)AS table_count  FROM `cw_health_insured_persons_details` WHERE prime_health_id="'. $form_view_id .'" AND trans_status=1';
		$insured_person_table_info  = $this->db->query("CALL sp_a_run ('SELECT','$insured_person_table_qry')");
		$insured_person_table_rlst  = $insured_person_table_info->result();
		$insured_person_table_info->next_result();
		$insured_person_table_count = (int)$insured_person_table_rlst[0]->table_count;
		$data['insured_person_table_count'] = $insured_person_table_count;

		// FANILY COUNT AND TABLE COUNT GETTING FOR ON LOAD VALIDATION -> GV CLOSE
		//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;

		$emp_qry    = 'select * from cw_employees where  prime_employees_id = "'.$this->logged_id.'" and trans_status = 1';
		$emp_data   = $this->db->query("CALL sp_a_run ('SELECT','$emp_qry')");
		$emp_result = $emp_data->result();
		$emp_data->next_result();
		$data['emp_result_details'] = json_decode(json_encode($emp_result[0]),true);
		
		$this->load->view("$this->control_name/form",$data);
	}
	
	//SAVE MODEL DATA TO DATA BASE
	public function save(){
		$unq_chk         = array();
		$prime_qry_key   = "";
		$prime_qry_value = "";
		$prime_upd_query = "";
		$cf_qry_key      = "";
		$cf_qry_value    = "";
		$cf_upd_query    = "";	
		$health_id      = $this->input->post('prime_health_id');
		$cf_has          = false;
		$form_id         = (int)$this->input->post($this->prime_id);
		$form_post_data  = array();	
		$db_name         = $this->config->item("db_name");
		//check policy number already exist or not
		$policy_no 		 = $this->input->post('policy_number');
		if($policy_no){
			$policy_num_qry='SELECT count(policy_number) policy_count from cw_health WHERE policy_number ="'.$policy_no.'" and trans_status = 1 and prime_health_id!="'.$form_id.'"';
			$policy_num_data      = $this->db->query("CALL sp_a_run ('SELECT','$policy_num_qry')");
			$policy_num_result    = $policy_num_data->result();
			$policy_num_data->next_result();
			$policy_count =$policy_num_result[0]->policy_count;
			if((int)$policy_count > 0){
				echo json_encode(array('success' => false, 'message' => "Policy Number Already Exists"));
				exit(0);
			}
		}
		//INFORMATION SCHEMA QUERY FOR GET A LIFE RENEWALS TABLE COLUMN NAME
		$health_renewals_qry      = $this->db->query("SELECT COLUMN_NAME AS col_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA ='$db_name' and TABLE_NAME = 'cw_health_renewals' and COLUMN_NAME not like '%trans%' and COLUMN_NAME not in ('prime_health_renewals_id','due_date','due_month','due_year','current_policy_no','covered_year','ncd','call_count','renewal_status','call_status','renewed_date','renewed_month','renewed_year','sub_disposition','claim_count','claim_status','month_count','grace_date')");
		$health_renewals_col      = $health_renewals_qry->result_array();
		$health_renewals_col_arr  = array_map(function($array){
				return implode("~",$array);
			},$health_renewals_col);

		//FOR INSERT A LIFE RENWALS TABLE AS SAME COL NAME OF PORTFOLIO TABLE
		$health_renewals_col_name = implode(',', $health_renewals_col_arr);
		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){
				if($value){
					$value = date('Y-m-d',strtotime($value));
				}
			}else
			if((int)$field_type === 8 || (int)$field_type === 1){//textbox and input box
				$value = str_replace('"',"#xdbquot#",$value);
				$value = str_replace("'","#xquot#",$value);
				$value = str_replace("&","#xxamp#",$value);
			}else
			if((int)$field_type === 13){
				$value = date('Y-m-d H:i:s',strtotime($value));
			}	
			if(($input_view_type === 1) || ($input_view_type === 2)){
				if((int)$field_isdefault === 1){
					$prime_qry_key     .= $label_id.",";
					$prime_qry_value   .= '"'.$value.'",';
					$prime_upd_query   .= $label_id.' = "'.$value.'",';
					if($unique_field === 1){
						$prime_unq_chk = $label_id.'= "'.$value.'"';
						$query = "select count(*) as rslt_count from $this->prime_table where $this->prime_id != $form_id and $prime_unq_chk";
						$unq_chk[] = array('label_id'=>$label_id,'view_name'=>$view_name,'query'=>$query,);
					}
				}
				if($label_id === "issuance_status"){
					$issuance_status = (int)$value;
				}else
				if($label_id === "issued_date"){
					$issued_date  = $value;
				}else
				if($label_id === "payment_mode"){
					$mode  = $value;
				}else
				if($label_id === "premium"){
					$premium = (int)$value;
				}else
				if($label_id === "plus_on_premium"){
					$plus_on_premium = (int)$value;
				}else
				if($label_id === "net_premium"){
					$net_premium     = (int)$value;
				}else
				if($label_id === "gst"){
					$gst             = $value;
				}else
				if($label_id === "total_premium"){
					$total_premium  = (int)$value;
				}else
				if($label_id === "sum_insured"){
					$sum_insured  = $value;
				}else
				if($label_id === "policy_from_date"){
					$policy_from_date  = $value;
				}else
				if($label_id === "next_due_date"){
					$next_due_date  = date("Y-m-d",strtotime($value));
				}else
				if($label_id === "policy_number"){
					$policy_number  = $value;
				}
				if($label_id === 'issuance_status'){
					$issuence_sts   = (int)$value;
				}
				if(array_search("$label_id",$health_renewals_col_arr)){
					$upd_col_name  .= 'cw_health_renewals.'.$label_id.' = cw_health.'.$label_id.','; 
				}
			}
		}
		$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");

			//ISSUANCE STATUS 2 THEN ADD A DUE DATE AND NEXT DUE DATE
			if($issuance_status === 1){
				$renewal_mode_list = array("1"=>"+1 year","2"=>"+6 months","3"=>"+1 months","4"=>"+2 years","5"=>"+3 months","6"=>"+3 years");
				
				$next_due      = $renewal_mode_list[$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->modify("-1 days");
				// $next_due_date = $next_due_date->format("Y-m-d");
				$policy_from_date    = new DateTime($policy_from_date);
				$due_date            = $policy_from_date->format("Y-m-d");
				$next_due_date       = $policy_from_date->modify("$next_due");
				$next_due_date       = $next_due_date->modify("-1 days");
				$next_due_date       = $next_due_date->format("Y-m-d");
				$next_due_mon        = date("F", strtotime("$next_due_date"));
				$next_due_year       = date("Y", strtotime("$next_due_date"));
				if($mode === "1"){
					$covered_year   = "1 Year";
				}else
				if($mode === "2"){
					$covered_year   = "6 Months";
				}else
				if($mode === "3"){
					$covered_year   = "1 Month";
				}else
				if($mode === "4"){
					$covered_year   = "2 Years";
				}else
				if($mode === "5"){
					$covered_year   = "3 Months";
				}else
				if($mode === "6"){
					$covered_year   = "3 Years";
				}
			}

			if((int)$form_id === 0){
				// $select_health_qry  = 'SELECT COUNT(*) as count FROM cw_health WHERE  cw_health.policy_number = "'.$policy_number.'"';
				// $select_health_info = $this->db->query("CALL sp_a_run ('SELECT','$select_health_qry')");
				// $select_health_rlst = $select_health_info->result();
				// $select_health_info->next_result();
				// $health_count       = (int)$select_health_rlst[0]->count;
				// if($health_count){
				// 	echo json_encode(array('success' => FALSE, 'message' => "Same Policy Number Already Exist!Please Check it..?"));
				// }else{
					$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;		

					if($insert_id && $issuance_status === 1){
						//CODE AND BELOW QUERY FOR INSERT A LIFE RENEWALS TABLE 
						$ins_health_renewals_qry     = 'insert into cw_health_renewals ('.$health_renewals_col_name.',due_date,due_month,due_year,current_policy_no,trans_created_by,trans_created_date) SELECT '.$health_renewals_col_name.',"'.$next_due_date.'","'.$next_due_mon.'","'.$next_due_year.'","'.$policy_number.'","'.$this->logged_id.'","'.$created_on.'" FROM cw_health where '.$this->prime_table.'.prime_health_id = "'.$insert_id.'"';
						//echo $ins_life_renewals_qry; die;
						$ins_health_renewals_info    = $this->db->query("CALL sp_a_run ('INSERT','$ins_health_renewals_qry')");
						$ins_health_renewals_rslt    = $ins_health_renewals_info->result();
						$ins_health_renewals_info->next_result();
						$renewal_ins_id              = $ins_health_renewals_rslt[0]->ins_id;	

						//INSERT A LIFE RENEWAL INSURANCE TABLE
						if((int)$renewal_ins_id){
							$ins_health_renewal_log_qry  = 'insert into cw_health_renewals_log (prime_health_id,prime_health_renewals_id,renewal_mode,renewal_covered_year,renewal_policy_no,renewal_from_date,renewal_to_date,renewal_due_date,renewal_premium_amount,renewal_plus_on_premium,renewal_gst,renewal_net_premium,renewal_total_premium,renewal_next_due_date,renewal_sum_insured,from_health,trans_created_by,trans_created_date) SELECT prime_health_id,prime_health_renewals_id,payment_mode,"'.$covered_year.'",	policy_number,policy_from_date,policy_to_date,"'.$due_date.'",premium,plus_on_premium,gst,net_premium,total_premium,"'.$next_due_date.'",sum_insured,"1","'.$this->logged_id.'","'.$created_on.'" FROM cw_health_renewals where cw_health_renewals.prime_health_id = "'.$insert_id.'" and cw_health_renewals.trans_status = 1';
							$ins_health_renewal_log_info = $this->db->query("CALL sp_a_run ('INSERT','$ins_health_renewal_log_qry')");
							$ins_health_renewal_log_rslt = $ins_health_renewal_log_info->result();
							$ins_health_renewal_log_info->next_result();

							//INSERT QUERY FOR INSERT A FAMILY DETAILS
							$ins_health_renewal_family_log_qry  = 'insert into cw_health_renewals_insured_detail_log (prime_health_id,prime_health_renewals_id,insured_name,relationship,insured_dob,insured_gender,ped,ped_remarks,premium,plus_on_premium,net_premium,gst,total_premium,mem_sum_insured,trans_created_by,trans_created_date) SELECT prime_health_id,"'.$renewal_ins_id.'",insured_name,relationship,insured_dob,insured_gender,ped,ped_remarks,"'.$premium.'","'.$plus_on_premium.'","'.$net_premium.'","'.$gst.'","'.$total_premium.'","'.$sum_insured.'","'.$this->logged_id.'","'.$created_on.'" FROM cw_health_insured_persons_details where cw_health_insured_persons_details.prime_health_id = "'.$insert_id.'" and cw_health_insured_persons_details.trans_status = 1';
							$ins_health_renewal_family_log_info = $this->db->query("CALL sp_a_run ('INSERT','$ins_health_renewal_family_log_qry')");
							$ins_health_renewal_family_log_rslt = $ins_health_renewal_family_log_info->result();
							$ins_health_renewal_family_log_info->next_result();
						}
					}	
					// $log_based_arr = $this->log_based_operation();
					// FANILY COUNT AND TABLE COUNT GETTING FOR VALIDATION -> GV START
					$family_count_qry	= 'SELECT cw_family_size.family_count FROM cw_family_size INNER JOIN cw_health ON cw_health.family_size=cw_family_size.prime_family_size_id WHERE prime_health_id =  "'. $insert_id .'"';
					$family_count_info	=  $this->db->query("CALL sp_a_run ('SELECT','$family_count_qry')");
					$family_count_rlst 	=  $family_count_info->result();
					$family_count_info->next_result();
					$family_count 		=  (int)$family_count_rlst[0]->family_count;

					$insured_person_table_qry	= 'SELECT COUNT(*)AS table_count  FROM `cw_health_insured_persons_details` WHERE prime_health_id="'. $insert_id .'" AND trans_status=1';
					$insured_person_table_info	=  $this->db->query("CALL sp_a_run ('SELECT','$insured_person_table_qry')");
					$insured_person_table_rlst 	=  $insured_person_table_info->result();
					$insured_person_table_info->next_result();
					$insured_person_table_count =  (int)$insured_person_table_rlst[0]->table_count;	
					echo json_encode(array('success' => TRUE, 'message' => "Successfully added", 'insert_id' => $insert_id,'table_count' => $insured_person_table_count,'family_count' => $family_count));
				// }
			}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')");

				// FANILY COUNT AND TABLE COUNT GETTING FOR VALIDATION -> GV START
				$family_count_qry	= 'SELECT cw_family_size.family_count FROM cw_family_size INNER JOIN cw_health ON cw_health.family_size=cw_family_size.prime_family_size_id WHERE prime_health_id =  "'. $form_id .'"';
				$family_count_info	=  $this->db->query("CALL sp_a_run ('SELECT','$family_count_qry')");
				$family_count_rlst 	=  $family_count_info->result();
				$family_count_info->next_result();
				$family_count 		=  (int)$family_count_rlst[0]->family_count;

				$insured_person_table_qry	= 'SELECT COUNT(*)AS table_count  FROM `cw_health_insured_persons_details` WHERE prime_health_id="'. $form_id .'" AND trans_status=1';
				$insured_person_table_info	=  $this->db->query("CALL sp_a_run ('SELECT','$insured_person_table_qry')");
				$insured_person_table_rlst 	=  $insured_person_table_info->result();
				$insured_person_table_info->next_result();
				$insured_person_table_count =  (int)$insured_person_table_rlst[0]->table_count;	
				// FANILY COUNT AND TABLE COUNT GETTING FOR VALIDATION -> GV CLOSE

				$health_renewal_log_qry  = 'SELECT COUNT(*) as count FROM cw_health_renewals_log WHERE (prime_health_id = "'.$form_id.'")';
				$health_log_info         = $this->db->query("CALL sp_a_run ('SELECT','$health_renewal_log_qry')");
				$health_log_rslt          = $health_log_info->result();
				$health_log_info->next_result();
				$health_log_count        = (int)$health_log_rslt[0]->count;
				if($health_log_count === 1){
					$upd_col_name            = rtrim($upd_col_name, ',');
					$upd_health_renewals_qry   = 'UPDATE cw_health_renewals INNER JOIN cw_health ON cw_health_renewals.prime_health_id = cw_health.prime_health_id SET '.$upd_col_name.',cw_health_renewals.due_date = "'.$next_due_date.'",cw_health_renewals.due_month = "'.$next_due_mon.'",cw_health_renewals.due_year = "'.$next_due_year.'",cw_health_renewals.trans_updated_by = "'. $this->logged_id .'",cw_health_renewals.trans_updated_date = "'.$created_on.'" where cw_health_renewals.prime_health_id = "'.$form_id.'"';
					$upd_health_renewals_info  = $this->db->query("CALL sp_a_run ('UPDATE','$upd_health_renewals_qry')");

					//UPDATE QUERY FOR UPDATE A LIFE INSURANCE RENEWAL
					$upd_health_renewals_log_qry   = 'UPDATE cw_health_renewals_log INNER JOIN cw_health_renewals ON cw_health_renewals_log.prime_health_renewals_id = cw_health_renewals.prime_health_renewals_id SET cw_health_renewals_log.renewal_mode = cw_health_renewals.payment_mode,cw_health_renewals_log.renewal_covered_year = "'.$covered_year.'",cw_health_renewals_log.renewal_due_date = "'.$due_date.'",cw_health_renewals_log.renewal_policy_no = cw_health_renewals.policy_number,cw_health_renewals_log.renewal_from_date = cw_health_renewals.policy_from_date,cw_health_renewals_log.renewal_to_date = cw_health_renewals.policy_to_date,cw_health_renewals_log.renewal_premium_amount = cw_health_renewals.premium,cw_health_renewals_log.renewal_plus_on_premium = cw_health_renewals.plus_on_premium,cw_health_renewals_log.renewal_net_premium = cw_health_renewals.net_premium,cw_health_renewals_log.renewal_gst = cw_health_renewals.gst,cw_health_renewals_log.renewal_total_premium = cw_health_renewals.total_premium,cw_health_renewals_log.renewal_next_due_date = "'.$next_due_date.'",cw_health_renewals_log.renewal_sum_insured = cw_health_renewals.sum_insured,cw_health_renewals_log.trans_updated_by = "'. $this->logged_id .'",cw_health_renewals_log.trans_updated_date = "'.$created_on.'" where cw_health_renewals_log.prime_health_id = "'.$form_id.'"';
					$upd_health_renewals_log_info  = $this->db->query("CALL sp_a_run ('UPDATE','$upd_health_renewals_log_qry')");
					
					if($issuence_sts === 9){
						$chk_qry        = 'select count(*) as count from cw_health_renewals_log where '. $this->prime_id .' = "'. $form_id .'"';
						$chk_qry_data   = $this->db->query("CALL sp_a_run ('SELECT','$chk_qry')");
						$chk_rslt       = $chk_qry_data->result();
						$chk_qry_data->next_result();
						$chk_count      = (int)$chk_rslt[0]->count;
						if($chk_count === 1){
							$upd_trans_sts_qry ='UPDATE cw_health_renewals JOIN cw_health_renewals_log ON cw_health_renewals.prime_health_renewals_id = cw_health_renewals_log.prime_health_renewals_id SET cw_health_renewals.trans_status = 0,cw_health_renewals_log.trans_status = 0 WHERE cw_health_renewals.'.$this->prime_id .' = "'. $form_id .'" and cw_health_renewals_log.'.$this->prime_id .' = "'. $form_id .'"';
							$this->db->query("CALL sp_a_run ('UPDATE','$upd_trans_sts_qry')");
						}	
					}
				}

				//UPDATE QUERY FOR UPDATE A HEALTH RENEWALS TABLE FROM HEALTH TABLE IN SAME HEALTH ID
				if($issuance_status === 1){
					$health_renewal_ins_log_qry  = 'SELECT COUNT(*) as count FROM cw_health_renewals_insured_detail_log WHERE prime_health_id = "'.$form_id.'"';
					$health_renewal_ins_log_info = $this->db->query("CALL sp_a_run ('SELECT','$health_renewal_ins_log_qry')");
					$health_renewal_ins_log_rlst = $health_renewal_ins_log_info->result();
					$health_renewal_ins_log_info->next_result();
					$health_renewal_ins_log_count = (int)$health_renewal_ins_log_rlst[0]->count;


					$select_health_renewal_qry  = 'SELECT COUNT(*) as count FROM cw_health_renewals WHERE (prime_health_id = "'.$form_id.'" or cw_health_renewals.policy_number = "'.$policy_number.'")';
					$select_health_renewal_info = $this->db->query("CALL sp_a_run ('SELECT','$select_health_renewal_qry')");
					$select_health_renewal_rlst = $select_health_renewal_info->result();
					$select_health_renewal_info->next_result();
					$health_renewal_count       = (int)$select_health_renewal_rlst[0]->count;
					if(!$health_renewal_count){
						// 	$upd_col_name            = rtrim($upd_col_name, ',');
						// 	$upd_health_renewals_qry   = 'UPDATE cw_health_renewals INNER JOIN cw_health ON cw_health_renewals.prime_health_id = cw_health.prime_health_id SET '.$upd_col_name.',cw_health_renewals.due_date = "'.$next_due_date.'",cw_health_renewals.due_month = "'.$next_due_mon.'",cw_health_renewals.due_year = "'.$next_due_year.'",cw_health_renewals.trans_updated_by = "'. $this->logged_id .'",cw_health_renewals.trans_updated_date = "'.$created_on.'" where cw_health_renewals.prime_health_id = "'.$form_id.'"';
						// 	$upd_health_renewals_info  = $this->db->query("CALL sp_a_run ('UPDATE','$upd_health_renewals_qry')");
						// // //UPDATE QUERY FOR UPDATE A LIFE INSURANCE RENEWAL
						// 	$upd_health_renewals_log_qry   = 'UPDATE cw_health_renewals_log INNER JOIN cw_health_renewals ON cw_health_renewals_log.prime_health_renewals_id = cw_health_renewals.prime_health_renewals_id SET cw_health_renewals_log.renewal_mode = cw_health_renewals.payment_mode,cw_health_renewals_log.renewal_covered_year = "'.$covered_year.'",cw_health_renewals_log.renewal_due_date = "'.$due_date.'",cw_health_renewals_log.renewal_policy_no = cw_health_renewals.policy_number,cw_health_renewals_log.renewal_from_date = cw_health_renewals.policy_from_date,cw_health_renewals_log.renewal_to_date = cw_health_renewals.policy_to_date,cw_health_renewals_log.renewal_premium_amount = cw_health_renewals.premium,cw_health_renewals_log.renewal_plus_on_premium = cw_health_renewals.plus_on_premium,cw_health_renewals_log.renewal_net_premium = cw_health_renewals.net_premium,cw_health_renewals_log.renewal_gst = cw_health_renewals.gst,cw_health_renewals_log.renewal_total_premium = cw_health_renewals.total_premium,cw_health_renewals_log.renewal_next_due_date = "'.$next_due_date.'",cw_health_renewals_log.renewal_sum_insured = cw_health_renewals.sum_insured,cw_health_renewals_log.trans_updated_by = "'. $this->logged_id .'",cw_health_renewals_log.trans_updated_date = "'.$created_on.'" where cw_health_renewals_log.prime_health_id = "'.$form_id.'"';
						// 	$upd_health_renewals_log_info  = $this->db->query("CALL sp_a_run ('UPDATE','$upd_health_renewals_log_qry')");
						// }else{
						
						//CODE AND BELOW QUERY FOR INSERT A LIFE RENEWALS TABLE 
						$ins_health_renewals_qry     = 'insert into cw_health_renewals ('.$health_renewals_col_name.',due_date,due_month,due_year,current_policy_no,trans_created_by,trans_created_date) SELECT '.$health_renewals_col_name.',"'.$next_due_date.'","'.$next_due_mon.'","'.$next_due_year.'","'.$policy_number.'","'.$this->logged_id.'","'.$created_on.'" FROM cw_health where '.$this->prime_table.'.prime_health_id = "'.$form_id.'"';
						$ins_health_renewals_info    = $this->db->query("CALL sp_a_run ('INSERT','$ins_health_renewals_qry')");
						$ins_health_renewals_rslt    = $ins_health_renewals_info->result();
						$ins_health_renewals_info->next_result();
						$renewal_ins_id              = $ins_health_renewals_rslt[0]->ins_id;	

						//INSERT A HEALTH RENEWAL LOG TABLE
						if((int)$renewal_ins_id){
							$ins_health_renewal_log_qry  = 'insert into cw_health_renewals_log (prime_health_id,prime_health_renewals_id,renewal_mode,renewal_covered_year,renewal_policy_no,renewal_from_date,renewal_to_date,renewal_due_date,renewal_premium_amount,renewal_plus_on_premium,renewal_gst,renewal_net_premium,renewal_total_premium,renewal_next_due_date,renewal_sum_insured,from_health,trans_created_by,trans_created_date) SELECT prime_health_id,prime_health_renewals_id,payment_mode,"'.$covered_year.'",	policy_number,policy_from_date,policy_to_date,"'.$due_date.'",premium,plus_on_premium,gst,net_premium,total_premium,"'.$next_due_date.'",sum_insured,"1","'.$this->logged_id.'","'.$created_on.'" FROM cw_health_renewals where cw_health_renewals.prime_health_id = "'.$form_id.'" and cw_health_renewals.trans_status = 1';
							$ins_health_renewal_log_info = $this->db->query("CALL sp_a_run ('INSERT','$ins_health_renewal_log_qry')");
							$ins_health_renewal_log_rslt = $ins_health_renewal_log_info->result();
							$ins_health_renewal_log_info->next_result();

							if(!$health_renewal_ins_log_count){
								//INSERT QUERY FOR INSERT A FAMILY DETAILS
								$ins_health_renewal_family_log_qry  = 'insert into cw_health_renewals_insured_detail_log (prime_health_id,prime_health_renewals_id,insured_name,relationship,insured_dob,insured_gender,ped,ped_remarks,premium,plus_on_premium,net_premium,gst,total_premium,mem_sum_insured,trans_created_by,trans_created_date) SELECT prime_health_id,"'.$renewal_ins_id.'",insured_name,relationship,insured_dob,insured_gender,ped,ped_remarks,"'.$premium.'","'.$plus_on_premium.'","'.$net_premium.'","'.$gst.'","'.$total_premium.'","'.$sum_insured.'","'.$this->logged_id.'","'.$created_on.'" FROM cw_health_insured_persons_details where cw_health_insured_persons_details.prime_health_id = "'.$form_id.'" and cw_health_insured_persons_details.trans_status = 1';
								$ins_health_renewal_family_log_info = $this->db->query("CALL sp_a_run ('INSERT','$ins_health_renewal_family_log_qry')");
								$ins_health_renewal_family_log_rslt = $ins_health_renewal_family_log_info->result();
								$ins_health_renewal_family_log_info->next_result();
							}
						}
					}
				}				
				// $log_based_arr = $this->log_based_operation();	
				echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'insert_id' => $form_id,'table_count' => $insured_person_table_count,'family_count' => $family_count));
			}
		}
	}
	
	public function due_date_fetch(){
		$policy_from_date  = $this->input->post('policy_from_date');
		$mode              = $this->input->post('mode');
		$renewal_mode_list = array("1"=>"+1 year","2"=>"+6 months","3"=>"+1 months","4"=>"+2 years","5"=>"+3 months","6"=>"+3 years");
		$next_due          = $renewal_mode_list[$mode];
		$policy_from_date  = new DateTime($policy_from_date);
		$due_date          = $policy_from_date->format("Y-m-d");
		$next_due_date     = $policy_from_date->modify("$next_due");
		$next_due_date     = $next_due_date->modify("-1 days");
		$next_due_date     = $next_due_date->format("d-m-Y");
		echo json_encode(array('success' => TRUE, 'next_due_date' => $next_due_date));
	}
	//UPDATE STATUS TO DELETE IN MODULE PRIMARY TABLE
	public function delete(){
		$delete_ids    = implode(",",$this->input->post('delete_ids'));
		$can_process   = TRUE;
		$delete_status = FALSE;
		if($this->check_delete_status()){
			$delete_status = TRUE;
			$check_table_query  = 'SELECT GROUP_CONCAT(prime_module_id) as prime_module_id,GROUP_CONCAT(label_name) as label_name from cw_form_setting WHERE pick_table = "'. $this->prime_table .'" and  trans_status = 1 ';
			$check_table_info   = $this->db->query("CALL sp_a_run ('SELECT','$check_table_query')");
			$check_table_rlst   = $check_table_info->row();
			$check_table_info->next_result();
			if($check_table_rlst->prime_module_id){
				$prime_module_id         = explode(",",$check_table_rlst->prime_module_id);
				$label_name              = explode(",",$check_table_rlst->label_name);
				$i                       = 0;
				$select_table            = '';
				$select_label            = '';
				$select_trans_status     = '';
				$select_where            = '';
				foreach($prime_module_id as $check_modules){
					$table_name            = "cw_".$check_modules;
					$table_rename          = $table_name."_$i";
					$select_table         .= "$table_rename.$label_name[$i],";
					$select_label         .= " $table_name $table_rename,";
					if((int)$i === 0){
						$select_trans_status  .= "( $table_rename.trans_status = 1";
						$select_where         .= " and ($table_rename.$label_name[$i] in ($delete_ids)";
					}else{
						$select_trans_status  .= " and $table_rename.trans_status = 1";
						$select_where         .= " or $table_rename.$label_name[$i] in ($delete_ids)";
					}
					$i++;
				}
				$select_trans_status .= ")";
				$select_where        .= ")";
				$select_table         = rtrim($select_table,',');
				$select_label         = rtrim($select_label,',');
				$check_module_query  .= 'SELECT '.$select_table.' from '.$select_label.' WHERE '.$select_trans_status.' '.$select_where.' LIMIT 0,1'; 
				$check_module_info   = $this->db->query("CALL sp_a_run ('SELECT','$check_module_query')");
				$values_count        = $check_module_info->num_rows();
				$check_module_info->next_result();
				if((int)$values_count > 0){
					$can_process   = False;
					$delete_status = False;
				}
			}
			if($delete_status){
				$delete_query  = 'DELETE FROM '. $this->prime_table .'  WHERE '. $this->prime_id .' in ('. $delete_ids .')';
				if($this->db->query("CALL sp_a_run ('RUN','$delete_query')")){
					$row_set_query   = 'SELECT form_view_label_name from cw_form_view_setting where form_view_type = "3" and prime_view_module_id = "'. $this->control_name .'" and trans_status = 1';
					$row_set_info    = $this->db->query("CALL sp_a_run ('SELECT','$row_set_query')");
					$row_count       = (int)$row_set_info->num_rows();
					$row_set_info->next_result();
					if($row_count !== 0){
						$row_set_result         = $row_set_info->result();
						$delete_table_name      = '';
						$delete_table_condition = '';
						foreach($row_set_result as $row_set){
							$row_set_table_name      = "cw_".$this->control_name."_".$row_set->form_view_label_name;
							$delete_table_name      .= "$row_set_table_name,";
							$delete_table_condition .= " $row_set_table_name.$this->prime_id  in ('$delete_ids') and";
						}
						$delete_table_name           = rtrim($delete_table_name,',');
						$delete_table_condition      = rtrim($delete_table_condition,'and');
						$delete_row_set_query  = 'DELETE FROM '. $delete_table_name .'  WHERE '. $delete_table_condition.'';
						$this->db->query("CALL sp_a_run ('RUN','$delete_row_set_query')");						
					}
					$can_process = False;
				}
				
			}
		}
		if($can_process){
			$created_on = date("Y-m-d h:i:s");
			$prime_upd_query    .= 'trans_deleted_by = "'. $this->logged_id .'",trans_deleted_date = "'.$created_on.'"';
			$prime_update_query  = 'UPDATE '. $this->prime_table .' SET trans_status = 0,'. $prime_upd_query .' WHERE '. $this->prime_id .' in ('. $delete_ids .')';
			if($this->db->query("CALL sp_a_run ('UPDATE','$prime_update_query')")){
				echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
			}else{
				echo json_encode(array('success' => FALSE, 'message' => "Unable to delete"));
			}
		}else
		if($delete_status){
			echo json_encode(array('success' => TRUE, 'message' => "Successfully Deleted"));
		}else{
			$modules = ucwords($check_table_rlst->prime_module_id);
			echo json_encode(array('success' => FALSE, 'message' => "Unable to delete, This value is already used in $modules modules"));
		}
	}
	
	//CHECK UNIQUE FIELD STATUS
	public function check_delete_status(){
		$check_delete_query  = 'SELECT GROUP_CONCAT(unique_field) as unique_field from cw_form_setting WHERE prime_module_id = "'. $this->control_name .'" and  trans_status = 1 ';
		$check_delete_info   = $this->db->query("CALL sp_a_run ('SELECT','$check_delete_query')");
		$check_delete_rlst   = $check_delete_info->row();
		$check_delete_info->next_result();
		$unique_info         = explode(",",$check_delete_rlst->unique_field);
		if(in_array('1', $unique_info)){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	//UPDATE STATUS TO DELETE FOR UPLOAD FILES or DOCUMENTS
		public function remove_file(){
		$prime_id_val  = $this->input->post('prime_id_val');
		$is_defult     = (int)$this->input->post('is_defult');
		$input_name     = $this->input->post('input_name');
		$table_name = '';
		if($is_defult === 1){
			$table_name = $this->prime_table;
		}else
		if($is_defult === 2){
			$table_name = $this->cf_table;
		}
		if($table_name){
			$created_on    = date("Y-m-d h:i:s");
			$set_query     = $input_name .' = "" ,trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
			$update_query  = 'UPDATE '.$table_name .' SET '. $set_query .' WHERE '. $this->prime_id .' = "'. $prime_id_val .'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			echo json_encode(array('success' => TRUE, 'message' => "Successfully updated"));
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to process your request"));
		}
	}
	public function rowset_remove_file(){
		$table_name  = $this->input->post('table_name_set');
		$row_id     = (int)$this->input->post('row_id');
		$input_name     = $this->input->post('input_name');
		$view_id     = (int)$this->input->post('view_id');
		$primes_id     = (int)$this->input->post('primes_id');
		$remove_cw = str_replace("cw_","",$table_name);
		$primary_id = "prime_".$remove_cw."_id";
		if($table_name){
			$created_on    = date("Y-m-d h:i:s");
			$set_query     = $input_name .' = "" ,trans_updated_by = "'. $this->logged_id .'",trans_updated_date = "'.$created_on.'"';
			$update_query  = 'UPDATE '.$table_name .' SET '. $set_query .' WHERE '.$primary_id.' = "'. $row_id .'"';
			$this->db->query("CALL sp_a_run ('UPDATE','$update_query')");
			$row_set_edit = $this->get_row_set_data($view_id,$primes_id);
			$div_id = $row_set_edit['div_id'];
			$table_id = $row_set_edit['table_id'];
			$row_set_view = $row_set_edit['row_set_view'];
			echo json_encode(array('success' => TRUE, 'message' => "Successfully updated",'div_id' => $div_id, 'table_id' => $table_id,'row_set_view'=>$row_set_view));
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Unable to process your request"));
		}
	}
	
	//IMPORT FILE VIEW INFORMATION
	public function import(){
		$data['module_id']     = $this->control_name;		
		$excel_format_qry = 'select prime_excel_format_id,excel_name from cw_util_excel_format where excel_module_id = "'.$this->control_name.'" and trans_status = 1';
		$excel_format   = $this->db->query("CALL sp_a_run ('SELECT','$excel_format_qry')");
		$excel_result    = $excel_format->result();
		$excel_format->next_result();
		$excel_format_drop[""] = "---- Excel Format ----";
		foreach($excel_result as $excel){
			$prime_excel_format_id = $excel->prime_excel_format_id;
			$excel_name            = $excel->excel_name;
			$excel_format_drop[$prime_excel_format_id] = $excel_name;
		}
		$data['excel_format_drop'] = $excel_format_drop;
		
		$this->load->view("$this->control_name/import",$data);
	}
	public function excel($module_id,$excel_format){
		$excel_format_qry = 'select excel_line_column_name,excel_line_value from cw_util_excel_format_line where excel_line_module_id = "'.$module_id.'" and prime_excel_format_id ="'.$excel_format.'" 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();

		require_once APPPATH."/third_party/PHPExcel.php";
		$obj = new PHPExcel();		
		//Set the first row as the header row
		foreach($excel_result as $excel){
			$excel_line_column_name = $excel->excel_line_column_name;
			$excel_line_value       = $excel->excel_line_value;
			$obj->getActiveSheet()->setCellValue($excel_line_value."1", $excel_line_column_name);
		}		
		// Rename worksheet name
		 $filename= $module_id.".xls"; //save our workbook as this file name
		 header('Content-Type: application/vnd.ms-excel'); //mime type
		 header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		 header('Cache-Control: max-age=0'); //no cache
		 
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		 //if you want to save it as .XLSX Excel 2007 format
		 $objWriter = PHPExcel_IOFactory::createWriter($obj, 'Excel5');
		 //force user to download the Excel file without writing it to server's HD
		 $objWriter->save('php://output');
		echo json_encode(array('success' => TRUE, 'output' => $excelOutput));
	}
	public function fetch_product(){
		$company    = $this->input->post('company');
		$product_id = $this->input->post('product');
		$product_qry = 'select prime_health_product_id,product_name from cw_health_product where company = "'.$company.'" and trans_status = 1';
		$product_info   = $this->db->query("CALL sp_a_run ('SELECT','$product_qry')");
		$product_result    = $product_info->result();
		$product_info->next_result();
		$products = "<option value=''>--- Select Product ---</option>";
		foreach($product_result as $product){
			$prime_health_product_id = $product->prime_health_product_id;
			$product_name            = $product->product_name;
			if((int)$product_id === (int)$prime_health_product_id){
				$selected = "selected";
			}else{
				$selected = "";
			}
			$products .= "<option value='$prime_health_product_id' $selected> $product_name </option>";
		}
		echo $products;
	}
	public function discount(){
		$discount         = (int)$this->input->post("discount");
		$login_date       = $this->input->post("login_date");
		$net_premium      = (int)$this->input->post("net_premium");
		$product          = (int)$this->input->post("product");
		$channel          = (int)$this->input->post("channel");
		$designation      = (int)$this->input->post("designation");
		$business_type    = (int)$this->input->post('business_type');
		$date             = date('Y-m-d',strtotime($login_date));
		$discount_qry     = 'select credit_percentage,non_credit_percentage,dis_source_credit_port,dis_non_source_credit_port from cw_health_product_credit_details where cw_health_product_credit_details.prime_health_product_id = "'.$product.'" and discount = "'.$discount.'" and from_date <="'.$date.'" and to_date >="'.$date.'" and cw_health_product_credit_details.trans_status = 1';
		$discount_info    = $this->db->query("CALL sp_a_run ('SELECT','$discount_qry')");
		$discount_result  = $discount_info->result();
		$discount_info->next_result();
		if($business_type === 2){
			$credit_percentage            = $discount_result[0]->dis_source_credit_port;
			$non_credit_percentage        = $discount_result[0]->dis_non_source_credit_port;
		}else{
			$credit_percentage            = $discount_result[0]->credit_percentage;
			$non_credit_percentage        = $discount_result[0]->non_credit_percentage;
		}
		$credit_percentage_count      = count($credit_percentage);
		$non_credit_percentage_count  = count($non_credit_percentage);

		$source_qry       = 'SELECT source_credit,non_source_credit,source_credit_port,non_source_credit_port FROM cw_health_product_product_credit WHERE cw_health_product_product_credit.prime_health_product_id = "'.$product.'" AND cr_from_date <="'.$date.'" AND cr_to_date >="'.$date.'" AND FIND_IN_SET("'.$designation.'",designation) AND FIND_IN_SET("'.$channel.'",channel_name) AND cw_health_product_product_credit.trans_status = 1';
		$source_info      = $this->db->query("CALL sp_a_run ('SELECT','$source_qry')");
		$source_result    = $source_info->result();
		$source_info->next_result();
		
		if($business_type === 2){
			$source_credit_percentage       = $source_result[0]->source_credit_port;
			$non_source_credit_percentage   = $source_result[0]->non_source_credit_port;
		}else{
			$source_credit_percentage       = $source_result[0]->source_credit;
			$non_source_credit_percentage   = $source_result[0]->non_source_credit;
		}
		$source_credit_count          = count($source_credit_percentage);
		$non_source_credit_count      = count($non_source_credit_percentage);
		if((int)$credit_percentage_count > 0){
			$percentage   = $net_premium * $credit_percentage/100;
		}else
		if((int)$source_credit_count > 0){
			$percentage   = $net_premium * $source_credit_percentage/100;
		}else{
			$percentage   = $net_premium * 100/100;
		}
		if((int)$non_credit_percentage_count > 0){
			$non_percentage = $net_premium * $non_credit_percentage/100;
		}else
		if((int)$non_source_credit_count > 0){
			$non_percentage = $net_premium * $non_source_credit_percentage/100;
		}else{
			$non_percentage = $net_premium * 100/100;
		}
		echo json_encode(array('success' => TRUE, 'percentage' => $percentage, 'non_percentage' => $non_percentage));
	}
	function family_size_count(){
		$family_size   = (int)$this->input->post('family_size');
		$health_id     = (int)$this->input->post('health_id');
		$family_qry    = 'select family_count from cw_family_size where prime_family_size_id="'.$family_size.'" and trans_status = 1';
		$family_info   = $this->db->query("CALL sp_a_run ('SELECT','$family_qry')");
		$family_result = $family_info->result();
		$family_info->next_result();
		$family_count = $family_result[0]->family_count;

		$family_member_qry    = 'select count(*) as family_member_count from cw_health_insured_persons_details where prime_health_id="'.$health_id.'" and trans_status = 1';
		$family_member_info   = $this->db->query("CALL sp_a_run ('SELECT','$family_member_qry')");
		$family_member_result = $family_member_info->result();
		$family_member_info->next_result();
		$family_member_count = $family_member_result[0]->family_member_count;
		
		if((int)$family_count <= (int)$family_member_count){
			echo json_encode(array('success' => TRUE, 'message' => "Family Size Count Matched"));
		}else{
			echo json_encode(array('success' => FALSE, 'message' => "Family Size Count Not Matched"));
		}
	}
}
?>