File: //home/cafsindia/wealth_cafsindia_com/application/controllers/Goals.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Goals extends Base_controller{
public function __construct(){
parent::__construct('goals');
if(!$this->Appconfig->isAppvalid()){
redirect('config');
}
$this->collect_base_info();
}
// LOAD PAGE WITH TABLE DATA
public function index(){
$data['table_headers']= $this->xss_clean(get_dbtable_headers($this->table_info));
$data['fliter_list'] = $this->fliter_list;
$data['link_info'] = $this->quick_link;
$this->load->view("$this->control_name/manage",$data);
}
//LOAD MODEL PAGE VIEW WITH DATA
public function view($form_view_id=-1){
//VIEW, FORM INPUT
$data['view_info'] = $this->view_info;
$data['form_info'] = $this->form_info;
//VIEW DATA
$base_query = str_replace("@SELECT",$this->view_select,$this->base_query);
$view_query = $base_query ." where $this->prime_table.$this->prime_id = $form_view_id and $this->prime_table.trans_status = 1";
$view_data = $this->db->query("CALL sp_a_run ('SELECT','$view_query')");
$view_result = $view_data->result();
$view_data->next_result();
$data['form_view'] = $view_result[0];
//AUTO COMPLTE,PICK LIST AND CONDITION
foreach($this->form_info as $from){
$prime_form_id = (int)$from->prime_form_id;
$field_type = (int)$from->field_type;
$pick_table = $from->pick_table;
$auto_prime_id = $from->auto_prime_id;
$auto_dispaly_value = $from->auto_dispaly_value;
$label_id = $from->label_name;
if($field_type === 9){
if($view_result[0]){
$get_value = $view_result[0]->$label_id;
if($get_value){
$pick_query = 'select '.$auto_dispaly_value.' from '.$pick_table.' where '.$auto_prime_id.' = "'.$get_value.'" and trans_status = 1';
$pick_data = $this->db->query("CALL sp_a_run ('SELECT','$pick_query')");
$pick_result = $pick_data->result();
$pick_data->next_result();
$this->all_pick[$prime_form_id] = $pick_result[0]->$auto_dispaly_value;
}
}
}
}
$data['all_pick'] = $this->all_pick;
$data['condition_list'] = $this->condition_list;
$view_qry = 'select * from cw_form_view_setting where prime_view_module_id = "'.$this->control_name.'" and form_view_type = "3" and trans_status = 1';
$view_data = $this->db->query("CALL sp_a_run ('SELECT','$view_qry')");
$view_result = $view_data->result();
$view_data->next_result();
$row_view_list = array();
foreach($view_result as $view){
$prime_form_view_id = $view->prime_form_view_id;
$row_set_data = $this->get_row_set_data($prime_form_view_id,$form_view_id);
$row_view_list[$prime_form_view_id] = $row_set_data;
}
$data['row_view_list'] = $row_view_list;
$this->load->view("$this->control_name/form",$data);
}
//IMPORT FILE VIEW INFORMATION
public function import(){
$data['module_id'] = $this->control_name;
$excel_format_qry = 'select prime_excel_format_id,excel_name from cw_util_excel_format where excel_module_id = "'.$this->control_name.'" and trans_status = 1';
$excel_format = $this->db->query("CALL sp_a_run ('SELECT','$excel_format_qry')");
$excel_result = $excel_format->result();
$excel_format->next_result();
$excel_format_drop[""] = "---- Excel Format ----";
foreach($excel_result as $excel){
$prime_excel_format_id = $excel->prime_excel_format_id;
$excel_name = $excel->excel_name;
$excel_format_drop[$prime_excel_format_id] = $excel_name;
}
$data['excel_format_drop'] = $excel_format_drop;
$this->load->view("$this->control_name/import",$data);
}
//LOAD PAGE TABLE VIEW WITH DATA BASED ON SEARCH FILTERS
public function search(){
$search = $this->input->get('search');
$limit = $this->input->get('limit');
$offset = $this->input->get('offset');
$sort = $this->input->get('sort');
$order = $this->input->get('order');
$fliter_query = "";
foreach($this->fliter_list as $fliter){
$label_id = $fliter['label_id'];
$label_name = $fliter['label_name'];
$field_isdefault = (int)$fliter['field_isdefault'];
$array_list = $fliter['array_list'];
$field_type = (int)$fliter['field_type'];
$condition_id = $label_id."_cond";
$label_id = "search_".$label_id."_cond";
if($field_isdefault === 1){
$column_name = $this->prime_table .".$label_id";
}else
if($field_isdefault === 2){
$column_name = $this->cf_table .".$label_id";
}
if($field_type === 4){
$search_val = $_GET[$label_id];
$search_val = date('Y-m-d',strtotime($search_val));
$condition = trim(implode(",",$_GET[$condition_id]));
}else
if(((int)$field_type === 5) || ((int)$field_type === 7)){
$search_val = trim(implode(",",$_GET[$label_id]));
$condition = "in";
}else{
$search_val = trim(implode(",",$_GET[$label_id]));
$condition = trim(implode(",",$_GET[$condition_id]));
}
if(($condition) && ($search_val)){
if($condition === "LIKE"){ $search_val = "$search_val%"; }
$column_name = str_replace("search_",'',$column_name);
$column_name = str_replace("_cond",'',$column_name);
if(((int)$field_type === 5) || ((int)$field_type === 7)){
$search_val = str_replace(",",'","',$search_val);
$fliter_query .= ' and '.$column_name." ". $condition .' ("'.$search_val.'")';
}else{
$fliter_query .= ' and '.$column_name." ". $condition .' "'.$search_val.'"';
}
}
}
$common_search = "";
if($search){
$count=0;
foreach($this->form_info as $setting){
$prime_form_id = $setting->prime_form_id;
$field_type = $setting->field_type;
$pick_list = $setting->pick_list;
$pick_table = $setting->pick_table;
$pick_list_type = $setting->pick_list_type;
$input_view_type = (int)$setting->input_view_type;
$auto_prime_id = $setting->auto_prime_id;
$auto_dispaly_value = $setting->auto_dispaly_value;
$label_id = strtolower(str_replace(" ","_",$setting->label_name));
$field_isdefault = (int)$setting->field_isdefault;
if($field_isdefault === 1){
$pick_sel_table = "$this->prime_table";
}else
if($field_isdefault === 2){
$pick_sel_table = "$this->cf_table";
}
$other_label_id = $pick_sel_table.".".$label_id;
if(($input_view_type === 1) || ($input_view_type === 2)){
if((int)$field_type === 4){
if(strtotime($search)){
$search_val = date('Y-m-d',strtotime($search));
$common_search .= ' or '. $other_label_id .' like "'.$search_val.'%"';
}
}else
if(((int)$field_type === 5) || ((int)$field_type === 7)){
if((int)$pick_list_type === 1){
$column_name = explode(",",$pick_list);
$column_name = $column_name[1];
}else{
$column_name = $pick_table."_value";
}
$pick_query_as = $pick_table."_".$prime_form_id;
$label_id = "$pick_query_as.$column_name";
$common_search .= ' or '. $label_id .' like "'.$search.'%"';
}else
if((int)$field_type === 9){
$pick_query_as = $pick_table."_".$prime_form_id;
$label_id = "$pick_query_as.$auto_dispaly_value";
$common_search .= ' or '. $label_id .' like "'.$search.'%"';
}else{
if(!strtotime($search)){
$common_search .= ' or '. $other_label_id .' like "'.$search.'%"';
}
}
}
}
if($common_search){
$common_search = ltrim($common_search,' or ');
$common_search = " and ($common_search)";
}
}
if(!$sort){ $sort = $this->prime_table.".".$this->prime_id; }
if(!$order){ $order = "asc"; }
$select_info = str_replace("@SELECT",$this->select_query,$this->base_query);
$search_query = $select_info. $this->pick_query;
//ADDED BASIC,FILTER,COMMON QUERY HERE
$basic_query = "";
if($this->table_search_info){
$basic_query = $this->table_search_info;
}
$search_query .= " where $this->prime_table.trans_status = 1 $basic_query $fliter_query $common_search";
$search_query .= " ORDER BY $sort $order";
$search_query .= " LIMIT $offset,$limit";
//FETCH RECORDS DATA
$search_data = $this->db->query("CALL sp_a_run ('SELECT','$search_query')");
$search_result = $search_data->result();
$num_rows = $search_data->num_rows();
$search_data->next_result();
$data_rows = array();
foreach ($search_result as $search){
$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();
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')");
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"));
}
}
public function update_investments_list(){
$map_from = $this->input->post('map_from[]');
$logged_role = $this->session->userdata('logged_role');
$where_query = "";
if((int)$logged_role === 12){
$logged_cust_ids = $this->session->userdata('logged_cust_ids');
$logged_cust_ids = '"'.str_replace(",",'","',$logged_cust_ids).'"';
$where_query = "and cw_customer.prime_customer_id in ($logged_cust_ids)";
}
$investments_list = array();
if(count($map_from) > 0){
foreach($map_from as $key => $mode){
if((int)$mode === 1){
// ONLY LIFE INSURANCE TRADITIONAL AND ULIP
$life_where = $where_query.' order by life_assured_name';
$life_insurance_query = 'SELECT cw_life_insurance.prime_life_insurance_id,short_name,cw_company.company_name AS comp_name,product_name,sum_assured FROM cw_customer INNER JOIN cw_customer_cf ON cw_customer.prime_customer_id = cw_customer_cf.prime_customer_id LEFT JOIN cw_life_insurance ON cw_life_insurance.life_assured_name = cw_customer.prime_customer_id LEFT JOIN cw_life_insurance_cf ON cw_life_insurance.prime_life_insurance_id = cw_life_insurance_cf.prime_life_insurance_id INNER JOIN cw_product ON cw_product.prime_product_id = cw_life_insurance.product INNER JOIN cw_company ON cw_company.prime_company_id = cw_product.product_company WHERE cw_life_insurance.trans_status = 1 and product_type != 3'.$life_where;
$life_insurance_data = $this->db->query("CALL sp_a_run ('SELECT','$life_insurance_query')");
$life_insurance_rslt = $life_insurance_data->result();
$life_insurance_data->next_result();
foreach($life_insurance_rslt as $life){
$prime_life_insurance_id = $life->prime_life_insurance_id;
$short_name = ucwords($life->short_name);
$comp_name = ucwords($life->comp_name);
$product_name = ucwords($life->product_name);
$sum_assured = $life->sum_assured;
$option_id = "INS_".$prime_life_insurance_id;
$option_name = "$short_name - $product_name - $sum_assured ";
$investments_list[$option_id] = $option_name;
}
}else
if((int)$mode === 2){
//BANK DEPOSIT
$deposit_scheme_list = array(1=>"Bank Fixed Deposit",2=>"Company Deposit",3=>"Bank Recurring Deposit",4=>"Bonds",5=>"Other Deposits");
$today_date = date("Y-m-d");
$bank_where = $where_query.' order by depositor_name,deposit_scheme';
$bank_query = 'SELECT cw_bank_deposit.prime_bank_deposit_id,short_name,depositor_name,deposit_scheme,principal_amount,compounding,start_date,tenure_value,tenure_mode,rate_of_return,maturity_date,maturity_amount FROM cw_customer INNER JOIN cw_customer_cf ON cw_customer.prime_customer_id = cw_customer_cf.prime_customer_id LEFT JOIN cw_bank_deposit ON cw_bank_deposit.depositor_name = cw_customer.prime_customer_id INNER JOIN cw_bank_deposit_cf ON cw_bank_deposit.prime_bank_deposit_id = cw_bank_deposit_cf.prime_bank_deposit_id where cw_bank_deposit.trans_status = 1 '.$bank_where;
$bank_data = $this->db->query("CALL sp_a_run ('SELECT','$bank_query')");
$bank_rslt = $bank_data->result();
$bank_data->next_result();
foreach($bank_rslt as $bank){
$prime_bank_deposit_id = (int)$bank->prime_bank_deposit_id;
$short_name = ucwords($bank->short_name);
$depositor_name = $bank->depositor_name;
$deposit_scheme = (int)$bank->deposit_scheme;
$deposit_scheme = $deposit_scheme_list[$deposit_scheme];
$principal_amount = $bank->principal_amount;
$maturity_amount = $bank->maturity_amount;
$option_id = "BK_".$prime_bank_deposit_id;
$option_name = "$short_name - $deposit_scheme - $maturity_amount</span>";
$investments_list[$option_id] = $option_name;
}
//GOVERNMENT SAVING
$gov_where = $where_query.'order by holder,scheme';
$gov_query = 'SELECT cw_government_investments.prime_government_investments_id,short_name,scheme_name,holder,scheme,paying_type,amount,start_date,maturity_date,rate_of_return,maturity_value FROM cw_customer INNER JOIN cw_customer_cf ON cw_customer.prime_customer_id = cw_customer_cf.prime_customer_id LEFT JOIN cw_government_investments ON cw_government_investments.holder = cw_customer.prime_customer_id INNER JOIN cw_government_investments_cf ON cw_government_investments.prime_government_investments_id = cw_government_investments_cf.prime_government_investments_id INNER JOIN cw_government_scheme ON cw_government_scheme.prime_government_scheme_id = cw_government_investments.scheme where cw_government_investments.trans_status = 1 '.$gov_where;
$gov_data = $this->db->query("CALL sp_a_run ('SELECT','$gov_query')");
$gov_rslt = $gov_data->result();
$gov_data->next_result();
foreach($gov_rslt as $gov){
$prime_government_investments_id = $gov->prime_government_investments_id;
$short_name = ucwords($gov->short_name);
$holder = $gov->holder;
$scheme_name = $gov->scheme_name;
$paying_type = $gov->paying_type;
$amount = $gov->amount;
$start_date = $gov->start_date;
$maturity_date = $gov->maturity_date;
$rate_of_return = $gov->rate_of_return;
$maturity_value = $gov->maturity_value;
$option_id = "GOV_".$prime_government_investments_id;
$option_name = "$short_name - $scheme_name - $maturity_value</span>";
$investments_list[$option_id] = $option_name;
}
//INVESTMENTS
$inv_where = $where_query.' order by investor';
$inv_query = 'SELECT cw_other_investments.prime_other_investments_id,short_name,investor,description,paying_type,amount,tenure_value,tenure,expected_returns,maturity_date,maturity_value,start_date FROM cw_customer INNER JOIN cw_customer_cf ON cw_customer.prime_customer_id = cw_customer_cf.prime_customer_id LEFT JOIN cw_other_investments ON cw_other_investments.investor = cw_customer.prime_customer_id INNER JOIN cw_other_investments_cf ON cw_other_investments.prime_other_investments_id = cw_other_investments_cf.prime_other_investments_id where cw_other_investments.trans_status = 1 '.$inv_where;
$inv_data = $this->db->query("CALL sp_a_run ('SELECT','$inv_query')");
$inv_rslt = $inv_data->result();
$inv_data->next_result();
foreach($inv_rslt as $inv){
$prime_other_investments_id = $inv->prime_other_investments_id;
$short_name = ucwords($inv->short_name);
$investor = $inv->investor;
$description = $inv->description;
$paying_type = $inv->paying_type;
$amount = $inv->amount;
$tenure_value = $inv->tenure_value;
$tenure = $inv->tenure;
$expected_returns = $inv->expected_returns;
$maturity_date = $inv->maturity_date;
$maturity_value = $inv->maturity_value;
$start_date = $inv->start_date;
$option_id = "INV".$prime_other_investments_id;
$option_name = "$short_name - $description - $maturity_value";
$investments_list[$option_id] = $option_name;
}
}else
if((int)$mode === 3){
// Stock, Bullion
//echo "Investments<br>";
}
}
//print_r($investments_list);
echo json_encode(array('success' => TRUE, 'investments_list' => $investments_list));
}else{
echo json_encode(array('success' => FALSE, 'message' => "Unable to process your request"));
}
}
}
?>